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 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #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.
36 #include "rx_globals.h"
37 #endif /* AFS_NT40_ENV */
40 #include "rx/rx_kcommon.h"
49 /* only used for generating random noise */
51 afs_uint32 rxi_tempAddr = 0; /* default attempt */
53 /* set the advisory noise */
55 rxi_setaddr(afs_uint32 x)
60 /* get approx to net addr */
71 /* to satisfy those who call setaddr */
73 rxi_setaddr(afs_uint32 x)
80 #if !defined(AFS_NT40_ENV)
81 /* For NT, rxi_getaddr has moved to rx_user.c. rxi_GetIfInfo is called by
82 * rx_Init which sets up the list of addresses for us.
87 /* Return our internet address as a long in network byte order. Returns zero
88 * if it can't find one.
93 afs_uint32 buffer[1024];
96 count = rx_getAllAddr(buffer, 1024);
98 return buffer[0]; /* returns the first address */
105 #if !defined(KERNEL) || defined(UKERNEL)
108 #define MAX(A,B) (((A)<(B)) ? (B) : (A))
116 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
118 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
119 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
122 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
127 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
128 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
129 if ((rtinfo->rti_addrs & (1 << i)) == 0)
131 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
138 /* this function returns the total number of interface addresses
139 ** the buffer has to be passed in by the caller
141 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
143 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
147 struct if_msghdr *ifm, *nextifm;
148 struct ifa_msghdr *ifam;
149 struct sockaddr_dl *sdl;
150 struct rt_addrinfo info;
151 char *buf, *lim, *next;
152 int count = 0, addrcount = 0;
157 mib[3] = AF_INET; /* address family */
158 mib[4] = NET_RT_IFLIST;
160 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
162 if ((buf = malloc(needed)) == NULL)
164 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
171 ifm = (struct if_msghdr *)next;
172 if (ifm->ifm_type != RTM_IFINFO) {
173 dpf(("out of sync parsing NET_RT_IFLIST\n"));
177 sdl = (struct sockaddr_dl *)(ifm + 1);
178 next += ifm->ifm_msglen;
182 nextifm = (struct if_msghdr *)next;
183 if (nextifm->ifm_type != RTM_NEWADDR)
186 ifam = (struct ifa_msghdr *)nextifm;
188 next += nextifm->ifm_msglen;
190 if ((ifm->ifm_flags & IFF_UP) == 0)
191 continue; /* not up */
192 while (addrcount > 0) {
193 struct sockaddr_in *a;
195 info.rti_addrs = ifam->ifam_addrs;
197 /* Expand the compacted addresses */
198 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
200 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
204 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
206 if (count >= maxSize) /* no more space */
207 dpf(("Too many interfaces..ignoring 0x%x\n",
208 a->sin_addr.s_addr));
209 else if (!loopbacks && a->sin_addr.s_addr == htonl(0x7f000001)) {
211 continue; /* skip loopback address as well. */
212 } else if (loopbacks && ifm->ifm_flags & IFF_LOOPBACK) {
214 continue; /* skip aliased loopbacks as well. */
216 buffer[count++] = a->sin_addr.s_addr;
218 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
226 rxi_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
227 afs_uint32 mtuBuffer[], int maxSize)
233 struct if_msghdr *ifm, *nextifm;
234 struct ifa_msghdr *ifam;
235 struct sockaddr_dl *sdl;
236 struct rt_addrinfo info;
237 char *buf, *lim, *next;
238 int count = 0, addrcount = 0;
243 mib[3] = AF_INET; /* address family */
244 mib[4] = NET_RT_IFLIST;
246 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
248 if ((buf = malloc(needed)) == NULL)
250 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
254 s = socket(PF_INET, SOCK_DGRAM, 0);
260 ifm = (struct if_msghdr *)next;
261 if (ifm->ifm_type != RTM_IFINFO) {
262 dpf(("out of sync parsing NET_RT_IFLIST\n"));
266 sdl = (struct sockaddr_dl *)(ifm + 1);
267 next += ifm->ifm_msglen;
271 nextifm = (struct if_msghdr *)next;
272 if (nextifm->ifm_type != RTM_NEWADDR)
275 ifam = (struct ifa_msghdr *)nextifm;
277 next += nextifm->ifm_msglen;
279 if ((ifm->ifm_flags & IFF_UP) == 0)
280 continue; /* not up */
281 while (addrcount > 0) {
282 struct sockaddr_in *a;
284 info.rti_addrs = ifam->ifam_addrs;
286 /* Expand the compacted addresses */
287 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
289 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
293 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
295 if (a->sin_addr.s_addr != htonl(0x7f000001) ) {
296 if (count >= maxSize) { /* no more space */
297 dpf(("Too many interfaces..ignoring 0x%x\n",
298 a->sin_addr.s_addr));
302 addrBuffer[count] = a->sin_addr.s_addr;
303 a = (struct sockaddr_in *) info.rti_info[RTAX_NETMASK];
305 maskBuffer[count] = a->sin_addr.s_addr;
307 maskBuffer[count] = htonl(0xffffffff);
308 memset(&ifr, 0, sizeof(ifr));
309 ifr.ifr_addr.sa_family = AF_INET;
310 strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
311 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
312 mtuBuffer[count] = htonl(1500);
314 mtuBuffer[count] = htonl(ifr.ifr_mtu);
319 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
328 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
330 return rx_getAllAddr_internal(buffer, maxSize, 0);
332 /* this function returns the total number of interface addresses
333 ** the buffer has to be passed in by the caller
337 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
340 int i, len, count = 0;
342 struct ifreq ifs[NIFS], *ifr;
343 struct sockaddr_in *a;
344 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
345 char *cp, *cplim, *cpnext; /* used only for AIX 41 */
348 s = socket(AF_INET, SOCK_DGRAM, 0);
351 ifc.ifc_len = sizeof(ifs);
352 ifc.ifc_buf = (caddr_t) ifs;
353 i = ioctl(s, SIOCGIFCONF, &ifc);
356 len = ifc.ifc_len / sizeof(struct ifreq);
359 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
360 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
361 ifc.ifc_len = sizeof(ifs);
362 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
364 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
365 cp += _SIZEOF_ADDR_IFREQ(*ifr)
370 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
375 for (i = 0; i < len; ++i)
378 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
379 ifr = (struct ifreq *)cp;
383 a = (struct sockaddr_in *)&ifr->ifr_addr;
385 cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
387 if (a->sin_family != AF_INET)
389 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
390 perror("SIOCGIFFLAGS");
391 continue; /* ignore this address */
393 if (a->sin_addr.s_addr != 0) {
395 if (a->sin_addr.s_addr == htonl(0x7f000001))
396 continue; /* skip loopback address as well. */
398 if (ifr->ifr_flags & IFF_LOOPBACK)
399 continue; /* skip aliased loopbacks as well. */
401 if (count >= maxSize) /* no more space */
402 dpf(("Too many interfaces..ignoring 0x%x\n",
403 a->sin_addr.s_addr));
405 buffer[count++] = a->sin_addr.s_addr;
413 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
415 return rx_getAllAddr_internal(buffer, maxSize, 0);
418 /* this function returns the total number of interface addresses
419 * the buffer has to be passed in by the caller. It also returns
420 * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
421 * gets the mask which is then passed into the kernel and is used
422 * by afsi_SetServerIPRank().
425 rxi_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
426 afs_uint32 mtuBuffer[], int maxSize)
429 int i, len, count = 0;
431 struct ifreq ifs[NIFS], *ifr;
432 struct sockaddr_in *a;
433 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
434 char *cp, *cplim; /* used only for AIX 41 */
437 #if !defined(AFS_USERSPACE_IP_ADDR)
438 count = rx_getAllAddr_internal(addrBuffer, 1024, 0);
439 for (i = 0; i < count; i++) {
440 maskBuffer[i] = htonl(0xffffffff);
441 mtuBuffer[i] = htonl(1500);
444 #else /* AFS_USERSPACE_IP_ADDR */
445 s = socket(AF_INET, SOCK_DGRAM, 0);
449 ifc.ifc_len = sizeof(ifs);
450 ifc.ifc_buf = (caddr_t) ifs;
451 i = ioctl(s, SIOCGIFCONF, &ifc);
456 len = ifc.ifc_len / sizeof(struct ifreq);
460 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
461 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
462 ifc.ifc_len = sizeof(ifs);
463 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
465 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
466 ifr = (struct ifreq *)cp;
468 for (i = 0; i < len; ++i) {
471 a = (struct sockaddr_in *)&ifr->ifr_addr;
472 if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
474 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
475 perror("SIOCGIFFLAGS");
476 continue; /* ignore this address */
479 if (a->sin_addr.s_addr == htonl(0x7f000001) )
480 continue; /* skip loopback address as well. */
482 if (count >= maxSize) { /* no more space */
483 dpf(("Too many interfaces..ignoring 0x%x\n",
484 a->sin_addr.s_addr));
488 addrBuffer[count] = a->sin_addr.s_addr;
490 if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
491 perror("SIOCGIFNETMASK");
492 maskBuffer[count] = htonl(0xffffffff);
494 maskBuffer[count] = (((struct sockaddr_in *)
495 (&ifr->ifr_addr))->sin_addr).s_addr;
498 mtuBuffer[count] = htonl(1500);
500 if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
501 perror("SIOCGIFMTU");
503 mtuBuffer[count] = htonl(ifr->ifr_metric);
505 #endif /* SIOCGIFMTU */
507 if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
508 perror("SIOCRIPMTU");
510 mtuBuffer[count] = htonl(ifr->ifr_metric);
512 #endif /* SIOCRIPMTU */
519 #endif /* AFS_USERSPACE_IP_ADDR */
523 #endif /* ! AFS_NT40_ENV */
524 #endif /* !KERNEL || UKERNEL */