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