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