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