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