if_ethersubr.c revision 1.299 1 /* $NetBSD: if_ethersubr.c,v 1.299 2021/09/30 04:13:42 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.299 2021/09/30 04:13:42 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
1071 #if NLAGG > 0
1072 if (ifp->if_lagg)
1073 lagg_ifdetach(ifp);
1074 #endif
1075
1076 ETHER_LOCK(ec);
1077 KASSERT(ec->ec_nvlans == 0);
1078 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1079 LIST_REMOVE(enm, enm_list);
1080 kmem_free(enm, sizeof(*enm));
1081 ec->ec_multicnt--;
1082 }
1083 ETHER_UNLOCK(ec);
1084
1085 mutex_obj_free(ec->ec_lock);
1086 ec->ec_lock = NULL;
1087
1088 ifp->if_mowner = NULL;
1089 MOWNER_DETACH(&ec->ec_rx_mowner);
1090 MOWNER_DETACH(&ec->ec_tx_mowner);
1091 }
1092
1093 void *
1094 ether_ifdetachhook_establish(struct ifnet *ifp,
1095 void (*fn)(void *), void *arg)
1096 {
1097 struct ethercom *ec;
1098 khook_t *hk;
1099
1100 if (ifp->if_type != IFT_ETHER)
1101 return NULL;
1102
1103 ec = (struct ethercom *)ifp;
1104 hk = simplehook_establish(ec->ec_ifdetach_hooks,
1105 fn, arg);
1106
1107 return (void *)hk;
1108 }
1109
1110 void
1111 ether_ifdetachhook_disestablish(struct ifnet *ifp,
1112 void *vhook, kmutex_t *lock)
1113 {
1114 struct ethercom *ec;
1115
1116 if (vhook == NULL)
1117 return;
1118
1119 ec = (struct ethercom *)ifp;
1120 simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
1121 }
1122
1123 #if 0
1124 /*
1125 * This is for reference. We have a table-driven version
1126 * of the little-endian crc32 generator, which is faster
1127 * than the double-loop.
1128 */
1129 uint32_t
1130 ether_crc32_le(const uint8_t *buf, size_t len)
1131 {
1132 uint32_t c, crc, carry;
1133 size_t i, j;
1134
1135 crc = 0xffffffffU; /* initial value */
1136
1137 for (i = 0; i < len; i++) {
1138 c = buf[i];
1139 for (j = 0; j < 8; j++) {
1140 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1141 crc >>= 1;
1142 c >>= 1;
1143 if (carry)
1144 crc = (crc ^ ETHER_CRC_POLY_LE);
1145 }
1146 }
1147
1148 return (crc);
1149 }
1150 #else
1151 uint32_t
1152 ether_crc32_le(const uint8_t *buf, size_t len)
1153 {
1154 static const uint32_t crctab[] = {
1155 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1156 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1157 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1158 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1159 };
1160 uint32_t crc;
1161 size_t i;
1162
1163 crc = 0xffffffffU; /* initial value */
1164
1165 for (i = 0; i < len; i++) {
1166 crc ^= buf[i];
1167 crc = (crc >> 4) ^ crctab[crc & 0xf];
1168 crc = (crc >> 4) ^ crctab[crc & 0xf];
1169 }
1170
1171 return (crc);
1172 }
1173 #endif
1174
1175 uint32_t
1176 ether_crc32_be(const uint8_t *buf, size_t len)
1177 {
1178 uint32_t c, crc, carry;
1179 size_t i, j;
1180
1181 crc = 0xffffffffU; /* initial value */
1182
1183 for (i = 0; i < len; i++) {
1184 c = buf[i];
1185 for (j = 0; j < 8; j++) {
1186 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1187 crc <<= 1;
1188 c >>= 1;
1189 if (carry)
1190 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1191 }
1192 }
1193
1194 return (crc);
1195 }
1196
1197 #ifdef INET
1198 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1199 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1200 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1201 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1202 #endif
1203 #ifdef INET6
1204 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1205 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1206 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1207 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1208 #endif
1209
1210 /*
1211 * ether_aton implementation, not using a static buffer.
1212 */
1213 int
1214 ether_aton_r(u_char *dest, size_t len, const char *str)
1215 {
1216 const u_char *cp = (const void *)str;
1217 u_char *ep;
1218
1219 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1220
1221 if (len < ETHER_ADDR_LEN)
1222 return ENOSPC;
1223
1224 ep = dest + ETHER_ADDR_LEN;
1225
1226 while (*cp) {
1227 if (!isxdigit(*cp))
1228 return EINVAL;
1229
1230 *dest = atox(*cp);
1231 cp++;
1232 if (isxdigit(*cp)) {
1233 *dest = (*dest << 4) | atox(*cp);
1234 cp++;
1235 }
1236 dest++;
1237
1238 if (dest == ep)
1239 return (*cp == '\0') ? 0 : ENAMETOOLONG;
1240
1241 switch (*cp) {
1242 case ':':
1243 case '-':
1244 case '.':
1245 cp++;
1246 break;
1247 }
1248 }
1249 return ENOBUFS;
1250 }
1251
1252 /*
1253 * Convert a sockaddr into an Ethernet address or range of Ethernet
1254 * addresses.
1255 */
1256 int
1257 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1258 uint8_t addrhi[ETHER_ADDR_LEN])
1259 {
1260 #ifdef INET
1261 const struct sockaddr_in *sin;
1262 #endif
1263 #ifdef INET6
1264 const struct sockaddr_in6 *sin6;
1265 #endif
1266
1267 switch (sa->sa_family) {
1268
1269 case AF_UNSPEC:
1270 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1271 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1272 break;
1273
1274 #ifdef INET
1275 case AF_INET:
1276 sin = satocsin(sa);
1277 if (sin->sin_addr.s_addr == INADDR_ANY) {
1278 /*
1279 * An IP address of INADDR_ANY means listen to
1280 * or stop listening to all of the Ethernet
1281 * multicast addresses used for IP.
1282 * (This is for the sake of IP multicast routers.)
1283 */
1284 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1285 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1286 } else {
1287 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1288 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1289 }
1290 break;
1291 #endif
1292 #ifdef INET6
1293 case AF_INET6:
1294 sin6 = satocsin6(sa);
1295 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1296 /*
1297 * An IP6 address of 0 means listen to or stop
1298 * listening to all of the Ethernet multicast
1299 * address used for IP6.
1300 * (This is used for multicast routers.)
1301 */
1302 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1303 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1304 } else {
1305 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1306 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1307 }
1308 break;
1309 #endif
1310
1311 default:
1312 return EAFNOSUPPORT;
1313 }
1314 return 0;
1315 }
1316
1317 /*
1318 * Add an Ethernet multicast address or range of addresses to the list for a
1319 * given interface.
1320 */
1321 int
1322 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1323 {
1324 struct ether_multi *enm, *_enm;
1325 u_char addrlo[ETHER_ADDR_LEN];
1326 u_char addrhi[ETHER_ADDR_LEN];
1327 int error = 0;
1328
1329 /* Allocate out of lock */
1330 enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
1331
1332 ETHER_LOCK(ec);
1333 error = ether_multiaddr(sa, addrlo, addrhi);
1334 if (error != 0)
1335 goto out;
1336
1337 /*
1338 * Verify that we have valid Ethernet multicast addresses.
1339 */
1340 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1341 error = EINVAL;
1342 goto out;
1343 }
1344
1345 /*
1346 * See if the address range is already in the list.
1347 */
1348 _enm = ether_lookup_multi(addrlo, addrhi, ec);
1349 if (_enm != NULL) {
1350 /*
1351 * Found it; just increment the reference count.
1352 */
1353 ++_enm->enm_refcount;
1354 error = 0;
1355 goto out;
1356 }
1357
1358 /*
1359 * Link a new multicast record into the interface's multicast list.
1360 */
1361 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
1362 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
1363 enm->enm_refcount = 1;
1364 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1365 ec->ec_multicnt++;
1366
1367 /*
1368 * Return ENETRESET to inform the driver that the list has changed
1369 * and its reception filter should be adjusted accordingly.
1370 */
1371 error = ENETRESET;
1372 enm = NULL;
1373
1374 out:
1375 ETHER_UNLOCK(ec);
1376 if (enm != NULL)
1377 kmem_free(enm, sizeof(*enm));
1378 return error;
1379 }
1380
1381 /*
1382 * Delete a multicast address record.
1383 */
1384 int
1385 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1386 {
1387 struct ether_multi *enm;
1388 u_char addrlo[ETHER_ADDR_LEN];
1389 u_char addrhi[ETHER_ADDR_LEN];
1390 int error;
1391
1392 ETHER_LOCK(ec);
1393 error = ether_multiaddr(sa, addrlo, addrhi);
1394 if (error != 0)
1395 goto error;
1396
1397 /*
1398 * Look up the address in our list.
1399 */
1400 enm = ether_lookup_multi(addrlo, addrhi, ec);
1401 if (enm == NULL) {
1402 error = ENXIO;
1403 goto error;
1404 }
1405 if (--enm->enm_refcount != 0) {
1406 /*
1407 * Still some claims to this record.
1408 */
1409 error = 0;
1410 goto error;
1411 }
1412
1413 /*
1414 * No remaining claims to this record; unlink and free it.
1415 */
1416 LIST_REMOVE(enm, enm_list);
1417 ec->ec_multicnt--;
1418 ETHER_UNLOCK(ec);
1419 kmem_free(enm, sizeof(*enm));
1420
1421 /*
1422 * Return ENETRESET to inform the driver that the list has changed
1423 * and its reception filter should be adjusted accordingly.
1424 */
1425 return ENETRESET;
1426
1427 error:
1428 ETHER_UNLOCK(ec);
1429 return error;
1430 }
1431
1432 void
1433 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1434 {
1435 ec->ec_ifflags_cb = cb;
1436 }
1437
1438 void
1439 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
1440 {
1441
1442 ec->ec_vlan_cb = cb;
1443 }
1444
1445 static int
1446 ether_ioctl_reinit(struct ethercom *ec)
1447 {
1448 struct ifnet *ifp = &ec->ec_if;
1449 int error;
1450
1451 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1452 case IFF_RUNNING:
1453 /*
1454 * If interface is marked down and it is running,
1455 * then stop and disable it.
1456 */
1457 (*ifp->if_stop)(ifp, 1);
1458 break;
1459 case IFF_UP:
1460 /*
1461 * If interface is marked up and it is stopped, then
1462 * start it.
1463 */
1464 return (*ifp->if_init)(ifp);
1465 case IFF_UP | IFF_RUNNING:
1466 error = 0;
1467 if (ec->ec_ifflags_cb != NULL) {
1468 error = (*ec->ec_ifflags_cb)(ec);
1469 if (error == ENETRESET) {
1470 /*
1471 * Reset the interface to pick up
1472 * changes in any other flags that
1473 * affect the hardware state.
1474 */
1475 return (*ifp->if_init)(ifp);
1476 }
1477 } else
1478 error = (*ifp->if_init)(ifp);
1479 return error;
1480 case 0:
1481 break;
1482 }
1483
1484 return 0;
1485 }
1486
1487 /*
1488 * Common ioctls for Ethernet interfaces. Note, we must be
1489 * called at splnet().
1490 */
1491 int
1492 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1493 {
1494 struct ethercom *ec = (void *)ifp;
1495 struct eccapreq *eccr;
1496 struct ifreq *ifr = (struct ifreq *)data;
1497 struct if_laddrreq *iflr = data;
1498 const struct sockaddr_dl *sdl;
1499 static const uint8_t zero[ETHER_ADDR_LEN];
1500 int error;
1501
1502 switch (cmd) {
1503 case SIOCINITIFADDR:
1504 {
1505 struct ifaddr *ifa = (struct ifaddr *)data;
1506 if (ifa->ifa_addr->sa_family != AF_LINK
1507 && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
1508 (IFF_UP | IFF_RUNNING)) {
1509 ifp->if_flags |= IFF_UP;
1510 if ((error = (*ifp->if_init)(ifp)) != 0)
1511 return error;
1512 }
1513 #ifdef INET
1514 if (ifa->ifa_addr->sa_family == AF_INET)
1515 arp_ifinit(ifp, ifa);
1516 #endif
1517 return 0;
1518 }
1519
1520 case SIOCSIFMTU:
1521 {
1522 int maxmtu;
1523
1524 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1525 maxmtu = ETHERMTU_JUMBO;
1526 else
1527 maxmtu = ETHERMTU;
1528
1529 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1530 return EINVAL;
1531 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1532 return error;
1533 else if (ifp->if_flags & IFF_UP) {
1534 /* Make sure the device notices the MTU change. */
1535 return (*ifp->if_init)(ifp);
1536 } else
1537 return 0;
1538 }
1539
1540 case SIOCSIFFLAGS:
1541 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1542 return error;
1543 return ether_ioctl_reinit(ec);
1544 case SIOCGIFFLAGS:
1545 error = ifioctl_common(ifp, cmd, data);
1546 if (error == 0) {
1547 /* Set IFF_ALLMULTI for backcompat */
1548 ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
1549 IFF_ALLMULTI : 0;
1550 }
1551 return error;
1552 case SIOCGETHERCAP:
1553 eccr = (struct eccapreq *)data;
1554 eccr->eccr_capabilities = ec->ec_capabilities;
1555 eccr->eccr_capenable = ec->ec_capenable;
1556 return 0;
1557 case SIOCSETHERCAP:
1558 eccr = (struct eccapreq *)data;
1559 if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
1560 return EINVAL;
1561 if (eccr->eccr_capenable == ec->ec_capenable)
1562 return 0;
1563 #if 0 /* notyet */
1564 ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
1565 | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
1566 #else
1567 ec->ec_capenable = eccr->eccr_capenable;
1568 #endif
1569 return ether_ioctl_reinit(ec);
1570 case SIOCADDMULTI:
1571 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1572 case SIOCDELMULTI:
1573 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1574 case SIOCSIFMEDIA:
1575 case SIOCGIFMEDIA:
1576 if (ec->ec_mii != NULL)
1577 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
1578 cmd);
1579 else if (ec->ec_ifmedia != NULL)
1580 return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
1581 else
1582 return ENOTTY;
1583 break;
1584 case SIOCALIFADDR:
1585 sdl = satocsdl(sstocsa(&iflr->addr));
1586 if (sdl->sdl_family != AF_LINK)
1587 ;
1588 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1589 return EINVAL;
1590 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1591 return EINVAL;
1592 /*FALLTHROUGH*/
1593 default:
1594 return ifioctl_common(ifp, cmd, data);
1595 }
1596 return 0;
1597 }
1598
1599 /*
1600 * Enable/disable passing VLAN packets if the parent interface supports it.
1601 * Return:
1602 * 0: Ok
1603 * -1: Parent interface does not support vlans
1604 * >0: Error
1605 */
1606 int
1607 ether_enable_vlan_mtu(struct ifnet *ifp)
1608 {
1609 int error;
1610 struct ethercom *ec = (void *)ifp;
1611
1612 /* Parent does not support VLAN's */
1613 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1614 return -1;
1615
1616 /*
1617 * Parent supports the VLAN_MTU capability,
1618 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1619 * enable it.
1620 */
1621 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1622
1623 /* Interface is down, defer for later */
1624 if ((ifp->if_flags & IFF_UP) == 0)
1625 return 0;
1626
1627 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1628 return 0;
1629
1630 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1631 return error;
1632 }
1633
1634 int
1635 ether_disable_vlan_mtu(struct ifnet *ifp)
1636 {
1637 int error;
1638 struct ethercom *ec = (void *)ifp;
1639
1640 /* We still have VLAN's, defer for later */
1641 if (ec->ec_nvlans != 0)
1642 return 0;
1643
1644 /* Parent does not support VLAB's, nothing to do. */
1645 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1646 return -1;
1647
1648 /*
1649 * Disable Tx/Rx of VLAN-sized frames.
1650 */
1651 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1652
1653 /* Interface is down, defer for later */
1654 if ((ifp->if_flags & IFF_UP) == 0)
1655 return 0;
1656
1657 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1658 return 0;
1659
1660 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1661 return error;
1662 }
1663
1664 static int
1665 ether_multicast_sysctl(SYSCTLFN_ARGS)
1666 {
1667 struct ether_multi *enm;
1668 struct ifnet *ifp;
1669 struct ethercom *ec;
1670 int error = 0;
1671 size_t written;
1672 struct psref psref;
1673 int bound;
1674 unsigned int multicnt;
1675 struct ether_multi_sysctl *addrs;
1676 int i;
1677
1678 if (namelen != 1)
1679 return EINVAL;
1680
1681 bound = curlwp_bind();
1682 ifp = if_get_byindex(name[0], &psref);
1683 if (ifp == NULL) {
1684 error = ENODEV;
1685 goto out;
1686 }
1687 if (ifp->if_type != IFT_ETHER) {
1688 if_put(ifp, &psref);
1689 *oldlenp = 0;
1690 goto out;
1691 }
1692 ec = (struct ethercom *)ifp;
1693
1694 if (oldp == NULL) {
1695 if_put(ifp, &psref);
1696 *oldlenp = ec->ec_multicnt * sizeof(*addrs);
1697 goto out;
1698 }
1699
1700 /*
1701 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
1702 * is sleepable, while holding it. Copy data to a local buffer first
1703 * with the lock taken and then call sysctl_copyout without holding it.
1704 */
1705 retry:
1706 multicnt = ec->ec_multicnt;
1707
1708 if (multicnt == 0) {
1709 if_put(ifp, &psref);
1710 *oldlenp = 0;
1711 goto out;
1712 }
1713
1714 addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
1715
1716 ETHER_LOCK(ec);
1717 if (multicnt != ec->ec_multicnt) {
1718 /* The number of multicast addresses has changed */
1719 ETHER_UNLOCK(ec);
1720 kmem_free(addrs, sizeof(*addrs) * multicnt);
1721 goto retry;
1722 }
1723
1724 i = 0;
1725 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1726 struct ether_multi_sysctl *addr = &addrs[i];
1727 addr->enm_refcount = enm->enm_refcount;
1728 memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1729 memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1730 i++;
1731 }
1732 ETHER_UNLOCK(ec);
1733
1734 error = 0;
1735 written = 0;
1736 for (i = 0; i < multicnt; i++) {
1737 struct ether_multi_sysctl *addr = &addrs[i];
1738
1739 if (written + sizeof(*addr) > *oldlenp)
1740 break;
1741 error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
1742 if (error)
1743 break;
1744 written += sizeof(*addr);
1745 oldp = (char *)oldp + sizeof(*addr);
1746 }
1747 kmem_free(addrs, sizeof(*addrs) * multicnt);
1748
1749 if_put(ifp, &psref);
1750
1751 *oldlenp = written;
1752 out:
1753 curlwp_bindx(bound);
1754 return error;
1755 }
1756
1757 static void
1758 ether_sysctl_setup(struct sysctllog **clog)
1759 {
1760 const struct sysctlnode *rnode = NULL;
1761
1762 sysctl_createv(clog, 0, NULL, &rnode,
1763 CTLFLAG_PERMANENT,
1764 CTLTYPE_NODE, "ether",
1765 SYSCTL_DESCR("Ethernet-specific information"),
1766 NULL, 0, NULL, 0,
1767 CTL_NET, CTL_CREATE, CTL_EOL);
1768
1769 sysctl_createv(clog, 0, &rnode, NULL,
1770 CTLFLAG_PERMANENT,
1771 CTLTYPE_NODE, "multicast",
1772 SYSCTL_DESCR("multicast addresses"),
1773 ether_multicast_sysctl, 0, NULL, 0,
1774 CTL_CREATE, CTL_EOL);
1775 }
1776
1777 void
1778 etherinit(void)
1779 {
1780
1781 #ifdef DIAGNOSTIC
1782 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1783 #endif
1784 ether_sysctl_setup(NULL);
1785 }
1786