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