if_ethersubr.c revision 1.312 1 /* $NetBSD: if_ethersubr.c,v 1.312 2022/06/20 08:02:25 yamaguchi 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.312 2022/06/20 08:02:25 yamaguchi 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/mbuf.h>
84 #include <sys/mutex.h>
85 #include <sys/ioctl.h>
86 #include <sys/errno.h>
87 #include <sys/device.h>
88 #include <sys/entropy.h>
89 #include <sys/rndsource.h>
90 #include <sys/cpu.h>
91 #include <sys/kmem.h>
92 #include <sys/hook.h>
93
94 #include <net/if.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97 #include <net/if_llc.h>
98 #include <net/if_dl.h>
99 #include <net/if_types.h>
100 #include <net/pktqueue.h>
101
102 #include <net/if_media.h>
103 #include <dev/mii/mii.h>
104 #include <dev/mii/miivar.h>
105
106 #if NARP == 0
107 /*
108 * XXX there should really be a way to issue this warning from within config(8)
109 */
110 #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.
111 #endif
112
113 #include <net/bpf.h>
114
115 #include <net/if_ether.h>
116 #include <net/if_vlanvar.h>
117
118 #if NPPPOE > 0
119 #include <net/if_pppoe.h>
120 #endif
121
122 #if NAGR > 0
123 #include <net/ether_slowprotocols.h>
124 #include <net/agr/ieee8023ad.h>
125 #include <net/agr/if_agrvar.h>
126 #endif
127
128 #if NBRIDGE > 0
129 #include <net/if_bridgevar.h>
130 #endif
131
132 #include <netinet/in.h>
133 #ifdef INET
134 #include <netinet/in_var.h>
135 #endif
136 #include <netinet/if_inarp.h>
137
138 #ifdef INET6
139 #ifndef INET
140 #include <netinet/in.h>
141 #endif
142 #include <netinet6/in6_var.h>
143 #include <netinet6/nd6.h>
144 #endif
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 CTASSERT(sizeof(struct ether_addr) == 6);
169 CTASSERT(sizeof(struct ether_header) == 14);
170
171 #ifdef DIAGNOSTIC
172 static struct timeval bigpktppslim_last;
173 static int bigpktppslim = 2; /* XXX */
174 static int bigpktpps_count;
175 static kmutex_t bigpktpps_lock __cacheline_aligned;
176 #endif
177
178 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
179 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
180 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
181 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
182 #define senderr(e) { error = (e); goto bad;}
183
184 static pktq_rps_hash_func_t ether_pktq_rps_hash_p;
185
186 static int ether_output(struct ifnet *, struct mbuf *,
187 const struct sockaddr *, const struct rtentry *);
188
189 /*
190 * Ethernet output routine.
191 * Encapsulate a packet of type family for the local net.
192 * Assumes that ifp is actually pointer to ethercom structure.
193 */
194 static int
195 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
196 const struct sockaddr * const dst, const struct rtentry *rt)
197 {
198 uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
199 uint16_t etype = 0;
200 int error = 0, hdrcmplt = 0;
201 struct mbuf *m = m0;
202 struct mbuf *mcopy = NULL;
203 struct ether_header *eh;
204 struct ifnet *ifp = ifp0;
205 #ifdef INET
206 struct arphdr *ah;
207 #endif
208 #ifdef NETATALK
209 struct at_ifaddr *aa;
210 #endif
211
212 #ifdef MBUFTRACE
213 m_claimm(m, ifp->if_mowner);
214 #endif
215
216 #if NCARP > 0
217 if (ifp->if_type == IFT_CARP) {
218 struct ifaddr *ifa;
219 int s = pserialize_read_enter();
220
221 /* loop back if this is going to the carp interface */
222 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
223 (ifa = ifa_ifwithaddr(dst)) != NULL) {
224 if (ifa->ifa_ifp == ifp0) {
225 pserialize_read_exit(s);
226 return looutput(ifp0, m, dst, rt);
227 }
228 }
229 pserialize_read_exit(s);
230
231 ifp = ifp->if_carpdev;
232 /* ac = (struct arpcom *)ifp; */
233
234 if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
235 (IFF_UP | IFF_RUNNING))
236 senderr(ENETDOWN);
237 }
238 #endif
239
240 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
241 senderr(ENETDOWN);
242
243 switch (dst->sa_family) {
244
245 #ifdef INET
246 case AF_INET:
247 if (m->m_flags & M_BCAST) {
248 memcpy(edst, etherbroadcastaddr, sizeof(edst));
249 } else if (m->m_flags & M_MCAST) {
250 ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
251 } else {
252 error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst));
253 if (error)
254 return (error == EWOULDBLOCK) ? 0 : error;
255 }
256 /* If broadcasting on a simplex interface, loopback a copy */
257 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
258 mcopy = m_copypacket(m, M_DONTWAIT);
259 etype = htons(ETHERTYPE_IP);
260 break;
261
262 case AF_ARP:
263 ah = mtod(m, struct arphdr *);
264 if (m->m_flags & M_BCAST) {
265 memcpy(edst, etherbroadcastaddr, sizeof(edst));
266 } else {
267 void *tha = ar_tha(ah);
268
269 if (tha == NULL) {
270 /* fake with ARPHRD_IEEE1394 */
271 m_freem(m);
272 return 0;
273 }
274 memcpy(edst, tha, sizeof(edst));
275 }
276
277 ah->ar_hrd = htons(ARPHRD_ETHER);
278
279 switch (ntohs(ah->ar_op)) {
280 case ARPOP_REVREQUEST:
281 case ARPOP_REVREPLY:
282 etype = htons(ETHERTYPE_REVARP);
283 break;
284
285 case ARPOP_REQUEST:
286 case ARPOP_REPLY:
287 default:
288 etype = htons(ETHERTYPE_ARP);
289 }
290 break;
291 #endif
292
293 #ifdef INET6
294 case AF_INET6:
295 if (m->m_flags & M_BCAST) {
296 memcpy(edst, etherbroadcastaddr, sizeof(edst));
297 } else if (m->m_flags & M_MCAST) {
298 ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
299 edst);
300 } else {
301 error = nd6_resolve(ifp0, rt, m, dst, edst,
302 sizeof(edst));
303 if (error)
304 return (error == EWOULDBLOCK) ? 0 : error;
305 }
306 etype = htons(ETHERTYPE_IPV6);
307 break;
308 #endif
309
310 #ifdef NETATALK
311 case AF_APPLETALK: {
312 struct ifaddr *ifa;
313 int s;
314
315 KERNEL_LOCK(1, NULL);
316
317 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
318 KERNEL_UNLOCK_ONE(NULL);
319 return 0;
320 }
321
322 /*
323 * ifaddr is the first thing in at_ifaddr
324 */
325 s = pserialize_read_enter();
326 ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
327 if (ifa == NULL) {
328 pserialize_read_exit(s);
329 KERNEL_UNLOCK_ONE(NULL);
330 senderr(EADDRNOTAVAIL);
331 }
332 aa = (struct at_ifaddr *)ifa;
333
334 /*
335 * In the phase 2 case, we need to prepend an mbuf for the
336 * llc header.
337 */
338 if (aa->aa_flags & AFA_PHASE2) {
339 struct llc llc;
340
341 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
342 if (m == NULL) {
343 pserialize_read_exit(s);
344 KERNEL_UNLOCK_ONE(NULL);
345 senderr(ENOBUFS);
346 }
347
348 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
349 llc.llc_control = LLC_UI;
350 memcpy(llc.llc_snap_org_code, at_org_code,
351 sizeof(llc.llc_snap_org_code));
352 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
353 memcpy(mtod(m, void *), &llc, sizeof(struct llc));
354 } else {
355 etype = htons(ETHERTYPE_ATALK);
356 }
357 pserialize_read_exit(s);
358 KERNEL_UNLOCK_ONE(NULL);
359 break;
360 }
361 #endif /* NETATALK */
362
363 case pseudo_AF_HDRCMPLT:
364 hdrcmplt = 1;
365 memcpy(esrc,
366 ((const struct ether_header *)dst->sa_data)->ether_shost,
367 sizeof(esrc));
368 /* FALLTHROUGH */
369
370 case AF_UNSPEC:
371 memcpy(edst,
372 ((const struct ether_header *)dst->sa_data)->ether_dhost,
373 sizeof(edst));
374 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
375 etype = ((const struct ether_header *)dst->sa_data)->ether_type;
376 break;
377
378 default:
379 printf("%s: can't handle af%d\n", ifp->if_xname,
380 dst->sa_family);
381 senderr(EAFNOSUPPORT);
382 }
383
384 #ifdef MPLS
385 {
386 struct m_tag *mtag;
387 mtag = m_tag_find(m, PACKET_TAG_MPLS);
388 if (mtag != NULL) {
389 /* Having the tag itself indicates it's MPLS */
390 etype = htons(ETHERTYPE_MPLS);
391 m_tag_delete(m, mtag);
392 }
393 }
394 #endif
395
396 if (mcopy)
397 (void)looutput(ifp, mcopy, dst, rt);
398
399 KASSERT((m->m_flags & M_PKTHDR) != 0);
400
401 /*
402 * If no ether type is set, this must be a 802.2 formatted packet.
403 */
404 if (etype == 0)
405 etype = htons(m->m_pkthdr.len);
406
407 /*
408 * Add local net header. If no space in first mbuf, allocate another.
409 */
410 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
411 if (m == NULL)
412 senderr(ENOBUFS);
413
414 eh = mtod(m, struct ether_header *);
415 /* Note: etype is already in network byte order. */
416 memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
417 memcpy(eh->ether_dhost, edst, sizeof(edst));
418 if (hdrcmplt) {
419 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
420 } else {
421 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
422 sizeof(eh->ether_shost));
423 }
424
425 #if NCARP > 0
426 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
427 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
428 sizeof(eh->ether_shost));
429 }
430 #endif
431
432 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
433 return error;
434 if (m == NULL)
435 return 0;
436
437 #if NBRIDGE > 0
438 /*
439 * Bridges require special output handling.
440 */
441 if (ifp->if_bridge)
442 return bridge_output(ifp, m, NULL, NULL);
443 #endif
444
445 #if NCARP > 0
446 if (ifp != ifp0)
447 if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
448 #endif
449
450 #ifdef ALTQ
451 KERNEL_LOCK(1, NULL);
452 /*
453 * If ALTQ is enabled on the parent interface, do
454 * classification; the queueing discipline might not
455 * require classification, but might require the
456 * address family/header pointer in the pktattr.
457 */
458 if (ALTQ_IS_ENABLED(&ifp->if_snd))
459 altq_etherclassify(&ifp->if_snd, m);
460 KERNEL_UNLOCK_ONE(NULL);
461 #endif
462 return ifq_enqueue(ifp, m);
463
464 bad:
465 if_statinc(ifp, if_oerrors);
466 if (m)
467 m_freem(m);
468 return error;
469 }
470
471 #ifdef ALTQ
472 /*
473 * This routine is a slight hack to allow a packet to be classified
474 * if the Ethernet headers are present. It will go away when ALTQ's
475 * classification engine understands link headers.
476 *
477 * XXX: We may need to do m_pullups here. First to ensure struct ether_header
478 * is indeed contiguous, then to read the LLC and so on.
479 */
480 void
481 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
482 {
483 struct ether_header *eh;
484 struct mbuf *mtop = m;
485 uint16_t ether_type;
486 int hlen, af, hdrsize;
487 void *hdr;
488
489 KASSERT((mtop->m_flags & M_PKTHDR) != 0);
490
491 hlen = ETHER_HDR_LEN;
492 eh = mtod(m, struct ether_header *);
493
494 ether_type = htons(eh->ether_type);
495
496 if (ether_type < ETHERMTU) {
497 /* LLC/SNAP */
498 struct llc *llc = (struct llc *)(eh + 1);
499 hlen += 8;
500
501 if (m->m_len < hlen ||
502 llc->llc_dsap != LLC_SNAP_LSAP ||
503 llc->llc_ssap != LLC_SNAP_LSAP ||
504 llc->llc_control != LLC_UI) {
505 /* Not SNAP. */
506 goto bad;
507 }
508
509 ether_type = htons(llc->llc_un.type_snap.ether_type);
510 }
511
512 switch (ether_type) {
513 case ETHERTYPE_IP:
514 af = AF_INET;
515 hdrsize = 20; /* sizeof(struct ip) */
516 break;
517
518 case ETHERTYPE_IPV6:
519 af = AF_INET6;
520 hdrsize = 40; /* sizeof(struct ip6_hdr) */
521 break;
522
523 default:
524 af = AF_UNSPEC;
525 hdrsize = 0;
526 break;
527 }
528
529 while (m->m_len <= hlen) {
530 hlen -= m->m_len;
531 m = m->m_next;
532 if (m == NULL)
533 goto bad;
534 }
535
536 if (m->m_len < (hlen + hdrsize)) {
537 /*
538 * protocol header not in a single mbuf.
539 * We can't cope with this situation right
540 * now (but it shouldn't ever happen, really, anyhow).
541 */
542 #ifdef DEBUG
543 printf("altq_etherclassify: headers span multiple mbufs: "
544 "%d < %d\n", m->m_len, (hlen + hdrsize));
545 #endif
546 goto bad;
547 }
548
549 m->m_data += hlen;
550 m->m_len -= hlen;
551
552 hdr = mtod(m, void *);
553
554 if (ALTQ_NEEDS_CLASSIFY(ifq)) {
555 mtop->m_pkthdr.pattr_class =
556 (*ifq->altq_classify)(ifq->altq_clfier, m, af);
557 }
558 mtop->m_pkthdr.pattr_af = af;
559 mtop->m_pkthdr.pattr_hdr = hdr;
560
561 m->m_data -= hlen;
562 m->m_len += hlen;
563
564 return;
565
566 bad:
567 mtop->m_pkthdr.pattr_class = NULL;
568 mtop->m_pkthdr.pattr_hdr = NULL;
569 mtop->m_pkthdr.pattr_af = AF_UNSPEC;
570 }
571 #endif /* ALTQ */
572
573 #if defined (LLC) || defined (NETATALK)
574 static void
575 ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh)
576 {
577 struct ifqueue *inq = NULL;
578 int isr = 0;
579 struct llc *l;
580
581 if (m->m_len < sizeof(*eh) + sizeof(struct llc))
582 goto error;
583
584 l = (struct llc *)(eh+1);
585 switch (l->llc_dsap) {
586 #ifdef NETATALK
587 case LLC_SNAP_LSAP:
588 switch (l->llc_control) {
589 case LLC_UI:
590 if (l->llc_ssap != LLC_SNAP_LSAP)
591 goto error;
592
593 if (memcmp(&(l->llc_snap_org_code)[0],
594 at_org_code, sizeof(at_org_code)) == 0 &&
595 ntohs(l->llc_snap_ether_type) ==
596 ETHERTYPE_ATALK) {
597 inq = &atintrq2;
598 m_adj(m, sizeof(struct ether_header)
599 + sizeof(struct llc));
600 isr = NETISR_ATALK;
601 break;
602 }
603
604 if (memcmp(&(l->llc_snap_org_code)[0],
605 aarp_org_code,
606 sizeof(aarp_org_code)) == 0 &&
607 ntohs(l->llc_snap_ether_type) ==
608 ETHERTYPE_AARP) {
609 m_adj(m, sizeof(struct ether_header)
610 + sizeof(struct llc));
611 aarpinput(ifp, m); /* XXX queue? */
612 return;
613 }
614
615 default:
616 goto error;
617 }
618 break;
619 #endif
620 default:
621 goto noproto;
622 }
623
624 KASSERT(inq != NULL);
625 IFQ_ENQUEUE_ISR(inq, m, isr);
626 return;
627
628 noproto:
629 m_freem(m);
630 if_statinc(ifp, if_noproto);
631 return;
632 error:
633 m_freem(m);
634 if_statinc(ifp, if_ierrors);
635 return;
636 }
637 #endif /* defined (LLC) || defined (NETATALK) */
638
639 /*
640 * Process a received Ethernet packet;
641 * the packet is in the mbuf chain m with
642 * the ether header.
643 */
644 void
645 ether_input(struct ifnet *ifp, struct mbuf *m)
646 {
647 struct ethercom *ec = (struct ethercom *) ifp;
648 pktqueue_t *pktq = NULL;
649 struct ifqueue *inq = NULL;
650 uint16_t etype;
651 struct ether_header *eh;
652 size_t ehlen;
653 static int earlypkts;
654 int isr = 0;
655
656 KASSERT(!cpu_intr_p());
657 KASSERT((m->m_flags & M_PKTHDR) != 0);
658
659 if ((ifp->if_flags & IFF_UP) == 0)
660 goto drop;
661
662 #ifdef MBUFTRACE
663 m_claimm(m, &ec->ec_rx_mowner);
664 #endif
665
666 if (__predict_false(m->m_len < sizeof(*eh))) {
667 if ((m = m_pullup(m, sizeof(*eh))) == NULL) {
668 if_statinc(ifp, if_ierrors);
669 return;
670 }
671 }
672
673 eh = mtod(m, struct ether_header *);
674 etype = ntohs(eh->ether_type);
675 ehlen = sizeof(*eh);
676
677 if (__predict_false(earlypkts < 100 ||
678 entropy_epoch() == (unsigned)-1)) {
679 rnd_add_data(NULL, eh, ehlen, 0);
680 earlypkts++;
681 }
682
683 /*
684 * Determine if the packet is within its size limits. For MPLS the
685 * header length is variable, so we skip the check.
686 */
687 if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
688 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
689 #ifdef DIAGNOSTIC
690 mutex_enter(&bigpktpps_lock);
691 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
692 bigpktppslim)) {
693 printf("%s: discarding oversize frame (len=%d)\n",
694 ifp->if_xname, m->m_pkthdr.len);
695 }
696 mutex_exit(&bigpktpps_lock);
697 #endif
698 goto error;
699 }
700
701 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
702 /*
703 * If this is not a simplex interface, drop the packet
704 * if it came from us.
705 */
706 if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
707 memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
708 ETHER_ADDR_LEN) == 0) {
709 goto drop;
710 }
711
712 if (memcmp(etherbroadcastaddr,
713 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
714 m->m_flags |= M_BCAST;
715 else
716 m->m_flags |= M_MCAST;
717 if_statinc(ifp, if_imcasts);
718 }
719
720 /* If the CRC is still on the packet, trim it off. */
721 if (m->m_flags & M_HASFCS) {
722 m_adj(m, -ETHER_CRC_LEN);
723 m->m_flags &= ~M_HASFCS;
724 }
725
726 if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
727
728 if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
729 (ifp->if_flags & IFF_PROMISC) != 0 &&
730 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
731 ETHER_ADDR_LEN) != 0) {
732 m->m_flags |= M_PROMISC;
733 }
734
735 if ((m->m_flags & M_PROMISC) == 0) {
736 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
737 return;
738 if (m == NULL)
739 return;
740
741 eh = mtod(m, struct ether_header *);
742 etype = ntohs(eh->ether_type);
743 }
744
745 /*
746 * Processing a logical interfaces that are able
747 * to configure vlan(4).
748 */
749 #if NAGR > 0
750 if (ifp->if_lagg != NULL &&
751 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
752 m->m_flags &= ~M_PROMISC;
753 agr_input(ifp, m);
754 return;
755 }
756 #endif
757
758 /*
759 * VLAN processing.
760 *
761 * VLAN provides service delimiting so the frames are
762 * processed before other handlings. If a VLAN interface
763 * does not exist to take those frames, they're returned
764 * to ether_input().
765 */
766 if (vlan_has_tag(m) || etype == ETHERTYPE_VLAN) {
767 struct ether_vlan_header *evl = (void *)eh;
768 uint16_t vlan_id;
769
770 if (vlan_has_tag(m)) {
771 vlan_id = EVL_VLANOFTAG(vlan_get_tag(m));
772 } else {
773 if (m->m_len < sizeof(*evl))
774 goto error;
775
776 vlan_id = EVL_VLANOFTAG(ntohs(evl->evl_tag));
777 etype = ntohs(evl->evl_proto);
778 ehlen = sizeof(*evl);
779 }
780
781 if (vlan_id == 0) {
782 if (etype == ETHERTYPE_VLAN ||
783 etype == ETHERTYPE_QINQ)
784 goto drop;
785
786 /* XXX we should actually use the prio value? */
787 m->m_flags &= ~M_VLANTAG;
788 } else {
789 #if NVLAN > 0
790 if (ec->ec_nvlans > 0) {
791 m = vlan_input(ifp, m);
792
793 /* vlan_input() called ether_input() recursively */
794 if (m == NULL)
795 return;
796 }
797 #endif
798 /* drop VLAN frames not for this port. */
799 goto noproto;
800 }
801 }
802
803 #if NCARP > 0
804 if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
805 /*
806 * Clear M_PROMISC, in case the packet comes from a
807 * vlan.
808 */
809 m->m_flags &= ~M_PROMISC;
810 if (carp_input(m, (uint8_t *)&eh->ether_shost,
811 (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
812 return;
813 }
814 #endif
815
816 /*
817 * Handle protocols that expect to have the Ethernet header
818 * (and possibly FCS) intact.
819 */
820 switch (etype) {
821 #if NPPPOE > 0
822 case ETHERTYPE_PPPOEDISC:
823 pppoedisc_input(ifp, m);
824 return;
825
826 case ETHERTYPE_PPPOE:
827 pppoe_input(ifp, m);
828 return;
829 #endif
830
831 case ETHERTYPE_SLOWPROTOCOLS: {
832 uint8_t subtype;
833
834 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
835 goto error;
836
837 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
838 switch (subtype) {
839 #if NAGR > 0
840 case SLOWPROTOCOLS_SUBTYPE_LACP:
841 if (ifp->if_lagg != NULL) {
842 ieee8023ad_lacp_input(ifp, m);
843 return;
844 }
845 break;
846
847 case SLOWPROTOCOLS_SUBTYPE_MARKER:
848 if (ifp->if_lagg != NULL) {
849 ieee8023ad_marker_input(ifp, m);
850 return;
851 }
852 break;
853 #endif
854
855 default:
856 if (subtype == 0 || subtype > 10) {
857 /* illegal value */
858 goto error;
859 }
860 /* unknown subtype */
861 break;
862 }
863 }
864 /* FALLTHROUGH */
865 default:
866 if (m->m_flags & M_PROMISC)
867 goto drop;
868 }
869
870 /* If the CRC is still on the packet, trim it off. */
871 if (m->m_flags & M_HASFCS) {
872 m_adj(m, -ETHER_CRC_LEN);
873 m->m_flags &= ~M_HASFCS;
874 }
875
876 /* etype represents the size of the payload in this case */
877 if (etype <= ETHERMTU + sizeof(struct ether_header)) {
878 KASSERT(ehlen == sizeof(*eh));
879 #if defined (LLC) || defined (NETATALK)
880 ether_input_llc(ifp, m, eh);
881 return;
882 #else
883 /* ethertype of 0-1500 is regarded as noproto */
884 goto noproto;
885 #endif
886 }
887
888 /* Strip off the Ethernet header. */
889 m_adj(m, ehlen);
890
891 switch (etype) {
892 #ifdef INET
893 case ETHERTYPE_IP:
894 #ifdef GATEWAY
895 if (ipflow_fastforward(m))
896 return;
897 #endif
898 pktq = ip_pktq;
899 break;
900
901 case ETHERTYPE_ARP:
902 isr = NETISR_ARP;
903 inq = &arpintrq;
904 break;
905
906 case ETHERTYPE_REVARP:
907 revarpinput(m); /* XXX queue? */
908 return;
909 #endif
910
911 #ifdef INET6
912 case ETHERTYPE_IPV6:
913 if (__predict_false(!in6_present))
914 goto noproto;
915 #ifdef GATEWAY
916 if (ip6flow_fastforward(&m))
917 return;
918 #endif
919 pktq = ip6_pktq;
920 break;
921 #endif
922
923 #ifdef NETATALK
924 case ETHERTYPE_ATALK:
925 isr = NETISR_ATALK;
926 inq = &atintrq1;
927 break;
928
929 case ETHERTYPE_AARP:
930 aarpinput(ifp, m); /* XXX queue? */
931 return;
932 #endif
933
934 #ifdef MPLS
935 case ETHERTYPE_MPLS:
936 isr = NETISR_MPLS;
937 inq = &mplsintrq;
938 break;
939 #endif
940
941 default:
942 goto noproto;
943 }
944
945 if (__predict_true(pktq)) {
946 const uint32_t h = pktq_rps_hash(ðer_pktq_rps_hash_p, m);
947 if (__predict_false(!pktq_enqueue(pktq, m, h))) {
948 m_freem(m);
949 }
950 return;
951 }
952
953 if (__predict_false(!inq)) {
954 /* Should not happen. */
955 goto error;
956 }
957
958 IFQ_ENQUEUE_ISR(inq, m, isr);
959 return;
960
961 drop:
962 m_freem(m);
963 if_statinc(ifp, if_iqdrops);
964 return;
965 noproto:
966 m_freem(m);
967 if_statinc(ifp, if_noproto);
968 return;
969 error:
970 m_freem(m);
971 if_statinc(ifp, if_ierrors);
972 return;
973 }
974
975 /*
976 * Convert Ethernet address to printable (loggable) representation.
977 */
978 char *
979 ether_sprintf(const u_char *ap)
980 {
981 static char etherbuf[3 * ETHER_ADDR_LEN];
982 return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
983 }
984
985 char *
986 ether_snprintf(char *buf, size_t len, const u_char *ap)
987 {
988 char *cp = buf;
989 size_t i;
990
991 for (i = 0; i < len / 3; i++) {
992 *cp++ = hexdigits[*ap >> 4];
993 *cp++ = hexdigits[*ap++ & 0xf];
994 *cp++ = ':';
995 }
996 *--cp = '\0';
997 return buf;
998 }
999
1000 /*
1001 * Perform common duties while attaching to interface list
1002 */
1003 void
1004 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
1005 {
1006 struct ethercom *ec = (struct ethercom *)ifp;
1007 char xnamebuf[HOOKNAMSIZ];
1008
1009 ifp->if_type = IFT_ETHER;
1010 ifp->if_hdrlen = ETHER_HDR_LEN;
1011 ifp->if_dlt = DLT_EN10MB;
1012 ifp->if_mtu = ETHERMTU;
1013 ifp->if_output = ether_output;
1014 ifp->_if_input = ether_input;
1015 if (ifp->if_baudrate == 0)
1016 ifp->if_baudrate = IF_Mbps(10); /* just a default */
1017
1018 if (lla != NULL)
1019 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1020
1021 LIST_INIT(&ec->ec_multiaddrs);
1022 SIMPLEQ_INIT(&ec->ec_vids);
1023 ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
1024 ec->ec_flags = 0;
1025 ifp->if_broadcastaddr = etherbroadcastaddr;
1026 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1027 snprintf(xnamebuf, sizeof(xnamebuf),
1028 "%s-ether_ifdetachhooks", ifp->if_xname);
1029 ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
1030 #ifdef MBUFTRACE
1031 mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
1032 mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
1033 MOWNER_ATTACH(&ec->ec_tx_mowner);
1034 MOWNER_ATTACH(&ec->ec_rx_mowner);
1035 ifp->if_mowner = &ec->ec_tx_mowner;
1036 #endif
1037 }
1038
1039 void
1040 ether_ifdetach(struct ifnet *ifp)
1041 {
1042 struct ethercom *ec = (void *) ifp;
1043 struct ether_multi *enm;
1044
1045 IFNET_ASSERT_UNLOCKED(ifp);
1046 /*
1047 * Prevent further calls to ioctl (for example turning off
1048 * promiscuous mode from the bridge code), which eventually can
1049 * call if_init() which can cause panics because the interface
1050 * is in the process of being detached. Return device not configured
1051 * instead.
1052 */
1053 ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
1054 enxio);
1055
1056 simplehook_dohooks(ec->ec_ifdetach_hooks);
1057 KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
1058 simplehook_destroy(ec->ec_ifdetach_hooks);
1059
1060 bpf_detach(ifp);
1061
1062 ETHER_LOCK(ec);
1063 KASSERT(ec->ec_nvlans == 0);
1064 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1065 LIST_REMOVE(enm, enm_list);
1066 kmem_free(enm, sizeof(*enm));
1067 ec->ec_multicnt--;
1068 }
1069 ETHER_UNLOCK(ec);
1070
1071 mutex_obj_free(ec->ec_lock);
1072 ec->ec_lock = NULL;
1073
1074 ifp->if_mowner = NULL;
1075 MOWNER_DETACH(&ec->ec_rx_mowner);
1076 MOWNER_DETACH(&ec->ec_tx_mowner);
1077 }
1078
1079 void *
1080 ether_ifdetachhook_establish(struct ifnet *ifp,
1081 void (*fn)(void *), void *arg)
1082 {
1083 struct ethercom *ec;
1084 khook_t *hk;
1085
1086 if (ifp->if_type != IFT_ETHER)
1087 return NULL;
1088
1089 ec = (struct ethercom *)ifp;
1090 hk = simplehook_establish(ec->ec_ifdetach_hooks,
1091 fn, arg);
1092
1093 return (void *)hk;
1094 }
1095
1096 void
1097 ether_ifdetachhook_disestablish(struct ifnet *ifp,
1098 void *vhook, kmutex_t *lock)
1099 {
1100 struct ethercom *ec;
1101
1102 if (vhook == NULL)
1103 return;
1104
1105 ec = (struct ethercom *)ifp;
1106 simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
1107 }
1108
1109 #if 0
1110 /*
1111 * This is for reference. We have a table-driven version
1112 * of the little-endian crc32 generator, which is faster
1113 * than the double-loop.
1114 */
1115 uint32_t
1116 ether_crc32_le(const uint8_t *buf, size_t len)
1117 {
1118 uint32_t c, crc, carry;
1119 size_t i, j;
1120
1121 crc = 0xffffffffU; /* initial value */
1122
1123 for (i = 0; i < len; i++) {
1124 c = buf[i];
1125 for (j = 0; j < 8; j++) {
1126 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1127 crc >>= 1;
1128 c >>= 1;
1129 if (carry)
1130 crc = (crc ^ ETHER_CRC_POLY_LE);
1131 }
1132 }
1133
1134 return (crc);
1135 }
1136 #else
1137 uint32_t
1138 ether_crc32_le(const uint8_t *buf, size_t len)
1139 {
1140 static const uint32_t crctab[] = {
1141 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1142 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1143 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1144 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1145 };
1146 uint32_t crc;
1147 size_t i;
1148
1149 crc = 0xffffffffU; /* initial value */
1150
1151 for (i = 0; i < len; i++) {
1152 crc ^= buf[i];
1153 crc = (crc >> 4) ^ crctab[crc & 0xf];
1154 crc = (crc >> 4) ^ crctab[crc & 0xf];
1155 }
1156
1157 return (crc);
1158 }
1159 #endif
1160
1161 uint32_t
1162 ether_crc32_be(const uint8_t *buf, size_t len)
1163 {
1164 uint32_t c, crc, carry;
1165 size_t i, j;
1166
1167 crc = 0xffffffffU; /* initial value */
1168
1169 for (i = 0; i < len; i++) {
1170 c = buf[i];
1171 for (j = 0; j < 8; j++) {
1172 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1173 crc <<= 1;
1174 c >>= 1;
1175 if (carry)
1176 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1177 }
1178 }
1179
1180 return (crc);
1181 }
1182
1183 #ifdef INET
1184 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1185 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1186 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1187 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1188 #endif
1189 #ifdef INET6
1190 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1191 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1192 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1193 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1194 #endif
1195
1196 /*
1197 * ether_aton implementation, not using a static buffer.
1198 */
1199 int
1200 ether_aton_r(u_char *dest, size_t len, const char *str)
1201 {
1202 const u_char *cp = (const void *)str;
1203 u_char *ep;
1204
1205 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1206
1207 if (len < ETHER_ADDR_LEN)
1208 return ENOSPC;
1209
1210 ep = dest + ETHER_ADDR_LEN;
1211
1212 while (*cp) {
1213 if (!isxdigit(*cp))
1214 return EINVAL;
1215
1216 *dest = atox(*cp);
1217 cp++;
1218 if (isxdigit(*cp)) {
1219 *dest = (*dest << 4) | atox(*cp);
1220 cp++;
1221 }
1222 dest++;
1223
1224 if (dest == ep)
1225 return (*cp == '\0') ? 0 : ENAMETOOLONG;
1226
1227 switch (*cp) {
1228 case ':':
1229 case '-':
1230 case '.':
1231 cp++;
1232 break;
1233 }
1234 }
1235 return ENOBUFS;
1236 }
1237
1238 /*
1239 * Convert a sockaddr into an Ethernet address or range of Ethernet
1240 * addresses.
1241 */
1242 int
1243 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1244 uint8_t addrhi[ETHER_ADDR_LEN])
1245 {
1246 #ifdef INET
1247 const struct sockaddr_in *sin;
1248 #endif
1249 #ifdef INET6
1250 const struct sockaddr_in6 *sin6;
1251 #endif
1252
1253 switch (sa->sa_family) {
1254
1255 case AF_UNSPEC:
1256 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1257 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1258 break;
1259
1260 #ifdef INET
1261 case AF_INET:
1262 sin = satocsin(sa);
1263 if (sin->sin_addr.s_addr == INADDR_ANY) {
1264 /*
1265 * An IP address of INADDR_ANY means listen to
1266 * or stop listening to all of the Ethernet
1267 * multicast addresses used for IP.
1268 * (This is for the sake of IP multicast routers.)
1269 */
1270 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1271 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1272 } else {
1273 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1274 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1275 }
1276 break;
1277 #endif
1278 #ifdef INET6
1279 case AF_INET6:
1280 sin6 = satocsin6(sa);
1281 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1282 /*
1283 * An IP6 address of 0 means listen to or stop
1284 * listening to all of the Ethernet multicast
1285 * address used for IP6.
1286 * (This is used for multicast routers.)
1287 */
1288 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1289 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1290 } else {
1291 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1292 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1293 }
1294 break;
1295 #endif
1296
1297 default:
1298 return EAFNOSUPPORT;
1299 }
1300 return 0;
1301 }
1302
1303 /*
1304 * Add an Ethernet multicast address or range of addresses to the list for a
1305 * given interface.
1306 */
1307 int
1308 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1309 {
1310 struct ether_multi *enm, *_enm;
1311 u_char addrlo[ETHER_ADDR_LEN];
1312 u_char addrhi[ETHER_ADDR_LEN];
1313 int error = 0;
1314
1315 /* Allocate out of lock */
1316 enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
1317
1318 ETHER_LOCK(ec);
1319 error = ether_multiaddr(sa, addrlo, addrhi);
1320 if (error != 0)
1321 goto out;
1322
1323 /*
1324 * Verify that we have valid Ethernet multicast addresses.
1325 */
1326 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1327 error = EINVAL;
1328 goto out;
1329 }
1330
1331 /*
1332 * See if the address range is already in the list.
1333 */
1334 _enm = ether_lookup_multi(addrlo, addrhi, ec);
1335 if (_enm != NULL) {
1336 /*
1337 * Found it; just increment the reference count.
1338 */
1339 ++_enm->enm_refcount;
1340 error = 0;
1341 goto out;
1342 }
1343
1344 /*
1345 * Link a new multicast record into the interface's multicast list.
1346 */
1347 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
1348 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
1349 enm->enm_refcount = 1;
1350 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1351 ec->ec_multicnt++;
1352
1353 /*
1354 * Return ENETRESET to inform the driver that the list has changed
1355 * and its reception filter should be adjusted accordingly.
1356 */
1357 error = ENETRESET;
1358 enm = NULL;
1359
1360 out:
1361 ETHER_UNLOCK(ec);
1362 if (enm != NULL)
1363 kmem_free(enm, sizeof(*enm));
1364 return error;
1365 }
1366
1367 /*
1368 * Delete a multicast address record.
1369 */
1370 int
1371 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1372 {
1373 struct ether_multi *enm;
1374 u_char addrlo[ETHER_ADDR_LEN];
1375 u_char addrhi[ETHER_ADDR_LEN];
1376 int error;
1377
1378 ETHER_LOCK(ec);
1379 error = ether_multiaddr(sa, addrlo, addrhi);
1380 if (error != 0)
1381 goto error;
1382
1383 /*
1384 * Look up the address in our list.
1385 */
1386 enm = ether_lookup_multi(addrlo, addrhi, ec);
1387 if (enm == NULL) {
1388 error = ENXIO;
1389 goto error;
1390 }
1391 if (--enm->enm_refcount != 0) {
1392 /*
1393 * Still some claims to this record.
1394 */
1395 error = 0;
1396 goto error;
1397 }
1398
1399 /*
1400 * No remaining claims to this record; unlink and free it.
1401 */
1402 LIST_REMOVE(enm, enm_list);
1403 ec->ec_multicnt--;
1404 ETHER_UNLOCK(ec);
1405 kmem_free(enm, sizeof(*enm));
1406
1407 /*
1408 * Return ENETRESET to inform the driver that the list has changed
1409 * and its reception filter should be adjusted accordingly.
1410 */
1411 return ENETRESET;
1412
1413 error:
1414 ETHER_UNLOCK(ec);
1415 return error;
1416 }
1417
1418 void
1419 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1420 {
1421 ec->ec_ifflags_cb = cb;
1422 }
1423
1424 void
1425 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
1426 {
1427
1428 ec->ec_vlan_cb = cb;
1429 }
1430
1431 static int
1432 ether_ioctl_reinit(struct ethercom *ec)
1433 {
1434 struct ifnet *ifp = &ec->ec_if;
1435 int error;
1436
1437 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1438
1439 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1440 case IFF_RUNNING:
1441 /*
1442 * If interface is marked down and it is running,
1443 * then stop and disable it.
1444 */
1445 if_stop(ifp, 1);
1446 break;
1447 case IFF_UP:
1448 /*
1449 * If interface is marked up and it is stopped, then
1450 * start it.
1451 */
1452 return if_init(ifp);
1453 case IFF_UP | IFF_RUNNING:
1454 error = 0;
1455 if (ec->ec_ifflags_cb != NULL) {
1456 error = (*ec->ec_ifflags_cb)(ec);
1457 if (error == ENETRESET) {
1458 /*
1459 * Reset the interface to pick up
1460 * changes in any other flags that
1461 * affect the hardware state.
1462 */
1463 return if_init(ifp);
1464 }
1465 } else
1466 error = if_init(ifp);
1467 return error;
1468 case 0:
1469 break;
1470 }
1471
1472 return 0;
1473 }
1474
1475 /*
1476 * Common ioctls for Ethernet interfaces. Note, we must be
1477 * called at splnet().
1478 */
1479 int
1480 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1481 {
1482 struct ethercom *ec = (void *)ifp;
1483 struct eccapreq *eccr;
1484 struct ifreq *ifr = (struct ifreq *)data;
1485 struct if_laddrreq *iflr = data;
1486 const struct sockaddr_dl *sdl;
1487 static const uint8_t zero[ETHER_ADDR_LEN];
1488 int error;
1489
1490 switch (cmd) {
1491 case SIOCINITIFADDR:
1492 {
1493 struct ifaddr *ifa = (struct ifaddr *)data;
1494 if (ifa->ifa_addr->sa_family != AF_LINK
1495 && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
1496 (IFF_UP | IFF_RUNNING)) {
1497 ifp->if_flags |= IFF_UP;
1498 if ((error = if_init(ifp)) != 0)
1499 return error;
1500 }
1501 #ifdef INET
1502 if (ifa->ifa_addr->sa_family == AF_INET)
1503 arp_ifinit(ifp, ifa);
1504 #endif
1505 return 0;
1506 }
1507
1508 case SIOCSIFMTU:
1509 {
1510 int maxmtu;
1511
1512 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1513 maxmtu = ETHERMTU_JUMBO;
1514 else
1515 maxmtu = ETHERMTU;
1516
1517 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1518 return EINVAL;
1519 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1520 return error;
1521 else if (ifp->if_flags & IFF_UP) {
1522 /* Make sure the device notices the MTU change. */
1523 return if_init(ifp);
1524 } else
1525 return 0;
1526 }
1527
1528 case SIOCSIFFLAGS:
1529 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1530 return error;
1531 return ether_ioctl_reinit(ec);
1532 case SIOCGIFFLAGS:
1533 error = ifioctl_common(ifp, cmd, data);
1534 if (error == 0) {
1535 /* Set IFF_ALLMULTI for backcompat */
1536 ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
1537 IFF_ALLMULTI : 0;
1538 }
1539 return error;
1540 case SIOCGETHERCAP:
1541 eccr = (struct eccapreq *)data;
1542 eccr->eccr_capabilities = ec->ec_capabilities;
1543 eccr->eccr_capenable = ec->ec_capenable;
1544 return 0;
1545 case SIOCSETHERCAP:
1546 eccr = (struct eccapreq *)data;
1547 if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
1548 return EINVAL;
1549 if (eccr->eccr_capenable == ec->ec_capenable)
1550 return 0;
1551 #if 0 /* notyet */
1552 ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
1553 | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
1554 #else
1555 ec->ec_capenable = eccr->eccr_capenable;
1556 #endif
1557 return ether_ioctl_reinit(ec);
1558 case SIOCADDMULTI:
1559 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1560 case SIOCDELMULTI:
1561 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1562 case SIOCSIFMEDIA:
1563 case SIOCGIFMEDIA:
1564 if (ec->ec_mii != NULL)
1565 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
1566 cmd);
1567 else if (ec->ec_ifmedia != NULL)
1568 return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
1569 else
1570 return ENOTTY;
1571 break;
1572 case SIOCALIFADDR:
1573 sdl = satocsdl(sstocsa(&iflr->addr));
1574 if (sdl->sdl_family != AF_LINK)
1575 ;
1576 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1577 return EINVAL;
1578 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1579 return EINVAL;
1580 /*FALLTHROUGH*/
1581 default:
1582 return ifioctl_common(ifp, cmd, data);
1583 }
1584 return 0;
1585 }
1586
1587 /*
1588 * Enable/disable passing VLAN packets if the parent interface supports it.
1589 * Return:
1590 * 0: Ok
1591 * -1: Parent interface does not support vlans
1592 * >0: Error
1593 */
1594 int
1595 ether_enable_vlan_mtu(struct ifnet *ifp)
1596 {
1597 int error;
1598 struct ethercom *ec = (void *)ifp;
1599
1600 /* Parent does not support VLAN's */
1601 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1602 return -1;
1603
1604 /*
1605 * Parent supports the VLAN_MTU capability,
1606 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1607 * enable it.
1608 */
1609 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1610
1611 /* Interface is down, defer for later */
1612 if ((ifp->if_flags & IFF_UP) == 0)
1613 return 0;
1614
1615 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1616 return 0;
1617
1618 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1619 return error;
1620 }
1621
1622 int
1623 ether_disable_vlan_mtu(struct ifnet *ifp)
1624 {
1625 int error;
1626 struct ethercom *ec = (void *)ifp;
1627
1628 /* We still have VLAN's, defer for later */
1629 if (ec->ec_nvlans != 0)
1630 return 0;
1631
1632 /* Parent does not support VLAB's, nothing to do. */
1633 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1634 return -1;
1635
1636 /*
1637 * Disable Tx/Rx of VLAN-sized frames.
1638 */
1639 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1640
1641 /* Interface is down, defer for later */
1642 if ((ifp->if_flags & IFF_UP) == 0)
1643 return 0;
1644
1645 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1646 return 0;
1647
1648 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1649 return error;
1650 }
1651
1652 /*
1653 * Add and delete VLAN TAG
1654 */
1655 int
1656 ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
1657 {
1658 struct ethercom *ec = (void *)ifp;
1659 struct vlanid_list *vidp;
1660 bool vlanmtu_enabled;
1661 uint16_t vid = EVL_VLANOFTAG(vtag);
1662 int error;
1663
1664 vlanmtu_enabled = false;
1665
1666 /* Add a vid to the list */
1667 vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
1668 vidp->vid = vid;
1669
1670 ETHER_LOCK(ec);
1671 ec->ec_nvlans++;
1672 SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
1673 ETHER_UNLOCK(ec);
1674
1675 if (ec->ec_nvlans == 1) {
1676 IFNET_LOCK(ifp);
1677 error = ether_enable_vlan_mtu(ifp);
1678 IFNET_UNLOCK(ifp);
1679
1680 if (error == 0) {
1681 vlanmtu_enabled = true;
1682 } else if (error != -1) {
1683 goto fail;
1684 }
1685 }
1686
1687 if (ec->ec_vlan_cb != NULL) {
1688 error = (*ec->ec_vlan_cb)(ec, vid, true);
1689 if (error != 0)
1690 goto fail;
1691 }
1692
1693 if (vlanmtu_status != NULL)
1694 *vlanmtu_status = vlanmtu_enabled;
1695
1696 return 0;
1697 fail:
1698 ETHER_LOCK(ec);
1699 ec->ec_nvlans--;
1700 SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
1701 ETHER_UNLOCK(ec);
1702
1703 if (vlanmtu_enabled) {
1704 IFNET_LOCK(ifp);
1705 (void)ether_disable_vlan_mtu(ifp);
1706 IFNET_UNLOCK(ifp);
1707 }
1708
1709 kmem_free(vidp, sizeof(*vidp));
1710
1711 return error;
1712 }
1713
1714 int
1715 ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
1716 {
1717 struct ethercom *ec = (void *)ifp;
1718 struct vlanid_list *vidp;
1719 uint16_t vid = EVL_VLANOFTAG(vtag);
1720
1721 ETHER_LOCK(ec);
1722 SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
1723 if (vidp->vid == vid) {
1724 SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
1725 vlanid_list, vid_list);
1726 ec->ec_nvlans--;
1727 break;
1728 }
1729 }
1730 ETHER_UNLOCK(ec);
1731
1732 if (vidp == NULL)
1733 return ENOENT;
1734
1735 if (ec->ec_vlan_cb != NULL) {
1736 (void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
1737 }
1738
1739 if (ec->ec_nvlans == 0) {
1740 IFNET_LOCK(ifp);
1741 (void)ether_disable_vlan_mtu(ifp);
1742 IFNET_UNLOCK(ifp);
1743 }
1744
1745 kmem_free(vidp, sizeof(*vidp));
1746
1747 return 0;
1748 }
1749
1750 static int
1751 ether_multicast_sysctl(SYSCTLFN_ARGS)
1752 {
1753 struct ether_multi *enm;
1754 struct ifnet *ifp;
1755 struct ethercom *ec;
1756 int error = 0;
1757 size_t written;
1758 struct psref psref;
1759 int bound;
1760 unsigned int multicnt;
1761 struct ether_multi_sysctl *addrs;
1762 int i;
1763
1764 if (namelen != 1)
1765 return EINVAL;
1766
1767 bound = curlwp_bind();
1768 ifp = if_get_byindex(name[0], &psref);
1769 if (ifp == NULL) {
1770 error = ENODEV;
1771 goto out;
1772 }
1773 if (ifp->if_type != IFT_ETHER) {
1774 if_put(ifp, &psref);
1775 *oldlenp = 0;
1776 goto out;
1777 }
1778 ec = (struct ethercom *)ifp;
1779
1780 if (oldp == NULL) {
1781 if_put(ifp, &psref);
1782 *oldlenp = ec->ec_multicnt * sizeof(*addrs);
1783 goto out;
1784 }
1785
1786 /*
1787 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
1788 * is sleepable, while holding it. Copy data to a local buffer first
1789 * with the lock taken and then call sysctl_copyout without holding it.
1790 */
1791 retry:
1792 multicnt = ec->ec_multicnt;
1793
1794 if (multicnt == 0) {
1795 if_put(ifp, &psref);
1796 *oldlenp = 0;
1797 goto out;
1798 }
1799
1800 addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
1801
1802 ETHER_LOCK(ec);
1803 if (multicnt != ec->ec_multicnt) {
1804 /* The number of multicast addresses has changed */
1805 ETHER_UNLOCK(ec);
1806 kmem_free(addrs, sizeof(*addrs) * multicnt);
1807 goto retry;
1808 }
1809
1810 i = 0;
1811 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1812 struct ether_multi_sysctl *addr = &addrs[i];
1813 addr->enm_refcount = enm->enm_refcount;
1814 memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1815 memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1816 i++;
1817 }
1818 ETHER_UNLOCK(ec);
1819
1820 error = 0;
1821 written = 0;
1822 for (i = 0; i < multicnt; i++) {
1823 struct ether_multi_sysctl *addr = &addrs[i];
1824
1825 if (written + sizeof(*addr) > *oldlenp)
1826 break;
1827 error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
1828 if (error)
1829 break;
1830 written += sizeof(*addr);
1831 oldp = (char *)oldp + sizeof(*addr);
1832 }
1833 kmem_free(addrs, sizeof(*addrs) * multicnt);
1834
1835 if_put(ifp, &psref);
1836
1837 *oldlenp = written;
1838 out:
1839 curlwp_bindx(bound);
1840 return error;
1841 }
1842
1843 static void
1844 ether_sysctl_setup(struct sysctllog **clog)
1845 {
1846 const struct sysctlnode *rnode = NULL;
1847
1848 sysctl_createv(clog, 0, NULL, &rnode,
1849 CTLFLAG_PERMANENT,
1850 CTLTYPE_NODE, "ether",
1851 SYSCTL_DESCR("Ethernet-specific information"),
1852 NULL, 0, NULL, 0,
1853 CTL_NET, CTL_CREATE, CTL_EOL);
1854
1855 sysctl_createv(clog, 0, &rnode, NULL,
1856 CTLFLAG_PERMANENT,
1857 CTLTYPE_NODE, "multicast",
1858 SYSCTL_DESCR("multicast addresses"),
1859 ether_multicast_sysctl, 0, NULL, 0,
1860 CTL_CREATE, CTL_EOL);
1861
1862 sysctl_createv(clog, 0, &rnode, NULL,
1863 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1864 CTLTYPE_STRING, "rps_hash",
1865 SYSCTL_DESCR("Interface rps hash function control"),
1866 sysctl_pktq_rps_hash_handler, 0, (void *)ðer_pktq_rps_hash_p,
1867 PKTQ_RPS_HASH_NAME_LEN,
1868 CTL_CREATE, CTL_EOL);
1869 }
1870
1871 void
1872 etherinit(void)
1873 {
1874
1875 #ifdef DIAGNOSTIC
1876 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1877 #endif
1878 ether_pktq_rps_hash_p = pktq_rps_hash_default;
1879 ether_sysctl_setup(NULL);
1880 }
1881