r13504: add back in a comment noting fred as the contributor of the address
[bbaumbach/samba-autobuild/.git] / source4 / lib / netif / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/network.h"
25 #include "lib/netif/netif.h"
26 #include "dlinklist.h"
27
28 /* used for network interfaces */
29 struct interface {
30         struct interface *next, *prev;
31         struct ipv4_addr ip;
32         struct ipv4_addr bcast;
33         struct ipv4_addr nmask;
34         const char *ip_s;
35         const char *bcast_s;
36         const char *nmask_s;
37 };
38
39 static struct interface *local_interfaces;
40
41 #define ALLONES  ((uint32_t)0xFFFFFFFF)
42 /*
43   address construction based on a patch from fred@datalync.com
44 */
45 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
46 #define MKNETADDR(_IP, _NM) (_IP & _NM)
47
48 static struct ipv4_addr tov4(struct in_addr in)
49 {
50         struct ipv4_addr in2;
51         in2.addr = in.s_addr;
52         return in2;
53 }
54
55 /****************************************************************************
56 Try and find an interface that matches an ip. If we cannot, return NULL
57   **************************************************************************/
58 static struct interface *iface_find(struct in_addr ip, BOOL CheckMask)
59 {
60         struct interface *i;
61         if (is_zero_ip(tov4(ip))) return local_interfaces;
62
63         for (i=local_interfaces;i;i=i->next)
64                 if (CheckMask) {
65                         if (same_net(i->ip,tov4(ip),i->nmask)) return i;
66                 } else if (i->ip.addr == ip.s_addr) return i;
67
68         return NULL;
69 }
70
71
72 /****************************************************************************
73 add an interface to the linked list of interfaces
74 ****************************************************************************/
75 static void add_interface(struct in_addr ip, struct in_addr nmask)
76 {
77         struct interface *iface;
78         if (iface_find(ip, False)) {
79                 DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
80                 return;
81         }
82
83         if (nmask.s_addr == ~0) {
84                 DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip)));
85                 return;
86         }
87
88         iface = talloc(local_interfaces, struct interface);
89         if (!iface) return;
90         
91         ZERO_STRUCTPN(iface);
92
93         iface->ip = tov4(ip);
94         iface->nmask = tov4(nmask);
95         iface->bcast.addr = MKBCADDR(iface->ip.addr, iface->nmask.addr);
96
97         /* keep string versions too, to avoid people tripping over the implied
98            static in sys_inet_ntoa() */
99         iface->ip_s = talloc_strdup(iface, sys_inet_ntoa(iface->ip));
100         iface->bcast_s = talloc_strdup(iface, sys_inet_ntoa(iface->bcast));
101         iface->nmask_s = talloc_strdup(iface, sys_inet_ntoa(iface->nmask));
102
103         DLIST_ADD_END(local_interfaces, iface, struct interface *);
104
105         DEBUG(2,("added interface ip=%s bcast=%s nmask=%s\n", 
106                  iface->ip_s, iface->bcast_s, iface->nmask_s));
107 }
108
109
110
111 /****************************************************************************
112 interpret a single element from a interfaces= config line 
113
114 This handles the following different forms:
115
116 1) wildcard interface name
117 2) DNS name
118 3) IP/masklen
119 4) ip/mask
120 5) bcast/mask
121 ****************************************************************************/
122 static void interpret_interface(const char *token, 
123                                 struct iface_struct *probed_ifaces, 
124                                 int total_probed)
125 {
126         struct in_addr ip, nmask;
127         char *p;
128         int i, added=0;
129
130         ip.s_addr = 0;
131         nmask.s_addr = 0;
132         
133         /* first check if it is an interface name */
134         for (i=0;i<total_probed;i++) {
135                 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
136                         add_interface(probed_ifaces[i].ip,
137                                       probed_ifaces[i].netmask);
138                         added = 1;
139                 }
140         }
141         if (added) return;
142
143         /* maybe it is a DNS name */
144         p = strchr_m(token,'/');
145         if (!p) {
146                 /* don't try to do dns lookups on wildcard names */
147                 if (strpbrk(token, "*?") != NULL) {
148                         return;
149                 }
150                 ip.s_addr = interpret_addr2(token).addr;
151                 for (i=0;i<total_probed;i++) {
152                         if (ip.s_addr == probed_ifaces[i].ip.s_addr &&
153                             probed_ifaces[i].netmask.s_addr != ~0) {
154                                 add_interface(probed_ifaces[i].ip,
155                                               probed_ifaces[i].netmask);
156                                 return;
157                         }
158                 }
159                 DEBUG(2,("can't determine netmask for %s\n", token));
160                 return;
161         }
162
163         /* parse it into an IP address/netmasklength pair */
164         *p++ = 0;
165
166         ip.s_addr = interpret_addr2(token).addr;
167
168         if (strlen(p) > 2) {
169                 nmask.s_addr = interpret_addr2(p).addr;
170         } else {
171                 nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
172         }
173
174         /* maybe the first component was a broadcast address */
175         if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
176             ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
177                 for (i=0;i<total_probed;i++) {
178                         if (same_net(tov4(ip), tov4(probed_ifaces[i].ip), tov4(nmask))) {
179                                 add_interface(probed_ifaces[i].ip, nmask);
180                                 return;
181                         }
182                 }
183                 DEBUG(2,("Can't determine ip for broadcast address %s\n", token));
184                 return;
185         }
186
187         add_interface(ip, nmask);
188 }
189
190
191 /****************************************************************************
192 load the list of network interfaces
193 ****************************************************************************/
194 static void load_interfaces(void)
195 {
196         const char **ptr;
197         int i;
198         struct iface_struct ifaces[MAX_INTERFACES];
199         struct ipv4_addr loopback_ip;
200         int total_probed;
201
202         if (local_interfaces != NULL) {
203                 return;
204         }
205
206         ptr = lp_interfaces();
207         loopback_ip = interpret_addr2("127.0.0.1");
208
209         /* probe the kernel for interfaces */
210         total_probed = get_interfaces(ifaces, MAX_INTERFACES);
211
212         /* if we don't have a interfaces line then use all broadcast capable 
213            interfaces except loopback */
214         if (!ptr || !*ptr || !**ptr) {
215                 if (total_probed <= 0) {
216                         DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
217                 }
218                 for (i=0;i<total_probed;i++) {
219                         if (ifaces[i].netmask.s_addr != ~0 &&
220                             ifaces[i].ip.s_addr != loopback_ip.addr) {
221                                 add_interface(ifaces[i].ip, 
222                                               ifaces[i].netmask);
223                         }
224                 }
225         }
226
227         while (ptr && *ptr) {
228                 interpret_interface(*ptr, ifaces, total_probed);
229                 ptr++;
230         }
231
232         if (!local_interfaces) {
233                 DEBUG(0,("WARNING: no network interfaces found\n"));
234         }
235 }
236
237
238 /*
239   unload the interfaces list, so it can be reloaded when needed
240 */
241 void unload_interfaces(void)
242 {
243         talloc_free(local_interfaces);
244         local_interfaces = NULL;
245 }
246
247 /****************************************************************************
248   how many interfaces do we have
249   **************************************************************************/
250 int iface_count(void)
251 {
252         int ret = 0;
253         struct interface *i;
254
255         load_interfaces();
256
257         for (i=local_interfaces;i;i=i->next)
258                 ret++;
259         return ret;
260 }
261
262 /****************************************************************************
263   return IP of the Nth interface
264   **************************************************************************/
265 const char *iface_n_ip(int n)
266 {
267         struct interface *i;
268   
269         load_interfaces();
270
271         for (i=local_interfaces;i && n;i=i->next)
272                 n--;
273
274         if (i) {
275                 return i->ip_s;
276         }
277         return NULL;
278 }
279
280 /****************************************************************************
281   return bcast of the Nth interface
282   **************************************************************************/
283 const char *iface_n_bcast(int n)
284 {
285         struct interface *i;
286   
287         load_interfaces();
288
289         for (i=local_interfaces;i && n;i=i->next)
290                 n--;
291
292         if (i) {
293                 return i->bcast_s;
294         }
295         return NULL;
296 }
297
298 /****************************************************************************
299   return netmask of the Nth interface
300   **************************************************************************/
301 const char *iface_n_netmask(int n)
302 {
303         struct interface *i;
304   
305         load_interfaces();
306
307         for (i=local_interfaces;i && n;i=i->next)
308                 n--;
309
310         if (i) {
311                 return i->nmask_s;
312         }
313         return NULL;
314 }
315
316 /*
317   return the local IP address that best matches a destination IP, or
318   our first interface if none match
319 */
320 const char *iface_best_ip(const char *dest)
321 {
322         struct interface *iface;
323         struct in_addr ip;
324
325         load_interfaces();
326
327         ip.s_addr = interpret_addr(dest);
328         iface = iface_find(ip, True);
329         if (iface) {
330                 return iface->ip_s;
331         }
332         return iface_n_ip(0);
333 }
334
335 /*
336   return True if an IP is one one of our local networks
337 */
338 BOOL iface_is_local(const char *dest)
339 {
340         struct in_addr ip;
341
342         load_interfaces();
343
344         ip.s_addr = interpret_addr(dest);
345         if (iface_find(ip, True)) {
346                 return True;
347         }
348         return False;
349 }
350
351 /*
352   return True if a IP matches a IP/netmask pair
353 */
354 BOOL iface_same_net(const char *ip1, const char *ip2, const char *netmask)
355 {
356         return same_net(interpret_addr2(ip1),
357                         interpret_addr2(ip2),
358                         interpret_addr2(netmask));
359 }