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>
18 # if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
19 # include <sys/sysctl.h>
20 # ifndef AFS_ARM_DARWIN_ENV
21 # include <net/route.h>
23 # include <net/if_dl.h>
27 * By including this, we get any system dependencies. In particular,
28 * the pthreads for solaris requires the socket call to be mapped.
31 # include "rx_globals.h"
32 # endif /* AFS_NT40_ENV */
34 # include "rx/rx_kcommon.h"
42 #if defined(AFS_USR_DFBSD_ENV)
44 # include <sys/sockio.h>
48 /* only used for generating random noise */
50 afs_uint32 rxi_tempAddr = 0; /* default attempt */
52 /* set the advisory noise */
54 rxi_setaddr(afs_uint32 x)
59 /* get approx to net addr */
70 /* to satisfy those who call setaddr */
72 rxi_setaddr(afs_uint32 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_uint32 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))
114 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
116 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
117 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
120 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
125 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
126 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
127 if ((rtinfo->rti_addrs & (1 << i)) == 0)
129 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
136 rxi_IsLoopbackIface(struct sockaddr_in *a, unsigned long flags)
138 afs_uint32 addr = ntohl(a->sin_addr.s_addr);
139 if (rx_IsLoopbackAddr(addr)) {
142 if ((flags & IFF_LOOPBACK) && ((addr & 0xff000000) == 0x7f000000)) {
148 /* this function returns the total number of interface addresses
149 ** the buffer has to be passed in by the caller
151 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
152 #if defined(AFS_OBSD42_ENV)
154 ifm_fixversion(char *buffer, size_t *size) {
155 struct if_msghdr *ifm;
159 if ((t = malloc(*size)) != NULL) {
160 memcpy(t, buffer, *size);
162 for (s = t; s < t + *size; s += ifm->ifm_msglen) {
163 ifm = (struct if_msghdr *)s;
165 if (ifm->ifm_version == RTM_VERSION) {
166 memcpy(b, ifm, ifm->ifm_msglen);
167 b += ifm->ifm_msglen;
179 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
183 struct if_msghdr *ifm, *nextifm;
184 struct ifa_msghdr *ifam;
185 struct rt_addrinfo info;
186 char *buf, *lim, *next;
187 int count = 0, addrcount = 0;
192 mib[3] = AF_INET; /* address family */
193 mib[4] = NET_RT_IFLIST;
195 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
197 if ((buf = malloc(needed)) == NULL)
199 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
203 #if defined(AFS_OBSD42_ENV)
204 ifm_fixversion(buf, &needed);
209 ifm = (struct if_msghdr *)next;
210 if (ifm->ifm_type != RTM_IFINFO) {
211 dpf(("out of sync parsing NET_RT_IFLIST\n"));
215 next += ifm->ifm_msglen;
219 nextifm = (struct if_msghdr *)next;
220 if (nextifm->ifm_type != RTM_NEWADDR)
223 ifam = (struct ifa_msghdr *)nextifm;
225 next += nextifm->ifm_msglen;
227 if ((ifm->ifm_flags & IFF_UP) == 0)
228 continue; /* not up */
229 while (addrcount > 0) {
230 struct sockaddr_in *a;
232 info.rti_addrs = ifam->ifam_addrs;
234 /* Expand the compacted addresses */
235 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
237 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
241 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
243 if (count >= maxSize) /* no more space */
244 dpf(("Too many interfaces..ignoring 0x%x\n",
245 a->sin_addr.s_addr));
246 else if (!loopbacks && rxi_IsLoopbackIface(a, ifm->ifm_flags)) {
248 continue; /* skip loopback address as well. */
249 } else if (loopbacks && ifm->ifm_flags & IFF_LOOPBACK) {
251 continue; /* skip aliased loopbacks as well. */
253 buffer[count++] = a->sin_addr.s_addr;
255 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
263 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
264 afs_uint32 mtuBuffer[], int maxSize)
270 struct if_msghdr *ifm, *nextifm;
271 struct ifa_msghdr *ifam;
272 struct sockaddr_dl *sdl;
273 struct rt_addrinfo info;
274 char *buf, *lim, *next;
275 int count = 0, addrcount = 0;
280 mib[3] = AF_INET; /* address family */
281 mib[4] = NET_RT_IFLIST;
283 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
285 if ((buf = malloc(needed)) == NULL)
287 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
291 #if defined(AFS_OBSD42_ENV)
292 ifm_fixversion(buf, &needed);
294 s = socket(PF_INET, SOCK_DGRAM, 0);
300 ifm = (struct if_msghdr *)next;
301 if (ifm->ifm_type != RTM_IFINFO) {
302 dpf(("out of sync parsing NET_RT_IFLIST\n"));
306 sdl = (struct sockaddr_dl *)(ifm + 1);
307 next += ifm->ifm_msglen;
311 nextifm = (struct if_msghdr *)next;
312 if (nextifm->ifm_type != RTM_NEWADDR)
315 ifam = (struct ifa_msghdr *)nextifm;
317 next += nextifm->ifm_msglen;
319 if ((ifm->ifm_flags & IFF_UP) == 0)
320 continue; /* not up */
321 while (addrcount > 0) {
322 struct sockaddr_in *a;
324 info.rti_addrs = ifam->ifam_addrs;
326 /* Expand the compacted addresses */
327 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
329 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
333 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
335 if (!rx_IsLoopbackAddr(ntohl(a->sin_addr.s_addr))) {
336 if (count >= maxSize) { /* no more space */
337 dpf(("Too many interfaces..ignoring 0x%x\n",
338 a->sin_addr.s_addr));
342 addrBuffer[count] = a->sin_addr.s_addr;
343 a = (struct sockaddr_in *) info.rti_info[RTAX_NETMASK];
345 maskBuffer[count] = a->sin_addr.s_addr;
347 maskBuffer[count] = htonl(0xffffffff);
348 memset(&ifr, 0, sizeof(ifr));
349 ifr.ifr_addr.sa_family = AF_INET;
350 strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
351 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
352 mtuBuffer[count] = htonl(1500);
354 mtuBuffer[count] = htonl(ifr.ifr_mtu);
359 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
368 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
370 return rx_getAllAddr_internal(buffer, maxSize, 0);
372 /* this function returns the total number of interface addresses
373 ** the buffer has to be passed in by the caller
375 #else /* UKERNEL indirectly, on DARWIN or XBSD */
377 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
380 int i, len, count = 0;
382 struct ifreq ifs[NIFS], *ifr;
383 struct sockaddr_in *a;
384 /* can't ever be AFS_DARWIN_ENV or AFS_XBSD_ENV, no? */
385 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
386 char *cp, *cplim, *cpnext; /* used only for AIX 41 */
389 s = socket(AF_INET, SOCK_DGRAM, 0);
392 ifc.ifc_len = sizeof(ifs);
393 ifc.ifc_buf = (caddr_t) ifs;
394 i = ioctl(s, SIOCGIFCONF, &ifc);
397 len = ifc.ifc_len / sizeof(struct ifreq);
400 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
401 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
402 ifc.ifc_len = sizeof(ifs);
403 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
405 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
406 cp += _SIZEOF_ADDR_IFREQ(*ifr)
411 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
416 for (i = 0; i < len; ++i)
419 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
420 ifr = (struct ifreq *)cp;
424 a = (struct sockaddr_in *)&ifr->ifr_addr;
426 cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
428 if (a->sin_family != AF_INET)
430 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
431 perror("SIOCGIFFLAGS");
432 continue; /* ignore this address */
434 if (a->sin_addr.s_addr != 0) {
436 if (rxi_IsLoopbackIface(a, ifr->ifr_flags))
437 continue; /* skip loopback address as well. */
439 if (ifr->ifr_flags & IFF_LOOPBACK)
440 continue; /* skip aliased loopbacks as well. */
442 if (count >= maxSize) /* no more space */
443 dpf(("Too many interfaces..ignoring 0x%x\n",
444 a->sin_addr.s_addr));
446 buffer[count++] = a->sin_addr.s_addr;
454 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
456 return rx_getAllAddr_internal(buffer, maxSize, 0);
459 /* this function returns the total number of interface addresses
460 * the buffer has to be passed in by the caller. It also returns
461 * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
462 * gets the mask which is then passed into the kernel and is used
463 * by afsi_SetServerIPRank().
466 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
467 afs_uint32 mtuBuffer[], int maxSize)
470 #if defined(AFS_USERSPACE_IP_ADDR)
473 struct ifreq ifs[NIFS], *ifr;
474 struct sockaddr_in *a;
477 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
478 char *cp, *cplim; /* used only for AIX 41 */
481 #if !defined(AFS_USERSPACE_IP_ADDR)
482 count = rx_getAllAddr_internal(addrBuffer, 1024, 0);
483 for (i = 0; i < count; i++) {
484 maskBuffer[i] = htonl(0xffffffff);
485 mtuBuffer[i] = htonl(1500);
488 #else /* AFS_USERSPACE_IP_ADDR */
489 s = socket(AF_INET, SOCK_DGRAM, 0);
493 ifc.ifc_len = sizeof(ifs);
494 ifc.ifc_buf = (caddr_t) ifs;
495 i = ioctl(s, SIOCGIFCONF, &ifc);
500 len = ifc.ifc_len / sizeof(struct ifreq);
504 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
505 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
506 ifc.ifc_len = sizeof(ifs);
507 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
509 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
510 ifr = (struct ifreq *)cp;
512 for (i = 0; i < len; ++i) {
515 a = (struct sockaddr_in *)&ifr->ifr_addr;
516 if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
518 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
519 perror("SIOCGIFFLAGS");
520 continue; /* ignore this address */
523 if (rx_IsLoopbackAddr(ntohl(a->sin_addr.s_addr)))
524 continue; /* skip loopback address as well. */
526 if (count >= maxSize) { /* no more space */
527 dpf(("Too many interfaces..ignoring 0x%x\n",
528 a->sin_addr.s_addr));
532 addrBuffer[count] = a->sin_addr.s_addr;
534 if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
535 perror("SIOCGIFNETMASK");
536 maskBuffer[count] = htonl(0xffffffff);
538 maskBuffer[count] = (((struct sockaddr_in *)
539 (&ifr->ifr_addr))->sin_addr).s_addr;
542 mtuBuffer[count] = htonl(1500);
544 if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
545 perror("SIOCGIFMTU");
547 mtuBuffer[count] = htonl(ifr->ifr_metric);
549 #endif /* SIOCGIFMTU */
551 if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
552 perror("SIOCRIPMTU");
554 mtuBuffer[count] = htonl(ifr->ifr_metric);
556 #endif /* SIOCRIPMTU */
563 #endif /* AFS_USERSPACE_IP_ADDR */
567 #endif /* ! AFS_NT40_ENV */
568 #endif /* !KERNEL || UKERNEL */