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