2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE". Use of the mrouted program represents acceptance of
4 * the terms and conditions listed in that file.
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
10 * config.c,v 3.8.4.10 1998/01/06 01:57:41 fenner Exp
14 static const char rcsid[] =
24 * Query the kernel to find network interfaces that are multicast-capable
25 * and install them in the uvifs array.
28 config_vifs_from_kernel()
30 struct ifreq *ifrp, *ifend;
31 register struct uvif *v;
34 u_int32 addr, mask, subnet;
38 ifc.ifc_len = num_ifreq * sizeof(struct ifreq);
39 ifc.ifc_buf = malloc(ifc.ifc_len);
41 if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0)
42 log(LOG_ERR, errno, "ioctl SIOCGIFCONF");
45 * If the buffer was large enough to hold all the addresses
46 * then break out, otherwise increase the buffer size and
49 * The only way to know that we definitely had enough space
50 * is to know that there was enough space for at least one
51 * more struct ifreq. ???
53 if ((num_ifreq * sizeof(struct ifreq)) >=
54 ifc.ifc_len + sizeof(struct ifreq))
58 ifc.ifc_len = num_ifreq * sizeof(struct ifreq);
59 ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
61 if (ifc.ifc_buf == NULL)
62 log(LOG_ERR, 0, "config_vifs_from_kernel: ran out of memory");
64 ifrp = (struct ifreq *)ifc.ifc_buf;
65 ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len);
67 * Loop through all of the interfaces.
69 for (; ifrp < ifend; ifrp = (struct ifreq *)((char *)ifrp + n)) {
72 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
73 if (n < sizeof(*ifrp))
79 * Ignore any interface for an address family other than IP.
81 if (ifrp->ifr_addr.sa_family != AF_INET)
84 addr = ((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr;
87 * Need a template to preserve address info that is
88 * used below to locate the next entry. (Otherwise,
89 * SIOCGIFFLAGS stomps over it because the requests
90 * are returned in a union.)
92 bcopy(ifrp->ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
95 * Ignore loopback interfaces and interfaces that do not support
98 if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ifr) < 0)
99 log(LOG_ERR, errno, "ioctl SIOCGIFFLAGS for %s", ifr.ifr_name);
100 flags = ifr.ifr_flags;
101 if ((flags & (IFF_LOOPBACK|IFF_MULTICAST)) != IFF_MULTICAST) continue;
104 * Ignore any interface whose address and mask do not define a
105 * valid subnet number, or whose address is of the form {subnet,0}
108 if (ioctl(udp_socket, SIOCGIFNETMASK, (char *)&ifr) < 0)
109 log(LOG_ERR, errno, "ioctl SIOCGIFNETMASK for %s", ifr.ifr_name);
110 mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
111 subnet = addr & mask;
112 if (!inet_valid_subnet(subnet, mask) ||
114 addr == (subnet | ~mask)) {
116 "ignoring %s, has invalid address (%s) and/or mask (%s)",
117 ifr.ifr_name, inet_fmt(addr, s1), inet_fmt(mask, s2));
122 * Ignore any interface that is connected to the same subnet as
123 * one already installed in the uvifs array.
125 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
126 if (strcmp(v->uv_name, ifr.ifr_name) == 0) {
127 log(LOG_DEBUG, 0, "skipping %s (%s on subnet %s) (alias for vif#%u?)",
128 v->uv_name, inet_fmt(addr, s1),
129 inet_fmts(subnet, mask, s2), vifi);
132 if ((addr & v->uv_subnetmask) == v->uv_subnet ||
133 (v->uv_subnet & mask) == subnet) {
134 log(LOG_WARNING, 0, "ignoring %s, same subnet as %s",
135 ifr.ifr_name, v->uv_name);
139 if (vifi != numvifs) continue;
142 * If there is room in the uvifs array, install this interface.
144 if (numvifs == MAXVIFS) {
145 log(LOG_WARNING, 0, "too many vifs, ignoring %s", ifr.ifr_name);
150 v->uv_lcl_addr = addr;
151 v->uv_subnet = subnet;
152 v->uv_subnetmask = mask;
153 v->uv_subnetbcast = subnet | ~mask;
154 strncpy(v->uv_name, ifr.ifr_name, IFNAMSIZ);
155 v->uv_name[IFNAMSIZ-1] = '\0';
157 if (flags & IFF_POINTOPOINT)
158 v->uv_flags |= VIFF_REXMIT_PRUNES;
160 log(LOG_INFO,0,"installing %s (%s on subnet %s) as vif #%u - rate=%d",
161 v->uv_name, inet_fmt(addr, s1), inet_fmts(subnet, mask, s2),
162 numvifs, v->uv_rate_limit);
167 * If the interface is not yet up, set the vifs_down flag to
168 * remind us to check again later.
170 if (!(flags & IFF_UP)) {
171 v->uv_flags |= VIFF_DOWN;