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.40 2008/06/14 08:40:16 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 *, struct in_ifaddr *,
72 const 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);
97 struct in_ifaddr_container *iac;
100 if (subnetsarelocal) {
101 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
104 if ((i & ia->ia_netmask) == ia->ia_net)
108 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
111 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
119 * Determine whether an IP address is in a reserved set of addresses
120 * that may not be forwarded, or whether datagrams to that destination
124 in_canforward(struct in_addr in)
126 u_long i = ntohl(in.s_addr);
129 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
132 net = i & IN_CLASSA_NET;
133 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
140 * Trim a mask in a sockaddr
143 in_socktrim(struct sockaddr_in *ap)
145 char *cplim = (char *) &ap->sin_addr;
146 char *cp = (char *) (&ap->sin_addr + 1);
149 while (--cp >= cplim)
151 (ap)->sin_len = cp - (char *) (ap) + 1;
157 in_mask2len(struct in_addr *mask)
163 for (x = 0; x < sizeof *mask; x++) {
168 if (x < sizeof *mask) {
169 for (y = 0; y < 8; y++) {
170 if ((p[x] & (0x80 >> y)) == 0)
178 in_len2mask(struct in_addr *mask, int len)
184 bzero(mask, sizeof *mask);
185 for (i = 0; i < len / 8; i++)
188 p[i] = (0xff00 >> (len % 8)) & 0xff;
191 static int in_interfaces; /* number of external internet interfaces */
193 struct in_control_arg {
201 in_control_dispatch(struct netmsg *nmsg)
203 struct lwkt_msg *msg = &nmsg->nm_lmsg;
204 const struct in_control_arg *arg = msg->u.ms_resultp;
207 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
208 lwkt_replymsg(msg, error);
212 * Generic internet control operations (ioctl's).
213 * Ifp is 0 if not an interface-specific ioctl.
215 * NOTE! td might be NULL.
219 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
223 struct in_control_arg arg;
224 struct lwkt_msg *msg;
230 if (td && (error = suser(td)) != 0)
236 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
239 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
240 ("recursive SIOC%cLIFADDR!\n",
241 cmd == SIOCDLIFADDR ? 'D' : 'A'));
244 * IFADDR alterations are serialized by netisr0
253 bzero(&arg, sizeof(arg));
259 netmsg_init(&nmsg, &curthread->td_msgport, 0,
260 in_control_dispatch);
262 msg->u.ms_resultp = &arg;
264 lwkt_domsg(cpu_portfn(0), msg, 0);
265 return msg->ms_error;
267 return in_control_internal(cmd, data, ifp, td);
272 in_ialink_dispatch(struct netmsg *nmsg)
274 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
275 struct in_ifaddr *ia = lmsg->u.ms_resultp;
276 struct ifaddr_container *ifac;
277 struct in_ifaddr_container *iac;
282 ifac = &ia->ia_ifa.ifa_containers[cpu];
283 ASSERT_IFAC_VALID(ifac);
284 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
285 ("ia is on in_ifaddrheads\n"));
287 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
288 iac = &ifac->ifa_proto_u.u_in_ifac;
289 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
293 ifa_forwardmsg(lmsg, cpu + 1);
297 in_iaunlink_dispatch(struct netmsg *nmsg)
299 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
300 struct in_ifaddr *ia = lmsg->u.ms_resultp;
301 struct ifaddr_container *ifac;
302 struct in_ifaddr_container *iac;
307 ifac = &ia->ia_ifa.ifa_containers[cpu];
308 ASSERT_IFAC_VALID(ifac);
309 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
310 ("ia is not on in_ifaddrheads\n"));
312 iac = &ifac->ifa_proto_u.u_in_ifac;
313 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
314 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
318 ifa_forwardmsg(lmsg, cpu + 1);
322 in_iahashins_dispatch(struct netmsg *nmsg)
324 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
325 struct in_ifaddr *ia = lmsg->u.ms_resultp;
326 struct ifaddr_container *ifac;
327 struct in_ifaddr_container *iac;
332 ifac = &ia->ia_ifa.ifa_containers[cpu];
333 ASSERT_IFAC_VALID(ifac);
334 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
335 ("ia is on in_ifaddrhashtbls\n"));
337 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
338 iac = &ifac->ifa_proto_u.u_in_ifac;
339 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
344 ifa_forwardmsg(lmsg, cpu + 1);
348 in_iahashrem_dispatch(struct netmsg *nmsg)
350 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
351 struct in_ifaddr *ia = lmsg->u.ms_resultp;
352 struct ifaddr_container *ifac;
353 struct in_ifaddr_container *iac;
358 ifac = &ia->ia_ifa.ifa_containers[cpu];
359 ASSERT_IFAC_VALID(ifac);
360 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
361 ("ia is not on in_ifaddrhashtbls\n"));
363 iac = &ifac->ifa_proto_u.u_in_ifac;
364 LIST_REMOVE(iac, ia_hash);
365 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
369 ifa_forwardmsg(lmsg, cpu + 1);
373 in_ialink(struct in_ifaddr *ia)
376 struct lwkt_msg *lmsg;
378 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch);
379 lmsg = &nmsg.nm_lmsg;
380 lmsg->u.ms_resultp = ia;
386 in_iaunlink(struct in_ifaddr *ia)
389 struct lwkt_msg *lmsg;
391 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch);
392 lmsg = &nmsg.nm_lmsg;
393 lmsg->u.ms_resultp = ia;
399 in_iahash_insert(struct in_ifaddr *ia)
402 struct lwkt_msg *lmsg;
404 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashins_dispatch);
405 lmsg = &nmsg.nm_lmsg;
406 lmsg->u.ms_resultp = ia;
412 in_iahash_remove(struct in_ifaddr *ia)
415 struct lwkt_msg *lmsg;
417 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashrem_dispatch);
418 lmsg = &nmsg.nm_lmsg;
419 lmsg->u.ms_resultp = ia;
424 static __inline struct in_ifaddr *
425 in_ianext(struct in_ifaddr *oia)
427 struct ifaddr_container *ifac;
428 struct in_ifaddr_container *iac;
430 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
431 ASSERT_IFAC_VALID(ifac);
432 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
433 ("ia is not on in_ifaddrheads\n"));
435 iac = &ifac->ifa_proto_u.u_in_ifac;
436 iac = TAILQ_NEXT(iac, ia_link);
444 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
447 struct ifreq *ifr = (struct ifreq *)data;
448 struct in_ifaddr *ia = NULL;
450 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
451 struct ifaddr_container *ifac;
452 struct in_ifaddr_container *iac;
453 struct sockaddr_in oldaddr;
454 int hostIsNew, iaIsNew, maskIsNew;
460 * Find address for this interface, if it exists.
462 * If an alias address was specified, find that one instead of
463 * the first one on the interface, if possible
466 struct in_ifaddr *iap;
468 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
469 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
471 if (iap->ia_ifp == ifp &&
472 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
478 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
480 iap = ifatoia(ifac->ifa);
481 if (iap->ia_addr.sin_family == AF_INET) {
493 return (EADDRNOTAVAIL);
494 if (ifra->ifra_addr.sin_family == AF_INET) {
496 if (ia->ia_ifp == ifp &&
497 ia->ia_addr.sin_addr.s_addr ==
498 ifra->ifra_addr.sin_addr.s_addr)
502 if ((ifp->if_flags & IFF_POINTOPOINT) &&
503 cmd == SIOCAIFADDR &&
504 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
508 if (cmd == SIOCDIFADDR && ia == NULL)
509 return (EADDRNOTAVAIL);
514 if (td && (error = suser(td)) != 0)
518 return (EADDRNOTAVAIL);
520 if (cmd == SIOCSIFDSTADDR &&
521 (ifp->if_flags & IFF_POINTOPOINT) == 0)
528 ia = ifa_create(sizeof(*ia), M_WAITOK);
532 * Setup per-CPU information
534 for (i = 0; i < ncpus; ++i) {
535 ifac = &ifa->ifa_containers[i];
536 iac = &ifac->ifa_proto_u.u_in_ifac;
542 * Protect from NETISR_IP traversing address list
543 * while we're modifying it.
548 ifa_iflink(ifa, ifp, 1);
550 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
551 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
552 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
553 ia->ia_sockmask.sin_len = 8;
554 ia->ia_sockmask.sin_family = AF_INET;
555 if (ifp->if_flags & IFF_BROADCAST) {
556 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
557 ia->ia_broadaddr.sin_family = AF_INET;
560 if (!(ifp->if_flags & IFF_LOOPBACK))
569 if (td && (error = suser(td)) != 0)
578 return (EADDRNOTAVAIL);
584 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
588 if ((ifp->if_flags & IFF_BROADCAST) == 0)
590 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
594 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
596 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
600 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
604 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
606 oldaddr = ia->ia_dstaddr;
607 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
608 if (ifp->if_ioctl != NULL) {
609 lwkt_serialize_enter(ifp->if_serializer);
610 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
611 td->td_proc->p_ucred);
612 lwkt_serialize_exit(ifp->if_serializer);
614 ia->ia_dstaddr = oldaddr;
618 if (ia->ia_flags & IFA_ROUTE) {
619 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
620 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
621 ia->ia_ifa.ifa_dstaddr =
622 (struct sockaddr *)&ia->ia_dstaddr;
623 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
628 if ((ifp->if_flags & IFF_BROADCAST) == 0)
630 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
634 error = in_ifinit(ifp, ia,
635 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
636 if (error != 0 && iaIsNew)
639 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
643 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
644 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
651 if (ia->ia_addr.sin_family == AF_INET) {
652 if (ifra->ifra_addr.sin_len == 0) {
653 ifra->ifra_addr = ia->ia_addr;
655 } else if (ifra->ifra_addr.sin_addr.s_addr ==
656 ia->ia_addr.sin_addr.s_addr) {
660 if (ifra->ifra_mask.sin_len) {
662 ia->ia_sockmask = ifra->ifra_mask;
663 ia->ia_sockmask.sin_family = AF_INET;
665 ntohl(ia->ia_sockmask.sin_addr.s_addr);
668 if ((ifp->if_flags & IFF_POINTOPOINT) &&
669 ifra->ifra_dstaddr.sin_family == AF_INET) {
671 ia->ia_dstaddr = ifra->ifra_dstaddr;
672 maskIsNew = 1; /* We lie; but the effect's the same */
674 if (ifra->ifra_addr.sin_family == AF_INET &&
675 (hostIsNew || maskIsNew))
676 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
678 if (error != 0 && iaIsNew)
681 if ((ifp->if_flags & IFF_BROADCAST) &&
682 ifra->ifra_broadaddr.sin_family == AF_INET)
683 ia->ia_broadaddr = ifra->ifra_broadaddr;
685 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
690 * in_ifscrub kills the interface route.
694 * in_ifadown gets rid of all the rest of
695 * the routes. This is not quite the right
696 * thing to do, but at least if we are running
697 * a routing process they will come back.
699 in_ifadown(&ia->ia_ifa, 1);
700 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
705 if (ifp == NULL || ifp->if_ioctl == NULL)
707 lwkt_serialize_enter(ifp->if_serializer);
708 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
709 lwkt_serialize_exit(ifp->if_serializer);
713 KKASSERT(cmd == SIOCDIFADDR ||
714 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
716 ifa_ifunlink(&ia->ia_ifa, ifp);
719 if (cmd == SIOCDIFADDR) {
720 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
721 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
722 in_iahash_remove(ia);
727 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
728 * already taken care of the deletion from hash table
730 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
731 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
732 ("SIOC%cIFADDR failed on new ia, "
733 "but the new ia is still in hash table\n",
734 cmd == SIOCSIFADDR ? 'S' : 'A'));
738 ifa_destroy(&ia->ia_ifa);
745 * SIOCGLIFADDR: get first address. (?!?)
746 * SIOCGLIFADDR with IFLR_PREFIX:
747 * get first address that matches the specified prefix.
748 * SIOCALIFADDR: add the specified address.
749 * SIOCALIFADDR with IFLR_PREFIX:
750 * EINVAL since we can't deduce hostid part of the address.
751 * SIOCDLIFADDR: delete the specified address.
752 * SIOCDLIFADDR with IFLR_PREFIX:
753 * delete the first address that matches the specified prefix.
755 * EINVAL on invalid parameters
756 * EADDRNOTAVAIL on prefix match failed/specified address not found
757 * other values may be returned from in_ioctl()
759 * NOTE! td might be NULL.
762 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
765 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
769 panic("invalid argument to in_lifaddr_ioctl");
775 /* address must be specified on GET with IFLR_PREFIX */
776 if ((iflr->flags & IFLR_PREFIX) == 0)
781 /* address must be specified on ADD and DELETE */
782 if (iflr->addr.ss_family != AF_INET)
784 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
786 /* XXX need improvement */
787 if (iflr->dstaddr.ss_family
788 && iflr->dstaddr.ss_family != AF_INET)
790 if (iflr->dstaddr.ss_family
791 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
794 default: /*shouldn't happen*/
797 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
803 struct in_aliasreq ifra;
805 if (iflr->flags & IFLR_PREFIX)
808 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
809 bzero(&ifra, sizeof ifra);
810 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
812 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
814 if (iflr->dstaddr.ss_family) { /*XXX*/
815 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
816 iflr->dstaddr.ss_len);
819 ifra.ifra_mask.sin_family = AF_INET;
820 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
821 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
823 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
828 struct ifaddr_container *ifac;
829 struct in_ifaddr *ia;
830 struct in_addr mask, candidate, match;
831 struct sockaddr_in *sin;
834 bzero(&mask, sizeof mask);
835 if (iflr->flags & IFLR_PREFIX) {
836 /* lookup a prefix rather than address. */
837 in_len2mask(&mask, iflr->prefixlen);
839 sin = (struct sockaddr_in *)&iflr->addr;
840 match.s_addr = sin->sin_addr.s_addr;
841 match.s_addr &= mask.s_addr;
843 /* if you set extra bits, that's wrong */
844 if (match.s_addr != sin->sin_addr.s_addr)
849 if (cmd == SIOCGLIFADDR) {
850 /* on getting an address, take the 1st match */
851 match.s_addr = 0; /* gcc4 warning */
854 /* on deleting an address, do exact match */
855 in_len2mask(&mask, 32);
856 sin = (struct sockaddr_in *)&iflr->addr;
857 match.s_addr = sin->sin_addr.s_addr;
863 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
864 struct ifaddr *ifa = ifac->ifa;
866 if (ifa->ifa_addr->sa_family != AF_INET6)
871 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
872 candidate.s_addr &= mask.s_addr;
873 if (candidate.s_addr == match.s_addr)
877 return EADDRNOTAVAIL;
878 ia = (struct in_ifaddr *)(ifac->ifa);
880 if (cmd == SIOCGLIFADDR) {
881 /* fill in the if_laddrreq structure */
882 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
884 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
885 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
886 ia->ia_dstaddr.sin_len);
888 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
891 in_mask2len(&ia->ia_sockmask.sin_addr);
893 iflr->flags = 0; /*XXX*/
897 struct in_aliasreq ifra;
899 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
900 bzero(&ifra, sizeof ifra);
901 bcopy(iflr->iflr_name, ifra.ifra_name,
902 sizeof ifra.ifra_name);
904 bcopy(&ia->ia_addr, &ifra.ifra_addr,
905 ia->ia_addr.sin_len);
906 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
907 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
908 ia->ia_dstaddr.sin_len);
910 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
911 ia->ia_sockmask.sin_len);
913 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
919 return EOPNOTSUPP; /*just for safety*/
923 * Delete any existing route for an interface.
926 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
929 if ((ia->ia_flags & IFA_ROUTE) == 0)
931 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
932 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
934 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
935 ia->ia_flags &= ~IFA_ROUTE;
939 * Initialize an interface's internet address
940 * and routing table entry.
943 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
944 const struct sockaddr_in *sin, int scrub)
946 u_long i = ntohl(sin->sin_addr.s_addr);
947 struct sockaddr_in oldaddr;
948 struct ifaddr_container *ifac;
949 int flags = RTF_UP, error = 0;
952 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
953 oldaddr = ia->ia_addr;
955 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
957 in_iahash_remove(ia);
961 if (ia->ia_addr.sin_family == AF_INET)
962 in_iahash_insert(ia);
965 * Give the interface a chance to initialize
966 * if this is its first address,
967 * and to validate the address if necessary.
969 if (ifp->if_ioctl != NULL) {
970 lwkt_serialize_enter(ifp->if_serializer);
971 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
972 lwkt_serialize_exit(ifp->if_serializer);
978 * Delete old route, if requested.
981 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
983 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
987 * Calculate netmask/subnetmask.
990 ia->ia_netmask = IN_CLASSA_NET;
991 else if (IN_CLASSB(i))
992 ia->ia_netmask = IN_CLASSB_NET;
994 ia->ia_netmask = IN_CLASSC_NET;
996 * The subnet mask usually includes at least the standard network part,
997 * but may may be smaller in the case of supernetting.
998 * If it is set, we believe it.
1000 if (ia->ia_subnetmask == 0) {
1001 ia->ia_subnetmask = ia->ia_netmask;
1002 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1004 ia->ia_netmask &= ia->ia_subnetmask;
1006 ia->ia_net = i & ia->ia_netmask;
1007 ia->ia_subnet = i & ia->ia_subnetmask;
1008 in_socktrim(&ia->ia_sockmask);
1011 * Add route for the network.
1013 ia->ia_ifa.ifa_metric = ifp->if_metric;
1014 if (ifp->if_flags & IFF_BROADCAST) {
1015 ia->ia_broadaddr.sin_addr.s_addr =
1016 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1017 ia->ia_netbroadcast.s_addr =
1018 htonl(ia->ia_net | ~ ia->ia_netmask);
1019 } else if (ifp->if_flags & IFF_LOOPBACK) {
1020 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1022 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1023 if (ia->ia_dstaddr.sin_family != AF_INET)
1029 * Don't add host routes for interface addresses of
1030 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1031 * possible to assign several such address pairs with consistent
1032 * results (no host route) and is required by BOOTP.
1034 * XXX: This is ugly ! There should be a way for the caller to
1035 * say that they don't want a host route.
1037 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1038 ia->ia_netmask != IN_CLASSA_NET ||
1039 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1040 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, flags)) != 0)
1042 ia->ia_flags |= IFA_ROUTE;
1046 * If the interface supports multicast, join the "all hosts"
1047 * multicast group on that interface.
1049 if (ifp->if_flags & IFF_MULTICAST) {
1050 struct in_addr addr;
1052 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1053 in_addmulti(&addr, ifp);
1057 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1058 in_iahash_remove(ia);
1060 ia->ia_addr = oldaddr;
1062 in_iahash_insert(ia);
1068 * Return 1 if the address might be a local broadcast address.
1071 in_broadcast(struct in_addr in, struct ifnet *ifp)
1073 struct ifaddr_container *ifac;
1076 if (in.s_addr == INADDR_BROADCAST ||
1077 in.s_addr == INADDR_ANY)
1079 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1081 t = ntohl(in.s_addr);
1083 * Look through the list of addresses for a match
1084 * with a broadcast address.
1086 #define ia ((struct in_ifaddr *)ifa)
1087 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1088 struct ifaddr *ifa = ifac->ifa;
1090 if (ifa->ifa_addr->sa_family == AF_INET &&
1091 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1092 in.s_addr == ia->ia_netbroadcast.s_addr ||
1094 * Check for old-style (host 0) broadcast.
1096 t == ia->ia_subnet || t == ia->ia_net) &&
1098 * Check for an all one subnetmask. These
1099 * only exist when an interface gets a secondary
1102 ia->ia_subnetmask != (u_long)0xffffffff)
1109 * Add an address to the list of IP multicast addresses for a given interface.
1112 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1114 struct in_multi *inm;
1116 struct sockaddr_in sin;
1117 struct ifmultiaddr *ifma;
1120 * Call generic routine to add membership or increment
1121 * refcount. It wants addresses in the form of a sockaddr,
1122 * so we build one here (being careful to zero the unused bytes).
1124 bzero(&sin, sizeof sin);
1125 sin.sin_family = AF_INET;
1126 sin.sin_len = sizeof sin;
1129 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1136 * If ifma->ifma_protospec is null, then if_addmulti() created
1137 * a new record. Otherwise, we are done.
1139 if (ifma->ifma_protospec != 0) {
1141 return ifma->ifma_protospec;
1144 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1145 at interrupt time? If so, need to fix if_addmulti. XXX */
1146 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1147 inm->inm_addr = *ap;
1149 inm->inm_ifma = ifma;
1150 ifma->ifma_protospec = inm;
1151 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1154 * Let IGMP know that we have joined a new IP multicast group.
1156 igmp_joingroup(inm);
1162 * Delete a multicast address record.
1165 in_delmulti(struct in_multi *inm)
1167 struct ifmultiaddr *ifma;
1168 struct in_multi my_inm;
1171 ifma = inm->inm_ifma;
1172 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1173 if (ifma->ifma_refcount == 1) {
1175 * No remaining claims to this record; let IGMP know that
1176 * we are leaving the multicast group.
1177 * But do it after the if_delmulti() which might reset
1178 * the interface and nuke the packet.
1181 ifma->ifma_protospec = 0;
1182 LIST_REMOVE(inm, inm_link);
1183 kfree(inm, M_IPMADDR);
1185 /* XXX - should be separate API for when we have an ifma? */
1186 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1187 if (my_inm.inm_ifp != NULL)
1188 igmp_leavegroup(&my_inm);
1193 in_ifdetach(struct ifnet *ifp)
1195 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1196 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);