2 Unix SMB/Netbios implementation.
4 multiple interface handling
5 Copyright (C) Andrew Tridgell 1992-1995
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
27 struct in_addr ipzero;
28 static struct in_addr default_ip;
29 static struct in_addr default_bcast;
30 static struct in_addr default_nmask;
31 static BOOL got_ip=False;
32 static BOOL got_bcast=False;
33 static BOOL got_nmask=False;
35 static struct interface {
36 struct interface *next;
41 struct interface *last_iface;
43 /****************************************************************************
44 calculate the default netmask for an address
45 ****************************************************************************/
46 static void default_netmask(struct in_addr *inm, struct in_addr *iad)
48 unsigned long ad = ntohl(iad->s_addr);
51 ** Guess a netmask based on the class of the IP address given.
53 if ( (ad & 0x80000000) == 0 ) {
56 } else if ( (ad & 0xC0000000) == 0x80000000 ) {
59 } else if ( (ad & 0xE0000000) == 0xC0000000 ) {
63 /* class D or E; netmask doesn't make much sense - guess 4 bits */
66 inm->s_addr = htonl(nm);
70 /****************************************************************************
71 get the broadcast address for our address
72 (troyer@saifr00.ateng.az.honeywell.com)
73 ****************************************************************************/
74 static void get_broadcast(struct in_addr *if_ipaddr,
75 struct in_addr *if_bcast,
76 struct in_addr *if_nmask)
79 #ifndef NO_GET_BROADCAST
80 int sock = -1; /* AF_INET raw socket desc */
82 struct ifreq *ifr=NULL;
89 #elif defined(USE_IFREQ)
91 struct strioctl strioctl;
98 /* get a default netmask and broadcast */
99 default_netmask(if_nmask, if_ipaddr);
101 #ifndef NO_GET_BROADCAST
102 /* Create a socket to the INET kernel. */
104 if ((sock = socket(AF_INET, SOCK_RAW, PF_INET )) < 0)
106 if ((sock = socket(AF_INET, SOCK_DGRAM, 0 )) < 0)
109 DEBUG(0,( "Unable to open socket to get broadcast address\n"));
113 /* Get a list of the configured interfaces */
115 /* This is part of SCO Openserver 5: The ioctls are no longer part
116 if the lower level STREAMS interface glue. They are now real
119 if (ioctl(sock, SIOCGIFANUM, &n_interfaces) < 0) {
120 DEBUG(0,( "SIOCGIFANUM: %s\n", strerror(errno)));
122 DEBUG(0,( "number of interfaces returned is: %d\n", n_interfaces));
124 ifc.ifc_len = sizeof(struct ifreq) * n_interfaces;
125 ifc.ifc_buf = (caddr_t) alloca(ifc.ifc_len);
127 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
128 DEBUG(0, ( "SIOCGIFCONF: %s\n", strerror(errno)));
132 for (i = 0; i < n_interfaces; ++i) {
133 if (if_ipaddr->s_addr ==
134 ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr.s_addr) {
141 #elif defined(USE_IFREQ)
142 ifc = (struct ifconf *)buff;
143 ifc->ifc_len = BUFSIZ - sizeof(struct ifconf);
144 strioctl.ic_cmd = SIOCGIFCONF;
145 strioctl.ic_dp = (char *)ifc;
146 strioctl.ic_len = sizeof(buff);
147 if (ioctl(sock, I_STR, &strioctl) < 0) {
148 DEBUG(0,( "I_STR/SIOCGIFCONF: %s\n", strerror(errno)));
150 ifr = (struct ifreq *)ifc->ifc_req;
152 /* Loop through interfaces, looking for given IP address */
153 for (i = ifc->ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) {
154 if (if_ipaddr->s_addr ==
155 (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
161 #elif defined(__FreeBSD__) || defined(NETBSD)
162 ifc.ifc_len = sizeof(buff);
164 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
165 DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno)));
168 /* Loop through interfaces, looking for given IP address */
171 if (if_ipaddr->s_addr ==
172 (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
176 i -= ifr->ifr_addr.sa_len + IFNAMSIZ;
177 ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ);
181 ifc.ifc_len = sizeof(buff);
183 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
184 DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno)));
188 /* Loop through interfaces, looking for given IP address */
189 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) {
191 if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break;
193 if (if_ipaddr->s_addr ==
194 (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
203 DEBUG(0,("No interface found for address %s\n", inet_ntoa(*if_ipaddr)));
205 /* Get the netmask address from the kernel */
209 strioctl.ic_cmd = SIOCGIFNETMASK;
210 strioctl.ic_dp = (char *)&ifreq;
211 strioctl.ic_len = sizeof(struct ifreq);
212 if (ioctl(sock, I_STR, &strioctl) < 0)
213 DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno)));
215 *if_nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
217 if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0)
218 DEBUG(0,("SIOCGIFNETMASK failed\n"));
220 *if_nmask = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
223 DEBUG(4,("Netmask for %s = %s\n", ifr->ifr_name,
224 inet_ntoa(*if_nmask)));
232 /* sanity check on the netmask */
234 unsigned long nm = ntohl(if_nmask->s_addr);
235 if ((nm >> 24) != 0xFF) {
236 DEBUG(0,("Impossible netmask %s - using defaults\n",inet_ntoa(*if_nmask)));
237 default_netmask(if_nmask, if_ipaddr);
241 /* derive the broadcast assuming a 1's broadcast, as this is what
242 all MS operating systems do, we have to comply even if the unix
243 box is setup differently */
245 unsigned long ad = ntohl(if_ipaddr->s_addr);
246 unsigned long nm = ntohl(if_nmask->s_addr);
247 unsigned long bc = (ad & nm) | (0xffffffff & ~nm);
248 if_bcast->s_addr = htonl(bc);
251 DEBUG(4,("Derived broadcast address %s\n", inet_ntoa(*if_bcast)));
252 } /* get_broadcast */
257 /****************************************************************************
258 load a list of network interfaces
259 ****************************************************************************/
260 void load_interfaces(void)
262 char *s = lp_interfaces();
265 struct interface *iface;
268 ipzero = *interpret_addr2("0.0.0.0");
270 while (next_token(&ptr,token,NULL)) {
271 /* parse it into an IP address/netmasklength pair */
272 char *p = strchr(token,'/');
275 ip = *interpret_addr2(token);
277 /* maybe we already have it listed */
280 for (i=interfaces;i;i=i->next)
281 if (ip_equal(ip,i->ip)) break;
285 iface = (struct interface *)malloc(sizeof(*iface));
292 iface->nmask = *interpret_addr2(p+1);
294 iface->nmask.s_addr = htonl(~((1<<(32-atoi(p+1)))-1));
296 default_netmask(&iface->nmask,&iface->ip);
298 iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr;
304 last_iface->next = iface;
307 DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip)));
308 DEBUG(1,("bcast=%s ",inet_ntoa(iface->bcast)));
309 DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask)));
312 if (interfaces) return;
314 /* setup a default interface */
315 iface = (struct interface *)malloc(sizeof(*iface));
319 iface->ip = default_ip;
321 get_myname(NULL,&iface->ip);
325 iface->bcast = default_bcast;
327 get_broadcast(&iface->ip,&iface->bcast,&iface->nmask);
331 iface->nmask = default_nmask;
332 iface->bcast.s_addr = iface->ip.s_addr | ~iface->nmask.s_addr;
335 if (iface->bcast.s_addr != (iface->ip.s_addr | ~iface->nmask.s_addr)) {
336 DEBUG(2,("Warning: inconsistant interface %s\n",inet_ntoa(iface->ip)));
339 interfaces = last_iface = iface;
341 DEBUG(1,("Added interface ip=%s ",inet_ntoa(iface->ip)));
342 DEBUG(1,("bcast=%s ",inet_ntoa(iface->bcast)));
343 DEBUG(1,("nmask=%s\n",inet_ntoa(iface->nmask)));
347 /****************************************************************************
348 override the defaults
349 **************************************************************************/
350 void iface_set_default(char *ip,char *bcast,char *nmask)
354 default_ip = *interpret_addr2(ip);
359 default_bcast = *interpret_addr2(bcast);
364 default_nmask = *interpret_addr2(nmask);
369 /****************************************************************************
370 check if an IP is one of mine
371 **************************************************************************/
372 BOOL ismyip(struct in_addr ip)
375 for (i=interfaces;i;i=i->next)
376 if (ip_equal(i->ip,ip)) return True;
380 /****************************************************************************
381 check if a bcast is one of mine
382 **************************************************************************/
383 BOOL ismybcast(struct in_addr bcast)
386 for (i=interfaces;i;i=i->next)
387 if (ip_equal(i->bcast,bcast)) return True;
391 static struct interface *iface_find(struct in_addr ip)
394 if (zero_ip(ip)) return interfaces;
396 for (i=interfaces;i;i=i->next)
397 if (same_net(i->ip,ip,i->nmask)) return i;
403 /* these 3 functions return the ip/bcast/nmask for the interface
404 most appropriate for the given ip address */
406 struct in_addr *iface_bcast(struct in_addr ip)
408 return(&iface_find(ip)->bcast);
411 struct in_addr *iface_nmask(struct in_addr ip)
413 return(&iface_find(ip)->nmask);
416 struct in_addr *iface_ip(struct in_addr ip)
418 return(&iface_find(ip)->ip);