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