s4-ipv6: allow IPv6 addresses in our interfaces list
[abartlet/samba.git/.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->flags & (IFF_BROADCAST|IFF_LOOPBACK))) {
90                 DEBUG(3,("not adding non-broadcast interface %s\n",
91                                         ifs->name ));
92                 return;
93         }
94
95         iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
96         if (iface == NULL) 
97                 return;
98         
99         ZERO_STRUCTPN(iface);
100
101         iface->name = talloc_strdup(iface, ifs->name);
102         if (!iface->name) {
103                 SAFE_FREE(iface);
104                 return;
105         }
106         iface->flags = ifs->flags;
107         iface->ip = ifs->ip;
108         iface->netmask = ifs->netmask;
109         iface->bcast = ifs->bcast;
110
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 ",
115                  iface->name, addr));
116         iface->ip_s = talloc_strdup(iface, addr);
117
118         print_sockaddr(addr, sizeof(addr),
119                        &iface->bcast);
120         DEBUG(2,("bcast=%s ", addr));
121         iface->bcast_s = talloc_strdup(iface, addr);
122
123         print_sockaddr(addr, sizeof(addr),
124                        &iface->netmask);
125         DEBUG(2,("netmask=%s\n", addr));
126         iface->nmask_s = talloc_strdup(iface, addr);
127
128         /*
129            this needs to be a ADD_END, as some tests (such as the
130            spoolss notify test) depend on the interfaces ordering
131         */
132         DLIST_ADD_END(*interfaces, iface, NULL);
133 }
134
135 /**
136 interpret a single element from a interfaces= config line 
137
138 This handles the following different forms:
139
140 1) wildcard interface name
141 2) DNS name
142 3) IP/masklen
143 4) ip/mask
144 5) bcast/mask
145 **/
146 static void interpret_interface(TALLOC_CTX *mem_ctx, 
147                                 const char *token, 
148                                 struct iface_struct *probed_ifaces, 
149                                 int total_probed,
150                                 struct interface **local_interfaces)
151 {
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;
157         char *p;
158         int i;
159         bool added=false;
160         bool goodaddr = false;
161
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],
166                                       local_interfaces);
167                         added = true;
168                 }
169         }
170         if (added) {
171                 return;
172         }
173
174         /* maybe it is a DNS name */
175         p = strchr_m(token,'/');
176         if (p == NULL) {
177                 if (!interpret_string_addr(&ss, token, 0)) {
178                         DEBUG(2, ("interpret_interface: Can't find address "
179                                   "for %s\n", token));
180                         return;
181                 }
182
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],
186                                               local_interfaces);
187                                 return;
188                         }
189                 }
190                 DEBUG(2,("interpret_interface: "
191                         "can't determine interface for %s\n",
192                         token));
193                 return;
194         }
195
196         /* parse it into an IP address/netmasklength pair */
197         *p = 0;
198         goodaddr = interpret_string_addr(&ss, token, 0);
199         *p++ = '/';
200
201         if (!goodaddr) {
202                 DEBUG(2,("interpret_interface: "
203                         "can't determine interface for %s\n",
204                         token));
205                 return;
206         }
207
208         if (strlen(p) > 2) {
209                 goodaddr = interpret_string_addr(&ss_mask, p, 0);
210                 if (!goodaddr) {
211                         DEBUG(2,("interpret_interface: "
212                                 "can't determine netmask from %s\n",
213                                 p));
214                         return;
215                 }
216         } else {
217                 char *endp = NULL;
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",
222                                 p));
223                         return;
224                 }
225                 if (!make_netmask(&ss_mask, &ss, val)) {
226                         DEBUG(2,("interpret_interface: "
227                                 "can't apply netmask value %lu from %s\n",
228                                 val,
229                                 p));
230                         return;
231                 }
232         }
233
234         make_bcast(&ss_bcast, &ss, &ss_mask);
235         make_net(&ss_net, &ss, &ss_mask);
236
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
246                                  * best.... */
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",
253                                         p,
254                                         probed_ifaces[i].name));
255                                 add_interface(mem_ctx, &probed_ifaces[i],
256                                               local_interfaces);
257                                 probed_ifaces[i].netmask = saved_mask;
258                                 return;
259                         }
260                 }
261                 DEBUG(2,("interpret_interface: Can't determine ip for "
262                         "broadcast address %s\n",
263                         token));
264                 return;
265         }
266
267         /* Just fake up the interface definition. User knows best. */
268
269         DEBUG(2,("interpret_interface: Adding interface %s\n",
270                 token));
271
272         ZERO_STRUCT(ifs);
273         (void)strlcpy(ifs.name, token, sizeof(ifs.name));
274         ifs.flags = IFF_BROADCAST;
275         ifs.ip = ss;
276         ifs.netmask = ss_mask;
277         ifs.bcast = ss_bcast;
278         add_interface(mem_ctx, &ifs,
279                       local_interfaces);
280 }
281
282
283 /**
284 load the list of network interfaces
285 **/
286 void load_interface_list(TALLOC_CTX *mem_ctx, const char **interfaces, struct interface **local_interfaces)
287 {
288         const char **ptr = interfaces;
289         int i;
290         struct iface_struct *ifaces;
291         int total_probed;
292
293         *local_interfaces = NULL;
294
295         /* probe the kernel for interfaces */
296         total_probed = get_interfaces(mem_ctx, &ifaces);
297
298         /* if we don't have a interfaces line then use all interfaces
299            except loopback */
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"));
303                 }
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);
307                         }
308                 }
309         }
310
311         while (ptr && *ptr) {
312                 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
313                 ptr++;
314         }
315
316         if (!*local_interfaces) {
317                 DEBUG(0,("WARNING: no network interfaces found\n"));
318         }
319         talloc_free(ifaces);
320 }
321
322 /**
323   how many interfaces do we have
324   **/
325 int iface_list_count(struct interface *ifaces)
326 {
327         int ret = 0;
328         struct interface *i;
329
330         for (i=ifaces;i;i=i->next)
331                 ret++;
332         return ret;
333 }
334
335 /**
336   return IP of the Nth interface
337   **/
338 const char *iface_list_n_ip(struct interface *ifaces, int n)
339 {
340         struct interface *i;
341   
342         for (i=ifaces;i && n;i=i->next)
343                 n--;
344
345         if (i) {
346                 return i->ip_s;
347         }
348         return NULL;
349 }
350
351 /**
352   return bcast of the Nth interface
353   **/
354 const char *iface_list_n_bcast(struct interface *ifaces, int n)
355 {
356         struct interface *i;
357   
358         for (i=ifaces;i && n;i=i->next)
359                 n--;
360
361         if (i) {
362                 return i->bcast_s;
363         }
364         return NULL;
365 }
366
367 /**
368   return netmask of the Nth interface
369   **/
370 const char *iface_list_n_netmask(struct interface *ifaces, int n)
371 {
372         struct interface *i;
373   
374         for (i=ifaces;i && n;i=i->next)
375                 n--;
376
377         if (i) {
378                 return i->nmask_s;
379         }
380         return NULL;
381 }
382
383 /**
384   return the local IP address that best matches a destination IP, or
385   our first interface if none match
386 */
387 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
388 {
389         struct interface *iface;
390         struct sockaddr_storage ss;
391
392         if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
393                 return iface_list_n_ip(ifaces, 0);
394         }
395         iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
396         if (iface) {
397                 return iface->ip_s;
398         }
399         return iface_list_n_ip(ifaces, 0);
400 }
401
402 /**
403   return true if an IP is one one of our local networks
404 */
405 bool iface_list_is_local(struct interface *ifaces, const char *dest)
406 {
407         struct sockaddr_storage ss;
408
409         if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
410                 return false;
411         }
412         if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
413                 return true;
414         }
415         return false;
416 }
417
418 /**
419   return true if a IP matches a IP/netmask pair
420 */
421 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
422 {
423         return same_net_v4(interpret_addr2(ip1),
424                         interpret_addr2(ip2),
425                         interpret_addr2(netmask));
426 }
427
428 /**
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
432 */
433 const char **iface_list_wildcard(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
434 {
435         const char **ret;
436         const char *socket_address;
437
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);
442                 return ret;
443         }
444
445         ret = (const char **)str_list_make(mem_ctx, "0.0.0.0", NULL);
446         if (ret == NULL) return NULL;
447
448 #ifdef HAVE_IPV6
449         return str_list_add(ret, "::");
450 #endif
451
452         return ret;
453 }