if_ethersubr.c revision 1.322 1 /* $NetBSD: if_ethersubr.c,v 1.322 2022/11/15 09:14:28 roy 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.322 2022/11/15 09:14:28 roy 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 /* Strip off the Ethernet header. */
890 m_adj(m, ehlen);
891
892 switch (etype) {
893 #ifdef INET
894 case ETHERTYPE_IP:
895 #ifdef GATEWAY
896 if (ipflow_fastforward(m))
897 return;
898 #endif
899 pktq = ip_pktq;
900 rps_hash = atomic_load_relaxed(ðer_pktq_rps_hash_p);
901 break;
902
903 case ETHERTYPE_ARP:
904 pktq = arp_pktq;
905 break;
906
907 case ETHERTYPE_REVARP:
908 revarpinput(m); /* XXX queue? */
909 return;
910 #endif
911
912 #ifdef INET6
913 case ETHERTYPE_IPV6:
914 if (__predict_false(!in6_present))
915 goto noproto;
916 #ifdef GATEWAY
917 if (ip6flow_fastforward(&m))
918 return;
919 #endif
920 pktq = ip6_pktq;
921 rps_hash = atomic_load_relaxed(ðer_pktq_rps_hash_p);
922 break;
923 #endif
924
925 #ifdef NETATALK
926 case ETHERTYPE_ATALK:
927 pktq = at_pktq1;
928 break;
929
930 case ETHERTYPE_AARP:
931 aarpinput(ifp, m); /* XXX queue? */
932 return;
933 #endif
934
935 #ifdef MPLS
936 case ETHERTYPE_MPLS:
937 pktq = mpls_pktq;
938 break;
939 #endif
940
941 default:
942 goto noproto;
943 }
944
945 KASSERT(pktq != NULL);
946 const uint32_t h = rps_hash ? pktq_rps_hash(&rps_hash, m) : 0;
947 if (__predict_false(!pktq_enqueue(pktq, m, h))) {
948 m_freem(m);
949 }
950 return;
951
952 drop:
953 m_freem(m);
954 if_statinc(ifp, if_iqdrops);
955 return;
956 noproto:
957 m_freem(m);
958 if_statinc(ifp, if_noproto);
959 return;
960 error:
961 m_freem(m);
962 if_statinc(ifp, if_ierrors);
963 return;
964 }
965
966 static void
967 ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
968 {
969 struct ether_vlan_header evl;
970 struct m_hdr mh, md;
971
972 KASSERT(bp != NULL);
973
974 if (!vlan_has_tag(m)) {
975 bpf_mtap3(bp, m, direction);
976 return;
977 }
978
979 memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN);
980 evl.evl_proto = evl.evl_encap_proto;
981 evl.evl_encap_proto = htons(ETHERTYPE_VLAN);
982 evl.evl_tag = htons(vlan_get_tag(m));
983
984 md.mh_flags = 0;
985 md.mh_data = m->m_data + ETHER_HDR_LEN;
986 md.mh_len = m->m_len - ETHER_HDR_LEN;
987 md.mh_next = m->m_next;
988
989 mh.mh_flags = 0;
990 mh.mh_data = (char *)&evl;
991 mh.mh_len = sizeof(evl);
992 mh.mh_next = (struct mbuf *)&md;
993
994 bpf_mtap3(bp, (struct mbuf *)&mh, direction);
995 }
996
997 /*
998 * Convert Ethernet address to printable (loggable) representation.
999 */
1000 char *
1001 ether_sprintf(const u_char *ap)
1002 {
1003 static char etherbuf[3 * ETHER_ADDR_LEN];
1004 return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
1005 }
1006
1007 char *
1008 ether_snprintf(char *buf, size_t len, const u_char *ap)
1009 {
1010 char *cp = buf;
1011 size_t i;
1012
1013 for (i = 0; i < len / 3; i++) {
1014 *cp++ = hexdigits[*ap >> 4];
1015 *cp++ = hexdigits[*ap++ & 0xf];
1016 *cp++ = ':';
1017 }
1018 *--cp = '\0';
1019 return buf;
1020 }
1021
1022 /*
1023 * Perform common duties while attaching to interface list
1024 */
1025 void
1026 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
1027 {
1028 struct ethercom *ec = (struct ethercom *)ifp;
1029 char xnamebuf[HOOKNAMSIZ];
1030
1031 ifp->if_type = IFT_ETHER;
1032 ifp->if_hdrlen = ETHER_HDR_LEN;
1033 ifp->if_dlt = DLT_EN10MB;
1034 ifp->if_mtu = ETHERMTU;
1035 ifp->if_output = ether_output;
1036 ifp->_if_input = ether_input;
1037 ifp->if_bpf_mtap = ether_bpf_mtap;
1038 if (ifp->if_baudrate == 0)
1039 ifp->if_baudrate = IF_Mbps(10); /* just a default */
1040
1041 if (lla != NULL)
1042 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1043
1044 LIST_INIT(&ec->ec_multiaddrs);
1045 SIMPLEQ_INIT(&ec->ec_vids);
1046 ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
1047 ec->ec_flags = 0;
1048 ifp->if_broadcastaddr = etherbroadcastaddr;
1049 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1050 snprintf(xnamebuf, sizeof(xnamebuf),
1051 "%s-ether_ifdetachhooks", ifp->if_xname);
1052 ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
1053 #ifdef MBUFTRACE
1054 mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
1055 mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
1056 MOWNER_ATTACH(&ec->ec_tx_mowner);
1057 MOWNER_ATTACH(&ec->ec_rx_mowner);
1058 ifp->if_mowner = &ec->ec_tx_mowner;
1059 #endif
1060 }
1061
1062 void
1063 ether_ifdetach(struct ifnet *ifp)
1064 {
1065 struct ethercom *ec = (void *) ifp;
1066 struct ether_multi *enm;
1067
1068 IFNET_ASSERT_UNLOCKED(ifp);
1069 /*
1070 * Prevent further calls to ioctl (for example turning off
1071 * promiscuous mode from the bridge code), which eventually can
1072 * call if_init() which can cause panics because the interface
1073 * is in the process of being detached. Return device not configured
1074 * instead.
1075 */
1076 ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
1077 enxio);
1078
1079 simplehook_dohooks(ec->ec_ifdetach_hooks);
1080 KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
1081 simplehook_destroy(ec->ec_ifdetach_hooks);
1082
1083 bpf_detach(ifp);
1084
1085 ETHER_LOCK(ec);
1086 KASSERT(ec->ec_nvlans == 0);
1087 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1088 LIST_REMOVE(enm, enm_list);
1089 kmem_free(enm, sizeof(*enm));
1090 ec->ec_multicnt--;
1091 }
1092 ETHER_UNLOCK(ec);
1093
1094 mutex_obj_free(ec->ec_lock);
1095 ec->ec_lock = NULL;
1096
1097 ifp->if_mowner = NULL;
1098 MOWNER_DETACH(&ec->ec_rx_mowner);
1099 MOWNER_DETACH(&ec->ec_tx_mowner);
1100 }
1101
1102 void *
1103 ether_ifdetachhook_establish(struct ifnet *ifp,
1104 void (*fn)(void *), void *arg)
1105 {
1106 struct ethercom *ec;
1107 khook_t *hk;
1108
1109 if (ifp->if_type != IFT_ETHER)
1110 return NULL;
1111
1112 ec = (struct ethercom *)ifp;
1113 hk = simplehook_establish(ec->ec_ifdetach_hooks,
1114 fn, arg);
1115
1116 return (void *)hk;
1117 }
1118
1119 void
1120 ether_ifdetachhook_disestablish(struct ifnet *ifp,
1121 void *vhook, kmutex_t *lock)
1122 {
1123 struct ethercom *ec;
1124
1125 if (vhook == NULL)
1126 return;
1127
1128 ec = (struct ethercom *)ifp;
1129 simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
1130 }
1131
1132 #if 0
1133 /*
1134 * This is for reference. We have a table-driven version
1135 * of the little-endian crc32 generator, which is faster
1136 * than the double-loop.
1137 */
1138 uint32_t
1139 ether_crc32_le(const uint8_t *buf, size_t len)
1140 {
1141 uint32_t c, crc, carry;
1142 size_t i, j;
1143
1144 crc = 0xffffffffU; /* initial value */
1145
1146 for (i = 0; i < len; i++) {
1147 c = buf[i];
1148 for (j = 0; j < 8; j++) {
1149 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1150 crc >>= 1;
1151 c >>= 1;
1152 if (carry)
1153 crc = (crc ^ ETHER_CRC_POLY_LE);
1154 }
1155 }
1156
1157 return (crc);
1158 }
1159 #else
1160 uint32_t
1161 ether_crc32_le(const uint8_t *buf, size_t len)
1162 {
1163 static const uint32_t crctab[] = {
1164 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1165 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1166 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1167 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1168 };
1169 uint32_t crc;
1170 size_t i;
1171
1172 crc = 0xffffffffU; /* initial value */
1173
1174 for (i = 0; i < len; i++) {
1175 crc ^= buf[i];
1176 crc = (crc >> 4) ^ crctab[crc & 0xf];
1177 crc = (crc >> 4) ^ crctab[crc & 0xf];
1178 }
1179
1180 return (crc);
1181 }
1182 #endif
1183
1184 uint32_t
1185 ether_crc32_be(const uint8_t *buf, size_t len)
1186 {
1187 uint32_t c, crc, carry;
1188 size_t i, j;
1189
1190 crc = 0xffffffffU; /* initial value */
1191
1192 for (i = 0; i < len; i++) {
1193 c = buf[i];
1194 for (j = 0; j < 8; j++) {
1195 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1196 crc <<= 1;
1197 c >>= 1;
1198 if (carry)
1199 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1200 }
1201 }
1202
1203 return (crc);
1204 }
1205
1206 #ifdef INET
1207 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1208 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1209 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1210 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1211 #endif
1212 #ifdef INET6
1213 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1214 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1215 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1216 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1217 #endif
1218
1219 /*
1220 * ether_aton implementation, not using a static buffer.
1221 */
1222 int
1223 ether_aton_r(u_char *dest, size_t len, const char *str)
1224 {
1225 const u_char *cp = (const void *)str;
1226 u_char *ep;
1227
1228 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1229
1230 if (len < ETHER_ADDR_LEN)
1231 return ENOSPC;
1232
1233 ep = dest + ETHER_ADDR_LEN;
1234
1235 while (*cp) {
1236 if (!isxdigit(*cp))
1237 return EINVAL;
1238
1239 *dest = atox(*cp);
1240 cp++;
1241 if (isxdigit(*cp)) {
1242 *dest = (*dest << 4) | atox(*cp);
1243 cp++;
1244 }
1245 dest++;
1246
1247 if (dest == ep)
1248 return (*cp == '\0') ? 0 : ENAMETOOLONG;
1249
1250 switch (*cp) {
1251 case ':':
1252 case '-':
1253 case '.':
1254 cp++;
1255 break;
1256 }
1257 }
1258 return ENOBUFS;
1259 }
1260
1261 /*
1262 * Convert a sockaddr into an Ethernet address or range of Ethernet
1263 * addresses.
1264 */
1265 int
1266 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1267 uint8_t addrhi[ETHER_ADDR_LEN])
1268 {
1269 #ifdef INET
1270 const struct sockaddr_in *sin;
1271 #endif
1272 #ifdef INET6
1273 const struct sockaddr_in6 *sin6;
1274 #endif
1275
1276 switch (sa->sa_family) {
1277
1278 case AF_UNSPEC:
1279 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1280 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1281 break;
1282
1283 #ifdef INET
1284 case AF_INET:
1285 sin = satocsin(sa);
1286 if (sin->sin_addr.s_addr == INADDR_ANY) {
1287 /*
1288 * An IP address of INADDR_ANY means listen to
1289 * or stop listening to all of the Ethernet
1290 * multicast addresses used for IP.
1291 * (This is for the sake of IP multicast routers.)
1292 */
1293 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1294 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1295 } else {
1296 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1297 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1298 }
1299 break;
1300 #endif
1301 #ifdef INET6
1302 case AF_INET6:
1303 sin6 = satocsin6(sa);
1304 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1305 /*
1306 * An IP6 address of 0 means listen to or stop
1307 * listening to all of the Ethernet multicast
1308 * address used for IP6.
1309 * (This is used for multicast routers.)
1310 */
1311 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1312 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1313 } else {
1314 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1315 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1316 }
1317 break;
1318 #endif
1319
1320 default:
1321 return EAFNOSUPPORT;
1322 }
1323 return 0;
1324 }
1325
1326 /*
1327 * Add an Ethernet multicast address or range of addresses to the list for a
1328 * given interface.
1329 */
1330 int
1331 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1332 {
1333 struct ether_multi *enm, *_enm;
1334 u_char addrlo[ETHER_ADDR_LEN];
1335 u_char addrhi[ETHER_ADDR_LEN];
1336 int error = 0;
1337
1338 /* Allocate out of lock */
1339 enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
1340
1341 ETHER_LOCK(ec);
1342 error = ether_multiaddr(sa, addrlo, addrhi);
1343 if (error != 0)
1344 goto out;
1345
1346 /*
1347 * Verify that we have valid Ethernet multicast addresses.
1348 */
1349 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1350 error = EINVAL;
1351 goto out;
1352 }
1353
1354 /*
1355 * See if the address range is already in the list.
1356 */
1357 _enm = ether_lookup_multi(addrlo, addrhi, ec);
1358 if (_enm != NULL) {
1359 /*
1360 * Found it; just increment the reference count.
1361 */
1362 ++_enm->enm_refcount;
1363 error = 0;
1364 goto out;
1365 }
1366
1367 /*
1368 * Link a new multicast record into the interface's multicast list.
1369 */
1370 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
1371 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
1372 enm->enm_refcount = 1;
1373 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1374 ec->ec_multicnt++;
1375
1376 /*
1377 * Return ENETRESET to inform the driver that the list has changed
1378 * and its reception filter should be adjusted accordingly.
1379 */
1380 error = ENETRESET;
1381 enm = NULL;
1382
1383 out:
1384 ETHER_UNLOCK(ec);
1385 if (enm != NULL)
1386 kmem_free(enm, sizeof(*enm));
1387 return error;
1388 }
1389
1390 /*
1391 * Delete a multicast address record.
1392 */
1393 int
1394 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1395 {
1396 struct ether_multi *enm;
1397 u_char addrlo[ETHER_ADDR_LEN];
1398 u_char addrhi[ETHER_ADDR_LEN];
1399 int error;
1400
1401 ETHER_LOCK(ec);
1402 error = ether_multiaddr(sa, addrlo, addrhi);
1403 if (error != 0)
1404 goto error;
1405
1406 /*
1407 * Look up the address in our list.
1408 */
1409 enm = ether_lookup_multi(addrlo, addrhi, ec);
1410 if (enm == NULL) {
1411 error = ENXIO;
1412 goto error;
1413 }
1414 if (--enm->enm_refcount != 0) {
1415 /*
1416 * Still some claims to this record.
1417 */
1418 error = 0;
1419 goto error;
1420 }
1421
1422 /*
1423 * No remaining claims to this record; unlink and free it.
1424 */
1425 LIST_REMOVE(enm, enm_list);
1426 ec->ec_multicnt--;
1427 ETHER_UNLOCK(ec);
1428 kmem_free(enm, sizeof(*enm));
1429
1430 /*
1431 * Return ENETRESET to inform the driver that the list has changed
1432 * and its reception filter should be adjusted accordingly.
1433 */
1434 return ENETRESET;
1435
1436 error:
1437 ETHER_UNLOCK(ec);
1438 return error;
1439 }
1440
1441 void
1442 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1443 {
1444 ec->ec_ifflags_cb = cb;
1445 }
1446
1447 void
1448 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
1449 {
1450
1451 ec->ec_vlan_cb = cb;
1452 }
1453
1454 static int
1455 ether_ioctl_reinit(struct ethercom *ec)
1456 {
1457 struct ifnet *ifp = &ec->ec_if;
1458 int error;
1459
1460 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1461
1462 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1463 case IFF_RUNNING:
1464 /*
1465 * If interface is marked down and it is running,
1466 * then stop and disable it.
1467 */
1468 if_stop(ifp, 1);
1469 break;
1470 case IFF_UP:
1471 /*
1472 * If interface is marked up and it is stopped, then
1473 * start it.
1474 */
1475 return if_init(ifp);
1476 case IFF_UP | IFF_RUNNING:
1477 error = 0;
1478 if (ec->ec_ifflags_cb != NULL) {
1479 error = (*ec->ec_ifflags_cb)(ec);
1480 if (error == ENETRESET) {
1481 /*
1482 * Reset the interface to pick up
1483 * changes in any other flags that
1484 * affect the hardware state.
1485 */
1486 return if_init(ifp);
1487 }
1488 } else
1489 error = if_init(ifp);
1490 return error;
1491 case 0:
1492 break;
1493 }
1494
1495 return 0;
1496 }
1497
1498 /*
1499 * Common ioctls for Ethernet interfaces. Note, we must be
1500 * called at splnet().
1501 */
1502 int
1503 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1504 {
1505 struct ethercom *ec = (void *)ifp;
1506 struct eccapreq *eccr;
1507 struct ifreq *ifr = (struct ifreq *)data;
1508 struct if_laddrreq *iflr = data;
1509 const struct sockaddr_dl *sdl;
1510 static const uint8_t zero[ETHER_ADDR_LEN];
1511 int error;
1512
1513 switch (cmd) {
1514 case SIOCINITIFADDR:
1515 {
1516 struct ifaddr *ifa = (struct ifaddr *)data;
1517 if (ifa->ifa_addr->sa_family != AF_LINK
1518 && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
1519 (IFF_UP | IFF_RUNNING)) {
1520 ifp->if_flags |= IFF_UP;
1521 if ((error = if_init(ifp)) != 0)
1522 return error;
1523 }
1524 #ifdef INET
1525 if (ifa->ifa_addr->sa_family == AF_INET)
1526 arp_ifinit(ifp, ifa);
1527 #endif
1528 return 0;
1529 }
1530
1531 case SIOCSIFMTU:
1532 {
1533 int maxmtu;
1534
1535 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1536 maxmtu = ETHERMTU_JUMBO;
1537 else
1538 maxmtu = ETHERMTU;
1539
1540 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1541 return EINVAL;
1542 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1543 return error;
1544 else if (ifp->if_flags & IFF_UP) {
1545 /* Make sure the device notices the MTU change. */
1546 return if_init(ifp);
1547 } else
1548 return 0;
1549 }
1550
1551 case SIOCSIFFLAGS:
1552 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1553 return error;
1554 return ether_ioctl_reinit(ec);
1555 case SIOCGIFFLAGS:
1556 error = ifioctl_common(ifp, cmd, data);
1557 if (error == 0) {
1558 /* Set IFF_ALLMULTI for backcompat */
1559 ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
1560 IFF_ALLMULTI : 0;
1561 }
1562 return error;
1563 case SIOCGETHERCAP:
1564 eccr = (struct eccapreq *)data;
1565 eccr->eccr_capabilities = ec->ec_capabilities;
1566 eccr->eccr_capenable = ec->ec_capenable;
1567 return 0;
1568 case SIOCSETHERCAP:
1569 eccr = (struct eccapreq *)data;
1570 if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
1571 return EINVAL;
1572 if (eccr->eccr_capenable == ec->ec_capenable)
1573 return 0;
1574 #if 0 /* notyet */
1575 ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
1576 | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
1577 #else
1578 ec->ec_capenable = eccr->eccr_capenable;
1579 #endif
1580 return ether_ioctl_reinit(ec);
1581 case SIOCADDMULTI:
1582 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1583 case SIOCDELMULTI:
1584 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1585 case SIOCSIFMEDIA:
1586 case SIOCGIFMEDIA:
1587 if (ec->ec_mii != NULL)
1588 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
1589 cmd);
1590 else if (ec->ec_ifmedia != NULL)
1591 return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
1592 else
1593 return ENOTTY;
1594 break;
1595 case SIOCALIFADDR:
1596 sdl = satocsdl(sstocsa(&iflr->addr));
1597 if (sdl->sdl_family != AF_LINK)
1598 ;
1599 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1600 return EINVAL;
1601 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1602 return EINVAL;
1603 /*FALLTHROUGH*/
1604 default:
1605 return ifioctl_common(ifp, cmd, data);
1606 }
1607 return 0;
1608 }
1609
1610 /*
1611 * Enable/disable passing VLAN packets if the parent interface supports it.
1612 * Return:
1613 * 0: Ok
1614 * -1: Parent interface does not support vlans
1615 * >0: Error
1616 */
1617 int
1618 ether_enable_vlan_mtu(struct ifnet *ifp)
1619 {
1620 int error;
1621 struct ethercom *ec = (void *)ifp;
1622
1623 /* Parent does not support VLAN's */
1624 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1625 return -1;
1626
1627 /*
1628 * Parent supports the VLAN_MTU capability,
1629 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1630 * enable it.
1631 */
1632 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1633
1634 /* Interface is down, defer for later */
1635 if ((ifp->if_flags & IFF_UP) == 0)
1636 return 0;
1637
1638 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1639 return 0;
1640
1641 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1642 return error;
1643 }
1644
1645 int
1646 ether_disable_vlan_mtu(struct ifnet *ifp)
1647 {
1648 int error;
1649 struct ethercom *ec = (void *)ifp;
1650
1651 /* We still have VLAN's, defer for later */
1652 if (ec->ec_nvlans != 0)
1653 return 0;
1654
1655 /* Parent does not support VLAB's, nothing to do. */
1656 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1657 return -1;
1658
1659 /*
1660 * Disable Tx/Rx of VLAN-sized frames.
1661 */
1662 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1663
1664 /* Interface is down, defer for later */
1665 if ((ifp->if_flags & IFF_UP) == 0)
1666 return 0;
1667
1668 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1669 return 0;
1670
1671 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1672 return error;
1673 }
1674
1675 /*
1676 * Add and delete VLAN TAG
1677 */
1678 int
1679 ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
1680 {
1681 struct ethercom *ec = (void *)ifp;
1682 struct vlanid_list *vidp;
1683 bool vlanmtu_enabled;
1684 uint16_t vid = EVL_VLANOFTAG(vtag);
1685 int error;
1686
1687 vlanmtu_enabled = false;
1688
1689 /* Add a vid to the list */
1690 vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
1691 vidp->vid = vid;
1692
1693 ETHER_LOCK(ec);
1694 ec->ec_nvlans++;
1695 SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
1696 ETHER_UNLOCK(ec);
1697
1698 if (ec->ec_nvlans == 1) {
1699 IFNET_LOCK(ifp);
1700 error = ether_enable_vlan_mtu(ifp);
1701 IFNET_UNLOCK(ifp);
1702
1703 if (error == 0) {
1704 vlanmtu_enabled = true;
1705 } else if (error != -1) {
1706 goto fail;
1707 }
1708 }
1709
1710 if (ec->ec_vlan_cb != NULL) {
1711 error = (*ec->ec_vlan_cb)(ec, vid, true);
1712 if (error != 0)
1713 goto fail;
1714 }
1715
1716 if (vlanmtu_status != NULL)
1717 *vlanmtu_status = vlanmtu_enabled;
1718
1719 return 0;
1720 fail:
1721 ETHER_LOCK(ec);
1722 ec->ec_nvlans--;
1723 SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
1724 ETHER_UNLOCK(ec);
1725
1726 if (vlanmtu_enabled) {
1727 IFNET_LOCK(ifp);
1728 (void)ether_disable_vlan_mtu(ifp);
1729 IFNET_UNLOCK(ifp);
1730 }
1731
1732 kmem_free(vidp, sizeof(*vidp));
1733
1734 return error;
1735 }
1736
1737 int
1738 ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
1739 {
1740 struct ethercom *ec = (void *)ifp;
1741 struct vlanid_list *vidp;
1742 uint16_t vid = EVL_VLANOFTAG(vtag);
1743
1744 ETHER_LOCK(ec);
1745 SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
1746 if (vidp->vid == vid) {
1747 SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
1748 vlanid_list, vid_list);
1749 ec->ec_nvlans--;
1750 break;
1751 }
1752 }
1753 ETHER_UNLOCK(ec);
1754
1755 if (vidp == NULL)
1756 return ENOENT;
1757
1758 if (ec->ec_vlan_cb != NULL) {
1759 (void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
1760 }
1761
1762 if (ec->ec_nvlans == 0) {
1763 IFNET_LOCK(ifp);
1764 (void)ether_disable_vlan_mtu(ifp);
1765 IFNET_UNLOCK(ifp);
1766 }
1767
1768 kmem_free(vidp, sizeof(*vidp));
1769
1770 return 0;
1771 }
1772
1773 int
1774 ether_inject_vlantag(struct mbuf **mp, uint16_t etype, uint16_t tag)
1775 {
1776 static const size_t min_data_len =
1777 ETHER_MIN_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1778 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
1779 static const char vlan_zero_pad_buff[ETHER_MIN_LEN] = { 0 };
1780
1781 struct ether_vlan_header *evl;
1782 struct mbuf *m = *mp;
1783 int error;
1784
1785 error = 0;
1786
1787 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1788 if (m == NULL) {
1789 error = ENOBUFS;
1790 goto out;
1791 }
1792
1793 if (m->m_len < sizeof(*evl)) {
1794 m = m_pullup(m, sizeof(*evl));
1795 if (m == NULL) {
1796 error = ENOBUFS;
1797 goto out;
1798 }
1799 }
1800
1801 /*
1802 * Transform the Ethernet header into an
1803 * Ethernet header with 802.1Q encapsulation.
1804 */
1805 memmove(mtod(m, void *),
1806 mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
1807 sizeof(struct ether_header));
1808 evl = mtod(m, struct ether_vlan_header *);
1809 evl->evl_proto = evl->evl_encap_proto;
1810 evl->evl_encap_proto = htons(etype);
1811 evl->evl_tag = htons(tag);
1812
1813 /*
1814 * To cater for VLAN-aware layer 2 ethernet
1815 * switches which may need to strip the tag
1816 * before forwarding the packet, make sure
1817 * the packet+tag is at least 68 bytes long.
1818 * This is necessary because our parent will
1819 * only pad to 64 bytes (ETHER_MIN_LEN) and
1820 * some switches will not pad by themselves
1821 * after deleting a tag.
1822 */
1823 if (m->m_pkthdr.len < min_data_len) {
1824 m_copyback(m, m->m_pkthdr.len,
1825 min_data_len - m->m_pkthdr.len,
1826 vlan_zero_pad_buff);
1827 }
1828
1829 m->m_flags &= ~M_VLANTAG;
1830
1831 out:
1832 *mp = m;
1833 return error;
1834 }
1835
1836 struct mbuf *
1837 ether_strip_vlantag(struct mbuf *m)
1838 {
1839 struct ether_vlan_header *evl;
1840
1841 if (m->m_len < sizeof(*evl) &&
1842 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1843 return NULL;
1844 }
1845
1846 if (m_makewritable(&m, 0, sizeof(*evl), M_DONTWAIT)) {
1847 m_freem(m);
1848 return NULL;
1849 }
1850
1851 evl = mtod(m, struct ether_vlan_header *);
1852 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
1853
1854 vlan_set_tag(m, ntohs(evl->evl_tag));
1855
1856 /*
1857 * Restore the original ethertype. We'll remove
1858 * the encapsulation after we've found the vlan
1859 * interface corresponding to the tag.
1860 */
1861 evl->evl_encap_proto = evl->evl_proto;
1862
1863 /*
1864 * Remove the encapsulation header and append tag.
1865 * The original header has already been fixed up above.
1866 */
1867 vlan_set_tag(m, ntohs(evl->evl_tag));
1868 memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
1869 offsetof(struct ether_vlan_header, evl_encap_proto));
1870 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1871
1872 return m;
1873 }
1874
1875 static int
1876 ether_multicast_sysctl(SYSCTLFN_ARGS)
1877 {
1878 struct ether_multi *enm;
1879 struct ifnet *ifp;
1880 struct ethercom *ec;
1881 int error = 0;
1882 size_t written;
1883 struct psref psref;
1884 int bound;
1885 unsigned int multicnt;
1886 struct ether_multi_sysctl *addrs;
1887 int i;
1888
1889 if (namelen != 1)
1890 return EINVAL;
1891
1892 bound = curlwp_bind();
1893 ifp = if_get_byindex(name[0], &psref);
1894 if (ifp == NULL) {
1895 error = ENODEV;
1896 goto out;
1897 }
1898 if (ifp->if_type != IFT_ETHER) {
1899 if_put(ifp, &psref);
1900 *oldlenp = 0;
1901 goto out;
1902 }
1903 ec = (struct ethercom *)ifp;
1904
1905 if (oldp == NULL) {
1906 if_put(ifp, &psref);
1907 *oldlenp = ec->ec_multicnt * sizeof(*addrs);
1908 goto out;
1909 }
1910
1911 /*
1912 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
1913 * is sleepable, while holding it. Copy data to a local buffer first
1914 * with the lock taken and then call sysctl_copyout without holding it.
1915 */
1916 retry:
1917 multicnt = ec->ec_multicnt;
1918
1919 if (multicnt == 0) {
1920 if_put(ifp, &psref);
1921 *oldlenp = 0;
1922 goto out;
1923 }
1924
1925 addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
1926
1927 ETHER_LOCK(ec);
1928 if (multicnt != ec->ec_multicnt) {
1929 /* The number of multicast addresses has changed */
1930 ETHER_UNLOCK(ec);
1931 kmem_free(addrs, sizeof(*addrs) * multicnt);
1932 goto retry;
1933 }
1934
1935 i = 0;
1936 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1937 struct ether_multi_sysctl *addr = &addrs[i];
1938 addr->enm_refcount = enm->enm_refcount;
1939 memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1940 memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1941 i++;
1942 }
1943 ETHER_UNLOCK(ec);
1944
1945 error = 0;
1946 written = 0;
1947 for (i = 0; i < multicnt; i++) {
1948 struct ether_multi_sysctl *addr = &addrs[i];
1949
1950 if (written + sizeof(*addr) > *oldlenp)
1951 break;
1952 error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
1953 if (error)
1954 break;
1955 written += sizeof(*addr);
1956 oldp = (char *)oldp + sizeof(*addr);
1957 }
1958 kmem_free(addrs, sizeof(*addrs) * multicnt);
1959
1960 if_put(ifp, &psref);
1961
1962 *oldlenp = written;
1963 out:
1964 curlwp_bindx(bound);
1965 return error;
1966 }
1967
1968 static void
1969 ether_sysctl_setup(struct sysctllog **clog)
1970 {
1971 const struct sysctlnode *rnode = NULL;
1972
1973 sysctl_createv(clog, 0, NULL, &rnode,
1974 CTLFLAG_PERMANENT,
1975 CTLTYPE_NODE, "ether",
1976 SYSCTL_DESCR("Ethernet-specific information"),
1977 NULL, 0, NULL, 0,
1978 CTL_NET, CTL_CREATE, CTL_EOL);
1979
1980 sysctl_createv(clog, 0, &rnode, NULL,
1981 CTLFLAG_PERMANENT,
1982 CTLTYPE_NODE, "multicast",
1983 SYSCTL_DESCR("multicast addresses"),
1984 ether_multicast_sysctl, 0, NULL, 0,
1985 CTL_CREATE, CTL_EOL);
1986
1987 sysctl_createv(clog, 0, &rnode, NULL,
1988 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1989 CTLTYPE_STRING, "rps_hash",
1990 SYSCTL_DESCR("Interface rps hash function control"),
1991 sysctl_pktq_rps_hash_handler, 0, (void *)ðer_pktq_rps_hash_p,
1992 PKTQ_RPS_HASH_NAME_LEN,
1993 CTL_CREATE, CTL_EOL);
1994 }
1995
1996 void
1997 etherinit(void)
1998 {
1999
2000 #ifdef DIAGNOSTIC
2001 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
2002 #endif
2003 ether_pktq_rps_hash_p = pktq_rps_hash_default;
2004 ether_sysctl_setup(NULL);
2005 }
2006