2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <sys/ioctl.h>
25 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
26 #include <sys/sysctl.h>
27 #include <net/route.h>
28 #include <net/if_dl.h>
32 * By including this, we get any system dependencies. In particular,
33 * the pthreads for solaris requires the socket call to be mapped.
39 #include "rx/rx_kcommon.h"
48 /* only used for generating random noise */
50 afs_int32 rxi_tempAddr = 0; /* default attempt */
52 /* set the advisory noise */
54 rxi_setaddr(afs_int32 x)
59 /* get approx to net addr */
70 /* to satisfy those who call setaddr */
72 rxi_setaddr(afs_int32 x)
79 #if !defined(AFS_NT40_ENV)
80 /* For NT, rxi_getaddr has moved to rx_user.c. rxi_GetIfInfo is called by
81 * rx_Init which sets up the list of addresses for us.
86 /* Return our internet address as a long in network byte order. Returns zero
87 * if it can't find one.
92 afs_int32 buffer[1024];
95 count = rx_getAllAddr(buffer, 1024);
97 return buffer[0]; /* returns the first address */
104 #if !defined(KERNEL) || defined(UKERNEL)
107 #define MAX(A,B) (((A)<(B)) ? (B) : (A))
115 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
117 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
118 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
121 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
126 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
127 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
128 if ((rtinfo->rti_addrs & (1 << i)) == 0)
130 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
137 /* this function returns the total number of interface addresses
138 ** the buffer has to be passed in by the caller
140 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
142 rx_getAllAddr(afs_int32 buffer[], int maxSize)
146 struct if_msghdr *ifm, *nextifm;
147 struct ifa_msghdr *ifam;
148 struct sockaddr_dl *sdl;
149 struct rt_addrinfo info;
150 char *buf, *lim, *next;
151 int count = 0, addrcount = 0;
156 mib[3] = AF_INET; /* address family */
157 mib[4] = NET_RT_IFLIST;
159 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
161 if ((buf = malloc(needed)) == NULL)
163 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
170 ifm = (struct if_msghdr *)next;
171 if (ifm->ifm_type != RTM_IFINFO) {
172 printf("out of sync parsing NET_RT_IFLIST\n");
176 sdl = (struct sockaddr_dl *)(ifm + 1);
177 next += ifm->ifm_msglen;
181 nextifm = (struct if_msghdr *)next;
182 if (nextifm->ifm_type != RTM_NEWADDR)
185 ifam = (struct ifa_msghdr *)nextifm;
187 next += nextifm->ifm_msglen;
189 if ((ifm->ifm_flags & IFF_UP) == 0)
190 continue; /* not up */
191 if (ifm->ifm_flags & IFF_LOOPBACK) {
192 continue; /* skip aliased loopbacks as well. */
194 while (addrcount > 0) {
195 struct sockaddr_in *a;
197 info.rti_addrs = ifam->ifam_addrs;
199 /* Expand the compacted addresses */
200 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
202 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
204 a = info.rti_info[RTAX_IFA];
206 if (count >= maxSize) /* no more space */
207 printf("Too many interfaces..ignoring 0x%x\n",
210 buffer[count++] = a->sin_addr.s_addr;
212 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
220 rxi_getAllAddrMaskMtu(afs_int32 addrBuffer[], afs_int32 maskBuffer[],
221 afs_int32 mtuBuffer[], int maxSize)
227 struct if_msghdr *ifm, *nextifm;
228 struct ifa_msghdr *ifam;
229 struct sockaddr_dl *sdl;
230 struct rt_addrinfo info;
231 char *buf, *lim, *next;
232 int count = 0, addrcount = 0;
237 mib[3] = AF_INET; /* address family */
238 mib[4] = NET_RT_IFLIST;
240 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
242 if ((buf = malloc(needed)) == NULL)
244 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
248 s = socket(PF_INET, SOCK_DGRAM, 0);
254 ifm = (struct if_msghdr *)next;
255 if (ifm->ifm_type != RTM_IFINFO) {
256 printf("out of sync parsing NET_RT_IFLIST\n");
260 sdl = (struct sockaddr_dl *)(ifm + 1);
261 next += ifm->ifm_msglen;
265 nextifm = (struct if_msghdr *)next;
266 if (nextifm->ifm_type != RTM_NEWADDR)
269 ifam = (struct ifa_msghdr *)nextifm;
271 next += nextifm->ifm_msglen;
273 if ((ifm->ifm_flags & IFF_UP) == 0)
274 continue; /* not up */
275 while (addrcount > 0) {
276 struct sockaddr_in *a;
278 info.rti_addrs = ifam->ifam_addrs;
280 /* Expand the compacted addresses */
281 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
283 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
285 a = info.rti_info[RTAX_IFA];
287 if (count >= maxSize) { /* no more space */
288 printf("Too many interfaces..ignoring 0x%x\n",
293 addrBuffer[count] = a->sin_addr.s_addr;
294 a = info.rti_info[RTAX_NETMASK];
296 maskBuffer[count] = a->sin_addr.s_addr;
298 maskBuffer[count] = htonl(0xffffffff);
299 memset(&ifr, sizeof(ifr), 0);
300 ifr.ifr_addr.sa_family = AF_INET;
301 strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
302 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
303 mtuBuffer[count] = htonl(1500);
305 mtuBuffer[count] = htonl(ifr.ifr_mtu);
309 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
317 rx_getAllAddr_internal(afs_int32 buffer[], int maxSize, int loopbacks)
320 int i, len, count = 0;
322 struct ifreq ifs[NIFS], *ifr;
323 struct sockaddr_in *a;
324 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
325 char *cp, *cplim, *cpnext; /* used only for AIX 41 */
328 s = socket(AF_INET, SOCK_DGRAM, 0);
331 ifc.ifc_len = sizeof(ifs);
332 ifc.ifc_buf = (caddr_t) ifs;
333 i = ioctl(s, SIOCGIFCONF, &ifc);
336 len = ifc.ifc_len / sizeof(struct ifreq);
339 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
340 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
341 ifc.ifc_len = sizeof(ifs);
342 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
344 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
345 cp += _SIZEOF_ADDR_IFREQ(*ifr)
350 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
355 for (i = 0; i < len; ++i)
358 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
359 ifr = (struct ifreq *)cp;
363 a = (struct sockaddr_in *)&ifr->ifr_addr;
365 cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
367 if (a->sin_family != AF_INET)
369 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
370 perror("SIOCGIFFLAGS");
371 continue; /* ignore this address */
373 if (a->sin_addr.s_addr != 0) {
375 if (a->sin_addr.s_addr == htonl(0x7f000001))
376 continue; /* skip loopback address as well. */
378 if (ifr->ifr_flags & IFF_LOOPBACK)
379 continue; /* skip aliased loopbacks as well. */
381 if (count >= maxSize) /* no more space */
382 printf("Too many interfaces..ignoring 0x%x\n",
385 buffer[count++] = a->sin_addr.s_addr;
393 rx_getAllAddr(afs_int32 buffer[], int maxSize)
395 return rx_getAllAddr_internal(buffer, maxSize, 0);
398 /* this function returns the total number of interface addresses
399 * the buffer has to be passed in by the caller. It also returns
400 * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
401 * gets the mask which is then passed into the kernel and is used
402 * by afsi_SetServerIPRank().
405 rxi_getAllAddrMaskMtu(afs_int32 addrBuffer[], afs_int32 maskBuffer[],
406 afs_int32 mtuBuffer[], int maxSize)
409 int i, len, count = 0;
411 struct ifreq ifs[NIFS], *ifr;
412 struct sockaddr_in *a;
413 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
414 char *cp, *cplim; /* used only for AIX 41 */
417 #if !defined(AFS_USERSPACE_IP_ADDR)
418 count = rx_getAllAddr_internal(addrBuffer, 1024, 1);
419 for (i = 0; i < count; i++) {
420 maskBuffer[i] = htonl(0xffffffff);
421 mtuBuffer[i] = htonl(1500);
424 #else /* AFS_USERSPACE_IP_ADDR */
425 s = socket(AF_INET, SOCK_DGRAM, 0);
429 ifc.ifc_len = sizeof(ifs);
430 ifc.ifc_buf = (caddr_t) ifs;
431 i = ioctl(s, SIOCGIFCONF, &ifc);
436 len = ifc.ifc_len / sizeof(struct ifreq);
440 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
441 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
442 ifc.ifc_len = sizeof(ifs);
443 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
445 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
446 ifr = (struct ifreq *)cp;
448 for (i = 0; i < len; ++i) {
451 a = (struct sockaddr_in *)&ifr->ifr_addr;
452 if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
454 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
455 perror("SIOCGIFFLAGS");
456 continue; /* ignore this address */
459 if (a->sin_addr.s_addr == htonl(0x7f000001) )
460 continue; /* skip loopback address as well. */
462 if (count >= maxSize) { /* no more space */
463 printf("Too many interfaces..ignoring 0x%x\n",
468 addrBuffer[count] = a->sin_addr.s_addr;
470 if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
471 perror("SIOCGIFNETMASK");
472 maskBuffer[count] = htonl(0xffffffff);
474 maskBuffer[count] = (((struct sockaddr_in *)
475 (&ifr->ifr_addr))->sin_addr).s_addr;
478 mtuBuffer[count] = htonl(1500);
480 if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
481 perror("SIOCGIFMTU");
483 mtuBuffer[count] = htonl(ifr->ifr_metric);
485 #endif /* SIOCGIFMTU */
487 if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
488 perror("SIOCRIPMTU");
490 mtuBuffer[count] = htonl(ifr->ifr_metric);
492 #endif /* SIOCRIPMTU */
499 #endif /* AFS_USERSPACE_IP_ADDR */
503 #endif /* ! AFS_NT40_ENV */
504 #endif /* !KERNEL || UKERNEL */
506 #endif /* !AFS_DJGPP_ENV */