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