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