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