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 if (ifs->ip.ss_family != AF_INET) {
96 DEBUG(5, ("not adding IPv6 interface %s\n", ifs->name));
100 iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
104 ZERO_STRUCTPN(iface);
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(2,("added interface %s ip=%s ",
121 iface->ip_s = talloc_strdup(iface, addr);
123 print_sockaddr(addr, sizeof(addr),
125 DEBUG(2,("bcast=%s ", addr));
126 iface->bcast_s = talloc_strdup(iface, addr);
128 print_sockaddr(addr, sizeof(addr),
130 DEBUG(2,("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, NULL);
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 /* maybe it is a DNS name */
180 p = strchr_m(token,'/');
182 if (!interpret_string_addr(&ss, token, 0)) {
183 DEBUG(2, ("interpret_interface: Can't find address "
188 for (i=0;i<total_probed;i++) {
189 if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) {
190 add_interface(mem_ctx, &probed_ifaces[i],
195 DEBUG(2,("interpret_interface: "
196 "can't determine interface for %s\n",
201 /* parse it into an IP address/netmasklength pair */
203 goodaddr = interpret_string_addr(&ss, token, 0);
207 DEBUG(2,("interpret_interface: "
208 "can't determine interface for %s\n",
214 goodaddr = interpret_string_addr(&ss_mask, p, 0);
216 DEBUG(2,("interpret_interface: "
217 "can't determine netmask from %s\n",
223 unsigned long val = strtoul(p, &endp, 0);
224 if (p == endp || (endp && *endp != '\0')) {
225 DEBUG(2,("interpret_interface: "
226 "can't determine netmask value from %s\n",
230 if (!make_netmask(&ss_mask, &ss, val)) {
231 DEBUG(2,("interpret_interface: "
232 "can't apply netmask value %lu from %s\n",
239 make_bcast(&ss_bcast, &ss, &ss_mask);
240 make_net(&ss_net, &ss, &ss_mask);
242 /* Maybe the first component was a broadcast address. */
243 if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
244 sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
245 for (i=0;i<total_probed;i++) {
246 if (same_net((struct sockaddr *)&ss,
247 (struct sockaddr *)&probed_ifaces[i].ip,
248 (struct sockaddr *)&ss_mask)) {
249 /* Temporarily replace netmask on
250 * the detected interface - user knows
252 struct sockaddr_storage saved_mask =
253 probed_ifaces[i].netmask;
254 probed_ifaces[i].netmask = ss_mask;
255 DEBUG(2,("interpret_interface: "
256 "using netmask value %s from "
257 "config file on interface %s\n",
259 probed_ifaces[i].name));
260 add_interface(mem_ctx, &probed_ifaces[i],
262 probed_ifaces[i].netmask = saved_mask;
266 DEBUG(2,("interpret_interface: Can't determine ip for "
267 "broadcast address %s\n",
272 /* Just fake up the interface definition. User knows best. */
274 DEBUG(2,("interpret_interface: Adding interface %s\n",
278 (void)strlcpy(ifs.name, token, sizeof(ifs.name));
279 ifs.flags = IFF_BROADCAST;
281 ifs.netmask = ss_mask;
282 ifs.bcast = ss_bcast;
283 add_interface(mem_ctx, &ifs,
289 load the list of network interfaces
291 void load_interface_list(TALLOC_CTX *mem_ctx, const char **interfaces, struct interface **local_interfaces)
293 const char **ptr = interfaces;
295 struct iface_struct *ifaces;
298 *local_interfaces = NULL;
300 /* probe the kernel for interfaces */
301 total_probed = get_interfaces(mem_ctx, &ifaces);
303 /* if we don't have a interfaces line then use all interfaces
305 if (!ptr || !*ptr || !**ptr) {
306 if (total_probed <= 0) {
307 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
309 for (i=0;i<total_probed;i++) {
310 if (!is_loopback_addr(&ifaces[i].ip)) {
311 add_interface(mem_ctx, &ifaces[i], local_interfaces);
316 while (ptr && *ptr) {
317 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
321 if (!*local_interfaces) {
322 DEBUG(0,("WARNING: no network interfaces found\n"));
328 how many interfaces do we have
330 int iface_list_count(struct interface *ifaces)
335 for (i=ifaces;i;i=i->next)
341 return IP of the Nth interface
343 const char *iface_list_n_ip(struct interface *ifaces, int n)
347 for (i=ifaces;i && n;i=i->next)
357 return bcast of the Nth interface
359 const char *iface_list_n_bcast(struct interface *ifaces, int n)
363 for (i=ifaces;i && n;i=i->next)
373 return netmask of the Nth interface
375 const char *iface_list_n_netmask(struct interface *ifaces, int n)
379 for (i=ifaces;i && n;i=i->next)
389 return the local IP address that best matches a destination IP, or
390 our first interface if none match
392 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
394 struct interface *iface;
395 struct sockaddr_storage ss;
397 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
398 return iface_list_n_ip(ifaces, 0);
400 iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
404 return iface_list_n_ip(ifaces, 0);
408 return true if an IP is one one of our local networks
410 bool iface_list_is_local(struct interface *ifaces, const char *dest)
412 struct sockaddr_storage ss;
414 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
417 if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
424 return true if a IP matches a IP/netmask pair
426 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
428 return same_net_v4(interpret_addr2(ip1),
429 interpret_addr2(ip2),
430 interpret_addr2(netmask));
434 return the list of wildcard interfaces
435 this will include the IPv4 0.0.0.0, and may include IPv6 ::
436 it is overridden by the 'socket address' option in smb.conf
438 const char **iface_list_wildcard(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
441 const char *socket_address;
443 /* the user may have configured a specific address */
444 socket_address = lpcfg_socket_address(lp_ctx);
445 if (strcmp(socket_address, "") != 0) {
446 ret = (const char **)str_list_make(mem_ctx, socket_address, NULL);
450 ret = (const char **)str_list_make(mem_ctx, "0.0.0.0", NULL);
451 if (ret == NULL) return NULL;
454 return str_list_add(ret, "::");