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