2 * Copyright (c) 1983, 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 acknowledgment:
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 * $FreeBSD: src/sbin/routed/if.c,v 1.6.2.1 2000/08/14 17:00:03 sheldonh Exp $
34 * $DragonFly: src/sbin/routed/if.c,v 1.5 2005/03/16 21:21:34 cpressey Exp $
38 #include "pathnames.h"
40 #if !defined(sgi) && !defined(__NetBSD__)
41 static char sccsid
[] __attribute__((unused
)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
42 #elif defined(__NetBSD__)
43 #include <sys/cdefs.h>
47 struct interface
*ifnet
; /* all interfaces */
49 /* hash table for all interfaces, big enough to tolerate ridiculous
50 * numbers of IP aliases. Crazy numbers of aliases such as 7000
51 * still will not do well, but not just in looking up interfaces
54 #define AHASH_LEN 211 /* must be prime */
55 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
56 struct interface
*ahash_tbl
[AHASH_LEN
];
58 #define BHASH_LEN 211 /* must be prime */
59 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
60 struct interface
*bhash_tbl
[BHASH_LEN
];
62 struct interface
*remote_if
; /* remote interfaces */
64 /* hash for physical interface names.
65 * Assume there are never more 100 or 200 real interfaces, and that
66 * aliases are put on the end of the hash chains.
69 struct interface
*nhash_tbl
[NHASH_LEN
];
71 int tot_interfaces
; /* # of remote and local interfaces */
72 int rip_interfaces
; /* # of interfaces doing RIP */
73 int foundloopback
; /* valid flag for loopaddr */
74 naddr loopaddr
; /* our address on loopback */
75 struct rt_spare loop_rts
;
77 struct timeval ifinit_timer
;
78 static struct timeval last_ifinit
;
79 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
80 && last_ifinit.tv_usec == now.tv_usec \
81 && timercmp(&ifinit_timer, &now, >))
83 int have_ripv1_out
; /* have a RIPv1 interface */
87 static struct interface
**
92 for (i
= 0; *p
!= '\0'; p
++) {
93 i
= ((i
<<1) & 0x7fffffff) | ((i
>>31) & 1);
96 return &nhash_tbl
[i
% NHASH_LEN
];
100 /* Link a new interface into the lists and hash tables.
103 if_link(struct interface
*ifp
)
105 struct interface
**hifp
;
107 ifp
->int_prev
= &ifnet
;
108 ifp
->int_next
= ifnet
;
110 ifnet
->int_prev
= &ifp
->int_next
;
113 hifp
= AHASH(ifp
->int_addr
);
114 ifp
->int_ahash_prev
= hifp
;
115 if ((ifp
->int_ahash
= *hifp
) != 0)
116 (*hifp
)->int_ahash_prev
= &ifp
->int_ahash
;
119 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
120 hifp
= BHASH(ifp
->int_brdaddr
);
121 ifp
->int_bhash_prev
= hifp
;
122 if ((ifp
->int_bhash
= *hifp
) != 0)
123 (*hifp
)->int_bhash_prev
= &ifp
->int_bhash
;
127 if (ifp
->int_state
& IS_REMOTE
) {
128 ifp
->int_rlink_prev
= &remote_if
;
129 ifp
->int_rlink
= remote_if
;
131 remote_if
->int_rlink_prev
= &ifp
->int_rlink
;
135 hifp
= nhash(ifp
->int_name
);
136 if (ifp
->int_state
& IS_ALIAS
) {
137 /* put aliases on the end of the hash chain */
139 hifp
= &(*hifp
)->int_nhash
;
141 ifp
->int_nhash_prev
= hifp
;
142 if ((ifp
->int_nhash
= *hifp
) != 0)
143 (*hifp
)->int_nhash_prev
= &ifp
->int_nhash
;
148 /* Find the interface with an address
151 ifwithaddr(naddr addr
,
152 int bcast
, /* notice IFF_BROADCAST address */
153 int remote
) /* include IS_REMOTE interfaces */
155 struct interface
*ifp
, *possible
= 0;
157 remote
= (remote
== 0) ? IS_REMOTE
: 0;
159 for (ifp
= *AHASH(addr
); ifp
; ifp
= ifp
->int_ahash
) {
160 if (ifp
->int_addr
!= addr
)
162 if ((ifp
->int_state
& remote
) != 0)
164 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
169 if (possible
|| !bcast
)
172 for (ifp
= *BHASH(addr
); ifp
; ifp
= ifp
->int_bhash
) {
173 if (ifp
->int_brdaddr
!= addr
)
175 if ((ifp
->int_state
& remote
) != 0)
177 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
186 /* find the interface with a name
189 ifwithname(char *name
, /* "ec0" or whatever */
190 naddr addr
) /* 0 or network address */
192 struct interface
*ifp
;
195 for (ifp
= *nhash(name
); ifp
!= 0; ifp
= ifp
->int_nhash
) {
196 /* If the network address is not specified,
197 * ignore any alias interfaces. Otherwise, look
198 * for the interface with the target name and address.
200 if (!strcmp(ifp
->int_name
, name
)
201 && ((addr
== 0 && !(ifp
->int_state
& IS_ALIAS
))
202 || (ifp
->int_addr
== addr
)))
206 /* If there is no known interface, maybe there is a
207 * new interface. So just once look for new interfaces.
209 if (IF_RESCAN_DELAY())
217 ifwithindex(u_short idx
, int rescan_ok
)
219 struct interface
*ifp
;
222 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
) {
223 if (ifp
->int_index
== idx
)
227 /* If there is no known interface, maybe there is a
228 * new interface. So just once look for new interfaces.
231 || IF_RESCAN_DELAY())
238 /* Find an interface from which the specified address
239 * should have come from. Used for figuring out which
240 * interface a packet came in on.
245 struct interface
*ifp
, *maybe
;
249 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
250 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
251 /* finished with a match */
252 if (ifp
->int_dstaddr
== addr
)
256 /* finished with an exact match */
257 if (ifp
->int_addr
== addr
)
260 /* Look for the longest approximate match.
262 if (on_net(addr
, ifp
->int_net
, ifp
->int_mask
)
264 || ifp
->int_mask
> maybe
->int_mask
))
270 || IF_RESCAN_DELAY())
273 /* If there is no known interface, maybe there is a
274 * new interface. So just once look for new interfaces.
281 /* Return the classical netmask for an IP address.
283 naddr
/* host byte order */
284 std_mask(naddr addr
) /* network byte order */
286 addr
= ntohl(addr
); /* was a host, not a network */
288 if (addr
== 0) /* default route has mask 0 */
291 return IN_CLASSA_NET
;
293 return IN_CLASSB_NET
;
294 return IN_CLASSC_NET
;
298 /* Find the netmask that would be inferred by RIPv1 listeners
299 * on the given interface for a given network.
300 * If no interface is specified, look for the best fitting interface.
303 ripv1_mask_net(naddr addr
, /* in network byte order */
304 struct interface
*ifp
) /* as seen on this interface */
309 if (addr
== 0) /* default always has 0 mask */
312 if (ifp
!= 0 && ifp
->int_ripv1_mask
!= HOST_MASK
) {
313 /* If the target network is that of the associated interface
314 * on which it arrived, then use the netmask of the interface.
316 if (on_net(addr
, ifp
->int_net
, ifp
->int_std_mask
))
317 mask
= ifp
->int_ripv1_mask
;
320 /* Examine all interfaces, and if it the target seems
321 * to have the same network number of an interface, use the
322 * netmask of that interface. If there is more than one
323 * such interface, prefer the interface with the longest
326 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
327 if (on_net(addr
, ifp
->int_std_net
, ifp
->int_std_mask
)
328 && ifp
->int_ripv1_mask
> mask
329 && ifp
->int_ripv1_mask
!= HOST_MASK
)
330 mask
= ifp
->int_ripv1_mask
;
335 /* check special definitions */
337 for (r1p
= r1nets
; r1p
!= 0; r1p
= r1p
->r1net_next
) {
338 if (on_net(addr
, r1p
->r1net_net
, r1p
->r1net_match
)
339 && r1p
->r1net_mask
> mask
)
340 mask
= r1p
->r1net_mask
;
343 /* Otherwise, make the classic A/B/C guess.
346 mask
= std_mask(addr
);
354 ripv1_mask_host(naddr addr
, /* in network byte order */
355 struct interface
*ifp
) /* as seen on this interface */
357 naddr mask
= ripv1_mask_net(addr
, ifp
);
360 /* If the computed netmask does not mask the address,
361 * then assume it is a host address
363 if ((ntohl(addr
) & ~mask
) != 0)
369 /* See if a IP address looks reasonable as a destination
372 check_dst(naddr addr
)
376 if (IN_CLASSA(addr
)) {
378 return 1; /* default */
380 addr
>>= IN_CLASSA_NSHIFT
;
381 return (addr
!= 0 && addr
!= IN_LOOPBACKNET
);
384 return (IN_CLASSB(addr
) || IN_CLASSC(addr
));
388 /* See a new interface duplicates an existing interface.
391 check_dup(naddr addr
, /* IP address, so network byte order */
392 naddr dstaddr
, /* ditto */
393 naddr mask
, /* mask, so host byte order */
396 struct interface
*ifp
;
398 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
) {
399 if (ifp
->int_mask
!= mask
)
402 if (!iff_up(ifp
->int_if_flags
))
405 /* The local address can only be shared with a point-to-point
408 if (ifp
->int_addr
== addr
409 && (((if_flags
|ifp
->int_if_flags
) & IFF_POINTOPOINT
) == 0))
412 if (on_net(ifp
->int_dstaddr
, ntohl(dstaddr
),mask
))
419 /* See that a remote gateway is reachable.
420 * Note that the answer can change as real interfaces come and go.
423 check_remote(struct interface
*ifp
)
427 /* do not worry about other kinds */
428 if (!(ifp
->int_state
& IS_REMOTE
))
431 rt
= rtfind(ifp
->int_addr
);
434 &&on_net(ifp
->int_addr
,
435 rt
->rt_ifp
->int_net
, rt
->rt_ifp
->int_mask
))
438 /* the gateway cannot be reached directly from one of our
441 if (!(ifp
->int_state
& IS_BROKE
)) {
442 msglog("unreachable gateway %s in "_PATH_GATEWAYS
,
443 naddr_ntoa(ifp
->int_addr
));
450 /* Delete an interface.
453 ifdel(struct interface
*ifp
)
456 struct interface
*ifp1
;
459 trace_if("Del", ifp
);
461 ifp
->int_state
|= IS_BROKE
;
463 /* unlink the interface
465 *ifp
->int_prev
= ifp
->int_next
;
466 if (ifp
->int_next
!= 0)
467 ifp
->int_next
->int_prev
= ifp
->int_prev
;
468 *ifp
->int_ahash_prev
= ifp
->int_ahash
;
469 if (ifp
->int_ahash
!= 0)
470 ifp
->int_ahash
->int_ahash_prev
= ifp
->int_ahash_prev
;
471 *ifp
->int_nhash_prev
= ifp
->int_nhash
;
472 if (ifp
->int_nhash
!= 0)
473 ifp
->int_nhash
->int_nhash_prev
= ifp
->int_nhash_prev
;
474 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
475 *ifp
->int_bhash_prev
= ifp
->int_bhash
;
476 if (ifp
->int_bhash
!= 0)
477 ifp
->int_bhash
->int_bhash_prev
= ifp
->int_bhash_prev
;
479 if (ifp
->int_state
& IS_REMOTE
) {
480 *ifp
->int_rlink_prev
= ifp
->int_rlink
;
481 if (ifp
->int_rlink
!= 0)
482 ifp
->int_rlink
->int_rlink_prev
= ifp
->int_rlink_prev
;
485 if (!(ifp
->int_state
& IS_ALIAS
)) {
486 /* delete aliases when the main interface dies
488 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
490 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
494 if ((ifp
->int_if_flags
& IFF_MULTICAST
)
496 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
499 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
500 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
504 if (setsockopt(rip_sock
,IPPROTO_IP
,IP_DROP_MEMBERSHIP
,
506 && errno
!= EADDRNOTAVAIL
508 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
509 if (rip_sock_mcast
== ifp
)
512 if (ifp
->int_rip_sock
>= 0) {
513 close(ifp
->int_rip_sock
);
514 ifp
->int_rip_sock
= -1;
519 if (!IS_RIP_OFF(ifp
->int_state
))
522 /* Zap all routes associated with this interface.
523 * Assume routes just using gateways beyond this interface
524 * will timeout naturally, and have probably already died.
526 rn_walktree(rhead
, walk_bad
, 0);
528 set_rdisc_mg(ifp
, 0);
536 /* Mark an interface ill.
539 if_sick(struct interface
*ifp
)
541 if (0 == (ifp
->int_state
& (IS_SICK
| IS_BROKE
))) {
542 ifp
->int_state
|= IS_SICK
;
543 ifp
->int_act_time
= NEVER
;
544 trace_if("Chg", ifp
);
546 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
551 /* Mark an interface dead.
554 if_bad(struct interface
*ifp
)
556 struct interface
*ifp1
;
559 if (ifp
->int_state
& IS_BROKE
)
562 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
564 ifp
->int_state
|= (IS_BROKE
| IS_SICK
);
565 ifp
->int_act_time
= NEVER
;
566 ifp
->int_query_time
= NEVER
;
567 ifp
->int_data
.ts
= now
.tv_sec
;
569 trace_if("Chg", ifp
);
571 if (!(ifp
->int_state
& IS_ALIAS
)) {
572 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
574 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
577 rn_walktree(rhead
, walk_bad
, 0);
583 /* Mark an interface alive
585 int /* 1=it was dead */
586 if_ok(struct interface
*ifp
,
589 struct interface
*ifp1
;
592 if (!(ifp
->int_state
& IS_BROKE
)) {
593 if (ifp
->int_state
& IS_SICK
) {
594 trace_act("%sinterface %s to %s working better",
596 ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
597 ifp
->int_state
&= ~IS_SICK
;
602 msglog("%sinterface %s to %s restored",
603 type
, ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
604 ifp
->int_state
&= ~(IS_BROKE
| IS_SICK
);
605 ifp
->int_data
.ts
= 0;
607 if (!(ifp
->int_state
& IS_ALIAS
)) {
608 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
610 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
616 if (ifp
->int_state
& IS_REMOTE
) {
617 if (!addrouteforif(ifp
))
624 /* disassemble routing message
627 rt_xaddrs(struct rt_addrinfo
*info
,
629 struct sockaddr
*lim
,
634 static struct sockaddr sa_zero
;
637 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
638 : sizeof(__uint64_t))
640 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
645 memset(info
, 0, sizeof(*info
));
646 info
->rti_addrs
= addrs
;
647 for (i
= 0; i
< RTAX_MAX
&& sa
< lim
; i
++) {
648 if ((addrs
& (1 << i
)) == 0)
651 info
->rti_info
[i
] = (sa
->sa_len
!= 0) ? sa
: &sa_zero
;
652 sa
= (struct sockaddr
*)((char*)(sa
)
653 + ROUNDUP(sa
->sa_len
));
655 info
->rti_info
[i
] = sa
;
656 sa
= (struct sockaddr
*)((char*)(sa
)
657 + ROUNDUP(_FAKE_SA_LEN_DST(sa
)));
663 /* Find the network interfaces which have configured themselves.
664 * This must be done regularly, if only for extra addresses
665 * that come and go on interfaces.
670 static char *sysctl_buf
;
671 static size_t sysctl_buf_size
= 0;
673 static u_int prev_complaints
= 0;
674 # define COMP_NOT_INET 0x001
675 # define COMP_NOADDR 0x002
676 # define COMP_BADADDR 0x004
677 # define COMP_NODST 0x008
678 # define COMP_NOBADR 0x010
679 # define COMP_NOMASK 0x020
680 # define COMP_DUP 0x040
681 # define COMP_BAD_METRIC 0x080
682 # define COMP_NETMASK 0x100
684 struct interface ifs
, ifs0
, *ifp
, *ifp1
;
688 struct if_msghdr
*ifm
;
689 struct ifa_msghdr
*ifam
, *ifam_lim
, *ifam2
;
690 int in
, ierr
, out
, oerr
;
691 struct intnet
*intnetp
;
692 struct rt_addrinfo info
;
699 ifinit_timer
.tv_sec
= now
.tv_sec
+ (supplier
701 : CHECK_QUIET_INTERVAL
);
703 /* mark all interfaces so we can get rid of those that disappear */
704 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
)
705 ifp
->int_state
&= ~(IS_CHECKED
| IS_DUP
);
707 /* Fetch the interface list, without too many system calls
708 * since we do it repeatedly.
714 mib
[4] = NET_RT_IFLIST
;
717 if ((needed
= sysctl_buf_size
) != 0) {
718 if (sysctl(mib
, 6, sysctl_buf
,&needed
, 0, 0) >= 0)
720 /* retry if the table grew */
721 if (errno
!= ENOMEM
&& errno
!= EFAULT
)
722 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
726 if (sysctl(mib
, 6, 0, &needed
, 0, 0) < 0)
727 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
728 sysctl_buf
= rtmalloc(sysctl_buf_size
= needed
,
732 ifam_lim
= (struct ifa_msghdr
*)(sysctl_buf
+ needed
);
733 for (ifam
= (struct ifa_msghdr
*)sysctl_buf
;
737 ifam2
= (struct ifa_msghdr
*)((char*)ifam
+ ifam
->ifam_msglen
);
739 if (ifam
->ifam_type
== RTM_IFINFO
) {
740 struct sockaddr_dl
*sdl
;
742 ifm
= (struct if_msghdr
*)ifam
;
743 /* make prototype structure for the IP aliases
745 memset(&ifs0
, 0, sizeof(ifs0
));
746 ifs0
.int_rip_sock
= -1;
747 ifs0
.int_index
= ifm
->ifm_index
;
748 ifs0
.int_if_flags
= ifm
->ifm_flags
;
749 ifs0
.int_state
= IS_CHECKED
;
750 ifs0
.int_query_time
= NEVER
;
751 ifs0
.int_act_time
= now
.tv_sec
;
752 ifs0
.int_data
.ts
= now
.tv_sec
;
753 ifs0
.int_data
.ipackets
= ifm
->ifm_data
.ifi_ipackets
;
754 ifs0
.int_data
.ierrors
= ifm
->ifm_data
.ifi_ierrors
;
755 ifs0
.int_data
.opackets
= ifm
->ifm_data
.ifi_opackets
;
756 ifs0
.int_data
.oerrors
= ifm
->ifm_data
.ifi_oerrors
;
758 ifs0
.int_data
.odrops
= ifm
->ifm_data
.ifi_odrops
;
760 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
761 sdl
->sdl_data
[sdl
->sdl_nlen
] = 0;
762 strncpy(ifs0
.int_name
, sdl
->sdl_data
,
763 MIN(sizeof(ifs0
.int_name
), sdl
->sdl_nlen
));
766 if (ifam
->ifam_type
!= RTM_NEWADDR
) {
767 logbad(1,"ifinit: out of sync");
770 rt_xaddrs(&info
, (struct sockaddr
*)(ifam
+1),
771 (struct sockaddr
*)ifam2
,
774 /* Prepare for the next address of this interface, which
776 * Do not output RIP or Router-Discovery packets via aliases.
778 memcpy(&ifs
, &ifs0
, sizeof(ifs
));
779 ifs0
.int_state
|= (IS_ALIAS
| IS_NO_RIP_OUT
| IS_NO_RDISC
);
781 if (INFO_IFA(&info
) == 0) {
782 if (iff_up(ifs
.int_if_flags
)) {
783 if (!(prev_complaints
& COMP_NOADDR
))
784 msglog("%s has no address",
786 complaints
|= COMP_NOADDR
;
790 if (INFO_IFA(&info
)->sa_family
!= AF_INET
) {
791 if (iff_up(ifs
.int_if_flags
)) {
792 if (!(prev_complaints
& COMP_NOT_INET
))
793 trace_act("%s: not AF_INET",
795 complaints
|= COMP_NOT_INET
;
800 ifs
.int_addr
= S_ADDR(INFO_IFA(&info
));
802 if (ntohl(ifs
.int_addr
)>>24 == 0
803 || ntohl(ifs
.int_addr
)>>24 == 0xff) {
804 if (iff_up(ifs
.int_if_flags
)) {
805 if (!(prev_complaints
& COMP_BADADDR
))
806 msglog("%s has a bad address",
808 complaints
|= COMP_BADADDR
;
813 if (ifs
.int_if_flags
& IFF_LOOPBACK
) {
814 ifs
.int_state
|= IS_PASSIVE
| IS_NO_RIP
| IS_NO_RDISC
;
815 ifs
.int_dstaddr
= ifs
.int_addr
;
816 ifs
.int_mask
= HOST_MASK
;
817 ifs
.int_ripv1_mask
= HOST_MASK
;
818 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
819 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
820 if (!foundloopback
) {
822 loopaddr
= ifs
.int_addr
;
823 loop_rts
.rts_gate
= loopaddr
;
824 loop_rts
.rts_router
= loopaddr
;
827 } else if (ifs
.int_if_flags
& IFF_POINTOPOINT
) {
828 if (INFO_BRD(&info
) == 0
829 || INFO_BRD(&info
)->sa_family
!= AF_INET
) {
830 if (iff_up(ifs
.int_if_flags
)) {
831 if (!(prev_complaints
& COMP_NODST
))
832 msglog("%s has a bad"
833 " destination address",
835 complaints
|= COMP_NODST
;
839 ifs
.int_dstaddr
= S_ADDR(INFO_BRD(&info
));
840 if (ntohl(ifs
.int_dstaddr
)>>24 == 0
841 || ntohl(ifs
.int_dstaddr
)>>24 == 0xff) {
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_mask
= HOST_MASK
;
852 ifs
.int_ripv1_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
853 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
854 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
857 if (INFO_MASK(&info
) == 0) {
858 if (iff_up(ifs
.int_if_flags
)) {
859 if (!(prev_complaints
& COMP_NOMASK
))
860 msglog("%s has no netmask",
862 complaints
|= COMP_NOMASK
;
866 ifs
.int_dstaddr
= ifs
.int_addr
;
867 ifs
.int_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
868 ifs
.int_ripv1_mask
= ifs
.int_mask
;
869 ifs
.int_std_mask
= std_mask(ifs
.int_addr
);
870 ifs
.int_net
= ntohl(ifs
.int_addr
) & ifs
.int_mask
;
871 if (ifs
.int_mask
!= ifs
.int_std_mask
)
872 ifs
.int_state
|= IS_SUBNET
;
874 if (ifs
.int_if_flags
& IFF_BROADCAST
) {
875 if (INFO_BRD(&info
) == 0) {
876 if (iff_up(ifs
.int_if_flags
)) {
877 if (!(prev_complaints
880 "no broadcast address",
882 complaints
|= COMP_NOBADR
;
886 ifs
.int_brdaddr
= S_ADDR(INFO_BRD(&info
));
889 ifs
.int_std_net
= ifs
.int_net
& ifs
.int_std_mask
;
890 ifs
.int_std_addr
= htonl(ifs
.int_std_net
);
892 /* Use a minimum metric of one. Treat the interface metric
893 * (default 0) as an increment to the hop count of one.
895 * The metric obtained from the routing socket dump of
896 * interface addresses is wrong. It is not set by the
897 * SIOCSIFMETRIC ioctl.
900 strncpy(ifr
.ifr_name
, ifs
.int_name
, sizeof(ifr
.ifr_name
));
901 if (ioctl(rt_sock
, SIOCGIFMETRIC
, &ifr
) < 0) {
902 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
905 ifs
.int_metric
= ifr
.ifr_metric
;
908 ifs
.int_metric
= ifam
->ifam_metric
;
910 if (ifs
.int_metric
> HOPCNT_INFINITY
) {
912 if (!(prev_complaints
& COMP_BAD_METRIC
)
913 && iff_up(ifs
.int_if_flags
)) {
914 complaints
|= COMP_BAD_METRIC
;
915 msglog("%s has a metric of %d",
916 ifs
.int_name
, ifs
.int_metric
);
920 /* See if this is a familiar interface.
921 * If so, stop worrying about it if it is the same.
922 * Start it over if it now is to somewhere else, as happens
923 * frequently with PPP and SLIP.
925 ifp
= ifwithname(ifs
.int_name
, ((ifs
.int_state
& IS_ALIAS
)
929 ifp
->int_state
|= IS_CHECKED
;
931 if (0 != ((ifp
->int_if_flags
^ ifs
.int_if_flags
)
936 || 0 != ((ifp
->int_state
^ ifs
.int_state
)
938 || ifp
->int_addr
!= ifs
.int_addr
939 || ifp
->int_brdaddr
!= ifs
.int_brdaddr
940 || ifp
->int_dstaddr
!= ifs
.int_dstaddr
941 || ifp
->int_mask
!= ifs
.int_mask
942 || ifp
->int_metric
!= ifs
.int_metric
) {
943 /* Forget old information about
944 * a changed interface.
946 trace_act("interface %s has changed",
954 /* The primary representative of an alias worries
955 * about how things are working.
957 if (ifp
->int_state
& IS_ALIAS
)
960 /* note interfaces that have been turned off
962 if (!iff_up(ifs
.int_if_flags
)) {
963 if (iff_up(ifp
->int_if_flags
)) {
964 msglog("interface %s to %s turned off",
966 naddr_ntoa(ifp
->int_dstaddr
));
968 ifp
->int_if_flags
&= ~IFF_UP
;
969 } else if (now
.tv_sec
>(ifp
->int_data
.ts
970 + CHECK_BAD_INTERVAL
)) {
971 trace_act("interface %s has been off"
972 " %ld seconds; forget it",
974 now
.tv_sec
-ifp
->int_data
.ts
);
979 /* or that were off and are now ok */
980 if (!iff_up(ifp
->int_if_flags
)) {
981 ifp
->int_if_flags
|= IFF_UP
;
985 /* If it has been long enough,
986 * see if the interface is broken.
988 if (now
.tv_sec
< ifp
->int_data
.ts
+CHECK_BAD_INTERVAL
)
991 in
= ifs
.int_data
.ipackets
- ifp
->int_data
.ipackets
;
992 ierr
= ifs
.int_data
.ierrors
- ifp
->int_data
.ierrors
;
993 out
= ifs
.int_data
.opackets
- ifp
->int_data
.opackets
;
994 oerr
= ifs
.int_data
.oerrors
- ifp
->int_data
.oerrors
;
996 /* Through at least IRIX 6.2, PPP and SLIP
997 * count packets dropped by the filters.
998 * But FDDI rings stuck non-operational count
999 * dropped packets as they wait for improvement.
1001 if (!(ifp
->int_if_flags
& IFF_POINTOPOINT
))
1002 oerr
+= (ifs
.int_data
.odrops
1003 - ifp
->int_data
.odrops
);
1005 /* If the interface just awoke, restart the counters.
1007 if (ifp
->int_data
.ts
== 0) {
1008 ifp
->int_data
= ifs
.int_data
;
1011 ifp
->int_data
= ifs
.int_data
;
1013 /* Withhold judgment when the short error
1014 * counters wrap or the interface is reset.
1016 if (ierr
< 0 || in
< 0 || oerr
< 0 || out
< 0) {
1017 LIM_SEC(ifinit_timer
,
1018 now
.tv_sec
+CHECK_BAD_INTERVAL
);
1022 /* Withhold judgement when there is no traffic
1024 if (in
== 0 && out
== 0 && ierr
== 0 && oerr
== 0)
1027 /* It is bad if input or output is not working.
1028 * Require presistent problems before marking it dead.
1030 if ((in
<= ierr
&& ierr
> 0)
1031 || (out
<= oerr
&& oerr
> 0)) {
1032 if (!(ifp
->int_state
& IS_SICK
)) {
1033 trace_act("interface %s to %s"
1034 " sick: in=%d ierr=%d"
1037 naddr_ntoa(ifp
->int_dstaddr
),
1038 in
, ierr
, out
, oerr
);
1042 if (!(ifp
->int_state
& IS_BROKE
)) {
1043 msglog("interface %s to %s broken:"
1044 " in=%d ierr=%d out=%d oerr=%d",
1046 naddr_ntoa(ifp
->int_dstaddr
),
1047 in
, ierr
, out
, oerr
);
1053 /* otherwise, it is active and healthy
1055 ifp
->int_act_time
= now
.tv_sec
;
1060 /* This is a new interface.
1061 * If it is dead, forget it.
1063 if (!iff_up(ifs
.int_if_flags
))
1066 /* If it duplicates an existing interface,
1067 * complain about it, mark the other one
1068 * duplicated, and forget this one.
1070 ifp
= check_dup(ifs
.int_addr
,ifs
.int_dstaddr
,ifs
.int_mask
,
1073 /* Ignore duplicates of itself, caused by having
1074 * IP aliases on the same network.
1076 if (!strcmp(ifp
->int_name
, ifs
.int_name
))
1079 if (!(prev_complaints
& COMP_DUP
)) {
1080 complaints
|= COMP_DUP
;
1081 msglog("%s (%s%s%s) is duplicated by"
1084 addrname(ifs
.int_addr
,ifs
.int_mask
,1),
1085 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1087 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1088 ? naddr_ntoa(ifs
.int_dstaddr
) : ""),
1090 addrname(ifp
->int_addr
,ifp
->int_mask
,1),
1091 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1093 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1094 ? naddr_ntoa(ifp
->int_dstaddr
) : ""));
1096 ifp
->int_state
|= IS_DUP
;
1100 if (0 == (ifs
.int_if_flags
& (IFF_POINTOPOINT
| IFF_BROADCAST
))
1101 && !(ifs
.int_state
& IS_PASSIVE
)) {
1102 trace_act("%s is neither broadcast, point-to-point,"
1105 if (!(ifs
.int_state
& IFF_MULTICAST
))
1106 ifs
.int_state
|= IS_NO_RDISC
;
1110 /* It is new and ok. Add it to the list of interfaces
1112 ifp
= (struct interface
*)rtmalloc(sizeof(*ifp
), "ifinit ifp");
1113 memcpy(ifp
, &ifs
, sizeof(*ifp
));
1116 trace_if("Add", ifp
);
1118 /* Notice likely bad netmask.
1120 if (!(prev_complaints
& COMP_NETMASK
)
1121 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
1122 && ifp
->int_addr
!= RIP_DEFAULT
) {
1123 for (ifp1
= ifnet
; 0 != ifp1
; ifp1
= ifp1
->int_next
) {
1124 if (ifp1
->int_mask
== ifp
->int_mask
)
1126 if (ifp1
->int_if_flags
& IFF_POINTOPOINT
)
1128 if (ifp1
->int_dstaddr
== RIP_DEFAULT
)
1130 /* ignore aliases on the right network */
1131 if (!strcmp(ifp
->int_name
, ifp1
->int_name
))
1133 if (on_net(ifp
->int_dstaddr
,
1134 ifp1
->int_net
, ifp1
->int_mask
)
1135 || on_net(ifp1
->int_dstaddr
,
1136 ifp
->int_net
, ifp
->int_mask
)) {
1137 msglog("possible netmask problem"
1138 " between %s:%s and %s:%s",
1140 addrname(htonl(ifp
->int_net
),
1143 addrname(htonl(ifp1
->int_net
),
1144 ifp1
->int_mask
, 1));
1145 complaints
|= COMP_NETMASK
;
1150 if (!(ifp
->int_state
& IS_ALIAS
)) {
1151 /* Count the # of directly connected networks.
1153 if (!(ifp
->int_if_flags
& IFF_LOOPBACK
))
1155 if (!IS_RIP_OFF(ifp
->int_state
))
1158 /* turn on router discovery and RIP If needed */
1164 /* If we are multi-homed and have at least two interfaces
1165 * listening to RIP, then output by default.
1167 if (!supplier_set
&& rip_interfaces
> 1)
1170 /* If we are multi-homed, optionally advertise a route to
1174 || (tot_interfaces
> 1
1176 && (ifp
= ifwithaddr(myaddr
, 0, 0)) != 0
1177 && foundloopback
)) {
1178 advertise_mhome
= 1;
1179 rt
= rtget(myaddr
, HOST_MASK
);
1181 if (rt
->rt_ifp
!= ifp
1182 || rt
->rt_router
!= loopaddr
) {
1186 loop_rts
.rts_ifp
= ifp
;
1187 loop_rts
.rts_metric
= 0;
1188 loop_rts
.rts_time
= rt
->rt_time
;
1189 rtchange(rt
, rt
->rt_state
| RS_MHOME
,
1194 loop_rts
.rts_ifp
= ifp
;
1195 loop_rts
.rts_metric
= 0;
1196 rtadd(myaddr
, HOST_MASK
, RS_MHOME
, &loop_rts
);
1200 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp1
) {
1201 ifp1
= ifp
->int_next
; /* because we may delete it */
1203 /* Forget any interfaces that have disappeared.
1205 if (!(ifp
->int_state
& (IS_CHECKED
| IS_REMOTE
))) {
1206 trace_act("interface %s has disappeared",
1212 if ((ifp
->int_state
& IS_BROKE
)
1213 && !(ifp
->int_state
& IS_PASSIVE
))
1214 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
1216 /* If we ever have a RIPv1 interface, assume we always will.
1217 * It might come back if it ever goes away.
1219 if (!(ifp
->int_state
& IS_NO_RIPV1_OUT
) && supplier
)
1221 if (!(ifp
->int_state
& IS_NO_RIPV1_IN
))
1225 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
1226 /* Ensure there is always a network route for interfaces,
1227 * after any dead interfaces have been deleted, which
1228 * might affect routes for point-to-point links.
1230 if (!addrouteforif(ifp
))
1233 /* Add routes to the local end of point-to-point interfaces
1236 if ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1237 && !(ifp
->int_state
& IS_REMOTE
)
1239 /* Delete any routes to the network address through
1240 * foreign routers. Remove even static routes.
1242 del_static(ifp
->int_addr
, HOST_MASK
, 0, 0);
1243 rt
= rtget(ifp
->int_addr
, HOST_MASK
);
1244 if (rt
!= 0 && rt
->rt_router
!= loopaddr
) {
1249 if (!(rt
->rt_state
& RS_LOCAL
)
1250 || rt
->rt_metric
> ifp
->int_metric
) {
1255 loop_rts
.rts_ifp
= ifp1
;
1256 loop_rts
.rts_metric
= 0;
1257 loop_rts
.rts_time
= rt
->rt_time
;
1258 rtchange(rt
, ((rt
->rt_state
& ~RS_NET_SYN
)
1259 | (RS_IF
|RS_LOCAL
)),
1262 loop_rts
.rts_ifp
= ifp
;
1263 loop_rts
.rts_metric
= 0;
1264 rtadd(ifp
->int_addr
, HOST_MASK
,
1265 (RS_IF
| RS_LOCAL
), &loop_rts
);
1270 /* add the authority routes */
1271 for (intnetp
= intnets
; intnetp
!=0; intnetp
= intnetp
->intnet_next
) {
1272 rt
= rtget(intnetp
->intnet_addr
, intnetp
->intnet_mask
);
1274 && !(rt
->rt_state
& RS_NO_NET_SYN
)
1275 && !(rt
->rt_state
& RS_NET_INT
)) {
1280 loop_rts
.rts_ifp
= 0;
1281 loop_rts
.rts_metric
= intnetp
->intnet_metric
-1;
1282 rtadd(intnetp
->intnet_addr
, intnetp
->intnet_mask
,
1283 RS_NET_SYN
| RS_NET_INT
, &loop_rts
);
1287 prev_complaints
= complaints
;
1292 check_net_syn(struct interface
*ifp
)
1294 struct rt_entry
*rt
;
1295 static struct rt_spare
new;
1298 /* Turn on the need to automatically synthesize a network route
1299 * for this interface only if we are running RIPv1 on some other
1300 * interface that is on a different class-A,B,or C network.
1302 if (have_ripv1_out
|| have_ripv1_in
) {
1303 ifp
->int_state
|= IS_NEED_NET_SYN
;
1304 rt
= rtget(ifp
->int_std_addr
, ifp
->int_std_mask
);
1306 && 0 == (rt
->rt_state
& RS_NO_NET_SYN
)
1307 && (!(rt
->rt_state
& RS_NET_SYN
)
1308 || rt
->rt_metric
> ifp
->int_metric
)) {
1314 new.rts_gate
= ifp
->int_addr
;
1315 new.rts_router
= ifp
->int_addr
;
1316 new.rts_metric
= ifp
->int_metric
;
1317 rtadd(ifp
->int_std_addr
, ifp
->int_std_mask
,
1322 ifp
->int_state
&= ~IS_NEED_NET_SYN
;
1324 rt
= rtget(ifp
->int_std_addr
,
1327 && (rt
->rt_state
& RS_NET_SYN
)
1328 && rt
->rt_ifp
== ifp
)
1334 /* Add route for interface if not currently installed.
1335 * Create route to other end if a point-to-point link,
1336 * otherwise a route to this (sub)network.
1338 int /* 0=bad interface */
1339 addrouteforif(struct interface
*ifp
)
1341 struct rt_entry
*rt
;
1342 static struct rt_spare
new;
1346 /* skip sick interfaces
1348 if (ifp
->int_state
& IS_BROKE
)
1351 /* If the interface on a subnet, then install a RIPv1 route to
1352 * the network as well (unless it is sick).
1354 if (ifp
->int_state
& IS_SUBNET
)
1357 dst
= (0 != (ifp
->int_if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))
1359 : htonl(ifp
->int_net
));
1362 new.rts_router
= ifp
->int_addr
;
1363 new.rts_gate
= ifp
->int_addr
;
1364 new.rts_metric
= ifp
->int_metric
;
1365 new.rts_time
= now
.tv_sec
;
1367 /* If we are going to send packets to the gateway,
1368 * it must be reachable using our physical interfaces
1370 if ((ifp
->int_state
& IS_REMOTE
)
1371 && !(ifp
->int_state
& IS_EXTERNAL
)
1372 && !check_remote(ifp
))
1375 /* We are finished if the correct main interface route exists.
1376 * The right route must be for the right interface, not synthesized
1377 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1379 del_static(dst
, ifp
->int_mask
, 0, 0);
1380 rt
= rtget(dst
, ifp
->int_mask
);
1382 if ((rt
->rt_ifp
!= ifp
1383 || rt
->rt_router
!= ifp
->int_addr
)
1384 && (!(ifp
->int_state
& IS_DUP
)
1386 || (rt
->rt_ifp
->int_state
& IS_BROKE
))) {
1390 rtchange(rt
, ((rt
->rt_state
| RS_IF
)
1391 & ~(RS_NET_SYN
| RS_LOCAL
)),
1396 if (ifp
->int_transitions
++ > 0)
1397 trace_act("re-install interface %s",
1400 rtadd(dst
, ifp
->int_mask
, RS_IF
, &new);