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