The interface list that comes back from the PF_ROUTE/NET_RT_IFLIST mib

is aligned.  Teach this to ``show route''.

Clean up some of the sockaddr parsing routines.
This commit is contained in:
brian 2000-03-14 01:46:44 +00:00
parent ca63a8dc14
commit a69e155f87
8 changed files with 130 additions and 128 deletions

View File

@ -68,6 +68,7 @@
#include "radius.h"
#endif
#include "bundle.h"
#include "iface.h"
#include "arp.h"
/*
@ -225,14 +226,13 @@ arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s)
int
get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr)
{
int mib[6], sa_len, skip, b;
int mib[6], skip;
size_t needed;
char *buf, *ptr, *end;
struct if_msghdr *ifm;
struct ifa_msghdr *ifam;
struct sockaddr *sa;
struct sockaddr_dl *dl;
struct sockaddr_in *ifa, *mask;
struct sockaddr *sa[RTAX_MAX];
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
@ -269,7 +269,6 @@ get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr)
ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */
if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */
break;
sa = (struct sockaddr *)(ifam+1); /* pile of sa's at end */
ptr += ifam->ifam_msglen;
if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) !=
(RTA_NETMASK|RTA_IFA))
@ -279,43 +278,32 @@ get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr)
ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen)
log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n",
dl->sdl_nlen, dl->sdl_data);
b = 1;
ifa = mask = NULL;
while (b < (RTA_NETMASK|RTA_IFA) && sa < (struct sockaddr *)ptr) {
switch (b) {
case RTA_IFA:
ifa = (struct sockaddr_in *)sa;
break;
case RTA_NETMASK:
/*
* Careful here ! this sockaddr doesn't have sa_family set to
* AF_INET, and is only 8 bytes big ! I have no idea why !
*/
mask = (struct sockaddr_in *)sa;
break;
iface_ParseHdr(ifam, sa);
if (sa[RTAX_IFA]->sa_family == AF_INET) {
struct sockaddr_in *ifa, *netmask;
ifa = (struct sockaddr_in *)sa[RTAX_IFA];
netmask = (struct sockaddr_in *)sa[RTAX_NETMASK];
if (log_IsKept(LogDEBUG)) {
char a[16];
strncpy(a, inet_ntoa(netmask->sin_addr), sizeof a - 1);
a[sizeof a - 1] = '\0';
log_Printf(LogDEBUG, "Check addr %s, mask %s\n",
inet_ntoa(ifa->sin_addr), a);
}
if (ifam->ifam_addrs & b) {
#define ALN sizeof(ifa->sin_addr.s_addr)
sa_len = sa->sa_len > 0 ? ((sa->sa_len-1)|(ALN-1))+1 : ALN;
sa = (struct sockaddr *)((char *)sa + sa_len);
if ((ifa->sin_addr.s_addr & netmask->sin_addr.s_addr) ==
(ipaddr.s_addr & netmask->sin_addr.s_addr)) {
log_Printf(LogPHASE, "Found interface %.*s for %s\n",
dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr));
memcpy(hwaddr, dl, dl->sdl_len);
free(buf);
return 1;
}
b <<= 1;
}
if (log_IsKept(LogDEBUG)) {
char a[16];
strncpy(a, inet_ntoa(mask->sin_addr), sizeof a - 1);
a[sizeof a - 1] = '\0';
log_Printf(LogDEBUG, "Check addr %s, mask %s\n",
inet_ntoa(ifa->sin_addr), a);
}
if (ifa->sin_family == AF_INET &&
(ifa->sin_addr.s_addr & mask->sin_addr.s_addr) ==
(ipaddr.s_addr & mask->sin_addr.s_addr)) {
log_Printf(LogPHASE, "Found interface %.*s for %s\n",
dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr));
memcpy(hwaddr, dl, dl->sdl_len);
free(buf);
return 1;
}
}
}

View File

@ -96,6 +96,8 @@
#define PARSE_REDUCE 1
#define PARSE_NOHASH 2
#define ROUNDUP(x) ((x) ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
extern void randinit(void);
extern ssize_t fullread(int, void *, size_t);
extern const char *mode2Nam(int);

View File

@ -94,13 +94,13 @@ bitsinmask(struct in_addr mask)
struct iface *
iface_Create(const char *name)
{
int mib[6], i, s;
int mib[6], s;
size_t needed;
char *buf, *ptr, *end, *cp, *lim;
char *buf, *ptr, *end;
struct if_msghdr *ifm;
struct ifa_msghdr *ifam;
struct sockaddr_dl *dl;
struct rt_addrinfo rti;
struct sockaddr *sa[RTAX_MAX];
struct iface *iface;
struct iface_addr *addr;
@ -165,29 +165,12 @@ iface_Create(const char *name)
if (ifam->ifam_type != RTM_NEWADDR) /* finished this if */
break;
if (iface == NULL) /* Keep wading */
continue;
if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
/* Found a configured interface ! */
iface_ParseHdr(ifam, sa);
/* Found an address ! */
if (ifam->ifam_addrs & (1 << RTAX_IFA)) {
/* *And* it's configured ! */
rti.rti_addrs = ifam->ifam_addrs;
lim = (char *)ifam + ifam->ifam_msglen;
cp = (char *)(ifam + 1);
memset(rti.rti_info, '\0', sizeof(rti.rti_info));
for (i = 0; i < RTAX_MAX && cp < lim; i++) {
if ((rti.rti_addrs & (1 << i)) == 0)
continue;
rti.rti_info[i] = (struct sockaddr *)cp;
#define ROUNDUP(x) \
((x) > 0 ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
cp += ROUNDUP(rti.rti_info[i]->sa_len);
}
if (rti.rti_info[RTAX_IFA] &&
rti.rti_info[RTAX_IFA]->sa_family == AF_INET) {
/* Record the iface address rti */
if (sa[RTAX_IFA] && sa[RTAX_IFA]->sa_family == AF_INET) {
/* Record the address */
addr = (struct iface_addr *)realloc
(iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
@ -198,14 +181,17 @@ iface_Create(const char *name)
addr += iface->in_addrs;
iface->in_addrs++;
addr->ifa.s_addr = ((struct sockaddr_in *)rti.rti_info[RTAX_IFA])->
sin_addr.s_addr;
addr->brd.s_addr = rti.rti_info[RTAX_BRD] ?
((struct sockaddr_in *)rti.rti_info[RTAX_BRD])->sin_addr.s_addr :
INADDR_ANY;
addr->mask.s_addr = rti.rti_info[RTAX_NETMASK] ?
((struct sockaddr_in *)rti.rti_info[RTAX_NETMASK])->sin_addr.s_addr:
INADDR_ANY;
addr->ifa = ((struct sockaddr_in *)sa[RTAX_IFA])->sin_addr;
if (sa[RTAX_BRD])
addr->brd = ((struct sockaddr_in *)sa[RTAX_BRD])->sin_addr;
else
addr->brd.s_addr = INADDR_ANY;
if (sa[RTAX_NETMASK])
addr->mask = ((struct sockaddr_in *)sa[RTAX_NETMASK])->sin_addr;
else
addr->mask.s_addr = INADDR_ANY;
addr->bits = bitsinmask(addr->mask);
}
@ -540,3 +526,19 @@ iface_Show(struct cmdargs const *arg)
return 0;
}
void
iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
{
char *wp;
int rtax;
wp = (char *)(ifam + 1);
for (rtax = 0; rtax < RTAX_MAX; rtax++)
if (ifam->ifam_addrs & (1 << rtax)) {
sa[rtax] = (struct sockaddr *)wp;
wp += ROUNDUP(sa[rtax]->sa_len);
} else
sa[rtax] = NULL;
}

View File

@ -26,6 +26,8 @@
* $FreeBSD$
*/
struct ifa_msghdr;
struct iface_addr {
struct in_addr ifa; /* local address */
struct in_addr mask; /* netmask */
@ -60,3 +62,4 @@ extern int iface_Show(struct cmdargs const *);
extern int iface_SetFlags(struct iface *, int);
extern int iface_ClearFlags(struct iface *, int);
extern void iface_Destroy(struct iface *);
extern void iface_ParseHdr(struct ifa_msghdr *, struct sockaddr *[RTAX_MAX]);

View File

@ -27,6 +27,8 @@
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <net/route.h>
#include <errno.h>
#include <fcntl.h>

View File

@ -28,11 +28,13 @@
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <net/route.h>
#include <errno.h>
#include <radlib.h>

View File

@ -71,6 +71,7 @@
#include "prompt.h"
#include "iface.h"
static void
p_sockaddr(struct prompt *prompt, struct sockaddr *phost,
struct sockaddr *pmask, int width)
@ -86,10 +87,10 @@ p_sockaddr(struct prompt *prompt, struct sockaddr *phost,
log_Printf(LogDEBUG, "Found the following sockaddr:\n");
log_Printf(LogDEBUG, " Family %d, len %d\n",
(int)phost->sa_family, (int)phost->sa_len);
inet_ntop(phost->sa_family, phost, tmp, sizeof tmp);
inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp);
log_Printf(LogDEBUG, " Addr %s\n", tmp);
if (pmask) {
inet_ntop(pmask->sa_family, pmask, tmp, sizeof tmp);
inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp);
log_Printf(LogDEBUG, " Mask %s\n", tmp);
}
}
@ -312,6 +313,8 @@ Index2Nam(int idx)
have = 0;
for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
ifm = (struct if_msghdr *)ptr;
if (ifm->ifm_type != RTM_IFINFO)
break;
dl = (struct sockaddr_dl *)(ifm + 1);
if (ifm->ifm_index > 0) {
if (ifm->ifm_index > have) {
@ -366,12 +369,28 @@ Index2Nam(int idx)
return ifs[idx-1];
}
void
route_ParseHdr(struct rt_msghdr *rtm, struct sockaddr *sa[RTAX_MAX])
{
char *wp;
int rtax;
wp = (char *)(rtm + 1);
for (rtax = 0; rtax < RTAX_MAX; rtax++)
if (rtm->rtm_addrs & (1 << rtax)) {
sa[rtax] = (struct sockaddr *)wp;
wp += ROUNDUP(sa[rtax]->sa_len);
} else
sa[rtax] = NULL;
}
int
route_Show(struct cmdargs const *arg)
{
struct rt_msghdr *rtm;
struct sockaddr *sa_dst, *sa_gw, *sa_mask;
char *sp, *ep, *cp, *wp;
struct sockaddr *sa[RTAX_MAX];
char *sp, *ep, *cp;
size_t needed;
int mib[6];
@ -398,36 +417,13 @@ route_Show(struct cmdargs const *arg)
prompt_Printf(arg->prompt, "%-20s%-20sFlags Netif\n",
"Destination", "Gateway");
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *) cp;
wp = (char *)(rtm+1);
rtm = (struct rt_msghdr *)cp;
/*
* This code relies on RTA_DST, RTA_GATEWAY and RTA_NETMASK
* having values 1, 2 and 4 respectively (or at least relies
* on them being the first 3 possible sockaddrs in that order) !
*/
route_ParseHdr(rtm, sa);
if (rtm->rtm_addrs & RTA_DST) {
sa_dst = (struct sockaddr *)wp;
wp += sa_dst->sa_len;
} else
sa_dst = NULL;
if (rtm->rtm_addrs & RTA_GATEWAY) {
sa_gw = (struct sockaddr *)wp;
wp += sa_gw->sa_len;
} else
sa_gw = NULL;
if (rtm->rtm_addrs & RTA_NETMASK) {
sa_mask = (struct sockaddr *)wp;
wp += sa_mask->sa_len;
} else
sa_mask = NULL;
if (sa_dst && sa_gw) {
p_sockaddr(arg->prompt, sa_dst, sa_mask, 20);
p_sockaddr(arg->prompt, sa_gw, NULL, 20);
if (sa[RTAX_DST] && sa[RTAX_GATEWAY]) {
p_sockaddr(arg->prompt, sa[RTAX_DST], sa[RTAX_NETMASK], 20);
p_sockaddr(arg->prompt, sa[RTAX_GATEWAY], NULL, 20);
p_flags(arg->prompt, rtm->rtm_flags, 6);
prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index));
@ -445,8 +441,9 @@ void
route_IfDelete(struct bundle *bundle, int all)
{
struct rt_msghdr *rtm;
struct sockaddr *sa;
struct in_addr sa_dst, sa_none;
struct sockaddr *sa[RTAX_MAX];
struct sockaddr_in **in;
struct in_addr sa_none;
int pass;
size_t needed;
char *sp, *cp, *ep;
@ -454,6 +451,7 @@ route_IfDelete(struct bundle *bundle, int all)
log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index);
sa_none.s_addr = INADDR_ANY;
in = (struct sockaddr_in **)sa;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
@ -482,7 +480,7 @@ route_IfDelete(struct bundle *bundle, int all)
for (pass = 0; pass < 2; pass++) {
/*
* We do 2 passes. The first deletes all cloned routes. The second
* deletes all non-cloned routes. This is necessary to avoid
* deletes all non-cloned routes. This is done to avoid
* potential errors from trying to delete route X after route Y where
* route X was cloned from route Y (and is no longer there 'cos it
* may have gone with route Y).
@ -491,28 +489,30 @@ route_IfDelete(struct bundle *bundle, int all)
/* So we can't tell ! */
continue;
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *) cp;
sa = (struct sockaddr *) (rtm + 1);
log_Printf(LogDEBUG, "route_IfDelete: addrs: %x, Netif: %d (%s),"
" flags: %x, dst: %s ?\n", rtm->rtm_addrs, rtm->rtm_index,
Index2Nam(rtm->rtm_index), rtm->rtm_flags,
inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
if (rtm->rtm_addrs & RTA_DST && rtm->rtm_addrs & RTA_GATEWAY &&
rtm->rtm_index == bundle->iface->index &&
(all || (rtm->rtm_flags & RTF_GATEWAY))) {
sa_dst.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
sa = (struct sockaddr *)((char *)sa + sa->sa_len);
if (sa->sa_family == AF_INET || sa->sa_family == AF_LINK) {
if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) ||
(pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) {
log_Printf(LogDEBUG, "route_IfDelete: Remove it (pass %d)\n", pass);
bundle_SetRoute(bundle, RTM_DELETE, sa_dst, sa_none, sa_none, 0, 0);
rtm = (struct rt_msghdr *)cp;
route_ParseHdr(rtm, sa);
if (sa[RTAX_DST]) {
log_Printf(LogDEBUG, "route_IfDelete: addrs: %x, Netif: %d (%s),"
" flags: %x, dst: %s ?\n", rtm->rtm_addrs, rtm->rtm_index,
Index2Nam(rtm->rtm_index), rtm->rtm_flags,
inet_ntoa(((struct sockaddr_in *)sa[RTAX_DST])->sin_addr));
if (sa[RTAX_GATEWAY] && rtm->rtm_index == bundle->iface->index &&
(all || (rtm->rtm_flags & RTF_GATEWAY))) {
if (sa[RTAX_GATEWAY]->sa_family == AF_INET ||
sa[RTAX_GATEWAY]->sa_family == AF_LINK) {
if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) ||
(pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) {
log_Printf(LogDEBUG, "route_IfDelete: Remove it (pass %d)\n",
pass);
bundle_SetRoute(bundle, RTM_DELETE, in[RTAX_DST]->sin_addr,
sa_none, sa_none, 0, 0);
} else
log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass);
} else
log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass);
} else
log_Printf(LogDEBUG,
"route_IfDelete: Can't remove routes of %d family !\n",
sa->sa_family);
log_Printf(LogDEBUG,
"route_IfDelete: Can't remove routes of %d family !\n",
sa[RTAX_GATEWAY]->sa_family);
}
}
}
}

View File

@ -23,6 +23,8 @@
struct bundle;
struct cmdargs;
struct rt_msghdr;
struct sockaddr;
#define ROUTE_STATIC 0
#define ROUTE_DSTMYADDR 1
@ -52,3 +54,4 @@ extern void route_DeleteAll(struct sticky_route **);
extern void route_Clean(struct bundle *, struct sticky_route *);
extern void route_ShowSticky(struct prompt *, struct sticky_route *,
const char *, int);
extern void route_ParseHdr(struct rt_msghdr *, struct sockaddr *[RTAX_MAX]);