if_ethersubr.c revision 1.199 1 /* $NetBSD: if_ethersubr.c,v 1.199 2014/06/05 23:48:16 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.199 2014/06/05 23:48:16 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 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 pktqueue_t *pktq = NULL;
579 struct ifqueue *inq = NULL;
580 uint16_t etype;
581 struct ether_header *eh;
582 size_t ehlen;
583 int isr = 0;
584 #if defined (LLC) || defined(NETATALK)
585 struct llc *l;
586 #endif
587
588 if ((ifp->if_flags & IFF_UP) == 0) {
589 m_freem(m);
590 return;
591 }
592
593 #ifdef MBUFTRACE
594 m_claimm(m, &ec->ec_rx_mowner);
595 #endif
596 eh = mtod(m, struct ether_header *);
597 etype = ntohs(eh->ether_type);
598 ehlen = sizeof(*eh);
599
600 /*
601 * Determine if the packet is within its size limits.
602 */
603 if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
604 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
605 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
606 bigpktppslim)) {
607 printf("%s: discarding oversize frame (len=%d)\n",
608 ifp->if_xname, m->m_pkthdr.len);
609 }
610 m_freem(m);
611 return;
612 }
613
614 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
615 /*
616 * If this is not a simplex interface, drop the packet
617 * if it came from us.
618 */
619 if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
620 memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
621 ETHER_ADDR_LEN) == 0) {
622 m_freem(m);
623 return;
624 }
625
626 if (memcmp(etherbroadcastaddr,
627 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
628 m->m_flags |= M_BCAST;
629 else
630 m->m_flags |= M_MCAST;
631 ifp->if_imcasts++;
632 }
633
634 /* If the CRC is still on the packet, trim it off. */
635 if (m->m_flags & M_HASFCS) {
636 m_adj(m, -ETHER_CRC_LEN);
637 m->m_flags &= ~M_HASFCS;
638 }
639
640 ifp->if_ibytes += m->m_pkthdr.len;
641
642 #if NBRIDGE > 0
643 /*
644 * Tap the packet off here for a bridge. bridge_input()
645 * will return NULL if it has consumed the packet, otherwise
646 * it gets processed as normal. Note that bridge_input()
647 * will always return the original packet if we need to
648 * process it locally.
649 */
650 if (ifp->if_bridge) {
651 /* clear M_PROMISC, in case the packets comes from a vlan */
652 m->m_flags &= ~M_PROMISC;
653 m = bridge_input(ifp, m);
654 if (m == NULL)
655 return;
656
657 /*
658 * Bridge has determined that the packet is for us.
659 * Update our interface pointer -- we may have had
660 * to "bridge" the packet locally.
661 */
662 ifp = m->m_pkthdr.rcvif;
663 } else
664 #endif /* NBRIDGE > 0 */
665 {
666
667 #if NCARP > 0
668 if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
669 /*
670 * clear M_PROMISC, in case the packets comes from a
671 * vlan
672 */
673 m->m_flags &= ~M_PROMISC;
674 if (carp_input(m, (uint8_t *)&eh->ether_shost,
675 (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
676 return;
677 }
678 #endif /* NCARP > 0 */
679 if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
680 (ifp->if_flags & IFF_PROMISC) != 0 &&
681 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
682 ETHER_ADDR_LEN) != 0) {
683 m->m_flags |= M_PROMISC;
684 }
685 }
686
687 if ((m->m_flags & M_PROMISC) == 0) {
688 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
689 return;
690 if (m == NULL)
691 return;
692
693 eh = mtod(m, struct ether_header *);
694 etype = ntohs(eh->ether_type);
695 ehlen = sizeof(*eh);
696 }
697
698 #if NAGR > 0
699 if (ifp->if_agrprivate &&
700 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
701 m->m_flags &= ~M_PROMISC;
702 agr_input(ifp, m);
703 return;
704 }
705 #endif /* NAGR > 0 */
706
707 /*
708 * If VLANs are configured on the interface, check to
709 * see if the device performed the decapsulation and
710 * provided us with the tag.
711 */
712 if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
713 #if NVLAN > 0
714 /*
715 * vlan_input() will either recursively call ether_input()
716 * or drop the packet.
717 */
718 vlan_input(ifp, m);
719 #else
720 m_freem(m);
721 #endif
722 return;
723 }
724
725 /*
726 * Handle protocols that expect to have the Ethernet header
727 * (and possibly FCS) intact.
728 */
729 switch (etype) {
730 case ETHERTYPE_VLAN: {
731 struct ether_vlan_header *evl = (void *)eh;
732 /*
733 * If there is a tag of 0, then the VLAN header was probably
734 * just being used to store the priority. Extract the ether
735 * type, and if IP or IPV6, let them deal with it.
736 */
737 if (m->m_len <= sizeof(*evl)
738 && EVL_VLANOFTAG(evl->evl_tag) == 0) {
739 etype = ntohs(evl->evl_proto);
740 ehlen = sizeof(*evl);
741 if ((m->m_flags & M_PROMISC) == 0
742 && (etype == ETHERTYPE_IP
743 || etype == ETHERTYPE_IPV6))
744 break;
745 }
746 #if NVLAN > 0
747 /*
748 * vlan_input() will either recursively call ether_input()
749 * or drop the packet.
750 */
751 if (((struct ethercom *)ifp)->ec_nvlans != 0)
752 vlan_input(ifp, m);
753 else
754 #endif /* NVLAN > 0 */
755 m_freem(m);
756 return;
757 }
758 #if NPPPOE > 0
759 case ETHERTYPE_PPPOEDISC:
760 case ETHERTYPE_PPPOE:
761 if (m->m_flags & M_PROMISC) {
762 m_freem(m);
763 return;
764 }
765 #ifndef PPPOE_SERVER
766 if (m->m_flags & (M_MCAST | M_BCAST)) {
767 m_freem(m);
768 return;
769 }
770 #endif
771
772 if (etype == ETHERTYPE_PPPOEDISC)
773 inq = &ppoediscinq;
774 else
775 inq = &ppoeinq;
776 if (IF_QFULL(inq)) {
777 IF_DROP(inq);
778 m_freem(m);
779 } else
780 IF_ENQUEUE(inq, m);
781 softint_schedule(pppoe_softintr);
782 return;
783 #endif /* NPPPOE > 0 */
784 case ETHERTYPE_SLOWPROTOCOLS: {
785 uint8_t subtype;
786
787 #if defined(DIAGNOSTIC)
788 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
789 panic("ether_input: too short slow protocol packet");
790 }
791 #endif
792 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
793 switch (subtype) {
794 #if NAGR > 0
795 case SLOWPROTOCOLS_SUBTYPE_LACP:
796 if (ifp->if_agrprivate) {
797 ieee8023ad_lacp_input(ifp, m);
798 return;
799 }
800 break;
801
802 case SLOWPROTOCOLS_SUBTYPE_MARKER:
803 if (ifp->if_agrprivate) {
804 ieee8023ad_marker_input(ifp, m);
805 return;
806 }
807 break;
808 #endif /* NAGR > 0 */
809 default:
810 if (subtype == 0 || subtype > 10) {
811 /* illegal value */
812 m_freem(m);
813 return;
814 }
815 /* unknown subtype */
816 break;
817 }
818 /* FALLTHROUGH */
819 }
820 default:
821 if (m->m_flags & M_PROMISC) {
822 m_freem(m);
823 return;
824 }
825 }
826
827 /* If the CRC is still on the packet, trim it off. */
828 if (m->m_flags & M_HASFCS) {
829 m_adj(m, -ETHER_CRC_LEN);
830 m->m_flags &= ~M_HASFCS;
831 }
832
833 if (etype > ETHERMTU + sizeof (struct ether_header)) {
834 /* Strip off the Ethernet header. */
835 m_adj(m, ehlen);
836
837 switch (etype) {
838 #ifdef INET
839 case ETHERTYPE_IP:
840 #ifdef GATEWAY
841 if (ipflow_fastforward(m))
842 return;
843 #endif
844 pktq = ip_pktq;
845 break;
846
847 case ETHERTYPE_ARP:
848 isr = NETISR_ARP;
849 inq = &arpintrq;
850 break;
851
852 case ETHERTYPE_REVARP:
853 revarpinput(m); /* XXX queue? */
854 return;
855 #endif
856 #ifdef INET6
857 case ETHERTYPE_IPV6:
858 if (__predict_false(!in6_present)) {
859 m_freem(m);
860 return;
861 }
862 #ifdef GATEWAY
863 if (ip6flow_fastforward(&m))
864 return;
865 #endif
866 pktq = ip6_pktq;
867 break;
868 #endif
869 #ifdef IPX
870 case ETHERTYPE_IPX:
871 isr = NETISR_IPX;
872 inq = &ipxintrq;
873 break;
874 #endif
875 #ifdef NETATALK
876 case ETHERTYPE_ATALK:
877 isr = 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 isr = 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 isr = 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 (__predict_true(pktq)) {
946 const uint32_t h = pktq_rps_hash(m);
947 if (__predict_false(!pktq_enqueue(pktq, m, h))) {
948 m_freem(m);
949 }
950 return;
951 }
952
953 if (__predict_false(!inq)) {
954 /* Should not happen. */
955 m_freem(m);
956 return;
957 }
958 if (IF_QFULL(inq)) {
959 IF_DROP(inq);
960 m_freem(m);
961 } else {
962 IF_ENQUEUE(inq, m);
963 schednetisr(isr);
964 }
965 }
966
967 /*
968 * Convert Ethernet address to printable (loggable) representation.
969 */
970 char *
971 ether_sprintf(const u_char *ap)
972 {
973 static char etherbuf[3 * ETHER_ADDR_LEN];
974 return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
975 }
976
977 char *
978 ether_snprintf(char *buf, size_t len, const u_char *ap)
979 {
980 char *cp = buf;
981 size_t i;
982
983 for (i = 0; i < len / 3; i++) {
984 *cp++ = hexdigits[*ap >> 4];
985 *cp++ = hexdigits[*ap++ & 0xf];
986 *cp++ = ':';
987 }
988 *--cp = '\0';
989 return buf;
990 }
991
992 /*
993 * Perform common duties while attaching to interface list
994 */
995 void
996 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
997 {
998 struct ethercom *ec = (struct ethercom *)ifp;
999
1000 ifp->if_type = IFT_ETHER;
1001 ifp->if_hdrlen = ETHER_HDR_LEN;
1002 ifp->if_dlt = DLT_EN10MB;
1003 ifp->if_mtu = ETHERMTU;
1004 ifp->if_output = ether_output;
1005 ifp->if_input = ether_input;
1006 if (ifp->if_baudrate == 0)
1007 ifp->if_baudrate = IF_Mbps(10); /* just a default */
1008
1009 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1010
1011 LIST_INIT(&ec->ec_multiaddrs);
1012 ifp->if_broadcastaddr = etherbroadcastaddr;
1013 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1014 #ifdef MBUFTRACE
1015 strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
1016 sizeof(ec->ec_tx_mowner.mo_name));
1017 strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
1018 sizeof(ec->ec_tx_mowner.mo_descr));
1019 strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
1020 sizeof(ec->ec_rx_mowner.mo_name));
1021 strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
1022 sizeof(ec->ec_rx_mowner.mo_descr));
1023 MOWNER_ATTACH(&ec->ec_tx_mowner);
1024 MOWNER_ATTACH(&ec->ec_rx_mowner);
1025 ifp->if_mowner = &ec->ec_tx_mowner;
1026 #endif
1027 }
1028
1029 void
1030 ether_ifdetach(struct ifnet *ifp)
1031 {
1032 struct ethercom *ec = (void *) ifp;
1033 struct ether_multi *enm;
1034 int s;
1035
1036 /*
1037 * Prevent further calls to ioctl (for example turning off
1038 * promiscuous mode from the bridge code), which eventually can
1039 * call if_init() which can cause panics because the interface
1040 * is in the process of being detached. Return device not configured
1041 * instead.
1042 */
1043 ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
1044
1045 #if NBRIDGE > 0
1046 if (ifp->if_bridge)
1047 bridge_ifdetach(ifp);
1048 #endif
1049
1050 bpf_detach(ifp);
1051
1052 #if NVLAN > 0
1053 if (ec->ec_nvlans)
1054 vlan_ifdetach(ifp);
1055 #endif
1056
1057 s = splnet();
1058 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1059 LIST_REMOVE(enm, enm_list);
1060 free(enm, M_IFMADDR);
1061 ec->ec_multicnt--;
1062 }
1063 splx(s);
1064
1065 #if 0 /* done in if_detach() */
1066 if_free_sadl(ifp);
1067 #endif
1068
1069 ifp->if_mowner = NULL;
1070 MOWNER_DETACH(&ec->ec_rx_mowner);
1071 MOWNER_DETACH(&ec->ec_tx_mowner);
1072 }
1073
1074 #if 0
1075 /*
1076 * This is for reference. We have a table-driven version
1077 * of the little-endian crc32 generator, which is faster
1078 * than the double-loop.
1079 */
1080 uint32_t
1081 ether_crc32_le(const uint8_t *buf, size_t len)
1082 {
1083 uint32_t c, crc, carry;
1084 size_t i, j;
1085
1086 crc = 0xffffffffU; /* initial value */
1087
1088 for (i = 0; i < len; i++) {
1089 c = buf[i];
1090 for (j = 0; j < 8; j++) {
1091 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1092 crc >>= 1;
1093 c >>= 1;
1094 if (carry)
1095 crc = (crc ^ ETHER_CRC_POLY_LE);
1096 }
1097 }
1098
1099 return (crc);
1100 }
1101 #else
1102 uint32_t
1103 ether_crc32_le(const uint8_t *buf, size_t len)
1104 {
1105 static const uint32_t crctab[] = {
1106 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1107 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1108 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1109 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1110 };
1111 uint32_t crc;
1112 size_t i;
1113
1114 crc = 0xffffffffU; /* initial value */
1115
1116 for (i = 0; i < len; i++) {
1117 crc ^= buf[i];
1118 crc = (crc >> 4) ^ crctab[crc & 0xf];
1119 crc = (crc >> 4) ^ crctab[crc & 0xf];
1120 }
1121
1122 return (crc);
1123 }
1124 #endif
1125
1126 uint32_t
1127 ether_crc32_be(const uint8_t *buf, size_t len)
1128 {
1129 uint32_t c, crc, carry;
1130 size_t i, j;
1131
1132 crc = 0xffffffffU; /* initial value */
1133
1134 for (i = 0; i < len; i++) {
1135 c = buf[i];
1136 for (j = 0; j < 8; j++) {
1137 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1138 crc <<= 1;
1139 c >>= 1;
1140 if (carry)
1141 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1142 }
1143 }
1144
1145 return (crc);
1146 }
1147
1148 #ifdef INET
1149 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1150 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1151 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1152 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1153 #endif
1154 #ifdef INET6
1155 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1156 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1157 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1158 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1159 #endif
1160
1161 /*
1162 * ether_aton implementation, not using a static buffer.
1163 */
1164 int
1165 ether_aton_r(u_char *dest, size_t len, const char *str)
1166 {
1167 const u_char *cp = (const void *)str;
1168 u_char *ep;
1169
1170 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1171
1172 if (len < ETHER_ADDR_LEN)
1173 return ENOSPC;
1174
1175 ep = dest + ETHER_ADDR_LEN;
1176
1177 while (*cp) {
1178 if (!isxdigit(*cp))
1179 return EINVAL;
1180 *dest = atox(*cp);
1181 cp++;
1182 if (isxdigit(*cp)) {
1183 *dest = (*dest << 4) | atox(*cp);
1184 dest++;
1185 cp++;
1186 } else
1187 dest++;
1188 if (dest == ep)
1189 return *cp == '\0' ? 0 : ENAMETOOLONG;
1190 switch (*cp) {
1191 case ':':
1192 case '-':
1193 case '.':
1194 cp++;
1195 break;
1196 }
1197 }
1198 return ENOBUFS;
1199 }
1200
1201 /*
1202 * Convert a sockaddr into an Ethernet address or range of Ethernet
1203 * addresses.
1204 */
1205 int
1206 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1207 uint8_t addrhi[ETHER_ADDR_LEN])
1208 {
1209 #ifdef INET
1210 const struct sockaddr_in *sin;
1211 #endif /* INET */
1212 #ifdef INET6
1213 const struct sockaddr_in6 *sin6;
1214 #endif /* INET6 */
1215
1216 switch (sa->sa_family) {
1217
1218 case AF_UNSPEC:
1219 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1220 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1221 break;
1222
1223 #ifdef INET
1224 case AF_INET:
1225 sin = satocsin(sa);
1226 if (sin->sin_addr.s_addr == INADDR_ANY) {
1227 /*
1228 * An IP address of INADDR_ANY means listen to
1229 * or stop listening to all of the Ethernet
1230 * multicast addresses used for IP.
1231 * (This is for the sake of IP multicast routers.)
1232 */
1233 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1234 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1235 }
1236 else {
1237 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1238 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1239 }
1240 break;
1241 #endif
1242 #ifdef INET6
1243 case AF_INET6:
1244 sin6 = satocsin6(sa);
1245 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1246 /*
1247 * An IP6 address of 0 means listen to or stop
1248 * listening to all of the Ethernet multicast
1249 * address used for IP6.
1250 * (This is used for multicast routers.)
1251 */
1252 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1253 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1254 } else {
1255 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1256 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1257 }
1258 break;
1259 #endif
1260
1261 default:
1262 return EAFNOSUPPORT;
1263 }
1264 return 0;
1265 }
1266
1267 /*
1268 * Add an Ethernet multicast address or range of addresses to the list for a
1269 * given interface.
1270 */
1271 int
1272 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1273 {
1274 struct ether_multi *enm;
1275 u_char addrlo[ETHER_ADDR_LEN];
1276 u_char addrhi[ETHER_ADDR_LEN];
1277 int s = splnet(), error;
1278
1279 error = ether_multiaddr(sa, addrlo, addrhi);
1280 if (error != 0) {
1281 splx(s);
1282 return error;
1283 }
1284
1285 /*
1286 * Verify that we have valid Ethernet multicast addresses.
1287 */
1288 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1289 splx(s);
1290 return EINVAL;
1291 }
1292 /*
1293 * See if the address range is already in the list.
1294 */
1295 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1296 if (enm != NULL) {
1297 /*
1298 * Found it; just increment the reference count.
1299 */
1300 ++enm->enm_refcount;
1301 splx(s);
1302 return 0;
1303 }
1304 /*
1305 * New address or range; malloc a new multicast record
1306 * and link it into the interface's multicast list.
1307 */
1308 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1309 if (enm == NULL) {
1310 splx(s);
1311 return ENOBUFS;
1312 }
1313 memcpy(enm->enm_addrlo, addrlo, 6);
1314 memcpy(enm->enm_addrhi, addrhi, 6);
1315 enm->enm_refcount = 1;
1316 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1317 ec->ec_multicnt++;
1318 splx(s);
1319 /*
1320 * Return ENETRESET to inform the driver that the list has changed
1321 * and its reception filter should be adjusted accordingly.
1322 */
1323 return ENETRESET;
1324 }
1325
1326 /*
1327 * Delete a multicast address record.
1328 */
1329 int
1330 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1331 {
1332 struct ether_multi *enm;
1333 u_char addrlo[ETHER_ADDR_LEN];
1334 u_char addrhi[ETHER_ADDR_LEN];
1335 int s = splnet(), error;
1336
1337 error = ether_multiaddr(sa, addrlo, addrhi);
1338 if (error != 0) {
1339 splx(s);
1340 return (error);
1341 }
1342
1343 /*
1344 * Look ur the address in our list.
1345 */
1346 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1347 if (enm == NULL) {
1348 splx(s);
1349 return (ENXIO);
1350 }
1351 if (--enm->enm_refcount != 0) {
1352 /*
1353 * Still some claims to this record.
1354 */
1355 splx(s);
1356 return (0);
1357 }
1358 /*
1359 * No remaining claims to this record; unlink and free it.
1360 */
1361 LIST_REMOVE(enm, enm_list);
1362 free(enm, M_IFMADDR);
1363 ec->ec_multicnt--;
1364 splx(s);
1365 /*
1366 * Return ENETRESET to inform the driver that the list has changed
1367 * and its reception filter should be adjusted accordingly.
1368 */
1369 return (ENETRESET);
1370 }
1371
1372 void
1373 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1374 {
1375 ec->ec_ifflags_cb = cb;
1376 }
1377
1378 /*
1379 * Common ioctls for Ethernet interfaces. Note, we must be
1380 * called at splnet().
1381 */
1382 int
1383 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1384 {
1385 struct ethercom *ec = (void *) ifp;
1386 struct eccapreq *eccr;
1387 struct ifreq *ifr = (struct ifreq *)data;
1388 struct if_laddrreq *iflr = data;
1389 const struct sockaddr_dl *sdl;
1390 static const uint8_t zero[ETHER_ADDR_LEN];
1391 int error;
1392
1393 switch (cmd) {
1394 case SIOCINITIFADDR:
1395 {
1396 struct ifaddr *ifa = (struct ifaddr *)data;
1397 if (ifa->ifa_addr->sa_family != AF_LINK
1398 && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1399 (IFF_UP|IFF_RUNNING)) {
1400 ifp->if_flags |= IFF_UP;
1401 if ((error = (*ifp->if_init)(ifp)) != 0)
1402 return error;
1403 }
1404 #ifdef INET
1405 if (ifa->ifa_addr->sa_family == AF_INET)
1406 arp_ifinit(ifp, ifa);
1407 #endif /* INET */
1408 return 0;
1409 }
1410
1411 case SIOCSIFMTU:
1412 {
1413 int maxmtu;
1414
1415 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1416 maxmtu = ETHERMTU_JUMBO;
1417 else
1418 maxmtu = ETHERMTU;
1419
1420 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1421 return EINVAL;
1422 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1423 return error;
1424 else if (ifp->if_flags & IFF_UP) {
1425 /* Make sure the device notices the MTU change. */
1426 return (*ifp->if_init)(ifp);
1427 } else
1428 return 0;
1429 }
1430
1431 case SIOCSIFFLAGS:
1432 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1433 return error;
1434 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1435 case IFF_RUNNING:
1436 /*
1437 * If interface is marked down and it is running,
1438 * then stop and disable it.
1439 */
1440 (*ifp->if_stop)(ifp, 1);
1441 break;
1442 case IFF_UP:
1443 /*
1444 * If interface is marked up and it is stopped, then
1445 * start it.
1446 */
1447 return (*ifp->if_init)(ifp);
1448 case IFF_UP|IFF_RUNNING:
1449 error = 0;
1450 if (ec->ec_ifflags_cb == NULL ||
1451 (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
1452 /*
1453 * Reset the interface to pick up
1454 * changes in any other flags that
1455 * affect the hardware state.
1456 */
1457 return (*ifp->if_init)(ifp);
1458 } else
1459 return error;
1460 case 0:
1461 break;
1462 }
1463 return 0;
1464 case SIOCGETHERCAP:
1465 eccr = (struct eccapreq *)data;
1466 eccr->eccr_capabilities = ec->ec_capabilities;
1467 eccr->eccr_capenable = ec->ec_capenable;
1468 return 0;
1469 case SIOCADDMULTI:
1470 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1471 case SIOCDELMULTI:
1472 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1473 case SIOCSIFMEDIA:
1474 case SIOCGIFMEDIA:
1475 if (ec->ec_mii == NULL)
1476 return ENOTTY;
1477 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1478 case SIOCALIFADDR:
1479 sdl = satocsdl(sstocsa(&iflr->addr));
1480 if (sdl->sdl_family != AF_LINK)
1481 ;
1482 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1483 return EINVAL;
1484 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1485 return EINVAL;
1486 /*FALLTHROUGH*/
1487 default:
1488 return ifioctl_common(ifp, cmd, data);
1489 }
1490 return 0;
1491 }
1492