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