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.41 2008/08/17 05:20:10 sephe Exp $
38 #include "opt_bootp.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
44 #include <sys/malloc.h>
47 #include <sys/msgport.h>
48 #include <sys/socket.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/thread2.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/netmsg2.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/udp_var.h>
64 #include <netinet/igmp_var.h>
66 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
68 static int in_mask2len (struct in_addr *);
69 static void in_len2mask (struct in_addr *, int);
70 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
71 struct ifnet *, struct thread *);
73 static void in_socktrim (struct sockaddr_in *);
74 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
75 const struct sockaddr_in *, int);
77 static int in_control_internal(u_long, caddr_t, struct ifnet *,
79 static int in_control_redispatch(u_long, caddr_t, struct ifnet *,
82 static int in_addprefix(struct in_ifaddr *, int);
83 static void in_scrubprefix(struct in_ifaddr *);
85 static int subnetsarelocal = 0;
86 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
88 "Count all internet addresses of subnets of the local net as local");
90 struct in_multihead in_multihead; /* XXX BSS initialization */
92 extern struct inpcbinfo ripcbinfo;
95 * Return 1 if an internet address is for a ``local'' host
96 * (one to which we have a connection). If subnetsarelocal
97 * is true, this includes other subnets of the local net.
98 * Otherwise, it includes only the directly-connected (sub)nets.
101 in_localaddr(struct in_addr in)
103 u_long i = ntohl(in.s_addr);
104 struct in_ifaddr_container *iac;
105 struct in_ifaddr *ia;
107 if (subnetsarelocal) {
108 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
111 if ((i & ia->ia_netmask) == ia->ia_net)
115 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
118 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
126 * Determine whether an IP address is in a reserved set of addresses
127 * that may not be forwarded, or whether datagrams to that destination
131 in_canforward(struct in_addr in)
133 u_long i = ntohl(in.s_addr);
136 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
139 net = i & IN_CLASSA_NET;
140 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
147 * Trim a mask in a sockaddr
150 in_socktrim(struct sockaddr_in *ap)
152 char *cplim = (char *) &ap->sin_addr;
153 char *cp = (char *) (&ap->sin_addr + 1);
156 while (--cp >= cplim)
158 (ap)->sin_len = cp - (char *) (ap) + 1;
164 in_mask2len(struct in_addr *mask)
170 for (x = 0; x < sizeof *mask; x++) {
175 if (x < sizeof *mask) {
176 for (y = 0; y < 8; y++) {
177 if ((p[x] & (0x80 >> y)) == 0)
185 in_len2mask(struct in_addr *mask, int len)
191 bzero(mask, sizeof *mask);
192 for (i = 0; i < len / 8; i++)
195 p[i] = (0xff00 >> (len % 8)) & 0xff;
198 static int in_interfaces; /* number of external internet interfaces */
201 in_control_dispatch(netmsg_t msg)
205 error = in_control_redispatch(msg->control.nm_cmd,
206 msg->control.nm_data,
209 lwkt_replymsg(&msg->lmsg, error);
213 in_control_internal_dispatch(netmsg_t msg)
217 error = in_control_internal(msg->control.nm_cmd,
218 msg->control.nm_data,
221 lwkt_replymsg(&msg->lmsg, error);
225 in_control_redispatch(u_long cmd, caddr_t data, struct ifnet *ifp,
228 struct netmsg_pru_control msg;
232 * IFADDR alterations are serialized by netisr0
241 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
242 0, in_control_internal_dispatch);
247 lwkt_domsg(cpu_portfn(0), &msg.base.lmsg, 0);
248 error = msg.base.lmsg.ms_error;
252 error = in_control_internal(cmd, data, ifp, td);
259 * Generic internet control operations (ioctl's).
260 * Ifp is 0 if not an interface-specific ioctl.
262 * NOTE! td might be NULL.
266 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
274 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
280 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
283 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
284 ("recursive SIOC%cLIFADDR!\n",
285 cmd == SIOCDLIFADDR ? 'D' : 'A'));
287 return in_control_redispatch(cmd, data, ifp, td);
291 in_ialink_dispatch(netmsg_t msg)
293 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
294 struct ifaddr_container *ifac;
295 struct in_ifaddr_container *iac;
300 ifac = &ia->ia_ifa.ifa_containers[cpu];
301 ASSERT_IFAC_VALID(ifac);
302 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
303 ("ia is on in_ifaddrheads\n"));
305 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
306 iac = &ifac->ifa_proto_u.u_in_ifac;
307 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
311 ifa_forwardmsg(&msg->lmsg, cpu + 1);
315 in_iaunlink_dispatch(netmsg_t msg)
317 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
318 struct ifaddr_container *ifac;
319 struct in_ifaddr_container *iac;
324 ifac = &ia->ia_ifa.ifa_containers[cpu];
325 ASSERT_IFAC_VALID(ifac);
326 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
327 ("ia is not on in_ifaddrheads\n"));
329 iac = &ifac->ifa_proto_u.u_in_ifac;
330 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
331 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
335 ifa_forwardmsg(&msg->lmsg, cpu + 1);
339 in_iahashins_dispatch(netmsg_t msg)
341 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
342 struct ifaddr_container *ifac;
343 struct in_ifaddr_container *iac;
348 ifac = &ia->ia_ifa.ifa_containers[cpu];
349 ASSERT_IFAC_VALID(ifac);
350 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
351 ("ia is on in_ifaddrhashtbls\n"));
353 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
354 iac = &ifac->ifa_proto_u.u_in_ifac;
355 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
360 ifa_forwardmsg(&msg->lmsg, cpu + 1);
364 in_iahashrem_dispatch(netmsg_t msg)
366 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
367 struct ifaddr_container *ifac;
368 struct in_ifaddr_container *iac;
373 ifac = &ia->ia_ifa.ifa_containers[cpu];
374 ASSERT_IFAC_VALID(ifac);
375 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
376 ("ia is not on in_ifaddrhashtbls\n"));
378 iac = &ifac->ifa_proto_u.u_in_ifac;
379 LIST_REMOVE(iac, ia_hash);
380 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
384 ifa_forwardmsg(&msg->lmsg, cpu + 1);
388 in_ialink(struct in_ifaddr *ia)
390 struct netmsg_base msg;
392 netmsg_init(&msg, NULL, &curthread->td_msgport,
393 0, in_ialink_dispatch);
394 msg.lmsg.u.ms_resultp = ia;
396 ifa_domsg(&msg.lmsg, 0);
400 in_iaunlink(struct in_ifaddr *ia)
402 struct netmsg_base msg;
404 netmsg_init(&msg, NULL, &curthread->td_msgport,
405 0, in_iaunlink_dispatch);
406 msg.lmsg.u.ms_resultp = ia;
408 ifa_domsg(&msg.lmsg, 0);
412 in_iahash_insert(struct in_ifaddr *ia)
414 struct netmsg_base msg;
416 netmsg_init(&msg, NULL, &curthread->td_msgport,
417 0, in_iahashins_dispatch);
418 msg.lmsg.u.ms_resultp = ia;
420 ifa_domsg(&msg.lmsg, 0);
424 in_iahash_remove(struct in_ifaddr *ia)
426 struct netmsg_base msg;
428 netmsg_init(&msg, NULL, &curthread->td_msgport,
429 0, in_iahashrem_dispatch);
430 msg.lmsg.u.ms_resultp = ia;
432 ifa_domsg(&msg.lmsg, 0);
435 static __inline struct in_ifaddr *
436 in_ianext(struct in_ifaddr *oia)
438 struct ifaddr_container *ifac;
439 struct in_ifaddr_container *iac;
441 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
442 ASSERT_IFAC_VALID(ifac);
443 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
444 ("ia is not on in_ifaddrheads\n"));
446 iac = &ifac->ifa_proto_u.u_in_ifac;
447 iac = TAILQ_NEXT(iac, ia_link);
455 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
458 struct ifreq *ifr = (struct ifreq *)data;
459 struct in_ifaddr *ia = NULL;
461 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
462 struct ifaddr_container *ifac;
463 struct in_ifaddr_container *iac;
464 struct sockaddr_in oldaddr;
465 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
472 * Find address for this interface, if it exists.
474 * If an alias address was specified, find that one instead of
475 * the first one on the interface, if possible
478 struct in_ifaddr *iap;
480 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
481 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
483 if (iap->ia_ifp == ifp &&
484 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
490 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
492 iap = ifatoia(ifac->ifa);
493 if (iap->ia_addr.sin_family == AF_INET) {
500 if (ifp->if_flags & IFF_UP)
508 return (EADDRNOTAVAIL);
509 if (ifra->ifra_addr.sin_family == AF_INET) {
511 if (ia->ia_ifp == ifp &&
512 ia->ia_addr.sin_addr.s_addr ==
513 ifra->ifra_addr.sin_addr.s_addr)
517 if ((ifp->if_flags & IFF_POINTOPOINT) &&
518 cmd == SIOCAIFADDR &&
519 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
523 if (cmd == SIOCDIFADDR && ia == NULL)
524 return (EADDRNOTAVAIL);
529 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
533 return (EADDRNOTAVAIL);
535 if (cmd == SIOCSIFDSTADDR &&
536 (ifp->if_flags & IFF_POINTOPOINT) == 0)
543 ia = ifa_create(sizeof(*ia), M_WAITOK);
547 * Setup per-CPU information
549 for (i = 0; i < ncpus; ++i) {
550 ifac = &ifa->ifa_containers[i];
551 iac = &ifac->ifa_proto_u.u_in_ifac;
557 * Protect from NETISR_IP traversing address list
558 * while we're modifying it.
563 ifa_iflink(ifa, ifp, 1);
565 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
566 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
567 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
568 ia->ia_sockmask.sin_len = 8;
569 ia->ia_sockmask.sin_family = AF_INET;
570 if (ifp->if_flags & IFF_BROADCAST) {
571 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
572 ia->ia_broadaddr.sin_family = AF_INET;
575 if (!(ifp->if_flags & IFF_LOOPBACK))
584 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
593 return (EADDRNOTAVAIL);
599 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
603 if ((ifp->if_flags & IFF_BROADCAST) == 0)
605 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
609 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
611 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
615 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
619 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
621 oldaddr = ia->ia_dstaddr;
622 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
623 if (ifp->if_ioctl != NULL) {
624 ifnet_serialize_all(ifp);
625 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
626 td->td_proc->p_ucred);
627 ifnet_deserialize_all(ifp);
629 ia->ia_dstaddr = oldaddr;
633 if (ia->ia_flags & IFA_ROUTE) {
634 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
635 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
636 ia->ia_ifa.ifa_dstaddr =
637 (struct sockaddr *)&ia->ia_dstaddr;
638 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
643 if ((ifp->if_flags & IFF_BROADCAST) == 0)
645 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
649 error = in_ifinit(ifp, ia,
650 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
651 if (error != 0 && iaIsNew)
654 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
655 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
658 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
660 * Interface is brought up by in_ifinit()
661 * (via ifp->if_ioctl). We act as if the
662 * interface got IFF_UP flag turned on.
669 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
670 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
677 if (ia->ia_addr.sin_family == AF_INET) {
678 if (ifra->ifra_addr.sin_len == 0) {
679 ifra->ifra_addr = ia->ia_addr;
681 } else if (ifra->ifra_addr.sin_addr.s_addr ==
682 ia->ia_addr.sin_addr.s_addr) {
686 if (ifra->ifra_mask.sin_len) {
688 ia->ia_sockmask = ifra->ifra_mask;
689 ia->ia_sockmask.sin_family = AF_INET;
691 ntohl(ia->ia_sockmask.sin_addr.s_addr);
694 if ((ifp->if_flags & IFF_POINTOPOINT) &&
695 ifra->ifra_dstaddr.sin_family == AF_INET) {
697 ia->ia_dstaddr = ifra->ifra_dstaddr;
698 maskIsNew = 1; /* We lie; but the effect's the same */
700 if (ifra->ifra_addr.sin_family == AF_INET &&
701 (hostIsNew || maskIsNew))
702 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
704 if (error != 0 && iaIsNew)
707 if ((ifp->if_flags & IFF_BROADCAST) &&
708 ifra->ifra_broadaddr.sin_family == AF_INET)
709 ia->ia_broadaddr = ifra->ifra_broadaddr;
711 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
712 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
715 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
716 /* See the comment in SIOCSIFADDR */
723 * in_ifscrub kills the interface route.
727 * in_ifadown gets rid of all the rest of
728 * the routes. This is not quite the right
729 * thing to do, but at least if we are running
730 * a routing process they will come back.
732 in_ifadown(&ia->ia_ifa, 1);
733 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
739 if (ifp == NULL || ifp->if_ioctl == NULL)
741 ifnet_serialize_all(ifp);
742 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
743 ifnet_deserialize_all(ifp);
747 KKASSERT(cmd == SIOCDIFADDR ||
748 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
750 ifa_ifunlink(&ia->ia_ifa, ifp);
753 if (cmd == SIOCDIFADDR) {
754 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
755 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
756 in_iahash_remove(ia);
761 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
762 * already taken care of the deletion from hash table
764 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
765 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
766 ("SIOC%cIFADDR failed on new ia, "
767 "but the new ia is still in hash table\n",
768 cmd == SIOCSIFADDR ? 'S' : 'A'));
772 ifa_destroy(&ia->ia_ifa);
774 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
775 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
777 * Though the address assignment failed, the
778 * interface is brought up by in_ifinit()
779 * (via ifp->if_ioctl). With the hope that
780 * the interface has some valid addresses, we
781 * act as if IFF_UP flag was just set on the
785 * This could only be done after the failed
786 * address is unlinked from the global address
797 * SIOCGLIFADDR: get first address. (?!?)
798 * SIOCGLIFADDR with IFLR_PREFIX:
799 * get first address that matches the specified prefix.
800 * SIOCALIFADDR: add the specified address.
801 * SIOCALIFADDR with IFLR_PREFIX:
802 * EINVAL since we can't deduce hostid part of the address.
803 * SIOCDLIFADDR: delete the specified address.
804 * SIOCDLIFADDR with IFLR_PREFIX:
805 * delete the first address that matches the specified prefix.
807 * EINVAL on invalid parameters
808 * EADDRNOTAVAIL on prefix match failed/specified address not found
809 * other values may be returned from in_ioctl()
811 * NOTE! td might be NULL.
814 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
817 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
821 panic("invalid argument to in_lifaddr_ioctl");
827 /* address must be specified on GET with IFLR_PREFIX */
828 if ((iflr->flags & IFLR_PREFIX) == 0)
833 /* address must be specified on ADD and DELETE */
834 if (iflr->addr.ss_family != AF_INET)
836 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
838 /* XXX need improvement */
839 if (iflr->dstaddr.ss_family
840 && iflr->dstaddr.ss_family != AF_INET)
842 if (iflr->dstaddr.ss_family
843 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
846 default: /*shouldn't happen*/
849 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
855 struct in_aliasreq ifra;
857 if (iflr->flags & IFLR_PREFIX)
860 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
861 bzero(&ifra, sizeof ifra);
862 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
864 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
866 if (iflr->dstaddr.ss_family) { /*XXX*/
867 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
868 iflr->dstaddr.ss_len);
871 ifra.ifra_mask.sin_family = AF_INET;
872 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
873 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
875 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
880 struct ifaddr_container *ifac;
881 struct in_ifaddr *ia;
882 struct in_addr mask, candidate, match;
883 struct sockaddr_in *sin;
886 bzero(&mask, sizeof mask);
887 if (iflr->flags & IFLR_PREFIX) {
888 /* lookup a prefix rather than address. */
889 in_len2mask(&mask, iflr->prefixlen);
891 sin = (struct sockaddr_in *)&iflr->addr;
892 match.s_addr = sin->sin_addr.s_addr;
893 match.s_addr &= mask.s_addr;
895 /* if you set extra bits, that's wrong */
896 if (match.s_addr != sin->sin_addr.s_addr)
901 if (cmd == SIOCGLIFADDR) {
902 /* on getting an address, take the 1st match */
903 match.s_addr = 0; /* gcc4 warning */
906 /* on deleting an address, do exact match */
907 in_len2mask(&mask, 32);
908 sin = (struct sockaddr_in *)&iflr->addr;
909 match.s_addr = sin->sin_addr.s_addr;
915 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
916 struct ifaddr *ifa = ifac->ifa;
918 if (ifa->ifa_addr->sa_family != AF_INET6)
923 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
924 candidate.s_addr &= mask.s_addr;
925 if (candidate.s_addr == match.s_addr)
929 return EADDRNOTAVAIL;
930 ia = (struct in_ifaddr *)(ifac->ifa);
932 if (cmd == SIOCGLIFADDR) {
933 /* fill in the if_laddrreq structure */
934 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
936 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
937 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
938 ia->ia_dstaddr.sin_len);
940 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
943 in_mask2len(&ia->ia_sockmask.sin_addr);
945 iflr->flags = 0; /*XXX*/
949 struct in_aliasreq ifra;
951 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
952 bzero(&ifra, sizeof ifra);
953 bcopy(iflr->iflr_name, ifra.ifra_name,
954 sizeof ifra.ifra_name);
956 bcopy(&ia->ia_addr, &ifra.ifra_addr,
957 ia->ia_addr.sin_len);
958 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
959 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
960 ia->ia_dstaddr.sin_len);
962 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
963 ia->ia_sockmask.sin_len);
965 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
971 return EOPNOTSUPP; /*just for safety*/
975 * Delete any existing route for an interface.
978 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
984 * Initialize an interface's internet address
985 * and routing table entry.
988 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
989 const struct sockaddr_in *sin, int scrub)
991 u_long i = ntohl(sin->sin_addr.s_addr);
992 struct sockaddr_in oldaddr;
993 struct ifaddr_container *ifac;
994 int flags = RTF_UP, error = 0;
997 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
998 oldaddr = ia->ia_addr;
1000 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
1002 in_iahash_remove(ia);
1006 if (ia->ia_addr.sin_family == AF_INET)
1007 in_iahash_insert(ia);
1010 * Give the interface a chance to initialize
1011 * if this is its first address,
1012 * and to validate the address if necessary.
1014 if (ifp->if_ioctl != NULL) {
1015 ifnet_serialize_all(ifp);
1016 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1017 ifnet_deserialize_all(ifp);
1023 * Delete old route, if requested.
1026 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1027 in_ifscrub(ifp, ia);
1028 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1032 * Calculate netmask/subnetmask.
1035 ia->ia_netmask = IN_CLASSA_NET;
1036 else if (IN_CLASSB(i))
1037 ia->ia_netmask = IN_CLASSB_NET;
1039 ia->ia_netmask = IN_CLASSC_NET;
1041 * The subnet mask usually includes at least the standard network part,
1042 * but may may be smaller in the case of supernetting.
1043 * If it is set, we believe it.
1045 if (ia->ia_subnetmask == 0) {
1046 ia->ia_subnetmask = ia->ia_netmask;
1047 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1049 ia->ia_netmask &= ia->ia_subnetmask;
1051 ia->ia_net = i & ia->ia_netmask;
1052 ia->ia_subnet = i & ia->ia_subnetmask;
1053 in_socktrim(&ia->ia_sockmask);
1056 * Add route for the network.
1058 ia->ia_ifa.ifa_metric = ifp->if_metric;
1059 if (ifp->if_flags & IFF_BROADCAST) {
1060 ia->ia_broadaddr.sin_addr.s_addr =
1061 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1062 ia->ia_netbroadcast.s_addr =
1063 htonl(ia->ia_net | ~ ia->ia_netmask);
1064 } else if (ifp->if_flags & IFF_LOOPBACK) {
1065 ia->ia_dstaddr = ia->ia_addr;
1067 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1068 if (ia->ia_dstaddr.sin_family != AF_INET)
1074 * Don't add host routes for interface addresses of
1075 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1076 * possible to assign several such address pairs with consistent
1077 * results (no host route) and is required by BOOTP.
1079 * XXX: This is ugly ! There should be a way for the caller to
1080 * say that they don't want a host route.
1082 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1083 ia->ia_netmask != IN_CLASSA_NET ||
1084 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1085 error = in_addprefix(ia, flags);
1091 * If the interface supports multicast, join the "all hosts"
1092 * multicast group on that interface.
1094 if (ifp->if_flags & IFF_MULTICAST) {
1095 struct in_addr addr;
1097 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1098 in_addmulti(&addr, ifp);
1102 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1103 in_iahash_remove(ia);
1105 ia->ia_addr = oldaddr;
1107 in_iahash_insert(ia);
1111 #define rtinitflags(x) \
1112 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1116 * Add a route to prefix ("connected route" in cisco terminology).
1117 * Do nothing, if there are some interface addresses with the same
1118 * prefix already. This function assumes that the 'target' parent
1122 in_addprefix(struct in_ifaddr *target, int flags)
1124 struct in_ifaddr_container *iac;
1125 struct in_addr prefix, mask;
1130 * Don't add prefix routes for CARP interfaces.
1131 * Prefix routes creation is handled by CARP
1132 * interfaces themselves.
1134 if (target->ia_ifp->if_type == IFT_CARP)
1138 mask = target->ia_sockmask.sin_addr;
1139 if (flags & RTF_HOST) {
1140 prefix = target->ia_dstaddr.sin_addr;
1142 prefix = target->ia_addr.sin_addr;
1143 prefix.s_addr &= mask.s_addr;
1146 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1147 struct in_ifaddr *ia = iac->ia;
1150 /* Don't test against self */
1154 /* The tested address does not own a route entry */
1155 if ((ia->ia_flags & IFA_ROUTE) == 0)
1159 if (rtinitflags(ia)) {
1160 p = ia->ia_dstaddr.sin_addr;
1162 p = ia->ia_addr.sin_addr;
1163 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1165 if (prefix.s_addr != p.s_addr)
1169 * If the to-be-added address and the curretly being
1170 * tested address are not host addresses, we need to
1171 * take subnetmask into consideration.
1173 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1174 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1178 * If we got a matching prefix route inserted by other
1179 * interface address, we don't need to bother.
1185 * No one seem to have prefix route; insert it.
1187 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1189 target->ia_flags |= IFA_ROUTE;
1194 * Remove a route to prefix ("connected route" in cisco terminology).
1195 * Re-installs the route by using another interface address, if there's
1196 * one with the same prefix (otherwise we lose the route mistakenly).
1199 in_scrubprefix(struct in_ifaddr *target)
1201 struct in_ifaddr_container *iac;
1202 struct in_addr prefix, mask;
1205 if ((target->ia_flags & IFA_ROUTE) == 0)
1208 mask = target->ia_sockmask.sin_addr;
1209 if (rtinitflags(target)) {
1210 prefix = target->ia_dstaddr.sin_addr;
1212 prefix = target->ia_addr.sin_addr;
1213 prefix.s_addr &= mask.s_addr;
1216 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1217 struct in_ifaddr *ia = iac->ia;
1220 /* Don't test against self */
1224 /* The tested address already owns a route entry */
1225 if (ia->ia_flags & IFA_ROUTE)
1229 * The prefix route of the tested address should
1230 * never be installed if its parent interface is
1233 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1238 * Don't add prefix routes for CARP interfaces.
1239 * Prefix routes creation is handled by CARP
1240 * interfaces themselves.
1242 if (ia->ia_ifp->if_type == IFT_CARP)
1247 if (rtinitflags(ia)) {
1248 p = ia->ia_dstaddr.sin_addr;
1250 p = ia->ia_addr.sin_addr;
1251 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1253 if (prefix.s_addr != p.s_addr)
1257 * We don't need to test subnetmask here, as what we do
1258 * in in_addprefix(), since if the the tested address's
1259 * parent interface is UP, the tested address should own
1260 * a prefix route entry and we would never reach here.
1264 * If we got a matching prefix route, move IFA_ROUTE to him
1266 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1267 target->ia_flags &= ~IFA_ROUTE;
1269 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1271 ia->ia_flags |= IFA_ROUTE;
1276 * No candidates for this prefix route; just remove it.
1278 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1279 target->ia_flags &= ~IFA_ROUTE;
1285 * Return 1 if the address might be a local broadcast address.
1288 in_broadcast(struct in_addr in, struct ifnet *ifp)
1290 struct ifaddr_container *ifac;
1293 if (in.s_addr == INADDR_BROADCAST ||
1294 in.s_addr == INADDR_ANY)
1296 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1298 t = ntohl(in.s_addr);
1300 * Look through the list of addresses for a match
1301 * with a broadcast address.
1303 #define ia ((struct in_ifaddr *)ifa)
1304 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1305 struct ifaddr *ifa = ifac->ifa;
1307 if (ifa->ifa_addr->sa_family == AF_INET &&
1308 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1309 in.s_addr == ia->ia_netbroadcast.s_addr ||
1311 * Check for old-style (host 0) broadcast.
1313 t == ia->ia_subnet || t == ia->ia_net) &&
1315 * Check for an all one subnetmask. These
1316 * only exist when an interface gets a secondary
1319 ia->ia_subnetmask != (u_long)0xffffffff)
1326 * Add an address to the list of IP multicast addresses for a given interface.
1329 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1331 struct in_multi *inm;
1333 struct sockaddr_in sin;
1334 struct ifmultiaddr *ifma;
1337 * Call generic routine to add membership or increment
1338 * refcount. It wants addresses in the form of a sockaddr,
1339 * so we build one here (being careful to zero the unused bytes).
1341 bzero(&sin, sizeof sin);
1342 sin.sin_family = AF_INET;
1343 sin.sin_len = sizeof sin;
1346 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1353 * If ifma->ifma_protospec is null, then if_addmulti() created
1354 * a new record. Otherwise, we are done.
1356 if (ifma->ifma_protospec != 0) {
1358 return ifma->ifma_protospec;
1361 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1362 at interrupt time? If so, need to fix if_addmulti. XXX */
1363 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1364 inm->inm_addr = *ap;
1366 inm->inm_ifma = ifma;
1367 ifma->ifma_protospec = inm;
1368 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1371 * Let IGMP know that we have joined a new IP multicast group.
1373 igmp_joingroup(inm);
1379 * Delete a multicast address record.
1382 in_delmulti(struct in_multi *inm)
1384 struct ifmultiaddr *ifma;
1385 struct in_multi my_inm;
1388 ifma = inm->inm_ifma;
1389 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1390 if (ifma->ifma_refcount == 1) {
1392 * No remaining claims to this record; let IGMP know that
1393 * we are leaving the multicast group.
1394 * But do it after the if_delmulti() which might reset
1395 * the interface and nuke the packet.
1398 ifma->ifma_protospec = 0;
1399 LIST_REMOVE(inm, inm_link);
1400 kfree(inm, M_IPMADDR);
1402 /* XXX - should be separate API for when we have an ifma? */
1403 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1404 if (my_inm.inm_ifp != NULL)
1405 igmp_leavegroup(&my_inm);
1410 in_ifdetach(struct ifnet *ifp)
1412 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1415 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);