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