s4:lib/socket/interface.c - "ifaces" strictly needs to be initialised
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    multiple interface handling
5
6    Copyright (C) Andrew Tridgell 1992-2005
7    
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.
12    
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.
17    
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/>.
20 */
21
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"
28
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;
40 };
41
42 #define ALLONES  ((uint32_t)0xFFFFFFFF)
43 /*
44   address construction based on a patch from fred@datalync.com
45 */
46 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
47 #define MKNETADDR(_IP, _NM) (_IP & _NM)
48
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)
55 {
56         struct interface *i;
57
58         if (is_address_any(ip)) {
59                 return interfaces;
60         }
61
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;
66                         }
67                 } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) {
68                         return i;
69                 }
70         }
71
72         return NULL;
73 }
74
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)
80 {
81         char addr[INET6_ADDRSTRLEN];
82         struct interface *iface;
83
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;
88         }
89
90         if (!(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) {
91                 DEBUG(3,("not adding non-broadcast interface %s\n",
92                                         ifs->name ));
93                 return;
94         }
95
96         if (!enable_ipv6 && ifs->ip.ss_family != AF_INET) {
97                 return;
98         }
99
100         iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
101         if (iface == NULL) 
102                 return;
103         
104         ZERO_STRUCTPN(iface);
105
106         iface->name = talloc_strdup(iface, ifs->name);
107         if (!iface->name) {
108                 SAFE_FREE(iface);
109                 return;
110         }
111         iface->flags = ifs->flags;
112         iface->ip = ifs->ip;
113         iface->netmask = ifs->netmask;
114         iface->bcast = ifs->bcast;
115
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 ",
120                  iface->name, addr));
121         iface->ip_s = talloc_strdup(iface, addr);
122
123         print_sockaddr(addr, sizeof(addr),
124                        &iface->bcast);
125         DEBUG(4,("bcast=%s ", addr));
126         iface->bcast_s = talloc_strdup(iface, addr);
127
128         print_sockaddr(addr, sizeof(addr),
129                        &iface->netmask);
130         DEBUG(4,("netmask=%s\n", addr));
131         iface->nmask_s = talloc_strdup(iface, addr);
132
133         /*
134            this needs to be a ADD_END, as some tests (such as the
135            spoolss notify test) depend on the interfaces ordering
136         */
137         DLIST_ADD_END(*interfaces, iface, NULL);
138 }
139
140 /**
141 interpret a single element from a interfaces= config line 
142
143 This handles the following different forms:
144
145 1) wildcard interface name
146 2) DNS name
147 3) IP/masklen
148 4) ip/mask
149 5) bcast/mask
150 **/
151 static void interpret_interface(TALLOC_CTX *mem_ctx, 
152                                 const char *token, 
153                                 struct iface_struct *probed_ifaces, 
154                                 int total_probed,
155                                 struct interface **local_interfaces,
156                                 bool enable_ipv6)
157 {
158         struct sockaddr_storage ss;
159         struct sockaddr_storage ss_mask;
160         struct sockaddr_storage ss_net;
161         struct sockaddr_storage ss_bcast;
162         struct iface_struct ifs;
163         char *p;
164         int i;
165         bool added=false;
166         bool goodaddr = false;
167
168         /* first check if it is an interface name */
169         for (i=0;i<total_probed;i++) {
170                 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
171                         add_interface(mem_ctx, &probed_ifaces[i],
172                                       local_interfaces, enable_ipv6);
173                         added = true;
174                 }
175         }
176         if (added) {
177                 return;
178         }
179
180         /* maybe it is a DNS name */
181         p = strchr_m(token,'/');
182         if (p == NULL) {
183                 if (!interpret_string_addr(&ss, token, 0)) {
184                         DEBUG(2, ("interpret_interface: Can't find address "
185                                   "for %s\n", token));
186                         return;
187                 }
188
189                 for (i=0;i<total_probed;i++) {
190                         if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) {
191                                 add_interface(mem_ctx, &probed_ifaces[i],
192                                               local_interfaces, enable_ipv6);
193                                 return;
194                         }
195                 }
196                 DEBUG(2,("interpret_interface: "
197                         "can't determine interface for %s\n",
198                         token));
199                 return;
200         }
201
202         /* parse it into an IP address/netmasklength pair */
203         *p = 0;
204         goodaddr = interpret_string_addr(&ss, token, 0);
205         *p++ = '/';
206
207         if (!goodaddr) {
208                 DEBUG(2,("interpret_interface: "
209                         "can't determine interface for %s\n",
210                         token));
211                 return;
212         }
213
214         if (strlen(p) > 2) {
215                 goodaddr = interpret_string_addr(&ss_mask, p, 0);
216                 if (!goodaddr) {
217                         DEBUG(2,("interpret_interface: "
218                                 "can't determine netmask from %s\n",
219                                 p));
220                         return;
221                 }
222         } else {
223                 char *endp = NULL;
224                 unsigned long val = strtoul(p, &endp, 0);
225                 if (p == endp || (endp && *endp != '\0')) {
226                         DEBUG(2,("interpret_interface: "
227                                 "can't determine netmask value from %s\n",
228                                 p));
229                         return;
230                 }
231                 if (!make_netmask(&ss_mask, &ss, val)) {
232                         DEBUG(2,("interpret_interface: "
233                                 "can't apply netmask value %lu from %s\n",
234                                 val,
235                                 p));
236                         return;
237                 }
238         }
239
240         make_bcast(&ss_bcast, &ss, &ss_mask);
241         make_net(&ss_net, &ss, &ss_mask);
242
243         /* Maybe the first component was a broadcast address. */
244         if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
245                 sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
246                 for (i=0;i<total_probed;i++) {
247                         if (same_net((struct sockaddr *)&ss,
248                                                  (struct sockaddr *)&probed_ifaces[i].ip,
249                                                  (struct sockaddr *)&ss_mask)) {
250                                 /* Temporarily replace netmask on
251                                  * the detected interface - user knows
252                                  * best.... */
253                                 struct sockaddr_storage saved_mask =
254                                         probed_ifaces[i].netmask;
255                                 probed_ifaces[i].netmask = ss_mask;
256                                 DEBUG(2,("interpret_interface: "
257                                         "using netmask value %s from "
258                                         "config file on interface %s\n",
259                                         p,
260                                         probed_ifaces[i].name));
261                                 add_interface(mem_ctx, &probed_ifaces[i],
262                                               local_interfaces, enable_ipv6);
263                                 probed_ifaces[i].netmask = saved_mask;
264                                 return;
265                         }
266                 }
267                 DEBUG(2,("interpret_interface: Can't determine ip for "
268                         "broadcast address %s\n",
269                         token));
270                 return;
271         }
272
273         /* Just fake up the interface definition. User knows best. */
274
275         DEBUG(2,("interpret_interface: Adding interface %s\n",
276                 token));
277
278         ZERO_STRUCT(ifs);
279         (void)strlcpy(ifs.name, token, sizeof(ifs.name));
280         ifs.flags = IFF_BROADCAST;
281         ifs.ip = ss;
282         ifs.netmask = ss_mask;
283         ifs.bcast = ss_bcast;
284         add_interface(mem_ctx, &ifs,
285                       local_interfaces, enable_ipv6);
286 }
287
288
289 /**
290 load the list of network interfaces
291 **/
292 void load_interface_list(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct interface **local_interfaces)
293 {
294         const char **ptr = lpcfg_interfaces(lp_ctx);
295         int i;
296         struct iface_struct *ifaces = NULL;
297         int total_probed;
298         bool enable_ipv6 = lpcfg_parm_bool(lp_ctx, NULL, "ipv6", "enable", true);
299
300         *local_interfaces = NULL;
301
302         /* probe the kernel for interfaces */
303         total_probed = get_interfaces(mem_ctx, &ifaces);
304
305         /* if we don't have a interfaces line then use all interfaces
306            except loopback */
307         if (!ptr || !*ptr || !**ptr) {
308                 if (total_probed <= 0) {
309                         DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
310                 }
311                 for (i=0;i<total_probed;i++) {
312                         if (!is_loopback_addr((struct sockaddr *)&ifaces[i].ip)) {
313                                 add_interface(mem_ctx, &ifaces[i], local_interfaces, enable_ipv6);
314                         }
315                 }
316         }
317
318         while (ptr && *ptr) {
319                 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces, enable_ipv6);
320                 ptr++;
321         }
322
323         if (!*local_interfaces) {
324                 DEBUG(0,("WARNING: no network interfaces found\n"));
325         }
326         talloc_free(ifaces);
327 }
328
329 /**
330   how many interfaces do we have
331   **/
332 int iface_list_count(struct interface *ifaces)
333 {
334         int ret = 0;
335         struct interface *i;
336
337         for (i=ifaces;i;i=i->next)
338                 ret++;
339         return ret;
340 }
341
342 /**
343   return IP of the Nth interface
344   **/
345 const char *iface_list_n_ip(struct interface *ifaces, int n)
346 {
347         struct interface *i;
348   
349         for (i=ifaces;i && n;i=i->next)
350                 n--;
351
352         if (i) {
353                 return i->ip_s;
354         }
355         return NULL;
356 }
357
358
359 /**
360   return the first IPv4 interface address we have registered
361   **/
362 const char *iface_list_first_v4(struct interface *ifaces)
363 {
364         struct interface *i;
365
366         for (i=ifaces; i; i=i->next) {
367                 if (i->ip.ss_family == AF_INET) {
368                         return i->ip_s;
369                 }
370         }
371         return NULL;
372 }
373
374 /**
375   return the first IPv6 interface address we have registered
376   **/
377 static const char *iface_list_first_v6(struct interface *ifaces)
378 {
379         struct interface *i;
380
381 #ifdef HAVE_IPV6
382         for (i=ifaces; i; i=i->next) {
383                 if (i->ip.ss_family == AF_INET6) {
384                         return i->ip_s;
385                 }
386         }
387 #endif
388         return NULL;
389 }
390
391 /**
392    check if an interface is IPv4
393   **/
394 bool iface_list_n_is_v4(struct interface *ifaces, int n)
395 {
396         struct interface *i;
397
398         for (i=ifaces;i && n;i=i->next)
399                 n--;
400
401         if (i) {
402                 return i->ip.ss_family == AF_INET;
403         }
404         return false;
405 }
406
407 /**
408   return bcast of the Nth interface
409   **/
410 const char *iface_list_n_bcast(struct interface *ifaces, int n)
411 {
412         struct interface *i;
413   
414         for (i=ifaces;i && n;i=i->next)
415                 n--;
416
417         if (i) {
418                 return i->bcast_s;
419         }
420         return NULL;
421 }
422
423 /**
424   return netmask of the Nth interface
425   **/
426 const char *iface_list_n_netmask(struct interface *ifaces, int n)
427 {
428         struct interface *i;
429   
430         for (i=ifaces;i && n;i=i->next)
431                 n--;
432
433         if (i) {
434                 return i->nmask_s;
435         }
436         return NULL;
437 }
438
439 /**
440   return the local IP address that best matches a destination IP, or
441   our first interface if none match
442 */
443 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
444 {
445         struct interface *iface;
446         struct sockaddr_storage ss;
447
448         if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
449                 return iface_list_n_ip(ifaces, 0);
450         }
451         iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
452         if (iface) {
453                 return iface->ip_s;
454         }
455 #ifdef HAVE_IPV6
456         if (ss.ss_family == AF_INET6) {
457                 return iface_list_first_v6(ifaces);
458         }
459 #endif
460         return iface_list_first_v4(ifaces);
461 }
462
463 /**
464   return true if an IP is one one of our local networks
465 */
466 bool iface_list_is_local(struct interface *ifaces, const char *dest)
467 {
468         struct sockaddr_storage ss;
469
470         if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
471                 return false;
472         }
473         if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
474                 return true;
475         }
476         return false;
477 }
478
479 /**
480   return true if a IP matches a IP/netmask pair
481 */
482 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
483 {
484         struct sockaddr_storage ip1_ss, ip2_ss, nm_ss;
485
486         if (!interpret_string_addr(&ip1_ss, ip1, AI_NUMERICHOST)) {
487                 return false;
488         }
489         if (!interpret_string_addr(&ip2_ss, ip2, AI_NUMERICHOST)) {
490                 return false;
491         }
492         if (!interpret_string_addr(&nm_ss, netmask, AI_NUMERICHOST)) {
493                 return false;
494         }
495
496         return same_net((struct sockaddr *)&ip1_ss,
497                         (struct sockaddr *)&ip2_ss,
498                         (struct sockaddr *)&nm_ss);
499 }
500
501 /**
502    return the list of wildcard interfaces
503    this will include the IPv4 0.0.0.0, and may include IPv6 ::
504    it is overridden by the 'socket address' option in smb.conf
505 */
506 const char **iface_list_wildcard(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
507 {
508         const char **ret;
509         const char *socket_address;
510
511         /* the user may have configured a specific address */
512         socket_address = lpcfg_socket_address(lp_ctx);
513         if (strcmp(socket_address, "") != 0) {
514                 ret = (const char **)str_list_make(mem_ctx, socket_address, NULL);
515                 return ret;
516         }
517
518         ret = (const char **)str_list_make(mem_ctx, "0.0.0.0", NULL);
519         if (ret == NULL) return NULL;
520
521 #ifdef HAVE_IPV6
522         if (lpcfg_parm_bool(lp_ctx, NULL, "ipv6", "enable", true)) {
523                 struct interface *local_interfaces = NULL;
524
525                 load_interface_list(ret, lp_ctx, &local_interfaces);
526
527                 if (iface_list_first_v6(local_interfaces)) {
528                         TALLOC_FREE(local_interfaces);
529                         /*
530                          * only add "::" if we have at least
531                          * one ipv6 interface
532                          */
533                         return str_list_add(ret, "::");
534                 }
535                 TALLOC_FREE(local_interfaces);
536         }
537 #endif
538
539         return ret;
540 }