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