s4-ipv6: added iface_list_wildcard()
[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         if (ifs->ip.ss_family != AF_INET) {
96                 DEBUG(5, ("not adding IPv6 interface %s\n", ifs->name));
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(2,("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(2,("bcast=%s ", addr));
126         iface->bcast_s = talloc_strdup(iface, addr);
127
128         print_sockaddr(addr, sizeof(addr),
129                        &iface->netmask);
130         DEBUG(2,("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 {
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;
162         char *p;
163         int i;
164         bool added=false;
165         bool goodaddr = false;
166
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],
171                                       local_interfaces);
172                         added = true;
173                 }
174         }
175         if (added) {
176                 return;
177         }
178
179         /* maybe it is a DNS name */
180         p = strchr_m(token,'/');
181         if (p == NULL) {
182                 if (!interpret_string_addr(&ss, token, 0)) {
183                         DEBUG(2, ("interpret_interface: Can't find address "
184                                   "for %s\n", token));
185                         return;
186                 }
187
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],
191                                               local_interfaces);
192                                 return;
193                         }
194                 }
195                 DEBUG(2,("interpret_interface: "
196                         "can't determine interface for %s\n",
197                         token));
198                 return;
199         }
200
201         /* parse it into an IP address/netmasklength pair */
202         *p = 0;
203         goodaddr = interpret_string_addr(&ss, token, 0);
204         *p++ = '/';
205
206         if (!goodaddr) {
207                 DEBUG(2,("interpret_interface: "
208                         "can't determine interface for %s\n",
209                         token));
210                 return;
211         }
212
213         if (strlen(p) > 2) {
214                 goodaddr = interpret_string_addr(&ss_mask, p, 0);
215                 if (!goodaddr) {
216                         DEBUG(2,("interpret_interface: "
217                                 "can't determine netmask from %s\n",
218                                 p));
219                         return;
220                 }
221         } else {
222                 char *endp = NULL;
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",
227                                 p));
228                         return;
229                 }
230                 if (!make_netmask(&ss_mask, &ss, val)) {
231                         DEBUG(2,("interpret_interface: "
232                                 "can't apply netmask value %lu from %s\n",
233                                 val,
234                                 p));
235                         return;
236                 }
237         }
238
239         make_bcast(&ss_bcast, &ss, &ss_mask);
240         make_net(&ss_net, &ss, &ss_mask);
241
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
251                                  * best.... */
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",
258                                         p,
259                                         probed_ifaces[i].name));
260                                 add_interface(mem_ctx, &probed_ifaces[i],
261                                               local_interfaces);
262                                 probed_ifaces[i].netmask = saved_mask;
263                                 return;
264                         }
265                 }
266                 DEBUG(2,("interpret_interface: Can't determine ip for "
267                         "broadcast address %s\n",
268                         token));
269                 return;
270         }
271
272         /* Just fake up the interface definition. User knows best. */
273
274         DEBUG(2,("interpret_interface: Adding interface %s\n",
275                 token));
276
277         ZERO_STRUCT(ifs);
278         (void)strlcpy(ifs.name, token, sizeof(ifs.name));
279         ifs.flags = IFF_BROADCAST;
280         ifs.ip = ss;
281         ifs.netmask = ss_mask;
282         ifs.bcast = ss_bcast;
283         add_interface(mem_ctx, &ifs,
284                       local_interfaces);
285 }
286
287
288 /**
289 load the list of network interfaces
290 **/
291 void load_interface_list(TALLOC_CTX *mem_ctx, const char **interfaces, struct interface **local_interfaces)
292 {
293         const char **ptr = interfaces;
294         int i;
295         struct iface_struct *ifaces;
296         int total_probed;
297
298         *local_interfaces = NULL;
299
300         /* probe the kernel for interfaces */
301         total_probed = get_interfaces(mem_ctx, &ifaces);
302
303         /* if we don't have a interfaces line then use all interfaces
304            except loopback */
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"));
308                 }
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);
312                         }
313                 }
314         }
315
316         while (ptr && *ptr) {
317                 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
318                 ptr++;
319         }
320
321         if (!*local_interfaces) {
322                 DEBUG(0,("WARNING: no network interfaces found\n"));
323         }
324         talloc_free(ifaces);
325 }
326
327 /**
328   how many interfaces do we have
329   **/
330 int iface_list_count(struct interface *ifaces)
331 {
332         int ret = 0;
333         struct interface *i;
334
335         for (i=ifaces;i;i=i->next)
336                 ret++;
337         return ret;
338 }
339
340 /**
341   return IP of the Nth interface
342   **/
343 const char *iface_list_n_ip(struct interface *ifaces, int n)
344 {
345         struct interface *i;
346   
347         for (i=ifaces;i && n;i=i->next)
348                 n--;
349
350         if (i) {
351                 return i->ip_s;
352         }
353         return NULL;
354 }
355
356 /**
357   return bcast of the Nth interface
358   **/
359 const char *iface_list_n_bcast(struct interface *ifaces, int n)
360 {
361         struct interface *i;
362   
363         for (i=ifaces;i && n;i=i->next)
364                 n--;
365
366         if (i) {
367                 return i->bcast_s;
368         }
369         return NULL;
370 }
371
372 /**
373   return netmask of the Nth interface
374   **/
375 const char *iface_list_n_netmask(struct interface *ifaces, int n)
376 {
377         struct interface *i;
378   
379         for (i=ifaces;i && n;i=i->next)
380                 n--;
381
382         if (i) {
383                 return i->nmask_s;
384         }
385         return NULL;
386 }
387
388 /**
389   return the local IP address that best matches a destination IP, or
390   our first interface if none match
391 */
392 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
393 {
394         struct interface *iface;
395         struct sockaddr_storage ss;
396
397         if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
398                 return iface_list_n_ip(ifaces, 0);
399         }
400         iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
401         if (iface) {
402                 return iface->ip_s;
403         }
404         return iface_list_n_ip(ifaces, 0);
405 }
406
407 /**
408   return true if an IP is one one of our local networks
409 */
410 bool iface_list_is_local(struct interface *ifaces, const char *dest)
411 {
412         struct sockaddr_storage ss;
413
414         if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
415                 return false;
416         }
417         if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
418                 return true;
419         }
420         return false;
421 }
422
423 /**
424   return true if a IP matches a IP/netmask pair
425 */
426 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
427 {
428         return same_net_v4(interpret_addr2(ip1),
429                         interpret_addr2(ip2),
430                         interpret_addr2(netmask));
431 }
432
433 /**
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
437 */
438 const char **iface_list_wildcard(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
439 {
440         const char **ret;
441         const char *socket_address;
442
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);
447                 return ret;
448         }
449
450         ret = (const char **)str_list_make(mem_ctx, "0.0.0.0", NULL);
451         if (ret == NULL) return NULL;
452
453 #ifdef HAVE_IPV6
454         return str_list_add(ret, "::");
455 #endif
456
457         return ret;
458 }