if_ethersubr.c revision 1.75 1 /* $NetBSD: if_ethersubr.c,v 1.75 2001/01/17 00:30:51 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, len, 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 ALTQ_DECL(struct altq_pktattr pktattr;)
184 #ifdef INET
185 struct arphdr *ah;
186 #endif /* INET */
187 #ifdef NETATALK
188 struct at_ifaddr *aa;
189 #endif /* NETATALK */
190 short mflags;
191
192 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
193 senderr(ENETDOWN);
194 ifp->if_lastchange = time;
195 if ((rt = rt0) != NULL) {
196 if ((rt->rt_flags & RTF_UP) == 0) {
197 if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
198 rt->rt_refcnt--;
199 if (rt->rt_ifp != ifp)
200 return (*rt->rt_ifp->if_output)
201 (ifp, m0, dst, rt);
202 } else
203 senderr(EHOSTUNREACH);
204 }
205 if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
206 if (rt->rt_gwroute == 0)
207 goto lookup;
208 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
209 rtfree(rt); rt = rt0;
210 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
211 if ((rt = rt->rt_gwroute) == 0)
212 senderr(EHOSTUNREACH);
213 /* the "G" test below also prevents rt == rt0 */
214 if ((rt->rt_flags & RTF_GATEWAY) ||
215 (rt->rt_ifp != ifp)) {
216 rt->rt_refcnt--;
217 rt0->rt_gwroute = 0;
218 senderr(EHOSTUNREACH);
219 }
220 }
221 }
222 if (rt->rt_flags & RTF_REJECT)
223 if (rt->rt_rmx.rmx_expire == 0 ||
224 time.tv_sec < rt->rt_rmx.rmx_expire)
225 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
226 }
227
228 /*
229 * If the queueing discipline needs packet classification,
230 * do it before prepending link headers.
231 */
232 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
233
234 switch (dst->sa_family) {
235
236 #ifdef INET
237 case AF_INET:
238 if (m->m_flags & M_BCAST)
239 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
240 sizeof(edst));
241
242 else if (m->m_flags & M_MCAST) {
243 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr,
244 (caddr_t)edst)
245
246 } else if (!arpresolve(ifp, rt, m, dst, edst))
247 return (0); /* if not yet resolved */
248 /* If broadcasting on a simplex interface, loopback a copy */
249 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
250 mcopy = m_copy(m, 0, (int)M_COPYALL);
251 etype = htons(ETHERTYPE_IP);
252 break;
253
254 case AF_ARP:
255 ah = mtod(m, struct arphdr *);
256 if (m->m_flags & M_BCAST)
257 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
258 sizeof(edst));
259 else
260 bcopy((caddr_t)ar_tha(ah),
261 (caddr_t)edst, sizeof(edst));
262
263 ah->ar_hrd = htons(ARPHRD_ETHER);
264
265 switch(ntohs(ah->ar_op)) {
266 case ARPOP_REVREQUEST:
267 case ARPOP_REVREPLY:
268 etype = htons(ETHERTYPE_REVARP);
269 break;
270
271 case ARPOP_REQUEST:
272 case ARPOP_REPLY:
273 default:
274 etype = htons(ETHERTYPE_ARP);
275 }
276
277 break;
278 #endif
279 #ifdef INET6
280 case AF_INET6:
281 #ifdef OLDIP6OUTPUT
282 if (!nd6_resolve(ifp, rt, m, dst, (u_char *)edst))
283 return(0); /* if not yet resolves */
284 #else
285 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){
286 /* something bad happened */
287 return(0);
288 }
289 #endif /* OLDIP6OUTPUT */
290 etype = htons(ETHERTYPE_IPV6);
291 break;
292 #endif
293 #ifdef NETATALK
294 case AF_APPLETALK:
295 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
296 #ifdef NETATALKDEBUG
297 printf("aarpresolv failed\n");
298 #endif /* NETATALKDEBUG */
299 return (0);
300 }
301 /*
302 * ifaddr is the first thing in at_ifaddr
303 */
304 aa = (struct at_ifaddr *) at_ifawithnet(
305 (struct sockaddr_at *)dst, ifp);
306 if (aa == NULL)
307 goto bad;
308
309 /*
310 * In the phase 2 case, we need to prepend an mbuf for the
311 * llc header. Since we must preserve the value of m,
312 * which is passed to us by value, we m_copy() the first
313 * mbuf, and use it for our llc header.
314 */
315 if (aa->aa_flags & AFA_PHASE2) {
316 struct llc llc;
317
318 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
319 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
320 llc.llc_control = LLC_UI;
321 bcopy(at_org_code, llc.llc_snap_org_code,
322 sizeof(llc.llc_snap_org_code));
323 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
324 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
325 } else {
326 etype = htons(ETHERTYPE_ATALK);
327 }
328 break;
329 #endif /* NETATALK */
330 #ifdef NS
331 case AF_NS:
332 etype = htons(ETHERTYPE_NS);
333 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
334 (caddr_t)edst, sizeof (edst));
335 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
336 return (looutput(ifp, m, dst, rt));
337 /* If broadcasting on a simplex interface, loopback a copy */
338 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
339 mcopy = m_copy(m, 0, (int)M_COPYALL);
340 break;
341 #endif
342 #ifdef IPX
343 case AF_IPX:
344 etype = htons(ETHERTYPE_IPX);
345 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
346 (caddr_t)edst, sizeof (edst));
347 /* If broadcasting on a simplex interface, loopback a copy */
348 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
349 mcopy = m_copy(m, 0, (int)M_COPYALL);
350 break;
351 #endif
352 #ifdef ISO
353 case AF_ISO: {
354 int snpalen;
355 struct llc *l;
356 struct sockaddr_dl *sdl;
357
358 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
359 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
360 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
361 } else {
362 error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
363 (char *)edst, &snpalen);
364 if (error)
365 goto bad; /* Not Resolved */
366 }
367 /* If broadcasting on a simplex interface, loopback a copy */
368 if (*edst & 1)
369 m->m_flags |= (M_BCAST|M_MCAST);
370 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
371 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
372 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
373 if (mcopy) {
374 eh = mtod(mcopy, struct ether_header *);
375 bcopy((caddr_t)edst,
376 (caddr_t)eh->ether_dhost, sizeof (edst));
377 bcopy(LLADDR(ifp->if_sadl),
378 (caddr_t)eh->ether_shost, sizeof (edst));
379 }
380 }
381 M_PREPEND(m, 3, M_DONTWAIT);
382 if (m == NULL)
383 return (0);
384 l = mtod(m, struct llc *);
385 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
386 l->llc_control = LLC_UI;
387 #ifdef ARGO_DEBUG
388 if (argo_debug[D_ETHER]) {
389 int i;
390 printf("unoutput: sending pkt to: ");
391 for (i=0; i<6; i++)
392 printf("%x ", edst[i] & 0xff);
393 printf("\n");
394 }
395 #endif
396 } break;
397 #endif /* ISO */
398 #ifdef LLC
399 /* case AF_NSAP: */
400 case AF_CCITT: {
401 struct sockaddr_dl *sdl =
402 (struct sockaddr_dl *) rt -> rt_gateway;
403
404 if (sdl && sdl->sdl_family == AF_LINK
405 && sdl->sdl_alen > 0) {
406 bcopy(LLADDR(sdl), (char *)edst,
407 sizeof(edst));
408 } else goto bad; /* Not a link interface ? Funny ... */
409 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
410 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
411 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
412 if (mcopy) {
413 eh = mtod(mcopy, struct ether_header *);
414 bcopy((caddr_t)edst,
415 (caddr_t)eh->ether_dhost, sizeof (edst));
416 bcopy(LLADDR(ifp->if_sadl),
417 (caddr_t)eh->ether_shost, sizeof (edst));
418 }
419 }
420 #ifdef LLC_DEBUG
421 {
422 int i;
423 struct llc *l = mtod(m, struct llc *);
424
425 printf("ether_output: sending LLC2 pkt to: ");
426 for (i=0; i<6; i++)
427 printf("%x ", edst[i] & 0xff);
428 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
429 m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
430 l->llc_control & 0xff);
431
432 }
433 #endif /* LLC_DEBUG */
434 } break;
435 #endif /* LLC */
436
437 case pseudo_AF_HDRCMPLT:
438 hdrcmplt = 1;
439 eh = (struct ether_header *)dst->sa_data;
440 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc));
441 /* FALLTHROUGH */
442
443 case AF_UNSPEC:
444 eh = (struct ether_header *)dst->sa_data;
445 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
446 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
447 etype = eh->ether_type;
448 break;
449
450 default:
451 printf("%s: can't handle af%d\n", ifp->if_xname,
452 dst->sa_family);
453 senderr(EAFNOSUPPORT);
454 }
455
456 if (mcopy)
457 (void) looutput(ifp, mcopy, dst, rt);
458
459 /* If no ether type is set, this must be a 802.2 formatted packet.
460 */
461 if (etype == 0)
462 etype = htons(m->m_pkthdr.len);
463 /*
464 * Add local net header. If no space in first mbuf,
465 * allocate another.
466 */
467 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
468 if (m == 0)
469 senderr(ENOBUFS);
470 eh = mtod(m, struct ether_header *);
471 bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
472 sizeof(eh->ether_type));
473 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
474 if (hdrcmplt)
475 bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost,
476 sizeof(eh->ether_shost));
477 else
478 bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost,
479 sizeof(eh->ether_shost));
480 mflags = m->m_flags;
481 len = m->m_pkthdr.len;
482 s = splimp();
483 /*
484 * Queue message on interface, and start output if interface
485 * not yet active.
486 */
487 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
488 if (error) {
489 /* mbuf is already freed */
490 splx(s);
491 return (error);
492 }
493 ifp->if_obytes += len;
494 if (mflags & M_MCAST)
495 ifp->if_omcasts++;
496 if ((ifp->if_flags & IFF_OACTIVE) == 0)
497 (*ifp->if_start)(ifp);
498 splx(s);
499 return (error);
500
501 bad:
502 if (m)
503 m_freem(m);
504 return (error);
505 }
506
507 /*
508 * Process a received Ethernet packet;
509 * the packet is in the mbuf chain m with
510 * the ether header.
511 */
512 static void
513 ether_input(struct ifnet *ifp, struct mbuf *m)
514 {
515 struct ifqueue *inq;
516 u_int16_t etype;
517 int s;
518 struct ether_header *eh;
519 struct mbuf *n;
520 #if defined (ISO) || defined (LLC) || defined(NETATALK)
521 struct llc *l;
522 #endif
523
524 if ((ifp->if_flags & IFF_UP) == 0) {
525 m_freem(m);
526 return;
527 }
528
529 eh = mtod(m, struct ether_header *);
530 etype = ntohs(eh->ether_type);
531
532 /*
533 * Determine if the packet is within its size limits.
534 */
535 if (m->m_pkthdr.len > ETHER_MAX_FRAME(etype, m->m_flags & M_HASFCS)) {
536 printf("%s: discarding oversize frame (len=%d)\n",
537 ifp->if_xname, m->m_pkthdr.len);
538 m_freem(m);
539 return;
540 }
541
542 ifp->if_lastchange = time;
543 ifp->if_ibytes += m->m_pkthdr.len;
544 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
545 if (memcmp(etherbroadcastaddr,
546 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
547 m->m_flags |= M_BCAST;
548 else
549 m->m_flags |= M_MCAST;
550 ifp->if_imcasts++;
551 } else if ((ifp->if_flags & IFF_PROMISC) != 0 &&
552 memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
553 ETHER_ADDR_LEN) != 0) {
554 m_freem(m);
555 return;
556 }
557
558 /* Check if the mbuf has a VLAN tag */
559 n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN);
560 if (n) {
561 #if NVLAN > 0
562 /*
563 * vlan_input() will either recursively call ether_input()
564 * or drop the packet.
565 */
566 if (((struct ethercom *)ifp)->ec_nvlans != 0)
567 vlan_input(ifp, m);
568 else
569 #endif
570 m_freem(m);
571 return;
572 }
573
574 /*
575 * Handle protocols that expect to have the Ethernet header
576 * (and possibly FCS) intact.
577 */
578 switch (etype) {
579 #if NVLAN > 0
580 case ETHERTYPE_VLAN:
581 /*
582 * vlan_input() will either recursively call ether_input()
583 * or drop the packet.
584 */
585 if (((struct ethercom *)ifp)->ec_nvlans != 0)
586 vlan_input(ifp, m);
587 else
588 m_freem(m);
589 return;
590 #endif /* NVLAN > 0 */
591 default:
592 /* Nothing. */
593 }
594
595 /* Strip off the Ethernet header. */
596 m_adj(m, sizeof(struct ether_header));
597
598 /* If the CRC is still on the packet, trim it off. */
599 if (m->m_flags & M_HASFCS)
600 m_adj(m, -ETHER_CRC_LEN);
601
602 switch (etype) {
603 #ifdef INET
604 case ETHERTYPE_IP:
605 #ifdef GATEWAY
606 if (ipflow_fastforward(m))
607 return;
608 #endif
609 schednetisr(NETISR_IP);
610 inq = &ipintrq;
611 break;
612
613 case ETHERTYPE_ARP:
614 schednetisr(NETISR_ARP);
615 inq = &arpintrq;
616 break;
617
618 case ETHERTYPE_REVARP:
619 revarpinput(m); /* XXX queue? */
620 return;
621 #endif
622 #ifdef INET6
623 case ETHERTYPE_IPV6:
624 schednetisr(NETISR_IPV6);
625 inq = &ip6intrq;
626 break;
627 #endif
628 #ifdef NS
629 case ETHERTYPE_NS:
630 schednetisr(NETISR_NS);
631 inq = &nsintrq;
632 break;
633
634 #endif
635 #ifdef IPX
636 case ETHERTYPE_IPX:
637 schednetisr(NETISR_IPX);
638 inq = &ipxintrq;
639 break;
640 #endif
641 #ifdef NETATALK
642 case ETHERTYPE_ATALK:
643 schednetisr(NETISR_ATALK);
644 inq = &atintrq1;
645 break;
646 case ETHERTYPE_AARP:
647 /* probably this should be done with a NETISR as well */
648 aarpinput(ifp, m); /* XXX */
649 return;
650 #endif /* NETATALK */
651 default:
652 #if defined (ISO) || defined (LLC) || defined (NETATALK)
653 if (etype > ETHERMTU)
654 goto dropanyway;
655 l = mtod(m, struct llc *);
656 switch (l->llc_dsap) {
657 #ifdef NETATALK
658 case LLC_SNAP_LSAP:
659 switch (l->llc_control) {
660 case LLC_UI:
661 if (l->llc_ssap != LLC_SNAP_LSAP) {
662 goto dropanyway;
663 }
664
665 if (Bcmp(&(l->llc_snap_org_code)[0],
666 at_org_code, sizeof(at_org_code)) == 0 &&
667 ntohs(l->llc_snap_ether_type) ==
668 ETHERTYPE_ATALK) {
669 inq = &atintrq2;
670 m_adj(m, sizeof(struct llc));
671 schednetisr(NETISR_ATALK);
672 break;
673 }
674
675 if (Bcmp(&(l->llc_snap_org_code)[0],
676 aarp_org_code,
677 sizeof(aarp_org_code)) == 0 &&
678 ntohs(l->llc_snap_ether_type) ==
679 ETHERTYPE_AARP) {
680 m_adj( m, sizeof(struct llc));
681 aarpinput(ifp, m); /* XXX */
682 return;
683 }
684
685 default:
686 goto dropanyway;
687 }
688 break;
689 #endif /* NETATALK */
690 #ifdef ISO
691 case LLC_ISO_LSAP:
692 switch (l->llc_control) {
693 case LLC_UI:
694 /* LLC_UI_P forbidden in class 1 service */
695 if ((l->llc_dsap == LLC_ISO_LSAP) &&
696 (l->llc_ssap == LLC_ISO_LSAP)) {
697 /* LSAP for ISO */
698 if (m->m_pkthdr.len > etype)
699 m_adj(m, etype - m->m_pkthdr.len);
700 m->m_data += 3; /* XXX */
701 m->m_len -= 3; /* XXX */
702 m->m_pkthdr.len -= 3; /* XXX */
703 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
704 if (m == 0)
705 return;
706 *mtod(m, struct ether_header *) = *eh;
707 #ifdef ARGO_DEBUG
708 if (argo_debug[D_ETHER])
709 printf("clnp packet");
710 #endif
711 schednetisr(NETISR_ISO);
712 inq = &clnlintrq;
713 break;
714 }
715 goto dropanyway;
716
717 case LLC_XID:
718 case LLC_XID_P:
719 if(m->m_len < 6)
720 goto dropanyway;
721 l->llc_window = 0;
722 l->llc_fid = 9;
723 l->llc_class = 1;
724 l->llc_dsap = l->llc_ssap = 0;
725 /* Fall through to */
726 case LLC_TEST:
727 case LLC_TEST_P:
728 {
729 struct sockaddr sa;
730 struct ether_header *eh2;
731 int i;
732 u_char c = l->llc_dsap;
733
734 l->llc_dsap = l->llc_ssap;
735 l->llc_ssap = c;
736 if (m->m_flags & (M_BCAST | M_MCAST))
737 bcopy(LLADDR(ifp->if_sadl),
738 (caddr_t)eh->ether_dhost, 6);
739 sa.sa_family = AF_UNSPEC;
740 sa.sa_len = sizeof(sa);
741 eh2 = (struct ether_header *)sa.sa_data;
742 for (i = 0; i < 6; i++) {
743 eh2->ether_shost[i] = c =
744 eh->ether_dhost[i];
745 eh2->ether_dhost[i] =
746 eh->ether_dhost[i] =
747 eh->ether_shost[i];
748 eh->ether_shost[i] = c;
749 }
750 ifp->if_output(ifp, m, &sa, NULL);
751 return;
752 }
753 default:
754 m_freem(m);
755 return;
756 }
757 break;
758 #endif /* ISO */
759 #ifdef LLC
760 case LLC_X25_LSAP:
761 {
762 if (m->m_pkthdr.len > etype)
763 m_adj(m, etype - m->m_pkthdr.len);
764 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
765 if (m == 0)
766 return;
767 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
768 eh->ether_dhost, LLC_X25_LSAP, 6,
769 mtod(m, struct sdl_hdr *)))
770 panic("ETHER cons addr failure");
771 mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
772 #ifdef LLC_DEBUG
773 printf("llc packet\n");
774 #endif /* LLC_DEBUG */
775 schednetisr(NETISR_CCITT);
776 inq = &llcintrq;
777 break;
778 }
779 #endif /* LLC */
780 dropanyway:
781 default:
782 m_freem(m);
783 return;
784 }
785 #else /* ISO || LLC || NETATALK*/
786 m_freem(m);
787 return;
788 #endif /* ISO || LLC || NETATALK*/
789 }
790
791 s = splimp();
792 if (IF_QFULL(inq)) {
793 IF_DROP(inq);
794 m_freem(m);
795 } else
796 IF_ENQUEUE(inq, m);
797 splx(s);
798 }
799
800 /*
801 * Convert Ethernet address to printable (loggable) representation.
802 */
803 static char digits[] = "0123456789abcdef";
804 char *
805 ether_sprintf(const u_char *ap)
806 {
807 static char etherbuf[18];
808 char *cp = etherbuf;
809 int i;
810
811 for (i = 0; i < 6; i++) {
812 *cp++ = digits[*ap >> 4];
813 *cp++ = digits[*ap++ & 0xf];
814 *cp++ = ':';
815 }
816 *--cp = 0;
817 return (etherbuf);
818 }
819
820 /*
821 * Perform common duties while attaching to interface list
822 */
823 void
824 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
825 {
826
827 ifp->if_type = IFT_ETHER;
828 ifp->if_addrlen = ETHER_ADDR_LEN;
829 ifp->if_hdrlen = 14;
830 ifp->if_dlt = DLT_EN10MB;
831 ifp->if_mtu = ETHERMTU;
832 ifp->if_output = ether_output;
833 ifp->if_input = ether_input;
834 if (ifp->if_baudrate == 0)
835 ifp->if_baudrate = IF_Mbps(10); /* just a default */
836
837 if_alloc_sadl(ifp);
838 memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen);
839
840 LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
841 ifp->if_broadcastaddr = etherbroadcastaddr;
842 #if NBPFILTER > 0
843 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
844 #endif
845 }
846
847 void
848 ether_ifdetach(struct ifnet *ifp)
849 {
850 struct ethercom *ec = (void *) ifp;
851 struct ether_multi *enm;
852 int s;
853
854 #if NBPFILTER > 0
855 bpfdetach(ifp);
856 #endif
857
858 #if NVLAN > 0
859 if (ec->ec_nvlans)
860 vlan_ifdetach(ifp);
861 #endif
862
863 s = splimp();
864 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
865 LIST_REMOVE(enm, enm_list);
866 free(enm, M_IFADDR);
867 ec->ec_multicnt--;
868 }
869 splx(s);
870
871 if_free_sadl(ifp);
872 }
873
874 #if 0
875 /*
876 * This is for reference. We have a table-driven version
877 * of the little-endian crc32 generator, which is faster
878 * than the double-loop.
879 */
880 u_int32_t
881 ether_crc32_le(const u_int8_t *buf, size_t len)
882 {
883 u_int32_t c, crc, carry;
884 size_t i, j;
885
886 crc = 0xffffffffU; /* initial value */
887
888 for (i = 0; i < len; i++) {
889 c = buf[i];
890 for (j = 0; j < 8; j++) {
891 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
892 crc >>= 1;
893 c >>= 1;
894 if (carry)
895 crc = (crc ^ ETHER_CRC_POLY_LE);
896 }
897 }
898
899 return (crc);
900 }
901 #else
902 u_int32_t
903 ether_crc32_le(const u_int8_t *buf, size_t len)
904 {
905 static const u_int32_t crctab[] = {
906 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
907 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
908 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
909 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
910 };
911 u_int32_t crc;
912 int i;
913
914 crc = 0xffffffffU; /* initial value */
915
916 for (i = 0; i < len; i++) {
917 crc ^= buf[i];
918 crc = (crc >> 4) ^ crctab[crc & 0xf];
919 crc = (crc >> 4) ^ crctab[crc & 0xf];
920 }
921
922 return (crc);
923 }
924 #endif
925
926 u_int32_t
927 ether_crc32_be(const u_int8_t *buf, size_t len)
928 {
929 u_int32_t c, crc, carry;
930 size_t i, j;
931
932 crc = 0xffffffffU; /* initial value */
933
934 for (i = 0; i < len; i++) {
935 c = buf[i];
936 for (j = 0; j < 8; j++) {
937 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
938 crc <<= 1;
939 c >>= 1;
940 if (carry)
941 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
942 }
943 }
944
945 return (crc);
946 }
947
948 #ifdef INET
949 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
950 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
951 #endif
952 #ifdef INET6
953 u_char ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
954 u_char ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
955 #endif
956
957 /*
958 * Convert a sockaddr into an Ethernet address or range of Ethernet
959 * addresses.
960 */
961 int
962 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
963 u_int8_t addrhi[ETHER_ADDR_LEN])
964 {
965 #ifdef INET
966 struct sockaddr_in *sin;
967 #endif /* INET */
968 #ifdef INET6
969 struct sockaddr_in6 *sin6;
970 #endif /* INET6 */
971
972 switch (sa->sa_family) {
973
974 case AF_UNSPEC:
975 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
976 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
977 break;
978
979 #ifdef INET
980 case AF_INET:
981 sin = satosin(sa);
982 if (sin->sin_addr.s_addr == INADDR_ANY) {
983 /*
984 * An IP address of INADDR_ANY means listen to
985 * or stop listening to all of the Ethernet
986 * multicast addresses used for IP.
987 * (This is for the sake of IP multicast routers.)
988 */
989 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
990 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
991 }
992 else {
993 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
994 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
995 }
996 break;
997 #endif
998 #ifdef INET6
999 case AF_INET6:
1000 sin6 = satosin6(sa);
1001 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1002 /*
1003 * An IP6 address of 0 means listen to or stop
1004 * listening to all of the Ethernet multicast
1005 * address used for IP6.
1006 * (This is used for multicast routers.)
1007 */
1008 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
1009 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
1010 } else {
1011 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1012 bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
1013 }
1014 break;
1015 #endif
1016
1017 default:
1018 return (EAFNOSUPPORT);
1019 }
1020 return (0);
1021 }
1022
1023 /*
1024 * Add an Ethernet multicast address or range of addresses to the list for a
1025 * given interface.
1026 */
1027 int
1028 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1029 {
1030 struct ether_multi *enm;
1031 u_char addrlo[ETHER_ADDR_LEN];
1032 u_char addrhi[ETHER_ADDR_LEN];
1033 int s = splimp(), error;
1034
1035 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1036 if (error != 0) {
1037 splx(s);
1038 return (error);
1039 }
1040
1041 /*
1042 * Verify that we have valid Ethernet multicast addresses.
1043 */
1044 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1045 splx(s);
1046 return (EINVAL);
1047 }
1048 /*
1049 * See if the address range is already in the list.
1050 */
1051 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1052 if (enm != NULL) {
1053 /*
1054 * Found it; just increment the reference count.
1055 */
1056 ++enm->enm_refcount;
1057 splx(s);
1058 return (0);
1059 }
1060 /*
1061 * New address or range; malloc a new multicast record
1062 * and link it into the interface's multicast list.
1063 */
1064 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1065 if (enm == NULL) {
1066 splx(s);
1067 return (ENOBUFS);
1068 }
1069 bcopy(addrlo, enm->enm_addrlo, 6);
1070 bcopy(addrhi, enm->enm_addrhi, 6);
1071 enm->enm_ec = ec;
1072 enm->enm_refcount = 1;
1073 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1074 ec->ec_multicnt++;
1075 splx(s);
1076 /*
1077 * Return ENETRESET to inform the driver that the list has changed
1078 * and its reception filter should be adjusted accordingly.
1079 */
1080 return (ENETRESET);
1081 }
1082
1083 /*
1084 * Delete a multicast address record.
1085 */
1086 int
1087 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1088 {
1089 struct ether_multi *enm;
1090 u_char addrlo[ETHER_ADDR_LEN];
1091 u_char addrhi[ETHER_ADDR_LEN];
1092 int s = splimp(), error;
1093
1094 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1095 if (error != 0) {
1096 splx(s);
1097 return (error);
1098 }
1099
1100 /*
1101 * Look ur the address in our list.
1102 */
1103 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1104 if (enm == NULL) {
1105 splx(s);
1106 return (ENXIO);
1107 }
1108 if (--enm->enm_refcount != 0) {
1109 /*
1110 * Still some claims to this record.
1111 */
1112 splx(s);
1113 return (0);
1114 }
1115 /*
1116 * No remaining claims to this record; unlink and free it.
1117 */
1118 LIST_REMOVE(enm, enm_list);
1119 free(enm, M_IFMADDR);
1120 ec->ec_multicnt--;
1121 splx(s);
1122 /*
1123 * Return ENETRESET to inform the driver that the list has changed
1124 * and its reception filter should be adjusted accordingly.
1125 */
1126 return (ENETRESET);
1127 }
1128
1129 /*
1130 * Common ioctls for Ethernet interfaces. Note, we must be
1131 * called at splnet().
1132 */
1133 int
1134 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1135 {
1136 struct ethercom *ec = (void *) ifp;
1137 struct ifreq *ifr = (struct ifreq *)data;
1138 struct ifaddr *ifa = (struct ifaddr *)data;
1139 int error = 0;
1140
1141 switch (cmd) {
1142 case SIOCSIFADDR:
1143 ifp->if_flags |= IFF_UP;
1144 switch (ifa->ifa_addr->sa_family) {
1145 case AF_LINK:
1146 {
1147 struct sockaddr_dl *sdl =
1148 (struct sockaddr_dl *) ifa->ifa_addr;
1149
1150 if (sdl->sdl_type != IFT_ETHER ||
1151 sdl->sdl_alen != ifp->if_addrlen) {
1152 error = EINVAL;
1153 break;
1154 }
1155
1156 memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
1157 ifp->if_addrlen);
1158
1159 /* Set new address. */
1160 error = (*ifp->if_init)(ifp);
1161 break;
1162 }
1163 #ifdef INET
1164 case AF_INET:
1165 if ((error = (*ifp->if_init)(ifp)) != 0)
1166 break;
1167 arp_ifinit(ifp, ifa);
1168 break;
1169 #endif /* INET */
1170 #ifdef NS
1171 case AF_NS:
1172 {
1173 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1174
1175 if (ns_nullhost(*ina))
1176 ina->x_host = *(union ns_host *)
1177 LLADDR(ifp->if_sadl);
1178 else
1179 memcpy(LLADDR(ifp->if_sadl),
1180 ina->x_host.c_host, ifp->if_addrlen);
1181 /* Set new address. */
1182 error = (*ifp->if_init)(ifp);
1183 break;
1184 }
1185 #endif /* NS */
1186 default:
1187 error = (*ifp->if_init)(ifp);
1188 break;
1189 }
1190 break;
1191
1192 case SIOCGIFADDR:
1193 memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1194 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1195 break;
1196
1197 case SIOCSIFMTU:
1198 if (ifr->ifr_mtu > ETHERMTU)
1199 error = EINVAL;
1200 else
1201 ifp->if_mtu = ifr->ifr_mtu;
1202 break;
1203
1204 case SIOCSIFFLAGS:
1205 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1206 /*
1207 * If interface is marked down and it is running,
1208 * then stop and disable it.
1209 */
1210 (*ifp->if_stop)(ifp, 1);
1211 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1212 /*
1213 * If interface is marked up and it is stopped, then
1214 * start it.
1215 */
1216 error = (*ifp->if_init)(ifp);
1217 } else if ((ifp->if_flags & IFF_UP) != 0) {
1218 /*
1219 * Reset the interface to pick up changes in any other
1220 * flags that affect the hardware state.
1221 */
1222 error = (*ifp->if_init)(ifp);
1223 }
1224 break;
1225
1226 case SIOCADDMULTI:
1227 error = ether_addmulti(ifr, ec);
1228 break;
1229
1230 case SIOCDELMULTI:
1231 error = ether_delmulti(ifr, ec);
1232 break;
1233
1234 default:
1235 error = ENOTTY;
1236 }
1237
1238 return (error);
1239 }
1240