2 Unix SMB/CIFS implementation.
4 multiple interface handling
6 Copyright (C) Andrew Tridgell 1992-2005
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.
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.
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/>.
23 #include "system/network.h"
24 #include "lib/socket/netif.h"
25 #include "lib/util/dlinklist.h"
26 #include "param/param.h"
28 /** used for network interfaces */
30 struct interface *next, *prev;
38 static struct interface *local_interfaces;
40 #define ALLONES ((uint32_t)0xFFFFFFFF)
42 address construction based on a patch from fred@datalync.com
44 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
45 #define MKNETADDR(_IP, _NM) (_IP & _NM)
47 /****************************************************************************
48 Try and find an interface that matches an ip. If we cannot, return NULL
49 **************************************************************************/
50 static struct interface *iface_find(struct interface *interfaces,
51 struct in_addr ip, bool CheckMask)
54 if (is_zero_ip(ip)) return interfaces;
56 for (i=interfaces;i;i=i->next)
58 if (same_net(i->ip,ip,i->nmask)) return i;
59 } else if (i->ip.s_addr == ip.s_addr) return i;
65 /****************************************************************************
66 add an interface to the linked list of interfaces
67 ****************************************************************************/
68 static void add_interface(struct in_addr ip, struct in_addr nmask, struct interface **interfaces)
70 struct interface *iface;
73 if (iface_find(*interfaces, ip, false)) {
74 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
78 iface = talloc(*interfaces == NULL ? talloc_autofree_context() : *interfaces, struct interface);
86 bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr);
88 /* keep string versions too, to avoid people tripping over the implied
89 static in inet_ntoa() */
90 iface->ip_s = talloc_strdup(iface, inet_ntoa(iface->ip));
91 iface->nmask_s = talloc_strdup(iface, inet_ntoa(iface->nmask));
93 if (nmask.s_addr != ~0) {
94 iface->bcast_s = talloc_strdup(iface, inet_ntoa(bcast));
97 DLIST_ADD_END(*interfaces, iface, struct interface *);
99 DEBUG(2,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s));
105 interpret a single element from a interfaces= config line
107 This handles the following different forms:
109 1) wildcard interface name
115 static void interpret_interface(const char *token,
116 struct iface_struct *probed_ifaces,
119 struct in_addr ip, nmask;
126 /* first check if it is an interface name */
127 for (i=0;i<total_probed;i++) {
128 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
129 add_interface(probed_ifaces[i].ip,
130 probed_ifaces[i].netmask,
137 /* maybe it is a DNS name */
138 p = strchr_m(token,'/');
140 /* don't try to do dns lookups on wildcard names */
141 if (strpbrk(token, "*?") != NULL) {
144 ip.s_addr = interpret_addr2(token).s_addr;
145 for (i=0;i<total_probed;i++) {
146 if (ip.s_addr == probed_ifaces[i].ip.s_addr) {
147 add_interface(probed_ifaces[i].ip,
148 probed_ifaces[i].netmask,
153 DEBUG(2,("can't determine netmask for %s\n", token));
157 /* parse it into an IP address/netmasklength pair */
160 ip.s_addr = interpret_addr2(token).s_addr;
163 nmask.s_addr = interpret_addr2(p).s_addr;
165 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
168 /* maybe the first component was a broadcast address */
169 if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
170 ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
171 for (i=0;i<total_probed;i++) {
172 if (same_net(ip, probed_ifaces[i].ip, nmask)) {
173 add_interface(probed_ifaces[i].ip, nmask,
178 DEBUG(2,("Can't determine ip for broadcast address %s\n", token));
182 add_interface(ip, nmask, &local_interfaces);
187 load the list of network interfaces
189 static void load_interfaces(const char **interfaces)
191 const char **ptr = interfaces;
193 struct iface_struct ifaces[MAX_INTERFACES];
194 struct in_addr loopback_ip;
197 if (local_interfaces != NULL) {
201 loopback_ip = interpret_addr2("127.0.0.1");
203 /* probe the kernel for interfaces */
204 total_probed = get_interfaces(ifaces, MAX_INTERFACES);
206 /* if we don't have a interfaces line then use all interfaces
208 if (!ptr || !*ptr || !**ptr) {
209 if (total_probed <= 0) {
210 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
212 for (i=0;i<total_probed;i++) {
213 if (ifaces[i].ip.s_addr != loopback_ip.s_addr) {
214 add_interface(ifaces[i].ip,
215 ifaces[i].netmask, &local_interfaces);
220 while (ptr && *ptr) {
221 interpret_interface(*ptr, ifaces, total_probed);
225 if (!local_interfaces) {
226 DEBUG(0,("WARNING: no network interfaces found\n"));
232 unload the interfaces list, so it can be reloaded when needed
234 void unload_interfaces(void)
236 talloc_free(local_interfaces);
237 local_interfaces = NULL;
241 how many interfaces do we have
243 int iface_count(struct loadparm_context *lp_ctx)
248 load_interfaces(lp_interfaces(lp_ctx));
250 for (i=local_interfaces;i;i=i->next)
256 return IP of the Nth interface
258 const char *iface_n_ip(struct loadparm_context *lp_ctx, int n)
262 load_interfaces(lp_interfaces(lp_ctx));
264 for (i=local_interfaces;i && n;i=i->next)
274 return bcast of the Nth interface
276 const char *iface_n_bcast(struct loadparm_context *lp_ctx, int n)
280 load_interfaces(lp_interfaces(lp_ctx));
282 for (i=local_interfaces;i && n;i=i->next)
292 return netmask of the Nth interface
294 const char *iface_n_netmask(struct loadparm_context *lp_ctx, int n)
298 load_interfaces(lp_interfaces(lp_ctx));
300 for (i=local_interfaces;i && n;i=i->next)
310 return the local IP address that best matches a destination IP, or
311 our first interface if none match
313 const char *iface_best_ip(struct loadparm_context *lp_ctx, const char *dest)
315 struct interface *iface;
318 load_interfaces(lp_interfaces(lp_ctx));
320 ip.s_addr = interpret_addr(dest);
321 iface = iface_find(local_interfaces, ip, true);
325 return iface_n_ip(lp_ctx, 0);
329 return true if an IP is one one of our local networks
331 bool iface_is_local(struct loadparm_context *lp_ctx, const char *dest)
335 load_interfaces(lp_interfaces(lp_ctx));
337 ip.s_addr = interpret_addr(dest);
338 if (iface_find(local_interfaces, ip, true)) {
345 return true if a IP matches a IP/netmask pair
347 bool iface_same_net(const char *ip1, const char *ip2, const char *netmask)
349 return same_net(interpret_addr2(ip1),
350 interpret_addr2(ip2),
351 interpret_addr2(netmask));