1 /* $NetBSD: if.c,v 1.27 2008/12/28 20:15:21 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "pathnames.h"
40 __RCSID("$NetBSD: if.c,v 1.27 2008/12/28 20:15:21 christos Exp $");
41 #elif defined(__FreeBSD__)
44 __RCSID("Revision: 2.27 ");
45 #ident "Revision: 2.27 "
48 struct interface
*ifnet
; /* all interfaces */
50 /* hash table for all interfaces, big enough to tolerate ridiculous
51 * numbers of IP aliases. Crazy numbers of aliases such as 7000
52 * still will not do well, but not just in looking up interfaces
55 #define AHASH_LEN 211 /* must be prime */
56 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
57 struct interface
*ahash_tbl
[AHASH_LEN
];
59 #define BHASH_LEN 211 /* must be prime */
60 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
61 struct interface
*bhash_tbl
[BHASH_LEN
];
63 struct interface
*remote_if
; /* remote interfaces */
65 /* hash for physical interface names.
66 * Assume there are never more 100 or 200 real interfaces, and that
67 * aliases are put on the end of the hash chains.
70 struct interface
*nhash_tbl
[NHASH_LEN
];
72 int tot_interfaces
; /* # of remote and local interfaces */
73 int rip_interfaces
; /* # of interfaces doing RIP */
74 int foundloopback
; /* valid flag for loopaddr */
75 naddr loopaddr
; /* our address on loopback */
76 struct rt_spare loop_rts
;
78 struct timeval ifinit_timer
;
79 static struct timeval last_ifinit
;
80 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
81 && last_ifinit.tv_usec == now.tv_usec \
82 && timercmp(&ifinit_timer, &now, >))
84 int have_ripv1_out
; /* have a RIPv1 interface */
88 static struct interface
**
93 for (i
= 0; *p
!= '\0'; p
++) {
94 i
= ((i
<<1) & 0x7fffffff) | ((i
>>31) & 1);
97 return &nhash_tbl
[i
% NHASH_LEN
];
101 /* Link a new interface into the lists and hash tables.
104 if_link(struct interface
*ifp
)
106 struct interface
**hifp
;
108 ifp
->int_prev
= &ifnet
;
109 ifp
->int_next
= ifnet
;
111 ifnet
->int_prev
= &ifp
->int_next
;
114 hifp
= AHASH(ifp
->int_addr
);
115 ifp
->int_ahash_prev
= hifp
;
116 if ((ifp
->int_ahash
= *hifp
) != 0)
117 (*hifp
)->int_ahash_prev
= &ifp
->int_ahash
;
120 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
121 hifp
= BHASH(ifp
->int_brdaddr
);
122 ifp
->int_bhash_prev
= hifp
;
123 if ((ifp
->int_bhash
= *hifp
) != 0)
124 (*hifp
)->int_bhash_prev
= &ifp
->int_bhash
;
128 if (ifp
->int_state
& IS_REMOTE
) {
129 ifp
->int_rlink_prev
= &remote_if
;
130 ifp
->int_rlink
= remote_if
;
132 remote_if
->int_rlink_prev
= &ifp
->int_rlink
;
136 hifp
= nhash(ifp
->int_name
);
137 if (ifp
->int_state
& IS_ALIAS
) {
138 /* put aliases on the end of the hash chain */
140 hifp
= &(*hifp
)->int_nhash
;
142 ifp
->int_nhash_prev
= hifp
;
143 if ((ifp
->int_nhash
= *hifp
) != 0)
144 (*hifp
)->int_nhash_prev
= &ifp
->int_nhash
;
149 /* Find the interface with an address
152 ifwithaddr(naddr addr
,
153 int bcast
, /* notice IFF_BROADCAST address */
154 int remote
) /* include IS_REMOTE interfaces */
156 struct interface
*ifp
, *possible
= 0;
158 remote
= (remote
== 0) ? IS_REMOTE
: 0;
160 for (ifp
= *AHASH(addr
); ifp
; ifp
= ifp
->int_ahash
) {
161 if (ifp
->int_addr
!= addr
)
163 if ((ifp
->int_state
& remote
) != 0)
165 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
170 if (possible
|| !bcast
)
173 for (ifp
= *BHASH(addr
); ifp
; ifp
= ifp
->int_bhash
) {
174 if (ifp
->int_brdaddr
!= addr
)
176 if ((ifp
->int_state
& remote
) != 0)
178 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
187 /* find the interface with a name
190 ifwithname(char *name
, /* "ec0" or whatever */
191 naddr addr
) /* 0 or network address */
193 struct interface
*ifp
;
196 for (ifp
= *nhash(name
); ifp
!= 0; ifp
= ifp
->int_nhash
) {
197 /* If the network address is not specified,
198 * ignore any alias interfaces. Otherwise, look
199 * for the interface with the target name and address.
201 if (!strcmp(ifp
->int_name
, name
)
202 && ((addr
== 0 && !(ifp
->int_state
& IS_ALIAS
))
203 || (ifp
->int_addr
== addr
)))
207 /* If there is no known interface, maybe there is a
208 * new interface. So just once look for new interfaces.
210 if (IF_RESCAN_DELAY())
218 ifwithindex(u_short ifindex
,
221 struct interface
*ifp
;
224 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
) {
225 if (ifp
->int_index
== ifindex
)
229 /* If there is no known interface, maybe there is a
230 * new interface. So just once look for new interfaces.
233 || IF_RESCAN_DELAY())
240 /* Find an interface from which the specified address
241 * should have come from. Used for figuring out which
242 * interface a packet came in on.
247 struct interface
*ifp
, *maybe
;
252 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
253 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
254 /* finished with a match */
255 if (ifp
->int_dstaddr
== addr
)
259 /* finished with an exact match */
260 if (ifp
->int_addr
== addr
)
263 /* Look for the longest approximate match.
265 if (on_net(addr
, ifp
->int_net
, ifp
->int_mask
)
267 || ifp
->int_mask
> maybe
->int_mask
))
272 if (maybe
!= 0 || once
|| IF_RESCAN_DELAY())
276 /* If there is no known interface, maybe there is a
277 * new interface. So just once look for new interfaces.
284 /* Return the classical netmask for an IP address.
286 naddr
/* host byte order */
287 std_mask(naddr addr
) /* network byte order */
289 addr
= ntohl(addr
); /* was a host, not a network */
291 if (addr
== 0) /* default route has mask 0 */
294 return IN_CLASSA_NET
;
296 return IN_CLASSB_NET
;
297 return IN_CLASSC_NET
;
301 /* Find the netmask that would be inferred by RIPv1 listeners
302 * on the given interface for a given network.
303 * If no interface is specified, look for the best fitting interface.
306 ripv1_mask_net(naddr addr
, /* in network byte order */
307 struct interface
*ifp
) /* as seen on this interface */
312 if (addr
== 0) /* default always has 0 mask */
315 if (ifp
!= 0 && ifp
->int_ripv1_mask
!= HOST_MASK
) {
316 /* If the target network is that of the associated interface
317 * on which it arrived, then use the netmask of the interface.
319 if (on_net(addr
, ifp
->int_net
, ifp
->int_std_mask
))
320 mask
= ifp
->int_ripv1_mask
;
323 /* Examine all interfaces, and if it the target seems
324 * to have the same network number of an interface, use the
325 * netmask of that interface. If there is more than one
326 * such interface, prefer the interface with the longest
329 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
330 if (on_net(addr
, ifp
->int_std_net
, ifp
->int_std_mask
)
331 && ifp
->int_ripv1_mask
> mask
332 && ifp
->int_ripv1_mask
!= HOST_MASK
)
333 mask
= ifp
->int_ripv1_mask
;
338 /* check special definitions */
340 for (r1p
= r1nets
; r1p
!= 0; r1p
= r1p
->r1net_next
) {
341 if (on_net(addr
, r1p
->r1net_net
, r1p
->r1net_match
)
342 && r1p
->r1net_mask
> mask
)
343 mask
= r1p
->r1net_mask
;
346 /* Otherwise, make the classic A/B/C guess.
349 mask
= std_mask(addr
);
357 ripv1_mask_host(naddr addr
, /* in network byte order */
358 struct interface
*ifp
) /* as seen on this interface */
360 naddr mask
= ripv1_mask_net(addr
, ifp
);
363 /* If the computed netmask does not mask the address,
364 * then assume it is a host address
366 if ((ntohl(addr
) & ~mask
) != 0)
372 /* See if a IP address looks reasonable as a destination
375 check_dst(naddr addr
)
379 if (IN_CLASSA(addr
)) {
381 return 1; /* default */
383 addr
>>= IN_CLASSA_NSHIFT
;
384 return (addr
!= 0 && addr
!= IN_LOOPBACKNET
);
387 return (IN_CLASSB(addr
) || IN_CLASSC(addr
));
391 /* See a new interface duplicates an existing interface.
394 check_dup(naddr addr
, /* IP address, so network byte order */
395 naddr dstaddr
, /* ditto */
396 naddr mask
, /* mask, so host byte order */
399 struct interface
*ifp
;
401 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
) {
402 if (ifp
->int_mask
!= mask
)
405 if (!iff_up(ifp
->int_if_flags
))
408 /* The local address can only be shared with a point-to-point
411 if ((!(ifp
->int_state
& IS_REMOTE
) || !(if_flags
& IS_REMOTE
))
412 && ifp
->int_addr
== addr
413 && (((if_flags
|ifp
->int_if_flags
) & IFF_POINTOPOINT
) == 0))
416 if (on_net(ifp
->int_dstaddr
, ntohl(dstaddr
),mask
))
423 /* See that a remote gateway is reachable.
424 * Note that the answer can change as real interfaces come and go.
427 check_remote(struct interface
*ifp
)
431 /* do not worry about other kinds */
432 if (!(ifp
->int_state
& IS_REMOTE
))
435 rt
= rtfind(ifp
->int_addr
);
438 &&on_net(ifp
->int_addr
,
439 rt
->rt_ifp
->int_net
, rt
->rt_ifp
->int_mask
))
442 /* the gateway cannot be reached directly from one of our
445 if (!(ifp
->int_state
& IS_BROKE
)) {
446 msglog("unreachable gateway %s in "_PATH_GATEWAYS
,
447 naddr_ntoa(ifp
->int_addr
));
454 /* Delete an interface.
457 ifdel(struct interface
*ifp
)
460 struct interface
*ifp1
;
463 trace_if("Del", ifp
);
465 ifp
->int_state
|= IS_BROKE
;
467 /* unlink the interface
469 *ifp
->int_prev
= ifp
->int_next
;
470 if (ifp
->int_next
!= 0)
471 ifp
->int_next
->int_prev
= ifp
->int_prev
;
472 *ifp
->int_ahash_prev
= ifp
->int_ahash
;
473 if (ifp
->int_ahash
!= 0)
474 ifp
->int_ahash
->int_ahash_prev
= ifp
->int_ahash_prev
;
475 *ifp
->int_nhash_prev
= ifp
->int_nhash
;
476 if (ifp
->int_nhash
!= 0)
477 ifp
->int_nhash
->int_nhash_prev
= ifp
->int_nhash_prev
;
478 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
479 *ifp
->int_bhash_prev
= ifp
->int_bhash
;
480 if (ifp
->int_bhash
!= 0)
481 ifp
->int_bhash
->int_bhash_prev
= ifp
->int_bhash_prev
;
483 if (ifp
->int_state
& IS_REMOTE
) {
484 *ifp
->int_rlink_prev
= ifp
->int_rlink
;
485 if (ifp
->int_rlink
!= 0)
486 ifp
->int_rlink
->int_rlink_prev
= ifp
->int_rlink_prev
;
489 if (!(ifp
->int_state
& IS_ALIAS
)) {
490 /* delete aliases when the main interface dies
492 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
494 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
498 if ((ifp
->int_if_flags
& IFF_MULTICAST
)
500 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
503 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
505 m
.imr_interface
.s_addr
= htonl(ifp
->int_index
);
507 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
512 if (setsockopt(rip_sock
,IPPROTO_IP
,IP_DROP_MEMBERSHIP
,
514 && errno
!= EADDRNOTAVAIL
516 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
517 if (rip_sock_mcast
== ifp
)
520 if (ifp
->int_rip_sock
>= 0) {
521 (void)close(ifp
->int_rip_sock
);
522 ifp
->int_rip_sock
= -1;
527 if (!IS_RIP_OFF(ifp
->int_state
))
530 /* Zap all routes associated with this interface.
531 * Assume routes just using gateways beyond this interface
532 * will timeout naturally, and have probably already died.
534 (void)rn_walktree(rhead
, walk_bad
, 0);
536 set_rdisc_mg(ifp
, 0);
544 /* Mark an interface ill.
547 if_sick(struct interface
*ifp
)
549 if (0 == (ifp
->int_state
& (IS_SICK
| IS_BROKE
))) {
550 ifp
->int_state
|= IS_SICK
;
551 ifp
->int_act_time
= NEVER
;
552 trace_if("Chg", ifp
);
554 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
559 /* Mark an interface dead.
562 if_bad(struct interface
*ifp
)
564 struct interface
*ifp1
;
567 if (ifp
->int_state
& IS_BROKE
)
570 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
572 ifp
->int_state
|= (IS_BROKE
| IS_SICK
);
573 ifp
->int_act_time
= NEVER
;
574 ifp
->int_query_time
= NEVER
;
575 ifp
->int_data
.ts
= now
.tv_sec
;
577 trace_if("Chg", ifp
);
579 if (!(ifp
->int_state
& IS_ALIAS
)) {
580 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
582 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
585 (void)rn_walktree(rhead
, walk_bad
, 0);
591 /* Mark an interface alive
593 int /* 1=it was dead */
594 if_ok(struct interface
*ifp
,
597 struct interface
*ifp1
;
600 if (!(ifp
->int_state
& IS_BROKE
)) {
601 if (ifp
->int_state
& IS_SICK
) {
602 trace_act("%sinterface %s to %s working better",
604 ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
605 ifp
->int_state
&= ~IS_SICK
;
610 msglog("%sinterface %s to %s restored",
611 type
, ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
612 ifp
->int_state
&= ~(IS_BROKE
| IS_SICK
);
613 ifp
->int_data
.ts
= 0;
615 if (!(ifp
->int_state
& IS_ALIAS
)) {
616 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
618 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
624 if (ifp
->int_state
& IS_REMOTE
) {
625 if (!addrouteforif(ifp
))
632 /* disassemble routing message
635 rt_xaddrs(struct rt_addrinfo
*info
,
637 struct sockaddr
*lim
,
642 static struct sockaddr sa_zero
;
645 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
646 : sizeof(__uint64_t))
648 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
653 memset(info
, 0, sizeof(*info
));
654 info
->rti_addrs
= addrs
;
655 for (i
= 0; i
< RTAX_MAX
&& sa
< lim
; i
++) {
656 if ((addrs
& (1 << i
)) == 0)
659 info
->rti_info
[i
] = (sa
->sa_len
!= 0) ? sa
: &sa_zero
;
660 sa
= (struct sockaddr
*)((char*)(sa
)
661 + ROUNDUP(sa
->sa_len
));
663 info
->rti_info
[i
] = sa
;
664 sa
= (struct sockaddr
*)((char*)(sa
)
665 + ROUNDUP(_FAKE_SA_LEN_DST(sa
)));
671 /* Find the network interfaces which have configured themselves.
672 * This must be done regularly, if only for extra addresses
673 * that come and go on interfaces.
678 static char *sysctl_buf
;
679 static size_t sysctl_buf_size
= 0;
681 static u_int prev_complaints
= 0;
682 # define COMP_NOT_INET 0x001
683 # define COMP_NOADDR 0x002
684 # define COMP_BADADDR 0x004
685 # define COMP_NODST 0x008
686 # define COMP_NOBADR 0x010
687 # define COMP_NOMASK 0x020
688 # define COMP_DUP 0x040
689 # define COMP_BAD_METRIC 0x080
690 # define COMP_NETMASK 0x100
692 struct interface ifs
, ifs0
, *ifp
, *ifp1
;
696 struct if_msghdr
*ifm
;
697 struct ifa_msghdr
*ifam
, *ifam_lim
, *ifam2
;
698 int in
, ierr
, out
, oerr
;
699 struct intnet
*intnetp
;
700 struct rt_addrinfo info
;
707 ifinit_timer
.tv_sec
= now
.tv_sec
+ (supplier
709 : CHECK_QUIET_INTERVAL
);
711 /* mark all interfaces so we can get rid of those that disappear */
712 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
)
713 ifp
->int_state
&= ~(IS_CHECKED
| IS_DUP
);
715 /* Fetch the interface list, without too many system calls
716 * since we do it repeatedly.
722 mib
[4] = NET_RT_IFLIST
;
725 if ((needed
= sysctl_buf_size
) != 0) {
726 if (sysctl(mib
, 6, sysctl_buf
,&needed
, 0, 0) >= 0)
728 /* retry if the table grew */
729 if (errno
!= ENOMEM
&& errno
!= EFAULT
)
730 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
734 if (sysctl(mib
, 6, 0, &needed
, 0, 0) < 0)
735 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
736 sysctl_buf
= rtmalloc(sysctl_buf_size
= needed
,
740 ifam_lim
= (struct ifa_msghdr
*)(sysctl_buf
+ needed
);
741 for (ifam
= (struct ifa_msghdr
*)sysctl_buf
;
745 ifam2
= (struct ifa_msghdr
*)((char*)ifam
+ ifam
->ifam_msglen
);
748 if (ifam
->ifam_type
== RTM_OIFINFO
)
749 continue; /* just ignore compat message */
751 if (ifam
->ifam_type
== RTM_IFINFO
) {
752 const struct sockaddr_dl
*sdl
;
754 ifm
= (struct if_msghdr
*)ifam
;
755 /* make prototype structure for the IP aliases
757 memset(&ifs0
, 0, sizeof(ifs0
));
758 ifs0
.int_rip_sock
= -1;
759 ifs0
.int_index
= ifm
->ifm_index
;
760 ifs0
.int_if_flags
= ifm
->ifm_flags
;
761 ifs0
.int_state
= IS_CHECKED
;
762 ifs0
.int_query_time
= NEVER
;
763 ifs0
.int_act_time
= now
.tv_sec
;
764 ifs0
.int_data
.ts
= now
.tv_sec
;
765 ifs0
.int_data
.ipackets
= ifm
->ifm_data
.ifi_ipackets
;
766 ifs0
.int_data
.ierrors
= ifm
->ifm_data
.ifi_ierrors
;
767 ifs0
.int_data
.opackets
= ifm
->ifm_data
.ifi_opackets
;
768 ifs0
.int_data
.oerrors
= ifm
->ifm_data
.ifi_oerrors
;
770 ifs0
.int_data
.odrops
= ifm
->ifm_data
.ifi_odrops
;
772 sdl
= (const struct sockaddr_dl
*)(ifm
+ 1);
773 /* NUL-termination by memset, above. */
774 memcpy(ifs0
.int_name
, sdl
->sdl_data
,
775 MIN(sizeof(ifs0
.int_name
) - 1, sdl
->sdl_nlen
));
778 if (ifam
->ifam_type
!= RTM_NEWADDR
) {
779 logbad(1,"ifinit: out of sync");
782 rt_xaddrs(&info
, (struct sockaddr
*)(ifam
+1),
783 (struct sockaddr
*)ifam2
,
786 /* Prepare for the next address of this interface, which
788 * Do not output RIP or Router-Discovery packets via aliases.
790 memcpy(&ifs
, &ifs0
, sizeof(ifs
));
791 ifs0
.int_state
|= (IS_ALIAS
| IS_NO_RIP_OUT
| IS_NO_RDISC
);
793 if (INFO_IFA(&info
) == 0) {
794 if (iff_up(ifs
.int_if_flags
)) {
795 if (!(prev_complaints
& COMP_NOADDR
))
796 msglog("%s has no address",
798 complaints
|= COMP_NOADDR
;
802 if (INFO_IFA(&info
)->sa_family
!= AF_INET
) {
803 if (iff_up(ifs
.int_if_flags
)) {
804 if (!(prev_complaints
& COMP_NOT_INET
))
805 trace_act("%s: not AF_INET",
807 complaints
|= COMP_NOT_INET
;
812 ifs
.int_addr
= S_ADDR(INFO_IFA(&info
));
814 if (ntohl(ifs
.int_addr
)>>24 == 0
815 || ntohl(ifs
.int_addr
)>>24 == 0xff) {
816 if (iff_up(ifs
.int_if_flags
)) {
817 if (!(prev_complaints
& COMP_BADADDR
))
818 msglog("%s has a bad address",
820 complaints
|= COMP_BADADDR
;
825 if (ifs
.int_if_flags
& IFF_LOOPBACK
) {
826 ifs
.int_state
|= IS_PASSIVE
| IS_NO_RIP
| IS_NO_RDISC
;
827 ifs
.int_dstaddr
= ifs
.int_addr
;
828 ifs
.int_mask
= HOST_MASK
;
829 ifs
.int_ripv1_mask
= HOST_MASK
;
830 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
831 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
832 if (!foundloopback
) {
834 loopaddr
= ifs
.int_addr
;
835 loop_rts
.rts_gate
= loopaddr
;
836 loop_rts
.rts_router
= loopaddr
;
839 } else if (ifs
.int_if_flags
& IFF_POINTOPOINT
) {
840 if (INFO_BRD(&info
) == 0
841 || INFO_BRD(&info
)->sa_family
!= AF_INET
) {
842 if (iff_up(ifs
.int_if_flags
)) {
843 if (!(prev_complaints
& COMP_NODST
))
844 msglog("%s has a bad"
845 " destination address",
847 complaints
|= COMP_NODST
;
851 ifs
.int_dstaddr
= S_ADDR(INFO_BRD(&info
));
852 if (ntohl(ifs
.int_dstaddr
)>>24 == 0
853 || ntohl(ifs
.int_dstaddr
)>>24 == 0xff) {
854 if (iff_up(ifs
.int_if_flags
)) {
855 if (!(prev_complaints
& COMP_NODST
))
856 msglog("%s has a bad"
857 " destination address",
859 complaints
|= COMP_NODST
;
863 ifs
.int_mask
= HOST_MASK
;
864 ifs
.int_ripv1_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
865 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
866 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
869 if (INFO_MASK(&info
) == 0) {
870 if (iff_up(ifs
.int_if_flags
)) {
871 if (!(prev_complaints
& COMP_NOMASK
))
872 msglog("%s has no netmask",
874 complaints
|= COMP_NOMASK
;
878 ifs
.int_dstaddr
= ifs
.int_addr
;
879 ifs
.int_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
880 ifs
.int_ripv1_mask
= ifs
.int_mask
;
881 ifs
.int_std_mask
= std_mask(ifs
.int_addr
);
882 ifs
.int_net
= ntohl(ifs
.int_addr
) & ifs
.int_mask
;
883 if (ifs
.int_mask
!= ifs
.int_std_mask
)
884 ifs
.int_state
|= IS_SUBNET
;
886 if (ifs
.int_if_flags
& IFF_BROADCAST
) {
887 if (INFO_BRD(&info
) == 0) {
888 if (iff_up(ifs
.int_if_flags
)) {
889 if (!(prev_complaints
892 "no broadcast address",
894 complaints
|= COMP_NOBADR
;
898 ifs
.int_brdaddr
= S_ADDR(INFO_BRD(&info
));
901 ifs
.int_std_net
= ifs
.int_net
& ifs
.int_std_mask
;
902 ifs
.int_std_addr
= htonl(ifs
.int_std_net
);
904 /* Use a minimum metric of one. Treat the interface metric
905 * (default 0) as an increment to the hop count of one.
907 * The metric obtained from the routing socket dump of
908 * interface addresses is wrong. It is not set by the
909 * SIOCSIFMETRIC ioctl.
912 strncpy(ifr
.ifr_name
, ifs
.int_name
, sizeof(ifr
.ifr_name
));
913 if (ioctl(rt_sock
, SIOCGIFMETRIC
, &ifr
) < 0) {
914 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
917 ifs
.int_metric
= ifr
.ifr_metric
;
920 ifs
.int_metric
= ifam
->ifam_metric
;
922 if (ifs
.int_metric
> HOPCNT_INFINITY
) {
924 if (!(prev_complaints
& COMP_BAD_METRIC
)
925 && iff_up(ifs
.int_if_flags
)) {
926 complaints
|= COMP_BAD_METRIC
;
927 msglog("%s has a metric of %d",
928 ifs
.int_name
, ifs
.int_metric
);
932 /* See if this is a familiar interface.
933 * If so, stop worrying about it if it is the same.
934 * Start it over if it now is to somewhere else, as happens
935 * frequently with PPP and SLIP.
937 ifp
= ifwithname(ifs
.int_name
, ((ifs
.int_state
& IS_ALIAS
)
941 ifp
->int_state
|= IS_CHECKED
;
943 if (0 != ((ifp
->int_if_flags
^ ifs
.int_if_flags
)
948 || 0 != ((ifp
->int_state
^ ifs
.int_state
)
950 || ifp
->int_addr
!= ifs
.int_addr
951 || ifp
->int_brdaddr
!= ifs
.int_brdaddr
952 || ifp
->int_dstaddr
!= ifs
.int_dstaddr
953 || ifp
->int_mask
!= ifs
.int_mask
954 || ifp
->int_metric
!= ifs
.int_metric
) {
955 /* Forget old information about
956 * a changed interface.
958 trace_act("interface %s has changed",
966 /* The primary representative of an alias worries
967 * about how things are working.
969 if (ifp
->int_state
& IS_ALIAS
)
972 /* note interfaces that have been turned off
974 if (!iff_up(ifs
.int_if_flags
)) {
975 if (iff_up(ifp
->int_if_flags
)) {
976 msglog("interface %s to %s turned off",
978 naddr_ntoa(ifp
->int_dstaddr
));
980 ifp
->int_if_flags
&= ~IFF_UP
;
981 } else if (now
.tv_sec
>(ifp
->int_data
.ts
982 + CHECK_BAD_INTERVAL
)) {
983 trace_act("interface %s has been off"
984 " %lld seconds; forget it",
986 (long long)now
.tv_sec
-
993 /* or that were off and are now ok */
994 if (!iff_up(ifp
->int_if_flags
)) {
995 ifp
->int_if_flags
|= IFF_UP
;
996 (void)if_ok(ifp
, "");
999 /* If it has been long enough,
1000 * see if the interface is broken.
1002 if (now
.tv_sec
< ifp
->int_data
.ts
+CHECK_BAD_INTERVAL
)
1005 in
= ifs
.int_data
.ipackets
- ifp
->int_data
.ipackets
;
1006 ierr
= ifs
.int_data
.ierrors
- ifp
->int_data
.ierrors
;
1007 out
= ifs
.int_data
.opackets
- ifp
->int_data
.opackets
;
1008 oerr
= ifs
.int_data
.oerrors
- ifp
->int_data
.oerrors
;
1010 /* Through at least IRIX 6.2, PPP and SLIP
1011 * count packets dropped by the filters.
1012 * But FDDI rings stuck non-operational count
1013 * dropped packets as they wait for improvement.
1015 if (!(ifp
->int_if_flags
& IFF_POINTOPOINT
))
1016 oerr
+= (ifs
.int_data
.odrops
1017 - ifp
->int_data
.odrops
);
1019 /* If the interface just awoke, restart the counters.
1021 if (ifp
->int_data
.ts
== 0) {
1022 ifp
->int_data
= ifs
.int_data
;
1025 ifp
->int_data
= ifs
.int_data
;
1027 /* Withhold judgment when the short error
1028 * counters wrap or the interface is reset.
1030 if (ierr
< 0 || in
< 0 || oerr
< 0 || out
< 0) {
1031 LIM_SEC(ifinit_timer
,
1032 now
.tv_sec
+CHECK_BAD_INTERVAL
);
1036 /* Withhold judgement when there is no traffic
1038 if (in
== 0 && out
== 0 && ierr
== 0 && oerr
== 0)
1041 /* It is bad if input or output is not working.
1042 * Require presistent problems before marking it dead.
1044 if ((in
<= ierr
&& ierr
> 0)
1045 || (out
<= oerr
&& oerr
> 0)) {
1046 if (!(ifp
->int_state
& IS_SICK
)) {
1047 trace_act("interface %s to %s"
1048 " sick: in=%d ierr=%d"
1051 naddr_ntoa(ifp
->int_dstaddr
),
1052 in
, ierr
, out
, oerr
);
1056 if (!(ifp
->int_state
& IS_BROKE
)) {
1057 msglog("interface %s to %s broken:"
1058 " in=%d ierr=%d out=%d oerr=%d",
1060 naddr_ntoa(ifp
->int_dstaddr
),
1061 in
, ierr
, out
, oerr
);
1067 /* otherwise, it is active and healthy
1069 ifp
->int_act_time
= now
.tv_sec
;
1070 (void)if_ok(ifp
, "");
1074 /* This is a new interface.
1075 * If it is dead, forget it.
1077 if (!iff_up(ifs
.int_if_flags
))
1080 /* If it duplicates an existing interface,
1081 * complain about it, mark the other one
1082 * duplicated, and forget this one.
1084 ifp
= check_dup(ifs
.int_addr
,ifs
.int_dstaddr
,ifs
.int_mask
,
1087 /* Ignore duplicates of itself, caused by having
1088 * IP aliases on the same network.
1090 if (!strcmp(ifp
->int_name
, ifs
.int_name
))
1093 if (!(prev_complaints
& COMP_DUP
)) {
1094 complaints
|= COMP_DUP
;
1095 msglog("%s (%s%s%s) is duplicated by"
1098 addrname(ifs
.int_addr
,ifs
.int_mask
,1),
1099 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1101 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1102 ? naddr_ntoa(ifs
.int_dstaddr
) : ""),
1104 addrname(ifp
->int_addr
,ifp
->int_mask
,1),
1105 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1107 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1108 ? naddr_ntoa(ifp
->int_dstaddr
) : ""));
1110 ifp
->int_state
|= IS_DUP
;
1114 if (0 == (ifs
.int_if_flags
& (IFF_POINTOPOINT
| IFF_BROADCAST
))
1115 && !(ifs
.int_state
& IS_PASSIVE
)) {
1116 trace_act("%s is neither broadcast, point-to-point,"
1119 if (!(ifs
.int_state
& IFF_MULTICAST
))
1120 ifs
.int_state
|= IS_NO_RDISC
;
1124 /* It is new and ok. Add it to the list of interfaces
1126 ifp
= (struct interface
*)rtmalloc(sizeof(*ifp
), "ifinit ifp");
1127 memcpy(ifp
, &ifs
, sizeof(*ifp
));
1130 trace_if("Add", ifp
);
1132 /* Notice likely bad netmask.
1134 if (!(prev_complaints
& COMP_NETMASK
)
1135 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
1136 && ifp
->int_addr
!= RIP_DEFAULT
) {
1137 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
1138 if (ifp1
->int_mask
== ifp
->int_mask
)
1140 if (ifp1
->int_if_flags
& IFF_POINTOPOINT
)
1142 if (ifp1
->int_dstaddr
== RIP_DEFAULT
)
1144 /* ignore aliases on the right network */
1145 if (!strcmp(ifp
->int_name
, ifp1
->int_name
))
1147 if (on_net(ifp
->int_dstaddr
,
1148 ifp1
->int_net
, ifp1
->int_mask
)
1149 || on_net(ifp1
->int_dstaddr
,
1150 ifp
->int_net
, ifp
->int_mask
)) {
1151 msglog("possible netmask problem"
1152 " between %s:%s and %s:%s",
1154 addrname(htonl(ifp
->int_net
),
1157 addrname(htonl(ifp1
->int_net
),
1158 ifp1
->int_mask
, 1));
1159 complaints
|= COMP_NETMASK
;
1164 if (!(ifp
->int_state
& IS_ALIAS
)) {
1165 /* Count the # of directly connected networks.
1167 if (!(ifp
->int_if_flags
& IFF_LOOPBACK
))
1169 if (!IS_RIP_OFF(ifp
->int_state
))
1172 /* turn on router discovery and RIP If needed */
1178 /* If we are multi-homed and have at least two interfaces
1179 * listening to RIP, then output by default.
1181 if (!supplier_set
&& rip_interfaces
> 1)
1184 /* If we are multi-homed, optionally advertise a route to
1187 if ((advertise_mhome
&& ifp
)
1188 || (tot_interfaces
> 1
1190 && (ifp
= ifwithaddr(myaddr
, 0, 0)) != 0
1191 && foundloopback
)) {
1192 advertise_mhome
= 1;
1193 rt
= rtget(myaddr
, HOST_MASK
);
1195 if (rt
->rt_ifp
!= ifp
1196 || rt
->rt_router
!= loopaddr
) {
1200 loop_rts
.rts_ifp
= ifp
;
1201 loop_rts
.rts_metric
= 0;
1202 loop_rts
.rts_time
= rt
->rt_time
;
1203 rtchange(rt
, rt
->rt_state
| RS_MHOME
,
1208 loop_rts
.rts_ifp
= ifp
;
1209 loop_rts
.rts_metric
= 0;
1210 rtadd(myaddr
, HOST_MASK
, RS_MHOME
, &loop_rts
);
1214 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp1
) {
1215 ifp1
= ifp
->int_next
; /* because we may delete it */
1217 /* Forget any interfaces that have disappeared.
1219 if (!(ifp
->int_state
& (IS_CHECKED
| IS_REMOTE
))) {
1220 trace_act("interface %s has disappeared",
1226 if ((ifp
->int_state
& IS_BROKE
)
1227 && !(ifp
->int_state
& IS_PASSIVE
))
1228 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
1230 /* If we ever have a RIPv1 interface, assume we always will.
1231 * It might come back if it ever goes away.
1233 if (!(ifp
->int_state
& IS_NO_RIPV1_OUT
) && supplier
)
1235 if (!(ifp
->int_state
& IS_NO_RIPV1_IN
))
1239 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
1240 /* Ensure there is always a network route for interfaces,
1241 * after any dead interfaces have been deleted, which
1242 * might affect routes for point-to-point links.
1244 if (!addrouteforif(ifp
))
1247 /* Add routes to the local end of point-to-point interfaces
1250 if ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1251 && !(ifp
->int_state
& IS_REMOTE
)
1253 /* Delete any routes to the network address through
1254 * foreign routers. Remove even static routes.
1256 del_static(ifp
->int_addr
, HOST_MASK
, 0, 0);
1257 rt
= rtget(ifp
->int_addr
, HOST_MASK
);
1258 if (rt
!= 0 && rt
->rt_router
!= loopaddr
) {
1263 if (!(rt
->rt_state
& RS_LOCAL
)
1264 || rt
->rt_metric
> ifp
->int_metric
) {
1269 loop_rts
.rts_ifp
= ifp1
;
1270 loop_rts
.rts_metric
= 0;
1271 loop_rts
.rts_time
= rt
->rt_time
;
1272 rtchange(rt
, ((rt
->rt_state
& ~RS_NET_SYN
)
1273 | (RS_IF
|RS_LOCAL
)),
1276 loop_rts
.rts_ifp
= ifp
;
1277 loop_rts
.rts_metric
= 0;
1278 rtadd(ifp
->int_addr
, HOST_MASK
,
1279 (RS_IF
| RS_LOCAL
), &loop_rts
);
1284 /* add the authority routes */
1285 for (intnetp
= intnets
; intnetp
!=0; intnetp
= intnetp
->intnet_next
) {
1286 rt
= rtget(intnetp
->intnet_addr
, intnetp
->intnet_mask
);
1288 && !(rt
->rt_state
& RS_NO_NET_SYN
)
1289 && !(rt
->rt_state
& RS_NET_INT
)) {
1294 loop_rts
.rts_ifp
= 0;
1295 loop_rts
.rts_metric
= intnetp
->intnet_metric
-1;
1296 rtadd(intnetp
->intnet_addr
, intnetp
->intnet_mask
,
1297 RS_NET_SYN
| RS_NET_INT
, &loop_rts
);
1301 prev_complaints
= complaints
;
1306 check_net_syn(struct interface
*ifp
)
1308 struct rt_entry
*rt
;
1309 static struct rt_spare
new;
1312 /* Turn on the need to automatically synthesize a network route
1313 * for this interface only if we are running RIPv1 on some other
1314 * interface that is on a different class-A,B,or C network.
1316 if (have_ripv1_out
|| have_ripv1_in
) {
1317 ifp
->int_state
|= IS_NEED_NET_SYN
;
1318 rt
= rtget(ifp
->int_std_addr
, ifp
->int_std_mask
);
1320 && 0 == (rt
->rt_state
& RS_NO_NET_SYN
)
1321 && (!(rt
->rt_state
& RS_NET_SYN
)
1322 || rt
->rt_metric
> ifp
->int_metric
)) {
1328 new.rts_gate
= ifp
->int_addr
;
1329 new.rts_router
= ifp
->int_addr
;
1330 new.rts_metric
= ifp
->int_metric
;
1331 rtadd(ifp
->int_std_addr
, ifp
->int_std_mask
,
1336 ifp
->int_state
&= ~IS_NEED_NET_SYN
;
1338 rt
= rtget(ifp
->int_std_addr
,
1341 && (rt
->rt_state
& RS_NET_SYN
)
1342 && rt
->rt_ifp
== ifp
)
1348 /* Add route for interface if not currently installed.
1349 * Create route to other end if a point-to-point link,
1350 * otherwise a route to this (sub)network.
1352 int /* 0=bad interface */
1353 addrouteforif(struct interface
*ifp
)
1355 struct rt_entry
*rt
;
1356 static struct rt_spare
new;
1360 /* skip sick interfaces
1362 if (ifp
->int_state
& IS_BROKE
)
1365 /* If the interface on a subnet, then install a RIPv1 route to
1366 * the network as well (unless it is sick).
1368 if (ifp
->int_state
& IS_SUBNET
)
1371 dst
= (0 != (ifp
->int_if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))
1373 : htonl(ifp
->int_net
));
1376 new.rts_router
= ifp
->int_addr
;
1377 new.rts_gate
= ifp
->int_addr
;
1378 new.rts_metric
= ifp
->int_metric
;
1379 new.rts_time
= now
.tv_sec
;
1381 /* If we are going to send packets to the gateway,
1382 * it must be reachable using our physical interfaces
1384 if ((ifp
->int_state
& IS_REMOTE
)
1385 && !(ifp
->int_state
& IS_EXTERNAL
)
1386 && !check_remote(ifp
))
1389 /* We are finished if the correct main interface route exists.
1390 * The right route must be for the right interface, not synthesized
1391 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1393 del_static(dst
, ifp
->int_mask
, 0, 0);
1394 rt
= rtget(dst
, ifp
->int_mask
);
1396 if ((rt
->rt_ifp
!= ifp
1397 || rt
->rt_router
!= ifp
->int_addr
)
1398 && (!(ifp
->int_state
& IS_DUP
)
1400 || (rt
->rt_ifp
->int_state
& IS_BROKE
))) {
1404 rtchange(rt
, ((rt
->rt_state
| RS_IF
)
1405 & ~(RS_NET_SYN
| RS_LOCAL
)),
1410 if (ifp
->int_transitions
++ > 0)
1411 trace_act("re-install interface %s",
1414 rtadd(dst
, ifp
->int_mask
, RS_IF
, &new);