2 Unix SMB/CIFS implementation.
4 multiple interface handling
6 Copyright (C) Andrew Tridgell 1992-2005
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 "system/network.h"
24 #include "param/param.h"
25 #include "lib/socket/netif.h"
26 #include "../lib/util/util_net.h"
27 #include "../lib/util/dlinklist.h"
28 #include "lib/util/smb_strtox.h"
30 /* used for network interfaces */
32 struct interface *next, *prev;
35 struct sockaddr_storage ip;
36 struct sockaddr_storage netmask;
37 struct sockaddr_storage bcast;
43 #define ALLONES ((uint32_t)0xFFFFFFFF)
45 address construction based on a patch from fred@datalync.com
47 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
48 #define MKNETADDR(_IP, _NM) (_IP & _NM)
50 /****************************************************************************
51 Try and find an interface that matches an ip. If we cannot, return NULL
52 **************************************************************************/
53 static struct interface *iface_list_find(struct interface *interfaces,
54 const struct sockaddr *ip,
59 if (is_address_any(ip)) {
63 for (i=interfaces;i;i=i->next) {
65 if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) {
68 } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) {
76 /****************************************************************************
77 add an interface to the linked list of interfaces
78 ****************************************************************************/
79 static void add_interface(TALLOC_CTX *mem_ctx, const struct iface_struct *ifs, struct interface **interfaces)
81 char addr[INET6_ADDRSTRLEN];
82 struct interface *iface;
84 if (iface_list_find(*interfaces, (const struct sockaddr *)&ifs->ip, false)) {
85 DEBUG(3,("add_interface: not adding duplicate interface %s\n",
86 print_sockaddr(addr, sizeof(addr), &ifs->ip) ));
90 if (ifs->ip.ss_family == AF_INET &&
91 !(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) {
92 DEBUG(3,("not adding non-broadcast interface %s\n",
97 if (*interfaces != NULL) {
98 mem_ctx = *interfaces;
101 iface = talloc_zero(mem_ctx, struct interface);
106 iface->name = talloc_strdup(iface, ifs->name);
111 iface->flags = ifs->flags;
113 iface->netmask = ifs->netmask;
114 iface->bcast = ifs->bcast;
116 /* keep string versions too, to avoid people tripping over the implied
117 static in inet_ntoa() */
118 print_sockaddr(addr, sizeof(addr), &iface->ip);
119 DEBUG(4,("added interface %s ip=%s ",
121 iface->ip_s = talloc_strdup(iface, addr);
123 print_sockaddr(addr, sizeof(addr),
125 DEBUG(4,("bcast=%s ", addr));
126 iface->bcast_s = talloc_strdup(iface, addr);
128 print_sockaddr(addr, sizeof(addr),
130 DEBUG(4,("netmask=%s\n", addr));
131 iface->nmask_s = talloc_strdup(iface, addr);
134 this needs to be a ADD_END, as some tests (such as the
135 spoolss notify test) depend on the interfaces ordering
137 DLIST_ADD_END(*interfaces, iface);
141 interpret a single element from a interfaces= config line
143 This handles the following different forms:
145 1) wildcard interface name
151 static void interpret_interface(TALLOC_CTX *mem_ctx,
153 struct iface_struct *probed_ifaces,
155 struct interface **local_interfaces)
157 struct sockaddr_storage ss;
158 struct sockaddr_storage ss_mask;
159 struct sockaddr_storage ss_net;
160 struct sockaddr_storage ss_bcast;
161 struct iface_struct ifs;
165 bool goodaddr = false;
167 /* first check if it is an interface name */
168 for (i=0;i<total_probed;i++) {
169 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
170 add_interface(mem_ctx, &probed_ifaces[i],
179 p = strchr_m(token, ';');
182 * skip smbd-specific extra data:
183 * link speed, capabilities, and interface index
188 /* maybe it is a DNS name */
189 p = strchr_m(token,'/');
191 if (!interpret_string_addr(&ss, token, 0)) {
192 DEBUG(2, ("interpret_interface: Can't find address "
197 for (i=0;i<total_probed;i++) {
198 if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) {
199 add_interface(mem_ctx, &probed_ifaces[i],
204 DEBUG(2,("interpret_interface: "
205 "can't determine interface for %s\n",
210 /* parse it into an IP address/netmasklength pair */
212 goodaddr = interpret_string_addr(&ss, token, 0);
216 DEBUG(2,("interpret_interface: "
217 "can't determine interface for %s\n",
223 goodaddr = interpret_string_addr(&ss_mask, p, 0);
225 DEBUG(2,("interpret_interface: "
226 "can't determine netmask from %s\n",
233 unsigned long val = smb_strtoul(p,
237 SMB_STR_FULL_STR_CONV);
239 DEBUG(2,("interpret_interface: "
240 "can't determine netmask value from %s\n",
244 if (!make_netmask(&ss_mask, &ss, val)) {
245 DEBUG(2,("interpret_interface: "
246 "can't apply netmask value %lu from %s\n",
253 make_bcast(&ss_bcast, &ss, &ss_mask);
254 make_net(&ss_net, &ss, &ss_mask);
256 /* Maybe the first component was a broadcast address. */
257 if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
258 sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
259 for (i=0;i<total_probed;i++) {
260 if (same_net((struct sockaddr *)&ss,
261 (struct sockaddr *)&probed_ifaces[i].ip,
262 (struct sockaddr *)&ss_mask)) {
263 /* Temporarily replace netmask on
264 * the detected interface - user knows
266 struct sockaddr_storage saved_mask =
267 probed_ifaces[i].netmask;
268 probed_ifaces[i].netmask = ss_mask;
269 DEBUG(2,("interpret_interface: "
270 "using netmask value %s from "
271 "config file on interface %s\n",
273 probed_ifaces[i].name));
274 add_interface(mem_ctx, &probed_ifaces[i],
276 probed_ifaces[i].netmask = saved_mask;
280 DEBUG(2,("interpret_interface: Can't determine ip for "
281 "broadcast address %s\n",
286 /* Just fake up the interface definition. User knows best. */
288 DEBUG(2,("interpret_interface: Adding interface %s\n",
292 (void)strlcpy(ifs.name, token, sizeof(ifs.name));
293 ifs.flags = IFF_BROADCAST;
295 ifs.netmask = ss_mask;
296 ifs.bcast = ss_bcast;
297 add_interface(mem_ctx, &ifs, local_interfaces);
302 load the list of network interfaces
304 void load_interface_list(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct interface **local_interfaces)
306 const char **ptr = lpcfg_interfaces(lp_ctx);
308 struct iface_struct *ifaces = NULL;
311 *local_interfaces = NULL;
313 /* probe the kernel for interfaces */
314 total_probed = get_interfaces(mem_ctx, &ifaces);
316 /* if we don't have a interfaces line then use all interfaces
318 if (!ptr || !*ptr || !**ptr) {
319 if (total_probed <= 0) {
320 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
322 for (i=0;i<total_probed;i++) {
323 if (!is_loopback_addr((struct sockaddr *)&ifaces[i].ip)) {
324 add_interface(mem_ctx, &ifaces[i], local_interfaces);
329 while (ptr && *ptr) {
330 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
334 if (!*local_interfaces) {
335 DEBUG(0,("WARNING: no network interfaces found\n"));
341 how many interfaces do we have
343 int iface_list_count(struct interface *ifaces)
348 for (i=ifaces;i;i=i->next)
354 return IP of the Nth interface
356 const char *iface_list_n_ip(struct interface *ifaces, int n)
360 for (i=ifaces;i && n;i=i->next)
371 return the first IPv4 interface address we have registered
373 const char *iface_list_first_v4(struct interface *ifaces)
377 for (i=ifaces; i; i=i->next) {
378 if (i->ip.ss_family == AF_INET) {
386 return the first IPv6 interface address we have registered
388 static const char *iface_list_first_v6(struct interface *ifaces)
393 for (i=ifaces; i; i=i->next) {
394 if (i->ip.ss_family == AF_INET6) {
403 check if an interface is IPv4
405 bool iface_list_n_is_v4(struct interface *ifaces, int n)
409 for (i=ifaces;i && n;i=i->next)
413 return i->ip.ss_family == AF_INET;
419 return bcast of the Nth interface
421 const char *iface_list_n_bcast(struct interface *ifaces, int n)
425 for (i=ifaces;i && n;i=i->next)
435 return netmask of the Nth interface
437 const char *iface_list_n_netmask(struct interface *ifaces, int n)
441 for (i=ifaces;i && n;i=i->next)
451 return the local IP address that best matches a destination IP, or
452 our first interface if none match
454 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
456 struct interface *iface;
457 struct sockaddr_storage ss;
459 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
460 return iface_list_n_ip(ifaces, 0);
462 iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
467 if (ss.ss_family == AF_INET6) {
468 return iface_list_first_v6(ifaces);
471 return iface_list_first_v4(ifaces);
475 return true if an IP is one one of our local networks
477 bool iface_list_is_local(struct interface *ifaces, const char *dest)
479 struct sockaddr_storage ss;
481 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
484 if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
491 return true if a IP matches a IP/netmask pair
493 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
495 struct sockaddr_storage ip1_ss, ip2_ss, nm_ss;
497 if (!interpret_string_addr(&ip1_ss, ip1, AI_NUMERICHOST)) {
500 if (!interpret_string_addr(&ip2_ss, ip2, AI_NUMERICHOST)) {
503 if (!interpret_string_addr(&nm_ss, netmask, AI_NUMERICHOST)) {
507 return same_net((struct sockaddr *)&ip1_ss,
508 (struct sockaddr *)&ip2_ss,
509 (struct sockaddr *)&nm_ss);
513 return the list of wildcard interfaces
514 this will include the IPv4 0.0.0.0, and may include IPv6 ::
516 char **iface_list_wildcard(TALLOC_CTX *mem_ctx)
520 ret = str_list_make(mem_ctx, "::,0.0.0.0", NULL);
522 ret = str_list_make(mem_ctx, "0.0.0.0", NULL);