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