2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
35 * $DragonFly: src/sys/netinet/in.c,v 1.17 2005/11/28 17:13:46 dillon Exp $
38 #include "opt_bootp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_pcb.h>
57 #include <netinet/igmp_var.h>
59 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
61 static int in_mask2len (struct in_addr *);
62 static void in_len2mask (struct in_addr *, int);
63 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
64 struct ifnet *, struct thread *);
66 static void in_socktrim (struct sockaddr_in *);
67 static int in_ifinit (struct ifnet *,
68 struct in_ifaddr *, struct sockaddr_in *, int);
70 static int subnetsarelocal = 0;
71 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
72 &subnetsarelocal, 0, "");
74 struct in_multihead in_multihead; /* XXX BSS initialization */
76 extern struct inpcbinfo ripcbinfo;
77 extern struct inpcbinfo udbinfo;
80 * Return 1 if an internet address is for a ``local'' host
81 * (one to which we have a connection). If subnetsarelocal
82 * is true, this includes other subnets of the local net.
83 * Otherwise, it includes only the directly-connected (sub)nets.
89 u_long i = ntohl(in.s_addr);
92 if (subnetsarelocal) {
93 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
94 if ((i & ia->ia_netmask) == ia->ia_net)
97 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
98 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
105 * Determine whether an IP address is in a reserved set of addresses
106 * that may not be forwarded, or whether datagrams to that destination
113 u_long i = ntohl(in.s_addr);
116 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
119 net = i & IN_CLASSA_NET;
120 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
127 * Trim a mask in a sockaddr
131 struct sockaddr_in *ap;
133 char *cplim = (char *) &ap->sin_addr;
134 char *cp = (char *) (&ap->sin_addr + 1);
137 while (--cp >= cplim)
139 (ap)->sin_len = cp - (char *) (ap) + 1;
146 struct in_addr *mask;
152 for (x = 0; x < sizeof *mask; x++) {
157 if (x < sizeof *mask) {
158 for (y = 0; y < 8; y++) {
159 if ((p[x] & (0x80 >> y)) == 0)
167 in_len2mask(mask, len)
168 struct in_addr *mask;
175 bzero(mask, sizeof *mask);
176 for (i = 0; i < len / 8; i++)
179 p[i] = (0xff00 >> (len % 8)) & 0xff;
182 static int in_interfaces; /* number of external internet interfaces */
185 * Generic internet control operations (ioctl's).
186 * Ifp is 0 if not an interface-specific ioctl.
188 * NOTE! td might be NULL.
192 in_control(so, cmd, data, ifp, td)
199 struct ifreq *ifr = (struct ifreq *)data;
200 struct in_ifaddr *ia = 0, *iap;
203 struct in_ifaddr *oia;
204 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
205 struct sockaddr_in oldaddr;
206 int hostIsNew, iaIsNew, maskIsNew;
214 if (td && (error = suser(td)) != 0)
220 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
224 * Find address for this interface, if it exists.
226 * If an alias address was specified, find that one instead of
227 * the first one on the interface, if possible
230 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
231 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
232 if (iap->ia_ifp == ifp &&
233 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
238 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
240 if (iap->ia_addr.sin_family == AF_INET) {
252 return (EADDRNOTAVAIL);
253 if (ifra->ifra_addr.sin_family == AF_INET) {
254 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
255 if (ia->ia_ifp == ifp &&
256 ia->ia_addr.sin_addr.s_addr ==
257 ifra->ifra_addr.sin_addr.s_addr)
260 if ((ifp->if_flags & IFF_POINTOPOINT)
261 && (cmd == SIOCAIFADDR)
262 && (ifra->ifra_dstaddr.sin_addr.s_addr
267 if (cmd == SIOCDIFADDR && ia == 0)
268 return (EADDRNOTAVAIL);
273 if (td && (error = suser(td)) != 0)
277 return (EADDRNOTAVAIL);
278 if (ia == (struct in_ifaddr *)0) {
279 ia = (struct in_ifaddr *)
280 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
281 if (ia == (struct in_ifaddr *)NULL)
283 bzero(ia, sizeof *ia);
285 * Protect from ipintr() traversing address list
286 * while we're modifying it.
290 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
292 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
294 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
295 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
296 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
297 ia->ia_sockmask.sin_len = 8;
298 ia->ia_sockmask.sin_family = AF_INET;
299 if (ifp->if_flags & IFF_BROADCAST) {
300 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
301 ia->ia_broadaddr.sin_family = AF_INET;
304 if (!(ifp->if_flags & IFF_LOOPBACK))
312 if (td && (error = suser(td)) != 0)
320 if (ia == (struct in_ifaddr *)0)
321 return (EADDRNOTAVAIL);
327 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
331 if ((ifp->if_flags & IFF_BROADCAST) == 0)
333 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
337 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
339 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
343 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
347 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
349 oldaddr = ia->ia_dstaddr;
350 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
351 lwkt_serialize_enter(ifp->if_serializer);
353 (error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
354 td->td_proc->p_ucred))) {
355 ia->ia_dstaddr = oldaddr;
356 lwkt_serialize_exit(ifp->if_serializer);
359 if (ia->ia_flags & IFA_ROUTE) {
360 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
361 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
362 ia->ia_ifa.ifa_dstaddr =
363 (struct sockaddr *)&ia->ia_dstaddr;
364 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
366 lwkt_serialize_exit(ifp->if_serializer);
370 if ((ifp->if_flags & IFF_BROADCAST) == 0)
372 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
376 error = in_ifinit(ifp, ia,
377 (struct sockaddr_in *) &ifr->ifr_addr, 1);
378 if (error != 0 && iaIsNew)
381 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
385 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
386 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
393 if (ia->ia_addr.sin_family == AF_INET) {
394 if (ifra->ifra_addr.sin_len == 0) {
395 ifra->ifra_addr = ia->ia_addr;
397 } else if (ifra->ifra_addr.sin_addr.s_addr ==
398 ia->ia_addr.sin_addr.s_addr)
401 if (ifra->ifra_mask.sin_len) {
403 ia->ia_sockmask = ifra->ifra_mask;
404 ia->ia_sockmask.sin_family = AF_INET;
406 ntohl(ia->ia_sockmask.sin_addr.s_addr);
409 if ((ifp->if_flags & IFF_POINTOPOINT) &&
410 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
412 ia->ia_dstaddr = ifra->ifra_dstaddr;
413 maskIsNew = 1; /* We lie; but the effect's the same */
415 if (ifra->ifra_addr.sin_family == AF_INET &&
416 (hostIsNew || maskIsNew))
417 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
419 if (error != 0 && iaIsNew)
422 if ((ifp->if_flags & IFF_BROADCAST) &&
423 (ifra->ifra_broadaddr.sin_family == AF_INET))
424 ia->ia_broadaddr = ifra->ifra_broadaddr;
426 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
431 * in_ifscrub kills the interface route.
435 * in_ifadown gets rid of all the rest of
436 * the routes. This is not quite the right
437 * thing to do, but at least if we are running
438 * a routing process they will come back.
440 in_ifadown(&ia->ia_ifa, 1);
441 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
446 if (ifp == NULL || ifp->if_ioctl == NULL)
448 lwkt_serialize_enter(ifp->if_serializer);
449 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
450 lwkt_serialize_exit(ifp->if_serializer);
455 * Protect from ipintr() traversing address list while we're modifying
458 lwkt_serialize_enter(ifp->if_serializer);
459 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
460 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
461 LIST_REMOVE(ia, ia_hash);
462 IFAFREE(&ia->ia_ifa);
463 lwkt_serialize_exit(ifp->if_serializer);
470 * SIOCGLIFADDR: get first address. (?!?)
471 * SIOCGLIFADDR with IFLR_PREFIX:
472 * get first address that matches the specified prefix.
473 * SIOCALIFADDR: add the specified address.
474 * SIOCALIFADDR with IFLR_PREFIX:
475 * EINVAL since we can't deduce hostid part of the address.
476 * SIOCDLIFADDR: delete the specified address.
477 * SIOCDLIFADDR with IFLR_PREFIX:
478 * delete the first address that matches the specified prefix.
480 * EINVAL on invalid parameters
481 * EADDRNOTAVAIL on prefix match failed/specified address not found
482 * other values may be returned from in_ioctl()
484 * NOTE! td might be NULL.
487 in_lifaddr_ioctl(so, cmd, data, ifp, td)
494 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
499 panic("invalid argument to in_lifaddr_ioctl");
505 /* address must be specified on GET with IFLR_PREFIX */
506 if ((iflr->flags & IFLR_PREFIX) == 0)
511 /* address must be specified on ADD and DELETE */
512 if (iflr->addr.ss_family != AF_INET)
514 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
516 /* XXX need improvement */
517 if (iflr->dstaddr.ss_family
518 && iflr->dstaddr.ss_family != AF_INET)
520 if (iflr->dstaddr.ss_family
521 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
524 default: /*shouldn't happen*/
527 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
533 struct in_aliasreq ifra;
535 if (iflr->flags & IFLR_PREFIX)
538 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
539 bzero(&ifra, sizeof ifra);
540 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
542 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
544 if (iflr->dstaddr.ss_family) { /*XXX*/
545 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
546 iflr->dstaddr.ss_len);
549 ifra.ifra_mask.sin_family = AF_INET;
550 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
551 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
553 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
558 struct in_ifaddr *ia;
559 struct in_addr mask, candidate, match;
560 struct sockaddr_in *sin;
563 bzero(&mask, sizeof mask);
564 if (iflr->flags & IFLR_PREFIX) {
565 /* lookup a prefix rather than address. */
566 in_len2mask(&mask, iflr->prefixlen);
568 sin = (struct sockaddr_in *)&iflr->addr;
569 match.s_addr = sin->sin_addr.s_addr;
570 match.s_addr &= mask.s_addr;
572 /* if you set extra bits, that's wrong */
573 if (match.s_addr != sin->sin_addr.s_addr)
578 if (cmd == SIOCGLIFADDR) {
579 /* on getting an address, take the 1st match */
582 /* on deleting an address, do exact match */
583 in_len2mask(&mask, 32);
584 sin = (struct sockaddr_in *)&iflr->addr;
585 match.s_addr = sin->sin_addr.s_addr;
591 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
592 if (ifa->ifa_addr->sa_family != AF_INET6)
596 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
597 candidate.s_addr &= mask.s_addr;
598 if (candidate.s_addr == match.s_addr)
602 return EADDRNOTAVAIL;
603 ia = (struct in_ifaddr *)ifa;
605 if (cmd == SIOCGLIFADDR) {
606 /* fill in the if_laddrreq structure */
607 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
609 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
610 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
611 ia->ia_dstaddr.sin_len);
613 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
616 in_mask2len(&ia->ia_sockmask.sin_addr);
618 iflr->flags = 0; /*XXX*/
622 struct in_aliasreq ifra;
624 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
625 bzero(&ifra, sizeof ifra);
626 bcopy(iflr->iflr_name, ifra.ifra_name,
627 sizeof ifra.ifra_name);
629 bcopy(&ia->ia_addr, &ifra.ifra_addr,
630 ia->ia_addr.sin_len);
631 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
632 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
633 ia->ia_dstaddr.sin_len);
635 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
636 ia->ia_sockmask.sin_len);
638 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
644 return EOPNOTSUPP; /*just for safety*/
648 * Delete any existing route for an interface.
653 struct in_ifaddr *ia;
656 if ((ia->ia_flags & IFA_ROUTE) == 0)
658 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
659 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
661 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
662 ia->ia_flags &= ~IFA_ROUTE;
666 * Initialize an interface's internet address
667 * and routing table entry.
670 in_ifinit(ifp, ia, sin, scrub)
672 struct in_ifaddr *ia;
673 struct sockaddr_in *sin;
676 u_long i = ntohl(sin->sin_addr.s_addr);
677 struct sockaddr_in oldaddr;
678 int flags = RTF_UP, error = 0;
680 lwkt_serialize_enter(ifp->if_serializer);
682 oldaddr = ia->ia_addr;
683 if (oldaddr.sin_family == AF_INET)
684 LIST_REMOVE(ia, ia_hash);
686 if (ia->ia_addr.sin_family == AF_INET)
687 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
690 * Give the interface a chance to initialize
691 * if this is its first address,
692 * and to validate the address if necessary.
695 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
696 lwkt_serialize_exit(ifp->if_serializer);
697 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
698 ia->ia_addr = oldaddr;
699 if (ia->ia_addr.sin_family == AF_INET)
700 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
704 lwkt_serialize_exit(ifp->if_serializer);
706 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
708 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
711 ia->ia_netmask = IN_CLASSA_NET;
712 else if (IN_CLASSB(i))
713 ia->ia_netmask = IN_CLASSB_NET;
715 ia->ia_netmask = IN_CLASSC_NET;
717 * The subnet mask usually includes at least the standard network part,
718 * but may may be smaller in the case of supernetting.
719 * If it is set, we believe it.
721 if (ia->ia_subnetmask == 0) {
722 ia->ia_subnetmask = ia->ia_netmask;
723 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
725 ia->ia_netmask &= ia->ia_subnetmask;
726 ia->ia_net = i & ia->ia_netmask;
727 ia->ia_subnet = i & ia->ia_subnetmask;
728 in_socktrim(&ia->ia_sockmask);
730 * Add route for the network.
732 ia->ia_ifa.ifa_metric = ifp->if_metric;
733 if (ifp->if_flags & IFF_BROADCAST) {
734 ia->ia_broadaddr.sin_addr.s_addr =
735 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
736 ia->ia_netbroadcast.s_addr =
737 htonl(ia->ia_net | ~ ia->ia_netmask);
738 } else if (ifp->if_flags & IFF_LOOPBACK) {
739 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
741 } else if (ifp->if_flags & IFF_POINTOPOINT) {
742 if (ia->ia_dstaddr.sin_family != AF_INET)
748 * Don't add host routes for interface addresses of
749 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
750 * possible to assign several such address pairs with consistent
751 * results (no host route) and is required by BOOTP.
753 * XXX: This is ugly ! There should be a way for the caller to
754 * say that they don't want a host route.
756 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
757 ia->ia_netmask != IN_CLASSA_NET ||
758 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
759 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
760 ia->ia_addr = oldaddr;
763 ia->ia_flags |= IFA_ROUTE;
767 * If the interface supports multicast, join the "all hosts"
768 * multicast group on that interface.
770 if (ifp->if_flags & IFF_MULTICAST) {
773 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
774 in_addmulti(&addr, ifp);
781 * Return 1 if the address might be a local broadcast address.
784 in_broadcast(struct in_addr in, struct ifnet *ifp)
789 if (in.s_addr == INADDR_BROADCAST ||
790 in.s_addr == INADDR_ANY)
792 if ((ifp->if_flags & IFF_BROADCAST) == 0)
794 t = ntohl(in.s_addr);
796 * Look through the list of addresses for a match
797 * with a broadcast address.
799 #define ia ((struct in_ifaddr *)ifa)
800 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
801 if (ifa->ifa_addr->sa_family == AF_INET &&
802 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
803 in.s_addr == ia->ia_netbroadcast.s_addr ||
805 * Check for old-style (host 0) broadcast.
807 t == ia->ia_subnet || t == ia->ia_net) &&
809 * Check for an all one subnetmask. These
810 * only exist when an interface gets a secondary
813 ia->ia_subnetmask != (u_long)0xffffffff)
819 * Add an address to the list of IP multicast addresses for a given interface.
826 struct in_multi *inm;
828 struct sockaddr_in sin;
829 struct ifmultiaddr *ifma;
832 * Call generic routine to add membership or increment
833 * refcount. It wants addresses in the form of a sockaddr,
834 * so we build one here (being careful to zero the unused bytes).
836 bzero(&sin, sizeof sin);
837 sin.sin_family = AF_INET;
838 sin.sin_len = sizeof sin;
841 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
848 * If ifma->ifma_protospec is null, then if_addmulti() created
849 * a new record. Otherwise, we are done.
851 if (ifma->ifma_protospec != 0) {
853 return ifma->ifma_protospec;
856 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
857 at interrupt time? If so, need to fix if_addmulti. XXX */
858 inm = malloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
861 inm->inm_ifma = ifma;
862 ifma->ifma_protospec = inm;
863 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
866 * Let IGMP know that we have joined a new IP multicast group.
874 * Delete a multicast address record.
878 struct in_multi *inm;
880 struct ifmultiaddr *ifma;
881 struct in_multi my_inm;
884 ifma = inm->inm_ifma;
885 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
886 if (ifma->ifma_refcount == 1) {
888 * No remaining claims to this record; let IGMP know that
889 * we are leaving the multicast group.
890 * But do it after the if_delmulti() which might reset
891 * the interface and nuke the packet.
894 ifma->ifma_protospec = 0;
895 LIST_REMOVE(inm, inm_link);
896 free(inm, M_IPMADDR);
898 /* XXX - should be separate API for when we have an ifma? */
899 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
900 if (my_inm.inm_ifp != NULL)
901 igmp_leavegroup(&my_inm);
906 in_ifdetach(struct ifnet *ifp)
908 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
909 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);