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>
16 #include <sys/types.h>
17 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <sys/ioctl.h>
23 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
24 #include <sys/sysctl.h>
25 #ifndef AFS_ARM_DARWIN_ENV
26 #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.
36 #include "rx_globals.h"
37 #endif /* AFS_NT40_ENV */
40 #include "rx/rx_kcommon.h"
48 #if defined(AFS_USR_DFBSD_ENV)
50 #include <sys/sockio.h>
54 /* only used for generating random noise */
56 afs_uint32 rxi_tempAddr = 0; /* default attempt */
58 /* set the advisory noise */
60 rxi_setaddr(afs_uint32 x)
65 /* get approx to net addr */
76 /* to satisfy those who call setaddr */
78 rxi_setaddr(afs_uint32 x)
85 #if !defined(AFS_NT40_ENV)
86 /* For NT, rxi_getaddr has moved to rx_user.c. rxi_GetIfInfo is called by
87 * rx_Init which sets up the list of addresses for us.
92 /* Return our internet address as a long in network byte order. Returns zero
93 * if it can't find one.
98 afs_uint32 buffer[1024];
101 count = rx_getAllAddr(buffer, 1024);
103 return buffer[0]; /* returns the first address */
110 #if !defined(KERNEL) || defined(UKERNEL)
113 #define MAX(A,B) (((A)<(B)) ? (B) : (A))
121 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
123 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
124 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
127 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
132 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
133 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
134 if ((rtinfo->rti_addrs & (1 << i)) == 0)
136 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
143 /* this function returns the total number of interface addresses
144 ** the buffer has to be passed in by the caller
146 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
147 #if defined(AFS_OBSD42_ENV)
149 ifm_fixversion(char *buffer, size_t *size) {
150 struct if_msghdr *ifm;
154 if ((t = malloc(*size)) != NULL) {
155 memcpy(t, buffer, *size);
157 for (s = t; s < t + *size; s += ifm->ifm_msglen) {
158 ifm = (struct if_msghdr *)s;
160 if (ifm->ifm_version == RTM_VERSION) {
161 memcpy(b, ifm, ifm->ifm_msglen);
162 b += ifm->ifm_msglen;
174 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
178 struct if_msghdr *ifm, *nextifm;
179 struct ifa_msghdr *ifam;
180 struct sockaddr_dl *sdl;
181 struct rt_addrinfo info;
182 char *buf, *lim, *next;
183 int count = 0, addrcount = 0;
188 mib[3] = AF_INET; /* address family */
189 mib[4] = NET_RT_IFLIST;
191 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
193 if ((buf = malloc(needed)) == NULL)
195 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
199 #if defined(AFS_OBSD42_ENV)
200 ifm_fixversion(buf, &needed);
205 ifm = (struct if_msghdr *)next;
206 if (ifm->ifm_type != RTM_IFINFO) {
207 dpf(("out of sync parsing NET_RT_IFLIST\n"));
211 sdl = (struct sockaddr_dl *)(ifm + 1);
212 next += ifm->ifm_msglen;
216 nextifm = (struct if_msghdr *)next;
217 if (nextifm->ifm_type != RTM_NEWADDR)
220 ifam = (struct ifa_msghdr *)nextifm;
222 next += nextifm->ifm_msglen;
224 if ((ifm->ifm_flags & IFF_UP) == 0)
225 continue; /* not up */
226 while (addrcount > 0) {
227 struct sockaddr_in *a;
229 info.rti_addrs = ifam->ifam_addrs;
231 /* Expand the compacted addresses */
232 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
234 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
238 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
240 if (count >= maxSize) /* no more space */
241 dpf(("Too many interfaces..ignoring 0x%x\n",
242 a->sin_addr.s_addr));
243 else if (!loopbacks && a->sin_addr.s_addr == htonl(0x7f000001)) {
245 continue; /* skip loopback address as well. */
246 } else if (loopbacks && ifm->ifm_flags & IFF_LOOPBACK) {
248 continue; /* skip aliased loopbacks as well. */
250 buffer[count++] = a->sin_addr.s_addr;
252 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
260 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
261 afs_uint32 mtuBuffer[], int maxSize)
267 struct if_msghdr *ifm, *nextifm;
268 struct ifa_msghdr *ifam;
269 struct sockaddr_dl *sdl;
270 struct rt_addrinfo info;
271 char *buf, *lim, *next;
272 int count = 0, addrcount = 0;
277 mib[3] = AF_INET; /* address family */
278 mib[4] = NET_RT_IFLIST;
280 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
282 if ((buf = malloc(needed)) == NULL)
284 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
288 #if defined(AFS_OBSD42_ENV)
289 ifm_fixversion(buf, &needed);
291 s = socket(PF_INET, SOCK_DGRAM, 0);
297 ifm = (struct if_msghdr *)next;
298 if (ifm->ifm_type != RTM_IFINFO) {
299 dpf(("out of sync parsing NET_RT_IFLIST\n"));
303 sdl = (struct sockaddr_dl *)(ifm + 1);
304 next += ifm->ifm_msglen;
308 nextifm = (struct if_msghdr *)next;
309 if (nextifm->ifm_type != RTM_NEWADDR)
312 ifam = (struct ifa_msghdr *)nextifm;
314 next += nextifm->ifm_msglen;
316 if ((ifm->ifm_flags & IFF_UP) == 0)
317 continue; /* not up */
318 while (addrcount > 0) {
319 struct sockaddr_in *a;
321 info.rti_addrs = ifam->ifam_addrs;
323 /* Expand the compacted addresses */
324 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
326 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
330 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
332 if (a->sin_addr.s_addr != htonl(0x7f000001) ) {
333 if (count >= maxSize) { /* no more space */
334 dpf(("Too many interfaces..ignoring 0x%x\n",
335 a->sin_addr.s_addr));
339 addrBuffer[count] = a->sin_addr.s_addr;
340 a = (struct sockaddr_in *) info.rti_info[RTAX_NETMASK];
342 maskBuffer[count] = a->sin_addr.s_addr;
344 maskBuffer[count] = htonl(0xffffffff);
345 memset(&ifr, 0, sizeof(ifr));
346 ifr.ifr_addr.sa_family = AF_INET;
347 strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
348 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
349 mtuBuffer[count] = htonl(1500);
351 mtuBuffer[count] = htonl(ifr.ifr_mtu);
356 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
365 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
367 return rx_getAllAddr_internal(buffer, maxSize, 0);
369 /* this function returns the total number of interface addresses
370 ** the buffer has to be passed in by the caller
372 #else /* UKERNEL indirectly, on DARWIN or XBSD */
374 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
377 int i, len, count = 0;
379 struct ifreq ifs[NIFS], *ifr;
380 struct sockaddr_in *a;
381 /* can't ever be AFS_DARWIN_ENV or AFS_XBSD_ENV, no? */
382 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
383 char *cp, *cplim, *cpnext; /* used only for AIX 41 */
386 s = socket(AF_INET, SOCK_DGRAM, 0);
389 ifc.ifc_len = sizeof(ifs);
390 ifc.ifc_buf = (caddr_t) ifs;
391 i = ioctl(s, SIOCGIFCONF, &ifc);
394 len = ifc.ifc_len / sizeof(struct ifreq);
397 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
398 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
399 ifc.ifc_len = sizeof(ifs);
400 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
402 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
403 cp += _SIZEOF_ADDR_IFREQ(*ifr)
408 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
413 for (i = 0; i < len; ++i)
416 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
417 ifr = (struct ifreq *)cp;
421 a = (struct sockaddr_in *)&ifr->ifr_addr;
423 cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
425 if (a->sin_family != AF_INET)
427 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
428 perror("SIOCGIFFLAGS");
429 continue; /* ignore this address */
431 if (a->sin_addr.s_addr != 0) {
433 if (a->sin_addr.s_addr == htonl(0x7f000001))
434 continue; /* skip loopback address as well. */
436 if (ifr->ifr_flags & IFF_LOOPBACK)
437 continue; /* skip aliased loopbacks as well. */
439 if (count >= maxSize) /* no more space */
440 dpf(("Too many interfaces..ignoring 0x%x\n",
441 a->sin_addr.s_addr));
443 buffer[count++] = a->sin_addr.s_addr;
451 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
453 return rx_getAllAddr_internal(buffer, maxSize, 0);
456 /* this function returns the total number of interface addresses
457 * the buffer has to be passed in by the caller. It also returns
458 * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
459 * gets the mask which is then passed into the kernel and is used
460 * by afsi_SetServerIPRank().
463 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
464 afs_uint32 mtuBuffer[], int maxSize)
467 #if defined(AFS_USERSPACE_IP_ADDR)
470 struct ifreq ifs[NIFS], *ifr;
471 struct sockaddr_in *a;
474 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
475 char *cp, *cplim; /* used only for AIX 41 */
478 #if !defined(AFS_USERSPACE_IP_ADDR)
479 count = rx_getAllAddr_internal(addrBuffer, 1024, 0);
480 for (i = 0; i < count; i++) {
481 maskBuffer[i] = htonl(0xffffffff);
482 mtuBuffer[i] = htonl(1500);
485 #else /* AFS_USERSPACE_IP_ADDR */
486 s = socket(AF_INET, SOCK_DGRAM, 0);
490 ifc.ifc_len = sizeof(ifs);
491 ifc.ifc_buf = (caddr_t) ifs;
492 i = ioctl(s, SIOCGIFCONF, &ifc);
497 len = ifc.ifc_len / sizeof(struct ifreq);
501 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
502 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
503 ifc.ifc_len = sizeof(ifs);
504 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
506 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
507 ifr = (struct ifreq *)cp;
509 for (i = 0; i < len; ++i) {
512 a = (struct sockaddr_in *)&ifr->ifr_addr;
513 if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
515 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
516 perror("SIOCGIFFLAGS");
517 continue; /* ignore this address */
520 if (a->sin_addr.s_addr == htonl(0x7f000001) )
521 continue; /* skip loopback address as well. */
523 if (count >= maxSize) { /* no more space */
524 dpf(("Too many interfaces..ignoring 0x%x\n",
525 a->sin_addr.s_addr));
529 addrBuffer[count] = a->sin_addr.s_addr;
531 if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
532 perror("SIOCGIFNETMASK");
533 maskBuffer[count] = htonl(0xffffffff);
535 maskBuffer[count] = (((struct sockaddr_in *)
536 (&ifr->ifr_addr))->sin_addr).s_addr;
539 mtuBuffer[count] = htonl(1500);
541 if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
542 perror("SIOCGIFMTU");
544 mtuBuffer[count] = htonl(ifr->ifr_metric);
546 #endif /* SIOCGIFMTU */
548 if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
549 perror("SIOCRIPMTU");
551 mtuBuffer[count] = htonl(ifr->ifr_metric);
553 #endif /* SIOCRIPMTU */
560 #endif /* AFS_USERSPACE_IP_ADDR */
564 #endif /* ! AFS_NT40_ENV */
565 #endif /* !KERNEL || UKERNEL */