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