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