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 #include <net/route.h>
26 #include <net/if_dl.h>
30 * By including this, we get any system dependencies. In particular,
31 * the pthreads for solaris requires the socket call to be mapped.
34 #include "rx_globals.h"
35 #endif /* AFS_NT40_ENV */
38 #include "rx/rx_kcommon.h"
46 #if defined(AFS_USR_DFBSD_ENV)
48 #include <sys/sockio.h>
52 /* only used for generating random noise */
54 afs_uint32 rxi_tempAddr = 0; /* default attempt */
56 /* set the advisory noise */
58 rxi_setaddr(afs_uint32 x)
63 /* get approx to net addr */
74 /* to satisfy those who call setaddr */
76 rxi_setaddr(afs_uint32 x)
83 #if !defined(AFS_NT40_ENV)
84 /* For NT, rxi_getaddr has moved to rx_user.c. rxi_GetIfInfo is called by
85 * rx_Init which sets up the list of addresses for us.
90 /* Return our internet address as a long in network byte order. Returns zero
91 * if it can't find one.
96 afs_uint32 buffer[1024];
99 count = rx_getAllAddr(buffer, 1024);
101 return buffer[0]; /* returns the first address */
108 #if !defined(KERNEL) || defined(UKERNEL)
111 #define MAX(A,B) (((A)<(B)) ? (B) : (A))
119 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
121 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
122 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
125 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
130 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
131 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
132 if ((rtinfo->rti_addrs & (1 << i)) == 0)
134 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
141 /* this function returns the total number of interface addresses
142 ** the buffer has to be passed in by the caller
144 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
146 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
150 struct if_msghdr *ifm, *nextifm;
151 struct ifa_msghdr *ifam;
152 struct sockaddr_dl *sdl;
153 struct rt_addrinfo info;
154 char *buf, *lim, *next;
155 int count = 0, addrcount = 0;
160 mib[3] = AF_INET; /* address family */
161 mib[4] = NET_RT_IFLIST;
163 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
165 if ((buf = malloc(needed)) == NULL)
167 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
174 ifm = (struct if_msghdr *)next;
175 if (ifm->ifm_type != RTM_IFINFO) {
176 dpf(("out of sync parsing NET_RT_IFLIST\n"));
180 sdl = (struct sockaddr_dl *)(ifm + 1);
181 next += ifm->ifm_msglen;
185 nextifm = (struct if_msghdr *)next;
186 if (nextifm->ifm_type != RTM_NEWADDR)
189 ifam = (struct ifa_msghdr *)nextifm;
191 next += nextifm->ifm_msglen;
193 if ((ifm->ifm_flags & IFF_UP) == 0)
194 continue; /* not up */
195 while (addrcount > 0) {
196 struct sockaddr_in *a;
198 info.rti_addrs = ifam->ifam_addrs;
200 /* Expand the compacted addresses */
201 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
203 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
207 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
209 if (count >= maxSize) /* no more space */
210 dpf(("Too many interfaces..ignoring 0x%x\n",
211 a->sin_addr.s_addr));
212 else if (!loopbacks && a->sin_addr.s_addr == htonl(0x7f000001)) {
214 continue; /* skip loopback address as well. */
215 } else if (loopbacks && ifm->ifm_flags & IFF_LOOPBACK) {
217 continue; /* skip aliased loopbacks as well. */
219 buffer[count++] = a->sin_addr.s_addr;
221 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
229 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
230 afs_uint32 mtuBuffer[], int maxSize)
236 struct if_msghdr *ifm, *nextifm;
237 struct ifa_msghdr *ifam;
238 struct sockaddr_dl *sdl;
239 struct rt_addrinfo info;
240 char *buf, *lim, *next;
241 int count = 0, addrcount = 0;
246 mib[3] = AF_INET; /* address family */
247 mib[4] = NET_RT_IFLIST;
249 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
251 if ((buf = malloc(needed)) == NULL)
253 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
257 s = socket(PF_INET, SOCK_DGRAM, 0);
263 ifm = (struct if_msghdr *)next;
264 if (ifm->ifm_type != RTM_IFINFO) {
265 dpf(("out of sync parsing NET_RT_IFLIST\n"));
269 sdl = (struct sockaddr_dl *)(ifm + 1);
270 next += ifm->ifm_msglen;
274 nextifm = (struct if_msghdr *)next;
275 if (nextifm->ifm_type != RTM_NEWADDR)
278 ifam = (struct ifa_msghdr *)nextifm;
280 next += nextifm->ifm_msglen;
282 if ((ifm->ifm_flags & IFF_UP) == 0)
283 continue; /* not up */
284 while (addrcount > 0) {
285 struct sockaddr_in *a;
287 info.rti_addrs = ifam->ifam_addrs;
289 /* Expand the compacted addresses */
290 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
292 if (info.rti_info[RTAX_IFA]->sa_family != AF_INET) {
296 a = (struct sockaddr_in *) info.rti_info[RTAX_IFA];
298 if (a->sin_addr.s_addr != htonl(0x7f000001) ) {
299 if (count >= maxSize) { /* no more space */
300 dpf(("Too many interfaces..ignoring 0x%x\n",
301 a->sin_addr.s_addr));
305 addrBuffer[count] = a->sin_addr.s_addr;
306 a = (struct sockaddr_in *) info.rti_info[RTAX_NETMASK];
308 maskBuffer[count] = a->sin_addr.s_addr;
310 maskBuffer[count] = htonl(0xffffffff);
311 memset(&ifr, 0, sizeof(ifr));
312 ifr.ifr_addr.sa_family = AF_INET;
313 strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
314 if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
315 mtuBuffer[count] = htonl(1500);
317 mtuBuffer[count] = htonl(ifr.ifr_mtu);
322 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
331 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
333 return rx_getAllAddr_internal(buffer, maxSize, 0);
335 /* this function returns the total number of interface addresses
336 ** the buffer has to be passed in by the caller
338 #else /* UKERNEL indirectly, on DARWIN or XBSD */
340 rx_getAllAddr_internal(afs_uint32 buffer[], int maxSize, int loopbacks)
343 int i, len, count = 0;
345 struct ifreq ifs[NIFS], *ifr;
346 struct sockaddr_in *a;
347 /* can't ever be AFS_DARWIN_ENV or AFS_XBSD_ENV, no? */
348 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
349 char *cp, *cplim, *cpnext; /* used only for AIX 41 */
352 s = socket(AF_INET, SOCK_DGRAM, 0);
355 ifc.ifc_len = sizeof(ifs);
356 ifc.ifc_buf = (caddr_t) ifs;
357 i = ioctl(s, SIOCGIFCONF, &ifc);
360 len = ifc.ifc_len / sizeof(struct ifreq);
363 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
364 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
365 ifc.ifc_len = sizeof(ifs);
366 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
368 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
369 cp += _SIZEOF_ADDR_IFREQ(*ifr)
374 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
379 for (i = 0; i < len; ++i)
382 #if defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
383 ifr = (struct ifreq *)cp;
387 a = (struct sockaddr_in *)&ifr->ifr_addr;
389 cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
391 if (a->sin_family != AF_INET)
393 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
394 perror("SIOCGIFFLAGS");
395 continue; /* ignore this address */
397 if (a->sin_addr.s_addr != 0) {
399 if (a->sin_addr.s_addr == htonl(0x7f000001))
400 continue; /* skip loopback address as well. */
402 if (ifr->ifr_flags & IFF_LOOPBACK)
403 continue; /* skip aliased loopbacks as well. */
405 if (count >= maxSize) /* no more space */
406 dpf(("Too many interfaces..ignoring 0x%x\n",
407 a->sin_addr.s_addr));
409 buffer[count++] = a->sin_addr.s_addr;
417 rx_getAllAddr(afs_uint32 buffer[], int maxSize)
419 return rx_getAllAddr_internal(buffer, maxSize, 0);
422 /* this function returns the total number of interface addresses
423 * the buffer has to be passed in by the caller. It also returns
424 * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
425 * gets the mask which is then passed into the kernel and is used
426 * by afsi_SetServerIPRank().
429 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
430 afs_uint32 mtuBuffer[], int maxSize)
433 #if defined(AFS_USERSPACE_IP_ADDR)
436 struct ifreq ifs[NIFS], *ifr;
437 struct sockaddr_in *a;
440 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
441 char *cp, *cplim; /* used only for AIX 41 */
444 #if !defined(AFS_USERSPACE_IP_ADDR)
445 count = rx_getAllAddr_internal(addrBuffer, 1024, 0);
446 for (i = 0; i < count; i++) {
447 maskBuffer[i] = htonl(0xffffffff);
448 mtuBuffer[i] = htonl(1500);
451 #else /* AFS_USERSPACE_IP_ADDR */
452 s = socket(AF_INET, SOCK_DGRAM, 0);
456 ifc.ifc_len = sizeof(ifs);
457 ifc.ifc_buf = (caddr_t) ifs;
458 i = ioctl(s, SIOCGIFCONF, &ifc);
463 len = ifc.ifc_len / sizeof(struct ifreq);
467 #if defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
468 if (ifc.ifc_len > sizeof(ifs)) /* safety check */
469 ifc.ifc_len = sizeof(ifs);
470 for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
472 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
473 ifr = (struct ifreq *)cp;
475 for (i = 0; i < len; ++i) {
478 a = (struct sockaddr_in *)&ifr->ifr_addr;
479 if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
481 if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
482 perror("SIOCGIFFLAGS");
483 continue; /* ignore this address */
486 if (a->sin_addr.s_addr == htonl(0x7f000001) )
487 continue; /* skip loopback address as well. */
489 if (count >= maxSize) { /* no more space */
490 dpf(("Too many interfaces..ignoring 0x%x\n",
491 a->sin_addr.s_addr));
495 addrBuffer[count] = a->sin_addr.s_addr;
497 if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
498 perror("SIOCGIFNETMASK");
499 maskBuffer[count] = htonl(0xffffffff);
501 maskBuffer[count] = (((struct sockaddr_in *)
502 (&ifr->ifr_addr))->sin_addr).s_addr;
505 mtuBuffer[count] = htonl(1500);
507 if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
508 perror("SIOCGIFMTU");
510 mtuBuffer[count] = htonl(ifr->ifr_metric);
512 #endif /* SIOCGIFMTU */
514 if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
515 perror("SIOCRIPMTU");
517 mtuBuffer[count] = htonl(ifr->ifr_metric);
519 #endif /* SIOCRIPMTU */
526 #endif /* AFS_USERSPACE_IP_ADDR */
530 #endif /* ! AFS_NT40_ENV */
531 #endif /* !KERNEL || UKERNEL */