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