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