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