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"
27 /** used for network interfaces */
29 struct interface *next, *prev;
31 struct ipv4_addr nmask;
37 static struct interface *local_interfaces;
39 #define ALLONES ((uint32_t)0xFFFFFFFF)
41 address construction based on a patch from fred@datalync.com
43 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
44 #define MKNETADDR(_IP, _NM) (_IP & _NM)
46 static struct ipv4_addr tov4(struct in_addr in)
53 /****************************************************************************
54 Try and find an interface that matches an ip. If we cannot, return NULL
55 **************************************************************************/
56 static struct interface *iface_find(struct in_addr ip, BOOL CheckMask)
59 if (is_zero_ip(tov4(ip))) return local_interfaces;
61 for (i=local_interfaces;i;i=i->next)
63 if (same_net(i->ip,tov4(ip),i->nmask)) return i;
64 } else if (i->ip.addr == ip.s_addr) return i;
70 /****************************************************************************
71 add an interface to the linked list of interfaces
72 ****************************************************************************/
73 static void add_interface(struct in_addr ip, struct in_addr nmask)
75 struct interface *iface;
76 struct ipv4_addr bcast;
77 if (iface_find(ip, False)) {
78 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
82 iface = talloc(local_interfaces, struct interface);
88 iface->nmask = tov4(nmask);
89 bcast.addr = MKBCADDR(iface->ip.addr, iface->nmask.addr);
91 /* keep string versions too, to avoid people tripping over the implied
92 static in sys_inet_ntoa() */
93 iface->ip_s = talloc_strdup(iface, sys_inet_ntoa(iface->ip));
94 iface->nmask_s = talloc_strdup(iface, sys_inet_ntoa(iface->nmask));
96 if (nmask.s_addr != ~0) {
97 iface->bcast_s = talloc_strdup(iface, sys_inet_ntoa(bcast));
100 DLIST_ADD_END(local_interfaces, iface, struct interface *);
102 DEBUG(2,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s));
108 interpret a single element from a interfaces= config line
110 This handles the following different forms:
112 1) wildcard interface name
118 static void interpret_interface(const char *token,
119 struct iface_struct *probed_ifaces,
122 struct in_addr ip, nmask;
129 /* first check if it is an interface name */
130 for (i=0;i<total_probed;i++) {
131 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
132 add_interface(probed_ifaces[i].ip,
133 probed_ifaces[i].netmask);
139 /* maybe it is a DNS name */
140 p = strchr_m(token,'/');
142 /* don't try to do dns lookups on wildcard names */
143 if (strpbrk(token, "*?") != NULL) {
146 ip.s_addr = interpret_addr2(token).addr;
147 for (i=0;i<total_probed;i++) {
148 if (ip.s_addr == probed_ifaces[i].ip.s_addr) {
149 add_interface(probed_ifaces[i].ip,
150 probed_ifaces[i].netmask);
154 DEBUG(2,("can't determine netmask for %s\n", token));
158 /* parse it into an IP address/netmasklength pair */
161 ip.s_addr = interpret_addr2(token).addr;
164 nmask.s_addr = interpret_addr2(p).addr;
166 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
169 /* maybe the first component was a broadcast address */
170 if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
171 ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
172 for (i=0;i<total_probed;i++) {
173 if (same_net(tov4(ip), tov4(probed_ifaces[i].ip), tov4(nmask))) {
174 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);
187 load the list of network interfaces
189 static void load_interfaces(void)
193 struct iface_struct ifaces[MAX_INTERFACES];
194 struct ipv4_addr loopback_ip;
197 if (local_interfaces != NULL) {
201 ptr = lp_interfaces();
202 loopback_ip = interpret_addr2("127.0.0.1");
204 /* probe the kernel for interfaces */
205 total_probed = get_interfaces(ifaces, MAX_INTERFACES);
207 /* if we don't have a interfaces line then use all interfaces
209 if (!ptr || !*ptr || !**ptr) {
210 if (total_probed <= 0) {
211 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
213 for (i=0;i<total_probed;i++) {
214 if (ifaces[i].ip.s_addr != loopback_ip.addr) {
215 add_interface(ifaces[i].ip,
221 while (ptr && *ptr) {
222 interpret_interface(*ptr, ifaces, total_probed);
226 if (!local_interfaces) {
227 DEBUG(0,("WARNING: no network interfaces found\n"));
233 unload the interfaces list, so it can be reloaded when needed
235 void unload_interfaces(void)
237 talloc_free(local_interfaces);
238 local_interfaces = NULL;
242 how many interfaces do we have
244 int iface_count(void)
251 for (i=local_interfaces;i;i=i->next)
257 return IP of the Nth interface
259 const char *iface_n_ip(int n)
265 for (i=local_interfaces;i && n;i=i->next)
275 return bcast of the Nth interface
277 const char *iface_n_bcast(int n)
283 for (i=local_interfaces;i && n;i=i->next)
293 return netmask of the Nth interface
295 const char *iface_n_netmask(int n)
301 for (i=local_interfaces;i && n;i=i->next)
311 return the local IP address that best matches a destination IP, or
312 our first interface if none match
314 const char *iface_best_ip(const char *dest)
316 struct interface *iface;
321 ip.s_addr = interpret_addr(dest);
322 iface = iface_find(ip, True);
326 return iface_n_ip(0);
330 return True if an IP is one one of our local networks
332 BOOL iface_is_local(const char *dest)
338 ip.s_addr = interpret_addr(dest);
339 if (iface_find(ip, True)) {
346 return True if a IP matches a IP/netmask pair
348 BOOL iface_same_net(const char *ip1, const char *ip2, const char *netmask)
350 return same_net(interpret_addr2(ip1),
351 interpret_addr2(ip2),
352 interpret_addr2(netmask));