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