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