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