lib:socket: fix CID 1350010 - integer OVERFLOW_BEFORE_WIDEN
[Samba.git] / lib / socket / interfaces.c
blobcf094f0b4c56607ce6ab2f6a7fc8505828409a5c
1 /*
2 Unix SMB/CIFS implementation.
3 return a list of network interfaces
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Jeremy Allison 2007
6 Copyright (C) Jelmer Vernooij 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/network.h"
25 #include "interfaces.h"
26 #include "lib/util/tsort.h"
27 #include "librpc/gen_ndr/ioctl.h"
29 #ifdef HAVE_ETHTOOL
30 #include "linux/sockios.h"
31 #include "linux/ethtool.h"
32 #endif
34 /****************************************************************************
35 Create a struct sockaddr_storage with the netmask bits set to 1.
36 ****************************************************************************/
38 bool make_netmask(struct sockaddr_storage *pss_out,
39 const struct sockaddr_storage *pss_in,
40 unsigned long masklen)
42 *pss_out = *pss_in;
43 /* Now apply masklen bits of mask. */
44 #if defined(HAVE_IPV6)
45 if (pss_in->ss_family == AF_INET6) {
46 char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
47 unsigned int i;
49 if (masklen > 128) {
50 return false;
52 for (i = 0; masklen >= 8; masklen -= 8, i++) {
53 *p++ = 0xff;
55 /* Deal with the partial byte. */
56 *p++ &= (0xff & ~(0xff>>masklen));
57 i++;
58 for (;i < sizeof(struct in6_addr); i++) {
59 *p++ = '\0';
61 return true;
63 #endif
64 if (pss_in->ss_family == AF_INET) {
65 if (masklen > 32) {
66 return false;
68 ((struct sockaddr_in *)pss_out)->sin_addr.s_addr =
69 htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL));
70 return true;
72 return false;
75 /****************************************************************************
76 Create a struct sockaddr_storage set to the broadcast or network adress from
77 an incoming sockaddr_storage.
78 ****************************************************************************/
80 static void make_bcast_or_net(struct sockaddr_storage *pss_out,
81 const struct sockaddr_storage *pss_in,
82 const struct sockaddr_storage *nmask,
83 bool make_bcast_p)
85 unsigned int i = 0, len = 0;
86 const char *pmask = NULL;
87 char *p = NULL;
88 *pss_out = *pss_in;
90 /* Set all zero netmask bits to 1. */
91 #if defined(HAVE_IPV6)
92 if (pss_in->ss_family == AF_INET6) {
93 p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
94 pmask = (const char *)&((const struct sockaddr_in6 *)nmask)->sin6_addr;
95 len = 16;
97 #endif
98 if (pss_in->ss_family == AF_INET) {
99 p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr;
100 pmask = (const char *)&((const struct sockaddr_in *)nmask)->sin_addr;
101 len = 4;
104 for (i = 0; i < len; i++, p++, pmask++) {
105 if (make_bcast_p) {
106 *p = (*p & *pmask) | (*pmask ^ 0xff);
107 } else {
108 /* make_net */
109 *p = (*p & *pmask);
114 void make_bcast(struct sockaddr_storage *pss_out,
115 const struct sockaddr_storage *pss_in,
116 const struct sockaddr_storage *nmask)
118 make_bcast_or_net(pss_out, pss_in, nmask, true);
121 void make_net(struct sockaddr_storage *pss_out,
122 const struct sockaddr_storage *pss_in,
123 const struct sockaddr_storage *nmask)
125 make_bcast_or_net(pss_out, pss_in, nmask, false);
128 #ifdef HAVE_ETHTOOL
129 static void query_iface_speed_from_name(const char *name, uint64_t *speed)
131 int ret = 0;
132 struct ethtool_cmd ecmd;
133 struct ethtool_value edata;
134 struct ifreq ifr;
135 int fd;
137 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
138 if (fd == -1) {
139 DBG_ERR("Failed to open socket.");
140 return;
143 strncpy(ifr.ifr_name, name, IF_NAMESIZE);
145 ifr.ifr_data = (void *)&edata;
146 edata.cmd = ETHTOOL_GLINK;
147 ret = ioctl(fd, SIOCETHTOOL, &ifr);
148 if (ret == -1) {
149 goto done;
151 if (edata.data == 0) {
152 /* no link detected */
153 *speed = 0;
154 goto done;
157 ifr.ifr_data = (void *)&ecmd;
158 ecmd.cmd = ETHTOOL_GSET;
159 ret = ioctl(fd, SIOCETHTOOL, &ifr);
160 if (ret == -1) {
161 goto done;
163 *speed = ((uint64_t)ethtool_cmd_speed(&ecmd)) * 1000 * 1000;
165 done:
166 (void)close(fd);
168 #endif
170 /****************************************************************************
171 Try the "standard" getifaddrs/freeifaddrs interfaces.
172 Also gets IPv6 interfaces.
173 ****************************************************************************/
175 /****************************************************************************
176 Get the netmask address for a local interface.
177 ****************************************************************************/
179 static int _get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
181 struct iface_struct *ifaces;
182 struct ifaddrs *iflist = NULL;
183 struct ifaddrs *ifptr = NULL;
184 int count;
185 int total = 0;
186 size_t copy_size;
187 uint64_t if_speed = 1000 * 1000 * 1000; /* 1GBit */
189 if (getifaddrs(&iflist) < 0) {
190 return -1;
193 count = 0;
194 for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
195 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
196 continue;
198 if (!(ifptr->ifa_flags & IFF_UP)) {
199 continue;
201 count += 1;
204 ifaces = talloc_array(mem_ctx, struct iface_struct, count);
205 if (ifaces == NULL) {
206 errno = ENOMEM;
207 return -1;
210 /* Loop through interfaces, looking for given IP address */
211 for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
213 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
214 continue;
217 /* Check the interface is up. */
218 if (!(ifptr->ifa_flags & IFF_UP)) {
219 continue;
222 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
224 copy_size = sizeof(struct sockaddr_in);
226 ifaces[total].flags = ifptr->ifa_flags;
228 #if defined(HAVE_IPV6)
229 if (ifptr->ifa_addr->sa_family == AF_INET6) {
230 copy_size = sizeof(struct sockaddr_in6);
232 #endif
234 memcpy(&ifaces[total].ip, ifptr->ifa_addr, copy_size);
235 memcpy(&ifaces[total].netmask, ifptr->ifa_netmask, copy_size);
237 /* calculate broadcast address */
238 #if defined(HAVE_IPV6)
239 if (ifptr->ifa_addr->sa_family == AF_INET6) {
240 struct sockaddr_in6 *sin6 =
241 (struct sockaddr_in6 *)ifptr->ifa_addr;
242 struct in6_addr *in6 =
243 (struct in6_addr *)&sin6->sin6_addr;
245 if (IN6_IS_ADDR_LINKLOCAL(in6) || IN6_IS_ADDR_V4COMPAT(in6)) {
246 continue;
248 /* IPv6 does not have broadcast it uses multicast. */
249 memset(&ifaces[total].bcast, '\0', copy_size);
250 } else
251 #endif
252 if (ifaces[total].flags & (IFF_BROADCAST|IFF_LOOPBACK)) {
253 make_bcast(&ifaces[total].bcast,
254 &ifaces[total].ip,
255 &ifaces[total].netmask);
256 } else if ((ifaces[total].flags & IFF_POINTOPOINT) &&
257 ifptr->ifa_dstaddr ) {
258 memcpy(&ifaces[total].bcast,
259 ifptr->ifa_dstaddr,
260 copy_size);
261 } else {
262 continue;
265 ifaces[total].if_index = if_nametoindex(ifptr->ifa_name);
266 if (ifaces[total].if_index == 0) {
267 DBG_ERR("Failed to retrieve interface index for '%s': "
268 "%s\n", ifptr->ifa_name, strerror(errno));
271 #ifdef HAVE_ETHTOOL
272 query_iface_speed_from_name(ifptr->ifa_name, &if_speed);
273 #endif
274 ifaces[total].linkspeed = if_speed;
275 ifaces[total].capability = FSCTL_NET_IFACE_NONE_CAPABLE;
277 if (strlcpy(ifaces[total].name, ifptr->ifa_name,
278 sizeof(ifaces[total].name)) >=
279 sizeof(ifaces[total].name)) {
280 /* Truncation ! Ignore. */
281 continue;
283 total++;
286 freeifaddrs(iflist);
288 *pifaces = ifaces;
289 return total;
292 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
294 int r;
296 #if defined(HAVE_IPV6)
298 * If we have IPv6 - sort these interfaces lower
299 * than any IPv4 ones.
301 if (i1->ip.ss_family == AF_INET6 &&
302 i2->ip.ss_family == AF_INET) {
303 return -1;
304 } else if (i1->ip.ss_family == AF_INET &&
305 i2->ip.ss_family == AF_INET6) {
306 return 1;
309 if (i1->ip.ss_family == AF_INET6) {
310 struct sockaddr_in6 *s1 = (struct sockaddr_in6 *)&i1->ip;
311 struct sockaddr_in6 *s2 = (struct sockaddr_in6 *)&i2->ip;
313 r = memcmp(&s1->sin6_addr,
314 &s2->sin6_addr,
315 sizeof(struct in6_addr));
316 if (r) {
317 return r;
320 s1 = (struct sockaddr_in6 *)&i1->netmask;
321 s2 = (struct sockaddr_in6 *)&i2->netmask;
323 r = memcmp(&s1->sin6_addr,
324 &s2->sin6_addr,
325 sizeof(struct in6_addr));
326 if (r) {
327 return r;
330 #endif
332 /* AIX uses __ss_family instead of ss_family inside of
333 sockaddr_storage. Instead of trying to figure out which field to
334 use, we can just cast it to a sockaddr.
337 if (((struct sockaddr *)&i1->ip)->sa_family == AF_INET) {
338 struct sockaddr_in *s1 = (struct sockaddr_in *)&i1->ip;
339 struct sockaddr_in *s2 = (struct sockaddr_in *)&i2->ip;
341 r = ntohl(s1->sin_addr.s_addr) -
342 ntohl(s2->sin_addr.s_addr);
343 if (r) {
344 return r;
347 s1 = (struct sockaddr_in *)&i1->netmask;
348 s2 = (struct sockaddr_in *)&i2->netmask;
350 return ntohl(s1->sin_addr.s_addr) -
351 ntohl(s2->sin_addr.s_addr);
353 return 0;
356 /* this wrapper is used to remove duplicates from the interface list generated
357 above */
358 int get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
360 struct iface_struct *ifaces;
361 int total, i, j;
363 total = _get_interfaces(mem_ctx, &ifaces);
364 if (total <= 0) return total;
366 /* now we need to remove duplicates */
367 TYPESAFE_QSORT(ifaces, total, iface_comp);
369 for (i=1;i<total;) {
370 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
371 for (j=i-1;j<total-1;j++) {
372 ifaces[j] = ifaces[j+1];
374 total--;
375 } else {
376 i++;
380 *pifaces = ifaces;
381 return total;