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 (struct socket *, u_long, caddr_t,
67 struct ifnet *, struct thread *);
69 static void in_socktrim (struct sockaddr_in *);
70 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
71 const struct sockaddr_in *, int);
73 static int in_control_internal(u_long, caddr_t, struct ifnet *,
75 static int in_control_redispatch(u_long, caddr_t, struct ifnet *,
78 static int in_addprefix(struct in_ifaddr *, int);
79 static void in_scrubprefix(struct in_ifaddr *);
81 static int subnetsarelocal = 0;
82 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
84 "Count all internet addresses of subnets of the local net as local");
86 struct in_multihead in_multihead; /* XXX BSS initialization */
88 extern struct inpcbinfo ripcbinfo;
91 * Return 1 if an internet address is for a ``local'' host
92 * (one to which we have a connection). If subnetsarelocal
93 * is true, this includes other subnets of the local net.
94 * Otherwise, it includes only the directly-connected (sub)nets.
97 in_localaddr(struct in_addr in)
99 u_long i = ntohl(in.s_addr);
100 struct in_ifaddr_container *iac;
101 struct in_ifaddr *ia;
103 if (subnetsarelocal) {
104 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
107 if ((i & ia->ia_netmask) == ia->ia_net)
111 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
114 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
122 * Determine whether an IP address is in a reserved set of addresses
123 * that may not be forwarded, or whether datagrams to that destination
127 in_canforward(struct in_addr in)
129 u_long i = ntohl(in.s_addr);
132 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
135 net = i & IN_CLASSA_NET;
136 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
143 * Trim a mask in a sockaddr
146 in_socktrim(struct sockaddr_in *ap)
148 char *cplim = (char *) &ap->sin_addr;
149 char *cp = (char *) (&ap->sin_addr + 1);
152 while (--cp >= cplim)
154 (ap)->sin_len = cp - (char *) (ap) + 1;
160 in_mask2len(struct in_addr *mask)
166 for (x = 0; x < sizeof *mask; x++) {
171 if (x < sizeof *mask) {
172 for (y = 0; y < 8; y++) {
173 if ((p[x] & (0x80 >> y)) == 0)
181 in_len2mask(struct in_addr *mask, int len)
187 bzero(mask, sizeof *mask);
188 for (i = 0; i < len / 8; i++)
191 p[i] = (0xff00 >> (len % 8)) & 0xff;
194 static int in_interfaces; /* number of external internet interfaces */
197 in_control_dispatch(netmsg_t msg)
201 error = in_control_redispatch(msg->control.nm_cmd,
202 msg->control.nm_data,
205 lwkt_replymsg(&msg->lmsg, error);
209 in_control_internal_dispatch(netmsg_t msg)
213 error = in_control_internal(msg->control.nm_cmd,
214 msg->control.nm_data,
217 lwkt_replymsg(&msg->lmsg, error);
221 in_control_redispatch(u_long cmd, caddr_t data, struct ifnet *ifp,
224 struct netmsg_pru_control msg;
228 * IFADDR alterations are serialized by netisr0
237 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
238 0, in_control_internal_dispatch);
243 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
244 error = msg.base.lmsg.ms_error;
248 error = in_control_internal(cmd, data, ifp, td);
255 * Generic internet control operations (ioctl's).
256 * Ifp is 0 if not an interface-specific ioctl.
258 * NOTE! td might be NULL.
262 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
270 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
276 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
279 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
280 ("recursive SIOC%cLIFADDR!",
281 cmd == SIOCDLIFADDR ? 'D' : 'A'));
283 return in_control_redispatch(cmd, data, ifp, td);
287 in_ialink_dispatch(netmsg_t msg)
289 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
290 struct ifaddr_container *ifac;
291 struct in_ifaddr_container *iac;
296 ifac = &ia->ia_ifa.ifa_containers[cpu];
297 ASSERT_IFAC_VALID(ifac);
298 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
299 ("ia is on in_ifaddrheads"));
301 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
302 iac = &ifac->ifa_proto_u.u_in_ifac;
303 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
307 ifa_forwardmsg(&msg->lmsg, cpu + 1);
311 in_iaunlink_dispatch(netmsg_t msg)
313 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
314 struct ifaddr_container *ifac;
315 struct in_ifaddr_container *iac;
320 ifac = &ia->ia_ifa.ifa_containers[cpu];
321 ASSERT_IFAC_VALID(ifac);
322 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
323 ("ia is not on in_ifaddrheads"));
325 iac = &ifac->ifa_proto_u.u_in_ifac;
326 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
327 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
331 ifa_forwardmsg(&msg->lmsg, cpu + 1);
335 in_iahashins_dispatch(netmsg_t msg)
337 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
338 struct ifaddr_container *ifac;
339 struct in_ifaddr_container *iac;
344 ifac = &ia->ia_ifa.ifa_containers[cpu];
345 ASSERT_IFAC_VALID(ifac);
346 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
347 ("ia is on in_ifaddrhashtbls"));
349 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
350 iac = &ifac->ifa_proto_u.u_in_ifac;
351 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
356 ifa_forwardmsg(&msg->lmsg, cpu + 1);
360 in_iahashrem_dispatch(netmsg_t msg)
362 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
363 struct ifaddr_container *ifac;
364 struct in_ifaddr_container *iac;
369 ifac = &ia->ia_ifa.ifa_containers[cpu];
370 ASSERT_IFAC_VALID(ifac);
371 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
372 ("ia is not on in_ifaddrhashtbls"));
374 iac = &ifac->ifa_proto_u.u_in_ifac;
375 LIST_REMOVE(iac, ia_hash);
376 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
380 ifa_forwardmsg(&msg->lmsg, cpu + 1);
384 in_ialink(struct in_ifaddr *ia)
386 struct netmsg_base msg;
388 netmsg_init(&msg, NULL, &curthread->td_msgport,
389 0, in_ialink_dispatch);
390 msg.lmsg.u.ms_resultp = ia;
392 ifa_domsg(&msg.lmsg, 0);
396 in_iaunlink(struct in_ifaddr *ia)
398 struct netmsg_base msg;
400 netmsg_init(&msg, NULL, &curthread->td_msgport,
401 0, in_iaunlink_dispatch);
402 msg.lmsg.u.ms_resultp = ia;
404 ifa_domsg(&msg.lmsg, 0);
408 in_iahash_insert(struct in_ifaddr *ia)
410 struct netmsg_base msg;
412 netmsg_init(&msg, NULL, &curthread->td_msgport,
413 0, in_iahashins_dispatch);
414 msg.lmsg.u.ms_resultp = ia;
416 ifa_domsg(&msg.lmsg, 0);
420 in_iahash_remove(struct in_ifaddr *ia)
422 struct netmsg_base msg;
424 netmsg_init(&msg, NULL, &curthread->td_msgport,
425 0, in_iahashrem_dispatch);
426 msg.lmsg.u.ms_resultp = ia;
428 ifa_domsg(&msg.lmsg, 0);
431 static __inline struct in_ifaddr *
432 in_ianext(struct in_ifaddr *oia)
434 struct ifaddr_container *ifac;
435 struct in_ifaddr_container *iac;
437 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
438 ASSERT_IFAC_VALID(ifac);
439 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
440 ("ia is not on in_ifaddrheads"));
442 iac = &ifac->ifa_proto_u.u_in_ifac;
443 iac = TAILQ_NEXT(iac, ia_link);
451 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
454 struct ifreq *ifr = (struct ifreq *)data;
455 struct in_ifaddr *ia = NULL;
457 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
458 struct ifaddr_container *ifac;
459 struct in_ifaddr_container *iac;
460 struct sockaddr_in oldaddr;
461 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
468 * Find address for this interface, if it exists.
470 * If an alias address was specified, find that one instead of
471 * the first one on the interface, if possible
474 struct in_ifaddr *iap;
476 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
477 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
479 if (iap->ia_ifp == ifp &&
480 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
486 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
488 iap = ifatoia(ifac->ifa);
489 if (iap->ia_addr.sin_family == AF_INET) {
496 if (ifp->if_flags & IFF_UP)
504 return (EADDRNOTAVAIL);
505 if (ifra->ifra_addr.sin_family == AF_INET) {
507 if (ia->ia_ifp == ifp &&
508 ia->ia_addr.sin_addr.s_addr ==
509 ifra->ifra_addr.sin_addr.s_addr)
513 if ((ifp->if_flags & IFF_POINTOPOINT) &&
514 cmd == SIOCAIFADDR &&
515 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
519 if (cmd == SIOCDIFADDR && ia == NULL)
520 return (EADDRNOTAVAIL);
525 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
529 return (EADDRNOTAVAIL);
531 if (cmd == SIOCSIFDSTADDR &&
532 (ifp->if_flags & IFF_POINTOPOINT) == 0)
539 ia = ifa_create(sizeof(*ia), M_WAITOK);
543 * Setup per-CPU information
545 for (i = 0; i < ncpus; ++i) {
546 ifac = &ifa->ifa_containers[i];
547 iac = &ifac->ifa_proto_u.u_in_ifac;
553 * Protect from NETISR_IP traversing address list
554 * while we're modifying it.
559 ifa_iflink(ifa, ifp, 1);
561 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
562 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
563 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
564 ia->ia_sockmask.sin_len = 8;
565 ia->ia_sockmask.sin_family = AF_INET;
566 if (ifp->if_flags & IFF_BROADCAST) {
567 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
568 ia->ia_broadaddr.sin_family = AF_INET;
571 if (!(ifp->if_flags & IFF_LOOPBACK))
580 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
589 return (EADDRNOTAVAIL);
595 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
599 if ((ifp->if_flags & IFF_BROADCAST) == 0)
601 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
605 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
607 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
611 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
615 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
617 oldaddr = ia->ia_dstaddr;
618 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
619 if (ifp->if_ioctl != NULL) {
620 ifnet_serialize_all(ifp);
621 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
622 td->td_proc->p_ucred);
623 ifnet_deserialize_all(ifp);
625 ia->ia_dstaddr = oldaddr;
629 if (ia->ia_flags & IFA_ROUTE) {
630 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
631 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
632 ia->ia_ifa.ifa_dstaddr =
633 (struct sockaddr *)&ia->ia_dstaddr;
634 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
639 if ((ifp->if_flags & IFF_BROADCAST) == 0)
641 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
645 error = in_ifinit(ifp, ia,
646 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
647 if (error != 0 && iaIsNew)
650 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
651 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
654 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
656 * Interface is brought up by in_ifinit()
657 * (via ifp->if_ioctl). We act as if the
658 * interface got IFF_UP flag turned on.
665 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
666 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
673 if (ia->ia_addr.sin_family == AF_INET) {
674 if (ifra->ifra_addr.sin_len == 0) {
675 ifra->ifra_addr = ia->ia_addr;
677 } else if (ifra->ifra_addr.sin_addr.s_addr ==
678 ia->ia_addr.sin_addr.s_addr) {
682 if (ifra->ifra_mask.sin_len) {
684 ia->ia_sockmask = ifra->ifra_mask;
685 ia->ia_sockmask.sin_family = AF_INET;
687 ntohl(ia->ia_sockmask.sin_addr.s_addr);
690 if ((ifp->if_flags & IFF_POINTOPOINT) &&
691 ifra->ifra_dstaddr.sin_family == AF_INET) {
693 ia->ia_dstaddr = ifra->ifra_dstaddr;
694 maskIsNew = 1; /* We lie; but the effect's the same */
696 if (ifra->ifra_addr.sin_family == AF_INET &&
697 (hostIsNew || maskIsNew))
698 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
700 if (error != 0 && iaIsNew)
703 if ((ifp->if_flags & IFF_BROADCAST) &&
704 ifra->ifra_broadaddr.sin_family == AF_INET)
705 ia->ia_broadaddr = ifra->ifra_broadaddr;
707 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
708 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
711 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
712 /* See the comment in SIOCSIFADDR */
719 * in_ifscrub kills the interface route.
723 * in_ifadown gets rid of all the rest of
724 * the routes. This is not quite the right
725 * thing to do, but at least if we are running
726 * a routing process they will come back.
728 in_ifadown(&ia->ia_ifa, 1);
729 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
735 if (ifp == NULL || ifp->if_ioctl == NULL)
737 ifnet_serialize_all(ifp);
738 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
739 ifnet_deserialize_all(ifp);
743 KKASSERT(cmd == SIOCDIFADDR ||
744 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
746 ifa_ifunlink(&ia->ia_ifa, ifp);
749 if (cmd == SIOCDIFADDR) {
750 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
751 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
752 in_iahash_remove(ia);
757 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
758 * already taken care of the deletion from hash table
760 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
761 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
762 ("SIOC%cIFADDR failed on new ia, "
763 "but the new ia is still in hash table",
764 cmd == SIOCSIFADDR ? 'S' : 'A'));
768 ifa_destroy(&ia->ia_ifa);
770 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
771 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
773 * Though the address assignment failed, the
774 * interface is brought up by in_ifinit()
775 * (via ifp->if_ioctl). With the hope that
776 * the interface has some valid addresses, we
777 * act as if IFF_UP flag was just set on the
781 * This could only be done after the failed
782 * address is unlinked from the global address
793 * SIOCGLIFADDR: get first address. (?!?)
794 * SIOCGLIFADDR with IFLR_PREFIX:
795 * get first address that matches the specified prefix.
796 * SIOCALIFADDR: add the specified address.
797 * SIOCALIFADDR with IFLR_PREFIX:
798 * EINVAL since we can't deduce hostid part of the address.
799 * SIOCDLIFADDR: delete the specified address.
800 * SIOCDLIFADDR with IFLR_PREFIX:
801 * delete the first address that matches the specified prefix.
803 * EINVAL on invalid parameters
804 * EADDRNOTAVAIL on prefix match failed/specified address not found
805 * other values may be returned from in_ioctl()
807 * NOTE! td might be NULL.
810 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
813 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
817 panic("invalid argument to in_lifaddr_ioctl");
823 /* address must be specified on GET with IFLR_PREFIX */
824 if ((iflr->flags & IFLR_PREFIX) == 0)
829 /* address must be specified on ADD and DELETE */
830 if (iflr->addr.ss_family != AF_INET)
832 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
834 /* XXX need improvement */
835 if (iflr->dstaddr.ss_family
836 && iflr->dstaddr.ss_family != AF_INET)
838 if (iflr->dstaddr.ss_family
839 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
842 default: /*shouldn't happen*/
845 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
851 struct in_aliasreq ifra;
853 if (iflr->flags & IFLR_PREFIX)
856 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
857 bzero(&ifra, sizeof ifra);
858 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
860 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
862 if (iflr->dstaddr.ss_family) { /*XXX*/
863 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
864 iflr->dstaddr.ss_len);
867 ifra.ifra_mask.sin_family = AF_INET;
868 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
869 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
871 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
876 struct ifaddr_container *ifac;
877 struct in_ifaddr *ia;
878 struct in_addr mask, candidate, match;
879 struct sockaddr_in *sin;
882 bzero(&mask, sizeof mask);
883 if (iflr->flags & IFLR_PREFIX) {
884 /* lookup a prefix rather than address. */
885 in_len2mask(&mask, iflr->prefixlen);
887 sin = (struct sockaddr_in *)&iflr->addr;
888 match.s_addr = sin->sin_addr.s_addr;
889 match.s_addr &= mask.s_addr;
891 /* if you set extra bits, that's wrong */
892 if (match.s_addr != sin->sin_addr.s_addr)
897 if (cmd == SIOCGLIFADDR) {
898 /* on getting an address, take the 1st match */
899 match.s_addr = 0; /* gcc4 warning */
902 /* on deleting an address, do exact match */
903 in_len2mask(&mask, 32);
904 sin = (struct sockaddr_in *)&iflr->addr;
905 match.s_addr = sin->sin_addr.s_addr;
911 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
912 struct ifaddr *ifa = ifac->ifa;
914 if (ifa->ifa_addr->sa_family != AF_INET6)
919 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
920 candidate.s_addr &= mask.s_addr;
921 if (candidate.s_addr == match.s_addr)
925 return EADDRNOTAVAIL;
926 ia = (struct in_ifaddr *)(ifac->ifa);
928 if (cmd == SIOCGLIFADDR) {
929 /* fill in the if_laddrreq structure */
930 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
932 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
933 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
934 ia->ia_dstaddr.sin_len);
936 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
939 in_mask2len(&ia->ia_sockmask.sin_addr);
941 iflr->flags = 0; /*XXX*/
945 struct in_aliasreq ifra;
947 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
948 bzero(&ifra, sizeof ifra);
949 bcopy(iflr->iflr_name, ifra.ifra_name,
950 sizeof ifra.ifra_name);
952 bcopy(&ia->ia_addr, &ifra.ifra_addr,
953 ia->ia_addr.sin_len);
954 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
955 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
956 ia->ia_dstaddr.sin_len);
958 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
959 ia->ia_sockmask.sin_len);
961 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
967 return EOPNOTSUPP; /*just for safety*/
971 * Delete any existing route for an interface.
974 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
980 * Initialize an interface's internet address
981 * and routing table entry.
984 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
985 const struct sockaddr_in *sin, int scrub)
987 u_long i = ntohl(sin->sin_addr.s_addr);
988 struct sockaddr_in oldaddr;
989 struct ifaddr_container *ifac;
990 int flags = RTF_UP, error = 0;
993 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
994 oldaddr = ia->ia_addr;
996 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
998 in_iahash_remove(ia);
1002 if (ia->ia_addr.sin_family == AF_INET)
1003 in_iahash_insert(ia);
1006 * Give the interface a chance to initialize
1007 * if this is its first address,
1008 * and to validate the address if necessary.
1010 if (ifp->if_ioctl != NULL) {
1011 ifnet_serialize_all(ifp);
1012 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1013 ifnet_deserialize_all(ifp);
1019 * Delete old route, if requested.
1022 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1023 in_ifscrub(ifp, ia);
1024 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1028 * Calculate netmask/subnetmask.
1031 ia->ia_netmask = IN_CLASSA_NET;
1032 else if (IN_CLASSB(i))
1033 ia->ia_netmask = IN_CLASSB_NET;
1035 ia->ia_netmask = IN_CLASSC_NET;
1037 * The subnet mask usually includes at least the standard network part,
1038 * but may may be smaller in the case of supernetting.
1039 * If it is set, we believe it.
1041 if (ia->ia_subnetmask == 0) {
1042 ia->ia_subnetmask = ia->ia_netmask;
1043 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1045 ia->ia_netmask &= ia->ia_subnetmask;
1047 ia->ia_net = i & ia->ia_netmask;
1048 ia->ia_subnet = i & ia->ia_subnetmask;
1049 in_socktrim(&ia->ia_sockmask);
1052 * Add route for the network.
1054 ia->ia_ifa.ifa_metric = ifp->if_metric;
1055 if (ifp->if_flags & IFF_BROADCAST) {
1056 ia->ia_broadaddr.sin_addr.s_addr =
1057 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1058 ia->ia_netbroadcast.s_addr =
1059 htonl(ia->ia_net | ~ ia->ia_netmask);
1060 } else if (ifp->if_flags & IFF_LOOPBACK) {
1061 ia->ia_dstaddr = ia->ia_addr;
1063 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1064 if (ia->ia_dstaddr.sin_family != AF_INET)
1070 * Don't add host routes for interface addresses of
1071 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1072 * possible to assign several such address pairs with consistent
1073 * results (no host route) and is required by BOOTP.
1075 * XXX: This is ugly ! There should be a way for the caller to
1076 * say that they don't want a host route.
1078 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1079 ia->ia_netmask != IN_CLASSA_NET ||
1080 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1081 error = in_addprefix(ia, flags);
1087 * If the interface supports multicast, join the "all hosts"
1088 * multicast group on that interface.
1090 if (ifp->if_flags & IFF_MULTICAST) {
1091 struct in_addr addr;
1093 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1094 in_addmulti(&addr, ifp);
1098 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1099 in_iahash_remove(ia);
1101 ia->ia_addr = oldaddr;
1103 in_iahash_insert(ia);
1107 #define rtinitflags(x) \
1108 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1112 * Add a route to prefix ("connected route" in cisco terminology).
1113 * Do nothing, if there are some interface addresses with the same
1114 * prefix already. This function assumes that the 'target' parent
1118 in_addprefix(struct in_ifaddr *target, int flags)
1120 struct in_ifaddr_container *iac;
1121 struct in_addr prefix, mask;
1126 * Don't add prefix routes for CARP interfaces.
1127 * Prefix routes creation is handled by CARP
1128 * interfaces themselves.
1130 if (target->ia_ifp->if_type == IFT_CARP)
1134 mask = target->ia_sockmask.sin_addr;
1135 if (flags & RTF_HOST) {
1136 prefix = target->ia_dstaddr.sin_addr;
1138 prefix = target->ia_addr.sin_addr;
1139 prefix.s_addr &= mask.s_addr;
1142 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1143 struct in_ifaddr *ia = iac->ia;
1146 /* Don't test against self */
1150 /* The tested address does not own a route entry */
1151 if ((ia->ia_flags & IFA_ROUTE) == 0)
1155 if (rtinitflags(ia)) {
1156 p = ia->ia_dstaddr.sin_addr;
1158 p = ia->ia_addr.sin_addr;
1159 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1161 if (prefix.s_addr != p.s_addr)
1165 * If the to-be-added address and the curretly being
1166 * tested address are not host addresses, we need to
1167 * take subnetmask into consideration.
1169 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1170 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1174 * If we got a matching prefix route inserted by other
1175 * interface address, we don't need to bother.
1181 * No one seem to have prefix route; insert it.
1183 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1185 target->ia_flags |= IFA_ROUTE;
1190 * Remove a route to prefix ("connected route" in cisco terminology).
1191 * Re-installs the route by using another interface address, if there's
1192 * one with the same prefix (otherwise we lose the route mistakenly).
1195 in_scrubprefix(struct in_ifaddr *target)
1197 struct in_ifaddr_container *iac;
1198 struct in_addr prefix, mask;
1203 * Don't scrub prefix routes for CARP interfaces.
1204 * Prefix routes deletion is handled by CARP
1205 * interfaces themselves.
1207 if (target->ia_ifp->if_type == IFT_CARP)
1211 if ((target->ia_flags & IFA_ROUTE) == 0)
1214 mask = target->ia_sockmask.sin_addr;
1215 if (rtinitflags(target)) {
1216 prefix = target->ia_dstaddr.sin_addr;
1218 prefix = target->ia_addr.sin_addr;
1219 prefix.s_addr &= mask.s_addr;
1222 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1223 struct in_ifaddr *ia = iac->ia;
1226 /* Don't test against self */
1230 /* The tested address already owns a route entry */
1231 if (ia->ia_flags & IFA_ROUTE)
1235 * The prefix route of the tested address should
1236 * never be installed if its parent interface is
1239 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1244 * Don't add prefix routes for CARP interfaces.
1245 * Prefix routes creation is handled by CARP
1246 * interfaces themselves.
1248 if (ia->ia_ifp->if_type == IFT_CARP)
1253 if (rtinitflags(ia)) {
1254 p = ia->ia_dstaddr.sin_addr;
1256 p = ia->ia_addr.sin_addr;
1257 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1259 if (prefix.s_addr != p.s_addr)
1263 * We don't need to test subnetmask here, as what we do
1264 * in in_addprefix(), since if the the tested address's
1265 * parent interface is UP, the tested address should own
1266 * a prefix route entry and we would never reach here.
1270 * If we got a matching prefix route, move IFA_ROUTE to him
1272 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1273 target->ia_flags &= ~IFA_ROUTE;
1275 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1277 ia->ia_flags |= IFA_ROUTE;
1282 * No candidates for this prefix route; just remove it.
1284 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1285 target->ia_flags &= ~IFA_ROUTE;
1291 * Return 1 if the address might be a local broadcast address.
1294 in_broadcast(struct in_addr in, struct ifnet *ifp)
1296 struct ifaddr_container *ifac;
1299 if (in.s_addr == INADDR_BROADCAST ||
1300 in.s_addr == INADDR_ANY)
1302 if (ifp == NULL || (ifp->if_flags & IFF_BROADCAST) == 0)
1304 t = ntohl(in.s_addr);
1306 * Look through the list of addresses for a match
1307 * with a broadcast address.
1309 #define ia ((struct in_ifaddr *)ifa)
1310 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1311 struct ifaddr *ifa = ifac->ifa;
1313 if (ifa->ifa_addr->sa_family == AF_INET &&
1314 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1315 in.s_addr == ia->ia_netbroadcast.s_addr ||
1317 * Check for old-style (host 0) broadcast.
1319 t == ia->ia_subnet || t == ia->ia_net) &&
1321 * Check for an all one subnetmask. These
1322 * only exist when an interface gets a secondary
1325 ia->ia_subnetmask != (u_long)0xffffffff)
1333 * Add an address to the list of IP multicast addresses for a given interface.
1336 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1338 struct in_multi *inm;
1340 struct sockaddr_in sin;
1341 struct ifmultiaddr *ifma;
1343 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1344 ("in_addmulti is not called in netisr0"));
1347 * Call generic routine to add membership or increment
1348 * refcount. It wants addresses in the form of a sockaddr,
1349 * so we build one here (being careful to zero the unused bytes).
1351 bzero(&sin, sizeof sin);
1352 sin.sin_family = AF_INET;
1353 sin.sin_len = sizeof sin;
1355 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1360 * If ifma->ifma_protospec is null, then if_addmulti() created
1361 * a new record. Otherwise, we are done.
1363 if (ifma->ifma_protospec != NULL)
1364 return ifma->ifma_protospec;
1366 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1367 inm->inm_addr = *ap;
1369 inm->inm_ifma = ifma;
1370 ifma->ifma_protospec = inm;
1371 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1374 * Let IGMP know that we have joined a new IP multicast group.
1376 igmp_joingroup(inm);
1381 * Delete a multicast address record.
1384 in_delmulti(struct in_multi *inm)
1386 struct ifmultiaddr *ifma;
1387 struct in_multi my_inm;
1389 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1390 ("in_delmulti is not called in netisr0"));
1392 ifma = inm->inm_ifma;
1393 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1394 if (ifma->ifma_refcount == 1) {
1396 * No remaining claims to this record; let IGMP know that
1397 * we are leaving the multicast group.
1398 * But do it after the if_delmulti() which might reset
1399 * the interface and nuke the packet.
1402 ifma->ifma_protospec = NULL;
1403 LIST_REMOVE(inm, inm_link);
1404 kfree(inm, M_IPMADDR);
1406 /* XXX - should be separate API for when we have an ifma? */
1407 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1408 if (my_inm.inm_ifp != NULL)
1409 igmp_leavegroup(&my_inm);
1413 in_ifdetach_dispatch(netmsg_t nmsg)
1415 struct lwkt_msg *lmsg = &nmsg->lmsg;
1416 struct ifnet *ifp = lmsg->u.ms_resultp;
1418 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1419 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
1421 lwkt_replymsg(lmsg, 0);
1425 in_ifdetach(struct ifnet *ifp)
1427 struct netmsg_base nmsg;
1428 struct lwkt_msg *lmsg = &nmsg.lmsg;
1430 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1431 in_ifdetach_dispatch);
1432 lmsg->u.ms_resultp = ifp;
1434 lwkt_domsg(netisr_cpuport(0), lmsg, 0);