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"
29 /* used for network interfaces */
31 struct interface *next, *prev;
34 struct sockaddr_storage ip;
35 struct sockaddr_storage netmask;
36 struct sockaddr_storage bcast;
42 #define ALLONES ((uint32_t)0xFFFFFFFF)
44 address construction based on a patch from fred@datalync.com
46 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
47 #define MKNETADDR(_IP, _NM) (_IP & _NM)
49 /****************************************************************************
50 Try and find an interface that matches an ip. If we cannot, return NULL
51 **************************************************************************/
52 static struct interface *iface_list_find(struct interface *interfaces,
53 const struct sockaddr *ip,
58 if (is_address_any(ip)) {
62 for (i=interfaces;i;i=i->next) {
64 if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) {
67 } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) {
75 /****************************************************************************
76 add an interface to the linked list of interfaces
77 ****************************************************************************/
78 static void add_interface(TALLOC_CTX *mem_ctx, const struct iface_struct *ifs, struct interface **interfaces)
80 char addr[INET6_ADDRSTRLEN];
81 struct interface *iface;
83 if (iface_list_find(*interfaces, (const struct sockaddr *)&ifs->ip, false)) {
84 DEBUG(3,("add_interface: not adding duplicate interface %s\n",
85 print_sockaddr(addr, sizeof(addr), &ifs->ip) ));
89 if (!(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) {
90 DEBUG(3,("not adding non-broadcast interface %s\n",
95 iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
101 iface->name = talloc_strdup(iface, ifs->name);
106 iface->flags = ifs->flags;
108 iface->netmask = ifs->netmask;
109 iface->bcast = ifs->bcast;
111 /* keep string versions too, to avoid people tripping over the implied
112 static in inet_ntoa() */
113 print_sockaddr(addr, sizeof(addr), &iface->ip);
114 DEBUG(2,("added interface %s ip=%s ",
116 iface->ip_s = talloc_strdup(iface, addr);
118 print_sockaddr(addr, sizeof(addr),
120 DEBUG(2,("bcast=%s ", addr));
121 iface->bcast_s = talloc_strdup(iface, addr);
123 print_sockaddr(addr, sizeof(addr),
125 DEBUG(2,("netmask=%s\n", addr));
126 iface->nmask_s = talloc_strdup(iface, addr);
129 this needs to be a ADD_END, as some tests (such as the
130 spoolss notify test) depend on the interfaces ordering
132 DLIST_ADD_END(*interfaces, iface, NULL);
136 interpret a single element from a interfaces= config line
138 This handles the following different forms:
140 1) wildcard interface name
146 static void interpret_interface(TALLOC_CTX *mem_ctx,
148 struct iface_struct *probed_ifaces,
150 struct interface **local_interfaces)
152 struct sockaddr_storage ss;
153 struct sockaddr_storage ss_mask;
154 struct sockaddr_storage ss_net;
155 struct sockaddr_storage ss_bcast;
156 struct iface_struct ifs;
160 bool goodaddr = false;
162 /* first check if it is an interface name */
163 for (i=0;i<total_probed;i++) {
164 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
165 add_interface(mem_ctx, &probed_ifaces[i],
174 /* maybe it is a DNS name */
175 p = strchr_m(token,'/');
177 if (!interpret_string_addr(&ss, token, 0)) {
178 DEBUG(2, ("interpret_interface: Can't find address "
183 for (i=0;i<total_probed;i++) {
184 if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) {
185 add_interface(mem_ctx, &probed_ifaces[i],
190 DEBUG(2,("interpret_interface: "
191 "can't determine interface for %s\n",
196 /* parse it into an IP address/netmasklength pair */
198 goodaddr = interpret_string_addr(&ss, token, 0);
202 DEBUG(2,("interpret_interface: "
203 "can't determine interface for %s\n",
209 goodaddr = interpret_string_addr(&ss_mask, p, 0);
211 DEBUG(2,("interpret_interface: "
212 "can't determine netmask from %s\n",
218 unsigned long val = strtoul(p, &endp, 0);
219 if (p == endp || (endp && *endp != '\0')) {
220 DEBUG(2,("interpret_interface: "
221 "can't determine netmask value from %s\n",
225 if (!make_netmask(&ss_mask, &ss, val)) {
226 DEBUG(2,("interpret_interface: "
227 "can't apply netmask value %lu from %s\n",
234 make_bcast(&ss_bcast, &ss, &ss_mask);
235 make_net(&ss_net, &ss, &ss_mask);
237 /* Maybe the first component was a broadcast address. */
238 if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
239 sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
240 for (i=0;i<total_probed;i++) {
241 if (same_net((struct sockaddr *)&ss,
242 (struct sockaddr *)&probed_ifaces[i].ip,
243 (struct sockaddr *)&ss_mask)) {
244 /* Temporarily replace netmask on
245 * the detected interface - user knows
247 struct sockaddr_storage saved_mask =
248 probed_ifaces[i].netmask;
249 probed_ifaces[i].netmask = ss_mask;
250 DEBUG(2,("interpret_interface: "
251 "using netmask value %s from "
252 "config file on interface %s\n",
254 probed_ifaces[i].name));
255 add_interface(mem_ctx, &probed_ifaces[i],
257 probed_ifaces[i].netmask = saved_mask;
261 DEBUG(2,("interpret_interface: Can't determine ip for "
262 "broadcast address %s\n",
267 /* Just fake up the interface definition. User knows best. */
269 DEBUG(2,("interpret_interface: Adding interface %s\n",
273 (void)strlcpy(ifs.name, token, sizeof(ifs.name));
274 ifs.flags = IFF_BROADCAST;
276 ifs.netmask = ss_mask;
277 ifs.bcast = ss_bcast;
278 add_interface(mem_ctx, &ifs,
284 load the list of network interfaces
286 void load_interface_list(TALLOC_CTX *mem_ctx, const char **interfaces, struct interface **local_interfaces)
288 const char **ptr = interfaces;
290 struct iface_struct *ifaces;
293 *local_interfaces = NULL;
295 /* probe the kernel for interfaces */
296 total_probed = get_interfaces(mem_ctx, &ifaces);
298 /* if we don't have a interfaces line then use all interfaces
300 if (!ptr || !*ptr || !**ptr) {
301 if (total_probed <= 0) {
302 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
304 for (i=0;i<total_probed;i++) {
305 if (!is_loopback_addr(&ifaces[i].ip)) {
306 add_interface(mem_ctx, &ifaces[i], local_interfaces);
311 while (ptr && *ptr) {
312 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
316 if (!*local_interfaces) {
317 DEBUG(0,("WARNING: no network interfaces found\n"));
323 how many interfaces do we have
325 int iface_list_count(struct interface *ifaces)
330 for (i=ifaces;i;i=i->next)
336 return IP of the Nth interface
338 const char *iface_list_n_ip(struct interface *ifaces, int n)
342 for (i=ifaces;i && n;i=i->next)
352 return bcast of the Nth interface
354 const char *iface_list_n_bcast(struct interface *ifaces, int n)
358 for (i=ifaces;i && n;i=i->next)
368 return netmask of the Nth interface
370 const char *iface_list_n_netmask(struct interface *ifaces, int n)
374 for (i=ifaces;i && n;i=i->next)
384 return the local IP address that best matches a destination IP, or
385 our first interface if none match
387 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
389 struct interface *iface;
390 struct sockaddr_storage ss;
392 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
393 return iface_list_n_ip(ifaces, 0);
395 iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
399 return iface_list_n_ip(ifaces, 0);
403 return true if an IP is one one of our local networks
405 bool iface_list_is_local(struct interface *ifaces, const char *dest)
407 struct sockaddr_storage ss;
409 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
412 if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
419 return true if a IP matches a IP/netmask pair
421 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
423 return same_net_v4(interpret_addr2(ip1),
424 interpret_addr2(ip2),
425 interpret_addr2(netmask));
429 return the list of wildcard interfaces
430 this will include the IPv4 0.0.0.0, and may include IPv6 ::
431 it is overridden by the 'socket address' option in smb.conf
433 const char **iface_list_wildcard(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
436 const char *socket_address;
438 /* the user may have configured a specific address */
439 socket_address = lpcfg_socket_address(lp_ctx);
440 if (strcmp(socket_address, "") != 0) {
441 ret = (const char **)str_list_make(mem_ctx, socket_address, NULL);
445 ret = (const char **)str_list_make(mem_ctx, "0.0.0.0", NULL);
446 if (ret == NULL) return NULL;
449 return str_list_add(ret, "::");