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