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. 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 * @(#)if.c 8.1 (Berkeley) 6/5/93
30 * $FreeBSD: src/sbin/routed/if.c,v 1.6.2.1 2000/08/14 17:00:03 sheldonh Exp $
34 #include "pathnames.h"
36 struct interface
*ifnet
; /* all interfaces */
38 /* hash table for all interfaces, big enough to tolerate ridiculous
39 * numbers of IP aliases. Crazy numbers of aliases such as 7000
40 * still will not do well, but not just in looking up interfaces
43 #define AHASH_LEN 211 /* must be prime */
44 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
45 struct interface
*ahash_tbl
[AHASH_LEN
];
47 #define BHASH_LEN 211 /* must be prime */
48 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
49 struct interface
*bhash_tbl
[BHASH_LEN
];
51 struct interface
*remote_if
; /* remote interfaces */
53 /* hash for physical interface names.
54 * Assume there are never more 100 or 200 real interfaces, and that
55 * aliases are put on the end of the hash chains.
58 struct interface
*nhash_tbl
[NHASH_LEN
];
60 int tot_interfaces
; /* # of remote and local interfaces */
61 int rip_interfaces
; /* # of interfaces doing RIP */
62 int foundloopback
; /* valid flag for loopaddr */
63 naddr loopaddr
; /* our address on loopback */
64 struct rt_spare loop_rts
;
66 struct timeval ifinit_timer
;
67 static struct timeval last_ifinit
;
68 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
69 && last_ifinit.tv_usec == now.tv_usec \
70 && timercmp(&ifinit_timer, &now, >))
72 int have_ripv1_out
; /* have a RIPv1 interface */
76 static struct interface
**
81 for (i
= 0; *p
!= '\0'; p
++) {
82 i
= ((i
<<1) & 0x7fffffff) | ((i
>>31) & 1);
85 return &nhash_tbl
[i
% NHASH_LEN
];
89 /* Link a new interface into the lists and hash tables.
92 if_link(struct interface
*ifp
)
94 struct interface
**hifp
;
96 ifp
->int_prev
= &ifnet
;
97 ifp
->int_next
= ifnet
;
99 ifnet
->int_prev
= &ifp
->int_next
;
102 hifp
= AHASH(ifp
->int_addr
);
103 ifp
->int_ahash_prev
= hifp
;
104 if ((ifp
->int_ahash
= *hifp
) != NULL
)
105 (*hifp
)->int_ahash_prev
= &ifp
->int_ahash
;
108 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
109 hifp
= BHASH(ifp
->int_brdaddr
);
110 ifp
->int_bhash_prev
= hifp
;
111 if ((ifp
->int_bhash
= *hifp
) != NULL
)
112 (*hifp
)->int_bhash_prev
= &ifp
->int_bhash
;
116 if (ifp
->int_state
& IS_REMOTE
) {
117 ifp
->int_rlink_prev
= &remote_if
;
118 ifp
->int_rlink
= remote_if
;
119 if (remote_if
!= NULL
)
120 remote_if
->int_rlink_prev
= &ifp
->int_rlink
;
124 hifp
= nhash(ifp
->int_name
);
125 if (ifp
->int_state
& IS_ALIAS
) {
126 /* put aliases on the end of the hash chain */
127 while (*hifp
!= NULL
)
128 hifp
= &(*hifp
)->int_nhash
;
130 ifp
->int_nhash_prev
= hifp
;
131 if ((ifp
->int_nhash
= *hifp
) != NULL
)
132 (*hifp
)->int_nhash_prev
= &ifp
->int_nhash
;
137 /* Find the interface with an address
140 ifwithaddr(naddr addr
,
141 int bcast
, /* notice IFF_BROADCAST address */
142 int remote
) /* include IS_REMOTE interfaces */
144 struct interface
*ifp
, *possible
= NULL
;
146 remote
= (remote
== 0) ? IS_REMOTE
: 0;
148 for (ifp
= *AHASH(addr
); ifp
; ifp
= ifp
->int_ahash
) {
149 if (ifp
->int_addr
!= addr
)
151 if ((ifp
->int_state
& remote
) != 0)
153 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
158 if (possible
|| !bcast
)
161 for (ifp
= *BHASH(addr
); ifp
; ifp
= ifp
->int_bhash
) {
162 if (ifp
->int_brdaddr
!= addr
)
164 if ((ifp
->int_state
& remote
) != 0)
166 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
175 /* find the interface with a name
178 ifwithname(char *name
, /* "ec0" or whatever */
179 naddr addr
) /* 0 or network address */
181 struct interface
*ifp
;
184 for (ifp
= *nhash(name
); ifp
!= NULL
; ifp
= ifp
->int_nhash
) {
185 /* If the network address is not specified,
186 * ignore any alias interfaces. Otherwise, look
187 * for the interface with the target name and address.
189 if (!strcmp(ifp
->int_name
, name
)
190 && ((addr
== 0 && !(ifp
->int_state
& IS_ALIAS
))
191 || (ifp
->int_addr
== addr
)))
195 /* If there is no known interface, maybe there is a
196 * new interface. So just once look for new interfaces.
198 if (IF_RESCAN_DELAY())
206 ifwithindex(u_short idx
, int rescan_ok
)
208 struct interface
*ifp
;
211 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
) {
212 if (ifp
->int_index
== idx
)
216 /* If there is no known interface, maybe there is a
217 * new interface. So just once look for new interfaces.
220 || IF_RESCAN_DELAY())
227 /* Find an interface from which the specified address
228 * should have come from. Used for figuring out which
229 * interface a packet came in on.
234 struct interface
*ifp
, *maybe
;
238 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
239 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
240 /* finished with a match */
241 if (ifp
->int_dstaddr
== addr
)
245 /* finished with an exact match */
246 if (ifp
->int_addr
== addr
)
249 /* Look for the longest approximate match.
251 if (on_net(addr
, ifp
->int_net
, ifp
->int_mask
)
253 || ifp
->int_mask
> maybe
->int_mask
))
259 || IF_RESCAN_DELAY())
262 /* If there is no known interface, maybe there is a
263 * new interface. So just once look for new interfaces.
270 /* Return the classical netmask for an IP address.
272 naddr
/* host byte order */
273 std_mask(naddr addr
) /* network byte order */
275 addr
= ntohl(addr
); /* was a host, not a network */
277 if (addr
== 0) /* default route has mask 0 */
280 return IN_CLASSA_NET
;
282 return IN_CLASSB_NET
;
283 return IN_CLASSC_NET
;
287 /* Find the netmask that would be inferred by RIPv1 listeners
288 * on the given interface for a given network.
289 * If no interface is specified, look for the best fitting interface.
292 ripv1_mask_net(naddr addr
, /* in network byte order */
293 struct interface
*ifp
) /* as seen on this interface */
298 if (addr
== 0) /* default always has 0 mask */
301 if (ifp
!= NULL
&& ifp
->int_ripv1_mask
!= HOST_MASK
) {
302 /* If the target network is that of the associated interface
303 * on which it arrived, then use the netmask of the interface.
305 if (on_net(addr
, ifp
->int_net
, ifp
->int_std_mask
))
306 mask
= ifp
->int_ripv1_mask
;
309 /* Examine all interfaces, and if it the target seems
310 * to have the same network number of an interface, use the
311 * netmask of that interface. If there is more than one
312 * such interface, prefer the interface with the longest
315 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
316 if (on_net(addr
, ifp
->int_std_net
, ifp
->int_std_mask
)
317 && ifp
->int_ripv1_mask
> mask
318 && ifp
->int_ripv1_mask
!= HOST_MASK
)
319 mask
= ifp
->int_ripv1_mask
;
324 /* check special definitions */
326 for (r1p
= r1nets
; r1p
!= NULL
; r1p
= r1p
->r1net_next
) {
327 if (on_net(addr
, r1p
->r1net_net
, r1p
->r1net_match
)
328 && r1p
->r1net_mask
> mask
)
329 mask
= r1p
->r1net_mask
;
332 /* Otherwise, make the classic A/B/C guess.
335 mask
= std_mask(addr
);
343 ripv1_mask_host(naddr addr
, /* in network byte order */
344 struct interface
*ifp
) /* as seen on this interface */
346 naddr mask
= ripv1_mask_net(addr
, ifp
);
349 /* If the computed netmask does not mask the address,
350 * then assume it is a host address
352 if ((ntohl(addr
) & ~mask
) != 0)
358 /* See if a IP address looks reasonable as a destination
361 check_dst(naddr addr
)
365 if (IN_CLASSA(addr
)) {
367 return 1; /* default */
369 addr
>>= IN_CLASSA_NSHIFT
;
370 return (addr
!= 0 && addr
!= IN_LOOPBACKNET
);
373 return (IN_CLASSB(addr
) || IN_CLASSC(addr
));
377 /* See a new interface duplicates an existing interface.
380 check_dup(naddr addr
, /* IP address, so network byte order */
381 naddr dstaddr
, /* ditto */
382 naddr mask
, /* mask, so host byte order */
385 struct interface
*ifp
;
387 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
) {
388 if (ifp
->int_mask
!= mask
)
391 if (!iff_up(ifp
->int_if_flags
))
394 /* The local address can only be shared with a point-to-point
397 if (ifp
->int_addr
== addr
398 && (((if_flags
|ifp
->int_if_flags
) & IFF_POINTOPOINT
) == 0))
401 if (on_net(ifp
->int_dstaddr
, ntohl(dstaddr
),mask
))
408 /* See that a remote gateway is reachable.
409 * Note that the answer can change as real interfaces come and go.
412 check_remote(struct interface
*ifp
)
416 /* do not worry about other kinds */
417 if (!(ifp
->int_state
& IS_REMOTE
))
420 rt
= rtfind(ifp
->int_addr
);
423 &&on_net(ifp
->int_addr
,
424 rt
->rt_ifp
->int_net
, rt
->rt_ifp
->int_mask
))
427 /* the gateway cannot be reached directly from one of our
430 if (!(ifp
->int_state
& IS_BROKE
)) {
431 msglog("unreachable gateway %s in "_PATH_GATEWAYS
,
432 naddr_ntoa(ifp
->int_addr
));
439 /* Delete an interface.
442 ifdel(struct interface
*ifp
)
445 struct interface
*ifp1
;
448 trace_if("Del", ifp
);
450 ifp
->int_state
|= IS_BROKE
;
452 /* unlink the interface
454 *ifp
->int_prev
= ifp
->int_next
;
455 if (ifp
->int_next
!= 0)
456 ifp
->int_next
->int_prev
= ifp
->int_prev
;
457 *ifp
->int_ahash_prev
= ifp
->int_ahash
;
458 if (ifp
->int_ahash
!= 0)
459 ifp
->int_ahash
->int_ahash_prev
= ifp
->int_ahash_prev
;
460 *ifp
->int_nhash_prev
= ifp
->int_nhash
;
461 if (ifp
->int_nhash
!= 0)
462 ifp
->int_nhash
->int_nhash_prev
= ifp
->int_nhash_prev
;
463 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
464 *ifp
->int_bhash_prev
= ifp
->int_bhash
;
465 if (ifp
->int_bhash
!= 0)
466 ifp
->int_bhash
->int_bhash_prev
= ifp
->int_bhash_prev
;
468 if (ifp
->int_state
& IS_REMOTE
) {
469 *ifp
->int_rlink_prev
= ifp
->int_rlink
;
470 if (ifp
->int_rlink
!= 0)
471 ifp
->int_rlink
->int_rlink_prev
= ifp
->int_rlink_prev
;
474 if (!(ifp
->int_state
& IS_ALIAS
)) {
475 /* delete aliases when the main interface dies
477 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
479 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
483 if ((ifp
->int_if_flags
& IFF_MULTICAST
)
485 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
488 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
489 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
493 if (setsockopt(rip_sock
,IPPROTO_IP
,IP_DROP_MEMBERSHIP
,
495 && errno
!= EADDRNOTAVAIL
497 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
498 if (rip_sock_mcast
== ifp
)
501 if (ifp
->int_rip_sock
>= 0) {
502 close(ifp
->int_rip_sock
);
503 ifp
->int_rip_sock
= -1;
508 if (!IS_RIP_OFF(ifp
->int_state
))
511 /* Zap all routes associated with this interface.
512 * Assume routes just using gateways beyond this interface
513 * will timeout naturally, and have probably already died.
515 rhead
->rnh_walktree(rhead
, walk_bad
, NULL
);
517 set_rdisc_mg(ifp
, 0);
525 /* Mark an interface ill.
528 if_sick(struct interface
*ifp
)
530 if (0 == (ifp
->int_state
& (IS_SICK
| IS_BROKE
))) {
531 ifp
->int_state
|= IS_SICK
;
532 ifp
->int_act_time
= NEVER
;
533 trace_if("Chg", ifp
);
535 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
540 /* Mark an interface dead.
543 if_bad(struct interface
*ifp
)
545 struct interface
*ifp1
;
548 if (ifp
->int_state
& IS_BROKE
)
551 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
553 ifp
->int_state
|= (IS_BROKE
| IS_SICK
);
554 ifp
->int_act_time
= NEVER
;
555 ifp
->int_query_time
= NEVER
;
556 ifp
->int_data
.ts
= now
.tv_sec
;
558 trace_if("Chg", ifp
);
560 if (!(ifp
->int_state
& IS_ALIAS
)) {
561 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
563 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
566 rhead
->rnh_walktree(rhead
, walk_bad
, NULL
);
572 /* Mark an interface alive
574 int /* 1=it was dead */
575 if_ok(struct interface
*ifp
,
578 struct interface
*ifp1
;
581 if (!(ifp
->int_state
& IS_BROKE
)) {
582 if (ifp
->int_state
& IS_SICK
) {
583 trace_act("%sinterface %s to %s working better",
585 ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
586 ifp
->int_state
&= ~IS_SICK
;
591 msglog("%sinterface %s to %s restored",
592 type
, ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
593 ifp
->int_state
&= ~(IS_BROKE
| IS_SICK
);
594 ifp
->int_data
.ts
= 0;
596 if (!(ifp
->int_state
& IS_ALIAS
)) {
597 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
599 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
605 if (ifp
->int_state
& IS_REMOTE
) {
606 if (!addrouteforif(ifp
))
613 /* disassemble routing message
616 rt_xaddrs(struct rt_addrinfo
*info
,
618 struct sockaddr
*lim
,
622 static struct sockaddr sa_zero
;
623 #define ROUNDUP(a) RT_ROUNDUP(a)
626 memset(info
, 0, sizeof(*info
));
627 info
->rti_addrs
= addrs
;
628 for (i
= 0; i
< RTAX_MAX
&& sa
< lim
; i
++) {
629 if ((addrs
& (1 << i
)) == 0)
631 info
->rti_info
[i
] = (sa
->sa_len
!= 0) ? sa
: &sa_zero
;
632 sa
= (struct sockaddr
*)((char*)(sa
)
633 + ROUNDUP(sa
->sa_len
));
638 /* Find the network interfaces which have configured themselves.
639 * This must be done regularly, if only for extra addresses
640 * that come and go on interfaces.
645 static char *sysctl_buf
;
646 static size_t sysctl_buf_size
= 0;
648 static u_int prev_complaints
= 0;
649 # define COMP_NOT_INET 0x001
650 # define COMP_NOADDR 0x002
651 # define COMP_BADADDR 0x004
652 # define COMP_NODST 0x008
653 # define COMP_NOBADR 0x010
654 # define COMP_NOMASK 0x020
655 # define COMP_DUP 0x040
656 # define COMP_BAD_METRIC 0x080
657 # define COMP_NETMASK 0x100
659 struct interface ifs
, ifs0
, *ifp
, *ifp1
;
663 struct if_msghdr
*ifm
;
664 struct ifa_msghdr
*ifam
, *ifam_lim
, *ifam2
;
665 int in
, ierr
, out
, oerr
;
666 struct intnet
*intnetp
;
667 struct rt_addrinfo info
;
674 ifinit_timer
.tv_sec
= now
.tv_sec
+ (supplier
676 : CHECK_QUIET_INTERVAL
);
678 /* mark all interfaces so we can get rid of those that disappear */
679 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
)
680 ifp
->int_state
&= ~(IS_CHECKED
| IS_DUP
);
682 /* Fetch the interface list, without too many system calls
683 * since we do it repeatedly.
689 mib
[4] = NET_RT_IFLIST
;
692 if ((needed
= sysctl_buf_size
) != 0) {
693 if (sysctl(mib
, 6, sysctl_buf
,&needed
, 0, 0) >= 0)
695 /* retry if the table grew */
696 if (errno
!= ENOMEM
&& errno
!= EFAULT
)
697 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
701 if (sysctl(mib
, 6, 0, &needed
, 0, 0) < 0)
702 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
703 sysctl_buf
= rtmalloc(sysctl_buf_size
= needed
,
707 ifam_lim
= (struct ifa_msghdr
*)(sysctl_buf
+ needed
);
708 for (ifam
= (struct ifa_msghdr
*)sysctl_buf
;
712 ifam2
= (struct ifa_msghdr
*)((char*)ifam
+ ifam
->ifam_msglen
);
714 if (ifam
->ifam_type
== RTM_IFINFO
) {
715 struct sockaddr_dl
*sdl
;
717 ifm
= (struct if_msghdr
*)ifam
;
718 /* make prototype structure for the IP aliases
720 memset(&ifs0
, 0, sizeof(ifs0
));
721 ifs0
.int_rip_sock
= -1;
722 ifs0
.int_index
= ifm
->ifm_index
;
723 ifs0
.int_if_flags
= ifm
->ifm_flags
;
724 ifs0
.int_state
= IS_CHECKED
;
725 ifs0
.int_query_time
= NEVER
;
726 ifs0
.int_act_time
= now
.tv_sec
;
727 ifs0
.int_data
.ts
= now
.tv_sec
;
728 ifs0
.int_data
.ipackets
= ifm
->ifm_data
.ifi_ipackets
;
729 ifs0
.int_data
.ierrors
= ifm
->ifm_data
.ifi_ierrors
;
730 ifs0
.int_data
.opackets
= ifm
->ifm_data
.ifi_opackets
;
731 ifs0
.int_data
.oerrors
= ifm
->ifm_data
.ifi_oerrors
;
732 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
733 sdl
->sdl_data
[sdl
->sdl_nlen
] = 0;
734 strncpy(ifs0
.int_name
, sdl
->sdl_data
,
735 MIN(sizeof(ifs0
.int_name
), sdl
->sdl_nlen
));
738 if (ifam
->ifam_type
!= RTM_NEWADDR
) {
739 logbad(1,"ifinit: out of sync");
742 rt_xaddrs(&info
, (struct sockaddr
*)(ifam
+1),
743 (struct sockaddr
*)ifam2
,
746 /* Prepare for the next address of this interface, which
748 * Do not output RIP or Router-Discovery packets via aliases.
750 memcpy(&ifs
, &ifs0
, sizeof(ifs
));
751 ifs0
.int_state
|= (IS_ALIAS
| IS_NO_RIP_OUT
| IS_NO_RDISC
);
753 if (INFO_IFA(&info
) == 0) {
754 if (iff_up(ifs
.int_if_flags
)) {
755 if (!(prev_complaints
& COMP_NOADDR
))
756 msglog("%s has no address",
758 complaints
|= COMP_NOADDR
;
762 if (INFO_IFA(&info
)->sa_family
!= AF_INET
) {
763 if (iff_up(ifs
.int_if_flags
)) {
764 if (!(prev_complaints
& COMP_NOT_INET
))
765 trace_act("%s: not AF_INET",
767 complaints
|= COMP_NOT_INET
;
772 ifs
.int_addr
= S_ADDR(INFO_IFA(&info
));
774 if (ntohl(ifs
.int_addr
)>>24 == 0
775 || ntohl(ifs
.int_addr
)>>24 == 0xff) {
776 if (iff_up(ifs
.int_if_flags
)) {
777 if (!(prev_complaints
& COMP_BADADDR
))
778 msglog("%s has a bad address",
780 complaints
|= COMP_BADADDR
;
785 if (ifs
.int_if_flags
& IFF_LOOPBACK
) {
786 ifs
.int_state
|= IS_PASSIVE
| IS_NO_RIP
| IS_NO_RDISC
;
787 ifs
.int_dstaddr
= ifs
.int_addr
;
788 ifs
.int_mask
= HOST_MASK
;
789 ifs
.int_ripv1_mask
= HOST_MASK
;
790 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
791 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
792 if (!foundloopback
) {
794 loopaddr
= ifs
.int_addr
;
795 loop_rts
.rts_gate
= loopaddr
;
796 loop_rts
.rts_router
= loopaddr
;
799 } else if (ifs
.int_if_flags
& IFF_POINTOPOINT
) {
800 if (INFO_BRD(&info
) == 0
801 || INFO_BRD(&info
)->sa_family
!= AF_INET
) {
802 if (iff_up(ifs
.int_if_flags
)) {
803 if (!(prev_complaints
& COMP_NODST
))
804 msglog("%s has a bad"
805 " destination address",
807 complaints
|= COMP_NODST
;
811 ifs
.int_dstaddr
= S_ADDR(INFO_BRD(&info
));
812 if (ntohl(ifs
.int_dstaddr
)>>24 == 0
813 || ntohl(ifs
.int_dstaddr
)>>24 == 0xff) {
814 if (iff_up(ifs
.int_if_flags
)) {
815 if (!(prev_complaints
& COMP_NODST
))
816 msglog("%s has a bad"
817 " destination address",
819 complaints
|= COMP_NODST
;
823 ifs
.int_mask
= HOST_MASK
;
824 ifs
.int_ripv1_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
825 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
826 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
829 if (INFO_MASK(&info
) == 0) {
830 if (iff_up(ifs
.int_if_flags
)) {
831 if (!(prev_complaints
& COMP_NOMASK
))
832 msglog("%s has no netmask",
834 complaints
|= COMP_NOMASK
;
838 ifs
.int_dstaddr
= ifs
.int_addr
;
839 ifs
.int_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
840 ifs
.int_ripv1_mask
= ifs
.int_mask
;
841 ifs
.int_std_mask
= std_mask(ifs
.int_addr
);
842 ifs
.int_net
= ntohl(ifs
.int_addr
) & ifs
.int_mask
;
843 if (ifs
.int_mask
!= ifs
.int_std_mask
)
844 ifs
.int_state
|= IS_SUBNET
;
846 if (ifs
.int_if_flags
& IFF_BROADCAST
) {
847 if (INFO_BRD(&info
) == 0) {
848 if (iff_up(ifs
.int_if_flags
)) {
849 if (!(prev_complaints
852 "no broadcast address",
854 complaints
|= COMP_NOBADR
;
858 ifs
.int_brdaddr
= S_ADDR(INFO_BRD(&info
));
861 ifs
.int_std_net
= ifs
.int_net
& ifs
.int_std_mask
;
862 ifs
.int_std_addr
= htonl(ifs
.int_std_net
);
864 /* Use a minimum metric of one. Treat the interface metric
865 * (default 0) as an increment to the hop count of one.
867 * The metric obtained from the routing socket dump of
868 * interface addresses is wrong. It is not set by the
869 * SIOCSIFMETRIC ioctl.
872 strncpy(ifr
.ifr_name
, ifs
.int_name
, sizeof(ifr
.ifr_name
));
873 if (ioctl(rt_sock
, SIOCGIFMETRIC
, &ifr
) < 0) {
874 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
877 ifs
.int_metric
= ifr
.ifr_metric
;
880 ifs
.int_metric
= ifam
->ifam_metric
;
882 if (ifs
.int_metric
> HOPCNT_INFINITY
) {
884 if (!(prev_complaints
& COMP_BAD_METRIC
)
885 && iff_up(ifs
.int_if_flags
)) {
886 complaints
|= COMP_BAD_METRIC
;
887 msglog("%s has a metric of %d",
888 ifs
.int_name
, ifs
.int_metric
);
892 /* See if this is a familiar interface.
893 * If so, stop worrying about it if it is the same.
894 * Start it over if it now is to somewhere else, as happens
895 * frequently with PPP and SLIP.
897 ifp
= ifwithname(ifs
.int_name
, ((ifs
.int_state
& IS_ALIAS
)
901 ifp
->int_state
|= IS_CHECKED
;
903 if (0 != ((ifp
->int_if_flags
^ ifs
.int_if_flags
)
908 || 0 != ((ifp
->int_state
^ ifs
.int_state
)
910 || ifp
->int_addr
!= ifs
.int_addr
911 || ifp
->int_brdaddr
!= ifs
.int_brdaddr
912 || ifp
->int_dstaddr
!= ifs
.int_dstaddr
913 || ifp
->int_mask
!= ifs
.int_mask
914 || ifp
->int_metric
!= ifs
.int_metric
) {
915 /* Forget old information about
916 * a changed interface.
918 trace_act("interface %s has changed",
926 /* The primary representative of an alias worries
927 * about how things are working.
929 if (ifp
->int_state
& IS_ALIAS
)
932 /* note interfaces that have been turned off
934 if (!iff_up(ifs
.int_if_flags
)) {
935 if (iff_up(ifp
->int_if_flags
)) {
936 msglog("interface %s to %s turned off",
938 naddr_ntoa(ifp
->int_dstaddr
));
940 ifp
->int_if_flags
&= ~IFF_UP
;
941 } else if (now
.tv_sec
>(ifp
->int_data
.ts
942 + CHECK_BAD_INTERVAL
)) {
943 trace_act("interface %s has been off"
944 " %ld seconds; forget it",
946 now
.tv_sec
-ifp
->int_data
.ts
);
951 /* or that were off and are now ok */
952 if (!iff_up(ifp
->int_if_flags
)) {
953 ifp
->int_if_flags
|= IFF_UP
;
957 /* If it has been long enough,
958 * see if the interface is broken.
960 if (now
.tv_sec
< ifp
->int_data
.ts
+CHECK_BAD_INTERVAL
)
963 in
= ifs
.int_data
.ipackets
- ifp
->int_data
.ipackets
;
964 ierr
= ifs
.int_data
.ierrors
- ifp
->int_data
.ierrors
;
965 out
= ifs
.int_data
.opackets
- ifp
->int_data
.opackets
;
966 oerr
= ifs
.int_data
.oerrors
- ifp
->int_data
.oerrors
;
967 /* If the interface just awoke, restart the counters.
969 if (ifp
->int_data
.ts
== 0) {
970 ifp
->int_data
= ifs
.int_data
;
973 ifp
->int_data
= ifs
.int_data
;
975 /* Withhold judgment when the short error
976 * counters wrap or the interface is reset.
978 if (ierr
< 0 || in
< 0 || oerr
< 0 || out
< 0) {
979 LIM_SEC(ifinit_timer
,
980 now
.tv_sec
+CHECK_BAD_INTERVAL
);
984 /* Withhold judgement when there is no traffic
986 if (in
== 0 && out
== 0 && ierr
== 0 && oerr
== 0)
989 /* It is bad if input or output is not working.
990 * Require presistent problems before marking it dead.
992 if ((in
<= ierr
&& ierr
> 0)
993 || (out
<= oerr
&& oerr
> 0)) {
994 if (!(ifp
->int_state
& IS_SICK
)) {
995 trace_act("interface %s to %s"
996 " sick: in=%d ierr=%d"
999 naddr_ntoa(ifp
->int_dstaddr
),
1000 in
, ierr
, out
, oerr
);
1004 if (!(ifp
->int_state
& IS_BROKE
)) {
1005 msglog("interface %s to %s broken:"
1006 " in=%d ierr=%d out=%d oerr=%d",
1008 naddr_ntoa(ifp
->int_dstaddr
),
1009 in
, ierr
, out
, oerr
);
1015 /* otherwise, it is active and healthy
1017 ifp
->int_act_time
= now
.tv_sec
;
1022 /* This is a new interface.
1023 * If it is dead, forget it.
1025 if (!iff_up(ifs
.int_if_flags
))
1028 /* If it duplicates an existing interface,
1029 * complain about it, mark the other one
1030 * duplicated, and forget this one.
1032 ifp
= check_dup(ifs
.int_addr
,ifs
.int_dstaddr
,ifs
.int_mask
,
1035 /* Ignore duplicates of itself, caused by having
1036 * IP aliases on the same network.
1038 if (!strcmp(ifp
->int_name
, ifs
.int_name
))
1041 if (!(prev_complaints
& COMP_DUP
)) {
1042 complaints
|= COMP_DUP
;
1043 msglog("%s (%s%s%s) is duplicated by"
1046 addrname(ifs
.int_addr
,ifs
.int_mask
,1),
1047 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1049 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1050 ? naddr_ntoa(ifs
.int_dstaddr
) : ""),
1052 addrname(ifp
->int_addr
,ifp
->int_mask
,1),
1053 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1055 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1056 ? naddr_ntoa(ifp
->int_dstaddr
) : ""));
1058 ifp
->int_state
|= IS_DUP
;
1062 if (0 == (ifs
.int_if_flags
& (IFF_POINTOPOINT
| IFF_BROADCAST
))
1063 && !(ifs
.int_state
& IS_PASSIVE
)) {
1064 trace_act("%s is neither broadcast, point-to-point,"
1067 if (!(ifs
.int_state
& IFF_MULTICAST
))
1068 ifs
.int_state
|= IS_NO_RDISC
;
1072 /* It is new and ok. Add it to the list of interfaces
1074 ifp
= (struct interface
*)rtmalloc(sizeof(*ifp
), "ifinit ifp");
1075 memcpy(ifp
, &ifs
, sizeof(*ifp
));
1078 trace_if("Add", ifp
);
1080 /* Notice likely bad netmask.
1082 if (!(prev_complaints
& COMP_NETMASK
)
1083 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
1084 && ifp
->int_addr
!= RIP_DEFAULT
) {
1085 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
1086 if (ifp1
->int_mask
== ifp
->int_mask
)
1088 if (ifp1
->int_if_flags
& IFF_POINTOPOINT
)
1090 if (ifp1
->int_dstaddr
== RIP_DEFAULT
)
1092 /* ignore aliases on the right network */
1093 if (!strcmp(ifp
->int_name
, ifp1
->int_name
))
1095 if (on_net(ifp
->int_dstaddr
,
1096 ifp1
->int_net
, ifp1
->int_mask
)
1097 || on_net(ifp1
->int_dstaddr
,
1098 ifp
->int_net
, ifp
->int_mask
)) {
1099 msglog("possible netmask problem"
1100 " between %s:%s and %s:%s",
1102 addrname(htonl(ifp
->int_net
),
1105 addrname(htonl(ifp1
->int_net
),
1106 ifp1
->int_mask
, 1));
1107 complaints
|= COMP_NETMASK
;
1112 if (!(ifp
->int_state
& IS_ALIAS
)) {
1113 /* Count the # of directly connected networks.
1115 if (!(ifp
->int_if_flags
& IFF_LOOPBACK
))
1117 if (!IS_RIP_OFF(ifp
->int_state
))
1120 /* turn on router discovery and RIP If needed */
1126 /* If we are multi-homed and have at least two interfaces
1127 * listening to RIP, then output by default.
1129 if (!supplier_set
&& rip_interfaces
> 1)
1132 /* If we are multi-homed, optionally advertise a route to
1136 || (tot_interfaces
> 1
1138 && (ifp
= ifwithaddr(myaddr
, 0, 0)) != NULL
1139 && foundloopback
)) {
1140 advertise_mhome
= 1;
1141 rt
= rtget(myaddr
, HOST_MASK
);
1143 if (rt
->rt_ifp
!= ifp
1144 || rt
->rt_router
!= loopaddr
) {
1148 loop_rts
.rts_ifp
= ifp
;
1149 loop_rts
.rts_metric
= 0;
1150 loop_rts
.rts_time
= rt
->rt_time
;
1151 rtchange(rt
, rt
->rt_state
| RS_MHOME
,
1156 loop_rts
.rts_ifp
= ifp
;
1157 loop_rts
.rts_metric
= 0;
1158 rtadd(myaddr
, HOST_MASK
, RS_MHOME
, &loop_rts
);
1162 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp1
) {
1163 ifp1
= ifp
->int_next
; /* because we may delete it */
1165 /* Forget any interfaces that have disappeared.
1167 if (!(ifp
->int_state
& (IS_CHECKED
| IS_REMOTE
))) {
1168 trace_act("interface %s has disappeared",
1174 if ((ifp
->int_state
& IS_BROKE
)
1175 && !(ifp
->int_state
& IS_PASSIVE
))
1176 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
1178 /* If we ever have a RIPv1 interface, assume we always will.
1179 * It might come back if it ever goes away.
1181 if (!(ifp
->int_state
& IS_NO_RIPV1_OUT
) && supplier
)
1183 if (!(ifp
->int_state
& IS_NO_RIPV1_IN
))
1187 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
1188 /* Ensure there is always a network route for interfaces,
1189 * after any dead interfaces have been deleted, which
1190 * might affect routes for point-to-point links.
1192 if (!addrouteforif(ifp
))
1195 /* Add routes to the local end of point-to-point interfaces
1198 if ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1199 && !(ifp
->int_state
& IS_REMOTE
)
1201 /* Delete any routes to the network address through
1202 * foreign routers. Remove even static routes.
1204 del_static(ifp
->int_addr
, HOST_MASK
, 0, 0);
1205 rt
= rtget(ifp
->int_addr
, HOST_MASK
);
1206 if (rt
!= NULL
&& rt
->rt_router
!= loopaddr
) {
1211 if (!(rt
->rt_state
& RS_LOCAL
)
1212 || rt
->rt_metric
> ifp
->int_metric
) {
1217 loop_rts
.rts_ifp
= ifp1
;
1218 loop_rts
.rts_metric
= 0;
1219 loop_rts
.rts_time
= rt
->rt_time
;
1220 rtchange(rt
, ((rt
->rt_state
& ~RS_NET_SYN
)
1221 | (RS_IF
|RS_LOCAL
)),
1224 loop_rts
.rts_ifp
= ifp
;
1225 loop_rts
.rts_metric
= 0;
1226 rtadd(ifp
->int_addr
, HOST_MASK
,
1227 (RS_IF
| RS_LOCAL
), &loop_rts
);
1232 /* add the authority routes */
1233 for (intnetp
= intnets
; intnetp
!=NULL
; intnetp
= intnetp
->intnet_next
) {
1234 rt
= rtget(intnetp
->intnet_addr
, intnetp
->intnet_mask
);
1236 && !(rt
->rt_state
& RS_NO_NET_SYN
)
1237 && !(rt
->rt_state
& RS_NET_INT
)) {
1242 loop_rts
.rts_ifp
= 0;
1243 loop_rts
.rts_metric
= intnetp
->intnet_metric
-1;
1244 rtadd(intnetp
->intnet_addr
, intnetp
->intnet_mask
,
1245 RS_NET_SYN
| RS_NET_INT
, &loop_rts
);
1249 prev_complaints
= complaints
;
1254 check_net_syn(struct interface
*ifp
)
1256 struct rt_entry
*rt
;
1257 static struct rt_spare
new;
1260 /* Turn on the need to automatically synthesize a network route
1261 * for this interface only if we are running RIPv1 on some other
1262 * interface that is on a different class-A,B,or C network.
1264 if (have_ripv1_out
|| have_ripv1_in
) {
1265 ifp
->int_state
|= IS_NEED_NET_SYN
;
1266 rt
= rtget(ifp
->int_std_addr
, ifp
->int_std_mask
);
1268 && 0 == (rt
->rt_state
& RS_NO_NET_SYN
)
1269 && (!(rt
->rt_state
& RS_NET_SYN
)
1270 || rt
->rt_metric
> ifp
->int_metric
)) {
1276 new.rts_gate
= ifp
->int_addr
;
1277 new.rts_router
= ifp
->int_addr
;
1278 new.rts_metric
= ifp
->int_metric
;
1279 rtadd(ifp
->int_std_addr
, ifp
->int_std_mask
,
1284 ifp
->int_state
&= ~IS_NEED_NET_SYN
;
1286 rt
= rtget(ifp
->int_std_addr
,
1289 && (rt
->rt_state
& RS_NET_SYN
)
1290 && rt
->rt_ifp
== ifp
)
1296 /* Add route for interface if not currently installed.
1297 * Create route to other end if a point-to-point link,
1298 * otherwise a route to this (sub)network.
1300 int /* 0=bad interface */
1301 addrouteforif(struct interface
*ifp
)
1303 struct rt_entry
*rt
;
1304 static struct rt_spare
new;
1308 /* skip sick interfaces
1310 if (ifp
->int_state
& IS_BROKE
)
1313 /* If the interface on a subnet, then install a RIPv1 route to
1314 * the network as well (unless it is sick).
1316 if (ifp
->int_state
& IS_SUBNET
)
1319 dst
= (0 != (ifp
->int_if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))
1321 : htonl(ifp
->int_net
));
1324 new.rts_router
= ifp
->int_addr
;
1325 new.rts_gate
= ifp
->int_addr
;
1326 new.rts_metric
= ifp
->int_metric
;
1327 new.rts_time
= now
.tv_sec
;
1329 /* If we are going to send packets to the gateway,
1330 * it must be reachable using our physical interfaces
1332 if ((ifp
->int_state
& IS_REMOTE
)
1333 && !(ifp
->int_state
& IS_EXTERNAL
)
1334 && !check_remote(ifp
))
1337 /* We are finished if the correct main interface route exists.
1338 * The right route must be for the right interface, not synthesized
1339 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1341 del_static(dst
, ifp
->int_mask
, 0, 0);
1342 rt
= rtget(dst
, ifp
->int_mask
);
1344 if ((rt
->rt_ifp
!= ifp
1345 || rt
->rt_router
!= ifp
->int_addr
)
1346 && (!(ifp
->int_state
& IS_DUP
)
1348 || (rt
->rt_ifp
->int_state
& IS_BROKE
))) {
1352 rtchange(rt
, ((rt
->rt_state
| RS_IF
)
1353 & ~(RS_NET_SYN
| RS_LOCAL
)),
1358 if (ifp
->int_transitions
++ > 0)
1359 trace_act("re-install interface %s",
1362 rtadd(dst
, ifp
->int_mask
, RS_IF
, &new);