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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/network.h"
25 #include "lib/netif/netif.h"
26 #include "dlinklist.h"
28 /** used for network interfaces */
30 struct interface *next, *prev;
32 struct ipv4_addr nmask;
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 static struct ipv4_addr tov4(struct in_addr in)
54 /****************************************************************************
55 Try and find an interface that matches an ip. If we cannot, return NULL
56 **************************************************************************/
57 static struct interface *iface_find(struct in_addr ip, BOOL CheckMask)
60 if (is_zero_ip(tov4(ip))) return local_interfaces;
62 for (i=local_interfaces;i;i=i->next)
64 if (same_net(i->ip,tov4(ip),i->nmask)) return i;
65 } else if (i->ip.addr == ip.s_addr) return i;
71 /****************************************************************************
72 add an interface to the linked list of interfaces
73 ****************************************************************************/
74 static void add_interface(struct in_addr ip, struct in_addr nmask)
76 struct interface *iface;
77 struct ipv4_addr bcast;
78 if (iface_find(ip, False)) {
79 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
83 iface = talloc(local_interfaces, struct interface);
89 iface->nmask = tov4(nmask);
90 bcast.addr = MKBCADDR(iface->ip.addr, iface->nmask.addr);
92 /* keep string versions too, to avoid people tripping over the implied
93 static in sys_inet_ntoa() */
94 iface->ip_s = talloc_strdup(iface, sys_inet_ntoa(iface->ip));
95 iface->nmask_s = talloc_strdup(iface, sys_inet_ntoa(iface->nmask));
97 if (nmask.s_addr != ~0) {
98 iface->bcast_s = talloc_strdup(iface, sys_inet_ntoa(bcast));
101 DLIST_ADD_END(local_interfaces, iface, struct interface *);
103 DEBUG(2,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s));
109 interpret a single element from a interfaces= config line
111 This handles the following different forms:
113 1) wildcard interface name
119 static void interpret_interface(const char *token,
120 struct iface_struct *probed_ifaces,
123 struct in_addr ip, nmask;
130 /* first check if it is an interface name */
131 for (i=0;i<total_probed;i++) {
132 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
133 add_interface(probed_ifaces[i].ip,
134 probed_ifaces[i].netmask);
140 /* maybe it is a DNS name */
141 p = strchr_m(token,'/');
143 /* don't try to do dns lookups on wildcard names */
144 if (strpbrk(token, "*?") != NULL) {
147 ip.s_addr = interpret_addr2(token).addr;
148 for (i=0;i<total_probed;i++) {
149 if (ip.s_addr == probed_ifaces[i].ip.s_addr) {
150 add_interface(probed_ifaces[i].ip,
151 probed_ifaces[i].netmask);
155 DEBUG(2,("can't determine netmask for %s\n", token));
159 /* parse it into an IP address/netmasklength pair */
162 ip.s_addr = interpret_addr2(token).addr;
165 nmask.s_addr = interpret_addr2(p).addr;
167 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
170 /* maybe the first component was a broadcast address */
171 if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
172 ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
173 for (i=0;i<total_probed;i++) {
174 if (same_net(tov4(ip), tov4(probed_ifaces[i].ip), tov4(nmask))) {
175 add_interface(probed_ifaces[i].ip, nmask);
179 DEBUG(2,("Can't determine ip for broadcast address %s\n", token));
183 add_interface(ip, nmask);
188 load the list of network interfaces
190 static void load_interfaces(void)
194 struct iface_struct ifaces[MAX_INTERFACES];
195 struct ipv4_addr loopback_ip;
198 if (local_interfaces != NULL) {
202 ptr = lp_interfaces();
203 loopback_ip = interpret_addr2("127.0.0.1");
205 /* probe the kernel for interfaces */
206 total_probed = get_interfaces(ifaces, MAX_INTERFACES);
208 /* if we don't have a interfaces line then use all interfaces
210 if (!ptr || !*ptr || !**ptr) {
211 if (total_probed <= 0) {
212 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
214 for (i=0;i<total_probed;i++) {
215 if (ifaces[i].ip.s_addr != loopback_ip.addr) {
216 add_interface(ifaces[i].ip,
222 while (ptr && *ptr) {
223 interpret_interface(*ptr, ifaces, total_probed);
227 if (!local_interfaces) {
228 DEBUG(0,("WARNING: no network interfaces found\n"));
234 unload the interfaces list, so it can be reloaded when needed
236 void unload_interfaces(void)
238 talloc_free(local_interfaces);
239 local_interfaces = NULL;
243 how many interfaces do we have
245 int iface_count(void)
252 for (i=local_interfaces;i;i=i->next)
258 return IP of the Nth interface
260 const char *iface_n_ip(int n)
266 for (i=local_interfaces;i && n;i=i->next)
276 return bcast of the Nth interface
278 const char *iface_n_bcast(int n)
284 for (i=local_interfaces;i && n;i=i->next)
294 return netmask of the Nth interface
296 const char *iface_n_netmask(int n)
302 for (i=local_interfaces;i && n;i=i->next)
312 return the local IP address that best matches a destination IP, or
313 our first interface if none match
315 const char *iface_best_ip(const char *dest)
317 struct interface *iface;
322 ip.s_addr = interpret_addr(dest);
323 iface = iface_find(ip, True);
327 return iface_n_ip(0);
331 return True if an IP is one one of our local networks
333 BOOL iface_is_local(const char *dest)
339 ip.s_addr = interpret_addr(dest);
340 if (iface_find(ip, True)) {
347 return True if a IP matches a IP/netmask pair
349 BOOL iface_same_net(const char *ip1, const char *ip2, const char *netmask)
351 return same_net(interpret_addr2(ip1),
352 interpret_addr2(ip2),
353 interpret_addr2(netmask));