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