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