No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / mrouted / config.c
blob01d73b55e70e96427a8d4d58e9fefdbaa5be1ffa
1 /* $NetBSD: config.c,v 1.12 2003/05/16 18:10:38 itojun Exp $ */
3 /*
4 * The mrouted program is covered by the license in the accompanying file
5 * named "LICENSE". Use of the mrouted program represents acceptance of
6 * the terms and conditions listed in that file.
8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 * Leland Stanford Junior University.
12 #include "defs.h"
13 #include <net/if.h>
14 #include <ifaddrs.h>
18 * Query the kernel to find network interfaces that are multicast-capable
19 * and install them in the uvifs array.
21 void
22 config_vifs_from_kernel(void)
24 struct ifaddrs *ifa, *ifap;
25 struct uvif *v;
26 vifi_t vifi;
27 u_int32_t addr, mask, subnet;
28 short flags;
30 if (getifaddrs(&ifap) < 0)
31 logit(LOG_ERR, errno, "getifaddrs");
33 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
35 * Ignore any interface for an address family other than IP.
37 if (ifa->ifa_addr->sa_family != AF_INET)
38 continue;
40 addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
43 * Ignore loopback interfaces and interfaces that do not support
44 * multicast.
46 flags = ifa->ifa_flags;
47 if ((flags & (IFF_LOOPBACK|IFF_MULTICAST)) != IFF_MULTICAST)
48 continue;
51 * Ignore any interface whose address and mask do not define a
52 * valid subnet number, or whose address is of the form {subnet,0}
53 * or {subnet,-1}.
55 mask = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr;
56 subnet = addr & mask;
57 if (!inet_valid_subnet(subnet, mask) ||
58 addr == subnet ||
59 addr == (subnet | ~mask)) {
60 logit(LOG_WARNING, 0,
61 "ignoring %s, has invalid address (%s) and/or mask (%s)",
62 ifa->ifa_name, inet_fmt(addr),
63 inet_fmt(mask));
64 continue;
68 * Ignore any interface that is connected to the same subnet as
69 * one already installed in the uvifs array.
71 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
72 if ((addr & v->uv_subnetmask) == v->uv_subnet ||
73 (v->uv_subnet & mask) == subnet) {
74 logit(LOG_WARNING, 0, "ignoring %s, same subnet as %s",
75 ifa->ifa_name, v->uv_name);
76 break;
79 if (vifi != numvifs)
80 continue;
83 * If there is room in the uvifs array, install this interface.
85 if (numvifs == MAXVIFS) {
86 logit(LOG_WARNING, 0, "too many vifs, ignoring %s", ifa->ifa_name);
87 continue;
89 v = &uvifs[numvifs];
90 v->uv_flags = 0;
91 v->uv_metric = DEFAULT_METRIC;
92 v->uv_rate_limit = DEFAULT_PHY_RATE_LIMIT;
93 v->uv_threshold = DEFAULT_THRESHOLD;
94 v->uv_lcl_addr = addr;
95 v->uv_rmt_addr = 0;
96 v->uv_subnet = subnet;
97 v->uv_subnetmask = mask;
98 v->uv_subnetbcast = subnet | ~mask;
99 strlcpy(v->uv_name, ifa->ifa_name, sizeof(v->uv_name));
100 v->uv_groups = NULL;
101 v->uv_neighbors = NULL;
102 v->uv_acl = NULL;
103 v->uv_addrs = NULL;
105 logit(LOG_INFO,0,"installing %s (%s on subnet %s) as vif #%u - rate=%d",
106 v->uv_name, inet_fmt(addr),
107 inet_fmts(subnet, mask),
108 numvifs, v->uv_rate_limit);
110 ++numvifs;
113 * If the interface is not yet up, set the vifs_down flag to
114 * remind us to check again later.
116 if (!(flags & IFF_UP)) {
117 v->uv_flags |= VIFF_DOWN;
118 vifs_down = TRUE;
122 freeifaddrs(ifap);