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