less(1): Regenerate defines.h and update Makefile
[dragonfly.git] / sbin / routed / if.c
blobbf7fd86876286a8b53ff054fef41655994d1a843
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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 $
33 #include "defs.h"
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
41 * by name or address.
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.
57 #define NHASH_LEN 97
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 */
73 int have_ripv1_in;
76 static struct interface**
77 nhash(char *p)
79 u_int i;
81 for (i = 0; *p != '\0'; p++) {
82 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
83 i ^= *p;
85 return &nhash_tbl[i % NHASH_LEN];
89 /* Link a new interface into the lists and hash tables.
91 void
92 if_link(struct interface *ifp)
94 struct interface **hifp;
96 ifp->int_prev = &ifnet;
97 ifp->int_next = ifnet;
98 if (ifnet != NULL)
99 ifnet->int_prev = &ifp->int_next;
100 ifnet = ifp;
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;
106 *hifp = ifp;
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;
113 *hifp = ifp;
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;
121 remote_if = ifp;
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;
133 *hifp = ifp;
137 /* Find the interface with an address
139 struct interface *
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)
150 continue;
151 if ((ifp->int_state & remote) != 0)
152 continue;
153 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
154 return ifp;
155 possible = ifp;
158 if (possible || !bcast)
159 return possible;
161 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
162 if (ifp->int_brdaddr != addr)
163 continue;
164 if ((ifp->int_state & remote) != 0)
165 continue;
166 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
167 return ifp;
168 possible = ifp;
171 return possible;
175 /* find the interface with a name
177 struct interface *
178 ifwithname(char *name, /* "ec0" or whatever */
179 naddr addr) /* 0 or network address */
181 struct interface *ifp;
183 for (;;) {
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)))
192 return ifp;
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())
199 return 0;
200 ifinit();
205 struct interface *
206 ifwithindex(u_short idx, int rescan_ok)
208 struct interface *ifp;
210 for (;;) {
211 for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next) {
212 if (ifp->int_index == idx)
213 return ifp;
216 /* If there is no known interface, maybe there is a
217 * new interface. So just once look for new interfaces.
219 if (!rescan_ok
220 || IF_RESCAN_DELAY())
221 return 0;
222 ifinit();
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.
231 struct interface *
232 iflookup(naddr addr)
234 struct interface *ifp, *maybe;
236 maybe = NULL;
237 for (;;) {
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)
242 return ifp;
244 } else {
245 /* finished with an exact match */
246 if (ifp->int_addr == addr)
247 return ifp;
249 /* Look for the longest approximate match.
251 if (on_net(addr, ifp->int_net, ifp->int_mask)
252 && (maybe == NULL
253 || ifp->int_mask > maybe->int_mask))
254 maybe = ifp;
258 if (maybe != NULL
259 || IF_RESCAN_DELAY())
260 return maybe;
262 /* If there is no known interface, maybe there is a
263 * new interface. So just once look for new interfaces.
265 ifinit();
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 */
278 return 0;
279 if (IN_CLASSA(addr))
280 return IN_CLASSA_NET;
281 if (IN_CLASSB(addr))
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.
291 naddr
292 ripv1_mask_net(naddr addr, /* in network byte order */
293 struct interface *ifp) /* as seen on this interface */
295 struct r1net *r1p;
296 naddr mask = 0;
298 if (addr == 0) /* default always has 0 mask */
299 return 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;
308 } else {
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
313 * match.
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 */
325 if (mask == 0) {
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.
334 if (mask == 0)
335 mask = std_mask(addr);
338 return mask;
342 naddr
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)
353 mask = HOST_MASK;
354 return mask;
358 /* See if a IP address looks reasonable as a destination
360 int /* 0=bad */
361 check_dst(naddr addr)
363 addr = ntohl(addr);
365 if (IN_CLASSA(addr)) {
366 if (addr == 0)
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.
379 struct interface *
380 check_dup(naddr addr, /* IP address, so network byte order */
381 naddr dstaddr, /* ditto */
382 naddr mask, /* mask, so host byte order */
383 int if_flags)
385 struct interface *ifp;
387 for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next) {
388 if (ifp->int_mask != mask)
389 continue;
391 if (!iff_up(ifp->int_if_flags))
392 continue;
394 /* The local address can only be shared with a point-to-point
395 * link.
397 if (ifp->int_addr == addr
398 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
399 return ifp;
401 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
402 return ifp;
404 return 0;
408 /* See that a remote gateway is reachable.
409 * Note that the answer can change as real interfaces come and go.
411 int /* 0=bad */
412 check_remote(struct interface *ifp)
414 struct rt_entry *rt;
416 /* do not worry about other kinds */
417 if (!(ifp->int_state & IS_REMOTE))
418 return 1;
420 rt = rtfind(ifp->int_addr);
421 if (rt != NULL
422 && rt->rt_ifp != 0
423 &&on_net(ifp->int_addr,
424 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
425 return 1;
427 /* the gateway cannot be reached directly from one of our
428 * interfaces
430 if (!(ifp->int_state & IS_BROKE)) {
431 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
432 naddr_ntoa(ifp->int_addr));
433 if_bad(ifp);
435 return 0;
439 /* Delete an interface.
441 static void
442 ifdel(struct interface *ifp)
444 struct ip_mreq m;
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) {
478 if (ifp1 != ifp
479 && !strcmp(ifp->int_name, ifp1->int_name))
480 ifdel(ifp1);
483 if ((ifp->int_if_flags & IFF_MULTICAST)
484 #ifdef MCAST_PPP_BUG
485 && !(ifp->int_if_flags & IFF_POINTOPOINT)
486 #endif
487 && rip_sock >= 0) {
488 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
489 m.imr_interface.s_addr = ((ifp->int_if_flags
490 & IFF_POINTOPOINT)
491 ? ifp->int_dstaddr
492 : ifp->int_addr);
493 if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
494 &m, sizeof(m)) < 0
495 && errno != EADDRNOTAVAIL
496 && !TRACEACTIONS)
497 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
498 if (rip_sock_mcast == ifp)
499 rip_sock_mcast = 0;
501 if (ifp->int_rip_sock >= 0) {
502 close(ifp->int_rip_sock);
503 ifp->int_rip_sock = -1;
504 fix_select();
507 tot_interfaces--;
508 if (!IS_RIP_OFF(ifp->int_state))
509 rip_interfaces--;
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);
518 if_bad_rdisc(ifp);
521 free(ifp);
525 /* Mark an interface ill.
527 void
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.
542 void
543 if_bad(struct interface *ifp)
545 struct interface *ifp1;
548 if (ifp->int_state & IS_BROKE)
549 return;
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) {
562 if (ifp1 != ifp
563 && !strcmp(ifp->int_name, ifp1->int_name))
564 if_bad(ifp1);
566 rhead->rnh_walktree(rhead, walk_bad, NULL);
567 if_bad_rdisc(ifp);
572 /* Mark an interface alive
574 int /* 1=it was dead */
575 if_ok(struct interface *ifp,
576 const char *type)
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",
584 type,
585 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
586 ifp->int_state &= ~IS_SICK;
588 return 0;
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) {
598 if (ifp1 != ifp
599 && !strcmp(ifp->int_name, ifp1->int_name))
600 if_ok(ifp1, type);
602 if_ok_rdisc(ifp);
605 if (ifp->int_state & IS_REMOTE) {
606 if (!addrouteforif(ifp))
607 return 0;
609 return 1;
613 /* disassemble routing message
615 void
616 rt_xaddrs(struct rt_addrinfo *info,
617 struct sockaddr *sa,
618 struct sockaddr *lim,
619 int addrs)
621 int i;
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)
630 continue;
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.
642 void
643 ifinit(void)
645 static char *sysctl_buf;
646 static size_t sysctl_buf_size = 0;
647 uint complaints = 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;
660 struct rt_entry *rt;
661 size_t needed;
662 int mib[6];
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;
668 #ifdef SIOCGIFMETRIC
669 struct ifreq ifr;
670 #endif
673 last_ifinit = now;
674 ifinit_timer.tv_sec = now.tv_sec + (supplier
675 ? CHECK_ACT_INTERVAL
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.
685 mib[0] = CTL_NET;
686 mib[1] = PF_ROUTE;
687 mib[2] = 0;
688 mib[3] = AF_INET;
689 mib[4] = NET_RT_IFLIST;
690 mib[5] = 0;
691 for (;;) {
692 if ((needed = sysctl_buf_size) != 0) {
693 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
694 break;
695 /* retry if the table grew */
696 if (errno != ENOMEM && errno != EFAULT)
697 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
698 free(sysctl_buf);
699 needed = 0;
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,
704 "ifinit sysctl");
707 ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
708 for (ifam = (struct ifa_msghdr *)sysctl_buf;
709 ifam < ifam_lim;
710 ifam = ifam2) {
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));
736 continue;
738 if (ifam->ifam_type != RTM_NEWADDR) {
739 logbad(1,"ifinit: out of sync");
740 continue;
742 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
743 (struct sockaddr *)ifam2,
744 ifam->ifam_addrs);
746 /* Prepare for the next address of this interface, which
747 * will be an alias.
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",
757 ifs.int_name);
758 complaints |= COMP_NOADDR;
760 continue;
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",
766 ifs.int_name);
767 complaints |= COMP_NOT_INET;
769 continue;
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",
779 ifs.int_name);
780 complaints |= COMP_BADADDR;
782 continue;
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) {
793 foundloopback = 1;
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",
806 ifs.int_name);
807 complaints |= COMP_NODST;
809 continue;
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",
818 ifs.int_name);
819 complaints |= COMP_NODST;
821 continue;
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);
828 } else {
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",
833 ifs.int_name);
834 complaints |= COMP_NOMASK;
836 continue;
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
850 & COMP_NOBADR))
851 msglog("%s has"
852 "no broadcast address",
853 ifs.int_name);
854 complaints |= COMP_NOBADR;
856 continue;
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.
871 #ifdef SIOCGIFMETRIC
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)");
875 ifs.int_metric = 0;
876 } else {
877 ifs.int_metric = ifr.ifr_metric;
879 #else
880 ifs.int_metric = ifam->ifam_metric;
881 #endif
882 if (ifs.int_metric > HOPCNT_INFINITY) {
883 ifs.int_metric = 0;
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)
898 ? ifs.int_addr
899 : 0));
900 if (ifp != NULL) {
901 ifp->int_state |= IS_CHECKED;
903 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
904 & (IFF_BROADCAST
905 | IFF_LOOPBACK
906 | IFF_POINTOPOINT
907 | IFF_MULTICAST))
908 || 0 != ((ifp->int_state ^ ifs.int_state)
909 & IS_ALIAS)
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",
919 ifp->int_name);
920 ifdel(ifp);
921 ifp = NULL;
925 if (ifp != NULL) {
926 /* The primary representative of an alias worries
927 * about how things are working.
929 if (ifp->int_state & IS_ALIAS)
930 continue;
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",
937 ifp->int_name,
938 naddr_ntoa(ifp->int_dstaddr));
939 if_bad(ifp);
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",
945 ifp->int_name,
946 now.tv_sec-ifp->int_data.ts);
947 ifdel(ifp);
949 continue;
951 /* or that were off and are now ok */
952 if (!iff_up(ifp->int_if_flags)) {
953 ifp->int_if_flags |= IFF_UP;
954 if_ok(ifp, "");
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)
961 continue;
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;
971 continue;
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);
981 continue;
984 /* Withhold judgement when there is no traffic
986 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
987 continue;
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"
997 " out=%d oerr=%d",
998 ifp->int_name,
999 naddr_ntoa(ifp->int_dstaddr),
1000 in, ierr, out, oerr);
1001 if_sick(ifp);
1002 continue;
1004 if (!(ifp->int_state & IS_BROKE)) {
1005 msglog("interface %s to %s broken:"
1006 " in=%d ierr=%d out=%d oerr=%d",
1007 ifp->int_name,
1008 naddr_ntoa(ifp->int_dstaddr),
1009 in, ierr, out, oerr);
1010 if_bad(ifp);
1012 continue;
1015 /* otherwise, it is active and healthy
1017 ifp->int_act_time = now.tv_sec;
1018 if_ok(ifp, "");
1019 continue;
1022 /* This is a new interface.
1023 * If it is dead, forget it.
1025 if (!iff_up(ifs.int_if_flags))
1026 continue;
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,
1033 ifs.int_if_flags);
1034 if (ifp != NULL) {
1035 /* Ignore duplicates of itself, caused by having
1036 * IP aliases on the same network.
1038 if (!strcmp(ifp->int_name, ifs.int_name))
1039 continue;
1041 if (!(prev_complaints & COMP_DUP)) {
1042 complaints |= COMP_DUP;
1043 msglog("%s (%s%s%s) is duplicated by"
1044 " %s (%s%s%s)",
1045 ifs.int_name,
1046 addrname(ifs.int_addr,ifs.int_mask,1),
1047 ((ifs.int_if_flags & IFF_POINTOPOINT)
1048 ? "-->" : ""),
1049 ((ifs.int_if_flags & IFF_POINTOPOINT)
1050 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1051 ifp->int_name,
1052 addrname(ifp->int_addr,ifp->int_mask,1),
1053 ((ifp->int_if_flags & IFF_POINTOPOINT)
1054 ? "-->" : ""),
1055 ((ifp->int_if_flags & IFF_POINTOPOINT)
1056 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1058 ifp->int_state |= IS_DUP;
1059 continue;
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,"
1065 " nor loopback",
1066 ifs.int_name);
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));
1076 get_parms(ifp);
1077 if_link(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)
1087 continue;
1088 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1089 continue;
1090 if (ifp1->int_dstaddr == RIP_DEFAULT)
1091 continue;
1092 /* ignore aliases on the right network */
1093 if (!strcmp(ifp->int_name, ifp1->int_name))
1094 continue;
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",
1101 ifp->int_name,
1102 addrname(htonl(ifp->int_net),
1103 ifp->int_mask, 1),
1104 ifp1->int_name,
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))
1116 tot_interfaces++;
1117 if (!IS_RIP_OFF(ifp->int_state))
1118 rip_interfaces++;
1120 /* turn on router discovery and RIP If needed */
1121 if_ok_rdisc(ifp);
1122 rip_on(ifp);
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)
1130 set_supplier();
1132 /* If we are multi-homed, optionally advertise a route to
1133 * our main address.
1135 if (advertise_mhome
1136 || (tot_interfaces > 1
1137 && mhome
1138 && (ifp = ifwithaddr(myaddr, 0, 0)) != NULL
1139 && foundloopback)) {
1140 advertise_mhome = 1;
1141 rt = rtget(myaddr, HOST_MASK);
1142 if (rt != NULL) {
1143 if (rt->rt_ifp != ifp
1144 || rt->rt_router != loopaddr) {
1145 rtdelete(rt);
1146 rt = NULL;
1147 } else {
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,
1152 &loop_rts, 0);
1155 if (rt == NULL) {
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",
1169 ifp->int_name);
1170 ifdel(ifp);
1171 continue;
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)
1182 have_ripv1_out = 1;
1183 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1184 have_ripv1_in = 1;
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))
1193 continue;
1195 /* Add routes to the local end of point-to-point interfaces
1196 * using loopback.
1198 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1199 && !(ifp->int_state & IS_REMOTE)
1200 && foundloopback) {
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) {
1207 rtdelete(rt);
1208 rt = NULL;
1210 if (rt != NULL) {
1211 if (!(rt->rt_state & RS_LOCAL)
1212 || rt->rt_metric > ifp->int_metric) {
1213 ifp1 = ifp;
1214 } else {
1215 ifp1 = rt->rt_ifp;
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)),
1222 &loop_rts, 0);
1223 } else {
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);
1235 if (rt != NULL
1236 && !(rt->rt_state & RS_NO_NET_SYN)
1237 && !(rt->rt_state & RS_NET_INT)) {
1238 rtdelete(rt);
1239 rt = NULL;
1241 if (rt == NULL) {
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;
1253 static void
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);
1267 if (rt != NULL
1268 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1269 && (!(rt->rt_state & RS_NET_SYN)
1270 || rt->rt_metric > ifp->int_metric)) {
1271 rtdelete(rt);
1272 rt = NULL;
1274 if (rt == NULL) {
1275 new.rts_ifp = ifp;
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,
1280 RS_NET_SYN, &new);
1283 } else {
1284 ifp->int_state &= ~IS_NEED_NET_SYN;
1286 rt = rtget(ifp->int_std_addr,
1287 ifp->int_std_mask);
1288 if (rt != NULL
1289 && (rt->rt_state & RS_NET_SYN)
1290 && rt->rt_ifp == ifp)
1291 rtbad_sub(rt);
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;
1305 naddr dst;
1308 /* skip sick interfaces
1310 if (ifp->int_state & IS_BROKE)
1311 return 0;
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)
1317 check_net_syn(ifp);
1319 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1320 ? ifp->int_dstaddr
1321 : htonl(ifp->int_net));
1323 new.rts_ifp = ifp;
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))
1335 return 0;
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);
1343 if (rt != NULL) {
1344 if ((rt->rt_ifp != ifp
1345 || rt->rt_router != ifp->int_addr)
1346 && (!(ifp->int_state & IS_DUP)
1347 || rt->rt_ifp == 0
1348 || (rt->rt_ifp->int_state & IS_BROKE))) {
1349 rtdelete(rt);
1350 rt = NULL;
1351 } else {
1352 rtchange(rt, ((rt->rt_state | RS_IF)
1353 & ~(RS_NET_SYN | RS_LOCAL)),
1354 &new, 0);
1357 if (rt == NULL) {
1358 if (ifp->int_transitions++ > 0)
1359 trace_act("re-install interface %s",
1360 ifp->int_name);
1362 rtadd(dst, ifp->int_mask, RS_IF, &new);
1365 return 1;