if_ethersubr.c revision 1.224 1 /* $NetBSD: if_ethersubr.c,v 1.224 2016/06/20 07:01:45 knakahara Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.224 2016/06/20 07:01:45 knakahara Exp $");
65
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_atalk.h"
69 #include "opt_mbuftrace.h"
70 #include "opt_mpls.h"
71 #include "opt_gateway.h"
72 #include "opt_pppoe.h"
73 #include "opt_net_mpsafe.h"
74 #endif
75
76 #include "vlan.h"
77 #include "pppoe.h"
78 #include "bridge.h"
79 #include "arp.h"
80 #include "agr.h"
81
82 #include <sys/sysctl.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/mutex.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/device.h>
89 #include <sys/rnd.h>
90 #include <sys/rndsource.h>
91 #include <sys/cpu.h>
92
93 #include <net/if.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
96 #include <net/if_llc.h>
97 #include <net/if_dl.h>
98 #include <net/if_types.h>
99 #include <net/pktqueue.h>
100
101 #include <net/if_media.h>
102 #include <dev/mii/mii.h>
103 #include <dev/mii/miivar.h>
104
105 #if NARP == 0
106 /*
107 * XXX there should really be a way to issue this warning from within config(8)
108 */
109 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
110 #endif
111
112 #include <net/bpf.h>
113
114 #include <net/if_ether.h>
115 #include <net/if_vlanvar.h>
116
117 #if NPPPOE > 0
118 #include <net/if_pppoe.h>
119 #endif
120
121 #if NAGR > 0
122 #include <net/agr/ieee8023_slowprotocols.h> /* XXX */
123 #include <net/agr/ieee8023ad.h>
124 #include <net/agr/if_agrvar.h>
125 #endif
126
127 #if NBRIDGE > 0
128 #include <net/if_bridgevar.h>
129 #endif
130
131 #include <netinet/in.h>
132 #ifdef INET
133 #include <netinet/in_var.h>
134 #endif
135 #include <netinet/if_inarp.h>
136
137 #ifdef INET6
138 #ifndef INET
139 #include <netinet/in.h>
140 #endif
141 #include <netinet6/in6_var.h>
142 #include <netinet6/nd6.h>
143 #endif
144
145
146 #include "carp.h"
147 #if NCARP > 0
148 #include <netinet/ip_carp.h>
149 #endif
150
151 #ifdef NETATALK
152 #include <netatalk/at.h>
153 #include <netatalk/at_var.h>
154 #include <netatalk/at_extern.h>
155
156 #define llc_snap_org_code llc_un.type_snap.org_code
157 #define llc_snap_ether_type llc_un.type_snap.ether_type
158
159 extern u_char at_org_code[3];
160 extern u_char aarp_org_code[3];
161 #endif /* NETATALK */
162
163 #ifdef MPLS
164 #include <netmpls/mpls.h>
165 #include <netmpls/mpls_var.h>
166 #endif
167
168 static struct timeval bigpktppslim_last;
169 static int bigpktppslim = 2; /* XXX */
170 static int bigpktpps_count;
171 static kmutex_t bigpktpps_lock __cacheline_aligned;
172
173
174 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
175 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
176 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
177 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
178 #define senderr(e) { error = (e); goto bad;}
179
180 static int ether_output(struct ifnet *, struct mbuf *,
181 const struct sockaddr *, const struct rtentry *);
182
183 /*
184 * Ethernet output routine.
185 * Encapsulate a packet of type family for the local net.
186 * Assumes that ifp is actually pointer to ethercom structure.
187 */
188 static int
189 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
190 const struct sockaddr * const dst,
191 const struct rtentry *rt)
192 {
193 uint16_t etype = 0;
194 int error = 0, hdrcmplt = 0;
195 uint8_t esrc[6], edst[6];
196 struct mbuf *m = m0;
197 struct mbuf *mcopy = NULL;
198 struct ether_header *eh;
199 struct ifnet *ifp = ifp0;
200 #ifdef INET
201 struct arphdr *ah;
202 #endif /* INET */
203 #ifdef NETATALK
204 struct at_ifaddr *aa;
205 #endif /* NETATALK */
206
207 KASSERT(ifp->if_extflags & IFEF_OUTPUT_MPSAFE);
208
209 #ifdef MBUFTRACE
210 m_claimm(m, ifp->if_mowner);
211 #endif
212
213 #if NCARP > 0
214 if (ifp->if_type == IFT_CARP) {
215 struct ifaddr *ifa;
216
217 /* loop back if this is going to the carp interface */
218 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
219 (ifa = ifa_ifwithaddr(dst)) != NULL &&
220 ifa->ifa_ifp == ifp0)
221 return looutput(ifp0, m, dst, rt);
222
223 ifp = ifp->if_carpdev;
224 /* ac = (struct arpcom *)ifp; */
225
226 if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
227 (IFF_UP|IFF_RUNNING))
228 senderr(ENETDOWN);
229 }
230 #endif /* NCARP > 0 */
231
232 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
233 senderr(ENETDOWN);
234
235 switch (dst->sa_family) {
236
237 #ifdef INET
238 case AF_INET:
239 KERNEL_LOCK(1, NULL);
240 if (m->m_flags & M_BCAST)
241 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
242 else if (m->m_flags & M_MCAST)
243 ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
244 else if ((error = arpresolve(ifp, rt, m, dst, edst,
245 sizeof(edst))) != 0) {
246 KERNEL_UNLOCK_ONE(NULL);
247 return error == EWOULDBLOCK ? 0 : error;
248 }
249 KERNEL_UNLOCK_ONE(NULL);
250 /* If broadcasting on a simplex interface, loopback a copy */
251 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
252 mcopy = m_copy(m, 0, (int)M_COPYALL);
253 etype = htons(ETHERTYPE_IP);
254 break;
255
256 case AF_ARP:
257 ah = mtod(m, struct arphdr *);
258 if (m->m_flags & M_BCAST)
259 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
260 else {
261 void *tha = ar_tha(ah);
262
263 if (tha == NULL) {
264 /* fake with ARPHDR_IEEE1394 */
265 return 0;
266 }
267 memcpy(edst, tha, sizeof(edst));
268 }
269
270 ah->ar_hrd = htons(ARPHRD_ETHER);
271
272 switch (ntohs(ah->ar_op)) {
273 case ARPOP_REVREQUEST:
274 case ARPOP_REVREPLY:
275 etype = htons(ETHERTYPE_REVARP);
276 break;
277
278 case ARPOP_REQUEST:
279 case ARPOP_REPLY:
280 default:
281 etype = htons(ETHERTYPE_ARP);
282 }
283
284 break;
285 #endif
286 #ifdef INET6
287 case AF_INET6:
288 if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
289 /* something bad happened */
290 return (0);
291 }
292 etype = htons(ETHERTYPE_IPV6);
293 break;
294 #endif
295 #ifdef NETATALK
296 case AF_APPLETALK:
297 KERNEL_LOCK(1, NULL);
298 if (aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
299 #ifdef NETATALKDEBUG
300 printf("aarpresolv failed\n");
301 #endif /* NETATALKDEBUG */
302 KERNEL_UNLOCK_ONE(NULL);
303 return (0);
304 }
305 /*
306 * ifaddr is the first thing in at_ifaddr
307 */
308 aa = (struct at_ifaddr *) at_ifawithnet(
309 (const struct sockaddr_at *)dst, ifp);
310 if (aa == NULL) {
311 KERNEL_UNLOCK_ONE(NULL);
312 goto bad;
313 }
314
315 /*
316 * In the phase 2 case, we need to prepend an mbuf for the
317 * llc header. Since we must preserve the value of m,
318 * which is passed to us by value, we m_copy() the first
319 * mbuf, and use it for our llc header.
320 */
321 if (aa->aa_flags & AFA_PHASE2) {
322 struct llc llc;
323
324 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
325 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
326 llc.llc_control = LLC_UI;
327 memcpy(llc.llc_snap_org_code, at_org_code,
328 sizeof(llc.llc_snap_org_code));
329 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
330 memcpy(mtod(m, void *), &llc, sizeof(struct llc));
331 } else {
332 etype = htons(ETHERTYPE_ATALK);
333 }
334 KERNEL_UNLOCK_ONE(NULL);
335 break;
336 #endif /* NETATALK */
337 case pseudo_AF_HDRCMPLT:
338 hdrcmplt = 1;
339 memcpy(esrc,
340 ((const struct ether_header *)dst->sa_data)->ether_shost,
341 sizeof(esrc));
342 /* FALLTHROUGH */
343
344 case AF_UNSPEC:
345 memcpy(edst,
346 ((const struct ether_header *)dst->sa_data)->ether_dhost,
347 sizeof(edst));
348 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
349 etype = ((const struct ether_header *)dst->sa_data)->ether_type;
350 break;
351
352 default:
353 printf("%s: can't handle af%d\n", ifp->if_xname,
354 dst->sa_family);
355 senderr(EAFNOSUPPORT);
356 }
357
358 #ifdef MPLS
359 KERNEL_LOCK(1, NULL);
360 {
361 struct m_tag *mtag;
362 mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL);
363 if (mtag != NULL) {
364 /* Having the tag itself indicates it's MPLS */
365 etype = htons(ETHERTYPE_MPLS);
366 m_tag_delete(m, mtag);
367 }
368 }
369 KERNEL_UNLOCK_ONE(NULL);
370 #endif
371
372 if (mcopy)
373 (void)looutput(ifp, mcopy, dst, rt);
374
375 /* If no ether type is set, this must be a 802.2 formatted packet.
376 */
377 if (etype == 0)
378 etype = htons(m->m_pkthdr.len);
379 /*
380 * Add local net header. If no space in first mbuf,
381 * allocate another.
382 */
383 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
384 if (m == 0)
385 senderr(ENOBUFS);
386 eh = mtod(m, struct ether_header *);
387 /* Note: etype is already in network byte order. */
388 (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
389 memcpy(eh->ether_dhost, edst, sizeof(edst));
390 if (hdrcmplt)
391 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
392 else
393 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
394 sizeof(eh->ether_shost));
395
396 #if NCARP > 0
397 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
398 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
399 sizeof(eh->ether_shost));
400 }
401 #endif /* NCARP > 0 */
402
403 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
404 return (error);
405 if (m == NULL)
406 return (0);
407
408 #if NBRIDGE > 0
409 /*
410 * Bridges require special output handling.
411 */
412 if (ifp->if_bridge)
413 return (bridge_output(ifp, m, NULL, NULL));
414 #endif
415
416 #if NCARP > 0
417 if (ifp != ifp0)
418 ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
419 #endif /* NCARP > 0 */
420
421 #ifdef ALTQ
422 KERNEL_LOCK(1, NULL);
423 /*
424 * If ALTQ is enabled on the parent interface, do
425 * classification; the queueing discipline might not
426 * require classification, but might require the
427 * address family/header pointer in the pktattr.
428 */
429 if (ALTQ_IS_ENABLED(&ifp->if_snd))
430 altq_etherclassify(&ifp->if_snd, m);
431 KERNEL_UNLOCK_ONE(NULL);
432 #endif
433 return ifq_enqueue(ifp, m);
434
435 bad:
436 if (m)
437 m_freem(m);
438 return (error);
439 }
440
441 #ifdef ALTQ
442 /*
443 * This routine is a slight hack to allow a packet to be classified
444 * if the Ethernet headers are present. It will go away when ALTQ's
445 * classification engine understands link headers.
446 */
447 void
448 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
449 {
450 struct ether_header *eh;
451 uint16_t ether_type;
452 int hlen, af, hdrsize;
453 void *hdr;
454
455 hlen = ETHER_HDR_LEN;
456 eh = mtod(m, struct ether_header *);
457
458 ether_type = htons(eh->ether_type);
459
460 if (ether_type < ETHERMTU) {
461 /* LLC/SNAP */
462 struct llc *llc = (struct llc *)(eh + 1);
463 hlen += 8;
464
465 if (m->m_len < hlen ||
466 llc->llc_dsap != LLC_SNAP_LSAP ||
467 llc->llc_ssap != LLC_SNAP_LSAP ||
468 llc->llc_control != LLC_UI) {
469 /* Not SNAP. */
470 goto bad;
471 }
472
473 ether_type = htons(llc->llc_un.type_snap.ether_type);
474 }
475
476 switch (ether_type) {
477 case ETHERTYPE_IP:
478 af = AF_INET;
479 hdrsize = 20; /* sizeof(struct ip) */
480 break;
481
482 case ETHERTYPE_IPV6:
483 af = AF_INET6;
484 hdrsize = 40; /* sizeof(struct ip6_hdr) */
485 break;
486
487 default:
488 af = AF_UNSPEC;
489 hdrsize = 0;
490 break;
491 }
492
493 while (m->m_len <= hlen) {
494 hlen -= m->m_len;
495 m = m->m_next;
496 }
497 if (m->m_len < (hlen + hdrsize)) {
498 /*
499 * protocol header not in a single mbuf.
500 * We can't cope with this situation right
501 * now (but it shouldn't ever happen, really, anyhow).
502 */
503 #ifdef DEBUG
504 printf("altq_etherclassify: headers span multiple mbufs: "
505 "%d < %d\n", m->m_len, (hlen + hdrsize));
506 #endif
507 goto bad;
508 }
509
510 m->m_data += hlen;
511 m->m_len -= hlen;
512
513 hdr = mtod(m, void *);
514
515 if (ALTQ_NEEDS_CLASSIFY(ifq))
516 m->m_pkthdr.pattr_class =
517 (*ifq->altq_classify)(ifq->altq_clfier, m, af);
518 m->m_pkthdr.pattr_af = af;
519 m->m_pkthdr.pattr_hdr = hdr;
520
521 m->m_data -= hlen;
522 m->m_len += hlen;
523
524 return;
525
526 bad:
527 m->m_pkthdr.pattr_class = NULL;
528 m->m_pkthdr.pattr_hdr = NULL;
529 m->m_pkthdr.pattr_af = AF_UNSPEC;
530 }
531 #endif /* ALTQ */
532
533 /*
534 * Process a received Ethernet packet;
535 * the packet is in the mbuf chain m with
536 * the ether header.
537 */
538 void
539 ether_input(struct ifnet *ifp, struct mbuf *m)
540 {
541 struct ethercom *ec = (struct ethercom *) ifp;
542 pktqueue_t *pktq = NULL;
543 struct ifqueue *inq = NULL;
544 uint16_t etype;
545 struct ether_header *eh;
546 size_t ehlen;
547 static int earlypkts;
548 int isr = 0;
549 #if defined (LLC) || defined(NETATALK)
550 struct llc *l;
551 #endif
552
553 KASSERT(!cpu_intr_p());
554
555 if ((ifp->if_flags & IFF_UP) == 0) {
556 m_freem(m);
557 return;
558 }
559
560 #ifdef MBUFTRACE
561 m_claimm(m, &ec->ec_rx_mowner);
562 #endif
563 eh = mtod(m, struct ether_header *);
564 etype = ntohs(eh->ether_type);
565 ehlen = sizeof(*eh);
566
567 if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
568 rnd_add_data(NULL, eh, ehlen, 0);
569 earlypkts++;
570 }
571
572 /*
573 * Determine if the packet is within its size limits.
574 */
575 if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
576 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
577 mutex_enter(&bigpktpps_lock);
578 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
579 bigpktppslim)) {
580 printf("%s: discarding oversize frame (len=%d)\n",
581 ifp->if_xname, m->m_pkthdr.len);
582 }
583 mutex_exit(&bigpktpps_lock);
584 m_freem(m);
585 return;
586 }
587
588 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
589 /*
590 * If this is not a simplex interface, drop the packet
591 * if it came from us.
592 */
593 if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
594 memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
595 ETHER_ADDR_LEN) == 0) {
596 m_freem(m);
597 return;
598 }
599
600 if (memcmp(etherbroadcastaddr,
601 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
602 m->m_flags |= M_BCAST;
603 else
604 m->m_flags |= M_MCAST;
605 ifp->if_imcasts++;
606 }
607
608 /* If the CRC is still on the packet, trim it off. */
609 if (m->m_flags & M_HASFCS) {
610 m_adj(m, -ETHER_CRC_LEN);
611 m->m_flags &= ~M_HASFCS;
612 }
613
614 ifp->if_ibytes += m->m_pkthdr.len;
615
616 #if NCARP > 0
617 if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
618 /*
619 * clear M_PROMISC, in case the packets comes from a
620 * vlan
621 */
622 m->m_flags &= ~M_PROMISC;
623 if (carp_input(m, (uint8_t *)&eh->ether_shost,
624 (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
625 return;
626 }
627 #endif /* NCARP > 0 */
628 if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
629 (ifp->if_flags & IFF_PROMISC) != 0 &&
630 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
631 ETHER_ADDR_LEN) != 0) {
632 m->m_flags |= M_PROMISC;
633 }
634
635 if ((m->m_flags & M_PROMISC) == 0) {
636 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
637 return;
638 if (m == NULL)
639 return;
640
641 eh = mtod(m, struct ether_header *);
642 etype = ntohs(eh->ether_type);
643 ehlen = sizeof(*eh);
644 }
645
646 #if NAGR > 0
647 if (ifp->if_agrprivate &&
648 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
649 m->m_flags &= ~M_PROMISC;
650 agr_input(ifp, m);
651 return;
652 }
653 #endif /* NAGR > 0 */
654
655 /*
656 * If VLANs are configured on the interface, check to
657 * see if the device performed the decapsulation and
658 * provided us with the tag.
659 */
660 if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
661 #if NVLAN > 0
662 /*
663 * vlan_input() will either recursively call ether_input()
664 * or drop the packet.
665 */
666 vlan_input(ifp, m);
667 #else
668 m_freem(m);
669 #endif
670 return;
671 }
672
673 /*
674 * Handle protocols that expect to have the Ethernet header
675 * (and possibly FCS) intact.
676 */
677 switch (etype) {
678 case ETHERTYPE_VLAN: {
679 struct ether_vlan_header *evl = (void *)eh;
680 /*
681 * If there is a tag of 0, then the VLAN header was probably
682 * just being used to store the priority. Extract the ether
683 * type, and if IP or IPV6, let them deal with it.
684 */
685 if (m->m_len <= sizeof(*evl)
686 && EVL_VLANOFTAG(evl->evl_tag) == 0) {
687 etype = ntohs(evl->evl_proto);
688 ehlen = sizeof(*evl);
689 if ((m->m_flags & M_PROMISC) == 0
690 && (etype == ETHERTYPE_IP
691 || etype == ETHERTYPE_IPV6))
692 break;
693 }
694 #if NVLAN > 0
695 /*
696 * vlan_input() will either recursively call ether_input()
697 * or drop the packet.
698 */
699 if (((struct ethercom *)ifp)->ec_nvlans != 0)
700 vlan_input(ifp, m);
701 else
702 #endif /* NVLAN > 0 */
703 m_freem(m);
704 return;
705 }
706 #if NPPPOE > 0
707 case ETHERTYPE_PPPOEDISC:
708 pppoedisc_input(ifp, m);
709 return;
710 case ETHERTYPE_PPPOE:
711 pppoe_input(ifp, m);
712 return;
713 #endif /* NPPPOE > 0 */
714 case ETHERTYPE_SLOWPROTOCOLS: {
715 uint8_t subtype;
716
717 #if defined(DIAGNOSTIC)
718 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
719 panic("ether_input: too short slow protocol packet");
720 }
721 #endif
722 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
723 switch (subtype) {
724 #if NAGR > 0
725 case SLOWPROTOCOLS_SUBTYPE_LACP:
726 if (ifp->if_agrprivate) {
727 ieee8023ad_lacp_input(ifp, m);
728 return;
729 }
730 break;
731
732 case SLOWPROTOCOLS_SUBTYPE_MARKER:
733 if (ifp->if_agrprivate) {
734 ieee8023ad_marker_input(ifp, m);
735 return;
736 }
737 break;
738 #endif /* NAGR > 0 */
739 default:
740 if (subtype == 0 || subtype > 10) {
741 /* illegal value */
742 m_freem(m);
743 return;
744 }
745 /* unknown subtype */
746 break;
747 }
748 /* FALLTHROUGH */
749 }
750 default:
751 if (m->m_flags & M_PROMISC) {
752 m_freem(m);
753 return;
754 }
755 }
756
757 /* If the CRC is still on the packet, trim it off. */
758 if (m->m_flags & M_HASFCS) {
759 m_adj(m, -ETHER_CRC_LEN);
760 m->m_flags &= ~M_HASFCS;
761 }
762
763 if (etype > ETHERMTU + sizeof (struct ether_header)) {
764 /* Strip off the Ethernet header. */
765 m_adj(m, ehlen);
766
767 switch (etype) {
768 #ifdef INET
769 case ETHERTYPE_IP:
770 #ifdef GATEWAY
771 if (ipflow_fastforward(m))
772 return;
773 #endif
774 pktq = ip_pktq;
775 break;
776
777 case ETHERTYPE_ARP:
778 isr = NETISR_ARP;
779 inq = &arpintrq;
780 break;
781
782 case ETHERTYPE_REVARP:
783 revarpinput(m); /* XXX queue? */
784 return;
785 #endif
786 #ifdef INET6
787 case ETHERTYPE_IPV6:
788 if (__predict_false(!in6_present)) {
789 m_freem(m);
790 return;
791 }
792 #ifdef GATEWAY
793 if (ip6flow_fastforward(&m))
794 return;
795 #endif
796 pktq = ip6_pktq;
797 break;
798 #endif
799 #ifdef NETATALK
800 case ETHERTYPE_ATALK:
801 isr = NETISR_ATALK;
802 inq = &atintrq1;
803 break;
804 case ETHERTYPE_AARP:
805 /* probably this should be done with a NETISR as well */
806 aarpinput(ifp, m); /* XXX */
807 return;
808 #endif /* NETATALK */
809 #ifdef MPLS
810 case ETHERTYPE_MPLS:
811 isr = NETISR_MPLS;
812 inq = &mplsintrq;
813 break;
814 #endif
815 default:
816 m_freem(m);
817 return;
818 }
819 } else {
820 #if defined (LLC) || defined (NETATALK)
821 l = (struct llc *)(eh+1);
822 switch (l->llc_dsap) {
823 #ifdef NETATALK
824 case LLC_SNAP_LSAP:
825 switch (l->llc_control) {
826 case LLC_UI:
827 if (l->llc_ssap != LLC_SNAP_LSAP) {
828 goto dropanyway;
829 }
830
831 if (memcmp(&(l->llc_snap_org_code)[0],
832 at_org_code, sizeof(at_org_code)) == 0 &&
833 ntohs(l->llc_snap_ether_type) ==
834 ETHERTYPE_ATALK) {
835 inq = &atintrq2;
836 m_adj(m, sizeof(struct ether_header)
837 + sizeof(struct llc));
838 isr = NETISR_ATALK;
839 break;
840 }
841
842 if (memcmp(&(l->llc_snap_org_code)[0],
843 aarp_org_code,
844 sizeof(aarp_org_code)) == 0 &&
845 ntohs(l->llc_snap_ether_type) ==
846 ETHERTYPE_AARP) {
847 m_adj( m, sizeof(struct ether_header)
848 + sizeof(struct llc));
849 aarpinput(ifp, m); /* XXX */
850 return;
851 }
852
853 default:
854 goto dropanyway;
855 }
856 break;
857 dropanyway:
858 #endif
859 default:
860 m_freem(m);
861 return;
862 }
863 #else /* ISO || LLC || NETATALK*/
864 m_freem(m);
865 return;
866 #endif /* ISO || LLC || NETATALK*/
867 }
868
869 if (__predict_true(pktq)) {
870 const uint32_t h = pktq_rps_hash(m);
871 if (__predict_false(!pktq_enqueue(pktq, m, h))) {
872 m_freem(m);
873 }
874 return;
875 }
876
877 if (__predict_false(!inq)) {
878 /* Should not happen. */
879 m_freem(m);
880 return;
881 }
882 if (IF_QFULL(inq)) {
883 IF_DROP(inq);
884 m_freem(m);
885 } else {
886 IF_ENQUEUE(inq, m);
887 schednetisr(isr);
888 }
889 }
890
891 /*
892 * Convert Ethernet address to printable (loggable) representation.
893 */
894 char *
895 ether_sprintf(const u_char *ap)
896 {
897 static char etherbuf[3 * ETHER_ADDR_LEN];
898 return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
899 }
900
901 char *
902 ether_snprintf(char *buf, size_t len, const u_char *ap)
903 {
904 char *cp = buf;
905 size_t i;
906
907 for (i = 0; i < len / 3; i++) {
908 *cp++ = hexdigits[*ap >> 4];
909 *cp++ = hexdigits[*ap++ & 0xf];
910 *cp++ = ':';
911 }
912 *--cp = '\0';
913 return buf;
914 }
915
916 /*
917 * Perform common duties while attaching to interface list
918 */
919 void
920 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
921 {
922 struct ethercom *ec = (struct ethercom *)ifp;
923
924 ifp->if_extflags |= IFEF_OUTPUT_MPSAFE;
925 ifp->if_type = IFT_ETHER;
926 ifp->if_hdrlen = ETHER_HDR_LEN;
927 ifp->if_dlt = DLT_EN10MB;
928 ifp->if_mtu = ETHERMTU;
929 ifp->if_output = ether_output;
930 ifp->_if_input = ether_input;
931 if (ifp->if_baudrate == 0)
932 ifp->if_baudrate = IF_Mbps(10); /* just a default */
933
934 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
935
936 LIST_INIT(&ec->ec_multiaddrs);
937 ifp->if_broadcastaddr = etherbroadcastaddr;
938 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
939 #ifdef MBUFTRACE
940 strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
941 sizeof(ec->ec_tx_mowner.mo_name));
942 strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
943 sizeof(ec->ec_tx_mowner.mo_descr));
944 strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
945 sizeof(ec->ec_rx_mowner.mo_name));
946 strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
947 sizeof(ec->ec_rx_mowner.mo_descr));
948 MOWNER_ATTACH(&ec->ec_tx_mowner);
949 MOWNER_ATTACH(&ec->ec_rx_mowner);
950 ifp->if_mowner = &ec->ec_tx_mowner;
951 #endif
952 }
953
954 void
955 ether_ifdetach(struct ifnet *ifp)
956 {
957 struct ethercom *ec = (void *) ifp;
958 struct ether_multi *enm;
959 int s;
960
961 /*
962 * Prevent further calls to ioctl (for example turning off
963 * promiscuous mode from the bridge code), which eventually can
964 * call if_init() which can cause panics because the interface
965 * is in the process of being detached. Return device not configured
966 * instead.
967 */
968 ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
969
970 #if NBRIDGE > 0
971 if (ifp->if_bridge)
972 bridge_ifdetach(ifp);
973 #endif
974
975 bpf_detach(ifp);
976
977 #if NVLAN > 0
978 if (ec->ec_nvlans)
979 vlan_ifdetach(ifp);
980 #endif
981
982 s = splnet();
983 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
984 LIST_REMOVE(enm, enm_list);
985 free(enm, M_IFMADDR);
986 ec->ec_multicnt--;
987 }
988 splx(s);
989
990 ifp->if_mowner = NULL;
991 MOWNER_DETACH(&ec->ec_rx_mowner);
992 MOWNER_DETACH(&ec->ec_tx_mowner);
993 }
994
995 #if 0
996 /*
997 * This is for reference. We have a table-driven version
998 * of the little-endian crc32 generator, which is faster
999 * than the double-loop.
1000 */
1001 uint32_t
1002 ether_crc32_le(const uint8_t *buf, size_t len)
1003 {
1004 uint32_t c, crc, carry;
1005 size_t i, j;
1006
1007 crc = 0xffffffffU; /* initial value */
1008
1009 for (i = 0; i < len; i++) {
1010 c = buf[i];
1011 for (j = 0; j < 8; j++) {
1012 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1013 crc >>= 1;
1014 c >>= 1;
1015 if (carry)
1016 crc = (crc ^ ETHER_CRC_POLY_LE);
1017 }
1018 }
1019
1020 return (crc);
1021 }
1022 #else
1023 uint32_t
1024 ether_crc32_le(const uint8_t *buf, size_t len)
1025 {
1026 static const uint32_t crctab[] = {
1027 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1028 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1029 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1030 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1031 };
1032 uint32_t crc;
1033 size_t i;
1034
1035 crc = 0xffffffffU; /* initial value */
1036
1037 for (i = 0; i < len; i++) {
1038 crc ^= buf[i];
1039 crc = (crc >> 4) ^ crctab[crc & 0xf];
1040 crc = (crc >> 4) ^ crctab[crc & 0xf];
1041 }
1042
1043 return (crc);
1044 }
1045 #endif
1046
1047 uint32_t
1048 ether_crc32_be(const uint8_t *buf, size_t len)
1049 {
1050 uint32_t c, crc, carry;
1051 size_t i, j;
1052
1053 crc = 0xffffffffU; /* initial value */
1054
1055 for (i = 0; i < len; i++) {
1056 c = buf[i];
1057 for (j = 0; j < 8; j++) {
1058 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1059 crc <<= 1;
1060 c >>= 1;
1061 if (carry)
1062 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1063 }
1064 }
1065
1066 return (crc);
1067 }
1068
1069 #ifdef INET
1070 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1071 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1072 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1073 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1074 #endif
1075 #ifdef INET6
1076 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1077 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1078 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1079 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1080 #endif
1081
1082 /*
1083 * ether_aton implementation, not using a static buffer.
1084 */
1085 int
1086 ether_aton_r(u_char *dest, size_t len, const char *str)
1087 {
1088 const u_char *cp = (const void *)str;
1089 u_char *ep;
1090
1091 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1092
1093 if (len < ETHER_ADDR_LEN)
1094 return ENOSPC;
1095
1096 ep = dest + ETHER_ADDR_LEN;
1097
1098 while (*cp) {
1099 if (!isxdigit(*cp))
1100 return EINVAL;
1101 *dest = atox(*cp);
1102 cp++;
1103 if (isxdigit(*cp)) {
1104 *dest = (*dest << 4) | atox(*cp);
1105 dest++;
1106 cp++;
1107 } else
1108 dest++;
1109 if (dest == ep)
1110 return *cp == '\0' ? 0 : ENAMETOOLONG;
1111 switch (*cp) {
1112 case ':':
1113 case '-':
1114 case '.':
1115 cp++;
1116 break;
1117 }
1118 }
1119 return ENOBUFS;
1120 }
1121
1122 /*
1123 * Convert a sockaddr into an Ethernet address or range of Ethernet
1124 * addresses.
1125 */
1126 int
1127 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1128 uint8_t addrhi[ETHER_ADDR_LEN])
1129 {
1130 #ifdef INET
1131 const struct sockaddr_in *sin;
1132 #endif /* INET */
1133 #ifdef INET6
1134 const struct sockaddr_in6 *sin6;
1135 #endif /* INET6 */
1136
1137 switch (sa->sa_family) {
1138
1139 case AF_UNSPEC:
1140 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1141 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1142 break;
1143
1144 #ifdef INET
1145 case AF_INET:
1146 sin = satocsin(sa);
1147 if (sin->sin_addr.s_addr == INADDR_ANY) {
1148 /*
1149 * An IP address of INADDR_ANY means listen to
1150 * or stop listening to all of the Ethernet
1151 * multicast addresses used for IP.
1152 * (This is for the sake of IP multicast routers.)
1153 */
1154 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1155 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1156 }
1157 else {
1158 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1159 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1160 }
1161 break;
1162 #endif
1163 #ifdef INET6
1164 case AF_INET6:
1165 sin6 = satocsin6(sa);
1166 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1167 /*
1168 * An IP6 address of 0 means listen to or stop
1169 * listening to all of the Ethernet multicast
1170 * address used for IP6.
1171 * (This is used for multicast routers.)
1172 */
1173 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1174 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1175 } else {
1176 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1177 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1178 }
1179 break;
1180 #endif
1181
1182 default:
1183 return EAFNOSUPPORT;
1184 }
1185 return 0;
1186 }
1187
1188 /*
1189 * Add an Ethernet multicast address or range of addresses to the list for a
1190 * given interface.
1191 */
1192 int
1193 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1194 {
1195 struct ether_multi *enm;
1196 u_char addrlo[ETHER_ADDR_LEN];
1197 u_char addrhi[ETHER_ADDR_LEN];
1198 int s = splnet(), error;
1199
1200 error = ether_multiaddr(sa, addrlo, addrhi);
1201 if (error != 0) {
1202 splx(s);
1203 return error;
1204 }
1205
1206 /*
1207 * Verify that we have valid Ethernet multicast addresses.
1208 */
1209 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1210 splx(s);
1211 return EINVAL;
1212 }
1213 /*
1214 * See if the address range is already in the list.
1215 */
1216 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1217 if (enm != NULL) {
1218 /*
1219 * Found it; just increment the reference count.
1220 */
1221 ++enm->enm_refcount;
1222 splx(s);
1223 return 0;
1224 }
1225 /*
1226 * New address or range; malloc a new multicast record
1227 * and link it into the interface's multicast list.
1228 */
1229 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1230 if (enm == NULL) {
1231 splx(s);
1232 return ENOBUFS;
1233 }
1234 memcpy(enm->enm_addrlo, addrlo, 6);
1235 memcpy(enm->enm_addrhi, addrhi, 6);
1236 enm->enm_refcount = 1;
1237 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1238 ec->ec_multicnt++;
1239 splx(s);
1240 /*
1241 * Return ENETRESET to inform the driver that the list has changed
1242 * and its reception filter should be adjusted accordingly.
1243 */
1244 return ENETRESET;
1245 }
1246
1247 /*
1248 * Delete a multicast address record.
1249 */
1250 int
1251 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1252 {
1253 struct ether_multi *enm;
1254 u_char addrlo[ETHER_ADDR_LEN];
1255 u_char addrhi[ETHER_ADDR_LEN];
1256 int s = splnet(), error;
1257
1258 error = ether_multiaddr(sa, addrlo, addrhi);
1259 if (error != 0) {
1260 splx(s);
1261 return (error);
1262 }
1263
1264 /*
1265 * Look ur the address in our list.
1266 */
1267 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1268 if (enm == NULL) {
1269 splx(s);
1270 return (ENXIO);
1271 }
1272 if (--enm->enm_refcount != 0) {
1273 /*
1274 * Still some claims to this record.
1275 */
1276 splx(s);
1277 return (0);
1278 }
1279 /*
1280 * No remaining claims to this record; unlink and free it.
1281 */
1282 LIST_REMOVE(enm, enm_list);
1283 free(enm, M_IFMADDR);
1284 ec->ec_multicnt--;
1285 splx(s);
1286 /*
1287 * Return ENETRESET to inform the driver that the list has changed
1288 * and its reception filter should be adjusted accordingly.
1289 */
1290 return (ENETRESET);
1291 }
1292
1293 void
1294 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1295 {
1296 ec->ec_ifflags_cb = cb;
1297 }
1298
1299 /*
1300 * Common ioctls for Ethernet interfaces. Note, we must be
1301 * called at splnet().
1302 */
1303 int
1304 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1305 {
1306 struct ethercom *ec = (void *) ifp;
1307 struct eccapreq *eccr;
1308 struct ifreq *ifr = (struct ifreq *)data;
1309 struct if_laddrreq *iflr = data;
1310 const struct sockaddr_dl *sdl;
1311 static const uint8_t zero[ETHER_ADDR_LEN];
1312 int error;
1313
1314 switch (cmd) {
1315 case SIOCINITIFADDR:
1316 {
1317 struct ifaddr *ifa = (struct ifaddr *)data;
1318 if (ifa->ifa_addr->sa_family != AF_LINK
1319 && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1320 (IFF_UP|IFF_RUNNING)) {
1321 ifp->if_flags |= IFF_UP;
1322 if ((error = (*ifp->if_init)(ifp)) != 0)
1323 return error;
1324 }
1325 #ifdef INET
1326 if (ifa->ifa_addr->sa_family == AF_INET)
1327 arp_ifinit(ifp, ifa);
1328 #endif /* INET */
1329 return 0;
1330 }
1331
1332 case SIOCSIFMTU:
1333 {
1334 int maxmtu;
1335
1336 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1337 maxmtu = ETHERMTU_JUMBO;
1338 else
1339 maxmtu = ETHERMTU;
1340
1341 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1342 return EINVAL;
1343 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1344 return error;
1345 else if (ifp->if_flags & IFF_UP) {
1346 /* Make sure the device notices the MTU change. */
1347 return (*ifp->if_init)(ifp);
1348 } else
1349 return 0;
1350 }
1351
1352 case SIOCSIFFLAGS:
1353 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1354 return error;
1355 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1356 case IFF_RUNNING:
1357 /*
1358 * If interface is marked down and it is running,
1359 * then stop and disable it.
1360 */
1361 (*ifp->if_stop)(ifp, 1);
1362 break;
1363 case IFF_UP:
1364 /*
1365 * If interface is marked up and it is stopped, then
1366 * start it.
1367 */
1368 return (*ifp->if_init)(ifp);
1369 case IFF_UP|IFF_RUNNING:
1370 error = 0;
1371 if (ec->ec_ifflags_cb == NULL ||
1372 (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
1373 /*
1374 * Reset the interface to pick up
1375 * changes in any other flags that
1376 * affect the hardware state.
1377 */
1378 return (*ifp->if_init)(ifp);
1379 } else
1380 return error;
1381 case 0:
1382 break;
1383 }
1384 return 0;
1385 case SIOCGETHERCAP:
1386 eccr = (struct eccapreq *)data;
1387 eccr->eccr_capabilities = ec->ec_capabilities;
1388 eccr->eccr_capenable = ec->ec_capenable;
1389 return 0;
1390 case SIOCADDMULTI:
1391 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1392 case SIOCDELMULTI:
1393 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1394 case SIOCSIFMEDIA:
1395 case SIOCGIFMEDIA:
1396 if (ec->ec_mii == NULL)
1397 return ENOTTY;
1398 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1399 case SIOCALIFADDR:
1400 sdl = satocsdl(sstocsa(&iflr->addr));
1401 if (sdl->sdl_family != AF_LINK)
1402 ;
1403 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1404 return EINVAL;
1405 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1406 return EINVAL;
1407 /*FALLTHROUGH*/
1408 default:
1409 return ifioctl_common(ifp, cmd, data);
1410 }
1411 return 0;
1412 }
1413
1414 /*
1415 * Enable/disable passing VLAN packets if the parent interface supports it.
1416 * Return:
1417 * 0: Ok
1418 * -1: Parent interface does not support vlans
1419 * >0: Error
1420 */
1421 int
1422 ether_enable_vlan_mtu(struct ifnet *ifp)
1423 {
1424 int error;
1425 struct ethercom *ec = (void *)ifp;
1426
1427 /* Already have VLAN's do nothing. */
1428 if (ec->ec_nvlans != 0)
1429 return 0;
1430
1431 /* Parent does not support VLAN's */
1432 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1433 return -1;
1434
1435 /*
1436 * Parent supports the VLAN_MTU capability,
1437 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1438 * enable it.
1439 */
1440 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1441
1442 /* Interface is down, defer for later */
1443 if ((ifp->if_flags & IFF_UP) == 0)
1444 return 0;
1445
1446 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1447 return 0;
1448
1449 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1450 return error;
1451 }
1452
1453 int
1454 ether_disable_vlan_mtu(struct ifnet *ifp)
1455 {
1456 int error;
1457 struct ethercom *ec = (void *)ifp;
1458
1459 /* We still have VLAN's, defer for later */
1460 if (ec->ec_nvlans != 0)
1461 return 0;
1462
1463 /* Parent does not support VLAB's, nothing to do. */
1464 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1465 return -1;
1466
1467 /*
1468 * Disable Tx/Rx of VLAN-sized frames.
1469 */
1470 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1471
1472 /* Interface is down, defer for later */
1473 if ((ifp->if_flags & IFF_UP) == 0)
1474 return 0;
1475
1476 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1477 return 0;
1478
1479 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1480 return error;
1481 }
1482
1483 static int
1484 ether_multicast_sysctl(SYSCTLFN_ARGS)
1485 {
1486 struct ether_multi *enm;
1487 struct ether_multi_sysctl addr;
1488 struct ifnet *ifp;
1489 struct ethercom *ec;
1490 int error = 0;
1491 size_t written;
1492 struct psref psref;
1493 int bound;
1494
1495 if (namelen != 1)
1496 return EINVAL;
1497
1498 bound = curlwp_bind();
1499 ifp = if_get_byindex(name[0], &psref);
1500 if (ifp == NULL) {
1501 error = ENODEV;
1502 goto out;
1503 }
1504 if (ifp->if_type != IFT_ETHER) {
1505 if_put(ifp, &psref);
1506 *oldlenp = 0;
1507 goto out;
1508 }
1509 ec = (struct ethercom *)ifp;
1510
1511 if (oldp == NULL) {
1512 if_put(ifp, &psref);
1513 *oldlenp = ec->ec_multicnt * sizeof(addr);
1514 goto out;
1515 }
1516
1517 memset(&addr, 0, sizeof(addr));
1518 error = 0;
1519 written = 0;
1520
1521 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1522 if (written + sizeof(addr) > *oldlenp)
1523 break;
1524 addr.enm_refcount = enm->enm_refcount;
1525 memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1526 memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1527 error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
1528 if (error)
1529 break;
1530 written += sizeof(addr);
1531 oldp = (char *)oldp + sizeof(addr);
1532 }
1533 if_put(ifp, &psref);
1534
1535 *oldlenp = written;
1536 out:
1537 curlwp_bindx(bound);
1538 return error;
1539 }
1540
1541 SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
1542 {
1543 const struct sysctlnode *rnode = NULL;
1544
1545 sysctl_createv(clog, 0, NULL, &rnode,
1546 CTLFLAG_PERMANENT,
1547 CTLTYPE_NODE, "ether",
1548 SYSCTL_DESCR("Ethernet-specific information"),
1549 NULL, 0, NULL, 0,
1550 CTL_NET, CTL_CREATE, CTL_EOL);
1551
1552 sysctl_createv(clog, 0, &rnode, NULL,
1553 CTLFLAG_PERMANENT,
1554 CTLTYPE_NODE, "multicast",
1555 SYSCTL_DESCR("multicast addresses"),
1556 ether_multicast_sysctl, 0, NULL, 0,
1557 CTL_CREATE, CTL_EOL);
1558 }
1559
1560 void
1561 etherinit(void)
1562 {
1563 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1564 }
1565