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>
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>
62 #include <netinet/igmp_var.h>
64 MALLOC_DEFINE(M_IPMADDR
, "in_multi", "internet multicast address");
66 static int in_mask2len (struct in_addr
*);
67 static void in_len2mask (struct in_addr
*, int);
68 static int in_lifaddr_ioctl (struct socket
*, u_long
, caddr_t
,
69 struct ifnet
*, struct thread
*);
71 static void in_socktrim (struct sockaddr_in
*);
72 static int in_ifinit(struct ifnet
*, struct in_ifaddr
*,
73 const struct sockaddr_in
*, int);
75 static void in_control_dispatch(struct netmsg
*);
76 static int in_control_internal(u_long
, caddr_t
, struct ifnet
*,
79 static int in_addprefix(struct in_ifaddr
*, int);
80 static void in_scrubprefix(struct in_ifaddr
*);
82 static int subnetsarelocal
= 0;
83 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
, CTLFLAG_RW
,
84 &subnetsarelocal
, 0, "");
86 struct in_multihead in_multihead
; /* XXX BSS initialization */
88 extern struct inpcbinfo ripcbinfo
;
89 extern struct inpcbinfo udbinfo
;
92 * Return 1 if an internet address is for a ``local'' host
93 * (one to which we have a connection). If subnetsarelocal
94 * is true, this includes other subnets of the local net.
95 * Otherwise, it includes only the directly-connected (sub)nets.
98 in_localaddr(struct in_addr in
)
100 u_long i
= ntohl(in
.s_addr
);
101 struct in_ifaddr_container
*iac
;
102 struct in_ifaddr
*ia
;
104 if (subnetsarelocal
) {
105 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
108 if ((i
& ia
->ia_netmask
) == ia
->ia_net
)
112 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
115 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
)
123 * Determine whether an IP address is in a reserved set of addresses
124 * that may not be forwarded, or whether datagrams to that destination
128 in_canforward(struct in_addr in
)
130 u_long i
= ntohl(in
.s_addr
);
133 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
))
136 net
= i
& IN_CLASSA_NET
;
137 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))
144 * Trim a mask in a sockaddr
147 in_socktrim(struct sockaddr_in
*ap
)
149 char *cplim
= (char *) &ap
->sin_addr
;
150 char *cp
= (char *) (&ap
->sin_addr
+ 1);
153 while (--cp
>= cplim
)
155 (ap
)->sin_len
= cp
- (char *) (ap
) + 1;
161 in_mask2len(struct in_addr
*mask
)
167 for (x
= 0; x
< sizeof *mask
; x
++) {
172 if (x
< sizeof *mask
) {
173 for (y
= 0; y
< 8; y
++) {
174 if ((p
[x
] & (0x80 >> y
)) == 0)
182 in_len2mask(struct in_addr
*mask
, int len
)
188 bzero(mask
, sizeof *mask
);
189 for (i
= 0; i
< len
/ 8; i
++)
192 p
[i
] = (0xff00 >> (len
% 8)) & 0xff;
195 static int in_interfaces
; /* number of external internet interfaces */
197 struct in_control_arg
{
205 in_control_dispatch(struct netmsg
*nmsg
)
207 struct lwkt_msg
*msg
= &nmsg
->nm_lmsg
;
208 const struct in_control_arg
*arg
= msg
->u
.ms_resultp
;
211 error
= in_control_internal(arg
->cmd
, arg
->data
, arg
->ifp
, arg
->td
);
212 lwkt_replymsg(msg
, error
);
216 * Generic internet control operations (ioctl's).
217 * Ifp is 0 if not an interface-specific ioctl.
219 * NOTE! td might be NULL.
223 in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
227 struct in_control_arg arg
;
228 struct lwkt_msg
*msg
;
234 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
240 return in_lifaddr_ioctl(so
, cmd
, data
, ifp
, td
);
243 KASSERT(cmd
!= SIOCALIFADDR
&& cmd
!= SIOCDLIFADDR
,
244 ("recursive SIOC%cLIFADDR!\n",
245 cmd
== SIOCDLIFADDR
? 'D' : 'A'));
248 * IFADDR alterations are serialized by netisr0
257 bzero(&arg
, sizeof(arg
));
263 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0,
264 in_control_dispatch
);
266 msg
->u
.ms_resultp
= &arg
;
268 lwkt_domsg(cpu_portfn(0), msg
, 0);
269 return msg
->ms_error
;
271 return in_control_internal(cmd
, data
, ifp
, td
);
276 in_ialink_dispatch(struct netmsg
*nmsg
)
278 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
279 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
280 struct ifaddr_container
*ifac
;
281 struct in_ifaddr_container
*iac
;
286 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
287 ASSERT_IFAC_VALID(ifac
);
288 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
) == 0,
289 ("ia is on in_ifaddrheads\n"));
291 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHEAD
;
292 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
293 TAILQ_INSERT_TAIL(&in_ifaddrheads
[cpu
], iac
, ia_link
);
297 ifa_forwardmsg(lmsg
, cpu
+ 1);
301 in_iaunlink_dispatch(struct netmsg
*nmsg
)
303 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
304 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
305 struct ifaddr_container
*ifac
;
306 struct in_ifaddr_container
*iac
;
311 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
312 ASSERT_IFAC_VALID(ifac
);
313 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
314 ("ia is not on in_ifaddrheads\n"));
316 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
317 TAILQ_REMOVE(&in_ifaddrheads
[cpu
], iac
, ia_link
);
318 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHEAD
;
322 ifa_forwardmsg(lmsg
, cpu
+ 1);
326 in_iahashins_dispatch(struct netmsg
*nmsg
)
328 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
329 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
330 struct ifaddr_container
*ifac
;
331 struct in_ifaddr_container
*iac
;
336 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
337 ASSERT_IFAC_VALID(ifac
);
338 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) == 0,
339 ("ia is on in_ifaddrhashtbls\n"));
341 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHASH
;
342 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
343 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
348 ifa_forwardmsg(lmsg
, cpu
+ 1);
352 in_iahashrem_dispatch(struct netmsg
*nmsg
)
354 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
355 struct in_ifaddr
*ia
= lmsg
->u
.ms_resultp
;
356 struct ifaddr_container
*ifac
;
357 struct in_ifaddr_container
*iac
;
362 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
363 ASSERT_IFAC_VALID(ifac
);
364 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
,
365 ("ia is not on in_ifaddrhashtbls\n"));
367 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
368 LIST_REMOVE(iac
, ia_hash
);
369 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHASH
;
373 ifa_forwardmsg(lmsg
, cpu
+ 1);
377 in_ialink(struct in_ifaddr
*ia
)
380 struct lwkt_msg
*lmsg
;
382 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0, in_ialink_dispatch
);
383 lmsg
= &nmsg
.nm_lmsg
;
384 lmsg
->u
.ms_resultp
= ia
;
390 in_iaunlink(struct in_ifaddr
*ia
)
393 struct lwkt_msg
*lmsg
;
395 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0, in_iaunlink_dispatch
);
396 lmsg
= &nmsg
.nm_lmsg
;
397 lmsg
->u
.ms_resultp
= ia
;
403 in_iahash_insert(struct in_ifaddr
*ia
)
406 struct lwkt_msg
*lmsg
;
408 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0, in_iahashins_dispatch
);
409 lmsg
= &nmsg
.nm_lmsg
;
410 lmsg
->u
.ms_resultp
= ia
;
416 in_iahash_remove(struct in_ifaddr
*ia
)
419 struct lwkt_msg
*lmsg
;
421 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0, in_iahashrem_dispatch
);
422 lmsg
= &nmsg
.nm_lmsg
;
423 lmsg
->u
.ms_resultp
= ia
;
428 static __inline
struct in_ifaddr
*
429 in_ianext(struct in_ifaddr
*oia
)
431 struct ifaddr_container
*ifac
;
432 struct in_ifaddr_container
*iac
;
434 ifac
= &oia
->ia_ifa
.ifa_containers
[mycpuid
];
435 ASSERT_IFAC_VALID(ifac
);
436 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
437 ("ia is not on in_ifaddrheads\n"));
439 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
440 iac
= TAILQ_NEXT(iac
, ia_link
);
448 in_control_internal(u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
451 struct ifreq
*ifr
= (struct ifreq
*)data
;
452 struct in_ifaddr
*ia
= NULL
;
454 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)data
;
455 struct ifaddr_container
*ifac
;
456 struct in_ifaddr_container
*iac
;
457 struct sockaddr_in oldaddr
;
458 int hostIsNew
, iaIsNew
, maskIsNew
, ifpWasUp
;
465 * Find address for this interface, if it exists.
467 * If an alias address was specified, find that one instead of
468 * the first one on the interface, if possible
471 struct in_ifaddr
*iap
;
473 dst
= ((struct sockaddr_in
*)&ifr
->ifr_addr
)->sin_addr
;
474 LIST_FOREACH(iac
, INADDR_HASH(dst
.s_addr
), ia_hash
) {
476 if (iap
->ia_ifp
== ifp
&&
477 iap
->ia_addr
.sin_addr
.s_addr
== dst
.s_addr
) {
483 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
],
485 iap
= ifatoia(ifac
->ifa
);
486 if (iap
->ia_addr
.sin_family
== AF_INET
) {
493 if (ifp
->if_flags
& IFF_UP
)
501 return (EADDRNOTAVAIL
);
502 if (ifra
->ifra_addr
.sin_family
== AF_INET
) {
504 if (ia
->ia_ifp
== ifp
&&
505 ia
->ia_addr
.sin_addr
.s_addr
==
506 ifra
->ifra_addr
.sin_addr
.s_addr
)
510 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
511 cmd
== SIOCAIFADDR
&&
512 ifra
->ifra_dstaddr
.sin_addr
.s_addr
== INADDR_ANY
) {
516 if (cmd
== SIOCDIFADDR
&& ia
== NULL
)
517 return (EADDRNOTAVAIL
);
522 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
526 return (EADDRNOTAVAIL
);
528 if (cmd
== SIOCSIFDSTADDR
&&
529 (ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
536 ia
= ifa_create(sizeof(*ia
), M_WAITOK
);
540 * Setup per-CPU information
542 for (i
= 0; i
< ncpus
; ++i
) {
543 ifac
= &ifa
->ifa_containers
[i
];
544 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
550 * Protect from NETISR_IP traversing address list
551 * while we're modifying it.
556 ifa_iflink(ifa
, ifp
, 1);
558 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
559 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
560 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
561 ia
->ia_sockmask
.sin_len
= 8;
562 ia
->ia_sockmask
.sin_family
= AF_INET
;
563 if (ifp
->if_flags
& IFF_BROADCAST
) {
564 ia
->ia_broadaddr
.sin_len
= sizeof ia
->ia_addr
;
565 ia
->ia_broadaddr
.sin_family
= AF_INET
;
568 if (!(ifp
->if_flags
& IFF_LOOPBACK
))
577 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
586 return (EADDRNOTAVAIL
);
592 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_addr
;
596 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
598 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_broadaddr
;
602 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
604 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_dstaddr
;
608 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_sockmask
;
612 KKASSERT(ifp
->if_flags
& IFF_POINTOPOINT
);
614 oldaddr
= ia
->ia_dstaddr
;
615 ia
->ia_dstaddr
= *(struct sockaddr_in
*)&ifr
->ifr_dstaddr
;
616 if (ifp
->if_ioctl
!= NULL
) {
617 ifnet_serialize_all(ifp
);
618 error
= ifp
->if_ioctl(ifp
, SIOCSIFDSTADDR
, (caddr_t
)ia
,
619 td
->td_proc
->p_ucred
);
620 ifnet_deserialize_all(ifp
);
622 ia
->ia_dstaddr
= oldaddr
;
626 if (ia
->ia_flags
& IFA_ROUTE
) {
627 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&oldaddr
;
628 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
629 ia
->ia_ifa
.ifa_dstaddr
=
630 (struct sockaddr
*)&ia
->ia_dstaddr
;
631 rtinit(&ia
->ia_ifa
, RTM_ADD
, RTF_HOST
| RTF_UP
);
636 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
638 ia
->ia_broadaddr
= *(struct sockaddr_in
*)&ifr
->ifr_broadaddr
;
642 error
= in_ifinit(ifp
, ia
,
643 (const struct sockaddr_in
*)&ifr
->ifr_addr
, 1);
644 if (error
!= 0 && iaIsNew
)
647 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
,
648 iaIsNew
? IFADDR_EVENT_ADD
: IFADDR_EVENT_CHANGE
,
651 if (!ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
653 * Interface is brought up by in_ifinit()
654 * (via ifp->if_ioctl). We act as if the
655 * interface got IFF_UP flag turned on.
662 ia
->ia_sockmask
.sin_addr
= ifra
->ifra_addr
.sin_addr
;
663 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
670 if (ia
->ia_addr
.sin_family
== AF_INET
) {
671 if (ifra
->ifra_addr
.sin_len
== 0) {
672 ifra
->ifra_addr
= ia
->ia_addr
;
674 } else if (ifra
->ifra_addr
.sin_addr
.s_addr
==
675 ia
->ia_addr
.sin_addr
.s_addr
) {
679 if (ifra
->ifra_mask
.sin_len
) {
681 ia
->ia_sockmask
= ifra
->ifra_mask
;
682 ia
->ia_sockmask
.sin_family
= AF_INET
;
684 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
687 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
688 ifra
->ifra_dstaddr
.sin_family
== AF_INET
) {
690 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
691 maskIsNew
= 1; /* We lie; but the effect's the same */
693 if (ifra
->ifra_addr
.sin_family
== AF_INET
&&
694 (hostIsNew
|| maskIsNew
))
695 error
= in_ifinit(ifp
, ia
, &ifra
->ifra_addr
, 0);
697 if (error
!= 0 && iaIsNew
)
700 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
701 ifra
->ifra_broadaddr
.sin_family
== AF_INET
)
702 ia
->ia_broadaddr
= ifra
->ifra_broadaddr
;
704 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
,
705 iaIsNew
? IFADDR_EVENT_ADD
: IFADDR_EVENT_CHANGE
,
708 if (!ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
709 /* See the comment in SIOCSIFADDR */
716 * in_ifscrub kills the interface route.
720 * in_ifadown gets rid of all the rest of
721 * the routes. This is not quite the right
722 * thing to do, but at least if we are running
723 * a routing process they will come back.
725 in_ifadown(&ia
->ia_ifa
, 1);
726 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
, IFADDR_EVENT_DELETE
,
732 if (ifp
== NULL
|| ifp
->if_ioctl
== NULL
)
734 ifnet_serialize_all(ifp
);
735 error
= ifp
->if_ioctl(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
736 ifnet_deserialize_all(ifp
);
740 KKASSERT(cmd
== SIOCDIFADDR
||
741 ((cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) && iaIsNew
));
743 ifa_ifunlink(&ia
->ia_ifa
, ifp
);
746 if (cmd
== SIOCDIFADDR
) {
747 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
748 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
)
749 in_iahash_remove(ia
);
754 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
755 * already taken care of the deletion from hash table
757 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
758 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) == 0,
759 ("SIOC%cIFADDR failed on new ia, "
760 "but the new ia is still in hash table\n",
761 cmd
== SIOCSIFADDR
? 'S' : 'A'));
765 ifa_destroy(&ia
->ia_ifa
);
767 if ((cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) &&
768 !ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
770 * Though the address assignment failed, the
771 * interface is brought up by in_ifinit()
772 * (via ifp->if_ioctl). With the hope that
773 * the interface has some valid addresses, we
774 * act as if IFF_UP flag was just set on the
778 * This could only be done after the failed
779 * address is unlinked from the global address
790 * SIOCGLIFADDR: get first address. (?!?)
791 * SIOCGLIFADDR with IFLR_PREFIX:
792 * get first address that matches the specified prefix.
793 * SIOCALIFADDR: add the specified address.
794 * SIOCALIFADDR with IFLR_PREFIX:
795 * EINVAL since we can't deduce hostid part of the address.
796 * SIOCDLIFADDR: delete the specified address.
797 * SIOCDLIFADDR with IFLR_PREFIX:
798 * delete the first address that matches the specified prefix.
800 * EINVAL on invalid parameters
801 * EADDRNOTAVAIL on prefix match failed/specified address not found
802 * other values may be returned from in_ioctl()
804 * NOTE! td might be NULL.
807 in_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
810 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
814 panic("invalid argument to in_lifaddr_ioctl");
820 /* address must be specified on GET with IFLR_PREFIX */
821 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
826 /* address must be specified on ADD and DELETE */
827 if (iflr
->addr
.ss_family
!= AF_INET
)
829 if (iflr
->addr
.ss_len
!= sizeof(struct sockaddr_in
))
831 /* XXX need improvement */
832 if (iflr
->dstaddr
.ss_family
833 && iflr
->dstaddr
.ss_family
!= AF_INET
)
835 if (iflr
->dstaddr
.ss_family
836 && iflr
->dstaddr
.ss_len
!= sizeof(struct sockaddr_in
))
839 default: /*shouldn't happen*/
842 if (sizeof(struct in_addr
) * 8 < iflr
->prefixlen
)
848 struct in_aliasreq ifra
;
850 if (iflr
->flags
& IFLR_PREFIX
)
853 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
854 bzero(&ifra
, sizeof ifra
);
855 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof ifra
.ifra_name
);
857 bcopy(&iflr
->addr
, &ifra
.ifra_addr
, iflr
->addr
.ss_len
);
859 if (iflr
->dstaddr
.ss_family
) { /*XXX*/
860 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
861 iflr
->dstaddr
.ss_len
);
864 ifra
.ifra_mask
.sin_family
= AF_INET
;
865 ifra
.ifra_mask
.sin_len
= sizeof(struct sockaddr_in
);
866 in_len2mask(&ifra
.ifra_mask
.sin_addr
, iflr
->prefixlen
);
868 return in_control(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, ifp
, td
);
873 struct ifaddr_container
*ifac
;
874 struct in_ifaddr
*ia
;
875 struct in_addr mask
, candidate
, match
;
876 struct sockaddr_in
*sin
;
879 bzero(&mask
, sizeof mask
);
880 if (iflr
->flags
& IFLR_PREFIX
) {
881 /* lookup a prefix rather than address. */
882 in_len2mask(&mask
, iflr
->prefixlen
);
884 sin
= (struct sockaddr_in
*)&iflr
->addr
;
885 match
.s_addr
= sin
->sin_addr
.s_addr
;
886 match
.s_addr
&= mask
.s_addr
;
888 /* if you set extra bits, that's wrong */
889 if (match
.s_addr
!= sin
->sin_addr
.s_addr
)
894 if (cmd
== SIOCGLIFADDR
) {
895 /* on getting an address, take the 1st match */
896 match
.s_addr
= 0; /* gcc4 warning */
899 /* on deleting an address, do exact match */
900 in_len2mask(&mask
, 32);
901 sin
= (struct sockaddr_in
*)&iflr
->addr
;
902 match
.s_addr
= sin
->sin_addr
.s_addr
;
908 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
909 struct ifaddr
*ifa
= ifac
->ifa
;
911 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
916 ((struct sockaddr_in
*)&ifa
->ifa_addr
)->sin_addr
.s_addr
;
917 candidate
.s_addr
&= mask
.s_addr
;
918 if (candidate
.s_addr
== match
.s_addr
)
922 return EADDRNOTAVAIL
;
923 ia
= (struct in_ifaddr
*)(ifac
->ifa
);
925 if (cmd
== SIOCGLIFADDR
) {
926 /* fill in the if_laddrreq structure */
927 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin_len
);
929 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
930 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
931 ia
->ia_dstaddr
.sin_len
);
933 bzero(&iflr
->dstaddr
, sizeof iflr
->dstaddr
);
936 in_mask2len(&ia
->ia_sockmask
.sin_addr
);
938 iflr
->flags
= 0; /*XXX*/
942 struct in_aliasreq ifra
;
944 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
945 bzero(&ifra
, sizeof ifra
);
946 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
947 sizeof ifra
.ifra_name
);
949 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
950 ia
->ia_addr
.sin_len
);
951 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
952 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
953 ia
->ia_dstaddr
.sin_len
);
955 bcopy(&ia
->ia_sockmask
, &ifra
.ifra_dstaddr
,
956 ia
->ia_sockmask
.sin_len
);
958 return in_control(so
, SIOCDIFADDR
, (caddr_t
)&ifra
,
964 return EOPNOTSUPP
; /*just for safety*/
968 * Delete any existing route for an interface.
971 in_ifscrub(struct ifnet
*ifp __unused
, struct in_ifaddr
*ia
)
977 * Initialize an interface's internet address
978 * and routing table entry.
981 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
,
982 const struct sockaddr_in
*sin
, int scrub
)
984 u_long i
= ntohl(sin
->sin_addr
.s_addr
);
985 struct sockaddr_in oldaddr
;
986 struct ifaddr_container
*ifac
;
987 int flags
= RTF_UP
, error
= 0;
990 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
991 oldaddr
= ia
->ia_addr
;
993 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) {
995 in_iahash_remove(ia
);
999 if (ia
->ia_addr
.sin_family
== AF_INET
)
1000 in_iahash_insert(ia
);
1003 * Give the interface a chance to initialize
1004 * if this is its first address,
1005 * and to validate the address if necessary.
1007 if (ifp
->if_ioctl
!= NULL
) {
1008 ifnet_serialize_all(ifp
);
1009 error
= ifp
->if_ioctl(ifp
, SIOCSIFADDR
, (caddr_t
)ia
, NULL
);
1010 ifnet_deserialize_all(ifp
);
1016 * Delete old route, if requested.
1019 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
1020 in_ifscrub(ifp
, ia
);
1021 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1025 * Calculate netmask/subnetmask.
1028 ia
->ia_netmask
= IN_CLASSA_NET
;
1029 else if (IN_CLASSB(i
))
1030 ia
->ia_netmask
= IN_CLASSB_NET
;
1032 ia
->ia_netmask
= IN_CLASSC_NET
;
1034 * The subnet mask usually includes at least the standard network part,
1035 * but may may be smaller in the case of supernetting.
1036 * If it is set, we believe it.
1038 if (ia
->ia_subnetmask
== 0) {
1039 ia
->ia_subnetmask
= ia
->ia_netmask
;
1040 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
1042 ia
->ia_netmask
&= ia
->ia_subnetmask
;
1044 ia
->ia_net
= i
& ia
->ia_netmask
;
1045 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
1046 in_socktrim(&ia
->ia_sockmask
);
1049 * Add route for the network.
1051 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1052 if (ifp
->if_flags
& IFF_BROADCAST
) {
1053 ia
->ia_broadaddr
.sin_addr
.s_addr
=
1054 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
1055 ia
->ia_netbroadcast
.s_addr
=
1056 htonl(ia
->ia_net
| ~ ia
->ia_netmask
);
1057 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
1058 ia
->ia_dstaddr
= ia
->ia_addr
;
1060 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1061 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
)
1067 * Don't add host routes for interface addresses of
1068 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1069 * possible to assign several such address pairs with consistent
1070 * results (no host route) and is required by BOOTP.
1072 * XXX: This is ugly ! There should be a way for the caller to
1073 * say that they don't want a host route.
1075 if (ia
->ia_addr
.sin_addr
.s_addr
!= INADDR_ANY
||
1076 ia
->ia_netmask
!= IN_CLASSA_NET
||
1077 ia
->ia_dstaddr
.sin_addr
.s_addr
!= htonl(IN_CLASSA_HOST
)) {
1078 error
= in_addprefix(ia
, flags
);
1084 * If the interface supports multicast, join the "all hosts"
1085 * multicast group on that interface.
1087 if (ifp
->if_flags
& IFF_MULTICAST
) {
1088 struct in_addr addr
;
1090 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1091 in_addmulti(&addr
, ifp
);
1095 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
)
1096 in_iahash_remove(ia
);
1098 ia
->ia_addr
= oldaddr
;
1100 in_iahash_insert(ia
);
1104 #define rtinitflags(x) \
1105 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1109 * Add a route to prefix ("connected route" in cisco terminology).
1110 * Do nothing, if there are some interface addresses with the same
1111 * prefix already. This function assumes that the 'target' parent
1115 in_addprefix(struct in_ifaddr
*target
, int flags
)
1117 struct in_ifaddr_container
*iac
;
1118 struct in_addr prefix
, mask
;
1121 mask
= target
->ia_sockmask
.sin_addr
;
1122 if (flags
& RTF_HOST
) {
1123 prefix
= target
->ia_dstaddr
.sin_addr
;
1125 prefix
= target
->ia_addr
.sin_addr
;
1126 prefix
.s_addr
&= mask
.s_addr
;
1129 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1130 struct in_ifaddr
*ia
= iac
->ia
;
1133 /* Don't test against self */
1137 /* The tested address does not own a route entry */
1138 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
1142 if (rtinitflags(ia
)) {
1143 p
= ia
->ia_dstaddr
.sin_addr
;
1145 p
= ia
->ia_addr
.sin_addr
;
1146 p
.s_addr
&= ia
->ia_sockmask
.sin_addr
.s_addr
;
1148 if (prefix
.s_addr
!= p
.s_addr
)
1152 * If the to-be-added address and the curretly being
1153 * tested address are not host addresses, we need to
1154 * take subnetmask into consideration.
1156 if (!(flags
& RTF_HOST
) && !rtinitflags(ia
) &&
1157 mask
.s_addr
!= ia
->ia_sockmask
.sin_addr
.s_addr
)
1161 * If we got a matching prefix route inserted by other
1162 * interface address, we don't need to bother.
1168 * No one seem to have prefix route; insert it.
1170 error
= rtinit(&target
->ia_ifa
, RTM_ADD
, flags
);
1172 target
->ia_flags
|= IFA_ROUTE
;
1177 * Remove a route to prefix ("connected route" in cisco terminology).
1178 * Re-installs the route by using another interface address, if there's
1179 * one with the same prefix (otherwise we lose the route mistakenly).
1182 in_scrubprefix(struct in_ifaddr
*target
)
1184 struct in_ifaddr_container
*iac
;
1185 struct in_addr prefix
, mask
;
1188 if ((target
->ia_flags
& IFA_ROUTE
) == 0)
1191 mask
= target
->ia_sockmask
.sin_addr
;
1192 if (rtinitflags(target
)) {
1193 prefix
= target
->ia_dstaddr
.sin_addr
;
1195 prefix
= target
->ia_addr
.sin_addr
;
1196 prefix
.s_addr
&= mask
.s_addr
;
1199 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1200 struct in_ifaddr
*ia
= iac
->ia
;
1203 /* Don't test against self */
1207 /* The tested address already owns a route entry */
1208 if (ia
->ia_flags
& IFA_ROUTE
)
1212 * The prefix route of the tested address should
1213 * never be installed if its parent interface is
1216 if ((ia
->ia_ifp
->if_flags
& IFF_UP
) == 0)
1220 if (rtinitflags(ia
)) {
1221 p
= ia
->ia_dstaddr
.sin_addr
;
1223 p
= ia
->ia_addr
.sin_addr
;
1224 p
.s_addr
&= ia
->ia_sockmask
.sin_addr
.s_addr
;
1226 if (prefix
.s_addr
!= p
.s_addr
)
1230 * We don't need to test subnetmask here, as what we do
1231 * in in_addprefix(), since if the the tested address's
1232 * parent interface is UP, the tested address should own
1233 * a prefix route entry and we would never reach here.
1237 * If we got a matching prefix route, move IFA_ROUTE to him
1239 rtinit(&target
->ia_ifa
, RTM_DELETE
, rtinitflags(target
));
1240 target
->ia_flags
&= ~IFA_ROUTE
;
1242 error
= rtinit(&ia
->ia_ifa
, RTM_ADD
, rtinitflags(ia
) | RTF_UP
);
1244 ia
->ia_flags
|= IFA_ROUTE
;
1249 * No candidates for this prefix route; just remove it.
1251 rtinit(&target
->ia_ifa
, RTM_DELETE
, rtinitflags(target
));
1252 target
->ia_flags
&= ~IFA_ROUTE
;
1258 * Return 1 if the address might be a local broadcast address.
1261 in_broadcast(struct in_addr in
, struct ifnet
*ifp
)
1263 struct ifaddr_container
*ifac
;
1266 if (in
.s_addr
== INADDR_BROADCAST
||
1267 in
.s_addr
== INADDR_ANY
)
1269 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
1271 t
= ntohl(in
.s_addr
);
1273 * Look through the list of addresses for a match
1274 * with a broadcast address.
1276 #define ia ((struct in_ifaddr *)ifa)
1277 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1278 struct ifaddr
*ifa
= ifac
->ifa
;
1280 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
1281 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
1282 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
1284 * Check for old-style (host 0) broadcast.
1286 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
1288 * Check for an all one subnetmask. These
1289 * only exist when an interface gets a secondary
1292 ia
->ia_subnetmask
!= (u_long
)0xffffffff)
1299 * Add an address to the list of IP multicast addresses for a given interface.
1302 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1304 struct in_multi
*inm
;
1306 struct sockaddr_in sin
;
1307 struct ifmultiaddr
*ifma
;
1310 * Call generic routine to add membership or increment
1311 * refcount. It wants addresses in the form of a sockaddr,
1312 * so we build one here (being careful to zero the unused bytes).
1314 bzero(&sin
, sizeof sin
);
1315 sin
.sin_family
= AF_INET
;
1316 sin
.sin_len
= sizeof sin
;
1319 error
= if_addmulti(ifp
, (struct sockaddr
*)&sin
, &ifma
);
1326 * If ifma->ifma_protospec is null, then if_addmulti() created
1327 * a new record. Otherwise, we are done.
1329 if (ifma
->ifma_protospec
!= 0) {
1331 return ifma
->ifma_protospec
;
1334 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1335 at interrupt time? If so, need to fix if_addmulti. XXX */
1336 inm
= kmalloc(sizeof *inm
, M_IPMADDR
, M_WAITOK
| M_ZERO
);
1337 inm
->inm_addr
= *ap
;
1339 inm
->inm_ifma
= ifma
;
1340 ifma
->ifma_protospec
= inm
;
1341 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
1344 * Let IGMP know that we have joined a new IP multicast group.
1346 igmp_joingroup(inm
);
1352 * Delete a multicast address record.
1355 in_delmulti(struct in_multi
*inm
)
1357 struct ifmultiaddr
*ifma
;
1358 struct in_multi my_inm
;
1361 ifma
= inm
->inm_ifma
;
1362 my_inm
.inm_ifp
= NULL
; /* don't send the leave msg */
1363 if (ifma
->ifma_refcount
== 1) {
1365 * No remaining claims to this record; let IGMP know that
1366 * we are leaving the multicast group.
1367 * But do it after the if_delmulti() which might reset
1368 * the interface and nuke the packet.
1371 ifma
->ifma_protospec
= 0;
1372 LIST_REMOVE(inm
, inm_link
);
1373 kfree(inm
, M_IPMADDR
);
1375 /* XXX - should be separate API for when we have an ifma? */
1376 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
1377 if (my_inm
.inm_ifp
!= NULL
)
1378 igmp_leavegroup(&my_inm
);
1383 in_ifdetach(struct ifnet
*ifp
)
1385 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo
.pcblisthead
), ifp
);
1386 in_pcbpurgeif0(LIST_FIRST(&udbinfo
.pcblisthead
), ifp
);