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