FreeBSD/Linux Kernel Cross Reference
sys/netinet/in.c
1 /*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: src/sys/netinet/in.c,v 1.25.2.2 1999/09/05 08:18:17 peter Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/systm.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_ether.h>
55
56 #include <netinet/igmp_var.h>
57
58 /*
59 * This structure is used to keep track of in_multi chains which belong to
60 * deleted interface addresses.
61 */
62 static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */
63
64 struct multi_kludge {
65 LIST_ENTRY(multi_kludge) mk_entry;
66 struct ifnet *mk_ifp;
67 struct in_multihead mk_head;
68 };
69
70 static void in_socktrim __P((struct sockaddr_in *));
71 static int in_ifinit __P((struct ifnet *,
72 struct in_ifaddr *, struct sockaddr_in *, int));
73 static void in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
74
75 static int subnetsarelocal = 0;
76 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
77 &subnetsarelocal, 0, "");
78 /*
79 * Return 1 if an internet address is for a ``local'' host
80 * (one to which we have a connection). If subnetsarelocal
81 * is true, this includes other subnets of the local net.
82 * Otherwise, it includes only the directly-connected (sub)nets.
83 */
84 int
85 in_localaddr(in)
86 struct in_addr in;
87 {
88 register u_long i = ntohl(in.s_addr);
89 register struct in_ifaddr *ia;
90
91 if (subnetsarelocal) {
92 for (ia = in_ifaddr; ia; ia = ia->ia_next)
93 if ((i & ia->ia_netmask) == ia->ia_net)
94 return (1);
95 } else {
96 for (ia = in_ifaddr; ia; ia = ia->ia_next)
97 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98 return (1);
99 }
100 return (0);
101 }
102
103 /*
104 * Determine whether an IP address is in a reserved set of addresses
105 * that may not be forwarded, or whether datagrams to that destination
106 * may be forwarded.
107 */
108 int
109 in_canforward(in)
110 struct in_addr in;
111 {
112 register u_long i = ntohl(in.s_addr);
113 register u_long net;
114
115 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
116 return (0);
117 if (IN_CLASSA(i)) {
118 net = i & IN_CLASSA_NET;
119 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
120 return (0);
121 }
122 return (1);
123 }
124
125 /*
126 * Trim a mask in a sockaddr
127 */
128 static void
129 in_socktrim(ap)
130 struct sockaddr_in *ap;
131 {
132 register char *cplim = (char *) &ap->sin_addr;
133 register char *cp = (char *) (&ap->sin_addr + 1);
134
135 ap->sin_len = 0;
136 while (--cp >= cplim)
137 if (*cp) {
138 (ap)->sin_len = cp - (char *) (ap) + 1;
139 break;
140 }
141 }
142
143 static int in_interfaces; /* number of external internet interfaces */
144
145 /*
146 * Generic internet control operations (ioctl's).
147 * Ifp is 0 if not an interface-specific ioctl.
148 */
149 /* ARGSUSED */
150 int
151 in_control(so, cmd, data, ifp)
152 struct socket *so;
153 u_long cmd;
154 caddr_t data;
155 register struct ifnet *ifp;
156 {
157 register struct ifreq *ifr = (struct ifreq *)data;
158 register struct in_ifaddr *ia = 0, *iap;
159 register struct ifaddr *ifa;
160 struct in_ifaddr *oia;
161 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
162 struct sockaddr_in oldaddr;
163 int error, hostIsNew, maskIsNew, s;
164 u_long i;
165 struct multi_kludge *mk;
166
167 /*
168 * Find address for this interface, if it exists.
169 *
170 * If an alias address was specified, find that one instead of
171 * the first one on the interface.
172 */
173 if (ifp)
174 for (iap = in_ifaddr; iap; iap = iap->ia_next)
175 if (iap->ia_ifp == ifp) {
176 if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
177 iap->ia_addr.sin_addr.s_addr) {
178 ia = iap;
179 break;
180 } else if (ia == NULL) {
181 ia = iap;
182 if (ifr->ifr_addr.sa_family != AF_INET)
183 break;
184 }
185 }
186
187 switch (cmd) {
188
189 case SIOCAIFADDR:
190 case SIOCDIFADDR:
191 if (ifp == 0)
192 return (EADDRNOTAVAIL);
193 if (ifra->ifra_addr.sin_family == AF_INET) {
194 for (oia = ia; ia; ia = ia->ia_next) {
195 if (ia->ia_ifp == ifp &&
196 ia->ia_addr.sin_addr.s_addr ==
197 ifra->ifra_addr.sin_addr.s_addr)
198 break;
199 }
200 if ((ifp->if_flags & IFF_POINTOPOINT)
201 && (cmd == SIOCAIFADDR)
202 && (ifra->ifra_dstaddr.sin_addr.s_addr
203 == INADDR_ANY)) {
204 return EDESTADDRREQ;
205 }
206 }
207 if (cmd == SIOCDIFADDR && ia == 0)
208 return (EADDRNOTAVAIL);
209 /* FALLTHROUGH */
210 case SIOCSIFADDR:
211 case SIOCSIFNETMASK:
212 case SIOCSIFDSTADDR:
213 if ((so->so_state & SS_PRIV) == 0)
214 return (EPERM);
215
216 if (ifp == 0)
217 return (EADDRNOTAVAIL);
218 if (ia == (struct in_ifaddr *)0) {
219 oia = (struct in_ifaddr *)
220 malloc(sizeof *oia, M_IFADDR, M_WAITOK);
221 if (oia == (struct in_ifaddr *)NULL)
222 return (ENOBUFS);
223 bzero((caddr_t)oia, sizeof *oia);
224 ia = in_ifaddr;
225 /*
226 * Protect from ipintr() traversing address list
227 * while we're modifying it.
228 */
229 s = splnet();
230
231 if (ia) {
232 for ( ; ia->ia_next; ia = ia->ia_next)
233 continue;
234 ia->ia_next = oia;
235 } else
236 in_ifaddr = oia;
237 ia = oia;
238 ifa = ifp->if_addrlist;
239 if (ifa) {
240 for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
241 continue;
242 ifa->ifa_next = (struct ifaddr *) ia;
243 } else
244 ifp->if_addrlist = (struct ifaddr *) ia;
245 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
246 ia->ia_ifa.ifa_dstaddr
247 = (struct sockaddr *)&ia->ia_dstaddr;
248 ia->ia_ifa.ifa_netmask
249 = (struct sockaddr *)&ia->ia_sockmask;
250 ia->ia_sockmask.sin_len = 8;
251 if (ifp->if_flags & IFF_BROADCAST) {
252 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
253 ia->ia_broadaddr.sin_family = AF_INET;
254 }
255 ia->ia_ifp = ifp;
256 if (!(ifp->if_flags & IFF_LOOPBACK))
257 in_interfaces++;
258 splx(s);
259 }
260 break;
261
262 case SIOCSIFBRDADDR:
263 if ((so->so_state & SS_PRIV) == 0)
264 return (EPERM);
265 /* FALLTHROUGH */
266
267 case SIOCGIFADDR:
268 case SIOCGIFNETMASK:
269 case SIOCGIFDSTADDR:
270 case SIOCGIFBRDADDR:
271 if (ia == (struct in_ifaddr *)0)
272 return (EADDRNOTAVAIL);
273 break;
274 }
275 switch (cmd) {
276
277 case SIOCGIFADDR:
278 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
279 break;
280
281 case SIOCGIFBRDADDR:
282 if ((ifp->if_flags & IFF_BROADCAST) == 0)
283 return (EINVAL);
284 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
285 break;
286
287 case SIOCGIFDSTADDR:
288 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
289 return (EINVAL);
290 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
291 break;
292
293 case SIOCGIFNETMASK:
294 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
295 break;
296
297 case SIOCSIFDSTADDR:
298 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
299 return (EINVAL);
300 oldaddr = ia->ia_dstaddr;
301 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
302 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
303 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
304 ia->ia_dstaddr = oldaddr;
305 return (error);
306 }
307 if (ia->ia_flags & IFA_ROUTE) {
308 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
309 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
310 ia->ia_ifa.ifa_dstaddr =
311 (struct sockaddr *)&ia->ia_dstaddr;
312 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
313 }
314 break;
315
316 case SIOCSIFBRDADDR:
317 if ((ifp->if_flags & IFF_BROADCAST) == 0)
318 return (EINVAL);
319 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
320 break;
321
322 case SIOCSIFADDR:
323 return (in_ifinit(ifp, ia,
324 (struct sockaddr_in *) &ifr->ifr_addr, 1));
325
326 case SIOCSIFNETMASK:
327 i = ifra->ifra_addr.sin_addr.s_addr;
328 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
329 break;
330
331 case SIOCAIFADDR:
332 maskIsNew = 0;
333 hostIsNew = 1;
334 error = 0;
335 if (ia->ia_addr.sin_family == AF_INET) {
336 if (ifra->ifra_addr.sin_len == 0) {
337 ifra->ifra_addr = ia->ia_addr;
338 hostIsNew = 0;
339 } else if (ifra->ifra_addr.sin_addr.s_addr ==
340 ia->ia_addr.sin_addr.s_addr)
341 hostIsNew = 0;
342 }
343 if (ifra->ifra_mask.sin_len) {
344 in_ifscrub(ifp, ia);
345 ia->ia_sockmask = ifra->ifra_mask;
346 ia->ia_subnetmask =
347 ntohl(ia->ia_sockmask.sin_addr.s_addr);
348 maskIsNew = 1;
349 }
350 if ((ifp->if_flags & IFF_POINTOPOINT) &&
351 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
352 in_ifscrub(ifp, ia);
353 ia->ia_dstaddr = ifra->ifra_dstaddr;
354 maskIsNew = 1; /* We lie; but the effect's the same */
355 }
356 if (ifra->ifra_addr.sin_family == AF_INET &&
357 (hostIsNew || maskIsNew))
358 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
359 if ((ifp->if_flags & IFF_BROADCAST) &&
360 (ifra->ifra_broadaddr.sin_family == AF_INET))
361 ia->ia_broadaddr = ifra->ifra_broadaddr;
362 return (error);
363
364 case SIOCDIFADDR:
365 mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK);
366 if (!mk)
367 return ENOBUFS;
368
369 in_ifscrub(ifp, ia);
370 /*
371 * Protect from ipintr() traversing address list
372 * while we're modifying it.
373 */
374 s = splnet();
375
376 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
377 ifp->if_addrlist = ifa->ifa_next;
378 else {
379 while (ifa->ifa_next &&
380 (ifa->ifa_next != (struct ifaddr *)ia))
381 ifa = ifa->ifa_next;
382 if (ifa->ifa_next)
383 ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
384 else
385 printf("Couldn't unlink inifaddr from ifp\n");
386 }
387 oia = ia;
388 if (oia == (ia = in_ifaddr))
389 in_ifaddr = ia->ia_next;
390 else {
391 while (ia->ia_next && (ia->ia_next != oia))
392 ia = ia->ia_next;
393 if (ia->ia_next)
394 ia->ia_next = oia->ia_next;
395 else
396 printf("Didn't unlink inifadr from list\n");
397 }
398
399 if (!oia->ia_multiaddrs.lh_first) {
400 IFAFREE(&oia->ia_ifa);
401 FREE(mk, M_IPMADDR);
402 splx(s);
403 break;
404 }
405
406 /*
407 * Multicast address kludge:
408 * If there were any multicast addresses attached to this
409 * interface address, either move them to another address
410 * on this interface, or save them until such time as this
411 * interface is reconfigured for IP.
412 */
413 IFP_TO_IA(oia->ia_ifp, ia);
414 if (ia) { /* there is another address */
415 struct in_multi *inm;
416 for(inm = oia->ia_multiaddrs.lh_first; inm;
417 inm = inm->inm_entry.le_next) {
418 IFAFREE(&inm->inm_ia->ia_ifa);
419 ia->ia_ifa.ifa_refcnt++;
420 inm->inm_ia = ia;
421 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm,
422 inm_entry);
423 }
424 FREE(mk, M_IPMADDR);
425 } else { /* last address on this if deleted, save */
426 struct in_multi *inm;
427
428 LIST_INIT(&mk->mk_head);
429 mk->mk_ifp = ifp;
430
431 for(inm = oia->ia_multiaddrs.lh_first; inm;
432 inm = inm->inm_entry.le_next) {
433 LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry);
434 }
435
436 if (mk->mk_head.lh_first) {
437 LIST_INSERT_HEAD(&in_mk, mk, mk_entry);
438 } else {
439 FREE(mk, M_IPMADDR);
440 }
441 }
442
443 IFAFREE((&oia->ia_ifa));
444 splx(s);
445 break;
446
447 default:
448 if (ifp == 0 || ifp->if_ioctl == 0)
449 return (EOPNOTSUPP);
450 return ((*ifp->if_ioctl)(ifp, cmd, data));
451 }
452 return (0);
453 }
454
455 /*
456 * Delete any existing route for an interface.
457 */
458 static void
459 in_ifscrub(ifp, ia)
460 register struct ifnet *ifp;
461 register struct in_ifaddr *ia;
462 {
463
464 if ((ia->ia_flags & IFA_ROUTE) == 0)
465 return;
466 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
467 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
468 else
469 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
470 ia->ia_flags &= ~IFA_ROUTE;
471 }
472
473 /*
474 * Initialize an interface's internet address
475 * and routing table entry.
476 */
477 static int
478 in_ifinit(ifp, ia, sin, scrub)
479 register struct ifnet *ifp;
480 register struct in_ifaddr *ia;
481 struct sockaddr_in *sin;
482 int scrub;
483 {
484 register u_long i = ntohl(sin->sin_addr.s_addr);
485 struct sockaddr_in oldaddr;
486 int s = splimp(), flags = RTF_UP, error;
487 struct multi_kludge *mk;
488
489 oldaddr = ia->ia_addr;
490 ia->ia_addr = *sin;
491 /*
492 * Give the interface a chance to initialize
493 * if this is its first address,
494 * and to validate the address if necessary.
495 */
496 if (ifp->if_ioctl &&
497 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
498 splx(s);
499 ia->ia_addr = oldaddr;
500 return (error);
501 }
502 splx(s);
503 if (scrub) {
504 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
505 in_ifscrub(ifp, ia);
506 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
507 }
508 if (IN_CLASSA(i))
509 ia->ia_netmask = IN_CLASSA_NET;
510 else if (IN_CLASSB(i))
511 ia->ia_netmask = IN_CLASSB_NET;
512 else
513 ia->ia_netmask = IN_CLASSC_NET;
514 /*
515 * The subnet mask usually includes at least the standard network part,
516 * but may may be smaller in the case of supernetting.
517 * If it is set, we believe it.
518 */
519 if (ia->ia_subnetmask == 0) {
520 ia->ia_subnetmask = ia->ia_netmask;
521 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
522 } else
523 ia->ia_netmask &= ia->ia_subnetmask;
524 ia->ia_net = i & ia->ia_netmask;
525 ia->ia_subnet = i & ia->ia_subnetmask;
526 in_socktrim(&ia->ia_sockmask);
527 /*
528 * Add route for the network.
529 */
530 ia->ia_ifa.ifa_metric = ifp->if_metric;
531 if (ifp->if_flags & IFF_BROADCAST) {
532 ia->ia_broadaddr.sin_addr.s_addr =
533 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
534 ia->ia_netbroadcast.s_addr =
535 htonl(ia->ia_net | ~ ia->ia_netmask);
536 } else if (ifp->if_flags & IFF_LOOPBACK) {
537 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
538 flags |= RTF_HOST;
539 } else if (ifp->if_flags & IFF_POINTOPOINT) {
540 if (ia->ia_dstaddr.sin_family != AF_INET)
541 return (0);
542 flags |= RTF_HOST;
543 }
544 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
545 ia->ia_flags |= IFA_ROUTE;
546
547 LIST_INIT(&ia->ia_multiaddrs);
548 /*
549 * If the interface supports multicast, join the "all hosts"
550 * multicast group on that interface.
551 */
552 if (ifp->if_flags & IFF_MULTICAST) {
553 struct in_addr addr;
554
555 /*
556 * Continuation of multicast address hack:
557 * If there was a multicast group list previously saved
558 * for this interface, then we re-attach it to the first
559 * address configured on the i/f.
560 */
561 for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
562 if(mk->mk_ifp == ifp) {
563 struct in_multi *inm;
564
565 for(inm = mk->mk_head.lh_first; inm;
566 inm = inm->inm_entry.le_next) {
567 IFAFREE(&inm->inm_ia->ia_ifa);
568 ia->ia_ifa.ifa_refcnt++;
569 inm->inm_ia = ia;
570 LIST_INSERT_HEAD(&ia->ia_multiaddrs,
571 inm, inm_entry);
572 }
573 LIST_REMOVE(mk, mk_entry);
574 free(mk, M_IPMADDR);
575 break;
576 }
577 }
578
579 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
580 in_addmulti(&addr, ifp);
581 }
582 return (error);
583 }
584
585
586 /*
587 * Return 1 if the address might be a local broadcast address.
588 */
589 int
590 in_broadcast(in, ifp)
591 struct in_addr in;
592 struct ifnet *ifp;
593 {
594 register struct ifaddr *ifa;
595 u_long t;
596
597 if (in.s_addr == INADDR_BROADCAST ||
598 in.s_addr == INADDR_ANY)
599 return 1;
600 if ((ifp->if_flags & IFF_BROADCAST) == 0)
601 return 0;
602 t = ntohl(in.s_addr);
603 /*
604 * Look through the list of addresses for a match
605 * with a broadcast address.
606 */
607 #define ia ((struct in_ifaddr *)ifa)
608 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
609 if (ifa->ifa_addr->sa_family == AF_INET &&
610 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
611 in.s_addr == ia->ia_netbroadcast.s_addr ||
612 /*
613 * Check for old-style (host 0) broadcast.
614 */
615 t == ia->ia_subnet || t == ia->ia_net) &&
616 /*
617 * Check for an all one subnetmask. These
618 * only exist when an interface gets a secondary
619 * address.
620 */
621 ia->ia_subnetmask != (u_long)0xffffffff)
622 return 1;
623 return (0);
624 #undef ia
625 }
626 /*
627 * Add an address to the list of IP multicast addresses for a given interface.
628 */
629 struct in_multi *
630 in_addmulti(ap, ifp)
631 register struct in_addr *ap;
632 register struct ifnet *ifp;
633 {
634 register struct in_multi *inm;
635 struct ifreq ifr;
636 struct in_ifaddr *ia;
637 int s = splnet();
638
639 /*
640 * See if address already in list.
641 */
642 IN_LOOKUP_MULTI(*ap, ifp, inm);
643 if (inm != NULL) {
644 /*
645 * Found it; just increment the reference count.
646 */
647 ++inm->inm_refcount;
648 }
649 else {
650 /*
651 * New address; allocate a new multicast record
652 * and link it into the interface's multicast list.
653 */
654 inm = (struct in_multi *)malloc(sizeof(*inm),
655 M_IPMADDR, M_NOWAIT);
656 if (inm == NULL) {
657 splx(s);
658 return (NULL);
659 }
660 inm->inm_addr = *ap;
661 inm->inm_ifp = ifp;
662 inm->inm_refcount = 1;
663 IFP_TO_IA(ifp, ia);
664 if (ia == NULL) {
665 free(inm, M_IPMADDR);
666 splx(s);
667 return (NULL);
668 }
669 inm->inm_ia = ia;
670 ia->ia_ifa.ifa_refcnt++; /* gain a reference */
671 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry);
672
673 /*
674 * Ask the network driver to update its multicast reception
675 * filter appropriately for the new address.
676 */
677 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
678 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap;
679 if ((ifp->if_ioctl == NULL) ||
680 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
681 LIST_REMOVE(inm, inm_entry);
682 IFAFREE(&ia->ia_ifa); /* release reference */
683 free(inm, M_IPMADDR);
684 splx(s);
685 return (NULL);
686 }
687 /*
688 * Let IGMP know that we have joined a new IP multicast group.
689 */
690 igmp_joingroup(inm);
691 }
692 splx(s);
693 return (inm);
694 }
695
696 /*
697 * Delete a multicast address record.
698 */
699 void
700 in_delmulti(inm)
701 register struct in_multi *inm;
702 {
703 struct ifreq ifr;
704 int s = splnet();
705
706 if (--inm->inm_refcount == 0) {
707 /*
708 * No remaining claims to this record; let IGMP know that
709 * we are leaving the multicast group.
710 */
711 igmp_leavegroup(inm);
712 /*
713 * Unlink from list.
714 */
715 LIST_REMOVE(inm, inm_entry);
716 IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */
717
718 /*
719 * Notify the network driver to update its multicast reception
720 * filter.
721 */
722 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
723 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr =
724 inm->inm_addr;
725 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
726 (caddr_t)&ifr);
727 free(inm, M_IPMADDR);
728 }
729 splx(s);
730 }
Cache object: 883f714f14dabb88089c739d69e74b6e
|