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