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