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.27 2008/05/24 03:57:26 sephe Exp $
38 #include "opt_bootp.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/malloc.h>
45 #include <sys/msgport.h>
46 #include <sys/socket.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/netmsg2.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/in_pcb.h>
61 #include <netinet/igmp_var.h>
63 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
65 static int in_mask2len (struct in_addr *);
66 static void in_len2mask (struct in_addr *, int);
67 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
68 struct ifnet *, struct thread *);
70 static void in_socktrim (struct sockaddr_in *);
71 static int in_ifinit (struct ifnet *,
72 struct in_ifaddr *, struct sockaddr_in *, int);
74 static void in_control_dispatch(struct netmsg *);
75 static int in_control_internal(u_long, caddr_t, struct ifnet *,
78 static int subnetsarelocal = 0;
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
80 &subnetsarelocal, 0, "");
82 struct in_multihead in_multihead; /* XXX BSS initialization */
84 extern struct inpcbinfo ripcbinfo;
85 extern struct inpcbinfo udbinfo;
88 * Return 1 if an internet address is for a ``local'' host
89 * (one to which we have a connection). If subnetsarelocal
90 * is true, this includes other subnets of the local net.
91 * Otherwise, it includes only the directly-connected (sub)nets.
94 in_localaddr(struct in_addr in)
96 u_long i = ntohl(in.s_addr);
99 if (subnetsarelocal) {
100 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
101 if ((i & ia->ia_netmask) == ia->ia_net)
104 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
112 * Determine whether an IP address is in a reserved set of addresses
113 * that may not be forwarded, or whether datagrams to that destination
117 in_canforward(struct in_addr in)
119 u_long i = ntohl(in.s_addr);
122 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
125 net = i & IN_CLASSA_NET;
126 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
133 * Trim a mask in a sockaddr
136 in_socktrim(struct sockaddr_in *ap)
138 char *cplim = (char *) &ap->sin_addr;
139 char *cp = (char *) (&ap->sin_addr + 1);
142 while (--cp >= cplim)
144 (ap)->sin_len = cp - (char *) (ap) + 1;
150 in_mask2len(struct in_addr *mask)
156 for (x = 0; x < sizeof *mask; x++) {
161 if (x < sizeof *mask) {
162 for (y = 0; y < 8; y++) {
163 if ((p[x] & (0x80 >> y)) == 0)
171 in_len2mask(struct in_addr *mask, int len)
177 bzero(mask, sizeof *mask);
178 for (i = 0; i < len / 8; i++)
181 p[i] = (0xff00 >> (len % 8)) & 0xff;
184 static int in_interfaces; /* number of external internet interfaces */
186 struct in_control_arg {
194 in_control_dispatch(struct netmsg *nmsg)
196 struct lwkt_msg *msg = &nmsg->nm_lmsg;
197 const struct in_control_arg *arg = msg->u.ms_resultp;
200 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
201 lwkt_replymsg(msg, error);
205 * Generic internet control operations (ioctl's).
206 * Ifp is 0 if not an interface-specific ioctl.
208 * NOTE! td might be NULL.
212 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
216 struct in_control_arg arg;
217 struct lwkt_msg *msg;
223 if (td && (error = suser(td)) != 0)
229 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
232 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
233 ("recursive SIOC%cLIFADDR!\n",
234 cmd == SIOCDLIFADDR ? 'D' : 'A'));
237 * IFADDR alterations are serialized by netisr0
246 bzero(&arg, sizeof(arg));
252 netmsg_init(&nmsg, &curthread->td_msgport, 0,
253 in_control_dispatch);
255 msg->u.ms_resultp = &arg;
257 lwkt_domsg(cpu_portfn(0), msg, 0);
258 return msg->ms_error;
260 return in_control_internal(cmd, data, ifp, td);
265 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
268 struct ifreq *ifr = (struct ifreq *)data;
269 struct in_ifaddr *ia = 0, *iap;
271 struct in_ifaddr *oia;
272 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
273 struct sockaddr_in oldaddr;
274 int hostIsNew, iaIsNew, maskIsNew;
280 * Find address for this interface, if it exists.
282 * If an alias address was specified, find that one instead of
283 * the first one on the interface, if possible
286 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
287 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
288 if (iap->ia_ifp == ifp &&
289 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
294 struct ifaddr_container *ifac;
296 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
298 iap = ifatoia(ifac->ifa);
299 if (iap->ia_addr.sin_family == AF_INET) {
312 return (EADDRNOTAVAIL);
313 if (ifra->ifra_addr.sin_family == AF_INET) {
314 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
315 if (ia->ia_ifp == ifp &&
316 ia->ia_addr.sin_addr.s_addr ==
317 ifra->ifra_addr.sin_addr.s_addr)
320 if ((ifp->if_flags & IFF_POINTOPOINT)
321 && (cmd == SIOCAIFADDR)
322 && (ifra->ifra_dstaddr.sin_addr.s_addr
327 if (cmd == SIOCDIFADDR && ia == NULL)
328 return (EADDRNOTAVAIL);
333 if (td && (error = suser(td)) != 0)
337 return (EADDRNOTAVAIL);
341 ia = ifa_create(sizeof(*ia), M_WAITOK);
344 * Protect from NETISR_IP traversing address list
345 * while we're modifying it.
349 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
351 ifa_iflink(ifa, ifp, 1);
353 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
354 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
355 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
356 ia->ia_sockmask.sin_len = 8;
357 ia->ia_sockmask.sin_family = AF_INET;
358 if (ifp->if_flags & IFF_BROADCAST) {
359 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
360 ia->ia_broadaddr.sin_family = AF_INET;
363 if (!(ifp->if_flags & IFF_LOOPBACK))
371 if (td && (error = suser(td)) != 0)
380 return (EADDRNOTAVAIL);
386 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
390 if ((ifp->if_flags & IFF_BROADCAST) == 0)
392 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
396 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
398 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
402 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
406 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
408 oldaddr = ia->ia_dstaddr;
409 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
410 lwkt_serialize_enter(ifp->if_serializer);
412 (error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
413 td->td_proc->p_ucred))) {
414 ia->ia_dstaddr = oldaddr;
415 lwkt_serialize_exit(ifp->if_serializer);
418 if (ia->ia_flags & IFA_ROUTE) {
419 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
420 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
421 ia->ia_ifa.ifa_dstaddr =
422 (struct sockaddr *)&ia->ia_dstaddr;
423 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
425 lwkt_serialize_exit(ifp->if_serializer);
429 if ((ifp->if_flags & IFF_BROADCAST) == 0)
431 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
435 error = in_ifinit(ifp, ia,
436 (struct sockaddr_in *) &ifr->ifr_addr, 1);
437 if (error != 0 && iaIsNew)
440 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
444 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
445 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
452 if (ia->ia_addr.sin_family == AF_INET) {
453 if (ifra->ifra_addr.sin_len == 0) {
454 ifra->ifra_addr = ia->ia_addr;
456 } else if (ifra->ifra_addr.sin_addr.s_addr ==
457 ia->ia_addr.sin_addr.s_addr)
460 if (ifra->ifra_mask.sin_len) {
462 ia->ia_sockmask = ifra->ifra_mask;
463 ia->ia_sockmask.sin_family = AF_INET;
465 ntohl(ia->ia_sockmask.sin_addr.s_addr);
468 if ((ifp->if_flags & IFF_POINTOPOINT) &&
469 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
471 ia->ia_dstaddr = ifra->ifra_dstaddr;
472 maskIsNew = 1; /* We lie; but the effect's the same */
474 if (ifra->ifra_addr.sin_family == AF_INET &&
475 (hostIsNew || maskIsNew))
476 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
478 if (error != 0 && iaIsNew)
481 if ((ifp->if_flags & IFF_BROADCAST) &&
482 (ifra->ifra_broadaddr.sin_family == AF_INET))
483 ia->ia_broadaddr = ifra->ifra_broadaddr;
485 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
490 * in_ifscrub kills the interface route.
494 * in_ifadown gets rid of all the rest of
495 * the routes. This is not quite the right
496 * thing to do, but at least if we are running
497 * a routing process they will come back.
499 in_ifadown(&ia->ia_ifa, 1);
500 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
505 if (ifp == NULL || ifp->if_ioctl == NULL)
507 lwkt_serialize_enter(ifp->if_serializer);
508 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
509 lwkt_serialize_exit(ifp->if_serializer);
513 ifa_ifunlink(&ia->ia_ifa, ifp);
516 * Protect from NETISR_IP traversing address list while we're modifying
519 crit_enter(); /* XXX MP */
520 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
521 LIST_REMOVE(ia, ia_hash);
522 crit_exit(); /* XXX MP */
524 ifa_destroy(&ia->ia_ifa);
531 * SIOCGLIFADDR: get first address. (?!?)
532 * SIOCGLIFADDR with IFLR_PREFIX:
533 * get first address that matches the specified prefix.
534 * SIOCALIFADDR: add the specified address.
535 * SIOCALIFADDR with IFLR_PREFIX:
536 * EINVAL since we can't deduce hostid part of the address.
537 * SIOCDLIFADDR: delete the specified address.
538 * SIOCDLIFADDR with IFLR_PREFIX:
539 * delete the first address that matches the specified prefix.
541 * EINVAL on invalid parameters
542 * EADDRNOTAVAIL on prefix match failed/specified address not found
543 * other values may be returned from in_ioctl()
545 * NOTE! td might be NULL.
548 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
551 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
555 panic("invalid argument to in_lifaddr_ioctl");
561 /* address must be specified on GET with IFLR_PREFIX */
562 if ((iflr->flags & IFLR_PREFIX) == 0)
567 /* address must be specified on ADD and DELETE */
568 if (iflr->addr.ss_family != AF_INET)
570 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
572 /* XXX need improvement */
573 if (iflr->dstaddr.ss_family
574 && iflr->dstaddr.ss_family != AF_INET)
576 if (iflr->dstaddr.ss_family
577 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
580 default: /*shouldn't happen*/
583 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
589 struct in_aliasreq ifra;
591 if (iflr->flags & IFLR_PREFIX)
594 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
595 bzero(&ifra, sizeof ifra);
596 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
598 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
600 if (iflr->dstaddr.ss_family) { /*XXX*/
601 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
602 iflr->dstaddr.ss_len);
605 ifra.ifra_mask.sin_family = AF_INET;
606 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
607 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
609 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
614 struct ifaddr_container *ifac;
615 struct in_ifaddr *ia;
616 struct in_addr mask, candidate, match;
617 struct sockaddr_in *sin;
620 bzero(&mask, sizeof mask);
621 if (iflr->flags & IFLR_PREFIX) {
622 /* lookup a prefix rather than address. */
623 in_len2mask(&mask, iflr->prefixlen);
625 sin = (struct sockaddr_in *)&iflr->addr;
626 match.s_addr = sin->sin_addr.s_addr;
627 match.s_addr &= mask.s_addr;
629 /* if you set extra bits, that's wrong */
630 if (match.s_addr != sin->sin_addr.s_addr)
635 if (cmd == SIOCGLIFADDR) {
636 /* on getting an address, take the 1st match */
637 match.s_addr = 0; /* gcc4 warning */
640 /* on deleting an address, do exact match */
641 in_len2mask(&mask, 32);
642 sin = (struct sockaddr_in *)&iflr->addr;
643 match.s_addr = sin->sin_addr.s_addr;
649 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
650 struct ifaddr *ifa = ifac->ifa;
652 if (ifa->ifa_addr->sa_family != AF_INET6)
657 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
658 candidate.s_addr &= mask.s_addr;
659 if (candidate.s_addr == match.s_addr)
663 return EADDRNOTAVAIL;
664 ia = (struct in_ifaddr *)(ifac->ifa);
666 if (cmd == SIOCGLIFADDR) {
667 /* fill in the if_laddrreq structure */
668 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
670 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
671 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
672 ia->ia_dstaddr.sin_len);
674 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
677 in_mask2len(&ia->ia_sockmask.sin_addr);
679 iflr->flags = 0; /*XXX*/
683 struct in_aliasreq ifra;
685 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
686 bzero(&ifra, sizeof ifra);
687 bcopy(iflr->iflr_name, ifra.ifra_name,
688 sizeof ifra.ifra_name);
690 bcopy(&ia->ia_addr, &ifra.ifra_addr,
691 ia->ia_addr.sin_len);
692 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
693 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
694 ia->ia_dstaddr.sin_len);
696 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
697 ia->ia_sockmask.sin_len);
699 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
705 return EOPNOTSUPP; /*just for safety*/
709 * Delete any existing route for an interface.
712 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
715 if ((ia->ia_flags & IFA_ROUTE) == 0)
717 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
718 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
720 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
721 ia->ia_flags &= ~IFA_ROUTE;
725 * Initialize an interface's internet address
726 * and routing table entry.
729 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, int scrub)
731 u_long i = ntohl(sin->sin_addr.s_addr);
732 struct sockaddr_in oldaddr;
733 int flags = RTF_UP, error = 0;
735 lwkt_serialize_enter(ifp->if_serializer);
737 oldaddr = ia->ia_addr;
738 if (oldaddr.sin_family == AF_INET)
739 LIST_REMOVE(ia, ia_hash);
741 if (ia->ia_addr.sin_family == AF_INET)
742 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
745 * Give the interface a chance to initialize
746 * if this is its first address,
747 * and to validate the address if necessary.
750 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
751 lwkt_serialize_exit(ifp->if_serializer);
752 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
753 ia->ia_addr = oldaddr;
754 if (ia->ia_addr.sin_family == AF_INET)
755 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
759 lwkt_serialize_exit(ifp->if_serializer);
761 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
763 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
766 ia->ia_netmask = IN_CLASSA_NET;
767 else if (IN_CLASSB(i))
768 ia->ia_netmask = IN_CLASSB_NET;
770 ia->ia_netmask = IN_CLASSC_NET;
772 * The subnet mask usually includes at least the standard network part,
773 * but may may be smaller in the case of supernetting.
774 * If it is set, we believe it.
776 if (ia->ia_subnetmask == 0) {
777 ia->ia_subnetmask = ia->ia_netmask;
778 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
780 ia->ia_netmask &= ia->ia_subnetmask;
781 ia->ia_net = i & ia->ia_netmask;
782 ia->ia_subnet = i & ia->ia_subnetmask;
783 in_socktrim(&ia->ia_sockmask);
785 * Add route for the network.
787 ia->ia_ifa.ifa_metric = ifp->if_metric;
788 if (ifp->if_flags & IFF_BROADCAST) {
789 ia->ia_broadaddr.sin_addr.s_addr =
790 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
791 ia->ia_netbroadcast.s_addr =
792 htonl(ia->ia_net | ~ ia->ia_netmask);
793 } else if (ifp->if_flags & IFF_LOOPBACK) {
794 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
796 } else if (ifp->if_flags & IFF_POINTOPOINT) {
797 if (ia->ia_dstaddr.sin_family != AF_INET)
803 * Don't add host routes for interface addresses of
804 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
805 * possible to assign several such address pairs with consistent
806 * results (no host route) and is required by BOOTP.
808 * XXX: This is ugly ! There should be a way for the caller to
809 * say that they don't want a host route.
811 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
812 ia->ia_netmask != IN_CLASSA_NET ||
813 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
814 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
815 ia->ia_addr = oldaddr;
818 ia->ia_flags |= IFA_ROUTE;
822 * If the interface supports multicast, join the "all hosts"
823 * multicast group on that interface.
825 if (ifp->if_flags & IFF_MULTICAST) {
828 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
829 in_addmulti(&addr, ifp);
836 * Return 1 if the address might be a local broadcast address.
839 in_broadcast(struct in_addr in, struct ifnet *ifp)
841 struct ifaddr_container *ifac;
844 if (in.s_addr == INADDR_BROADCAST ||
845 in.s_addr == INADDR_ANY)
847 if ((ifp->if_flags & IFF_BROADCAST) == 0)
849 t = ntohl(in.s_addr);
851 * Look through the list of addresses for a match
852 * with a broadcast address.
854 #define ia ((struct in_ifaddr *)ifa)
855 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
856 struct ifaddr *ifa = ifac->ifa;
858 if (ifa->ifa_addr->sa_family == AF_INET &&
859 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
860 in.s_addr == ia->ia_netbroadcast.s_addr ||
862 * Check for old-style (host 0) broadcast.
864 t == ia->ia_subnet || t == ia->ia_net) &&
866 * Check for an all one subnetmask. These
867 * only exist when an interface gets a secondary
870 ia->ia_subnetmask != (u_long)0xffffffff)
877 * Add an address to the list of IP multicast addresses for a given interface.
880 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
882 struct in_multi *inm;
884 struct sockaddr_in sin;
885 struct ifmultiaddr *ifma;
888 * Call generic routine to add membership or increment
889 * refcount. It wants addresses in the form of a sockaddr,
890 * so we build one here (being careful to zero the unused bytes).
892 bzero(&sin, sizeof sin);
893 sin.sin_family = AF_INET;
894 sin.sin_len = sizeof sin;
897 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
904 * If ifma->ifma_protospec is null, then if_addmulti() created
905 * a new record. Otherwise, we are done.
907 if (ifma->ifma_protospec != 0) {
909 return ifma->ifma_protospec;
912 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
913 at interrupt time? If so, need to fix if_addmulti. XXX */
914 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
917 inm->inm_ifma = ifma;
918 ifma->ifma_protospec = inm;
919 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
922 * Let IGMP know that we have joined a new IP multicast group.
930 * Delete a multicast address record.
933 in_delmulti(struct in_multi *inm)
935 struct ifmultiaddr *ifma;
936 struct in_multi my_inm;
939 ifma = inm->inm_ifma;
940 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
941 if (ifma->ifma_refcount == 1) {
943 * No remaining claims to this record; let IGMP know that
944 * we are leaving the multicast group.
945 * But do it after the if_delmulti() which might reset
946 * the interface and nuke the packet.
949 ifma->ifma_protospec = 0;
950 LIST_REMOVE(inm, inm_link);
951 kfree(inm, M_IPMADDR);
953 /* XXX - should be separate API for when we have an ifma? */
954 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
955 if (my_inm.inm_ifp != NULL)
956 igmp_leavegroup(&my_inm);
961 in_ifdetach(struct ifnet *ifp)
963 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
964 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);