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 (u_long
, caddr_t
, struct ifnet
*, struct thread
*);
68 static void in_socktrim (struct sockaddr_in
*);
69 static int in_ifinit(struct ifnet
*, struct in_ifaddr
*,
70 const struct sockaddr_in
*, int);
72 static int in_control_internal(u_long
, caddr_t
, struct ifnet
*,
75 static int in_addprefix(struct in_ifaddr
*, int);
76 static void in_scrubprefix(struct in_ifaddr
*);
78 static int subnetsarelocal
= 0;
79 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
, CTLFLAG_RW
,
81 "Count all internet addresses of subnets of the local net as local");
83 struct in_multihead in_multihead
; /* XXX BSS initialization */
85 extern struct inpcbinfo ripcbinfo
;
88 * Return 1 if an internet address is for a ``local'' host
89 * (one to which we have a connection). If subnetsarelocal
90 * is true, this includes other subnets of the local net.
91 * Otherwise, it includes only the directly-connected (sub)nets.
94 in_localaddr(struct in_addr in
)
96 u_long i
= ntohl(in
.s_addr
);
97 struct in_ifaddr_container
*iac
;
100 if (subnetsarelocal
) {
101 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
104 if ((i
& ia
->ia_netmask
) == ia
->ia_net
)
108 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
111 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
)
119 * Determine whether an IP address is in a reserved set of addresses
120 * that may not be forwarded, or whether datagrams to that destination
124 in_canforward(struct in_addr in
)
126 u_long i
= ntohl(in
.s_addr
);
129 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
))
132 net
= i
& IN_CLASSA_NET
;
133 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))
140 * Trim a mask in a sockaddr
143 in_socktrim(struct sockaddr_in
*ap
)
145 char *cplim
= (char *) &ap
->sin_addr
;
146 char *cp
= (char *) (&ap
->sin_addr
+ 1);
149 while (--cp
>= cplim
)
151 (ap
)->sin_len
= cp
- (char *) (ap
) + 1;
157 in_mask2len(struct in_addr
*mask
)
163 for (x
= 0; x
< sizeof *mask
; x
++) {
168 if (x
< sizeof *mask
) {
169 for (y
= 0; y
< 8; y
++) {
170 if ((p
[x
] & (0x80 >> y
)) == 0)
178 in_len2mask(struct in_addr
*mask
, int len
)
184 bzero(mask
, sizeof *mask
);
185 for (i
= 0; i
< len
/ 8; i
++)
188 p
[i
] = (0xff00 >> (len
% 8)) & 0xff;
192 in_control_dispatch(netmsg_t msg
)
196 error
= in_control(msg
->control
.nm_cmd
, msg
->control
.nm_data
,
197 msg
->control
.nm_ifp
, msg
->control
.nm_td
);
198 lwkt_replymsg(&msg
->lmsg
, error
);
202 in_control_internal_dispatch(netmsg_t msg
)
206 error
= in_control_internal(msg
->control
.nm_cmd
,
207 msg
->control
.nm_data
,
210 lwkt_replymsg(&msg
->lmsg
, error
);
214 * Generic internet control operations (ioctl's).
215 * Ifp is 0 if not an interface-specific ioctl.
217 * NOTE! td might be NULL.
220 in_control(u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct thread
*td
)
222 struct netmsg_pru_control msg
;
235 * Dispatch these SIOCs to netisr0.
237 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
238 in_control_internal_dispatch
);
243 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
244 return msg
.base
.lmsg
.ms_error
;
247 return in_control_internal(cmd
, data
, ifp
, td
);
252 in_ialink_dispatch(netmsg_t msg
)
254 struct in_ifaddr
*ia
= msg
->lmsg
.u
.ms_resultp
;
255 struct ifaddr_container
*ifac
;
256 struct in_ifaddr_container
*iac
;
261 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
262 ASSERT_IFAC_VALID(ifac
);
263 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
) == 0,
264 ("ia is on in_ifaddrheads"));
266 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHEAD
;
267 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
268 TAILQ_INSERT_TAIL(&in_ifaddrheads
[cpu
], iac
, ia_link
);
272 ifa_forwardmsg(&msg
->lmsg
, cpu
+ 1);
276 in_iaunlink_dispatch(netmsg_t msg
)
278 struct in_ifaddr
*ia
= msg
->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
,
288 ("ia is not on in_ifaddrheads"));
290 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
291 TAILQ_REMOVE(&in_ifaddrheads
[cpu
], iac
, ia_link
);
292 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHEAD
;
296 ifa_forwardmsg(&msg
->lmsg
, cpu
+ 1);
300 in_iahashins_dispatch(netmsg_t msg
)
302 struct in_ifaddr
*ia
= msg
->lmsg
.u
.ms_resultp
;
303 struct ifaddr_container
*ifac
;
304 struct in_ifaddr_container
*iac
;
309 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
310 ASSERT_IFAC_VALID(ifac
);
311 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) == 0,
312 ("ia is on in_ifaddrhashtbls"));
314 ifac
->ifa_listmask
|= IFA_LIST_IN_IFADDRHASH
;
315 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
316 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
321 ifa_forwardmsg(&msg
->lmsg
, cpu
+ 1);
325 in_iahashrem_dispatch(netmsg_t msg
)
327 struct in_ifaddr
*ia
= msg
->lmsg
.u
.ms_resultp
;
328 struct ifaddr_container
*ifac
;
329 struct in_ifaddr_container
*iac
;
334 ifac
= &ia
->ia_ifa
.ifa_containers
[cpu
];
335 ASSERT_IFAC_VALID(ifac
);
336 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
,
337 ("ia is not on in_ifaddrhashtbls"));
339 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
340 LIST_REMOVE(iac
, ia_hash
);
341 ifac
->ifa_listmask
&= ~IFA_LIST_IN_IFADDRHASH
;
345 ifa_forwardmsg(&msg
->lmsg
, cpu
+ 1);
349 in_ialink(struct in_ifaddr
*ia
)
351 struct netmsg_base msg
;
353 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
,
354 0, in_ialink_dispatch
);
355 msg
.lmsg
.u
.ms_resultp
= ia
;
357 ifa_domsg(&msg
.lmsg
, 0);
361 in_iaunlink(struct in_ifaddr
*ia
)
363 struct netmsg_base msg
;
365 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
,
366 0, in_iaunlink_dispatch
);
367 msg
.lmsg
.u
.ms_resultp
= ia
;
369 ifa_domsg(&msg
.lmsg
, 0);
373 in_iahash_insert(struct in_ifaddr
*ia
)
375 struct netmsg_base msg
;
377 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
,
378 0, in_iahashins_dispatch
);
379 msg
.lmsg
.u
.ms_resultp
= ia
;
381 ifa_domsg(&msg
.lmsg
, 0);
385 in_iahash_remove(struct in_ifaddr
*ia
)
387 struct netmsg_base msg
;
389 netmsg_init(&msg
, NULL
, &curthread
->td_msgport
,
390 0, in_iahashrem_dispatch
);
391 msg
.lmsg
.u
.ms_resultp
= ia
;
393 ifa_domsg(&msg
.lmsg
, 0);
396 static __inline
struct in_ifaddr
*
397 in_ianext(struct in_ifaddr
*oia
)
399 struct ifaddr_container
*ifac
;
400 struct in_ifaddr_container
*iac
;
402 ifac
= &oia
->ia_ifa
.ifa_containers
[mycpuid
];
403 ASSERT_IFAC_VALID(ifac
);
404 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHEAD
,
405 ("ia is not on in_ifaddrheads"));
407 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
408 iac
= TAILQ_NEXT(iac
, ia_link
);
416 in_control_internal(u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
419 struct ifreq
*ifr
= (struct ifreq
*)data
;
420 struct in_ifaddr
*ia
= NULL
;
422 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)data
;
423 struct ifaddr_container
*ifac
;
424 struct in_ifaddr_container
*iac
;
425 struct sockaddr_in oldaddr
;
426 int hostIsNew
, iaIsNew
, maskIsNew
, ifpWasUp
;
432 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
438 return in_lifaddr_ioctl(cmd
, data
, ifp
, td
);
445 * Find address for this interface, if it exists.
447 * If an alias address was specified, find that one instead of
448 * the first one on the interface, if possible
451 struct in_ifaddr
*iap
;
453 dst
= ((struct sockaddr_in
*)&ifr
->ifr_addr
)->sin_addr
;
454 LIST_FOREACH(iac
, INADDR_HASH(dst
.s_addr
), ia_hash
) {
456 if (iap
->ia_ifp
== ifp
&&
457 iap
->ia_addr
.sin_addr
.s_addr
== dst
.s_addr
) {
463 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
],
465 iap
= ifatoia(ifac
->ifa
);
466 if (iap
->ia_addr
.sin_family
== AF_INET
) {
473 if (ifp
->if_flags
& IFF_UP
)
481 return (EADDRNOTAVAIL
);
482 if (ifra
->ifra_addr
.sin_family
== AF_INET
) {
484 if (ia
->ia_ifp
== ifp
&&
485 ia
->ia_addr
.sin_addr
.s_addr
==
486 ifra
->ifra_addr
.sin_addr
.s_addr
)
490 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
491 cmd
== SIOCAIFADDR
&&
492 ifra
->ifra_dstaddr
.sin_addr
.s_addr
== INADDR_ANY
) {
496 if (cmd
== SIOCDIFADDR
&& ia
== NULL
)
497 return (EADDRNOTAVAIL
);
502 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
506 return (EADDRNOTAVAIL
);
508 if (cmd
== SIOCSIFDSTADDR
&&
509 (ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
516 ia
= ifa_create(sizeof(*ia
));
520 * Setup per-CPU information
522 for (i
= 0; i
< ncpus
; ++i
) {
523 ifac
= &ifa
->ifa_containers
[i
];
524 iac
= &ifac
->ifa_proto_u
.u_in_ifac
;
529 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
530 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
531 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
532 ia
->ia_sockmask
.sin_len
= 8;
533 ia
->ia_sockmask
.sin_family
= AF_INET
;
534 if (ifp
->if_flags
& IFF_BROADCAST
) {
535 ia
->ia_broadaddr
.sin_len
= sizeof ia
->ia_addr
;
536 ia
->ia_broadaddr
.sin_family
= AF_INET
;
542 ifa_iflink(ifa
, ifp
, 1);
547 if (td
&& (error
= priv_check(td
, PRIV_ROOT
)) != 0)
556 return (EADDRNOTAVAIL
);
562 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_addr
;
566 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
568 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_broadaddr
;
572 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
574 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_dstaddr
;
578 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_sockmask
;
582 KKASSERT(ifp
->if_flags
& IFF_POINTOPOINT
);
584 oldaddr
= ia
->ia_dstaddr
;
585 ia
->ia_dstaddr
= *(struct sockaddr_in
*)&ifr
->ifr_dstaddr
;
586 if (ifp
->if_ioctl
!= NULL
) {
587 ifnet_serialize_all(ifp
);
588 error
= ifp
->if_ioctl(ifp
, SIOCSIFDSTADDR
, (caddr_t
)ia
,
589 td
->td_proc
->p_ucred
);
590 ifnet_deserialize_all(ifp
);
592 ia
->ia_dstaddr
= oldaddr
;
596 if (ia
->ia_flags
& IFA_ROUTE
) {
597 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&oldaddr
;
598 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
599 ia
->ia_ifa
.ifa_dstaddr
=
600 (struct sockaddr
*)&ia
->ia_dstaddr
;
601 rtinit(&ia
->ia_ifa
, RTM_ADD
, RTF_HOST
| RTF_UP
);
606 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
608 ia
->ia_broadaddr
= *(struct sockaddr_in
*)&ifr
->ifr_broadaddr
;
612 error
= in_ifinit(ifp
, ia
,
613 (const struct sockaddr_in
*)&ifr
->ifr_addr
, 1);
614 if (error
!= 0 && iaIsNew
)
617 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
,
618 iaIsNew
? IFADDR_EVENT_ADD
: IFADDR_EVENT_CHANGE
,
621 if (!ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
623 * Interface is brought up by in_ifinit()
624 * (via ifp->if_ioctl). We act as if the
625 * interface got IFF_UP flag turned on.
632 ia
->ia_sockmask
.sin_addr
= ifra
->ifra_addr
.sin_addr
;
633 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
640 if (ia
->ia_addr
.sin_family
== AF_INET
) {
641 if (ifra
->ifra_addr
.sin_len
== 0) {
642 ifra
->ifra_addr
= ia
->ia_addr
;
644 } else if (ifra
->ifra_addr
.sin_addr
.s_addr
==
645 ia
->ia_addr
.sin_addr
.s_addr
) {
649 if (ifra
->ifra_mask
.sin_len
) {
651 ia
->ia_sockmask
= ifra
->ifra_mask
;
652 ia
->ia_sockmask
.sin_family
= AF_INET
;
654 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
657 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
658 ifra
->ifra_dstaddr
.sin_family
== AF_INET
) {
660 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
661 maskIsNew
= 1; /* We lie; but the effect's the same */
663 if (ifra
->ifra_addr
.sin_family
== AF_INET
&&
664 (hostIsNew
|| maskIsNew
))
665 error
= in_ifinit(ifp
, ia
, &ifra
->ifra_addr
, 0);
667 if (error
!= 0 && iaIsNew
)
670 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
671 ifra
->ifra_broadaddr
.sin_family
== AF_INET
)
672 ia
->ia_broadaddr
= ifra
->ifra_broadaddr
;
674 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
,
675 iaIsNew
? IFADDR_EVENT_ADD
: IFADDR_EVENT_CHANGE
,
678 if (!ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
679 /* See the comment in SIOCSIFADDR */
686 * in_ifscrub kills the interface route.
690 * in_ifadown gets rid of all the rest of
691 * the routes. This is not quite the right
692 * thing to do, but at least if we are running
693 * a routing process they will come back.
695 in_ifadown(&ia
->ia_ifa
, 1);
696 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
, IFADDR_EVENT_DELETE
,
702 if (ifp
== NULL
|| ifp
->if_ioctl
== NULL
)
704 ifnet_serialize_all(ifp
);
705 error
= ifp
->if_ioctl(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
706 ifnet_deserialize_all(ifp
);
710 KKASSERT(cmd
== SIOCDIFADDR
||
711 ((cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) && iaIsNew
));
713 ifa_ifunlink(&ia
->ia_ifa
, ifp
);
716 if (cmd
== SIOCDIFADDR
) {
717 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
718 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
)
719 in_iahash_remove(ia
);
724 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
725 * already taken care of the deletion from hash table
727 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
728 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) == 0,
729 ("SIOC%cIFADDR failed on new ia, "
730 "but the new ia is still in hash table",
731 cmd
== SIOCSIFADDR
? 'S' : 'A'));
735 ifa_destroy(&ia
->ia_ifa
);
737 if ((cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) &&
738 !ifpWasUp
&& (ifp
->if_flags
& IFF_UP
)) {
740 * Though the address assignment failed, the
741 * interface is brought up by in_ifinit()
742 * (via ifp->if_ioctl). With the hope that
743 * the interface has some valid addresses, we
744 * act as if IFF_UP flag was just set on the
748 * This could only be done after the failed
749 * address is unlinked from the global address
760 * SIOCGLIFADDR: get first address. (?!?)
761 * SIOCGLIFADDR with IFLR_PREFIX:
762 * get first address that matches the specified prefix.
763 * SIOCALIFADDR: add the specified address.
764 * SIOCALIFADDR with IFLR_PREFIX:
765 * EINVAL since we can't deduce hostid part of the address.
766 * SIOCDLIFADDR: delete the specified address.
767 * SIOCDLIFADDR with IFLR_PREFIX:
768 * delete the first address that matches the specified prefix.
770 * EINVAL on invalid parameters
771 * EADDRNOTAVAIL on prefix match failed/specified address not found
772 * other values may be returned from in_ioctl()
774 * NOTE! td might be NULL.
777 in_lifaddr_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct thread
*td
)
779 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
783 panic("invalid argument to in_lifaddr_ioctl");
789 /* address must be specified on GET with IFLR_PREFIX */
790 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
795 /* address must be specified on ADD and DELETE */
796 if (iflr
->addr
.ss_family
!= AF_INET
)
798 if (iflr
->addr
.ss_len
!= sizeof(struct sockaddr_in
))
800 /* XXX need improvement */
801 if (iflr
->dstaddr
.ss_family
802 && iflr
->dstaddr
.ss_family
!= AF_INET
)
804 if (iflr
->dstaddr
.ss_family
805 && iflr
->dstaddr
.ss_len
!= sizeof(struct sockaddr_in
))
808 default: /*shouldn't happen*/
811 if (sizeof(struct in_addr
) * 8 < iflr
->prefixlen
)
817 struct in_aliasreq ifra
;
819 if (iflr
->flags
& IFLR_PREFIX
)
822 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
823 bzero(&ifra
, sizeof ifra
);
824 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof ifra
.ifra_name
);
826 bcopy(&iflr
->addr
, &ifra
.ifra_addr
, iflr
->addr
.ss_len
);
828 if (iflr
->dstaddr
.ss_family
) { /*XXX*/
829 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
830 iflr
->dstaddr
.ss_len
);
833 ifra
.ifra_mask
.sin_family
= AF_INET
;
834 ifra
.ifra_mask
.sin_len
= sizeof(struct sockaddr_in
);
835 in_len2mask(&ifra
.ifra_mask
.sin_addr
, iflr
->prefixlen
);
837 return in_control_internal(SIOCAIFADDR
, (caddr_t
)&ifra
, ifp
,
843 struct ifaddr_container
*ifac
;
844 struct in_ifaddr
*ia
;
845 struct in_addr mask
, candidate
, match
;
846 struct sockaddr_in
*sin
;
849 bzero(&mask
, sizeof mask
);
850 if (iflr
->flags
& IFLR_PREFIX
) {
851 /* lookup a prefix rather than address. */
852 in_len2mask(&mask
, iflr
->prefixlen
);
854 sin
= (struct sockaddr_in
*)&iflr
->addr
;
855 match
.s_addr
= sin
->sin_addr
.s_addr
;
856 match
.s_addr
&= mask
.s_addr
;
858 /* if you set extra bits, that's wrong */
859 if (match
.s_addr
!= sin
->sin_addr
.s_addr
)
864 if (cmd
== SIOCGLIFADDR
) {
865 /* on getting an address, take the 1st match */
866 match
.s_addr
= 0; /* gcc4 warning */
869 /* on deleting an address, do exact match */
870 in_len2mask(&mask
, 32);
871 sin
= (struct sockaddr_in
*)&iflr
->addr
;
872 match
.s_addr
= sin
->sin_addr
.s_addr
;
878 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
879 struct ifaddr
*ifa
= ifac
->ifa
;
881 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
886 ((struct sockaddr_in
*)&ifa
->ifa_addr
)->sin_addr
.s_addr
;
887 candidate
.s_addr
&= mask
.s_addr
;
888 if (candidate
.s_addr
== match
.s_addr
)
892 return EADDRNOTAVAIL
;
893 ia
= (struct in_ifaddr
*)(ifac
->ifa
);
895 if (cmd
== SIOCGLIFADDR
) {
896 /* fill in the if_laddrreq structure */
897 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin_len
);
899 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
900 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
901 ia
->ia_dstaddr
.sin_len
);
903 bzero(&iflr
->dstaddr
, sizeof iflr
->dstaddr
);
906 in_mask2len(&ia
->ia_sockmask
.sin_addr
);
908 iflr
->flags
= 0; /*XXX*/
912 struct in_aliasreq ifra
;
914 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
915 bzero(&ifra
, sizeof ifra
);
916 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
917 sizeof ifra
.ifra_name
);
919 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
920 ia
->ia_addr
.sin_len
);
921 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
922 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
923 ia
->ia_dstaddr
.sin_len
);
925 bcopy(&ia
->ia_sockmask
, &ifra
.ifra_dstaddr
,
926 ia
->ia_sockmask
.sin_len
);
928 return in_control_internal(SIOCDIFADDR
, (caddr_t
)&ifra
,
934 return EOPNOTSUPP
; /*just for safety*/
938 * Delete any existing route for an interface.
941 in_ifscrub(struct ifnet
*ifp __unused
, struct in_ifaddr
*ia
)
947 * Initialize an interface's internet address
948 * and routing table entry.
951 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
,
952 const struct sockaddr_in
*sin
, int scrub
)
954 u_long i
= ntohl(sin
->sin_addr
.s_addr
);
955 struct sockaddr_in oldaddr
;
956 struct ifaddr_container
*ifac
;
957 int flags
= RTF_UP
, error
= 0;
960 ifac
= &ia
->ia_ifa
.ifa_containers
[mycpuid
];
961 oldaddr
= ia
->ia_addr
;
963 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
) {
965 in_iahash_remove(ia
);
969 if (ia
->ia_addr
.sin_family
== AF_INET
)
970 in_iahash_insert(ia
);
973 * Give the interface a chance to initialize
974 * if this is its first address,
975 * and to validate the address if necessary.
977 if (ifp
->if_ioctl
!= NULL
) {
978 ifnet_serialize_all(ifp
);
979 error
= ifp
->if_ioctl(ifp
, SIOCSIFADDR
, (caddr_t
)ia
, NULL
);
980 ifnet_deserialize_all(ifp
);
986 * Delete old route, if requested.
989 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
991 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
995 * Calculate netmask/subnetmask.
998 ia
->ia_netmask
= IN_CLASSA_NET
;
999 else if (IN_CLASSB(i
))
1000 ia
->ia_netmask
= IN_CLASSB_NET
;
1002 ia
->ia_netmask
= IN_CLASSC_NET
;
1004 * The subnet mask usually includes at least the standard network part,
1005 * but may may be smaller in the case of supernetting.
1006 * If it is set, we believe it.
1008 if (ia
->ia_subnetmask
== 0) {
1009 ia
->ia_subnetmask
= ia
->ia_netmask
;
1010 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
1012 ia
->ia_netmask
&= ia
->ia_subnetmask
;
1014 ia
->ia_net
= i
& ia
->ia_netmask
;
1015 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
1016 in_socktrim(&ia
->ia_sockmask
);
1019 * Add route for the network.
1021 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1022 if (ifp
->if_flags
& IFF_BROADCAST
) {
1023 ia
->ia_broadaddr
.sin_addr
.s_addr
=
1024 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
1025 ia
->ia_netbroadcast
.s_addr
=
1026 htonl(ia
->ia_net
| ~ ia
->ia_netmask
);
1027 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
1028 ia
->ia_dstaddr
= ia
->ia_addr
;
1030 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1031 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
)
1037 * Don't add host routes for interface addresses of
1038 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1039 * possible to assign several such address pairs with consistent
1040 * results (no host route) and is required by BOOTP.
1042 * XXX: This is ugly ! There should be a way for the caller to
1043 * say that they don't want a host route.
1045 if (ia
->ia_addr
.sin_addr
.s_addr
!= INADDR_ANY
||
1046 ia
->ia_netmask
!= IN_CLASSA_NET
||
1047 ia
->ia_dstaddr
.sin_addr
.s_addr
!= htonl(IN_CLASSA_HOST
)) {
1048 error
= in_addprefix(ia
, flags
);
1054 * If the interface supports multicast, join the "all hosts"
1055 * multicast group on that interface.
1057 if (ifp
->if_flags
& IFF_MULTICAST
) {
1058 struct in_addr addr
;
1060 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1061 in_addmulti(&addr
, ifp
);
1065 if (ifac
->ifa_listmask
& IFA_LIST_IN_IFADDRHASH
)
1066 in_iahash_remove(ia
);
1068 ia
->ia_addr
= oldaddr
;
1070 in_iahash_insert(ia
);
1074 #define rtinitflags(x) \
1075 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1079 * Add a route to prefix ("connected route" in cisco terminology).
1080 * Do nothing, if there are some interface addresses with the same
1081 * prefix already. This function assumes that the 'target' parent
1085 in_addprefix(struct in_ifaddr
*target
, int flags
)
1087 struct in_ifaddr_container
*iac
;
1088 struct in_addr prefix
, mask
;
1093 * Don't add prefix routes for CARP interfaces.
1094 * Prefix routes creation is handled by CARP
1095 * interfaces themselves.
1097 if (target
->ia_ifp
->if_type
== IFT_CARP
)
1101 mask
= target
->ia_sockmask
.sin_addr
;
1102 if (flags
& RTF_HOST
) {
1103 prefix
= target
->ia_dstaddr
.sin_addr
;
1105 prefix
= target
->ia_addr
.sin_addr
;
1106 prefix
.s_addr
&= mask
.s_addr
;
1109 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1110 struct in_ifaddr
*ia
= iac
->ia
;
1113 /* Don't test against self */
1117 /* The tested address does not own a route entry */
1118 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
1122 if (rtinitflags(ia
)) {
1123 p
= ia
->ia_dstaddr
.sin_addr
;
1125 p
= ia
->ia_addr
.sin_addr
;
1126 p
.s_addr
&= ia
->ia_sockmask
.sin_addr
.s_addr
;
1128 if (prefix
.s_addr
!= p
.s_addr
)
1132 * If the to-be-added address and the curretly being
1133 * tested address are not host addresses, we need to
1134 * take subnetmask into consideration.
1136 if (!(flags
& RTF_HOST
) && !rtinitflags(ia
) &&
1137 mask
.s_addr
!= ia
->ia_sockmask
.sin_addr
.s_addr
)
1141 * If we got a matching prefix route inserted by other
1142 * interface address, we don't need to bother.
1148 * No one seem to have prefix route; insert it.
1150 error
= rtinit(&target
->ia_ifa
, RTM_ADD
, flags
);
1152 target
->ia_flags
|= IFA_ROUTE
;
1157 * Remove a route to prefix ("connected route" in cisco terminology).
1158 * Re-installs the route by using another interface address, if there's
1159 * one with the same prefix (otherwise we lose the route mistakenly).
1162 in_scrubprefix(struct in_ifaddr
*target
)
1164 struct in_ifaddr_container
*iac
;
1165 struct in_addr prefix
, mask
;
1170 * Don't scrub prefix routes for CARP interfaces.
1171 * Prefix routes deletion is handled by CARP
1172 * interfaces themselves.
1174 if (target
->ia_ifp
->if_type
== IFT_CARP
)
1178 if ((target
->ia_flags
& IFA_ROUTE
) == 0)
1181 mask
= target
->ia_sockmask
.sin_addr
;
1182 if (rtinitflags(target
)) {
1183 prefix
= target
->ia_dstaddr
.sin_addr
;
1185 prefix
= target
->ia_addr
.sin_addr
;
1186 prefix
.s_addr
&= mask
.s_addr
;
1189 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1190 struct in_ifaddr
*ia
= iac
->ia
;
1193 /* Don't test against self */
1197 /* The tested address already owns a route entry */
1198 if (ia
->ia_flags
& IFA_ROUTE
)
1202 * The prefix route of the tested address should
1203 * never be installed if its parent interface is
1206 if ((ia
->ia_ifp
->if_flags
& IFF_UP
) == 0)
1211 * Don't add prefix routes for CARP interfaces.
1212 * Prefix routes creation is handled by CARP
1213 * interfaces themselves.
1215 if (ia
->ia_ifp
->if_type
== IFT_CARP
)
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
== NULL
|| (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)
1300 * Add an address to the list of IP multicast addresses for a given interface.
1303 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1305 struct in_multi
*inm
;
1307 struct sockaddr_in sin
;
1308 struct ifmultiaddr
*ifma
;
1310 ASSERT_IN_NETISR(0);
1313 * Call generic routine to add membership or increment
1314 * refcount. It wants addresses in the form of a sockaddr,
1315 * so we build one here (being careful to zero the unused bytes).
1317 bzero(&sin
, sizeof sin
);
1318 sin
.sin_family
= AF_INET
;
1319 sin
.sin_len
= sizeof sin
;
1321 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
!= NULL
)
1330 return ifma
->ifma_protospec
;
1332 inm
= kmalloc(sizeof *inm
, M_IPMADDR
, M_INTWAIT
| M_ZERO
);
1333 inm
->inm_addr
= *ap
;
1335 inm
->inm_ifma
= ifma
;
1336 ifma
->ifma_protospec
= inm
;
1337 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
1340 * Let IGMP know that we have joined a new IP multicast group.
1342 igmp_joingroup(inm
);
1347 * Delete a multicast address record.
1350 in_delmulti(struct in_multi
*inm
)
1352 struct ifmultiaddr
*ifma
;
1353 struct in_multi my_inm
;
1355 ASSERT_IN_NETISR(0);
1357 ifma
= inm
->inm_ifma
;
1358 my_inm
.inm_ifp
= NULL
; /* don't send the leave msg */
1359 if (ifma
->ifma_refcount
== 1) {
1361 * No remaining claims to this record; let IGMP know that
1362 * we are leaving the multicast group.
1363 * But do it after the if_delmulti() which might reset
1364 * the interface and nuke the packet.
1367 ifma
->ifma_protospec
= NULL
;
1368 LIST_REMOVE(inm
, inm_link
);
1369 kfree(inm
, M_IPMADDR
);
1371 /* XXX - should be separate API for when we have an ifma? */
1372 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
1373 if (my_inm
.inm_ifp
!= NULL
)
1374 igmp_leavegroup(&my_inm
);
1378 in_ifdetach_dispatch(netmsg_t nmsg
)
1380 struct lwkt_msg
*lmsg
= &nmsg
->lmsg
;
1381 struct ifnet
*ifp
= lmsg
->u
.ms_resultp
;
1384 in_pcbpurgeif0(&ripcbinfo
, ifp
);
1385 for (cpu
= 0; cpu
< ncpus2
; ++cpu
)
1386 in_pcbpurgeif0(&udbinfo
[cpu
], ifp
);
1388 lwkt_replymsg(lmsg
, 0);
1392 in_ifdetach(struct ifnet
*ifp
)
1394 struct netmsg_base nmsg
;
1395 struct lwkt_msg
*lmsg
= &nmsg
.lmsg
;
1397 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
, 0,
1398 in_ifdetach_dispatch
);
1399 lmsg
->u
.ms_resultp
= ifp
;
1401 lwkt_domsg(netisr_cpuport(0), lmsg
, 0);