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