if_ethersubr.c revision 1.95.2.4 1 /* $NetBSD: if_ethersubr.c,v 1.95.2.4 2003/06/24 08:17:22 grant 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.95.2.4 2003/06/24 08:17:22 grant 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 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 = etype;
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 #ifndef PPPOE_SERVER
800 if (m->m_flags & (M_MCAST | M_BCAST)) {
801 m_freem(m);
802 return;
803 }
804 #endif
805
806 if (etype == ETHERTYPE_PPPOEDISC)
807 inq = &ppoediscinq;
808 else
809 inq = &ppoeinq;
810 s = splnet();
811 if (IF_QFULL(inq)) {
812 IF_DROP(inq);
813 m_freem(m);
814 } else
815 IF_ENQUEUE(inq, m);
816 splx(s);
817 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
818 if (!callout_active(&pppoe_softintr))
819 callout_reset(&pppoe_softintr, 1, pppoe_softintr_handler, NULL);
820 #else
821 softintr_schedule(pppoe_softintr);
822 #endif
823 return;
824 #endif /* NPPPOE > 0 */
825 default:
826 ; /* Nothing. */
827 }
828
829 /* Strip off the Ethernet header. */
830 m_adj(m, sizeof(struct ether_header));
831
832 /* If the CRC is still on the packet, trim it off. */
833 if (m->m_flags & M_HASFCS) {
834 m_adj(m, -ETHER_CRC_LEN);
835 m->m_flags &= ~M_HASFCS;
836 }
837
838 switch (etype) {
839 #ifdef INET
840 case ETHERTYPE_IP:
841 #ifdef GATEWAY
842 if (ipflow_fastforward(m))
843 return;
844 #endif
845 schednetisr(NETISR_IP);
846 inq = &ipintrq;
847 break;
848
849 case ETHERTYPE_ARP:
850 schednetisr(NETISR_ARP);
851 inq = &arpintrq;
852 break;
853
854 case ETHERTYPE_REVARP:
855 revarpinput(m); /* XXX queue? */
856 return;
857 #endif
858 #ifdef INET6
859 case ETHERTYPE_IPV6:
860 schednetisr(NETISR_IPV6);
861 inq = &ip6intrq;
862 break;
863 #endif
864 #ifdef NS
865 case ETHERTYPE_NS:
866 schednetisr(NETISR_NS);
867 inq = &nsintrq;
868 break;
869
870 #endif
871 #ifdef IPX
872 case ETHERTYPE_IPX:
873 schednetisr(NETISR_IPX);
874 inq = &ipxintrq;
875 break;
876 #endif
877 #ifdef NETATALK
878 case ETHERTYPE_ATALK:
879 schednetisr(NETISR_ATALK);
880 inq = &atintrq1;
881 break;
882 case ETHERTYPE_AARP:
883 /* probably this should be done with a NETISR as well */
884 aarpinput(ifp, m); /* XXX */
885 return;
886 #endif /* NETATALK */
887 default:
888 #if defined (ISO) || defined (LLC) || defined (NETATALK)
889 if (etype > ETHERMTU)
890 goto dropanyway;
891 l = mtod(m, struct llc *);
892 switch (l->llc_dsap) {
893 #ifdef NETATALK
894 case LLC_SNAP_LSAP:
895 switch (l->llc_control) {
896 case LLC_UI:
897 if (l->llc_ssap != LLC_SNAP_LSAP) {
898 goto dropanyway;
899 }
900
901 if (Bcmp(&(l->llc_snap_org_code)[0],
902 at_org_code, sizeof(at_org_code)) == 0 &&
903 ntohs(l->llc_snap_ether_type) ==
904 ETHERTYPE_ATALK) {
905 inq = &atintrq2;
906 m_adj(m, sizeof(struct llc));
907 schednetisr(NETISR_ATALK);
908 break;
909 }
910
911 if (Bcmp(&(l->llc_snap_org_code)[0],
912 aarp_org_code,
913 sizeof(aarp_org_code)) == 0 &&
914 ntohs(l->llc_snap_ether_type) ==
915 ETHERTYPE_AARP) {
916 m_adj( m, sizeof(struct llc));
917 aarpinput(ifp, m); /* XXX */
918 return;
919 }
920
921 default:
922 goto dropanyway;
923 }
924 break;
925 #endif /* NETATALK */
926 #ifdef ISO
927 case LLC_ISO_LSAP:
928 switch (l->llc_control) {
929 case LLC_UI:
930 /* LLC_UI_P forbidden in class 1 service */
931 if ((l->llc_dsap == LLC_ISO_LSAP) &&
932 (l->llc_ssap == LLC_ISO_LSAP)) {
933 /* LSAP for ISO */
934 if (m->m_pkthdr.len > etype)
935 m_adj(m, etype - m->m_pkthdr.len);
936 m->m_data += 3; /* XXX */
937 m->m_len -= 3; /* XXX */
938 m->m_pkthdr.len -= 3; /* XXX */
939 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
940 if (m == 0)
941 return;
942 *mtod(m, struct ether_header *) = *eh;
943 #ifdef ARGO_DEBUG
944 if (argo_debug[D_ETHER])
945 printf("clnp packet");
946 #endif
947 schednetisr(NETISR_ISO);
948 inq = &clnlintrq;
949 break;
950 }
951 goto dropanyway;
952
953 case LLC_XID:
954 case LLC_XID_P:
955 if(m->m_len < 6)
956 goto dropanyway;
957 l->llc_window = 0;
958 l->llc_fid = 9;
959 l->llc_class = 1;
960 l->llc_dsap = l->llc_ssap = 0;
961 /* Fall through to */
962 case LLC_TEST:
963 case LLC_TEST_P:
964 {
965 struct sockaddr sa;
966 struct ether_header *eh2;
967 int i;
968 u_char c = l->llc_dsap;
969
970 l->llc_dsap = l->llc_ssap;
971 l->llc_ssap = c;
972 if (m->m_flags & (M_BCAST | M_MCAST))
973 bcopy(LLADDR(ifp->if_sadl),
974 (caddr_t)eh->ether_dhost, 6);
975 sa.sa_family = AF_UNSPEC;
976 sa.sa_len = sizeof(sa);
977 eh2 = (struct ether_header *)sa.sa_data;
978 for (i = 0; i < 6; i++) {
979 eh2->ether_shost[i] = c =
980 eh->ether_dhost[i];
981 eh2->ether_dhost[i] =
982 eh->ether_dhost[i] =
983 eh->ether_shost[i];
984 eh->ether_shost[i] = c;
985 }
986 ifp->if_output(ifp, m, &sa, NULL);
987 return;
988 }
989 default:
990 m_freem(m);
991 return;
992 }
993 break;
994 #endif /* ISO */
995 #ifdef LLC
996 case LLC_X25_LSAP:
997 {
998 if (m->m_pkthdr.len > etype)
999 m_adj(m, etype - m->m_pkthdr.len);
1000 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
1001 if (m == 0)
1002 return;
1003 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
1004 eh->ether_dhost, LLC_X25_LSAP, 6,
1005 mtod(m, struct sdl_hdr *)))
1006 panic("ETHER cons addr failure");
1007 mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
1008 #ifdef LLC_DEBUG
1009 printf("llc packet\n");
1010 #endif /* LLC_DEBUG */
1011 schednetisr(NETISR_CCITT);
1012 inq = &llcintrq;
1013 break;
1014 }
1015 #endif /* LLC */
1016 dropanyway:
1017 default:
1018 m_freem(m);
1019 return;
1020 }
1021 #else /* ISO || LLC || NETATALK*/
1022 m_freem(m);
1023 return;
1024 #endif /* ISO || LLC || NETATALK*/
1025 }
1026
1027 s = splnet();
1028 if (IF_QFULL(inq)) {
1029 IF_DROP(inq);
1030 m_freem(m);
1031 } else
1032 IF_ENQUEUE(inq, m);
1033 splx(s);
1034 }
1035
1036 /*
1037 * Convert Ethernet address to printable (loggable) representation.
1038 */
1039 static char digits[] = "0123456789abcdef";
1040 char *
1041 ether_sprintf(const u_char *ap)
1042 {
1043 static char etherbuf[18];
1044 char *cp = etherbuf;
1045 int i;
1046
1047 for (i = 0; i < 6; i++) {
1048 *cp++ = digits[*ap >> 4];
1049 *cp++ = digits[*ap++ & 0xf];
1050 *cp++ = ':';
1051 }
1052 *--cp = 0;
1053 return (etherbuf);
1054 }
1055
1056 /*
1057 * Perform common duties while attaching to interface list
1058 */
1059 void
1060 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
1061 {
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(&((struct ethercom *)ifp)->ec_multiaddrs);
1077 ifp->if_broadcastaddr = etherbroadcastaddr;
1078 #if NBPFILTER > 0
1079 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1080 #endif
1081 }
1082
1083 void
1084 ether_ifdetach(struct ifnet *ifp)
1085 {
1086 struct ethercom *ec = (void *) ifp;
1087 struct ether_multi *enm;
1088 int s;
1089
1090 #if NBPFILTER > 0
1091 bpfdetach(ifp);
1092 #endif
1093
1094 #if NVLAN > 0
1095 if (ec->ec_nvlans)
1096 vlan_ifdetach(ifp);
1097 #endif
1098
1099 s = splnet();
1100 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1101 LIST_REMOVE(enm, enm_list);
1102 free(enm, M_IFADDR);
1103 ec->ec_multicnt--;
1104 }
1105 splx(s);
1106
1107 #if 0 /* done in if_detach() */
1108 if_free_sadl(ifp);
1109 #endif
1110 }
1111
1112 #if 0
1113 /*
1114 * This is for reference. We have a table-driven version
1115 * of the little-endian crc32 generator, which is faster
1116 * than the double-loop.
1117 */
1118 u_int32_t
1119 ether_crc32_le(const u_int8_t *buf, size_t len)
1120 {
1121 u_int32_t c, crc, carry;
1122 size_t i, j;
1123
1124 crc = 0xffffffffU; /* initial value */
1125
1126 for (i = 0; i < len; i++) {
1127 c = buf[i];
1128 for (j = 0; j < 8; j++) {
1129 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1130 crc >>= 1;
1131 c >>= 1;
1132 if (carry)
1133 crc = (crc ^ ETHER_CRC_POLY_LE);
1134 }
1135 }
1136
1137 return (crc);
1138 }
1139 #else
1140 u_int32_t
1141 ether_crc32_le(const u_int8_t *buf, size_t len)
1142 {
1143 static const u_int32_t crctab[] = {
1144 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1145 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1146 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1147 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1148 };
1149 u_int32_t crc;
1150 int i;
1151
1152 crc = 0xffffffffU; /* initial value */
1153
1154 for (i = 0; i < len; i++) {
1155 crc ^= buf[i];
1156 crc = (crc >> 4) ^ crctab[crc & 0xf];
1157 crc = (crc >> 4) ^ crctab[crc & 0xf];
1158 }
1159
1160 return (crc);
1161 }
1162 #endif
1163
1164 u_int32_t
1165 ether_crc32_be(const u_int8_t *buf, size_t len)
1166 {
1167 u_int32_t c, crc, carry;
1168 size_t i, j;
1169
1170 crc = 0xffffffffU; /* initial value */
1171
1172 for (i = 0; i < len; i++) {
1173 c = buf[i];
1174 for (j = 0; j < 8; j++) {
1175 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1176 crc <<= 1;
1177 c >>= 1;
1178 if (carry)
1179 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1180 }
1181 }
1182
1183 return (crc);
1184 }
1185
1186 #ifdef INET
1187 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1188 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1189 #endif
1190 #ifdef INET6
1191 u_char ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1192 u_char ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1193 #endif
1194
1195 /*
1196 * Convert a sockaddr into an Ethernet address or range of Ethernet
1197 * addresses.
1198 */
1199 int
1200 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
1201 u_int8_t addrhi[ETHER_ADDR_LEN])
1202 {
1203 #ifdef INET
1204 struct sockaddr_in *sin;
1205 #endif /* INET */
1206 #ifdef INET6
1207 struct sockaddr_in6 *sin6;
1208 #endif /* INET6 */
1209
1210 switch (sa->sa_family) {
1211
1212 case AF_UNSPEC:
1213 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
1214 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1215 break;
1216
1217 #ifdef INET
1218 case AF_INET:
1219 sin = satosin(sa);
1220 if (sin->sin_addr.s_addr == INADDR_ANY) {
1221 /*
1222 * An IP address of INADDR_ANY means listen to
1223 * or stop listening to all of the Ethernet
1224 * multicast addresses used for IP.
1225 * (This is for the sake of IP multicast routers.)
1226 */
1227 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
1228 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
1229 }
1230 else {
1231 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1232 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1233 }
1234 break;
1235 #endif
1236 #ifdef INET6
1237 case AF_INET6:
1238 sin6 = satosin6(sa);
1239 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1240 /*
1241 * An IP6 address of 0 means listen to or stop
1242 * listening to all of the Ethernet multicast
1243 * address used for IP6.
1244 * (This is used for multicast routers.)
1245 */
1246 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1247 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1248 } else {
1249 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1250 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1251 }
1252 break;
1253 #endif
1254
1255 default:
1256 return (EAFNOSUPPORT);
1257 }
1258 return (0);
1259 }
1260
1261 /*
1262 * Add an Ethernet multicast address or range of addresses to the list for a
1263 * given interface.
1264 */
1265 int
1266 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1267 {
1268 struct ether_multi *enm;
1269 u_char addrlo[ETHER_ADDR_LEN];
1270 u_char addrhi[ETHER_ADDR_LEN];
1271 int s = splnet(), error;
1272
1273 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1274 if (error != 0) {
1275 splx(s);
1276 return (error);
1277 }
1278
1279 /*
1280 * Verify that we have valid Ethernet multicast addresses.
1281 */
1282 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1283 splx(s);
1284 return (EINVAL);
1285 }
1286 /*
1287 * See if the address range is already in the list.
1288 */
1289 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1290 if (enm != NULL) {
1291 /*
1292 * Found it; just increment the reference count.
1293 */
1294 ++enm->enm_refcount;
1295 splx(s);
1296 return (0);
1297 }
1298 /*
1299 * New address or range; malloc a new multicast record
1300 * and link it into the interface's multicast list.
1301 */
1302 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1303 if (enm == NULL) {
1304 splx(s);
1305 return (ENOBUFS);
1306 }
1307 bcopy(addrlo, enm->enm_addrlo, 6);
1308 bcopy(addrhi, enm->enm_addrhi, 6);
1309 enm->enm_ec = ec;
1310 enm->enm_refcount = 1;
1311 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1312 ec->ec_multicnt++;
1313 splx(s);
1314 /*
1315 * Return ENETRESET to inform the driver that the list has changed
1316 * and its reception filter should be adjusted accordingly.
1317 */
1318 return (ENETRESET);
1319 }
1320
1321 /*
1322 * Delete a multicast address record.
1323 */
1324 int
1325 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1326 {
1327 struct ether_multi *enm;
1328 u_char addrlo[ETHER_ADDR_LEN];
1329 u_char addrhi[ETHER_ADDR_LEN];
1330 int s = splnet(), error;
1331
1332 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1333 if (error != 0) {
1334 splx(s);
1335 return (error);
1336 }
1337
1338 /*
1339 * Look ur the address in our list.
1340 */
1341 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1342 if (enm == NULL) {
1343 splx(s);
1344 return (ENXIO);
1345 }
1346 if (--enm->enm_refcount != 0) {
1347 /*
1348 * Still some claims to this record.
1349 */
1350 splx(s);
1351 return (0);
1352 }
1353 /*
1354 * No remaining claims to this record; unlink and free it.
1355 */
1356 LIST_REMOVE(enm, enm_list);
1357 free(enm, M_IFMADDR);
1358 ec->ec_multicnt--;
1359 splx(s);
1360 /*
1361 * Return ENETRESET to inform the driver that the list has changed
1362 * and its reception filter should be adjusted accordingly.
1363 */
1364 return (ENETRESET);
1365 }
1366
1367 /*
1368 * Common ioctls for Ethernet interfaces. Note, we must be
1369 * called at splnet().
1370 */
1371 int
1372 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1373 {
1374 struct ethercom *ec = (void *) ifp;
1375 struct ifreq *ifr = (struct ifreq *)data;
1376 struct ifaddr *ifa = (struct ifaddr *)data;
1377 int error = 0;
1378
1379 switch (cmd) {
1380 case SIOCSIFADDR:
1381 ifp->if_flags |= IFF_UP;
1382 switch (ifa->ifa_addr->sa_family) {
1383 case AF_LINK:
1384 {
1385 struct sockaddr_dl *sdl =
1386 (struct sockaddr_dl *) ifa->ifa_addr;
1387
1388 if (sdl->sdl_type != IFT_ETHER ||
1389 sdl->sdl_alen != ifp->if_addrlen) {
1390 error = EINVAL;
1391 break;
1392 }
1393
1394 memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
1395 ifp->if_addrlen);
1396
1397 /* Set new address. */
1398 error = (*ifp->if_init)(ifp);
1399 break;
1400 }
1401 #ifdef INET
1402 case AF_INET:
1403 if ((ifp->if_flags & IFF_RUNNING) == 0 &&
1404 (error = (*ifp->if_init)(ifp)) != 0)
1405 break;
1406 arp_ifinit(ifp, ifa);
1407 break;
1408 #endif /* INET */
1409 #ifdef NS
1410 case AF_NS:
1411 {
1412 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1413
1414 if (ns_nullhost(*ina))
1415 ina->x_host = *(union ns_host *)
1416 LLADDR(ifp->if_sadl);
1417 else
1418 memcpy(LLADDR(ifp->if_sadl),
1419 ina->x_host.c_host, ifp->if_addrlen);
1420 /* Set new address. */
1421 error = (*ifp->if_init)(ifp);
1422 break;
1423 }
1424 #endif /* NS */
1425 default:
1426 if ((ifp->if_flags & IFF_RUNNING) == 0)
1427 error = (*ifp->if_init)(ifp);
1428 break;
1429 }
1430 break;
1431
1432 case SIOCGIFADDR:
1433 memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1434 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1435 break;
1436
1437 case SIOCSIFMTU:
1438 {
1439 int maxmtu;
1440
1441 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1442 maxmtu = ETHERMTU_JUMBO;
1443 else
1444 maxmtu = ETHERMTU;
1445
1446 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1447 error = EINVAL;
1448 else {
1449 ifp->if_mtu = ifr->ifr_mtu;
1450
1451 /* Make sure the device notices the MTU change. */
1452 if (ifp->if_flags & IFF_UP)
1453 error = (*ifp->if_init)(ifp);
1454 }
1455 break;
1456 }
1457
1458 case SIOCSIFFLAGS:
1459 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1460 /*
1461 * If interface is marked down and it is running,
1462 * then stop and disable it.
1463 */
1464 (*ifp->if_stop)(ifp, 1);
1465 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1466 /*
1467 * If interface is marked up and it is stopped, then
1468 * start it.
1469 */
1470 error = (*ifp->if_init)(ifp);
1471 } else if ((ifp->if_flags & IFF_UP) != 0) {
1472 /*
1473 * Reset the interface to pick up changes in any other
1474 * flags that affect the hardware state.
1475 */
1476 error = (*ifp->if_init)(ifp);
1477 }
1478 break;
1479
1480 case SIOCADDMULTI:
1481 error = ether_addmulti(ifr, ec);
1482 break;
1483
1484 case SIOCDELMULTI:
1485 error = ether_delmulti(ifr, ec);
1486 break;
1487
1488 default:
1489 error = ENOTTY;
1490 }
1491
1492 return (error);
1493 }
1494