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