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