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.20 2006/10/01 00:38:21 swildner Exp $
38 #include "opt_bootp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
44 #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>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_pcb.h>
57 #include <netinet/igmp_var.h>
59 MALLOC_DEFINE(M_IPMADDR
, "in_multi", "internet multicast address");
61 static int in_mask2len (struct in_addr
*);
62 static void in_len2mask (struct in_addr
*, int);
63 static int in_lifaddr_ioctl (struct socket
*, u_long
, caddr_t
,
64 struct ifnet
*, struct thread
*);
66 static void in_socktrim (struct sockaddr_in
*);
67 static int in_ifinit (struct ifnet
*,
68 struct in_ifaddr
*, struct sockaddr_in
*, int);
70 static int subnetsarelocal
= 0;
71 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
, CTLFLAG_RW
,
72 &subnetsarelocal
, 0, "");
74 struct in_multihead in_multihead
; /* XXX BSS initialization */
76 extern struct inpcbinfo ripcbinfo
;
77 extern struct inpcbinfo udbinfo
;
80 * Return 1 if an internet address is for a ``local'' host
81 * (one to which we have a connection). If subnetsarelocal
82 * is true, this includes other subnets of the local net.
83 * Otherwise, it includes only the directly-connected (sub)nets.
86 in_localaddr(struct in_addr in
)
88 u_long i
= ntohl(in
.s_addr
);
91 if (subnetsarelocal
) {
92 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
93 if ((i
& ia
->ia_netmask
) == ia
->ia_net
)
96 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
97 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
)
104 * Determine whether an IP address is in a reserved set of addresses
105 * that may not be forwarded, or whether datagrams to that destination
109 in_canforward(struct in_addr in
)
111 u_long i
= ntohl(in
.s_addr
);
114 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
))
117 net
= i
& IN_CLASSA_NET
;
118 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))
125 * Trim a mask in a sockaddr
128 in_socktrim(struct sockaddr_in
*ap
)
130 char *cplim
= (char *) &ap
->sin_addr
;
131 char *cp
= (char *) (&ap
->sin_addr
+ 1);
134 while (--cp
>= cplim
)
136 (ap
)->sin_len
= cp
- (char *) (ap
) + 1;
142 in_mask2len(struct in_addr
*mask
)
148 for (x
= 0; x
< sizeof *mask
; x
++) {
153 if (x
< sizeof *mask
) {
154 for (y
= 0; y
< 8; y
++) {
155 if ((p
[x
] & (0x80 >> y
)) == 0)
163 in_len2mask(struct in_addr
*mask
, int len
)
169 bzero(mask
, sizeof *mask
);
170 for (i
= 0; i
< len
/ 8; i
++)
173 p
[i
] = (0xff00 >> (len
% 8)) & 0xff;
176 static int in_interfaces
; /* number of external internet interfaces */
179 * Generic internet control operations (ioctl's).
180 * Ifp is 0 if not an interface-specific ioctl.
182 * NOTE! td might be NULL.
186 in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
189 struct ifreq
*ifr
= (struct ifreq
*)data
;
190 struct in_ifaddr
*ia
= 0, *iap
;
193 struct in_ifaddr
*oia
;
194 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)data
;
195 struct sockaddr_in oldaddr
;
196 int hostIsNew
, iaIsNew
, maskIsNew
;
204 if (td
&& (error
= suser(td
)) != 0)
210 return in_lifaddr_ioctl(so
, cmd
, data
, ifp
, td
);
214 * Find address for this interface, if it exists.
216 * If an alias address was specified, find that one instead of
217 * the first one on the interface, if possible
220 dst
= ((struct sockaddr_in
*)&ifr
->ifr_addr
)->sin_addr
;
221 LIST_FOREACH(iap
, INADDR_HASH(dst
.s_addr
), ia_hash
)
222 if (iap
->ia_ifp
== ifp
&&
223 iap
->ia_addr
.sin_addr
.s_addr
== dst
.s_addr
) {
228 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
230 if (iap
->ia_addr
.sin_family
== AF_INET
) {
242 return (EADDRNOTAVAIL
);
243 if (ifra
->ifra_addr
.sin_family
== AF_INET
) {
244 for (oia
= ia
; ia
; ia
= TAILQ_NEXT(ia
, ia_link
)) {
245 if (ia
->ia_ifp
== ifp
&&
246 ia
->ia_addr
.sin_addr
.s_addr
==
247 ifra
->ifra_addr
.sin_addr
.s_addr
)
250 if ((ifp
->if_flags
& IFF_POINTOPOINT
)
251 && (cmd
== SIOCAIFADDR
)
252 && (ifra
->ifra_dstaddr
.sin_addr
.s_addr
257 if (cmd
== SIOCDIFADDR
&& ia
== 0)
258 return (EADDRNOTAVAIL
);
263 if (td
&& (error
= suser(td
)) != 0)
267 return (EADDRNOTAVAIL
);
268 if (ia
== (struct in_ifaddr
*)0) {
269 ia
= (struct in_ifaddr
*)
270 kmalloc(sizeof *ia
, M_IFADDR
, M_WAITOK
);
271 if (ia
== (struct in_ifaddr
*)NULL
)
273 bzero(ia
, sizeof *ia
);
275 * Protect from ipintr() traversing address list
276 * while we're modifying it.
280 TAILQ_INSERT_TAIL(&in_ifaddrhead
, ia
, ia_link
);
282 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
284 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
285 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
286 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
287 ia
->ia_sockmask
.sin_len
= 8;
288 ia
->ia_sockmask
.sin_family
= AF_INET
;
289 if (ifp
->if_flags
& IFF_BROADCAST
) {
290 ia
->ia_broadaddr
.sin_len
= sizeof ia
->ia_addr
;
291 ia
->ia_broadaddr
.sin_family
= AF_INET
;
294 if (!(ifp
->if_flags
& IFF_LOOPBACK
))
302 if (td
&& (error
= suser(td
)) != 0)
310 if (ia
== (struct in_ifaddr
*)0)
311 return (EADDRNOTAVAIL
);
317 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_addr
;
321 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
323 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_broadaddr
;
327 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
329 *((struct sockaddr_in
*)&ifr
->ifr_dstaddr
) = ia
->ia_dstaddr
;
333 *((struct sockaddr_in
*)&ifr
->ifr_addr
) = ia
->ia_sockmask
;
337 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
339 oldaddr
= ia
->ia_dstaddr
;
340 ia
->ia_dstaddr
= *(struct sockaddr_in
*)&ifr
->ifr_dstaddr
;
341 lwkt_serialize_enter(ifp
->if_serializer
);
343 (error
= ifp
->if_ioctl(ifp
, SIOCSIFDSTADDR
, (caddr_t
)ia
,
344 td
->td_proc
->p_ucred
))) {
345 ia
->ia_dstaddr
= oldaddr
;
346 lwkt_serialize_exit(ifp
->if_serializer
);
349 if (ia
->ia_flags
& IFA_ROUTE
) {
350 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&oldaddr
;
351 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
352 ia
->ia_ifa
.ifa_dstaddr
=
353 (struct sockaddr
*)&ia
->ia_dstaddr
;
354 rtinit(&ia
->ia_ifa
, RTM_ADD
, RTF_HOST
| RTF_UP
);
356 lwkt_serialize_exit(ifp
->if_serializer
);
360 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
362 ia
->ia_broadaddr
= *(struct sockaddr_in
*)&ifr
->ifr_broadaddr
;
366 error
= in_ifinit(ifp
, ia
,
367 (struct sockaddr_in
*) &ifr
->ifr_addr
, 1);
368 if (error
!= 0 && iaIsNew
)
371 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
375 ia
->ia_sockmask
.sin_addr
= ifra
->ifra_addr
.sin_addr
;
376 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
383 if (ia
->ia_addr
.sin_family
== AF_INET
) {
384 if (ifra
->ifra_addr
.sin_len
== 0) {
385 ifra
->ifra_addr
= ia
->ia_addr
;
387 } else if (ifra
->ifra_addr
.sin_addr
.s_addr
==
388 ia
->ia_addr
.sin_addr
.s_addr
)
391 if (ifra
->ifra_mask
.sin_len
) {
393 ia
->ia_sockmask
= ifra
->ifra_mask
;
394 ia
->ia_sockmask
.sin_family
= AF_INET
;
396 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
399 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
400 (ifra
->ifra_dstaddr
.sin_family
== AF_INET
)) {
402 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
403 maskIsNew
= 1; /* We lie; but the effect's the same */
405 if (ifra
->ifra_addr
.sin_family
== AF_INET
&&
406 (hostIsNew
|| maskIsNew
))
407 error
= in_ifinit(ifp
, ia
, &ifra
->ifra_addr
, 0);
409 if (error
!= 0 && iaIsNew
)
412 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
413 (ifra
->ifra_broadaddr
.sin_family
== AF_INET
))
414 ia
->ia_broadaddr
= ifra
->ifra_broadaddr
;
416 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
421 * in_ifscrub kills the interface route.
425 * in_ifadown gets rid of all the rest of
426 * the routes. This is not quite the right
427 * thing to do, but at least if we are running
428 * a routing process they will come back.
430 in_ifadown(&ia
->ia_ifa
, 1);
431 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
436 if (ifp
== NULL
|| ifp
->if_ioctl
== NULL
)
438 lwkt_serialize_enter(ifp
->if_serializer
);
439 error
= ifp
->if_ioctl(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
440 lwkt_serialize_exit(ifp
->if_serializer
);
445 * Protect from ipintr() traversing address list while we're modifying
448 lwkt_serialize_enter(ifp
->if_serializer
);
449 TAILQ_REMOVE(&ifp
->if_addrhead
, &ia
->ia_ifa
, ifa_link
);
450 TAILQ_REMOVE(&in_ifaddrhead
, ia
, ia_link
);
451 LIST_REMOVE(ia
, ia_hash
);
452 IFAFREE(&ia
->ia_ifa
);
453 lwkt_serialize_exit(ifp
->if_serializer
);
460 * SIOCGLIFADDR: get first address. (?!?)
461 * SIOCGLIFADDR with IFLR_PREFIX:
462 * get first address that matches the specified prefix.
463 * SIOCALIFADDR: add the specified address.
464 * SIOCALIFADDR with IFLR_PREFIX:
465 * EINVAL since we can't deduce hostid part of the address.
466 * SIOCDLIFADDR: delete the specified address.
467 * SIOCDLIFADDR with IFLR_PREFIX:
468 * delete the first address that matches the specified prefix.
470 * EINVAL on invalid parameters
471 * EADDRNOTAVAIL on prefix match failed/specified address not found
472 * other values may be returned from in_ioctl()
474 * NOTE! td might be NULL.
477 in_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
480 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
485 panic("invalid argument to in_lifaddr_ioctl");
491 /* address must be specified on GET with IFLR_PREFIX */
492 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
497 /* address must be specified on ADD and DELETE */
498 if (iflr
->addr
.ss_family
!= AF_INET
)
500 if (iflr
->addr
.ss_len
!= sizeof(struct sockaddr_in
))
502 /* XXX need improvement */
503 if (iflr
->dstaddr
.ss_family
504 && iflr
->dstaddr
.ss_family
!= AF_INET
)
506 if (iflr
->dstaddr
.ss_family
507 && iflr
->dstaddr
.ss_len
!= sizeof(struct sockaddr_in
))
510 default: /*shouldn't happen*/
513 if (sizeof(struct in_addr
) * 8 < iflr
->prefixlen
)
519 struct in_aliasreq ifra
;
521 if (iflr
->flags
& IFLR_PREFIX
)
524 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
525 bzero(&ifra
, sizeof ifra
);
526 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof ifra
.ifra_name
);
528 bcopy(&iflr
->addr
, &ifra
.ifra_addr
, iflr
->addr
.ss_len
);
530 if (iflr
->dstaddr
.ss_family
) { /*XXX*/
531 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
532 iflr
->dstaddr
.ss_len
);
535 ifra
.ifra_mask
.sin_family
= AF_INET
;
536 ifra
.ifra_mask
.sin_len
= sizeof(struct sockaddr_in
);
537 in_len2mask(&ifra
.ifra_mask
.sin_addr
, iflr
->prefixlen
);
539 return in_control(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, ifp
, td
);
544 struct in_ifaddr
*ia
;
545 struct in_addr mask
, candidate
, match
;
546 struct sockaddr_in
*sin
;
549 bzero(&mask
, sizeof mask
);
550 if (iflr
->flags
& IFLR_PREFIX
) {
551 /* lookup a prefix rather than address. */
552 in_len2mask(&mask
, iflr
->prefixlen
);
554 sin
= (struct sockaddr_in
*)&iflr
->addr
;
555 match
.s_addr
= sin
->sin_addr
.s_addr
;
556 match
.s_addr
&= mask
.s_addr
;
558 /* if you set extra bits, that's wrong */
559 if (match
.s_addr
!= sin
->sin_addr
.s_addr
)
564 if (cmd
== SIOCGLIFADDR
) {
565 /* on getting an address, take the 1st match */
568 /* on deleting an address, do exact match */
569 in_len2mask(&mask
, 32);
570 sin
= (struct sockaddr_in
*)&iflr
->addr
;
571 match
.s_addr
= sin
->sin_addr
.s_addr
;
577 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
578 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
582 candidate
.s_addr
= ((struct sockaddr_in
*)&ifa
->ifa_addr
)->sin_addr
.s_addr
;
583 candidate
.s_addr
&= mask
.s_addr
;
584 if (candidate
.s_addr
== match
.s_addr
)
588 return EADDRNOTAVAIL
;
589 ia
= (struct in_ifaddr
*)ifa
;
591 if (cmd
== SIOCGLIFADDR
) {
592 /* fill in the if_laddrreq structure */
593 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin_len
);
595 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
596 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
597 ia
->ia_dstaddr
.sin_len
);
599 bzero(&iflr
->dstaddr
, sizeof iflr
->dstaddr
);
602 in_mask2len(&ia
->ia_sockmask
.sin_addr
);
604 iflr
->flags
= 0; /*XXX*/
608 struct in_aliasreq ifra
;
610 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
611 bzero(&ifra
, sizeof ifra
);
612 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
613 sizeof ifra
.ifra_name
);
615 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
616 ia
->ia_addr
.sin_len
);
617 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
618 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
619 ia
->ia_dstaddr
.sin_len
);
621 bcopy(&ia
->ia_sockmask
, &ifra
.ifra_dstaddr
,
622 ia
->ia_sockmask
.sin_len
);
624 return in_control(so
, SIOCDIFADDR
, (caddr_t
)&ifra
,
630 return EOPNOTSUPP
; /*just for safety*/
634 * Delete any existing route for an interface.
637 in_ifscrub(struct ifnet
*ifp
, struct in_ifaddr
*ia
)
640 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
642 if (ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
))
643 rtinit(&ia
->ia_ifa
, RTM_DELETE
, RTF_HOST
);
645 rtinit(&ia
->ia_ifa
, RTM_DELETE
, 0);
646 ia
->ia_flags
&= ~IFA_ROUTE
;
650 * Initialize an interface's internet address
651 * and routing table entry.
654 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
, struct sockaddr_in
*sin
, int scrub
)
656 u_long i
= ntohl(sin
->sin_addr
.s_addr
);
657 struct sockaddr_in oldaddr
;
658 int flags
= RTF_UP
, error
= 0;
660 lwkt_serialize_enter(ifp
->if_serializer
);
662 oldaddr
= ia
->ia_addr
;
663 if (oldaddr
.sin_family
== AF_INET
)
664 LIST_REMOVE(ia
, ia_hash
);
666 if (ia
->ia_addr
.sin_family
== AF_INET
)
667 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
670 * Give the interface a chance to initialize
671 * if this is its first address,
672 * and to validate the address if necessary.
675 (error
= ifp
->if_ioctl(ifp
, SIOCSIFADDR
, (caddr_t
)ia
, NULL
))) {
676 lwkt_serialize_exit(ifp
->if_serializer
);
677 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
678 ia
->ia_addr
= oldaddr
;
679 if (ia
->ia_addr
.sin_family
== AF_INET
)
680 LIST_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
684 lwkt_serialize_exit(ifp
->if_serializer
);
686 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
688 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
691 ia
->ia_netmask
= IN_CLASSA_NET
;
692 else if (IN_CLASSB(i
))
693 ia
->ia_netmask
= IN_CLASSB_NET
;
695 ia
->ia_netmask
= IN_CLASSC_NET
;
697 * The subnet mask usually includes at least the standard network part,
698 * but may may be smaller in the case of supernetting.
699 * If it is set, we believe it.
701 if (ia
->ia_subnetmask
== 0) {
702 ia
->ia_subnetmask
= ia
->ia_netmask
;
703 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
705 ia
->ia_netmask
&= ia
->ia_subnetmask
;
706 ia
->ia_net
= i
& ia
->ia_netmask
;
707 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
708 in_socktrim(&ia
->ia_sockmask
);
710 * Add route for the network.
712 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
713 if (ifp
->if_flags
& IFF_BROADCAST
) {
714 ia
->ia_broadaddr
.sin_addr
.s_addr
=
715 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
716 ia
->ia_netbroadcast
.s_addr
=
717 htonl(ia
->ia_net
| ~ ia
->ia_netmask
);
718 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
719 ia
->ia_ifa
.ifa_dstaddr
= ia
->ia_ifa
.ifa_addr
;
721 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
722 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
)
728 * Don't add host routes for interface addresses of
729 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
730 * possible to assign several such address pairs with consistent
731 * results (no host route) and is required by BOOTP.
733 * XXX: This is ugly ! There should be a way for the caller to
734 * say that they don't want a host route.
736 if (ia
->ia_addr
.sin_addr
.s_addr
!= INADDR_ANY
||
737 ia
->ia_netmask
!= IN_CLASSA_NET
||
738 ia
->ia_dstaddr
.sin_addr
.s_addr
!= htonl(IN_CLASSA_HOST
)) {
739 if ((error
= rtinit(&ia
->ia_ifa
, (int)RTM_ADD
, flags
)) != 0) {
740 ia
->ia_addr
= oldaddr
;
743 ia
->ia_flags
|= IFA_ROUTE
;
747 * If the interface supports multicast, join the "all hosts"
748 * multicast group on that interface.
750 if (ifp
->if_flags
& IFF_MULTICAST
) {
753 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
754 in_addmulti(&addr
, ifp
);
761 * Return 1 if the address might be a local broadcast address.
764 in_broadcast(struct in_addr in
, struct ifnet
*ifp
)
769 if (in
.s_addr
== INADDR_BROADCAST
||
770 in
.s_addr
== INADDR_ANY
)
772 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0)
774 t
= ntohl(in
.s_addr
);
776 * Look through the list of addresses for a match
777 * with a broadcast address.
779 #define ia ((struct in_ifaddr *)ifa)
780 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
781 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
782 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
783 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
785 * Check for old-style (host 0) broadcast.
787 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
789 * Check for an all one subnetmask. These
790 * only exist when an interface gets a secondary
793 ia
->ia_subnetmask
!= (u_long
)0xffffffff)
799 * Add an address to the list of IP multicast addresses for a given interface.
802 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
804 struct in_multi
*inm
;
806 struct sockaddr_in sin
;
807 struct ifmultiaddr
*ifma
;
810 * Call generic routine to add membership or increment
811 * refcount. It wants addresses in the form of a sockaddr,
812 * so we build one here (being careful to zero the unused bytes).
814 bzero(&sin
, sizeof sin
);
815 sin
.sin_family
= AF_INET
;
816 sin
.sin_len
= sizeof sin
;
819 error
= if_addmulti(ifp
, (struct sockaddr
*)&sin
, &ifma
);
826 * If ifma->ifma_protospec is null, then if_addmulti() created
827 * a new record. Otherwise, we are done.
829 if (ifma
->ifma_protospec
!= 0) {
831 return ifma
->ifma_protospec
;
834 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
835 at interrupt time? If so, need to fix if_addmulti. XXX */
836 inm
= kmalloc(sizeof *inm
, M_IPMADDR
, M_WAITOK
| M_ZERO
);
839 inm
->inm_ifma
= ifma
;
840 ifma
->ifma_protospec
= inm
;
841 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
844 * Let IGMP know that we have joined a new IP multicast group.
852 * Delete a multicast address record.
855 in_delmulti(struct in_multi
*inm
)
857 struct ifmultiaddr
*ifma
;
858 struct in_multi my_inm
;
861 ifma
= inm
->inm_ifma
;
862 my_inm
.inm_ifp
= NULL
; /* don't send the leave msg */
863 if (ifma
->ifma_refcount
== 1) {
865 * No remaining claims to this record; let IGMP know that
866 * we are leaving the multicast group.
867 * But do it after the if_delmulti() which might reset
868 * the interface and nuke the packet.
871 ifma
->ifma_protospec
= 0;
872 LIST_REMOVE(inm
, inm_link
);
873 kfree(inm
, M_IPMADDR
);
875 /* XXX - should be separate API for when we have an ifma? */
876 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
877 if (my_inm
.inm_ifp
!= NULL
)
878 igmp_leavegroup(&my_inm
);
883 in_ifdetach(struct ifnet
*ifp
)
885 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo
.pcblisthead
), ifp
);
886 in_pcbpurgeif0(LIST_FIRST(&udbinfo
.pcblisthead
), ifp
);