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