if_ethersubr.c revision 1.108 1 /* $NetBSD: if_ethersubr.c,v 1.108 2003/05/16 04:54:55 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.108 2003/05/16 04:54:55 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_free_sadl(ifp);
1135
1136 MOWNER_DETACH(&ec->ec_rx_mowner);
1137 MOWNER_DETACH(&ec->ec_tx_mowner);
1138 }
1139
1140 #if 0
1141 /*
1142 * This is for reference. We have a table-driven version
1143 * of the little-endian crc32 generator, which is faster
1144 * than the double-loop.
1145 */
1146 u_int32_t
1147 ether_crc32_le(const u_int8_t *buf, size_t len)
1148 {
1149 u_int32_t c, crc, carry;
1150 size_t i, j;
1151
1152 crc = 0xffffffffU; /* initial value */
1153
1154 for (i = 0; i < len; i++) {
1155 c = buf[i];
1156 for (j = 0; j < 8; j++) {
1157 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1158 crc >>= 1;
1159 c >>= 1;
1160 if (carry)
1161 crc = (crc ^ ETHER_CRC_POLY_LE);
1162 }
1163 }
1164
1165 return (crc);
1166 }
1167 #else
1168 u_int32_t
1169 ether_crc32_le(const u_int8_t *buf, size_t len)
1170 {
1171 static const u_int32_t crctab[] = {
1172 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1173 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1174 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1175 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1176 };
1177 u_int32_t crc;
1178 size_t i;
1179
1180 crc = 0xffffffffU; /* initial value */
1181
1182 for (i = 0; i < len; i++) {
1183 crc ^= buf[i];
1184 crc = (crc >> 4) ^ crctab[crc & 0xf];
1185 crc = (crc >> 4) ^ crctab[crc & 0xf];
1186 }
1187
1188 return (crc);
1189 }
1190 #endif
1191
1192 u_int32_t
1193 ether_crc32_be(const u_int8_t *buf, size_t len)
1194 {
1195 u_int32_t c, crc, carry;
1196 size_t i, j;
1197
1198 crc = 0xffffffffU; /* initial value */
1199
1200 for (i = 0; i < len; i++) {
1201 c = buf[i];
1202 for (j = 0; j < 8; j++) {
1203 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1204 crc <<= 1;
1205 c >>= 1;
1206 if (carry)
1207 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1208 }
1209 }
1210
1211 return (crc);
1212 }
1213
1214 #ifdef INET
1215 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1216 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1217 #endif
1218 #ifdef INET6
1219 u_char ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1220 u_char ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1221 #endif
1222
1223 /*
1224 * Convert a sockaddr into an Ethernet address or range of Ethernet
1225 * addresses.
1226 */
1227 int
1228 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
1229 u_int8_t addrhi[ETHER_ADDR_LEN])
1230 {
1231 #ifdef INET
1232 struct sockaddr_in *sin;
1233 #endif /* INET */
1234 #ifdef INET6
1235 struct sockaddr_in6 *sin6;
1236 #endif /* INET6 */
1237
1238 switch (sa->sa_family) {
1239
1240 case AF_UNSPEC:
1241 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
1242 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1243 break;
1244
1245 #ifdef INET
1246 case AF_INET:
1247 sin = satosin(sa);
1248 if (sin->sin_addr.s_addr == INADDR_ANY) {
1249 /*
1250 * An IP address of INADDR_ANY means listen to
1251 * or stop listening to all of the Ethernet
1252 * multicast addresses used for IP.
1253 * (This is for the sake of IP multicast routers.)
1254 */
1255 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
1256 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
1257 }
1258 else {
1259 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1260 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1261 }
1262 break;
1263 #endif
1264 #ifdef INET6
1265 case AF_INET6:
1266 sin6 = satosin6(sa);
1267 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1268 /*
1269 * An IP6 address of 0 means listen to or stop
1270 * listening to all of the Ethernet multicast
1271 * address used for IP6.
1272 * (This is used for multicast routers.)
1273 */
1274 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1275 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1276 } else {
1277 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1278 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1279 }
1280 break;
1281 #endif
1282
1283 default:
1284 return (EAFNOSUPPORT);
1285 }
1286 return (0);
1287 }
1288
1289 /*
1290 * Add an Ethernet multicast address or range of addresses to the list for a
1291 * given interface.
1292 */
1293 int
1294 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1295 {
1296 struct ether_multi *enm;
1297 u_char addrlo[ETHER_ADDR_LEN];
1298 u_char addrhi[ETHER_ADDR_LEN];
1299 int s = splnet(), error;
1300
1301 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1302 if (error != 0) {
1303 splx(s);
1304 return (error);
1305 }
1306
1307 /*
1308 * Verify that we have valid Ethernet multicast addresses.
1309 */
1310 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1311 splx(s);
1312 return (EINVAL);
1313 }
1314 /*
1315 * See if the address range is already in the list.
1316 */
1317 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1318 if (enm != NULL) {
1319 /*
1320 * Found it; just increment the reference count.
1321 */
1322 ++enm->enm_refcount;
1323 splx(s);
1324 return (0);
1325 }
1326 /*
1327 * New address or range; malloc a new multicast record
1328 * and link it into the interface's multicast list.
1329 */
1330 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1331 if (enm == NULL) {
1332 splx(s);
1333 return (ENOBUFS);
1334 }
1335 bcopy(addrlo, enm->enm_addrlo, 6);
1336 bcopy(addrhi, enm->enm_addrhi, 6);
1337 enm->enm_ec = ec;
1338 enm->enm_refcount = 1;
1339 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1340 ec->ec_multicnt++;
1341 splx(s);
1342 /*
1343 * Return ENETRESET to inform the driver that the list has changed
1344 * and its reception filter should be adjusted accordingly.
1345 */
1346 return (ENETRESET);
1347 }
1348
1349 /*
1350 * Delete a multicast address record.
1351 */
1352 int
1353 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1354 {
1355 struct ether_multi *enm;
1356 u_char addrlo[ETHER_ADDR_LEN];
1357 u_char addrhi[ETHER_ADDR_LEN];
1358 int s = splnet(), error;
1359
1360 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1361 if (error != 0) {
1362 splx(s);
1363 return (error);
1364 }
1365
1366 /*
1367 * Look ur the address in our list.
1368 */
1369 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1370 if (enm == NULL) {
1371 splx(s);
1372 return (ENXIO);
1373 }
1374 if (--enm->enm_refcount != 0) {
1375 /*
1376 * Still some claims to this record.
1377 */
1378 splx(s);
1379 return (0);
1380 }
1381 /*
1382 * No remaining claims to this record; unlink and free it.
1383 */
1384 LIST_REMOVE(enm, enm_list);
1385 free(enm, M_IFMADDR);
1386 ec->ec_multicnt--;
1387 splx(s);
1388 /*
1389 * Return ENETRESET to inform the driver that the list has changed
1390 * and its reception filter should be adjusted accordingly.
1391 */
1392 return (ENETRESET);
1393 }
1394
1395 /*
1396 * Common ioctls for Ethernet interfaces. Note, we must be
1397 * called at splnet().
1398 */
1399 int
1400 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1401 {
1402 struct ethercom *ec = (void *) ifp;
1403 struct ifreq *ifr = (struct ifreq *)data;
1404 struct ifaddr *ifa = (struct ifaddr *)data;
1405 int error = 0;
1406
1407 switch (cmd) {
1408 case SIOCSIFADDR:
1409 ifp->if_flags |= IFF_UP;
1410 switch (ifa->ifa_addr->sa_family) {
1411 case AF_LINK:
1412 {
1413 struct sockaddr_dl *sdl =
1414 (struct sockaddr_dl *) ifa->ifa_addr;
1415
1416 if (sdl->sdl_type != IFT_ETHER ||
1417 sdl->sdl_alen != ifp->if_addrlen) {
1418 error = EINVAL;
1419 break;
1420 }
1421
1422 memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
1423 ifp->if_addrlen);
1424
1425 /* Set new address. */
1426 error = (*ifp->if_init)(ifp);
1427 break;
1428 }
1429 #ifdef INET
1430 case AF_INET:
1431 if ((ifp->if_flags & IFF_RUNNING) == 0 &&
1432 (error = (*ifp->if_init)(ifp)) != 0)
1433 break;
1434 arp_ifinit(ifp, ifa);
1435 break;
1436 #endif /* INET */
1437 #ifdef NS
1438 case AF_NS:
1439 {
1440 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1441
1442 if (ns_nullhost(*ina))
1443 ina->x_host = *(union ns_host *)
1444 LLADDR(ifp->if_sadl);
1445 else
1446 memcpy(LLADDR(ifp->if_sadl),
1447 ina->x_host.c_host, ifp->if_addrlen);
1448 /* Set new address. */
1449 error = (*ifp->if_init)(ifp);
1450 break;
1451 }
1452 #endif /* NS */
1453 default:
1454 if ((ifp->if_flags & IFF_RUNNING) == 0)
1455 error = (*ifp->if_init)(ifp);
1456 break;
1457 }
1458 break;
1459
1460 case SIOCGIFADDR:
1461 memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1462 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1463 break;
1464
1465 case SIOCSIFMTU:
1466 {
1467 int maxmtu;
1468
1469 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1470 maxmtu = ETHERMTU_JUMBO;
1471 else
1472 maxmtu = ETHERMTU;
1473
1474 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1475 error = EINVAL;
1476 else {
1477 ifp->if_mtu = ifr->ifr_mtu;
1478
1479 /* Make sure the device notices the MTU change. */
1480 if (ifp->if_flags & IFF_UP)
1481 error = (*ifp->if_init)(ifp);
1482 }
1483 break;
1484 }
1485
1486 case SIOCSIFFLAGS:
1487 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1488 /*
1489 * If interface is marked down and it is running,
1490 * then stop and disable it.
1491 */
1492 (*ifp->if_stop)(ifp, 1);
1493 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1494 /*
1495 * If interface is marked up and it is stopped, then
1496 * start it.
1497 */
1498 error = (*ifp->if_init)(ifp);
1499 } else if ((ifp->if_flags & IFF_UP) != 0) {
1500 /*
1501 * Reset the interface to pick up changes in any other
1502 * flags that affect the hardware state.
1503 */
1504 error = (*ifp->if_init)(ifp);
1505 }
1506 break;
1507
1508 case SIOCADDMULTI:
1509 error = ether_addmulti(ifr, ec);
1510 break;
1511
1512 case SIOCDELMULTI:
1513 error = ether_delmulti(ifr, ec);
1514 break;
1515
1516 default:
1517 error = ENOTTY;
1518 }
1519
1520 return (error);
1521 }
1522