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"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/malloc.h>
45 #include <sys/msgport.h>
46 #include <sys/socket.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/netmsg2.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/in_pcb.h>
61 #include <netinet/igmp_var.h>
63 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
65 static int in_mask2len (struct in_addr *);
66 static void in_len2mask (struct in_addr *, int);
67 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
68 struct ifnet *, struct thread *);
70 static void in_socktrim (struct sockaddr_in *);
71 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
72 const struct sockaddr_in *, int);
74 static void in_control_dispatch(struct netmsg *);
75 static int in_control_internal(u_long, caddr_t, struct ifnet *,
78 static int 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,
83 &subnetsarelocal, 0, "");
85 struct in_multihead in_multihead; /* XXX BSS initialization */
87 extern struct inpcbinfo ripcbinfo;
88 extern struct inpcbinfo udbinfo;
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 */
196 struct in_control_arg {
204 in_control_dispatch(struct netmsg *nmsg)
206 struct lwkt_msg *msg = &nmsg->nm_lmsg;
207 const struct in_control_arg *arg = msg->u.ms_resultp;
210 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
211 lwkt_replymsg(msg, error);
215 * Generic internet control operations (ioctl's).
216 * Ifp is 0 if not an interface-specific ioctl.
218 * NOTE! td might be NULL.
222 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
226 struct in_control_arg arg;
227 struct lwkt_msg *msg;
233 if (td && (error = suser(td)) != 0)
239 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
242 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
243 ("recursive SIOC%cLIFADDR!\n",
244 cmd == SIOCDLIFADDR ? 'D' : 'A'));
247 * IFADDR alterations are serialized by netisr0
256 bzero(&arg, sizeof(arg));
262 netmsg_init(&nmsg, &curthread->td_msgport, 0,
263 in_control_dispatch);
265 msg->u.ms_resultp = &arg;
267 lwkt_domsg(cpu_portfn(0), msg, 0);
268 return msg->ms_error;
270 return in_control_internal(cmd, data, ifp, td);
275 in_ialink_dispatch(struct netmsg *nmsg)
277 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
278 struct in_ifaddr *ia = lmsg->u.ms_resultp;
279 struct ifaddr_container *ifac;
280 struct in_ifaddr_container *iac;
285 ifac = &ia->ia_ifa.ifa_containers[cpu];
286 ASSERT_IFAC_VALID(ifac);
287 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
288 ("ia is on in_ifaddrheads\n"));
290 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
291 iac = &ifac->ifa_proto_u.u_in_ifac;
292 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
296 ifa_forwardmsg(lmsg, cpu + 1);
300 in_iaunlink_dispatch(struct netmsg *nmsg)
302 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
303 struct in_ifaddr *ia = lmsg->u.ms_resultp;
304 struct ifaddr_container *ifac;
305 struct in_ifaddr_container *iac;
310 ifac = &ia->ia_ifa.ifa_containers[cpu];
311 ASSERT_IFAC_VALID(ifac);
312 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
313 ("ia is not on in_ifaddrheads\n"));
315 iac = &ifac->ifa_proto_u.u_in_ifac;
316 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
317 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
321 ifa_forwardmsg(lmsg, cpu + 1);
325 in_iahashins_dispatch(struct netmsg *nmsg)
327 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
328 struct in_ifaddr *ia = lmsg->u.ms_resultp;
329 struct ifaddr_container *ifac;
330 struct in_ifaddr_container *iac;
335 ifac = &ia->ia_ifa.ifa_containers[cpu];
336 ASSERT_IFAC_VALID(ifac);
337 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
338 ("ia is on in_ifaddrhashtbls\n"));
340 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
341 iac = &ifac->ifa_proto_u.u_in_ifac;
342 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
347 ifa_forwardmsg(lmsg, cpu + 1);
351 in_iahashrem_dispatch(struct netmsg *nmsg)
353 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
354 struct in_ifaddr *ia = lmsg->u.ms_resultp;
355 struct ifaddr_container *ifac;
356 struct in_ifaddr_container *iac;
361 ifac = &ia->ia_ifa.ifa_containers[cpu];
362 ASSERT_IFAC_VALID(ifac);
363 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
364 ("ia is not on in_ifaddrhashtbls\n"));
366 iac = &ifac->ifa_proto_u.u_in_ifac;
367 LIST_REMOVE(iac, ia_hash);
368 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
372 ifa_forwardmsg(lmsg, cpu + 1);
376 in_ialink(struct in_ifaddr *ia)
379 struct lwkt_msg *lmsg;
381 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch);
382 lmsg = &nmsg.nm_lmsg;
383 lmsg->u.ms_resultp = ia;
389 in_iaunlink(struct in_ifaddr *ia)
392 struct lwkt_msg *lmsg;
394 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch);
395 lmsg = &nmsg.nm_lmsg;
396 lmsg->u.ms_resultp = ia;
402 in_iahash_insert(struct in_ifaddr *ia)
405 struct lwkt_msg *lmsg;
407 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashins_dispatch);
408 lmsg = &nmsg.nm_lmsg;
409 lmsg->u.ms_resultp = ia;
415 in_iahash_remove(struct in_ifaddr *ia)
418 struct lwkt_msg *lmsg;
420 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashrem_dispatch);
421 lmsg = &nmsg.nm_lmsg;
422 lmsg->u.ms_resultp = ia;
427 static __inline struct in_ifaddr *
428 in_ianext(struct in_ifaddr *oia)
430 struct ifaddr_container *ifac;
431 struct in_ifaddr_container *iac;
433 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
434 ASSERT_IFAC_VALID(ifac);
435 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
436 ("ia is not on in_ifaddrheads\n"));
438 iac = &ifac->ifa_proto_u.u_in_ifac;
439 iac = TAILQ_NEXT(iac, ia_link);
447 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
450 struct ifreq *ifr = (struct ifreq *)data;
451 struct in_ifaddr *ia = NULL;
453 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
454 struct ifaddr_container *ifac;
455 struct in_ifaddr_container *iac;
456 struct sockaddr_in oldaddr;
457 int hostIsNew, iaIsNew, maskIsNew;
463 * Find address for this interface, if it exists.
465 * If an alias address was specified, find that one instead of
466 * the first one on the interface, if possible
469 struct in_ifaddr *iap;
471 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
472 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
474 if (iap->ia_ifp == ifp &&
475 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
481 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
483 iap = ifatoia(ifac->ifa);
484 if (iap->ia_addr.sin_family == AF_INET) {
496 return (EADDRNOTAVAIL);
497 if (ifra->ifra_addr.sin_family == AF_INET) {
499 if (ia->ia_ifp == ifp &&
500 ia->ia_addr.sin_addr.s_addr ==
501 ifra->ifra_addr.sin_addr.s_addr)
505 if ((ifp->if_flags & IFF_POINTOPOINT) &&
506 cmd == SIOCAIFADDR &&
507 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
511 if (cmd == SIOCDIFADDR && ia == NULL)
512 return (EADDRNOTAVAIL);
517 if (td && (error = suser(td)) != 0)
521 return (EADDRNOTAVAIL);
523 if (cmd == SIOCSIFDSTADDR &&
524 (ifp->if_flags & IFF_POINTOPOINT) == 0)
531 ia = ifa_create(sizeof(*ia), M_WAITOK);
535 * Setup per-CPU information
537 for (i = 0; i < ncpus; ++i) {
538 ifac = &ifa->ifa_containers[i];
539 iac = &ifac->ifa_proto_u.u_in_ifac;
545 * Protect from NETISR_IP traversing address list
546 * while we're modifying it.
551 ifa_iflink(ifa, ifp, 1);
553 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
554 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
555 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
556 ia->ia_sockmask.sin_len = 8;
557 ia->ia_sockmask.sin_family = AF_INET;
558 if (ifp->if_flags & IFF_BROADCAST) {
559 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
560 ia->ia_broadaddr.sin_family = AF_INET;
563 if (!(ifp->if_flags & IFF_LOOPBACK))
572 if (td && (error = suser(td)) != 0)
581 return (EADDRNOTAVAIL);
587 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
591 if ((ifp->if_flags & IFF_BROADCAST) == 0)
593 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
597 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
599 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
603 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
607 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
609 oldaddr = ia->ia_dstaddr;
610 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
611 if (ifp->if_ioctl != NULL) {
612 lwkt_serialize_enter(ifp->if_serializer);
613 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
614 td->td_proc->p_ucred);
615 lwkt_serialize_exit(ifp->if_serializer);
617 ia->ia_dstaddr = oldaddr;
621 if (ia->ia_flags & IFA_ROUTE) {
622 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
623 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
624 ia->ia_ifa.ifa_dstaddr =
625 (struct sockaddr *)&ia->ia_dstaddr;
626 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
631 if ((ifp->if_flags & IFF_BROADCAST) == 0)
633 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
637 error = in_ifinit(ifp, ia,
638 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
639 if (error != 0 && iaIsNew)
642 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
643 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
649 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
650 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
657 if (ia->ia_addr.sin_family == AF_INET) {
658 if (ifra->ifra_addr.sin_len == 0) {
659 ifra->ifra_addr = ia->ia_addr;
661 } else if (ifra->ifra_addr.sin_addr.s_addr ==
662 ia->ia_addr.sin_addr.s_addr) {
666 if (ifra->ifra_mask.sin_len) {
668 ia->ia_sockmask = ifra->ifra_mask;
669 ia->ia_sockmask.sin_family = AF_INET;
671 ntohl(ia->ia_sockmask.sin_addr.s_addr);
674 if ((ifp->if_flags & IFF_POINTOPOINT) &&
675 ifra->ifra_dstaddr.sin_family == AF_INET) {
677 ia->ia_dstaddr = ifra->ifra_dstaddr;
678 maskIsNew = 1; /* We lie; but the effect's the same */
680 if (ifra->ifra_addr.sin_family == AF_INET &&
681 (hostIsNew || maskIsNew))
682 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
684 if (error != 0 && iaIsNew)
687 if ((ifp->if_flags & IFF_BROADCAST) &&
688 ifra->ifra_broadaddr.sin_family == AF_INET)
689 ia->ia_broadaddr = ifra->ifra_broadaddr;
691 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
692 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
699 * in_ifscrub kills the interface route.
703 * in_ifadown gets rid of all the rest of
704 * the routes. This is not quite the right
705 * thing to do, but at least if we are running
706 * a routing process they will come back.
708 in_ifadown(&ia->ia_ifa, 1);
709 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
715 if (ifp == NULL || ifp->if_ioctl == NULL)
717 lwkt_serialize_enter(ifp->if_serializer);
718 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
719 lwkt_serialize_exit(ifp->if_serializer);
723 KKASSERT(cmd == SIOCDIFADDR ||
724 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
726 ifa_ifunlink(&ia->ia_ifa, ifp);
729 if (cmd == SIOCDIFADDR) {
730 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
731 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
732 in_iahash_remove(ia);
737 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
738 * already taken care of the deletion from hash table
740 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
741 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
742 ("SIOC%cIFADDR failed on new ia, "
743 "but the new ia is still in hash table\n",
744 cmd == SIOCSIFADDR ? 'S' : 'A'));
748 ifa_destroy(&ia->ia_ifa);
755 * SIOCGLIFADDR: get first address. (?!?)
756 * SIOCGLIFADDR with IFLR_PREFIX:
757 * get first address that matches the specified prefix.
758 * SIOCALIFADDR: add the specified address.
759 * SIOCALIFADDR with IFLR_PREFIX:
760 * EINVAL since we can't deduce hostid part of the address.
761 * SIOCDLIFADDR: delete the specified address.
762 * SIOCDLIFADDR with IFLR_PREFIX:
763 * delete the first address that matches the specified prefix.
765 * EINVAL on invalid parameters
766 * EADDRNOTAVAIL on prefix match failed/specified address not found
767 * other values may be returned from in_ioctl()
769 * NOTE! td might be NULL.
772 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
775 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
779 panic("invalid argument to in_lifaddr_ioctl");
785 /* address must be specified on GET with IFLR_PREFIX */
786 if ((iflr->flags & IFLR_PREFIX) == 0)
791 /* address must be specified on ADD and DELETE */
792 if (iflr->addr.ss_family != AF_INET)
794 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
796 /* XXX need improvement */
797 if (iflr->dstaddr.ss_family
798 && iflr->dstaddr.ss_family != AF_INET)
800 if (iflr->dstaddr.ss_family
801 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
804 default: /*shouldn't happen*/
807 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
813 struct in_aliasreq ifra;
815 if (iflr->flags & IFLR_PREFIX)
818 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
819 bzero(&ifra, sizeof ifra);
820 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
822 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
824 if (iflr->dstaddr.ss_family) { /*XXX*/
825 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
826 iflr->dstaddr.ss_len);
829 ifra.ifra_mask.sin_family = AF_INET;
830 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
831 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
833 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
838 struct ifaddr_container *ifac;
839 struct in_ifaddr *ia;
840 struct in_addr mask, candidate, match;
841 struct sockaddr_in *sin;
844 bzero(&mask, sizeof mask);
845 if (iflr->flags & IFLR_PREFIX) {
846 /* lookup a prefix rather than address. */
847 in_len2mask(&mask, iflr->prefixlen);
849 sin = (struct sockaddr_in *)&iflr->addr;
850 match.s_addr = sin->sin_addr.s_addr;
851 match.s_addr &= mask.s_addr;
853 /* if you set extra bits, that's wrong */
854 if (match.s_addr != sin->sin_addr.s_addr)
859 if (cmd == SIOCGLIFADDR) {
860 /* on getting an address, take the 1st match */
861 match.s_addr = 0; /* gcc4 warning */
864 /* on deleting an address, do exact match */
865 in_len2mask(&mask, 32);
866 sin = (struct sockaddr_in *)&iflr->addr;
867 match.s_addr = sin->sin_addr.s_addr;
873 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
874 struct ifaddr *ifa = ifac->ifa;
876 if (ifa->ifa_addr->sa_family != AF_INET6)
881 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
882 candidate.s_addr &= mask.s_addr;
883 if (candidate.s_addr == match.s_addr)
887 return EADDRNOTAVAIL;
888 ia = (struct in_ifaddr *)(ifac->ifa);
890 if (cmd == SIOCGLIFADDR) {
891 /* fill in the if_laddrreq structure */
892 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
894 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
895 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
896 ia->ia_dstaddr.sin_len);
898 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
901 in_mask2len(&ia->ia_sockmask.sin_addr);
903 iflr->flags = 0; /*XXX*/
907 struct in_aliasreq ifra;
909 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
910 bzero(&ifra, sizeof ifra);
911 bcopy(iflr->iflr_name, ifra.ifra_name,
912 sizeof ifra.ifra_name);
914 bcopy(&ia->ia_addr, &ifra.ifra_addr,
915 ia->ia_addr.sin_len);
916 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
917 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
918 ia->ia_dstaddr.sin_len);
920 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
921 ia->ia_sockmask.sin_len);
923 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
929 return EOPNOTSUPP; /*just for safety*/
933 * Delete any existing route for an interface.
936 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
942 * Initialize an interface's internet address
943 * and routing table entry.
946 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
947 const struct sockaddr_in *sin, int scrub)
949 u_long i = ntohl(sin->sin_addr.s_addr);
950 struct sockaddr_in oldaddr;
951 struct ifaddr_container *ifac;
952 int flags = RTF_UP, error = 0;
955 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
956 oldaddr = ia->ia_addr;
958 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
960 in_iahash_remove(ia);
964 if (ia->ia_addr.sin_family == AF_INET)
965 in_iahash_insert(ia);
968 * Give the interface a chance to initialize
969 * if this is its first address,
970 * and to validate the address if necessary.
972 if (ifp->if_ioctl != NULL) {
973 lwkt_serialize_enter(ifp->if_serializer);
974 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
975 lwkt_serialize_exit(ifp->if_serializer);
981 * Delete old route, if requested.
984 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
986 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
990 * Calculate netmask/subnetmask.
993 ia->ia_netmask = IN_CLASSA_NET;
994 else if (IN_CLASSB(i))
995 ia->ia_netmask = IN_CLASSB_NET;
997 ia->ia_netmask = IN_CLASSC_NET;
999 * The subnet mask usually includes at least the standard network part,
1000 * but may may be smaller in the case of supernetting.
1001 * If it is set, we believe it.
1003 if (ia->ia_subnetmask == 0) {
1004 ia->ia_subnetmask = ia->ia_netmask;
1005 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1007 ia->ia_netmask &= ia->ia_subnetmask;
1009 ia->ia_net = i & ia->ia_netmask;
1010 ia->ia_subnet = i & ia->ia_subnetmask;
1011 in_socktrim(&ia->ia_sockmask);
1014 * Add route for the network.
1016 ia->ia_ifa.ifa_metric = ifp->if_metric;
1017 if (ifp->if_flags & IFF_BROADCAST) {
1018 ia->ia_broadaddr.sin_addr.s_addr =
1019 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1020 ia->ia_netbroadcast.s_addr =
1021 htonl(ia->ia_net | ~ ia->ia_netmask);
1022 } else if (ifp->if_flags & IFF_LOOPBACK) {
1023 ia->ia_dstaddr = ia->ia_addr;
1025 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1026 if (ia->ia_dstaddr.sin_family != AF_INET)
1032 * Don't add host routes for interface addresses of
1033 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1034 * possible to assign several such address pairs with consistent
1035 * results (no host route) and is required by BOOTP.
1037 * XXX: This is ugly ! There should be a way for the caller to
1038 * say that they don't want a host route.
1040 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1041 ia->ia_netmask != IN_CLASSA_NET ||
1042 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1043 error = in_addprefix(ia, flags);
1049 * If the interface supports multicast, join the "all hosts"
1050 * multicast group on that interface.
1052 if (ifp->if_flags & IFF_MULTICAST) {
1053 struct in_addr addr;
1055 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1056 in_addmulti(&addr, ifp);
1060 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1061 in_iahash_remove(ia);
1063 ia->ia_addr = oldaddr;
1065 in_iahash_insert(ia);
1069 #define rtinitflags(x) \
1070 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1074 * Add a route to prefix ("connected route" in cisco terminology).
1075 * Do nothing, if there are some interface addresses with the same
1076 * prefix already. This function assumes that the 'target' parent
1080 in_addprefix(struct in_ifaddr *target, int flags)
1082 struct in_ifaddr_container *iac;
1083 struct in_addr prefix, mask;
1086 mask = target->ia_sockmask.sin_addr;
1087 if (flags & RTF_HOST) {
1088 prefix = target->ia_dstaddr.sin_addr;
1090 prefix = target->ia_addr.sin_addr;
1091 prefix.s_addr &= mask.s_addr;
1094 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1095 struct in_ifaddr *ia = iac->ia;
1098 /* Don't test against self */
1102 /* The tested address does not own a route entry */
1103 if ((ia->ia_flags & IFA_ROUTE) == 0)
1107 if (rtinitflags(ia)) {
1108 p = ia->ia_dstaddr.sin_addr;
1110 p = ia->ia_addr.sin_addr;
1111 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1113 if (prefix.s_addr != p.s_addr)
1117 * If the to-be-added address and the curretly being
1118 * tested address are not host addresses, we need to
1119 * take subnetmask into consideration.
1121 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1122 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1126 * If we got a matching prefix route inserted by other
1127 * interface address, we don't need to bother.
1133 * No one seem to have prefix route; insert it.
1135 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1137 target->ia_flags |= IFA_ROUTE;
1142 * Remove a route to prefix ("connected route" in cisco terminology).
1143 * Re-installs the route by using another interface address, if there's
1144 * one with the same prefix (otherwise we lose the route mistakenly).
1147 in_scrubprefix(struct in_ifaddr *target)
1149 struct in_ifaddr_container *iac;
1150 struct in_addr prefix, mask;
1153 if ((target->ia_flags & IFA_ROUTE) == 0)
1156 mask = target->ia_sockmask.sin_addr;
1157 if (rtinitflags(target)) {
1158 prefix = target->ia_dstaddr.sin_addr;
1160 prefix = target->ia_addr.sin_addr;
1161 prefix.s_addr &= mask.s_addr;
1164 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1165 struct in_ifaddr *ia = iac->ia;
1168 /* Don't test against self */
1172 /* The tested address already owns a route entry */
1173 if (ia->ia_flags & IFA_ROUTE)
1177 * The prefix route of the tested address should
1178 * never be installed if its parent interface is
1181 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1185 if (rtinitflags(ia)) {
1186 p = ia->ia_dstaddr.sin_addr;
1188 p = ia->ia_addr.sin_addr;
1189 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1191 if (prefix.s_addr != p.s_addr)
1195 * We don't need to test subnetmask here, as what we do
1196 * in in_addprefix(), since if the the tested address's
1197 * parent interface is UP, the tested address should own
1198 * a prefix route entry and we would never reach here.
1202 * If we got a matching prefix route, move IFA_ROUTE to him
1204 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1205 target->ia_flags &= ~IFA_ROUTE;
1207 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1209 ia->ia_flags |= IFA_ROUTE;
1214 * No candidates for this prefix route; just remove it.
1216 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1217 target->ia_flags &= ~IFA_ROUTE;
1223 * Return 1 if the address might be a local broadcast address.
1226 in_broadcast(struct in_addr in, struct ifnet *ifp)
1228 struct ifaddr_container *ifac;
1231 if (in.s_addr == INADDR_BROADCAST ||
1232 in.s_addr == INADDR_ANY)
1234 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1236 t = ntohl(in.s_addr);
1238 * Look through the list of addresses for a match
1239 * with a broadcast address.
1241 #define ia ((struct in_ifaddr *)ifa)
1242 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1243 struct ifaddr *ifa = ifac->ifa;
1245 if (ifa->ifa_addr->sa_family == AF_INET &&
1246 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1247 in.s_addr == ia->ia_netbroadcast.s_addr ||
1249 * Check for old-style (host 0) broadcast.
1251 t == ia->ia_subnet || t == ia->ia_net) &&
1253 * Check for an all one subnetmask. These
1254 * only exist when an interface gets a secondary
1257 ia->ia_subnetmask != (u_long)0xffffffff)
1264 * Add an address to the list of IP multicast addresses for a given interface.
1267 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1269 struct in_multi *inm;
1271 struct sockaddr_in sin;
1272 struct ifmultiaddr *ifma;
1275 * Call generic routine to add membership or increment
1276 * refcount. It wants addresses in the form of a sockaddr,
1277 * so we build one here (being careful to zero the unused bytes).
1279 bzero(&sin, sizeof sin);
1280 sin.sin_family = AF_INET;
1281 sin.sin_len = sizeof sin;
1284 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1291 * If ifma->ifma_protospec is null, then if_addmulti() created
1292 * a new record. Otherwise, we are done.
1294 if (ifma->ifma_protospec != 0) {
1296 return ifma->ifma_protospec;
1299 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1300 at interrupt time? If so, need to fix if_addmulti. XXX */
1301 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1302 inm->inm_addr = *ap;
1304 inm->inm_ifma = ifma;
1305 ifma->ifma_protospec = inm;
1306 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1309 * Let IGMP know that we have joined a new IP multicast group.
1311 igmp_joingroup(inm);
1317 * Delete a multicast address record.
1320 in_delmulti(struct in_multi *inm)
1322 struct ifmultiaddr *ifma;
1323 struct in_multi my_inm;
1326 ifma = inm->inm_ifma;
1327 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1328 if (ifma->ifma_refcount == 1) {
1330 * No remaining claims to this record; let IGMP know that
1331 * we are leaving the multicast group.
1332 * But do it after the if_delmulti() which might reset
1333 * the interface and nuke the packet.
1336 ifma->ifma_protospec = 0;
1337 LIST_REMOVE(inm, inm_link);
1338 kfree(inm, M_IPMADDR);
1340 /* XXX - should be separate API for when we have an ifma? */
1341 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1342 if (my_inm.inm_ifp != NULL)
1343 igmp_leavegroup(&my_inm);
1348 in_ifdetach(struct ifnet *ifp)
1350 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1351 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);