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"
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)
148 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
152 struct if_msghdr *ifm, *nextifm;
153 struct ifa_msghdr *ifam;
154 struct sockaddr_dl *sdl;
155 struct rt_addrinfo info;
156 char *buf, *lim, *next;
157 int count = 0, addrcount = 0;
162 mib[3] = AF_INET; /* address family */
163 mib[4] = NET_RT_IFLIST;
165 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
167 if ((buf = malloc(needed)) == NULL)
169 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
176 ifm = (struct if_msghdr *)next;
177 if (ifm->ifm_type != RTM_IFINFO) {
178 dpf(("out of sync parsing NET_RT_IFLIST\n"));
182 sdl = (struct sockaddr_dl *)(ifm + 1);
183 next += ifm->ifm_msglen;
187 nextifm = (struct if_msghdr *)next;
188 if (nextifm->ifm_type != RTM_NEWADDR)
191 ifam = (struct ifa_msghdr *)nextifm;
193 next += nextifm->ifm_msglen;
195 if ((ifm->ifm_flags & IFF_UP) == 0)
196 continue; /* not up */
197 while (addrcount > 0) {
198 struct sockaddr_in *a;
200 info.rti_addrs = ifam->ifam_addrs;
202 /* Expand the compacted addresses */
203 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
205 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
209 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
211 if (count >= maxSize) /* no more space */
212 dpf(("Too many interfaces..ignoring 0x%x\n",
213 a->sin_addr.s_addr));
214 else if (!loopbacks && a->sin_addr.s_addr == htonl(0x7f000001)) {
216 continue; /* skip loopback address as well. */
217 } else if (loopbacks && ifm->ifm_flags & IFF_LOOPBACK) {
219 continue; /* skip aliased loopbacks as well. */
221 buffer[count++] = a->sin_addr.s_addr;
223 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
231 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
232 afs_uint32 mtuBuffer[], int maxSize)
238 struct if_msghdr *ifm, *nextifm;
239 struct ifa_msghdr *ifam;
240 struct sockaddr_dl *sdl;
241 struct rt_addrinfo info;
242 char *buf, *lim, *next;
243 int count = 0, addrcount = 0;
248 mib[3] = AF_INET; /* address family */
249 mib[4] = NET_RT_IFLIST;
251 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
253 if ((buf = malloc(needed)) == NULL)
255 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
259 s = socket(PF_INET, SOCK_DGRAM, 0);
265 ifm = (struct if_msghdr *)next;
266 if (ifm->ifm_type != RTM_IFINFO) {
267 dpf(("out of sync parsing NET_RT_IFLIST\n"));
271 sdl = (struct sockaddr_dl *)(ifm + 1);
272 next += ifm->ifm_msglen;
276 nextifm = (struct if_msghdr *)next;
277 if (nextifm->ifm_type != RTM_NEWADDR)
280 ifam = (struct ifa_msghdr *)nextifm;
282 next += nextifm->ifm_msglen;
284 if ((ifm->ifm_flags & IFF_UP) == 0)
285 continue; /* not up */
286 while (addrcount > 0) {
287 struct sockaddr_in *a;
289 info.rti_addrs = ifam->ifam_addrs;
291 /* Expand the compacted addresses */
292 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
294 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
298 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
300 if (a->sin_addr.s_addr != htonl(0x7f000001) ) {
301 if (count >= maxSize) { /* no more space */
302 dpf(("Too many interfaces..ignoring 0x%x\n",
303 a->sin_addr.s_addr));
307 addrBuffer[count] = a->sin_addr.s_addr;
308 a = (struct sockaddr_in *) info.rti_info[RTAX_NETMASK];
310 maskBuffer[count] = a->sin_addr.s_addr;
312 maskBuffer[count] = htonl(0xffffffff);
313 memset(&ifr, 0, sizeof(ifr));
314 ifr.ifr_addr.sa_family = AF_INET;
315 strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
316 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
317 mtuBuffer[count] = htonl(1500);
319 mtuBuffer[count] = htonl(ifr.ifr_mtu);
324 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
333 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
335 return rx_getAllAddr_internal(buffer, maxSize, 0);
337 /* this function returns the total number of interface addresses
338 ** the buffer has to be passed in by the caller
340 #else /* UKERNEL indirectly, on DARWIN or XBSD */
342 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
345 int i, len, count = 0;
347 struct ifreq ifs[NIFS], *ifr;
348 struct sockaddr_in *a;
349 /* can't ever be AFS_DARWIN_ENV or AFS_XBSD_ENV, no? */
350 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
351 char *cp, *cplim, *cpnext; /* used only for AIX 41 */
354 s = socket(AF_INET, SOCK_DGRAM, 0);
357 ifc.ifc_len = sizeof(ifs);
358 ifc.ifc_buf = (caddr_t) ifs;
359 i = ioctl(s, SIOCGIFCONF, &ifc);
362 len = ifc.ifc_len / sizeof(struct ifreq);
365 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
366 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
367 ifc.ifc_len = sizeof(ifs);
368 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
370 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
371 cp += _SIZEOF_ADDR_IFREQ(*ifr)
376 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
381 for (i = 0; i < len; ++i)
384 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
385 ifr = (struct ifreq *)cp;
389 a = (struct sockaddr_in *)&ifr->ifr_addr;
391 cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
393 if (a->sin_family != AF_INET)
395 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
396 perror("SIOCGIFFLAGS");
397 continue; /* ignore this address */
399 if (a->sin_addr.s_addr != 0) {
401 if (a->sin_addr.s_addr == htonl(0x7f000001))
402 continue; /* skip loopback address as well. */
404 if (ifr->ifr_flags & IFF_LOOPBACK)
405 continue; /* skip aliased loopbacks as well. */
407 if (count >= maxSize) /* no more space */
408 dpf(("Too many interfaces..ignoring 0x%x\n",
409 a->sin_addr.s_addr));
411 buffer[count++] = a->sin_addr.s_addr;
419 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
421 return rx_getAllAddr_internal(buffer, maxSize, 0);
424 /* this function returns the total number of interface addresses
425 * the buffer has to be passed in by the caller. It also returns
426 * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
427 * gets the mask which is then passed into the kernel and is used
428 * by afsi_SetServerIPRank().
431 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
432 afs_uint32 mtuBuffer[], int maxSize)
435 int i, len, count = 0;
437 struct ifreq ifs[NIFS], *ifr;
438 struct sockaddr_in *a;
439 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
440 char *cp, *cplim; /* used only for AIX 41 */
443 #if !defined(AFS_USERSPACE_IP_ADDR)
444 count = rx_getAllAddr_internal(addrBuffer, 1024, 0);
445 for (i = 0; i < count; i++) {
446 maskBuffer[i] = htonl(0xffffffff);
447 mtuBuffer[i] = htonl(1500);
450 #else /* AFS_USERSPACE_IP_ADDR */
451 s = socket(AF_INET, SOCK_DGRAM, 0);
455 ifc.ifc_len = sizeof(ifs);
456 ifc.ifc_buf = (caddr_t) ifs;
457 i = ioctl(s, SIOCGIFCONF, &ifc);
462 len = ifc.ifc_len / sizeof(struct ifreq);
466 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
467 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
468 ifc.ifc_len = sizeof(ifs);
469 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
471 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
472 ifr = (struct ifreq *)cp;
474 for (i = 0; i < len; ++i) {
477 a = (struct sockaddr_in *)&ifr->ifr_addr;
478 if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
480 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
481 perror("SIOCGIFFLAGS");
482 continue; /* ignore this address */
485 if (a->sin_addr.s_addr == htonl(0x7f000001) )
486 continue; /* skip loopback address as well. */
488 if (count >= maxSize) { /* no more space */
489 dpf(("Too many interfaces..ignoring 0x%x\n",
490 a->sin_addr.s_addr));
494 addrBuffer[count] = a->sin_addr.s_addr;
496 if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
497 perror("SIOCGIFNETMASK");
498 maskBuffer[count] = htonl(0xffffffff);
500 maskBuffer[count] = (((struct sockaddr_in *)
501 (&ifr->ifr_addr))->sin_addr).s_addr;
504 mtuBuffer[count] = htonl(1500);
506 if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
507 perror("SIOCGIFMTU");
509 mtuBuffer[count] = htonl(ifr->ifr_metric);
511 #endif /* SIOCGIFMTU */
513 if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
514 perror("SIOCRIPMTU");
516 mtuBuffer[count] = htonl(ifr->ifr_metric);
518 #endif /* SIOCRIPMTU */
525 #endif /* AFS_USERSPACE_IP_ADDR */
529 #endif /* ! AFS_NT40_ENV */
530 #endif /* !KERNEL || UKERNEL */