s4-interfaces: allow pure ipv6 to work
[Samba/gebeck_regimport.git] / source4 / lib / socket / interface.c
blob27ac7151a3b18ae798c8db06339554dfe9dcffe7
1 /*
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/>.
22 #include "includes.h"
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 */
30 struct interface {
31 struct interface *next, *prev;
32 char *name;
33 int flags;
34 struct sockaddr_storage ip;
35 struct sockaddr_storage netmask;
36 struct sockaddr_storage bcast;
37 const char *ip_s;
38 const char *bcast_s;
39 const char *nmask_s;
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,
54 bool check_mask)
56 struct interface *i;
58 if (is_address_any(ip)) {
59 return interfaces;
62 for (i=interfaces;i;i=i->next) {
63 if (check_mask) {
64 if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) {
65 return i;
67 } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) {
68 return i;
72 return NULL;
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,
79 bool enable_ipv6)
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) ));
87 return;
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",
93 ifs->name ));
94 return;
97 if (!enable_ipv6 && ifs->ip.ss_family != AF_INET) {
98 return;
101 iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
102 if (iface == NULL)
103 return;
105 ZERO_STRUCTPN(iface);
107 iface->name = talloc_strdup(iface, ifs->name);
108 if (!iface->name) {
109 SAFE_FREE(iface);
110 return;
112 iface->flags = ifs->flags;
113 iface->ip = ifs->ip;
114 iface->netmask = ifs->netmask;
115 iface->bcast = ifs->bcast;
117 /* keep string versions too, to avoid people tripping over the implied
118 static in inet_ntoa() */
119 print_sockaddr(addr, sizeof(addr), &iface->ip);
120 DEBUG(4,("added interface %s ip=%s ",
121 iface->name, addr));
122 iface->ip_s = talloc_strdup(iface, addr);
124 print_sockaddr(addr, sizeof(addr),
125 &iface->bcast);
126 DEBUG(4,("bcast=%s ", addr));
127 iface->bcast_s = talloc_strdup(iface, addr);
129 print_sockaddr(addr, sizeof(addr),
130 &iface->netmask);
131 DEBUG(4,("netmask=%s\n", addr));
132 iface->nmask_s = talloc_strdup(iface, addr);
135 this needs to be a ADD_END, as some tests (such as the
136 spoolss notify test) depend on the interfaces ordering
138 DLIST_ADD_END(*interfaces, iface, NULL);
142 interpret a single element from a interfaces= config line
144 This handles the following different forms:
146 1) wildcard interface name
147 2) DNS name
148 3) IP/masklen
149 4) ip/mask
150 5) bcast/mask
152 static void interpret_interface(TALLOC_CTX *mem_ctx,
153 const char *token,
154 struct iface_struct *probed_ifaces,
155 int total_probed,
156 struct interface **local_interfaces,
157 bool enable_ipv6)
159 struct sockaddr_storage ss;
160 struct sockaddr_storage ss_mask;
161 struct sockaddr_storage ss_net;
162 struct sockaddr_storage ss_bcast;
163 struct iface_struct ifs;
164 char *p;
165 int i;
166 bool added=false;
167 bool goodaddr = false;
169 /* first check if it is an interface name */
170 for (i=0;i<total_probed;i++) {
171 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
172 add_interface(mem_ctx, &probed_ifaces[i],
173 local_interfaces, enable_ipv6);
174 added = true;
177 if (added) {
178 return;
181 /* maybe it is a DNS name */
182 p = strchr_m(token,'/');
183 if (p == NULL) {
184 if (!interpret_string_addr(&ss, token, 0)) {
185 DEBUG(2, ("interpret_interface: Can't find address "
186 "for %s\n", token));
187 return;
190 for (i=0;i<total_probed;i++) {
191 if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) {
192 add_interface(mem_ctx, &probed_ifaces[i],
193 local_interfaces, enable_ipv6);
194 return;
197 DEBUG(2,("interpret_interface: "
198 "can't determine interface for %s\n",
199 token));
200 return;
203 /* parse it into an IP address/netmasklength pair */
204 *p = 0;
205 goodaddr = interpret_string_addr(&ss, token, 0);
206 *p++ = '/';
208 if (!goodaddr) {
209 DEBUG(2,("interpret_interface: "
210 "can't determine interface for %s\n",
211 token));
212 return;
215 if (strlen(p) > 2) {
216 goodaddr = interpret_string_addr(&ss_mask, p, 0);
217 if (!goodaddr) {
218 DEBUG(2,("interpret_interface: "
219 "can't determine netmask from %s\n",
220 p));
221 return;
223 } else {
224 char *endp = NULL;
225 unsigned long val = strtoul(p, &endp, 0);
226 if (p == endp || (endp && *endp != '\0')) {
227 DEBUG(2,("interpret_interface: "
228 "can't determine netmask value from %s\n",
229 p));
230 return;
232 if (!make_netmask(&ss_mask, &ss, val)) {
233 DEBUG(2,("interpret_interface: "
234 "can't apply netmask value %lu from %s\n",
235 val,
236 p));
237 return;
241 make_bcast(&ss_bcast, &ss, &ss_mask);
242 make_net(&ss_net, &ss, &ss_mask);
244 /* Maybe the first component was a broadcast address. */
245 if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
246 sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
247 for (i=0;i<total_probed;i++) {
248 if (same_net((struct sockaddr *)&ss,
249 (struct sockaddr *)&probed_ifaces[i].ip,
250 (struct sockaddr *)&ss_mask)) {
251 /* Temporarily replace netmask on
252 * the detected interface - user knows
253 * best.... */
254 struct sockaddr_storage saved_mask =
255 probed_ifaces[i].netmask;
256 probed_ifaces[i].netmask = ss_mask;
257 DEBUG(2,("interpret_interface: "
258 "using netmask value %s from "
259 "config file on interface %s\n",
261 probed_ifaces[i].name));
262 add_interface(mem_ctx, &probed_ifaces[i],
263 local_interfaces, enable_ipv6);
264 probed_ifaces[i].netmask = saved_mask;
265 return;
268 DEBUG(2,("interpret_interface: Can't determine ip for "
269 "broadcast address %s\n",
270 token));
271 return;
274 /* Just fake up the interface definition. User knows best. */
276 DEBUG(2,("interpret_interface: Adding interface %s\n",
277 token));
279 ZERO_STRUCT(ifs);
280 (void)strlcpy(ifs.name, token, sizeof(ifs.name));
281 ifs.flags = IFF_BROADCAST;
282 ifs.ip = ss;
283 ifs.netmask = ss_mask;
284 ifs.bcast = ss_bcast;
285 add_interface(mem_ctx, &ifs,
286 local_interfaces, enable_ipv6);
291 load the list of network interfaces
293 void load_interface_list(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct interface **local_interfaces)
295 const char **ptr = lpcfg_interfaces(lp_ctx);
296 int i;
297 struct iface_struct *ifaces = NULL;
298 int total_probed;
299 bool enable_ipv6 = lpcfg_parm_bool(lp_ctx, NULL, "ipv6", "enable", true);
301 *local_interfaces = NULL;
303 /* probe the kernel for interfaces */
304 total_probed = get_interfaces(mem_ctx, &ifaces);
306 /* if we don't have a interfaces line then use all interfaces
307 except loopback */
308 if (!ptr || !*ptr || !**ptr) {
309 if (total_probed <= 0) {
310 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
312 for (i=0;i<total_probed;i++) {
313 if (!is_loopback_addr((struct sockaddr *)&ifaces[i].ip)) {
314 add_interface(mem_ctx, &ifaces[i], local_interfaces, enable_ipv6);
319 while (ptr && *ptr) {
320 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces, enable_ipv6);
321 ptr++;
324 if (!*local_interfaces) {
325 DEBUG(0,("WARNING: no network interfaces found\n"));
327 talloc_free(ifaces);
331 how many interfaces do we have
333 int iface_list_count(struct interface *ifaces)
335 int ret = 0;
336 struct interface *i;
338 for (i=ifaces;i;i=i->next)
339 ret++;
340 return ret;
344 return IP of the Nth interface
346 const char *iface_list_n_ip(struct interface *ifaces, int n)
348 struct interface *i;
350 for (i=ifaces;i && n;i=i->next)
351 n--;
353 if (i) {
354 return i->ip_s;
356 return NULL;
361 return the first IPv4 interface address we have registered
363 const char *iface_list_first_v4(struct interface *ifaces)
365 struct interface *i;
367 for (i=ifaces; i; i=i->next) {
368 if (i->ip.ss_family == AF_INET) {
369 return i->ip_s;
372 return NULL;
376 return the first IPv6 interface address we have registered
378 static const char *iface_list_first_v6(struct interface *ifaces)
380 struct interface *i;
382 #ifdef HAVE_IPV6
383 for (i=ifaces; i; i=i->next) {
384 if (i->ip.ss_family == AF_INET6) {
385 return i->ip_s;
388 #endif
389 return NULL;
393 check if an interface is IPv4
395 bool iface_list_n_is_v4(struct interface *ifaces, int n)
397 struct interface *i;
399 for (i=ifaces;i && n;i=i->next)
400 n--;
402 if (i) {
403 return i->ip.ss_family == AF_INET;
405 return false;
409 return bcast of the Nth interface
411 const char *iface_list_n_bcast(struct interface *ifaces, int n)
413 struct interface *i;
415 for (i=ifaces;i && n;i=i->next)
416 n--;
418 if (i) {
419 return i->bcast_s;
421 return NULL;
425 return netmask of the Nth interface
427 const char *iface_list_n_netmask(struct interface *ifaces, int n)
429 struct interface *i;
431 for (i=ifaces;i && n;i=i->next)
432 n--;
434 if (i) {
435 return i->nmask_s;
437 return NULL;
441 return the local IP address that best matches a destination IP, or
442 our first interface if none match
444 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
446 struct interface *iface;
447 struct sockaddr_storage ss;
449 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
450 return iface_list_n_ip(ifaces, 0);
452 iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
453 if (iface) {
454 return iface->ip_s;
456 #ifdef HAVE_IPV6
457 if (ss.ss_family == AF_INET6) {
458 return iface_list_first_v6(ifaces);
460 #endif
461 return iface_list_first_v4(ifaces);
465 return true if an IP is one one of our local networks
467 bool iface_list_is_local(struct interface *ifaces, const char *dest)
469 struct sockaddr_storage ss;
471 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
472 return false;
474 if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
475 return true;
477 return false;
481 return true if a IP matches a IP/netmask pair
483 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
485 struct sockaddr_storage ip1_ss, ip2_ss, nm_ss;
487 if (!interpret_string_addr(&ip1_ss, ip1, AI_NUMERICHOST)) {
488 return false;
490 if (!interpret_string_addr(&ip2_ss, ip2, AI_NUMERICHOST)) {
491 return false;
493 if (!interpret_string_addr(&nm_ss, netmask, AI_NUMERICHOST)) {
494 return false;
497 return same_net((struct sockaddr *)&ip1_ss,
498 (struct sockaddr *)&ip2_ss,
499 (struct sockaddr *)&nm_ss);
503 return the list of wildcard interfaces
504 this will include the IPv4 0.0.0.0, and may include IPv6 ::
505 it is overridden by the 'socket address' option in smb.conf
507 const char **iface_list_wildcard(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
509 const char **ret;
510 const char *socket_address;
512 /* the user may have configured a specific address */
513 socket_address = lpcfg_socket_address(lp_ctx);
514 if (strcmp(socket_address, "") != 0) {
515 ret = (const char **)str_list_make(mem_ctx, socket_address, NULL);
516 return ret;
519 ret = (const char **)str_list_make(mem_ctx, "0.0.0.0", NULL);
520 if (ret == NULL) return NULL;
522 #ifdef HAVE_IPV6
523 if (lpcfg_parm_bool(lp_ctx, NULL, "ipv6", "enable", true)) {
524 struct interface *local_interfaces = NULL;
526 load_interface_list(ret, lp_ctx, &local_interfaces);
528 if (iface_list_first_v6(local_interfaces)) {
529 TALLOC_FREE(local_interfaces);
531 * only add "::" if we have at least
532 * one ipv6 interface
534 return str_list_add(ret, "::");
536 TALLOC_FREE(local_interfaces);
538 #endif
540 return ret;