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