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