if_ethersubr.c revision 1.69 1 /* $NetBSD: if_ethersubr.c,v 1.69 2000/11/15 01:02:15 thorpej 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 #if defined (ISO) || defined (LLC) || defined(NETATALK)
509 struct llc *l;
510 #endif
511
512 if ((ifp->if_flags & IFF_UP) == 0) {
513 m_freem(m);
514 return;
515 }
516
517 eh = mtod(m, struct ether_header *);
518 etype = ntohs(eh->ether_type);
519
520 /*
521 * Determine if the packet is within its size limits.
522 */
523 if (m->m_pkthdr.len > ETHER_MAX_FRAME(etype, m->m_flags & M_HASFCS)) {
524 printf("%s: discarding oversize frame (len=%d)\n",
525 ifp->if_xname, m->m_pkthdr.len);
526 m_freem(m);
527 return;
528 }
529
530 ifp->if_lastchange = time;
531 ifp->if_ibytes += m->m_pkthdr.len;
532 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
533 if (memcmp(etherbroadcastaddr,
534 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
535 m->m_flags |= M_BCAST;
536 else
537 m->m_flags |= M_MCAST;
538 ifp->if_imcasts++;
539 } else if ((ifp->if_flags & IFF_PROMISC) != 0 &&
540 memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
541 ETHER_ADDR_LEN) != 0) {
542 m_freem(m);
543 return;
544 }
545
546 /*
547 * Handle protocols that expect to have the Ethernet header
548 * (and possibly FCS) intact.
549 */
550 switch (etype) {
551 #if NVLAN > 0
552 case ETHERTYPE_VLAN:
553 /*
554 * vlan_input() will either recursively call ether_input()
555 * or drop the packet.
556 */
557 if (((struct ethercom *)ifp)->ec_nvlans != 0)
558 vlan_input(ifp, m);
559 else
560 m_freem(m);
561 return;
562 #endif /* NVLAN > 0 */
563 default:
564 /* Nothing. */
565 }
566
567 /* Strip off the Ethernet header. */
568 m_adj(m, sizeof(struct ether_header));
569
570 /* If the CRC is still on the packet, trim it off. */
571 if (m->m_flags & M_HASFCS)
572 m_adj(m, -ETHER_CRC_LEN);
573
574 switch (etype) {
575 #ifdef INET
576 case ETHERTYPE_IP:
577 #ifdef GATEWAY
578 if (ipflow_fastforward(m))
579 return;
580 #endif
581 schednetisr(NETISR_IP);
582 inq = &ipintrq;
583 break;
584
585 case ETHERTYPE_ARP:
586 schednetisr(NETISR_ARP);
587 inq = &arpintrq;
588 break;
589
590 case ETHERTYPE_REVARP:
591 revarpinput(m); /* XXX queue? */
592 return;
593 #endif
594 #ifdef INET6
595 case ETHERTYPE_IPV6:
596 schednetisr(NETISR_IPV6);
597 inq = &ip6intrq;
598 break;
599 #endif
600 #ifdef NS
601 case ETHERTYPE_NS:
602 schednetisr(NETISR_NS);
603 inq = &nsintrq;
604 break;
605
606 #endif
607 #ifdef IPX
608 case ETHERTYPE_IPX:
609 schednetisr(NETISR_IPX);
610 inq = &ipxintrq;
611 break;
612 #endif
613 #ifdef NETATALK
614 case ETHERTYPE_ATALK:
615 schednetisr(NETISR_ATALK);
616 inq = &atintrq1;
617 break;
618 case ETHERTYPE_AARP:
619 /* probably this should be done with a NETISR as well */
620 aarpinput(ifp, m); /* XXX */
621 return;
622 #endif /* NETATALK */
623 default:
624 #if defined (ISO) || defined (LLC) || defined (NETATALK)
625 if (etype > ETHERMTU)
626 goto dropanyway;
627 l = mtod(m, struct llc *);
628 switch (l->llc_dsap) {
629 #ifdef NETATALK
630 case LLC_SNAP_LSAP:
631 switch (l->llc_control) {
632 case LLC_UI:
633 if (l->llc_ssap != LLC_SNAP_LSAP) {
634 goto dropanyway;
635 }
636
637 if (Bcmp(&(l->llc_snap_org_code)[0],
638 at_org_code, sizeof(at_org_code)) == 0 &&
639 ntohs(l->llc_snap_ether_type) ==
640 ETHERTYPE_ATALK) {
641 inq = &atintrq2;
642 m_adj(m, sizeof(struct llc));
643 schednetisr(NETISR_ATALK);
644 break;
645 }
646
647 if (Bcmp(&(l->llc_snap_org_code)[0],
648 aarp_org_code,
649 sizeof(aarp_org_code)) == 0 &&
650 ntohs(l->llc_snap_ether_type) ==
651 ETHERTYPE_AARP) {
652 m_adj( m, sizeof(struct llc));
653 aarpinput(ifp, m); /* XXX */
654 return;
655 }
656
657 default:
658 goto dropanyway;
659 }
660 break;
661 #endif /* NETATALK */
662 #ifdef ISO
663 case LLC_ISO_LSAP:
664 switch (l->llc_control) {
665 case LLC_UI:
666 /* LLC_UI_P forbidden in class 1 service */
667 if ((l->llc_dsap == LLC_ISO_LSAP) &&
668 (l->llc_ssap == LLC_ISO_LSAP)) {
669 /* LSAP for ISO */
670 if (m->m_pkthdr.len > etype)
671 m_adj(m, etype - m->m_pkthdr.len);
672 m->m_data += 3; /* XXX */
673 m->m_len -= 3; /* XXX */
674 m->m_pkthdr.len -= 3; /* XXX */
675 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
676 if (m == 0)
677 return;
678 *mtod(m, struct ether_header *) = *eh;
679 #ifdef ARGO_DEBUG
680 if (argo_debug[D_ETHER])
681 printf("clnp packet");
682 #endif
683 schednetisr(NETISR_ISO);
684 inq = &clnlintrq;
685 break;
686 }
687 goto dropanyway;
688
689 case LLC_XID:
690 case LLC_XID_P:
691 if(m->m_len < 6)
692 goto dropanyway;
693 l->llc_window = 0;
694 l->llc_fid = 9;
695 l->llc_class = 1;
696 l->llc_dsap = l->llc_ssap = 0;
697 /* Fall through to */
698 case LLC_TEST:
699 case LLC_TEST_P:
700 {
701 struct sockaddr sa;
702 struct ether_header *eh2;
703 int i;
704 u_char c = l->llc_dsap;
705
706 l->llc_dsap = l->llc_ssap;
707 l->llc_ssap = c;
708 if (m->m_flags & (M_BCAST | M_MCAST))
709 bcopy(LLADDR(ifp->if_sadl),
710 (caddr_t)eh->ether_dhost, 6);
711 sa.sa_family = AF_UNSPEC;
712 sa.sa_len = sizeof(sa);
713 eh2 = (struct ether_header *)sa.sa_data;
714 for (i = 0; i < 6; i++) {
715 eh2->ether_shost[i] = c =
716 eh->ether_dhost[i];
717 eh2->ether_dhost[i] =
718 eh->ether_dhost[i] =
719 eh->ether_shost[i];
720 eh->ether_shost[i] = c;
721 }
722 ifp->if_output(ifp, m, &sa, NULL);
723 return;
724 }
725 default:
726 m_freem(m);
727 return;
728 }
729 break;
730 #endif /* ISO */
731 #ifdef LLC
732 case LLC_X25_LSAP:
733 {
734 if (m->m_pkthdr.len > etype)
735 m_adj(m, etype - m->m_pkthdr.len);
736 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
737 if (m == 0)
738 return;
739 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
740 eh->ether_dhost, LLC_X25_LSAP, 6,
741 mtod(m, struct sdl_hdr *)))
742 panic("ETHER cons addr failure");
743 mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
744 #ifdef LLC_DEBUG
745 printf("llc packet\n");
746 #endif /* LLC_DEBUG */
747 schednetisr(NETISR_CCITT);
748 inq = &llcintrq;
749 break;
750 }
751 #endif /* LLC */
752 dropanyway:
753 default:
754 m_freem(m);
755 return;
756 }
757 #else /* ISO || LLC || NETATALK*/
758 m_freem(m);
759 return;
760 #endif /* ISO || LLC || NETATALK*/
761 }
762
763 s = splimp();
764 if (IF_QFULL(inq)) {
765 IF_DROP(inq);
766 m_freem(m);
767 } else
768 IF_ENQUEUE(inq, m);
769 splx(s);
770 }
771
772 /*
773 * Convert Ethernet address to printable (loggable) representation.
774 */
775 static char digits[] = "0123456789abcdef";
776 char *
777 ether_sprintf(const u_char *ap)
778 {
779 static char etherbuf[18];
780 char *cp = etherbuf;
781 int i;
782
783 for (i = 0; i < 6; i++) {
784 *cp++ = digits[*ap >> 4];
785 *cp++ = digits[*ap++ & 0xf];
786 *cp++ = ':';
787 }
788 *--cp = 0;
789 return (etherbuf);
790 }
791
792 /*
793 * Perform common duties while attaching to interface list
794 */
795 void
796 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
797 {
798 struct sockaddr_dl *sdl;
799
800 ifp->if_type = IFT_ETHER;
801 ifp->if_addrlen = 6;
802 ifp->if_hdrlen = 14;
803 ifp->if_mtu = ETHERMTU;
804 ifp->if_output = ether_output;
805 ifp->if_input = ether_input;
806 if (ifp->if_baudrate == 0)
807 ifp->if_baudrate = IF_Mbps(10); /* just a default */
808 if ((sdl = ifp->if_sadl) &&
809 sdl->sdl_family == AF_LINK) {
810 sdl->sdl_type = IFT_ETHER;
811 sdl->sdl_alen = ifp->if_addrlen;
812 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
813 }
814 LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
815 ifp->if_broadcastaddr = etherbroadcastaddr;
816 #if NBPFILTER > 0
817 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
818 #endif
819 }
820
821 void
822 ether_ifdetach(struct ifnet *ifp)
823 {
824 struct ethercom *ec = (void *) ifp;
825 struct sockaddr_dl *sdl = ifp->if_sadl;
826 struct ether_multi *enm;
827 int s;
828
829 #if NBPFILTER > 0
830 bpfdetach(ifp);
831 #endif
832
833 #if NVLAN > 0
834 if (ec->ec_nvlans)
835 vlan_ifdetach(ifp);
836 #endif
837
838 s = splimp();
839 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
840 LIST_REMOVE(enm, enm_list);
841 free(enm, M_IFADDR);
842 ec->ec_multicnt--;
843 }
844 splx(s);
845
846 memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
847 sdl->sdl_alen = 0;
848 sdl->sdl_type = 0;
849 }
850
851 #if 0
852 /*
853 * This is for reference. We have a table-driven version
854 * of the little-endian crc32 generator, which is faster
855 * than the double-loop.
856 */
857 u_int32_t
858 ether_crc32_le(const u_int8_t *buf, size_t len)
859 {
860 u_int32_t c, crc, carry;
861 size_t i, j;
862
863 crc = 0xffffffffU; /* initial value */
864
865 for (i = 0; i < len; i++) {
866 c = buf[i];
867 for (j = 0; j < 8; j++) {
868 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
869 crc >>= 1;
870 c >>= 1;
871 if (carry)
872 crc = (crc ^ ETHER_CRC_POLY_LE);
873 }
874 }
875
876 return (crc);
877 }
878 #else
879 u_int32_t
880 ether_crc32_le(const u_int8_t *buf, size_t len)
881 {
882 static const u_int32_t crctab[] = {
883 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
884 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
885 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
886 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
887 };
888 u_int32_t crc;
889 int i;
890
891 crc = 0xffffffffU; /* initial value */
892
893 for (i = 0; i < len; i++) {
894 crc ^= buf[i];
895 crc = (crc >> 4) ^ crctab[crc & 0xf];
896 crc = (crc >> 4) ^ crctab[crc & 0xf];
897 }
898
899 return (crc);
900 }
901 #endif
902
903 u_int32_t
904 ether_crc32_be(const u_int8_t *buf, size_t len)
905 {
906 u_int32_t c, crc, carry;
907 size_t i, j;
908
909 crc = 0xffffffffU; /* initial value */
910
911 for (i = 0; i < len; i++) {
912 c = buf[i];
913 for (j = 0; j < 8; j++) {
914 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
915 crc <<= 1;
916 c >>= 1;
917 if (carry)
918 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
919 }
920 }
921
922 return (crc);
923 }
924
925 #ifdef INET
926 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
927 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
928 #endif
929 #ifdef INET6
930 u_char ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
931 u_char ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
932 #endif
933
934 /*
935 * Convert a sockaddr into an Ethernet address or range of Ethernet
936 * addresses.
937 */
938 int
939 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
940 u_int8_t addrhi[ETHER_ADDR_LEN])
941 {
942 #ifdef INET
943 struct sockaddr_in *sin;
944 #endif /* INET */
945 #ifdef INET6
946 struct sockaddr_in6 *sin6;
947 #endif /* INET6 */
948
949 switch (sa->sa_family) {
950
951 case AF_UNSPEC:
952 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
953 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
954 break;
955
956 #ifdef INET
957 case AF_INET:
958 sin = satosin(sa);
959 if (sin->sin_addr.s_addr == INADDR_ANY) {
960 /*
961 * An IP address of INADDR_ANY means listen to
962 * or stop listening to all of the Ethernet
963 * multicast addresses used for IP.
964 * (This is for the sake of IP multicast routers.)
965 */
966 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
967 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
968 }
969 else {
970 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
971 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
972 }
973 break;
974 #endif
975 #ifdef INET6
976 case AF_INET6:
977 sin6 = satosin6(sa);
978 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
979 /*
980 * An IP6 address of 0 means listen to or stop
981 * listening to all of the Ethernet multicast
982 * address used for IP6.
983 * (This is used for multicast routers.)
984 */
985 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
986 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
987 } else {
988 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
989 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
990 }
991 break;
992 #endif
993
994 default:
995 return (EAFNOSUPPORT);
996 }
997 return (0);
998 }
999
1000 /*
1001 * Add an Ethernet multicast address or range of addresses to the list for a
1002 * given interface.
1003 */
1004 int
1005 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1006 {
1007 struct ether_multi *enm;
1008 u_char addrlo[ETHER_ADDR_LEN];
1009 u_char addrhi[ETHER_ADDR_LEN];
1010 int s = splimp(), error;
1011
1012 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1013 if (error != 0) {
1014 splx(s);
1015 return (error);
1016 }
1017
1018 /*
1019 * Verify that we have valid Ethernet multicast addresses.
1020 */
1021 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1022 splx(s);
1023 return (EINVAL);
1024 }
1025 /*
1026 * See if the address range is already in the list.
1027 */
1028 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1029 if (enm != NULL) {
1030 /*
1031 * Found it; just increment the reference count.
1032 */
1033 ++enm->enm_refcount;
1034 splx(s);
1035 return (0);
1036 }
1037 /*
1038 * New address or range; malloc a new multicast record
1039 * and link it into the interface's multicast list.
1040 */
1041 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1042 if (enm == NULL) {
1043 splx(s);
1044 return (ENOBUFS);
1045 }
1046 bcopy(addrlo, enm->enm_addrlo, 6);
1047 bcopy(addrhi, enm->enm_addrhi, 6);
1048 enm->enm_ec = ec;
1049 enm->enm_refcount = 1;
1050 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1051 ec->ec_multicnt++;
1052 splx(s);
1053 /*
1054 * Return ENETRESET to inform the driver that the list has changed
1055 * and its reception filter should be adjusted accordingly.
1056 */
1057 return (ENETRESET);
1058 }
1059
1060 /*
1061 * Delete a multicast address record.
1062 */
1063 int
1064 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1065 {
1066 struct ether_multi *enm;
1067 u_char addrlo[ETHER_ADDR_LEN];
1068 u_char addrhi[ETHER_ADDR_LEN];
1069 int s = splimp(), error;
1070
1071 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1072 if (error != 0) {
1073 splx(s);
1074 return (error);
1075 }
1076
1077 /*
1078 * Look ur the address in our list.
1079 */
1080 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1081 if (enm == NULL) {
1082 splx(s);
1083 return (ENXIO);
1084 }
1085 if (--enm->enm_refcount != 0) {
1086 /*
1087 * Still some claims to this record.
1088 */
1089 splx(s);
1090 return (0);
1091 }
1092 /*
1093 * No remaining claims to this record; unlink and free it.
1094 */
1095 LIST_REMOVE(enm, enm_list);
1096 free(enm, M_IFMADDR);
1097 ec->ec_multicnt--;
1098 splx(s);
1099 /*
1100 * Return ENETRESET to inform the driver that the list has changed
1101 * and its reception filter should be adjusted accordingly.
1102 */
1103 return (ENETRESET);
1104 }
1105
1106 /*
1107 * Common ioctls for Ethernet interfaces. Note, we must be
1108 * called at splnet().
1109 */
1110 int
1111 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1112 {
1113 struct ethercom *ec = (void *) ifp;
1114 struct ifreq *ifr = (struct ifreq *)data;
1115 struct ifaddr *ifa = (struct ifaddr *)data;
1116 int error = 0;
1117
1118 switch (cmd) {
1119 case SIOCSIFADDR:
1120 ifp->if_flags |= IFF_UP;
1121 switch (ifa->ifa_addr->sa_family) {
1122 #ifdef INET
1123 case AF_INET:
1124 if ((error = (*ifp->if_init)(ifp)) != 0)
1125 break;
1126 arp_ifinit(ifp, ifa);
1127 break;
1128 #endif /* INET */
1129 #ifdef NS
1130 case AF_NS:
1131 {
1132 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1133
1134 if (ns_nullhost(*ina))
1135 ina->x_host = *(union ns_host *)
1136 LLADDR(ifp->if_sadl);
1137 else
1138 memcpy(LLADDR(ifp->if_sadl),
1139 ina->x_host.c_host, ifp->if_addrlen);
1140 /* Set new address. */
1141 error = (*ifp->if_init)(ifp);
1142 break;
1143 }
1144 #endif /* NS */
1145 default:
1146 error = (*ifp->if_init)(ifp);
1147 break;
1148 }
1149 break;
1150
1151 case SIOCGIFADDR:
1152 memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1153 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1154 break;
1155
1156 case SIOCSIFMTU:
1157 if (ifr->ifr_mtu > ETHERMTU)
1158 error = EINVAL;
1159 else
1160 ifp->if_mtu = ifr->ifr_mtu;
1161 break;
1162
1163 case SIOCSIFFLAGS:
1164 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1165 /*
1166 * If interface is marked down and it is running,
1167 * then stop and disable it.
1168 */
1169 (*ifp->if_stop)(ifp, 1);
1170 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1171 /*
1172 * If interface is marked up and it is stopped, then
1173 * start it.
1174 */
1175 error = (*ifp->if_init)(ifp);
1176 } else if ((ifp->if_flags & IFF_UP) != 0) {
1177 /*
1178 * Reset the interface to pick up changes in any other
1179 * flags that affect the hardware state.
1180 */
1181 error = (*ifp->if_init)(ifp);
1182 }
1183 break;
1184
1185 case SIOCADDMULTI:
1186 case SIOCDELMULTI:
1187 error = (cmd == SIOCADDMULTI) ?
1188 ether_addmulti(ifr, ec) :
1189 ether_delmulti(ifr, ec);
1190 break;
1191
1192 default:
1193 error = ENOTTY;
1194 }
1195
1196 return (error);
1197 }
1198