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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)in.c 8.4 (Berkeley) 1/9/95
30 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
33 #include "opt_bootp.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
42 #include <sys/msgport.h>
43 #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>
52 #include <net/netmsg2.h>
53 #include <net/netisr2.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/udp_var.h>
60 #include <netinet/igmp_var.h>
62 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
64 static int in_mask2len (struct in_addr *);
65 static void in_len2mask (struct in_addr *, int);
66 static int in_lifaddr_ioctl (u_long, caddr_t, struct ifnet *, struct thread *);
68 static void in_socktrim (struct sockaddr_in *);
69 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
70 const struct sockaddr_in *, int);
72 static int in_control_internal(u_long, caddr_t, struct ifnet *,
75 static int in_addprefix(struct in_ifaddr *, int);
76 static void in_scrubprefix(struct in_ifaddr *);
78 static int subnetsarelocal = 0;
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
81 "Count all internet addresses of subnets of the local net as local");
83 struct in_multihead in_multihead; /* XXX BSS initialization */
85 extern struct inpcbinfo ripcbinfo;
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 */
194 in_control_dispatch(netmsg_t msg)
198 error = in_control(msg->control.nm_cmd, msg->control.nm_data,
199 msg->control.nm_ifp, msg->control.nm_td);
200 lwkt_replymsg(&msg->lmsg, error);
204 in_control_internal_dispatch(netmsg_t msg)
208 error = in_control_internal(msg->control.nm_cmd,
209 msg->control.nm_data,
212 lwkt_replymsg(&msg->lmsg, error);
216 * Generic internet control operations (ioctl's).
217 * Ifp is 0 if not an interface-specific ioctl.
219 * NOTE! td might be NULL.
222 in_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
224 struct netmsg_pru_control msg;
237 * Dispatch these SIOCs to netisr0.
239 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
240 in_control_internal_dispatch);
245 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
246 return msg.base.lmsg.ms_error;
249 return in_control_internal(cmd, data, ifp, td);
254 in_ialink_dispatch(netmsg_t msg)
256 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
257 struct ifaddr_container *ifac;
258 struct in_ifaddr_container *iac;
263 ifac = &ia->ia_ifa.ifa_containers[cpu];
264 ASSERT_IFAC_VALID(ifac);
265 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
266 ("ia is on in_ifaddrheads"));
268 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
269 iac = &ifac->ifa_proto_u.u_in_ifac;
270 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
274 ifa_forwardmsg(&msg->lmsg, cpu + 1);
278 in_iaunlink_dispatch(netmsg_t msg)
280 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
281 struct ifaddr_container *ifac;
282 struct in_ifaddr_container *iac;
287 ifac = &ia->ia_ifa.ifa_containers[cpu];
288 ASSERT_IFAC_VALID(ifac);
289 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
290 ("ia is not on in_ifaddrheads"));
292 iac = &ifac->ifa_proto_u.u_in_ifac;
293 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
294 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
298 ifa_forwardmsg(&msg->lmsg, cpu + 1);
302 in_iahashins_dispatch(netmsg_t msg)
304 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
305 struct ifaddr_container *ifac;
306 struct in_ifaddr_container *iac;
311 ifac = &ia->ia_ifa.ifa_containers[cpu];
312 ASSERT_IFAC_VALID(ifac);
313 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
314 ("ia is on in_ifaddrhashtbls"));
316 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
317 iac = &ifac->ifa_proto_u.u_in_ifac;
318 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
323 ifa_forwardmsg(&msg->lmsg, cpu + 1);
327 in_iahashrem_dispatch(netmsg_t msg)
329 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
330 struct ifaddr_container *ifac;
331 struct in_ifaddr_container *iac;
336 ifac = &ia->ia_ifa.ifa_containers[cpu];
337 ASSERT_IFAC_VALID(ifac);
338 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
339 ("ia is not on in_ifaddrhashtbls"));
341 iac = &ifac->ifa_proto_u.u_in_ifac;
342 LIST_REMOVE(iac, ia_hash);
343 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
347 ifa_forwardmsg(&msg->lmsg, cpu + 1);
351 in_ialink(struct in_ifaddr *ia)
353 struct netmsg_base msg;
355 netmsg_init(&msg, NULL, &curthread->td_msgport,
356 0, in_ialink_dispatch);
357 msg.lmsg.u.ms_resultp = ia;
359 ifa_domsg(&msg.lmsg, 0);
363 in_iaunlink(struct in_ifaddr *ia)
365 struct netmsg_base msg;
367 netmsg_init(&msg, NULL, &curthread->td_msgport,
368 0, in_iaunlink_dispatch);
369 msg.lmsg.u.ms_resultp = ia;
371 ifa_domsg(&msg.lmsg, 0);
375 in_iahash_insert(struct in_ifaddr *ia)
377 struct netmsg_base msg;
379 netmsg_init(&msg, NULL, &curthread->td_msgport,
380 0, in_iahashins_dispatch);
381 msg.lmsg.u.ms_resultp = ia;
383 ifa_domsg(&msg.lmsg, 0);
387 in_iahash_remove(struct in_ifaddr *ia)
389 struct netmsg_base msg;
391 netmsg_init(&msg, NULL, &curthread->td_msgport,
392 0, in_iahashrem_dispatch);
393 msg.lmsg.u.ms_resultp = ia;
395 ifa_domsg(&msg.lmsg, 0);
398 static __inline struct in_ifaddr *
399 in_ianext(struct in_ifaddr *oia)
401 struct ifaddr_container *ifac;
402 struct in_ifaddr_container *iac;
404 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
405 ASSERT_IFAC_VALID(ifac);
406 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
407 ("ia is not on in_ifaddrheads"));
409 iac = &ifac->ifa_proto_u.u_in_ifac;
410 iac = TAILQ_NEXT(iac, ia_link);
418 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
421 struct ifreq *ifr = (struct ifreq *)data;
422 struct in_ifaddr *ia = NULL;
424 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
425 struct ifaddr_container *ifac;
426 struct in_ifaddr_container *iac;
427 struct sockaddr_in oldaddr;
428 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
434 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
440 return in_lifaddr_ioctl(cmd, data, ifp, td);
447 * Find address for this interface, if it exists.
449 * If an alias address was specified, find that one instead of
450 * the first one on the interface, if possible
453 struct in_ifaddr *iap;
455 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
456 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
458 if (iap->ia_ifp == ifp &&
459 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
465 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
467 iap = ifatoia(ifac->ifa);
468 if (iap->ia_addr.sin_family == AF_INET) {
475 if (ifp->if_flags & IFF_UP)
483 return (EADDRNOTAVAIL);
484 if (ifra->ifra_addr.sin_family == AF_INET) {
486 if (ia->ia_ifp == ifp &&
487 ia->ia_addr.sin_addr.s_addr ==
488 ifra->ifra_addr.sin_addr.s_addr)
492 if ((ifp->if_flags & IFF_POINTOPOINT) &&
493 cmd == SIOCAIFADDR &&
494 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
498 if (cmd == SIOCDIFADDR && ia == NULL)
499 return (EADDRNOTAVAIL);
504 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
508 return (EADDRNOTAVAIL);
510 if (cmd == SIOCSIFDSTADDR &&
511 (ifp->if_flags & IFF_POINTOPOINT) == 0)
518 ia = ifa_create(sizeof(*ia), M_WAITOK);
522 * Setup per-CPU information
524 for (i = 0; i < ncpus; ++i) {
525 ifac = &ifa->ifa_containers[i];
526 iac = &ifac->ifa_proto_u.u_in_ifac;
532 * Protect from NETISR_IP traversing address list
533 * while we're modifying it.
538 ifa_iflink(ifa, ifp, 1);
540 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
541 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
542 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
543 ia->ia_sockmask.sin_len = 8;
544 ia->ia_sockmask.sin_family = AF_INET;
545 if (ifp->if_flags & IFF_BROADCAST) {
546 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
547 ia->ia_broadaddr.sin_family = AF_INET;
550 if (!(ifp->if_flags & IFF_LOOPBACK))
559 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
568 return (EADDRNOTAVAIL);
574 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
578 if ((ifp->if_flags & IFF_BROADCAST) == 0)
580 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
584 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
586 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
590 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
594 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
596 oldaddr = ia->ia_dstaddr;
597 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
598 if (ifp->if_ioctl != NULL) {
599 ifnet_serialize_all(ifp);
600 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
601 td->td_proc->p_ucred);
602 ifnet_deserialize_all(ifp);
604 ia->ia_dstaddr = oldaddr;
608 if (ia->ia_flags & IFA_ROUTE) {
609 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
610 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
611 ia->ia_ifa.ifa_dstaddr =
612 (struct sockaddr *)&ia->ia_dstaddr;
613 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
618 if ((ifp->if_flags & IFF_BROADCAST) == 0)
620 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
624 error = in_ifinit(ifp, ia,
625 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
626 if (error != 0 && iaIsNew)
629 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
630 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
633 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
635 * Interface is brought up by in_ifinit()
636 * (via ifp->if_ioctl). We act as if the
637 * interface got IFF_UP flag turned on.
644 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
645 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
652 if (ia->ia_addr.sin_family == AF_INET) {
653 if (ifra->ifra_addr.sin_len == 0) {
654 ifra->ifra_addr = ia->ia_addr;
656 } else if (ifra->ifra_addr.sin_addr.s_addr ==
657 ia->ia_addr.sin_addr.s_addr) {
661 if (ifra->ifra_mask.sin_len) {
663 ia->ia_sockmask = ifra->ifra_mask;
664 ia->ia_sockmask.sin_family = AF_INET;
666 ntohl(ia->ia_sockmask.sin_addr.s_addr);
669 if ((ifp->if_flags & IFF_POINTOPOINT) &&
670 ifra->ifra_dstaddr.sin_family == AF_INET) {
672 ia->ia_dstaddr = ifra->ifra_dstaddr;
673 maskIsNew = 1; /* We lie; but the effect's the same */
675 if (ifra->ifra_addr.sin_family == AF_INET &&
676 (hostIsNew || maskIsNew))
677 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
679 if (error != 0 && iaIsNew)
682 if ((ifp->if_flags & IFF_BROADCAST) &&
683 ifra->ifra_broadaddr.sin_family == AF_INET)
684 ia->ia_broadaddr = ifra->ifra_broadaddr;
686 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
687 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
690 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
691 /* See the comment in SIOCSIFADDR */
698 * in_ifscrub kills the interface route.
702 * in_ifadown gets rid of all the rest of
703 * the routes. This is not quite the right
704 * thing to do, but at least if we are running
705 * a routing process they will come back.
707 in_ifadown(&ia->ia_ifa, 1);
708 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
714 if (ifp == NULL || ifp->if_ioctl == NULL)
716 ifnet_serialize_all(ifp);
717 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
718 ifnet_deserialize_all(ifp);
722 KKASSERT(cmd == SIOCDIFADDR ||
723 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
725 ifa_ifunlink(&ia->ia_ifa, ifp);
728 if (cmd == SIOCDIFADDR) {
729 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
730 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
731 in_iahash_remove(ia);
736 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
737 * already taken care of the deletion from hash table
739 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
740 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
741 ("SIOC%cIFADDR failed on new ia, "
742 "but the new ia is still in hash table",
743 cmd == SIOCSIFADDR ? 'S' : 'A'));
747 ifa_destroy(&ia->ia_ifa);
749 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
750 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
752 * Though the address assignment failed, the
753 * interface is brought up by in_ifinit()
754 * (via ifp->if_ioctl). With the hope that
755 * the interface has some valid addresses, we
756 * act as if IFF_UP flag was just set on the
760 * This could only be done after the failed
761 * address is unlinked from the global address
772 * SIOCGLIFADDR: get first address. (?!?)
773 * SIOCGLIFADDR with IFLR_PREFIX:
774 * get first address that matches the specified prefix.
775 * SIOCALIFADDR: add the specified address.
776 * SIOCALIFADDR with IFLR_PREFIX:
777 * EINVAL since we can't deduce hostid part of the address.
778 * SIOCDLIFADDR: delete the specified address.
779 * SIOCDLIFADDR with IFLR_PREFIX:
780 * delete the first address that matches the specified prefix.
782 * EINVAL on invalid parameters
783 * EADDRNOTAVAIL on prefix match failed/specified address not found
784 * other values may be returned from in_ioctl()
786 * NOTE! td might be NULL.
789 in_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
791 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
795 panic("invalid argument to in_lifaddr_ioctl");
801 /* address must be specified on GET with IFLR_PREFIX */
802 if ((iflr->flags & IFLR_PREFIX) == 0)
807 /* address must be specified on ADD and DELETE */
808 if (iflr->addr.ss_family != AF_INET)
810 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
812 /* XXX need improvement */
813 if (iflr->dstaddr.ss_family
814 && iflr->dstaddr.ss_family != AF_INET)
816 if (iflr->dstaddr.ss_family
817 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
820 default: /*shouldn't happen*/
823 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
829 struct in_aliasreq ifra;
831 if (iflr->flags & IFLR_PREFIX)
834 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
835 bzero(&ifra, sizeof ifra);
836 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
838 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
840 if (iflr->dstaddr.ss_family) { /*XXX*/
841 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
842 iflr->dstaddr.ss_len);
845 ifra.ifra_mask.sin_family = AF_INET;
846 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
847 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
849 return in_control_internal(SIOCAIFADDR, (caddr_t)&ifra, ifp,
855 struct ifaddr_container *ifac;
856 struct in_ifaddr *ia;
857 struct in_addr mask, candidate, match;
858 struct sockaddr_in *sin;
861 bzero(&mask, sizeof mask);
862 if (iflr->flags & IFLR_PREFIX) {
863 /* lookup a prefix rather than address. */
864 in_len2mask(&mask, iflr->prefixlen);
866 sin = (struct sockaddr_in *)&iflr->addr;
867 match.s_addr = sin->sin_addr.s_addr;
868 match.s_addr &= mask.s_addr;
870 /* if you set extra bits, that's wrong */
871 if (match.s_addr != sin->sin_addr.s_addr)
876 if (cmd == SIOCGLIFADDR) {
877 /* on getting an address, take the 1st match */
878 match.s_addr = 0; /* gcc4 warning */
881 /* on deleting an address, do exact match */
882 in_len2mask(&mask, 32);
883 sin = (struct sockaddr_in *)&iflr->addr;
884 match.s_addr = sin->sin_addr.s_addr;
890 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
891 struct ifaddr *ifa = ifac->ifa;
893 if (ifa->ifa_addr->sa_family != AF_INET6)
898 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
899 candidate.s_addr &= mask.s_addr;
900 if (candidate.s_addr == match.s_addr)
904 return EADDRNOTAVAIL;
905 ia = (struct in_ifaddr *)(ifac->ifa);
907 if (cmd == SIOCGLIFADDR) {
908 /* fill in the if_laddrreq structure */
909 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
911 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
912 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
913 ia->ia_dstaddr.sin_len);
915 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
918 in_mask2len(&ia->ia_sockmask.sin_addr);
920 iflr->flags = 0; /*XXX*/
924 struct in_aliasreq ifra;
926 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
927 bzero(&ifra, sizeof ifra);
928 bcopy(iflr->iflr_name, ifra.ifra_name,
929 sizeof ifra.ifra_name);
931 bcopy(&ia->ia_addr, &ifra.ifra_addr,
932 ia->ia_addr.sin_len);
933 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
934 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
935 ia->ia_dstaddr.sin_len);
937 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
938 ia->ia_sockmask.sin_len);
940 return in_control_internal(SIOCDIFADDR, (caddr_t)&ifra,
946 return EOPNOTSUPP; /*just for safety*/
950 * Delete any existing route for an interface.
953 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
959 * Initialize an interface's internet address
960 * and routing table entry.
963 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
964 const struct sockaddr_in *sin, int scrub)
966 u_long i = ntohl(sin->sin_addr.s_addr);
967 struct sockaddr_in oldaddr;
968 struct ifaddr_container *ifac;
969 int flags = RTF_UP, error = 0;
972 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
973 oldaddr = ia->ia_addr;
975 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
977 in_iahash_remove(ia);
981 if (ia->ia_addr.sin_family == AF_INET)
982 in_iahash_insert(ia);
985 * Give the interface a chance to initialize
986 * if this is its first address,
987 * and to validate the address if necessary.
989 if (ifp->if_ioctl != NULL) {
990 ifnet_serialize_all(ifp);
991 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
992 ifnet_deserialize_all(ifp);
998 * Delete old route, if requested.
1001 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1002 in_ifscrub(ifp, ia);
1003 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1007 * Calculate netmask/subnetmask.
1010 ia->ia_netmask = IN_CLASSA_NET;
1011 else if (IN_CLASSB(i))
1012 ia->ia_netmask = IN_CLASSB_NET;
1014 ia->ia_netmask = IN_CLASSC_NET;
1016 * The subnet mask usually includes at least the standard network part,
1017 * but may may be smaller in the case of supernetting.
1018 * If it is set, we believe it.
1020 if (ia->ia_subnetmask == 0) {
1021 ia->ia_subnetmask = ia->ia_netmask;
1022 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1024 ia->ia_netmask &= ia->ia_subnetmask;
1026 ia->ia_net = i & ia->ia_netmask;
1027 ia->ia_subnet = i & ia->ia_subnetmask;
1028 in_socktrim(&ia->ia_sockmask);
1031 * Add route for the network.
1033 ia->ia_ifa.ifa_metric = ifp->if_metric;
1034 if (ifp->if_flags & IFF_BROADCAST) {
1035 ia->ia_broadaddr.sin_addr.s_addr =
1036 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1037 ia->ia_netbroadcast.s_addr =
1038 htonl(ia->ia_net | ~ ia->ia_netmask);
1039 } else if (ifp->if_flags & IFF_LOOPBACK) {
1040 ia->ia_dstaddr = ia->ia_addr;
1042 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1043 if (ia->ia_dstaddr.sin_family != AF_INET)
1049 * Don't add host routes for interface addresses of
1050 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1051 * possible to assign several such address pairs with consistent
1052 * results (no host route) and is required by BOOTP.
1054 * XXX: This is ugly ! There should be a way for the caller to
1055 * say that they don't want a host route.
1057 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1058 ia->ia_netmask != IN_CLASSA_NET ||
1059 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1060 error = in_addprefix(ia, flags);
1066 * If the interface supports multicast, join the "all hosts"
1067 * multicast group on that interface.
1069 if (ifp->if_flags & IFF_MULTICAST) {
1070 struct in_addr addr;
1072 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1073 in_addmulti(&addr, ifp);
1077 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1078 in_iahash_remove(ia);
1080 ia->ia_addr = oldaddr;
1082 in_iahash_insert(ia);
1086 #define rtinitflags(x) \
1087 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1091 * Add a route to prefix ("connected route" in cisco terminology).
1092 * Do nothing, if there are some interface addresses with the same
1093 * prefix already. This function assumes that the 'target' parent
1097 in_addprefix(struct in_ifaddr *target, int flags)
1099 struct in_ifaddr_container *iac;
1100 struct in_addr prefix, mask;
1105 * Don't add prefix routes for CARP interfaces.
1106 * Prefix routes creation is handled by CARP
1107 * interfaces themselves.
1109 if (target->ia_ifp->if_type == IFT_CARP)
1113 mask = target->ia_sockmask.sin_addr;
1114 if (flags & RTF_HOST) {
1115 prefix = target->ia_dstaddr.sin_addr;
1117 prefix = target->ia_addr.sin_addr;
1118 prefix.s_addr &= mask.s_addr;
1121 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1122 struct in_ifaddr *ia = iac->ia;
1125 /* Don't test against self */
1129 /* The tested address does not own a route entry */
1130 if ((ia->ia_flags & IFA_ROUTE) == 0)
1134 if (rtinitflags(ia)) {
1135 p = ia->ia_dstaddr.sin_addr;
1137 p = ia->ia_addr.sin_addr;
1138 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1140 if (prefix.s_addr != p.s_addr)
1144 * If the to-be-added address and the curretly being
1145 * tested address are not host addresses, we need to
1146 * take subnetmask into consideration.
1148 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1149 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1153 * If we got a matching prefix route inserted by other
1154 * interface address, we don't need to bother.
1160 * No one seem to have prefix route; insert it.
1162 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1164 target->ia_flags |= IFA_ROUTE;
1169 * Remove a route to prefix ("connected route" in cisco terminology).
1170 * Re-installs the route by using another interface address, if there's
1171 * one with the same prefix (otherwise we lose the route mistakenly).
1174 in_scrubprefix(struct in_ifaddr *target)
1176 struct in_ifaddr_container *iac;
1177 struct in_addr prefix, mask;
1182 * Don't scrub prefix routes for CARP interfaces.
1183 * Prefix routes deletion is handled by CARP
1184 * interfaces themselves.
1186 if (target->ia_ifp->if_type == IFT_CARP)
1190 if ((target->ia_flags & IFA_ROUTE) == 0)
1193 mask = target->ia_sockmask.sin_addr;
1194 if (rtinitflags(target)) {
1195 prefix = target->ia_dstaddr.sin_addr;
1197 prefix = target->ia_addr.sin_addr;
1198 prefix.s_addr &= mask.s_addr;
1201 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1202 struct in_ifaddr *ia = iac->ia;
1205 /* Don't test against self */
1209 /* The tested address already owns a route entry */
1210 if (ia->ia_flags & IFA_ROUTE)
1214 * The prefix route of the tested address should
1215 * never be installed if its parent interface is
1218 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1223 * Don't add prefix routes for CARP interfaces.
1224 * Prefix routes creation is handled by CARP
1225 * interfaces themselves.
1227 if (ia->ia_ifp->if_type == IFT_CARP)
1232 if (rtinitflags(ia)) {
1233 p = ia->ia_dstaddr.sin_addr;
1235 p = ia->ia_addr.sin_addr;
1236 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1238 if (prefix.s_addr != p.s_addr)
1242 * We don't need to test subnetmask here, as what we do
1243 * in in_addprefix(), since if the the tested address's
1244 * parent interface is UP, the tested address should own
1245 * a prefix route entry and we would never reach here.
1249 * If we got a matching prefix route, move IFA_ROUTE to him
1251 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1252 target->ia_flags &= ~IFA_ROUTE;
1254 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1256 ia->ia_flags |= IFA_ROUTE;
1261 * No candidates for this prefix route; just remove it.
1263 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1264 target->ia_flags &= ~IFA_ROUTE;
1270 * Return 1 if the address might be a local broadcast address.
1273 in_broadcast(struct in_addr in, struct ifnet *ifp)
1275 struct ifaddr_container *ifac;
1278 if (in.s_addr == INADDR_BROADCAST ||
1279 in.s_addr == INADDR_ANY)
1281 if (ifp == NULL || (ifp->if_flags & IFF_BROADCAST) == 0)
1283 t = ntohl(in.s_addr);
1285 * Look through the list of addresses for a match
1286 * with a broadcast address.
1288 #define ia ((struct in_ifaddr *)ifa)
1289 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1290 struct ifaddr *ifa = ifac->ifa;
1292 if (ifa->ifa_addr->sa_family == AF_INET &&
1293 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1294 in.s_addr == ia->ia_netbroadcast.s_addr ||
1296 * Check for old-style (host 0) broadcast.
1298 t == ia->ia_subnet || t == ia->ia_net) &&
1300 * Check for an all one subnetmask. These
1301 * only exist when an interface gets a secondary
1304 ia->ia_subnetmask != (u_long)0xffffffff)
1312 * Add an address to the list of IP multicast addresses for a given interface.
1315 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1317 struct in_multi *inm;
1319 struct sockaddr_in sin;
1320 struct ifmultiaddr *ifma;
1322 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1323 ("in_addmulti is not called in netisr0"));
1326 * Call generic routine to add membership or increment
1327 * refcount. It wants addresses in the form of a sockaddr,
1328 * so we build one here (being careful to zero the unused bytes).
1330 bzero(&sin, sizeof sin);
1331 sin.sin_family = AF_INET;
1332 sin.sin_len = sizeof sin;
1334 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1339 * If ifma->ifma_protospec is null, then if_addmulti() created
1340 * a new record. Otherwise, we are done.
1342 if (ifma->ifma_protospec != NULL)
1343 return ifma->ifma_protospec;
1345 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1346 inm->inm_addr = *ap;
1348 inm->inm_ifma = ifma;
1349 ifma->ifma_protospec = inm;
1350 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1353 * Let IGMP know that we have joined a new IP multicast group.
1355 igmp_joingroup(inm);
1360 * Delete a multicast address record.
1363 in_delmulti(struct in_multi *inm)
1365 struct ifmultiaddr *ifma;
1366 struct in_multi my_inm;
1368 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1369 ("in_delmulti is not called in netisr0"));
1371 ifma = inm->inm_ifma;
1372 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1373 if (ifma->ifma_refcount == 1) {
1375 * No remaining claims to this record; let IGMP know that
1376 * we are leaving the multicast group.
1377 * But do it after the if_delmulti() which might reset
1378 * the interface and nuke the packet.
1381 ifma->ifma_protospec = NULL;
1382 LIST_REMOVE(inm, inm_link);
1383 kfree(inm, M_IPMADDR);
1385 /* XXX - should be separate API for when we have an ifma? */
1386 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1387 if (my_inm.inm_ifp != NULL)
1388 igmp_leavegroup(&my_inm);
1392 in_ifdetach_dispatch(netmsg_t nmsg)
1394 struct lwkt_msg *lmsg = &nmsg->lmsg;
1395 struct ifnet *ifp = lmsg->u.ms_resultp;
1398 in_pcbpurgeif0(&ripcbinfo, ifp);
1399 for (cpu = 0; cpu < ncpus2; ++cpu)
1400 in_pcbpurgeif0(&udbinfo[cpu], ifp);
1402 lwkt_replymsg(lmsg, 0);
1406 in_ifdetach(struct ifnet *ifp)
1408 struct netmsg_base nmsg;
1409 struct lwkt_msg *lmsg = &nmsg.lmsg;
1411 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1412 in_ifdetach_dispatch);
1413 lmsg->u.ms_resultp = ifp;
1415 lwkt_domsg(netisr_cpuport(0), lmsg, 0);