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 * $FreeBSD: src/sbin/routed/if.c,v 1.6.2.1 2000/08/14 17:00:03 sheldonh Exp $
33 #include "pathnames.h"
35 #if !defined(sgi) && !defined(__NetBSD__)
36 static char sccsid
[] __attribute__((unused
)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
37 #elif defined(__NetBSD__)
38 #include <sys/cdefs.h>
42 struct interface
*ifnet
; /* all interfaces */
44 /* hash table for all interfaces, big enough to tolerate ridiculous
45 * numbers of IP aliases. Crazy numbers of aliases such as 7000
46 * still will not do well, but not just in looking up interfaces
49 #define AHASH_LEN 211 /* must be prime */
50 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
51 struct interface
*ahash_tbl
[AHASH_LEN
];
53 #define BHASH_LEN 211 /* must be prime */
54 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
55 struct interface
*bhash_tbl
[BHASH_LEN
];
57 struct interface
*remote_if
; /* remote interfaces */
59 /* hash for physical interface names.
60 * Assume there are never more 100 or 200 real interfaces, and that
61 * aliases are put on the end of the hash chains.
64 struct interface
*nhash_tbl
[NHASH_LEN
];
66 int tot_interfaces
; /* # of remote and local interfaces */
67 int rip_interfaces
; /* # of interfaces doing RIP */
68 int foundloopback
; /* valid flag for loopaddr */
69 naddr loopaddr
; /* our address on loopback */
70 struct rt_spare loop_rts
;
72 struct timeval ifinit_timer
;
73 static struct timeval last_ifinit
;
74 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
75 && last_ifinit.tv_usec == now.tv_usec \
76 && timercmp(&ifinit_timer, &now, >))
78 int have_ripv1_out
; /* have a RIPv1 interface */
82 static struct interface
**
87 for (i
= 0; *p
!= '\0'; p
++) {
88 i
= ((i
<<1) & 0x7fffffff) | ((i
>>31) & 1);
91 return &nhash_tbl
[i
% NHASH_LEN
];
95 /* Link a new interface into the lists and hash tables.
98 if_link(struct interface
*ifp
)
100 struct interface
**hifp
;
102 ifp
->int_prev
= &ifnet
;
103 ifp
->int_next
= ifnet
;
105 ifnet
->int_prev
= &ifp
->int_next
;
108 hifp
= AHASH(ifp
->int_addr
);
109 ifp
->int_ahash_prev
= hifp
;
110 if ((ifp
->int_ahash
= *hifp
) != NULL
)
111 (*hifp
)->int_ahash_prev
= &ifp
->int_ahash
;
114 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
115 hifp
= BHASH(ifp
->int_brdaddr
);
116 ifp
->int_bhash_prev
= hifp
;
117 if ((ifp
->int_bhash
= *hifp
) != NULL
)
118 (*hifp
)->int_bhash_prev
= &ifp
->int_bhash
;
122 if (ifp
->int_state
& IS_REMOTE
) {
123 ifp
->int_rlink_prev
= &remote_if
;
124 ifp
->int_rlink
= remote_if
;
125 if (remote_if
!= NULL
)
126 remote_if
->int_rlink_prev
= &ifp
->int_rlink
;
130 hifp
= nhash(ifp
->int_name
);
131 if (ifp
->int_state
& IS_ALIAS
) {
132 /* put aliases on the end of the hash chain */
133 while (*hifp
!= NULL
)
134 hifp
= &(*hifp
)->int_nhash
;
136 ifp
->int_nhash_prev
= hifp
;
137 if ((ifp
->int_nhash
= *hifp
) != NULL
)
138 (*hifp
)->int_nhash_prev
= &ifp
->int_nhash
;
143 /* Find the interface with an address
146 ifwithaddr(naddr addr
,
147 int bcast
, /* notice IFF_BROADCAST address */
148 int remote
) /* include IS_REMOTE interfaces */
150 struct interface
*ifp
, *possible
= NULL
;
152 remote
= (remote
== 0) ? IS_REMOTE
: 0;
154 for (ifp
= *AHASH(addr
); ifp
; ifp
= ifp
->int_ahash
) {
155 if (ifp
->int_addr
!= addr
)
157 if ((ifp
->int_state
& remote
) != 0)
159 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
164 if (possible
|| !bcast
)
167 for (ifp
= *BHASH(addr
); ifp
; ifp
= ifp
->int_bhash
) {
168 if (ifp
->int_brdaddr
!= addr
)
170 if ((ifp
->int_state
& remote
) != 0)
172 if ((ifp
->int_state
& (IS_BROKE
| IS_PASSIVE
)) == 0)
181 /* find the interface with a name
184 ifwithname(char *name
, /* "ec0" or whatever */
185 naddr addr
) /* 0 or network address */
187 struct interface
*ifp
;
190 for (ifp
= *nhash(name
); ifp
!= NULL
; ifp
= ifp
->int_nhash
) {
191 /* If the network address is not specified,
192 * ignore any alias interfaces. Otherwise, look
193 * for the interface with the target name and address.
195 if (!strcmp(ifp
->int_name
, name
)
196 && ((addr
== 0 && !(ifp
->int_state
& IS_ALIAS
))
197 || (ifp
->int_addr
== addr
)))
201 /* If there is no known interface, maybe there is a
202 * new interface. So just once look for new interfaces.
204 if (IF_RESCAN_DELAY())
212 ifwithindex(u_short idx
, int rescan_ok
)
214 struct interface
*ifp
;
217 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
) {
218 if (ifp
->int_index
== idx
)
222 /* If there is no known interface, maybe there is a
223 * new interface. So just once look for new interfaces.
226 || IF_RESCAN_DELAY())
233 /* Find an interface from which the specified address
234 * should have come from. Used for figuring out which
235 * interface a packet came in on.
240 struct interface
*ifp
, *maybe
;
244 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
245 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
246 /* finished with a match */
247 if (ifp
->int_dstaddr
== addr
)
251 /* finished with an exact match */
252 if (ifp
->int_addr
== addr
)
255 /* Look for the longest approximate match.
257 if (on_net(addr
, ifp
->int_net
, ifp
->int_mask
)
259 || ifp
->int_mask
> maybe
->int_mask
))
265 || IF_RESCAN_DELAY())
268 /* If there is no known interface, maybe there is a
269 * new interface. So just once look for new interfaces.
276 /* Return the classical netmask for an IP address.
278 naddr
/* host byte order */
279 std_mask(naddr addr
) /* network byte order */
281 addr
= ntohl(addr
); /* was a host, not a network */
283 if (addr
== 0) /* default route has mask 0 */
286 return IN_CLASSA_NET
;
288 return IN_CLASSB_NET
;
289 return IN_CLASSC_NET
;
293 /* Find the netmask that would be inferred by RIPv1 listeners
294 * on the given interface for a given network.
295 * If no interface is specified, look for the best fitting interface.
298 ripv1_mask_net(naddr addr
, /* in network byte order */
299 struct interface
*ifp
) /* as seen on this interface */
304 if (addr
== 0) /* default always has 0 mask */
307 if (ifp
!= NULL
&& ifp
->int_ripv1_mask
!= HOST_MASK
) {
308 /* If the target network is that of the associated interface
309 * on which it arrived, then use the netmask of the interface.
311 if (on_net(addr
, ifp
->int_net
, ifp
->int_std_mask
))
312 mask
= ifp
->int_ripv1_mask
;
315 /* Examine all interfaces, and if it the target seems
316 * to have the same network number of an interface, use the
317 * netmask of that interface. If there is more than one
318 * such interface, prefer the interface with the longest
321 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
322 if (on_net(addr
, ifp
->int_std_net
, ifp
->int_std_mask
)
323 && ifp
->int_ripv1_mask
> mask
324 && ifp
->int_ripv1_mask
!= HOST_MASK
)
325 mask
= ifp
->int_ripv1_mask
;
330 /* check special definitions */
332 for (r1p
= r1nets
; r1p
!= NULL
; r1p
= r1p
->r1net_next
) {
333 if (on_net(addr
, r1p
->r1net_net
, r1p
->r1net_match
)
334 && r1p
->r1net_mask
> mask
)
335 mask
= r1p
->r1net_mask
;
338 /* Otherwise, make the classic A/B/C guess.
341 mask
= std_mask(addr
);
349 ripv1_mask_host(naddr addr
, /* in network byte order */
350 struct interface
*ifp
) /* as seen on this interface */
352 naddr mask
= ripv1_mask_net(addr
, ifp
);
355 /* If the computed netmask does not mask the address,
356 * then assume it is a host address
358 if ((ntohl(addr
) & ~mask
) != 0)
364 /* See if a IP address looks reasonable as a destination
367 check_dst(naddr addr
)
371 if (IN_CLASSA(addr
)) {
373 return 1; /* default */
375 addr
>>= IN_CLASSA_NSHIFT
;
376 return (addr
!= 0 && addr
!= IN_LOOPBACKNET
);
379 return (IN_CLASSB(addr
) || IN_CLASSC(addr
));
383 /* See a new interface duplicates an existing interface.
386 check_dup(naddr addr
, /* IP address, so network byte order */
387 naddr dstaddr
, /* ditto */
388 naddr mask
, /* mask, so host byte order */
391 struct interface
*ifp
;
393 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
) {
394 if (ifp
->int_mask
!= mask
)
397 if (!iff_up(ifp
->int_if_flags
))
400 /* The local address can only be shared with a point-to-point
403 if (ifp
->int_addr
== addr
404 && (((if_flags
|ifp
->int_if_flags
) & IFF_POINTOPOINT
) == 0))
407 if (on_net(ifp
->int_dstaddr
, ntohl(dstaddr
),mask
))
414 /* See that a remote gateway is reachable.
415 * Note that the answer can change as real interfaces come and go.
418 check_remote(struct interface
*ifp
)
422 /* do not worry about other kinds */
423 if (!(ifp
->int_state
& IS_REMOTE
))
426 rt
= rtfind(ifp
->int_addr
);
429 &&on_net(ifp
->int_addr
,
430 rt
->rt_ifp
->int_net
, rt
->rt_ifp
->int_mask
))
433 /* the gateway cannot be reached directly from one of our
436 if (!(ifp
->int_state
& IS_BROKE
)) {
437 msglog("unreachable gateway %s in "_PATH_GATEWAYS
,
438 naddr_ntoa(ifp
->int_addr
));
445 /* Delete an interface.
448 ifdel(struct interface
*ifp
)
451 struct interface
*ifp1
;
454 trace_if("Del", ifp
);
456 ifp
->int_state
|= IS_BROKE
;
458 /* unlink the interface
460 *ifp
->int_prev
= ifp
->int_next
;
461 if (ifp
->int_next
!= 0)
462 ifp
->int_next
->int_prev
= ifp
->int_prev
;
463 *ifp
->int_ahash_prev
= ifp
->int_ahash
;
464 if (ifp
->int_ahash
!= 0)
465 ifp
->int_ahash
->int_ahash_prev
= ifp
->int_ahash_prev
;
466 *ifp
->int_nhash_prev
= ifp
->int_nhash
;
467 if (ifp
->int_nhash
!= 0)
468 ifp
->int_nhash
->int_nhash_prev
= ifp
->int_nhash_prev
;
469 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
470 *ifp
->int_bhash_prev
= ifp
->int_bhash
;
471 if (ifp
->int_bhash
!= 0)
472 ifp
->int_bhash
->int_bhash_prev
= ifp
->int_bhash_prev
;
474 if (ifp
->int_state
& IS_REMOTE
) {
475 *ifp
->int_rlink_prev
= ifp
->int_rlink
;
476 if (ifp
->int_rlink
!= 0)
477 ifp
->int_rlink
->int_rlink_prev
= ifp
->int_rlink_prev
;
480 if (!(ifp
->int_state
& IS_ALIAS
)) {
481 /* delete aliases when the main interface dies
483 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
485 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
489 if ((ifp
->int_if_flags
& IFF_MULTICAST
)
491 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
494 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
495 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
499 if (setsockopt(rip_sock
,IPPROTO_IP
,IP_DROP_MEMBERSHIP
,
501 && errno
!= EADDRNOTAVAIL
503 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
504 if (rip_sock_mcast
== ifp
)
507 if (ifp
->int_rip_sock
>= 0) {
508 close(ifp
->int_rip_sock
);
509 ifp
->int_rip_sock
= -1;
514 if (!IS_RIP_OFF(ifp
->int_state
))
517 /* Zap all routes associated with this interface.
518 * Assume routes just using gateways beyond this interface
519 * will timeout naturally, and have probably already died.
521 rn_walktree(rhead
, walk_bad
, 0);
523 set_rdisc_mg(ifp
, 0);
531 /* Mark an interface ill.
534 if_sick(struct interface
*ifp
)
536 if (0 == (ifp
->int_state
& (IS_SICK
| IS_BROKE
))) {
537 ifp
->int_state
|= IS_SICK
;
538 ifp
->int_act_time
= NEVER
;
539 trace_if("Chg", ifp
);
541 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
546 /* Mark an interface dead.
549 if_bad(struct interface
*ifp
)
551 struct interface
*ifp1
;
554 if (ifp
->int_state
& IS_BROKE
)
557 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
559 ifp
->int_state
|= (IS_BROKE
| IS_SICK
);
560 ifp
->int_act_time
= NEVER
;
561 ifp
->int_query_time
= NEVER
;
562 ifp
->int_data
.ts
= now
.tv_sec
;
564 trace_if("Chg", ifp
);
566 if (!(ifp
->int_state
& IS_ALIAS
)) {
567 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
569 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
572 rn_walktree(rhead
, walk_bad
, 0);
578 /* Mark an interface alive
580 int /* 1=it was dead */
581 if_ok(struct interface
*ifp
,
584 struct interface
*ifp1
;
587 if (!(ifp
->int_state
& IS_BROKE
)) {
588 if (ifp
->int_state
& IS_SICK
) {
589 trace_act("%sinterface %s to %s working better",
591 ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
592 ifp
->int_state
&= ~IS_SICK
;
597 msglog("%sinterface %s to %s restored",
598 type
, ifp
->int_name
, naddr_ntoa(ifp
->int_dstaddr
));
599 ifp
->int_state
&= ~(IS_BROKE
| IS_SICK
);
600 ifp
->int_data
.ts
= 0;
602 if (!(ifp
->int_state
& IS_ALIAS
)) {
603 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
605 && !strcmp(ifp
->int_name
, ifp1
->int_name
))
611 if (ifp
->int_state
& IS_REMOTE
) {
612 if (!addrouteforif(ifp
))
619 /* disassemble routing message
622 rt_xaddrs(struct rt_addrinfo
*info
,
624 struct sockaddr
*lim
,
629 static struct sockaddr sa_zero
;
632 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
633 : sizeof(__uint64_t))
635 #define ROUNDUP(a) RT_ROUNDUP(a)
639 memset(info
, 0, sizeof(*info
));
640 info
->rti_addrs
= addrs
;
641 for (i
= 0; i
< RTAX_MAX
&& sa
< lim
; i
++) {
642 if ((addrs
& (1 << i
)) == 0)
645 info
->rti_info
[i
] = (sa
->sa_len
!= 0) ? sa
: &sa_zero
;
646 sa
= (struct sockaddr
*)((char*)(sa
)
647 + ROUNDUP(sa
->sa_len
));
649 info
->rti_info
[i
] = sa
;
650 sa
= (struct sockaddr
*)((char*)(sa
)
651 + ROUNDUP(_FAKE_SA_LEN_DST(sa
)));
657 /* Find the network interfaces which have configured themselves.
658 * This must be done regularly, if only for extra addresses
659 * that come and go on interfaces.
664 static char *sysctl_buf
;
665 static size_t sysctl_buf_size
= 0;
667 static u_int prev_complaints
= 0;
668 # define COMP_NOT_INET 0x001
669 # define COMP_NOADDR 0x002
670 # define COMP_BADADDR 0x004
671 # define COMP_NODST 0x008
672 # define COMP_NOBADR 0x010
673 # define COMP_NOMASK 0x020
674 # define COMP_DUP 0x040
675 # define COMP_BAD_METRIC 0x080
676 # define COMP_NETMASK 0x100
678 struct interface ifs
, ifs0
, *ifp
, *ifp1
;
682 struct if_msghdr
*ifm
;
683 struct ifa_msghdr
*ifam
, *ifam_lim
, *ifam2
;
684 int in
, ierr
, out
, oerr
;
685 struct intnet
*intnetp
;
686 struct rt_addrinfo info
;
693 ifinit_timer
.tv_sec
= now
.tv_sec
+ (supplier
695 : CHECK_QUIET_INTERVAL
);
697 /* mark all interfaces so we can get rid of those that disappear */
698 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
)
699 ifp
->int_state
&= ~(IS_CHECKED
| IS_DUP
);
701 /* Fetch the interface list, without too many system calls
702 * since we do it repeatedly.
708 mib
[4] = NET_RT_IFLIST
;
711 if ((needed
= sysctl_buf_size
) != 0) {
712 if (sysctl(mib
, 6, sysctl_buf
,&needed
, 0, 0) >= 0)
714 /* retry if the table grew */
715 if (errno
!= ENOMEM
&& errno
!= EFAULT
)
716 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
720 if (sysctl(mib
, 6, 0, &needed
, 0, 0) < 0)
721 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
722 sysctl_buf
= rtmalloc(sysctl_buf_size
= needed
,
726 ifam_lim
= (struct ifa_msghdr
*)(sysctl_buf
+ needed
);
727 for (ifam
= (struct ifa_msghdr
*)sysctl_buf
;
731 ifam2
= (struct ifa_msghdr
*)((char*)ifam
+ ifam
->ifam_msglen
);
733 if (ifam
->ifam_type
== RTM_IFINFO
) {
734 struct sockaddr_dl
*sdl
;
736 ifm
= (struct if_msghdr
*)ifam
;
737 /* make prototype structure for the IP aliases
739 memset(&ifs0
, 0, sizeof(ifs0
));
740 ifs0
.int_rip_sock
= -1;
741 ifs0
.int_index
= ifm
->ifm_index
;
742 ifs0
.int_if_flags
= ifm
->ifm_flags
;
743 ifs0
.int_state
= IS_CHECKED
;
744 ifs0
.int_query_time
= NEVER
;
745 ifs0
.int_act_time
= now
.tv_sec
;
746 ifs0
.int_data
.ts
= now
.tv_sec
;
747 ifs0
.int_data
.ipackets
= ifm
->ifm_data
.ifi_ipackets
;
748 ifs0
.int_data
.ierrors
= ifm
->ifm_data
.ifi_ierrors
;
749 ifs0
.int_data
.opackets
= ifm
->ifm_data
.ifi_opackets
;
750 ifs0
.int_data
.oerrors
= ifm
->ifm_data
.ifi_oerrors
;
752 ifs0
.int_data
.odrops
= ifm
->ifm_data
.ifi_odrops
;
754 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
755 sdl
->sdl_data
[sdl
->sdl_nlen
] = 0;
756 strncpy(ifs0
.int_name
, sdl
->sdl_data
,
757 MIN(sizeof(ifs0
.int_name
), sdl
->sdl_nlen
));
760 if (ifam
->ifam_type
!= RTM_NEWADDR
) {
761 logbad(1,"ifinit: out of sync");
764 rt_xaddrs(&info
, (struct sockaddr
*)(ifam
+1),
765 (struct sockaddr
*)ifam2
,
768 /* Prepare for the next address of this interface, which
770 * Do not output RIP or Router-Discovery packets via aliases.
772 memcpy(&ifs
, &ifs0
, sizeof(ifs
));
773 ifs0
.int_state
|= (IS_ALIAS
| IS_NO_RIP_OUT
| IS_NO_RDISC
);
775 if (INFO_IFA(&info
) == 0) {
776 if (iff_up(ifs
.int_if_flags
)) {
777 if (!(prev_complaints
& COMP_NOADDR
))
778 msglog("%s has no address",
780 complaints
|= COMP_NOADDR
;
784 if (INFO_IFA(&info
)->sa_family
!= AF_INET
) {
785 if (iff_up(ifs
.int_if_flags
)) {
786 if (!(prev_complaints
& COMP_NOT_INET
))
787 trace_act("%s: not AF_INET",
789 complaints
|= COMP_NOT_INET
;
794 ifs
.int_addr
= S_ADDR(INFO_IFA(&info
));
796 if (ntohl(ifs
.int_addr
)>>24 == 0
797 || ntohl(ifs
.int_addr
)>>24 == 0xff) {
798 if (iff_up(ifs
.int_if_flags
)) {
799 if (!(prev_complaints
& COMP_BADADDR
))
800 msglog("%s has a bad address",
802 complaints
|= COMP_BADADDR
;
807 if (ifs
.int_if_flags
& IFF_LOOPBACK
) {
808 ifs
.int_state
|= IS_PASSIVE
| IS_NO_RIP
| IS_NO_RDISC
;
809 ifs
.int_dstaddr
= ifs
.int_addr
;
810 ifs
.int_mask
= HOST_MASK
;
811 ifs
.int_ripv1_mask
= HOST_MASK
;
812 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
813 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
814 if (!foundloopback
) {
816 loopaddr
= ifs
.int_addr
;
817 loop_rts
.rts_gate
= loopaddr
;
818 loop_rts
.rts_router
= loopaddr
;
821 } else if (ifs
.int_if_flags
& IFF_POINTOPOINT
) {
822 if (INFO_BRD(&info
) == 0
823 || INFO_BRD(&info
)->sa_family
!= AF_INET
) {
824 if (iff_up(ifs
.int_if_flags
)) {
825 if (!(prev_complaints
& COMP_NODST
))
826 msglog("%s has a bad"
827 " destination address",
829 complaints
|= COMP_NODST
;
833 ifs
.int_dstaddr
= S_ADDR(INFO_BRD(&info
));
834 if (ntohl(ifs
.int_dstaddr
)>>24 == 0
835 || ntohl(ifs
.int_dstaddr
)>>24 == 0xff) {
836 if (iff_up(ifs
.int_if_flags
)) {
837 if (!(prev_complaints
& COMP_NODST
))
838 msglog("%s has a bad"
839 " destination address",
841 complaints
|= COMP_NODST
;
845 ifs
.int_mask
= HOST_MASK
;
846 ifs
.int_ripv1_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
847 ifs
.int_std_mask
= std_mask(ifs
.int_dstaddr
);
848 ifs
.int_net
= ntohl(ifs
.int_dstaddr
);
851 if (INFO_MASK(&info
) == 0) {
852 if (iff_up(ifs
.int_if_flags
)) {
853 if (!(prev_complaints
& COMP_NOMASK
))
854 msglog("%s has no netmask",
856 complaints
|= COMP_NOMASK
;
860 ifs
.int_dstaddr
= ifs
.int_addr
;
861 ifs
.int_mask
= ntohl(S_ADDR(INFO_MASK(&info
)));
862 ifs
.int_ripv1_mask
= ifs
.int_mask
;
863 ifs
.int_std_mask
= std_mask(ifs
.int_addr
);
864 ifs
.int_net
= ntohl(ifs
.int_addr
) & ifs
.int_mask
;
865 if (ifs
.int_mask
!= ifs
.int_std_mask
)
866 ifs
.int_state
|= IS_SUBNET
;
868 if (ifs
.int_if_flags
& IFF_BROADCAST
) {
869 if (INFO_BRD(&info
) == 0) {
870 if (iff_up(ifs
.int_if_flags
)) {
871 if (!(prev_complaints
874 "no broadcast address",
876 complaints
|= COMP_NOBADR
;
880 ifs
.int_brdaddr
= S_ADDR(INFO_BRD(&info
));
883 ifs
.int_std_net
= ifs
.int_net
& ifs
.int_std_mask
;
884 ifs
.int_std_addr
= htonl(ifs
.int_std_net
);
886 /* Use a minimum metric of one. Treat the interface metric
887 * (default 0) as an increment to the hop count of one.
889 * The metric obtained from the routing socket dump of
890 * interface addresses is wrong. It is not set by the
891 * SIOCSIFMETRIC ioctl.
894 strncpy(ifr
.ifr_name
, ifs
.int_name
, sizeof(ifr
.ifr_name
));
895 if (ioctl(rt_sock
, SIOCGIFMETRIC
, &ifr
) < 0) {
896 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
899 ifs
.int_metric
= ifr
.ifr_metric
;
902 ifs
.int_metric
= ifam
->ifam_metric
;
904 if (ifs
.int_metric
> HOPCNT_INFINITY
) {
906 if (!(prev_complaints
& COMP_BAD_METRIC
)
907 && iff_up(ifs
.int_if_flags
)) {
908 complaints
|= COMP_BAD_METRIC
;
909 msglog("%s has a metric of %d",
910 ifs
.int_name
, ifs
.int_metric
);
914 /* See if this is a familiar interface.
915 * If so, stop worrying about it if it is the same.
916 * Start it over if it now is to somewhere else, as happens
917 * frequently with PPP and SLIP.
919 ifp
= ifwithname(ifs
.int_name
, ((ifs
.int_state
& IS_ALIAS
)
923 ifp
->int_state
|= IS_CHECKED
;
925 if (0 != ((ifp
->int_if_flags
^ ifs
.int_if_flags
)
930 || 0 != ((ifp
->int_state
^ ifs
.int_state
)
932 || ifp
->int_addr
!= ifs
.int_addr
933 || ifp
->int_brdaddr
!= ifs
.int_brdaddr
934 || ifp
->int_dstaddr
!= ifs
.int_dstaddr
935 || ifp
->int_mask
!= ifs
.int_mask
936 || ifp
->int_metric
!= ifs
.int_metric
) {
937 /* Forget old information about
938 * a changed interface.
940 trace_act("interface %s has changed",
948 /* The primary representative of an alias worries
949 * about how things are working.
951 if (ifp
->int_state
& IS_ALIAS
)
954 /* note interfaces that have been turned off
956 if (!iff_up(ifs
.int_if_flags
)) {
957 if (iff_up(ifp
->int_if_flags
)) {
958 msglog("interface %s to %s turned off",
960 naddr_ntoa(ifp
->int_dstaddr
));
962 ifp
->int_if_flags
&= ~IFF_UP
;
963 } else if (now
.tv_sec
>(ifp
->int_data
.ts
964 + CHECK_BAD_INTERVAL
)) {
965 trace_act("interface %s has been off"
966 " %ld seconds; forget it",
968 now
.tv_sec
-ifp
->int_data
.ts
);
973 /* or that were off and are now ok */
974 if (!iff_up(ifp
->int_if_flags
)) {
975 ifp
->int_if_flags
|= IFF_UP
;
979 /* If it has been long enough,
980 * see if the interface is broken.
982 if (now
.tv_sec
< ifp
->int_data
.ts
+CHECK_BAD_INTERVAL
)
985 in
= ifs
.int_data
.ipackets
- ifp
->int_data
.ipackets
;
986 ierr
= ifs
.int_data
.ierrors
- ifp
->int_data
.ierrors
;
987 out
= ifs
.int_data
.opackets
- ifp
->int_data
.opackets
;
988 oerr
= ifs
.int_data
.oerrors
- ifp
->int_data
.oerrors
;
990 /* Through at least IRIX 6.2, PPP and SLIP
991 * count packets dropped by the filters.
992 * But FDDI rings stuck non-operational count
993 * dropped packets as they wait for improvement.
995 if (!(ifp
->int_if_flags
& IFF_POINTOPOINT
))
996 oerr
+= (ifs
.int_data
.odrops
997 - ifp
->int_data
.odrops
);
999 /* If the interface just awoke, restart the counters.
1001 if (ifp
->int_data
.ts
== 0) {
1002 ifp
->int_data
= ifs
.int_data
;
1005 ifp
->int_data
= ifs
.int_data
;
1007 /* Withhold judgment when the short error
1008 * counters wrap or the interface is reset.
1010 if (ierr
< 0 || in
< 0 || oerr
< 0 || out
< 0) {
1011 LIM_SEC(ifinit_timer
,
1012 now
.tv_sec
+CHECK_BAD_INTERVAL
);
1016 /* Withhold judgement when there is no traffic
1018 if (in
== 0 && out
== 0 && ierr
== 0 && oerr
== 0)
1021 /* It is bad if input or output is not working.
1022 * Require presistent problems before marking it dead.
1024 if ((in
<= ierr
&& ierr
> 0)
1025 || (out
<= oerr
&& oerr
> 0)) {
1026 if (!(ifp
->int_state
& IS_SICK
)) {
1027 trace_act("interface %s to %s"
1028 " sick: in=%d ierr=%d"
1031 naddr_ntoa(ifp
->int_dstaddr
),
1032 in
, ierr
, out
, oerr
);
1036 if (!(ifp
->int_state
& IS_BROKE
)) {
1037 msglog("interface %s to %s broken:"
1038 " in=%d ierr=%d out=%d oerr=%d",
1040 naddr_ntoa(ifp
->int_dstaddr
),
1041 in
, ierr
, out
, oerr
);
1047 /* otherwise, it is active and healthy
1049 ifp
->int_act_time
= now
.tv_sec
;
1054 /* This is a new interface.
1055 * If it is dead, forget it.
1057 if (!iff_up(ifs
.int_if_flags
))
1060 /* If it duplicates an existing interface,
1061 * complain about it, mark the other one
1062 * duplicated, and forget this one.
1064 ifp
= check_dup(ifs
.int_addr
,ifs
.int_dstaddr
,ifs
.int_mask
,
1067 /* Ignore duplicates of itself, caused by having
1068 * IP aliases on the same network.
1070 if (!strcmp(ifp
->int_name
, ifs
.int_name
))
1073 if (!(prev_complaints
& COMP_DUP
)) {
1074 complaints
|= COMP_DUP
;
1075 msglog("%s (%s%s%s) is duplicated by"
1078 addrname(ifs
.int_addr
,ifs
.int_mask
,1),
1079 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1081 ((ifs
.int_if_flags
& IFF_POINTOPOINT
)
1082 ? naddr_ntoa(ifs
.int_dstaddr
) : ""),
1084 addrname(ifp
->int_addr
,ifp
->int_mask
,1),
1085 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1087 ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1088 ? naddr_ntoa(ifp
->int_dstaddr
) : ""));
1090 ifp
->int_state
|= IS_DUP
;
1094 if (0 == (ifs
.int_if_flags
& (IFF_POINTOPOINT
| IFF_BROADCAST
))
1095 && !(ifs
.int_state
& IS_PASSIVE
)) {
1096 trace_act("%s is neither broadcast, point-to-point,"
1099 if (!(ifs
.int_state
& IFF_MULTICAST
))
1100 ifs
.int_state
|= IS_NO_RDISC
;
1104 /* It is new and ok. Add it to the list of interfaces
1106 ifp
= (struct interface
*)rtmalloc(sizeof(*ifp
), "ifinit ifp");
1107 memcpy(ifp
, &ifs
, sizeof(*ifp
));
1110 trace_if("Add", ifp
);
1112 /* Notice likely bad netmask.
1114 if (!(prev_complaints
& COMP_NETMASK
)
1115 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
1116 && ifp
->int_addr
!= RIP_DEFAULT
) {
1117 for (ifp1
= ifnet
; NULL
!= ifp1
; ifp1
= ifp1
->int_next
) {
1118 if (ifp1
->int_mask
== ifp
->int_mask
)
1120 if (ifp1
->int_if_flags
& IFF_POINTOPOINT
)
1122 if (ifp1
->int_dstaddr
== RIP_DEFAULT
)
1124 /* ignore aliases on the right network */
1125 if (!strcmp(ifp
->int_name
, ifp1
->int_name
))
1127 if (on_net(ifp
->int_dstaddr
,
1128 ifp1
->int_net
, ifp1
->int_mask
)
1129 || on_net(ifp1
->int_dstaddr
,
1130 ifp
->int_net
, ifp
->int_mask
)) {
1131 msglog("possible netmask problem"
1132 " between %s:%s and %s:%s",
1134 addrname(htonl(ifp
->int_net
),
1137 addrname(htonl(ifp1
->int_net
),
1138 ifp1
->int_mask
, 1));
1139 complaints
|= COMP_NETMASK
;
1144 if (!(ifp
->int_state
& IS_ALIAS
)) {
1145 /* Count the # of directly connected networks.
1147 if (!(ifp
->int_if_flags
& IFF_LOOPBACK
))
1149 if (!IS_RIP_OFF(ifp
->int_state
))
1152 /* turn on router discovery and RIP If needed */
1158 /* If we are multi-homed and have at least two interfaces
1159 * listening to RIP, then output by default.
1161 if (!supplier_set
&& rip_interfaces
> 1)
1164 /* If we are multi-homed, optionally advertise a route to
1168 || (tot_interfaces
> 1
1170 && (ifp
= ifwithaddr(myaddr
, 0, 0)) != NULL
1171 && foundloopback
)) {
1172 advertise_mhome
= 1;
1173 rt
= rtget(myaddr
, HOST_MASK
);
1175 if (rt
->rt_ifp
!= ifp
1176 || rt
->rt_router
!= loopaddr
) {
1180 loop_rts
.rts_ifp
= ifp
;
1181 loop_rts
.rts_metric
= 0;
1182 loop_rts
.rts_time
= rt
->rt_time
;
1183 rtchange(rt
, rt
->rt_state
| RS_MHOME
,
1188 loop_rts
.rts_ifp
= ifp
;
1189 loop_rts
.rts_metric
= 0;
1190 rtadd(myaddr
, HOST_MASK
, RS_MHOME
, &loop_rts
);
1194 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp1
) {
1195 ifp1
= ifp
->int_next
; /* because we may delete it */
1197 /* Forget any interfaces that have disappeared.
1199 if (!(ifp
->int_state
& (IS_CHECKED
| IS_REMOTE
))) {
1200 trace_act("interface %s has disappeared",
1206 if ((ifp
->int_state
& IS_BROKE
)
1207 && !(ifp
->int_state
& IS_PASSIVE
))
1208 LIM_SEC(ifinit_timer
, now
.tv_sec
+CHECK_BAD_INTERVAL
);
1210 /* If we ever have a RIPv1 interface, assume we always will.
1211 * It might come back if it ever goes away.
1213 if (!(ifp
->int_state
& IS_NO_RIPV1_OUT
) && supplier
)
1215 if (!(ifp
->int_state
& IS_NO_RIPV1_IN
))
1219 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
1220 /* Ensure there is always a network route for interfaces,
1221 * after any dead interfaces have been deleted, which
1222 * might affect routes for point-to-point links.
1224 if (!addrouteforif(ifp
))
1227 /* Add routes to the local end of point-to-point interfaces
1230 if ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
1231 && !(ifp
->int_state
& IS_REMOTE
)
1233 /* Delete any routes to the network address through
1234 * foreign routers. Remove even static routes.
1236 del_static(ifp
->int_addr
, HOST_MASK
, 0, 0);
1237 rt
= rtget(ifp
->int_addr
, HOST_MASK
);
1238 if (rt
!= NULL
&& rt
->rt_router
!= loopaddr
) {
1243 if (!(rt
->rt_state
& RS_LOCAL
)
1244 || rt
->rt_metric
> ifp
->int_metric
) {
1249 loop_rts
.rts_ifp
= ifp1
;
1250 loop_rts
.rts_metric
= 0;
1251 loop_rts
.rts_time
= rt
->rt_time
;
1252 rtchange(rt
, ((rt
->rt_state
& ~RS_NET_SYN
)
1253 | (RS_IF
|RS_LOCAL
)),
1256 loop_rts
.rts_ifp
= ifp
;
1257 loop_rts
.rts_metric
= 0;
1258 rtadd(ifp
->int_addr
, HOST_MASK
,
1259 (RS_IF
| RS_LOCAL
), &loop_rts
);
1264 /* add the authority routes */
1265 for (intnetp
= intnets
; intnetp
!=NULL
; intnetp
= intnetp
->intnet_next
) {
1266 rt
= rtget(intnetp
->intnet_addr
, intnetp
->intnet_mask
);
1268 && !(rt
->rt_state
& RS_NO_NET_SYN
)
1269 && !(rt
->rt_state
& RS_NET_INT
)) {
1274 loop_rts
.rts_ifp
= 0;
1275 loop_rts
.rts_metric
= intnetp
->intnet_metric
-1;
1276 rtadd(intnetp
->intnet_addr
, intnetp
->intnet_mask
,
1277 RS_NET_SYN
| RS_NET_INT
, &loop_rts
);
1281 prev_complaints
= complaints
;
1286 check_net_syn(struct interface
*ifp
)
1288 struct rt_entry
*rt
;
1289 static struct rt_spare
new;
1292 /* Turn on the need to automatically synthesize a network route
1293 * for this interface only if we are running RIPv1 on some other
1294 * interface that is on a different class-A,B,or C network.
1296 if (have_ripv1_out
|| have_ripv1_in
) {
1297 ifp
->int_state
|= IS_NEED_NET_SYN
;
1298 rt
= rtget(ifp
->int_std_addr
, ifp
->int_std_mask
);
1300 && 0 == (rt
->rt_state
& RS_NO_NET_SYN
)
1301 && (!(rt
->rt_state
& RS_NET_SYN
)
1302 || rt
->rt_metric
> ifp
->int_metric
)) {
1308 new.rts_gate
= ifp
->int_addr
;
1309 new.rts_router
= ifp
->int_addr
;
1310 new.rts_metric
= ifp
->int_metric
;
1311 rtadd(ifp
->int_std_addr
, ifp
->int_std_mask
,
1316 ifp
->int_state
&= ~IS_NEED_NET_SYN
;
1318 rt
= rtget(ifp
->int_std_addr
,
1321 && (rt
->rt_state
& RS_NET_SYN
)
1322 && rt
->rt_ifp
== ifp
)
1328 /* Add route for interface if not currently installed.
1329 * Create route to other end if a point-to-point link,
1330 * otherwise a route to this (sub)network.
1332 int /* 0=bad interface */
1333 addrouteforif(struct interface
*ifp
)
1335 struct rt_entry
*rt
;
1336 static struct rt_spare
new;
1340 /* skip sick interfaces
1342 if (ifp
->int_state
& IS_BROKE
)
1345 /* If the interface on a subnet, then install a RIPv1 route to
1346 * the network as well (unless it is sick).
1348 if (ifp
->int_state
& IS_SUBNET
)
1351 dst
= (0 != (ifp
->int_if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))
1353 : htonl(ifp
->int_net
));
1356 new.rts_router
= ifp
->int_addr
;
1357 new.rts_gate
= ifp
->int_addr
;
1358 new.rts_metric
= ifp
->int_metric
;
1359 new.rts_time
= now
.tv_sec
;
1361 /* If we are going to send packets to the gateway,
1362 * it must be reachable using our physical interfaces
1364 if ((ifp
->int_state
& IS_REMOTE
)
1365 && !(ifp
->int_state
& IS_EXTERNAL
)
1366 && !check_remote(ifp
))
1369 /* We are finished if the correct main interface route exists.
1370 * The right route must be for the right interface, not synthesized
1371 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1373 del_static(dst
, ifp
->int_mask
, 0, 0);
1374 rt
= rtget(dst
, ifp
->int_mask
);
1376 if ((rt
->rt_ifp
!= ifp
1377 || rt
->rt_router
!= ifp
->int_addr
)
1378 && (!(ifp
->int_state
& IS_DUP
)
1380 || (rt
->rt_ifp
->int_state
& IS_BROKE
))) {
1384 rtchange(rt
, ((rt
->rt_state
| RS_IF
)
1385 & ~(RS_NET_SYN
| RS_LOCAL
)),
1390 if (ifp
->int_transitions
++ > 0)
1391 trace_act("re-install interface %s",
1394 rtadd(dst
, ifp
->int_mask
, RS_IF
, &new);