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