if_ethersubr.c revision 1.68 1 /* $NetBSD: if_ethersubr.c,v 1.68 2000/10/15 19:49:55 matt 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
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/ioctl.h>
84 #include <sys/errno.h>
85 #include <sys/syslog.h>
86
87 #include <machine/cpu.h>
88
89 #include <net/if.h>
90 #include <net/netisr.h>
91 #include <net/route.h>
92 #include <net/if_llc.h>
93 #include <net/if_dl.h>
94 #include <net/if_types.h>
95
96 #include <net/if_ether.h>
97 #if NVLAN > 0
98 #include <net/if_vlanvar.h>
99 #endif
100
101 #include <netinet/in.h>
102 #ifdef INET
103 #include <netinet/in_var.h>
104 #endif
105 #include <netinet/if_inarp.h>
106
107 #ifdef INET6
108 #ifndef INET
109 #include <netinet/in.h>
110 #endif
111 #include <netinet6/in6_var.h>
112 #include <netinet6/nd6.h>
113 #endif
114
115 #ifdef NS
116 #include <netns/ns.h>
117 #include <netns/ns_if.h>
118 #endif
119
120 #ifdef IPX
121 #include <netipx/ipx.h>
122 #include <netipx/ipx_if.h>
123 #endif
124
125 #ifdef ISO
126 #include <netiso/argo_debug.h>
127 #include <netiso/iso.h>
128 #include <netiso/iso_var.h>
129 #include <netiso/iso_snpac.h>
130 #endif
131
132 #ifdef LLC
133 #include <netccitt/dll.h>
134 #include <netccitt/llc_var.h>
135 #endif
136
137 #if defined(LLC) && defined(CCITT)
138 extern struct ifqueue pkintrq;
139 #endif
140
141 #ifdef NETATALK
142 #include <netatalk/at.h>
143 #include <netatalk/at_var.h>
144 #include <netatalk/at_extern.h>
145
146 #define llc_snap_org_code llc_un.type_snap.org_code
147 #define llc_snap_ether_type llc_un.type_snap.ether_type
148
149 extern u_char at_org_code[3];
150 extern u_char aarp_org_code[3];
151 #endif /* NETATALK */
152
153 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
154 #define senderr(e) { error = (e); goto bad;}
155
156 #define SIN(x) ((struct sockaddr_in *)x)
157
158 static int ether_output __P((struct ifnet *, struct mbuf *,
159 struct sockaddr *, struct rtentry *));
160 static void ether_input __P((struct ifnet *, struct mbuf *));
161
162 /*
163 * Ethernet output routine.
164 * Encapsulate a packet of type family for the local net.
165 * Assumes that ifp is actually pointer to ethercom structure.
166 */
167 static int
168 ether_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
169 struct rtentry *rt0)
170 {
171 u_int16_t etype = 0;
172 int s, error = 0, hdrcmplt = 0;
173 u_char esrc[6], edst[6];
174 struct mbuf *m = m0;
175 struct rtentry *rt;
176 struct mbuf *mcopy = (struct mbuf *)0;
177 struct ether_header *eh;
178 #ifdef INET
179 struct arphdr *ah;
180 #endif /* INET */
181 #ifdef NETATALK
182 struct at_ifaddr *aa;
183 #endif /* NETATALK */
184
185 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
186 senderr(ENETDOWN);
187 ifp->if_lastchange = time;
188 if ((rt = rt0) != NULL) {
189 if ((rt->rt_flags & RTF_UP) == 0) {
190 if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
191 rt->rt_refcnt--;
192 if (rt->rt_ifp != ifp)
193 return (*rt->rt_ifp->if_output)
194 (ifp, m0, dst, rt);
195 } else
196 senderr(EHOSTUNREACH);
197 }
198 if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
199 if (rt->rt_gwroute == 0)
200 goto lookup;
201 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
202 rtfree(rt); rt = rt0;
203 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
204 if ((rt = rt->rt_gwroute) == 0)
205 senderr(EHOSTUNREACH);
206 /* the "G" test below also prevents rt == rt0 */
207 if ((rt->rt_flags & RTF_GATEWAY) ||
208 (rt->rt_ifp != ifp)) {
209 rt->rt_refcnt--;
210 rt0->rt_gwroute = 0;
211 senderr(EHOSTUNREACH);
212 }
213 }
214 }
215 if (rt->rt_flags & RTF_REJECT)
216 if (rt->rt_rmx.rmx_expire == 0 ||
217 time.tv_sec < rt->rt_rmx.rmx_expire)
218 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
219 }
220 switch (dst->sa_family) {
221
222 #ifdef INET
223 case AF_INET:
224 if (m->m_flags & M_BCAST)
225 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
226 sizeof(edst));
227
228 else if (m->m_flags & M_MCAST) {
229 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr,
230 (caddr_t)edst)
231
232 } else if (!arpresolve(ifp, rt, m, dst, edst))
233 return (0); /* if not yet resolved */
234 /* If broadcasting on a simplex interface, loopback a copy */
235 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
236 mcopy = m_copy(m, 0, (int)M_COPYALL);
237 etype = htons(ETHERTYPE_IP);
238 break;
239
240 case AF_ARP:
241 ah = mtod(m, struct arphdr *);
242 if (m->m_flags & M_BCAST)
243 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
244 sizeof(edst));
245 else
246 bcopy((caddr_t)ar_tha(ah),
247 (caddr_t)edst, sizeof(edst));
248
249 ah->ar_hrd = htons(ARPHRD_ETHER);
250
251 switch(ntohs(ah->ar_op)) {
252 case ARPOP_REVREQUEST:
253 case ARPOP_REVREPLY:
254 etype = htons(ETHERTYPE_REVARP);
255 break;
256
257 case ARPOP_REQUEST:
258 case ARPOP_REPLY:
259 default:
260 etype = htons(ETHERTYPE_ARP);
261 }
262
263 break;
264 #endif
265 #ifdef INET6
266 case AF_INET6:
267 #ifdef OLDIP6OUTPUT
268 if (!nd6_resolve(ifp, rt, m, dst, (u_char *)edst))
269 return(0); /* if not yet resolves */
270 #else
271 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){
272 /* something bad happened */
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 (m->m_flags & M_MCAST)
478 ifp->if_omcasts++;
479 IF_ENQUEUE(&ifp->if_snd, m);
480 if ((ifp->if_flags & IFF_OACTIVE) == 0)
481 (*ifp->if_start)(ifp);
482 splx(s);
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(struct ifnet *ifp, struct mbuf *m)
498 {
499 struct ifqueue *inq;
500 u_int16_t etype;
501 int s;
502 struct ether_header *eh;
503 #if defined (ISO) || defined (LLC) || defined(NETATALK)
504 struct llc *l;
505 #endif
506
507 if ((ifp->if_flags & IFF_UP) == 0) {
508 m_freem(m);
509 return;
510 }
511
512 eh = mtod(m, struct ether_header *);
513 etype = ntohs(eh->ether_type);
514
515 /*
516 * Determine if the packet is within its size limits.
517 */
518 if (m->m_pkthdr.len > ETHER_MAX_FRAME(etype, m->m_flags & M_HASFCS)) {
519 printf("%s: discarding oversize frame (len=%d)\n",
520 ifp->if_xname, m->m_pkthdr.len);
521 m_freem(m);
522 return;
523 }
524
525 ifp->if_lastchange = time;
526 ifp->if_ibytes += m->m_pkthdr.len;
527 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
528 if (memcmp(etherbroadcastaddr,
529 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
530 m->m_flags |= M_BCAST;
531 else
532 m->m_flags |= M_MCAST;
533 ifp->if_imcasts++;
534 } else if ((ifp->if_flags & IFF_PROMISC) != 0 &&
535 memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
536 ETHER_ADDR_LEN) != 0) {
537 m_freem(m);
538 return;
539 }
540
541 /*
542 * Handle protocols that expect to have the Ethernet header
543 * (and possibly FCS) intact.
544 */
545 switch (etype) {
546 #if NVLAN > 0
547 case ETHERTYPE_VLAN:
548 /*
549 * vlan_input() will either recursively call ether_input()
550 * or drop the packet.
551 */
552 if (((struct ethercom *)ifp)->ec_nvlans != 0)
553 vlan_input(ifp, m);
554 else
555 m_freem(m);
556 return;
557 #endif /* NVLAN > 0 */
558 default:
559 /* Nothing. */
560 }
561
562 /* Strip off the Ethernet header. */
563 m_adj(m, sizeof(struct ether_header));
564
565 /* If the CRC is still on the packet, trim it off. */
566 if (m->m_flags & M_HASFCS)
567 m_adj(m, -ETHER_CRC_LEN);
568
569 switch (etype) {
570 #ifdef INET
571 case ETHERTYPE_IP:
572 #ifdef GATEWAY
573 if (ipflow_fastforward(m))
574 return;
575 #endif
576 schednetisr(NETISR_IP);
577 inq = &ipintrq;
578 break;
579
580 case ETHERTYPE_ARP:
581 schednetisr(NETISR_ARP);
582 inq = &arpintrq;
583 break;
584
585 case ETHERTYPE_REVARP:
586 revarpinput(m); /* XXX queue? */
587 return;
588 #endif
589 #ifdef INET6
590 case ETHERTYPE_IPV6:
591 schednetisr(NETISR_IPV6);
592 inq = &ip6intrq;
593 break;
594 #endif
595 #ifdef NS
596 case ETHERTYPE_NS:
597 schednetisr(NETISR_NS);
598 inq = &nsintrq;
599 break;
600
601 #endif
602 #ifdef IPX
603 case ETHERTYPE_IPX:
604 schednetisr(NETISR_IPX);
605 inq = &ipxintrq;
606 break;
607 #endif
608 #ifdef NETATALK
609 case ETHERTYPE_ATALK:
610 schednetisr(NETISR_ATALK);
611 inq = &atintrq1;
612 break;
613 case ETHERTYPE_AARP:
614 /* probably this should be done with a NETISR as well */
615 aarpinput(ifp, m); /* XXX */
616 return;
617 #endif /* NETATALK */
618 default:
619 #if defined (ISO) || defined (LLC) || defined (NETATALK)
620 if (etype > ETHERMTU)
621 goto dropanyway;
622 l = mtod(m, struct llc *);
623 switch (l->llc_dsap) {
624 #ifdef NETATALK
625 case LLC_SNAP_LSAP:
626 switch (l->llc_control) {
627 case LLC_UI:
628 if (l->llc_ssap != LLC_SNAP_LSAP) {
629 goto dropanyway;
630 }
631
632 if (Bcmp(&(l->llc_snap_org_code)[0],
633 at_org_code, sizeof(at_org_code)) == 0 &&
634 ntohs(l->llc_snap_ether_type) ==
635 ETHERTYPE_ATALK) {
636 inq = &atintrq2;
637 m_adj(m, sizeof(struct llc));
638 schednetisr(NETISR_ATALK);
639 break;
640 }
641
642 if (Bcmp(&(l->llc_snap_org_code)[0],
643 aarp_org_code,
644 sizeof(aarp_org_code)) == 0 &&
645 ntohs(l->llc_snap_ether_type) ==
646 ETHERTYPE_AARP) {
647 m_adj( m, sizeof(struct llc));
648 aarpinput(ifp, m); /* XXX */
649 return;
650 }
651
652 default:
653 goto dropanyway;
654 }
655 break;
656 #endif /* NETATALK */
657 #ifdef ISO
658 case LLC_ISO_LSAP:
659 switch (l->llc_control) {
660 case LLC_UI:
661 /* LLC_UI_P forbidden in class 1 service */
662 if ((l->llc_dsap == LLC_ISO_LSAP) &&
663 (l->llc_ssap == LLC_ISO_LSAP)) {
664 /* LSAP for ISO */
665 if (m->m_pkthdr.len > etype)
666 m_adj(m, etype - m->m_pkthdr.len);
667 m->m_data += 3; /* XXX */
668 m->m_len -= 3; /* XXX */
669 m->m_pkthdr.len -= 3; /* XXX */
670 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
671 if (m == 0)
672 return;
673 *mtod(m, struct ether_header *) = *eh;
674 #ifdef ARGO_DEBUG
675 if (argo_debug[D_ETHER])
676 printf("clnp packet");
677 #endif
678 schednetisr(NETISR_ISO);
679 inq = &clnlintrq;
680 break;
681 }
682 goto dropanyway;
683
684 case LLC_XID:
685 case LLC_XID_P:
686 if(m->m_len < 6)
687 goto dropanyway;
688 l->llc_window = 0;
689 l->llc_fid = 9;
690 l->llc_class = 1;
691 l->llc_dsap = l->llc_ssap = 0;
692 /* Fall through to */
693 case LLC_TEST:
694 case LLC_TEST_P:
695 {
696 struct sockaddr sa;
697 struct ether_header *eh2;
698 int i;
699 u_char c = l->llc_dsap;
700
701 l->llc_dsap = l->llc_ssap;
702 l->llc_ssap = c;
703 if (m->m_flags & (M_BCAST | M_MCAST))
704 bcopy(LLADDR(ifp->if_sadl),
705 (caddr_t)eh->ether_dhost, 6);
706 sa.sa_family = AF_UNSPEC;
707 sa.sa_len = sizeof(sa);
708 eh2 = (struct ether_header *)sa.sa_data;
709 for (i = 0; i < 6; i++) {
710 eh2->ether_shost[i] = c =
711 eh->ether_dhost[i];
712 eh2->ether_dhost[i] =
713 eh->ether_dhost[i] =
714 eh->ether_shost[i];
715 eh->ether_shost[i] = c;
716 }
717 ifp->if_output(ifp, m, &sa, NULL);
718 return;
719 }
720 default:
721 m_freem(m);
722 return;
723 }
724 break;
725 #endif /* ISO */
726 #ifdef LLC
727 case LLC_X25_LSAP:
728 {
729 if (m->m_pkthdr.len > etype)
730 m_adj(m, etype - m->m_pkthdr.len);
731 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
732 if (m == 0)
733 return;
734 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
735 eh->ether_dhost, LLC_X25_LSAP, 6,
736 mtod(m, struct sdl_hdr *)))
737 panic("ETHER cons addr failure");
738 mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
739 #ifdef LLC_DEBUG
740 printf("llc packet\n");
741 #endif /* LLC_DEBUG */
742 schednetisr(NETISR_CCITT);
743 inq = &llcintrq;
744 break;
745 }
746 #endif /* LLC */
747 dropanyway:
748 default:
749 m_freem(m);
750 return;
751 }
752 #else /* ISO || LLC || NETATALK*/
753 m_freem(m);
754 return;
755 #endif /* ISO || LLC || NETATALK*/
756 }
757
758 s = splimp();
759 if (IF_QFULL(inq)) {
760 IF_DROP(inq);
761 m_freem(m);
762 } else
763 IF_ENQUEUE(inq, m);
764 splx(s);
765 }
766
767 /*
768 * Convert Ethernet address to printable (loggable) representation.
769 */
770 static char digits[] = "0123456789abcdef";
771 char *
772 ether_sprintf(const u_char *ap)
773 {
774 static char etherbuf[18];
775 char *cp = etherbuf;
776 int i;
777
778 for (i = 0; i < 6; i++) {
779 *cp++ = digits[*ap >> 4];
780 *cp++ = digits[*ap++ & 0xf];
781 *cp++ = ':';
782 }
783 *--cp = 0;
784 return (etherbuf);
785 }
786
787 /*
788 * Perform common duties while attaching to interface list
789 */
790 void
791 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
792 {
793 struct sockaddr_dl *sdl;
794
795 ifp->if_type = IFT_ETHER;
796 ifp->if_addrlen = 6;
797 ifp->if_hdrlen = 14;
798 ifp->if_mtu = ETHERMTU;
799 ifp->if_output = ether_output;
800 ifp->if_input = ether_input;
801 if (ifp->if_baudrate == 0)
802 ifp->if_baudrate = IF_Mbps(10); /* just a default */
803 if ((sdl = ifp->if_sadl) &&
804 sdl->sdl_family == AF_LINK) {
805 sdl->sdl_type = IFT_ETHER;
806 sdl->sdl_alen = ifp->if_addrlen;
807 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
808 }
809 LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
810 ifp->if_broadcastaddr = etherbroadcastaddr;
811 }
812
813 void
814 ether_ifdetach(struct ifnet *ifp)
815 {
816 struct ethercom *ec = (void *) ifp;
817 struct sockaddr_dl *sdl = ifp->if_sadl;
818 struct ether_multi *enm;
819 int s;
820
821 #if NVLAN > 0
822 if (ec->ec_nvlans)
823 vlan_ifdetach(ifp);
824 #endif
825
826 s = splimp();
827 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
828 LIST_REMOVE(enm, enm_list);
829 free(enm, M_IFADDR);
830 ec->ec_multicnt--;
831 }
832 splx(s);
833
834 memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
835 sdl->sdl_alen = 0;
836 sdl->sdl_type = 0;
837 }
838
839 #if 0
840 /*
841 * This is for reference. We have a table-driven version
842 * of the little-endian crc32 generator, which is faster
843 * than the double-loop.
844 */
845 u_int32_t
846 ether_crc32_le(const u_int8_t *buf, size_t len)
847 {
848 u_int32_t c, crc, carry;
849 size_t i, j;
850
851 crc = 0xffffffffU; /* initial value */
852
853 for (i = 0; i < len; i++) {
854 c = buf[i];
855 for (j = 0; j < 8; j++) {
856 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
857 crc >>= 1;
858 c >>= 1;
859 if (carry)
860 crc = (crc ^ ETHER_CRC_POLY_LE);
861 }
862 }
863
864 return (crc);
865 }
866 #else
867 u_int32_t
868 ether_crc32_le(const u_int8_t *buf, size_t len)
869 {
870 static const u_int32_t crctab[] = {
871 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
872 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
873 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
874 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
875 };
876 u_int32_t crc;
877 int i;
878
879 crc = 0xffffffffU; /* initial value */
880
881 for (i = 0; i < len; i++) {
882 crc ^= buf[i];
883 crc = (crc >> 4) ^ crctab[crc & 0xf];
884 crc = (crc >> 4) ^ crctab[crc & 0xf];
885 }
886
887 return (crc);
888 }
889 #endif
890
891 u_int32_t
892 ether_crc32_be(const u_int8_t *buf, size_t len)
893 {
894 u_int32_t c, crc, carry;
895 size_t i, j;
896
897 crc = 0xffffffffU; /* initial value */
898
899 for (i = 0; i < len; i++) {
900 c = buf[i];
901 for (j = 0; j < 8; j++) {
902 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
903 crc <<= 1;
904 c >>= 1;
905 if (carry)
906 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
907 }
908 }
909
910 return (crc);
911 }
912
913 #ifdef INET
914 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
915 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
916 #endif
917 #ifdef INET6
918 u_char ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
919 u_char ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
920 #endif
921
922 /*
923 * Convert a sockaddr into an Ethernet address or range of Ethernet
924 * addresses.
925 */
926 int
927 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
928 u_int8_t addrhi[ETHER_ADDR_LEN])
929 {
930 #ifdef INET
931 struct sockaddr_in *sin;
932 #endif /* INET */
933 #ifdef INET6
934 struct sockaddr_in6 *sin6;
935 #endif /* INET6 */
936
937 switch (sa->sa_family) {
938
939 case AF_UNSPEC:
940 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
941 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
942 break;
943
944 #ifdef INET
945 case AF_INET:
946 sin = satosin(sa);
947 if (sin->sin_addr.s_addr == INADDR_ANY) {
948 /*
949 * An IP address of INADDR_ANY means listen to
950 * or stop listening to all of the Ethernet
951 * multicast addresses used for IP.
952 * (This is for the sake of IP multicast routers.)
953 */
954 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
955 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
956 }
957 else {
958 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
959 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
960 }
961 break;
962 #endif
963 #ifdef INET6
964 case AF_INET6:
965 sin6 = satosin6(sa);
966 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
967 /*
968 * An IP6 address of 0 means listen to or stop
969 * listening to all of the Ethernet multicast
970 * address used for IP6.
971 * (This is used for multicast routers.)
972 */
973 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
974 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
975 } else {
976 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
977 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
978 }
979 break;
980 #endif
981
982 default:
983 return (EAFNOSUPPORT);
984 }
985 return (0);
986 }
987
988 /*
989 * Add an Ethernet multicast address or range of addresses to the list for a
990 * given interface.
991 */
992 int
993 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
994 {
995 struct ether_multi *enm;
996 u_char addrlo[ETHER_ADDR_LEN];
997 u_char addrhi[ETHER_ADDR_LEN];
998 int s = splimp(), error;
999
1000 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1001 if (error != 0) {
1002 splx(s);
1003 return (error);
1004 }
1005
1006 /*
1007 * Verify that we have valid Ethernet multicast addresses.
1008 */
1009 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1010 splx(s);
1011 return (EINVAL);
1012 }
1013 /*
1014 * See if the address range is already in the list.
1015 */
1016 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1017 if (enm != NULL) {
1018 /*
1019 * Found it; just increment the reference count.
1020 */
1021 ++enm->enm_refcount;
1022 splx(s);
1023 return (0);
1024 }
1025 /*
1026 * New address or range; malloc a new multicast record
1027 * and link it into the interface's multicast list.
1028 */
1029 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1030 if (enm == NULL) {
1031 splx(s);
1032 return (ENOBUFS);
1033 }
1034 bcopy(addrlo, enm->enm_addrlo, 6);
1035 bcopy(addrhi, enm->enm_addrhi, 6);
1036 enm->enm_ec = ec;
1037 enm->enm_refcount = 1;
1038 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1039 ec->ec_multicnt++;
1040 splx(s);
1041 /*
1042 * Return ENETRESET to inform the driver that the list has changed
1043 * and its reception filter should be adjusted accordingly.
1044 */
1045 return (ENETRESET);
1046 }
1047
1048 /*
1049 * Delete a multicast address record.
1050 */
1051 int
1052 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1053 {
1054 struct ether_multi *enm;
1055 u_char addrlo[ETHER_ADDR_LEN];
1056 u_char addrhi[ETHER_ADDR_LEN];
1057 int s = splimp(), error;
1058
1059 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1060 if (error != 0) {
1061 splx(s);
1062 return (error);
1063 }
1064
1065 /*
1066 * Look ur the address in our list.
1067 */
1068 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1069 if (enm == NULL) {
1070 splx(s);
1071 return (ENXIO);
1072 }
1073 if (--enm->enm_refcount != 0) {
1074 /*
1075 * Still some claims to this record.
1076 */
1077 splx(s);
1078 return (0);
1079 }
1080 /*
1081 * No remaining claims to this record; unlink and free it.
1082 */
1083 LIST_REMOVE(enm, enm_list);
1084 free(enm, M_IFMADDR);
1085 ec->ec_multicnt--;
1086 splx(s);
1087 /*
1088 * Return ENETRESET to inform the driver that the list has changed
1089 * and its reception filter should be adjusted accordingly.
1090 */
1091 return (ENETRESET);
1092 }
1093
1094 /*
1095 * Common ioctls for Ethernet interfaces. Note, we must be
1096 * called at splnet().
1097 */
1098 int
1099 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1100 {
1101 struct ethercom *ec = (void *) ifp;
1102 struct ifreq *ifr = (struct ifreq *)data;
1103 struct ifaddr *ifa = (struct ifaddr *)data;
1104 int error = 0;
1105
1106 switch (cmd) {
1107 case SIOCSIFADDR:
1108 ifp->if_flags |= IFF_UP;
1109 switch (ifa->ifa_addr->sa_family) {
1110 #ifdef INET
1111 case AF_INET:
1112 if ((error = (*ifp->if_init)(ifp)) != 0)
1113 break;
1114 arp_ifinit(ifp, ifa);
1115 break;
1116 #endif /* INET */
1117 #ifdef NS
1118 case AF_NS:
1119 {
1120 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1121
1122 if (ns_nullhost(*ina))
1123 ina->x_host = *(union ns_host *)
1124 LLADDR(ifp->if_sadl);
1125 else
1126 memcpy(LLADDR(ifp->if_sadl),
1127 ina->x_host.c_host, ifp->if_addrlen);
1128 /* Set new address. */
1129 error = (*ifp->if_init)(ifp);
1130 break;
1131 }
1132 #endif /* NS */
1133 default:
1134 error = (*ifp->if_init)(ifp);
1135 break;
1136 }
1137 break;
1138
1139 case SIOCGIFADDR:
1140 memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1141 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1142 break;
1143
1144 case SIOCSIFMTU:
1145 if (ifr->ifr_mtu > ETHERMTU)
1146 error = EINVAL;
1147 else
1148 ifp->if_mtu = ifr->ifr_mtu;
1149 break;
1150
1151 case SIOCSIFFLAGS:
1152 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1153 /*
1154 * If interface is marked down and it is running,
1155 * then stop and disable it.
1156 */
1157 (*ifp->if_stop)(ifp, 1);
1158 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1159 /*
1160 * If interface is marked up and it is stopped, then
1161 * start it.
1162 */
1163 error = (*ifp->if_init)(ifp);
1164 } else if ((ifp->if_flags & IFF_UP) != 0) {
1165 /*
1166 * Reset the interface to pick up changes in any other
1167 * flags that affect the hardware state.
1168 */
1169 error = (*ifp->if_init)(ifp);
1170 }
1171 break;
1172
1173 case SIOCADDMULTI:
1174 case SIOCDELMULTI:
1175 error = (cmd == SIOCADDMULTI) ?
1176 ether_addmulti(ifr, ec) :
1177 ether_delmulti(ifr, ec);
1178 break;
1179
1180 default:
1181 error = ENOTTY;
1182 }
1183
1184 return (error);
1185 }
1186