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