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