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 $
37 #include "opt_bootp.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/malloc.h>
46 #include <sys/msgport.h>
47 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56 #include <net/netmsg2.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/udp_var.h>
63 #include <netinet/igmp_var.h>
65 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
67 static int in_mask2len (struct in_addr *);
68 static void in_len2mask (struct in_addr *, int);
69 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
70 struct ifnet *, struct thread *);
72 static void in_socktrim (struct sockaddr_in *);
73 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
74 const struct sockaddr_in *, int);
76 static int in_control_internal(u_long, caddr_t, struct ifnet *,
78 static int in_control_redispatch(u_long, caddr_t, struct ifnet *,
81 static int in_addprefix(struct in_ifaddr *, int);
82 static void in_scrubprefix(struct in_ifaddr *);
84 static int subnetsarelocal = 0;
85 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
87 "Count all internet addresses of subnets of the local net as local");
89 struct in_multihead in_multihead; /* XXX BSS initialization */
91 extern struct inpcbinfo ripcbinfo;
94 * Return 1 if an internet address is for a ``local'' host
95 * (one to which we have a connection). If subnetsarelocal
96 * is true, this includes other subnets of the local net.
97 * Otherwise, it includes only the directly-connected (sub)nets.
100 in_localaddr(struct in_addr in)
102 u_long i = ntohl(in.s_addr);
103 struct in_ifaddr_container *iac;
104 struct in_ifaddr *ia;
106 if (subnetsarelocal) {
107 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
110 if ((i & ia->ia_netmask) == ia->ia_net)
114 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
117 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
125 * Determine whether an IP address is in a reserved set of addresses
126 * that may not be forwarded, or whether datagrams to that destination
130 in_canforward(struct in_addr in)
132 u_long i = ntohl(in.s_addr);
135 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
138 net = i & IN_CLASSA_NET;
139 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
146 * Trim a mask in a sockaddr
149 in_socktrim(struct sockaddr_in *ap)
151 char *cplim = (char *) &ap->sin_addr;
152 char *cp = (char *) (&ap->sin_addr + 1);
155 while (--cp >= cplim)
157 (ap)->sin_len = cp - (char *) (ap) + 1;
163 in_mask2len(struct in_addr *mask)
169 for (x = 0; x < sizeof *mask; x++) {
174 if (x < sizeof *mask) {
175 for (y = 0; y < 8; y++) {
176 if ((p[x] & (0x80 >> y)) == 0)
184 in_len2mask(struct in_addr *mask, int len)
190 bzero(mask, sizeof *mask);
191 for (i = 0; i < len / 8; i++)
194 p[i] = (0xff00 >> (len % 8)) & 0xff;
197 static int in_interfaces; /* number of external internet interfaces */
200 in_control_dispatch(netmsg_t msg)
204 error = in_control_redispatch(msg->control.nm_cmd,
205 msg->control.nm_data,
208 lwkt_replymsg(&msg->lmsg, error);
212 in_control_internal_dispatch(netmsg_t msg)
216 error = in_control_internal(msg->control.nm_cmd,
217 msg->control.nm_data,
220 lwkt_replymsg(&msg->lmsg, error);
224 in_control_redispatch(u_long cmd, caddr_t data, struct ifnet *ifp,
227 struct netmsg_pru_control msg;
231 * IFADDR alterations are serialized by netisr0
240 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
241 0, in_control_internal_dispatch);
246 lwkt_domsg(cpu_portfn(0), &msg.base.lmsg, 0);
247 error = msg.base.lmsg.ms_error;
251 error = in_control_internal(cmd, data, ifp, td);
258 * Generic internet control operations (ioctl's).
259 * Ifp is 0 if not an interface-specific ioctl.
261 * NOTE! td might be NULL.
265 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
273 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
279 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
282 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
283 ("recursive SIOC%cLIFADDR!",
284 cmd == SIOCDLIFADDR ? 'D' : 'A'));
286 return in_control_redispatch(cmd, data, ifp, td);
290 in_ialink_dispatch(netmsg_t msg)
292 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
293 struct ifaddr_container *ifac;
294 struct in_ifaddr_container *iac;
299 ifac = &ia->ia_ifa.ifa_containers[cpu];
300 ASSERT_IFAC_VALID(ifac);
301 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
302 ("ia is on in_ifaddrheads"));
304 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
305 iac = &ifac->ifa_proto_u.u_in_ifac;
306 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
310 ifa_forwardmsg(&msg->lmsg, cpu + 1);
314 in_iaunlink_dispatch(netmsg_t msg)
316 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
317 struct ifaddr_container *ifac;
318 struct in_ifaddr_container *iac;
323 ifac = &ia->ia_ifa.ifa_containers[cpu];
324 ASSERT_IFAC_VALID(ifac);
325 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
326 ("ia is not on in_ifaddrheads"));
328 iac = &ifac->ifa_proto_u.u_in_ifac;
329 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
330 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
334 ifa_forwardmsg(&msg->lmsg, cpu + 1);
338 in_iahashins_dispatch(netmsg_t msg)
340 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
341 struct ifaddr_container *ifac;
342 struct in_ifaddr_container *iac;
347 ifac = &ia->ia_ifa.ifa_containers[cpu];
348 ASSERT_IFAC_VALID(ifac);
349 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
350 ("ia is on in_ifaddrhashtbls"));
352 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
353 iac = &ifac->ifa_proto_u.u_in_ifac;
354 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
359 ifa_forwardmsg(&msg->lmsg, cpu + 1);
363 in_iahashrem_dispatch(netmsg_t msg)
365 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
366 struct ifaddr_container *ifac;
367 struct in_ifaddr_container *iac;
372 ifac = &ia->ia_ifa.ifa_containers[cpu];
373 ASSERT_IFAC_VALID(ifac);
374 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
375 ("ia is not on in_ifaddrhashtbls"));
377 iac = &ifac->ifa_proto_u.u_in_ifac;
378 LIST_REMOVE(iac, ia_hash);
379 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
383 ifa_forwardmsg(&msg->lmsg, cpu + 1);
387 in_ialink(struct in_ifaddr *ia)
389 struct netmsg_base msg;
391 netmsg_init(&msg, NULL, &curthread->td_msgport,
392 0, in_ialink_dispatch);
393 msg.lmsg.u.ms_resultp = ia;
395 ifa_domsg(&msg.lmsg, 0);
399 in_iaunlink(struct in_ifaddr *ia)
401 struct netmsg_base msg;
403 netmsg_init(&msg, NULL, &curthread->td_msgport,
404 0, in_iaunlink_dispatch);
405 msg.lmsg.u.ms_resultp = ia;
407 ifa_domsg(&msg.lmsg, 0);
411 in_iahash_insert(struct in_ifaddr *ia)
413 struct netmsg_base msg;
415 netmsg_init(&msg, NULL, &curthread->td_msgport,
416 0, in_iahashins_dispatch);
417 msg.lmsg.u.ms_resultp = ia;
419 ifa_domsg(&msg.lmsg, 0);
423 in_iahash_remove(struct in_ifaddr *ia)
425 struct netmsg_base msg;
427 netmsg_init(&msg, NULL, &curthread->td_msgport,
428 0, in_iahashrem_dispatch);
429 msg.lmsg.u.ms_resultp = ia;
431 ifa_domsg(&msg.lmsg, 0);
434 static __inline struct in_ifaddr *
435 in_ianext(struct in_ifaddr *oia)
437 struct ifaddr_container *ifac;
438 struct in_ifaddr_container *iac;
440 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
441 ASSERT_IFAC_VALID(ifac);
442 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
443 ("ia is not on in_ifaddrheads"));
445 iac = &ifac->ifa_proto_u.u_in_ifac;
446 iac = TAILQ_NEXT(iac, ia_link);
454 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
457 struct ifreq *ifr = (struct ifreq *)data;
458 struct in_ifaddr *ia = NULL;
460 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
461 struct ifaddr_container *ifac;
462 struct in_ifaddr_container *iac;
463 struct sockaddr_in oldaddr;
464 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
471 * Find address for this interface, if it exists.
473 * If an alias address was specified, find that one instead of
474 * the first one on the interface, if possible
477 struct in_ifaddr *iap;
479 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
480 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
482 if (iap->ia_ifp == ifp &&
483 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
489 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
491 iap = ifatoia(ifac->ifa);
492 if (iap->ia_addr.sin_family == AF_INET) {
499 if (ifp->if_flags & IFF_UP)
507 return (EADDRNOTAVAIL);
508 if (ifra->ifra_addr.sin_family == AF_INET) {
510 if (ia->ia_ifp == ifp &&
511 ia->ia_addr.sin_addr.s_addr ==
512 ifra->ifra_addr.sin_addr.s_addr)
516 if ((ifp->if_flags & IFF_POINTOPOINT) &&
517 cmd == SIOCAIFADDR &&
518 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
522 if (cmd == SIOCDIFADDR && ia == NULL)
523 return (EADDRNOTAVAIL);
528 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
532 return (EADDRNOTAVAIL);
534 if (cmd == SIOCSIFDSTADDR &&
535 (ifp->if_flags & IFF_POINTOPOINT) == 0)
542 ia = ifa_create(sizeof(*ia), M_WAITOK);
546 * Setup per-CPU information
548 for (i = 0; i < ncpus; ++i) {
549 ifac = &ifa->ifa_containers[i];
550 iac = &ifac->ifa_proto_u.u_in_ifac;
556 * Protect from NETISR_IP traversing address list
557 * while we're modifying it.
562 ifa_iflink(ifa, ifp, 1);
564 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
565 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
566 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
567 ia->ia_sockmask.sin_len = 8;
568 ia->ia_sockmask.sin_family = AF_INET;
569 if (ifp->if_flags & IFF_BROADCAST) {
570 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
571 ia->ia_broadaddr.sin_family = AF_INET;
574 if (!(ifp->if_flags & IFF_LOOPBACK))
583 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
592 return (EADDRNOTAVAIL);
598 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
602 if ((ifp->if_flags & IFF_BROADCAST) == 0)
604 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
608 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
610 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
614 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
618 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
620 oldaddr = ia->ia_dstaddr;
621 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
622 if (ifp->if_ioctl != NULL) {
623 ifnet_serialize_all(ifp);
624 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
625 td->td_proc->p_ucred);
626 ifnet_deserialize_all(ifp);
628 ia->ia_dstaddr = oldaddr;
632 if (ia->ia_flags & IFA_ROUTE) {
633 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
634 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
635 ia->ia_ifa.ifa_dstaddr =
636 (struct sockaddr *)&ia->ia_dstaddr;
637 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
642 if ((ifp->if_flags & IFF_BROADCAST) == 0)
644 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
648 error = in_ifinit(ifp, ia,
649 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
650 if (error != 0 && iaIsNew)
653 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
654 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
657 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
659 * Interface is brought up by in_ifinit()
660 * (via ifp->if_ioctl). We act as if the
661 * interface got IFF_UP flag turned on.
668 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
669 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
676 if (ia->ia_addr.sin_family == AF_INET) {
677 if (ifra->ifra_addr.sin_len == 0) {
678 ifra->ifra_addr = ia->ia_addr;
680 } else if (ifra->ifra_addr.sin_addr.s_addr ==
681 ia->ia_addr.sin_addr.s_addr) {
685 if (ifra->ifra_mask.sin_len) {
687 ia->ia_sockmask = ifra->ifra_mask;
688 ia->ia_sockmask.sin_family = AF_INET;
690 ntohl(ia->ia_sockmask.sin_addr.s_addr);
693 if ((ifp->if_flags & IFF_POINTOPOINT) &&
694 ifra->ifra_dstaddr.sin_family == AF_INET) {
696 ia->ia_dstaddr = ifra->ifra_dstaddr;
697 maskIsNew = 1; /* We lie; but the effect's the same */
699 if (ifra->ifra_addr.sin_family == AF_INET &&
700 (hostIsNew || maskIsNew))
701 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
703 if (error != 0 && iaIsNew)
706 if ((ifp->if_flags & IFF_BROADCAST) &&
707 ifra->ifra_broadaddr.sin_family == AF_INET)
708 ia->ia_broadaddr = ifra->ifra_broadaddr;
710 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
711 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
714 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
715 /* See the comment in SIOCSIFADDR */
722 * in_ifscrub kills the interface route.
726 * in_ifadown gets rid of all the rest of
727 * the routes. This is not quite the right
728 * thing to do, but at least if we are running
729 * a routing process they will come back.
731 in_ifadown(&ia->ia_ifa, 1);
732 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
738 if (ifp == NULL || ifp->if_ioctl == NULL)
740 ifnet_serialize_all(ifp);
741 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
742 ifnet_deserialize_all(ifp);
746 KKASSERT(cmd == SIOCDIFADDR ||
747 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
749 ifa_ifunlink(&ia->ia_ifa, ifp);
752 if (cmd == SIOCDIFADDR) {
753 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
754 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
755 in_iahash_remove(ia);
760 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
761 * already taken care of the deletion from hash table
763 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
764 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
765 ("SIOC%cIFADDR failed on new ia, "
766 "but the new ia is still in hash table",
767 cmd == SIOCSIFADDR ? 'S' : 'A'));
771 ifa_destroy(&ia->ia_ifa);
773 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
774 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
776 * Though the address assignment failed, the
777 * interface is brought up by in_ifinit()
778 * (via ifp->if_ioctl). With the hope that
779 * the interface has some valid addresses, we
780 * act as if IFF_UP flag was just set on the
784 * This could only be done after the failed
785 * address is unlinked from the global address
796 * SIOCGLIFADDR: get first address. (?!?)
797 * SIOCGLIFADDR with IFLR_PREFIX:
798 * get first address that matches the specified prefix.
799 * SIOCALIFADDR: add the specified address.
800 * SIOCALIFADDR with IFLR_PREFIX:
801 * EINVAL since we can't deduce hostid part of the address.
802 * SIOCDLIFADDR: delete the specified address.
803 * SIOCDLIFADDR with IFLR_PREFIX:
804 * delete the first address that matches the specified prefix.
806 * EINVAL on invalid parameters
807 * EADDRNOTAVAIL on prefix match failed/specified address not found
808 * other values may be returned from in_ioctl()
810 * NOTE! td might be NULL.
813 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
816 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
820 panic("invalid argument to in_lifaddr_ioctl");
826 /* address must be specified on GET with IFLR_PREFIX */
827 if ((iflr->flags & IFLR_PREFIX) == 0)
832 /* address must be specified on ADD and DELETE */
833 if (iflr->addr.ss_family != AF_INET)
835 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
837 /* XXX need improvement */
838 if (iflr->dstaddr.ss_family
839 && iflr->dstaddr.ss_family != AF_INET)
841 if (iflr->dstaddr.ss_family
842 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
845 default: /*shouldn't happen*/
848 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
854 struct in_aliasreq ifra;
856 if (iflr->flags & IFLR_PREFIX)
859 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
860 bzero(&ifra, sizeof ifra);
861 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
863 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
865 if (iflr->dstaddr.ss_family) { /*XXX*/
866 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
867 iflr->dstaddr.ss_len);
870 ifra.ifra_mask.sin_family = AF_INET;
871 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
872 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
874 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
879 struct ifaddr_container *ifac;
880 struct in_ifaddr *ia;
881 struct in_addr mask, candidate, match;
882 struct sockaddr_in *sin;
885 bzero(&mask, sizeof mask);
886 if (iflr->flags & IFLR_PREFIX) {
887 /* lookup a prefix rather than address. */
888 in_len2mask(&mask, iflr->prefixlen);
890 sin = (struct sockaddr_in *)&iflr->addr;
891 match.s_addr = sin->sin_addr.s_addr;
892 match.s_addr &= mask.s_addr;
894 /* if you set extra bits, that's wrong */
895 if (match.s_addr != sin->sin_addr.s_addr)
900 if (cmd == SIOCGLIFADDR) {
901 /* on getting an address, take the 1st match */
902 match.s_addr = 0; /* gcc4 warning */
905 /* on deleting an address, do exact match */
906 in_len2mask(&mask, 32);
907 sin = (struct sockaddr_in *)&iflr->addr;
908 match.s_addr = sin->sin_addr.s_addr;
914 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
915 struct ifaddr *ifa = ifac->ifa;
917 if (ifa->ifa_addr->sa_family != AF_INET6)
922 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
923 candidate.s_addr &= mask.s_addr;
924 if (candidate.s_addr == match.s_addr)
928 return EADDRNOTAVAIL;
929 ia = (struct in_ifaddr *)(ifac->ifa);
931 if (cmd == SIOCGLIFADDR) {
932 /* fill in the if_laddrreq structure */
933 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
935 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
936 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
937 ia->ia_dstaddr.sin_len);
939 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
942 in_mask2len(&ia->ia_sockmask.sin_addr);
944 iflr->flags = 0; /*XXX*/
948 struct in_aliasreq ifra;
950 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
951 bzero(&ifra, sizeof ifra);
952 bcopy(iflr->iflr_name, ifra.ifra_name,
953 sizeof ifra.ifra_name);
955 bcopy(&ia->ia_addr, &ifra.ifra_addr,
956 ia->ia_addr.sin_len);
957 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
958 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
959 ia->ia_dstaddr.sin_len);
961 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
962 ia->ia_sockmask.sin_len);
964 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
970 return EOPNOTSUPP; /*just for safety*/
974 * Delete any existing route for an interface.
977 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
983 * Initialize an interface's internet address
984 * and routing table entry.
987 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
988 const struct sockaddr_in *sin, int scrub)
990 u_long i = ntohl(sin->sin_addr.s_addr);
991 struct sockaddr_in oldaddr;
992 struct ifaddr_container *ifac;
993 int flags = RTF_UP, error = 0;
996 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
997 oldaddr = ia->ia_addr;
999 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
1001 in_iahash_remove(ia);
1005 if (ia->ia_addr.sin_family == AF_INET)
1006 in_iahash_insert(ia);
1009 * Give the interface a chance to initialize
1010 * if this is its first address,
1011 * and to validate the address if necessary.
1013 if (ifp->if_ioctl != NULL) {
1014 ifnet_serialize_all(ifp);
1015 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1016 ifnet_deserialize_all(ifp);
1022 * Delete old route, if requested.
1025 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1026 in_ifscrub(ifp, ia);
1027 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1031 * Calculate netmask/subnetmask.
1034 ia->ia_netmask = IN_CLASSA_NET;
1035 else if (IN_CLASSB(i))
1036 ia->ia_netmask = IN_CLASSB_NET;
1038 ia->ia_netmask = IN_CLASSC_NET;
1040 * The subnet mask usually includes at least the standard network part,
1041 * but may may be smaller in the case of supernetting.
1042 * If it is set, we believe it.
1044 if (ia->ia_subnetmask == 0) {
1045 ia->ia_subnetmask = ia->ia_netmask;
1046 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1048 ia->ia_netmask &= ia->ia_subnetmask;
1050 ia->ia_net = i & ia->ia_netmask;
1051 ia->ia_subnet = i & ia->ia_subnetmask;
1052 in_socktrim(&ia->ia_sockmask);
1055 * Add route for the network.
1057 ia->ia_ifa.ifa_metric = ifp->if_metric;
1058 if (ifp->if_flags & IFF_BROADCAST) {
1059 ia->ia_broadaddr.sin_addr.s_addr =
1060 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1061 ia->ia_netbroadcast.s_addr =
1062 htonl(ia->ia_net | ~ ia->ia_netmask);
1063 } else if (ifp->if_flags & IFF_LOOPBACK) {
1064 ia->ia_dstaddr = ia->ia_addr;
1066 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1067 if (ia->ia_dstaddr.sin_family != AF_INET)
1073 * Don't add host routes for interface addresses of
1074 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1075 * possible to assign several such address pairs with consistent
1076 * results (no host route) and is required by BOOTP.
1078 * XXX: This is ugly ! There should be a way for the caller to
1079 * say that they don't want a host route.
1081 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1082 ia->ia_netmask != IN_CLASSA_NET ||
1083 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1084 error = in_addprefix(ia, flags);
1090 * If the interface supports multicast, join the "all hosts"
1091 * multicast group on that interface.
1093 if (ifp->if_flags & IFF_MULTICAST) {
1094 struct in_addr addr;
1096 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1097 in_addmulti(&addr, ifp);
1101 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1102 in_iahash_remove(ia);
1104 ia->ia_addr = oldaddr;
1106 in_iahash_insert(ia);
1110 #define rtinitflags(x) \
1111 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1115 * Add a route to prefix ("connected route" in cisco terminology).
1116 * Do nothing, if there are some interface addresses with the same
1117 * prefix already. This function assumes that the 'target' parent
1121 in_addprefix(struct in_ifaddr *target, int flags)
1123 struct in_ifaddr_container *iac;
1124 struct in_addr prefix, mask;
1129 * Don't add prefix routes for CARP interfaces.
1130 * Prefix routes creation is handled by CARP
1131 * interfaces themselves.
1133 if (target->ia_ifp->if_type == IFT_CARP)
1137 mask = target->ia_sockmask.sin_addr;
1138 if (flags & RTF_HOST) {
1139 prefix = target->ia_dstaddr.sin_addr;
1141 prefix = target->ia_addr.sin_addr;
1142 prefix.s_addr &= mask.s_addr;
1145 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1146 struct in_ifaddr *ia = iac->ia;
1149 /* Don't test against self */
1153 /* The tested address does not own a route entry */
1154 if ((ia->ia_flags & IFA_ROUTE) == 0)
1158 if (rtinitflags(ia)) {
1159 p = ia->ia_dstaddr.sin_addr;
1161 p = ia->ia_addr.sin_addr;
1162 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1164 if (prefix.s_addr != p.s_addr)
1168 * If the to-be-added address and the curretly being
1169 * tested address are not host addresses, we need to
1170 * take subnetmask into consideration.
1172 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1173 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1177 * If we got a matching prefix route inserted by other
1178 * interface address, we don't need to bother.
1184 * No one seem to have prefix route; insert it.
1186 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1188 target->ia_flags |= IFA_ROUTE;
1193 * Remove a route to prefix ("connected route" in cisco terminology).
1194 * Re-installs the route by using another interface address, if there's
1195 * one with the same prefix (otherwise we lose the route mistakenly).
1198 in_scrubprefix(struct in_ifaddr *target)
1200 struct in_ifaddr_container *iac;
1201 struct in_addr prefix, mask;
1204 if ((target->ia_flags & IFA_ROUTE) == 0)
1207 mask = target->ia_sockmask.sin_addr;
1208 if (rtinitflags(target)) {
1209 prefix = target->ia_dstaddr.sin_addr;
1211 prefix = target->ia_addr.sin_addr;
1212 prefix.s_addr &= mask.s_addr;
1215 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1216 struct in_ifaddr *ia = iac->ia;
1219 /* Don't test against self */
1223 /* The tested address already owns a route entry */
1224 if (ia->ia_flags & IFA_ROUTE)
1228 * The prefix route of the tested address should
1229 * never be installed if its parent interface is
1232 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1237 * Don't add prefix routes for CARP interfaces.
1238 * Prefix routes creation is handled by CARP
1239 * interfaces themselves.
1241 if (ia->ia_ifp->if_type == IFT_CARP)
1246 if (rtinitflags(ia)) {
1247 p = ia->ia_dstaddr.sin_addr;
1249 p = ia->ia_addr.sin_addr;
1250 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1252 if (prefix.s_addr != p.s_addr)
1256 * We don't need to test subnetmask here, as what we do
1257 * in in_addprefix(), since if the the tested address's
1258 * parent interface is UP, the tested address should own
1259 * a prefix route entry and we would never reach here.
1263 * If we got a matching prefix route, move IFA_ROUTE to him
1265 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1266 target->ia_flags &= ~IFA_ROUTE;
1268 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1270 ia->ia_flags |= IFA_ROUTE;
1275 * No candidates for this prefix route; just remove it.
1277 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1278 target->ia_flags &= ~IFA_ROUTE;
1284 * Return 1 if the address might be a local broadcast address.
1287 in_broadcast(struct in_addr in, struct ifnet *ifp)
1289 struct ifaddr_container *ifac;
1292 if (in.s_addr == INADDR_BROADCAST ||
1293 in.s_addr == INADDR_ANY)
1295 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1297 t = ntohl(in.s_addr);
1299 * Look through the list of addresses for a match
1300 * with a broadcast address.
1302 #define ia ((struct in_ifaddr *)ifa)
1303 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1304 struct ifaddr *ifa = ifac->ifa;
1306 if (ifa->ifa_addr->sa_family == AF_INET &&
1307 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1308 in.s_addr == ia->ia_netbroadcast.s_addr ||
1310 * Check for old-style (host 0) broadcast.
1312 t == ia->ia_subnet || t == ia->ia_net) &&
1314 * Check for an all one subnetmask. These
1315 * only exist when an interface gets a secondary
1318 ia->ia_subnetmask != (u_long)0xffffffff)
1325 * Add an address to the list of IP multicast addresses for a given interface.
1328 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1330 struct in_multi *inm;
1332 struct sockaddr_in sin;
1333 struct ifmultiaddr *ifma;
1336 * Call generic routine to add membership or increment
1337 * refcount. It wants addresses in the form of a sockaddr,
1338 * so we build one here (being careful to zero the unused bytes).
1340 bzero(&sin, sizeof sin);
1341 sin.sin_family = AF_INET;
1342 sin.sin_len = sizeof sin;
1345 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1352 * If ifma->ifma_protospec is null, then if_addmulti() created
1353 * a new record. Otherwise, we are done.
1355 if (ifma->ifma_protospec != 0) {
1357 return ifma->ifma_protospec;
1360 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1361 at interrupt time? If so, need to fix if_addmulti. XXX */
1362 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1363 inm->inm_addr = *ap;
1365 inm->inm_ifma = ifma;
1366 ifma->ifma_protospec = inm;
1367 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1370 * Let IGMP know that we have joined a new IP multicast group.
1372 igmp_joingroup(inm);
1378 * Delete a multicast address record.
1381 in_delmulti(struct in_multi *inm)
1383 struct ifmultiaddr *ifma;
1384 struct in_multi my_inm;
1387 ifma = inm->inm_ifma;
1388 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1389 if (ifma->ifma_refcount == 1) {
1391 * No remaining claims to this record; let IGMP know that
1392 * we are leaving the multicast group.
1393 * But do it after the if_delmulti() which might reset
1394 * the interface and nuke the packet.
1397 ifma->ifma_protospec = 0;
1398 LIST_REMOVE(inm, inm_link);
1399 kfree(inm, M_IPMADDR);
1401 /* XXX - should be separate API for when we have an ifma? */
1402 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1403 if (my_inm.inm_ifp != NULL)
1404 igmp_leavegroup(&my_inm);
1409 in_ifdetach(struct ifnet *ifp)
1411 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1414 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);