1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Routing related Module
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1997-11-22 03:37:54 +00:00
|
|
|
* $Id: route.c,v 1.26 1997/11/15 02:15:56 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <sys/param.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <sys/time.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/socket.h>
|
1997-11-15 02:15:56 +00:00
|
|
|
#include <net/if_types.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
1997-11-15 02:15:56 +00:00
|
|
|
#include <net/if_dl.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <machine/endian.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
1995-07-08 06:08:52 +00:00
|
|
|
#include "log.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "loadalias.h"
|
1997-11-09 14:18:55 +00:00
|
|
|
#include "defs.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "vars.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "route.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static int IfIndex;
|
|
|
|
|
|
|
|
struct rtmsg {
|
|
|
|
struct rt_msghdr m_rtm;
|
|
|
|
char m_space[64];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int seqno;
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
OsSetRoute(int cmd,
|
|
|
|
struct in_addr dst,
|
|
|
|
struct in_addr gateway,
|
|
|
|
struct in_addr mask)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct rtmsg rtmes;
|
|
|
|
int s, nb, wb;
|
1997-11-22 03:37:54 +00:00
|
|
|
char *cp;
|
|
|
|
const char *cmdstr;
|
1995-01-31 06:29:58 +00:00
|
|
|
u_long *lp;
|
|
|
|
struct sockaddr_in rtdata;
|
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
|
|
|
|
s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
|
1997-07-28 01:02:27 +00:00
|
|
|
if (s < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "OsSetRoute: socket(): %s\n", strerror(errno));
|
1997-07-28 01:02:27 +00:00
|
|
|
return;
|
|
|
|
}
|
1997-10-26 01:04:02 +00:00
|
|
|
memset(&rtmes, '\0', sizeof(rtmes));
|
1995-01-31 06:29:58 +00:00
|
|
|
rtmes.m_rtm.rtm_version = RTM_VERSION;
|
|
|
|
rtmes.m_rtm.rtm_type = cmd;
|
1997-11-09 03:22:49 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs = RTA_DST | RTA_NETMASK;
|
1995-01-31 06:29:58 +00:00
|
|
|
rtmes.m_rtm.rtm_seq = ++seqno;
|
|
|
|
rtmes.m_rtm.rtm_pid = getpid();
|
1996-08-13 09:19:45 +00:00
|
|
|
rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
memset(&rtdata, '\0', sizeof(rtdata));
|
1995-01-31 06:29:58 +00:00
|
|
|
rtdata.sin_len = 16;
|
|
|
|
rtdata.sin_family = AF_INET;
|
|
|
|
rtdata.sin_port = 0;
|
|
|
|
rtdata.sin_addr = dst;
|
|
|
|
|
|
|
|
cp = rtmes.m_space;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(cp, &rtdata, 16);
|
1995-01-31 06:29:58 +00:00
|
|
|
cp += 16;
|
|
|
|
if (gateway.s_addr) {
|
|
|
|
rtdata.sin_addr = gateway;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(cp, &rtdata, 16);
|
1995-01-31 06:29:58 +00:00
|
|
|
cp += 16;
|
1997-11-09 03:22:49 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (dst.s_addr == INADDR_ANY)
|
|
|
|
mask.s_addr = INADDR_ANY;
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
lp = (u_long *) cp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (mask.s_addr) {
|
|
|
|
*lp++ = 8;
|
|
|
|
cp += sizeof(int);
|
|
|
|
*lp = mask.s_addr;
|
|
|
|
} else
|
|
|
|
*lp = 0;
|
|
|
|
cp += sizeof(u_long);
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
nb = cp - (char *) &rtmes;
|
1995-01-31 06:29:58 +00:00
|
|
|
rtmes.m_rtm.rtm_msglen = nb;
|
|
|
|
wb = write(s, &rtmes, nb);
|
|
|
|
if (wb < 0) {
|
1997-06-13 03:59:36 +00:00
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Dst = %s\n", inet_ntoa(dst));
|
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Gateway = %s\n", inet_ntoa(gateway));
|
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Mask = %s\n", inet_ntoa(mask));
|
1997-08-25 00:29:32 +00:00
|
|
|
switch (rtmes.m_rtm.rtm_errno) {
|
|
|
|
case EEXIST:
|
|
|
|
LogPrintf(LogTCPIP, "Add route failed: Already exists\n");
|
|
|
|
break;
|
|
|
|
case ESRCH:
|
|
|
|
LogPrintf(LogTCPIP, "Del route failed: Non-existent\n");
|
|
|
|
break;
|
1997-11-09 06:22:49 +00:00
|
|
|
case 0:
|
|
|
|
LogPrintf(LogTCPIP, "%s route failed: %s\n", cmdstr, strerror(errno));
|
|
|
|
break;
|
1997-08-25 00:29:32 +00:00
|
|
|
case ENOBUFS:
|
|
|
|
default:
|
1997-11-09 06:22:49 +00:00
|
|
|
LogPrintf(LogTCPIP, "%s route failed: %s\n",
|
|
|
|
cmdstr, strerror(rtmes.m_rtm.rtm_errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
break;
|
1997-06-13 03:59:36 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-11-09 06:22:49 +00:00
|
|
|
LogPrintf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
|
|
|
|
wb, cmdstr, dst.s_addr, gateway.s_addr);
|
1995-01-31 06:29:58 +00:00
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-11-15 02:15:56 +00:00
|
|
|
p_sockaddr(struct sockaddr *phost, struct sockaddr *pmask, int width)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-22 03:37:54 +00:00
|
|
|
char buf[29];
|
|
|
|
const char *cp;
|
1997-11-15 02:15:56 +00:00
|
|
|
struct sockaddr_in *ihost = (struct sockaddr_in *)phost;
|
|
|
|
struct sockaddr_in *mask = (struct sockaddr_in *)pmask;
|
|
|
|
struct sockaddr_dl *dl = (struct sockaddr_dl *)phost;
|
|
|
|
|
|
|
|
switch (phost->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (!phost)
|
|
|
|
cp = "";
|
|
|
|
else if (ihost->sin_addr.s_addr == INADDR_ANY)
|
|
|
|
cp = "default";
|
|
|
|
else if (!mask)
|
|
|
|
cp = inet_ntoa(ihost->sin_addr);
|
|
|
|
else {
|
|
|
|
u_int msk = ntohl(mask->sin_addr.s_addr);
|
|
|
|
u_int tst;
|
|
|
|
int bits;
|
|
|
|
int len;
|
|
|
|
struct sockaddr_in net;
|
|
|
|
|
|
|
|
for (tst = 1, bits=32; tst; tst <<= 1, bits--)
|
|
|
|
if (msk & tst)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (tst <<=1; tst; tst <<= 1)
|
|
|
|
if (!(msk & tst))
|
|
|
|
break;
|
|
|
|
|
|
|
|
net.sin_addr.s_addr = ihost->sin_addr.s_addr & mask->sin_addr.s_addr;
|
|
|
|
sprintf(buf, "%s", inet_ntoa(net.sin_addr));
|
|
|
|
for (len = strlen(buf); len > 3; buf[len-=2] = '\0')
|
|
|
|
if (strcmp(buf+len-2, ".0"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tst) /* non-contiguous :-( */
|
|
|
|
sprintf(buf+strlen(buf),"&0x%08x", msk);
|
|
|
|
else
|
|
|
|
sprintf(buf+strlen(buf), "/%d", bits);
|
|
|
|
cp = buf;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_LINK:
|
|
|
|
if (!dl)
|
|
|
|
cp = "";
|
|
|
|
else if (dl->sdl_nlen == 0 && dl->sdl_alen == 0 && dl->sdl_slen == 0) {
|
|
|
|
sprintf(buf, "link#%d", dl->sdl_index);
|
|
|
|
cp = buf;
|
|
|
|
} else if (dl->sdl_type == IFT_ETHER && dl->sdl_alen &&
|
|
|
|
dl->sdl_alen < sizeof(buf)/3) {
|
|
|
|
int f;
|
|
|
|
u_char *MAC;
|
|
|
|
|
|
|
|
MAC = (u_char *)dl->sdl_data + dl->sdl_nlen;
|
|
|
|
for (f = 0; f < dl->sdl_alen; f++)
|
|
|
|
sprintf(buf+f*3, "%02x:", MAC[f]);
|
|
|
|
buf[f*3-1] = '\0';
|
|
|
|
cp = buf;
|
|
|
|
} else
|
|
|
|
cp = "???";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cp = "???";
|
|
|
|
break;
|
1997-06-09 03:27:43 +00:00
|
|
|
}
|
1997-11-15 02:15:56 +00:00
|
|
|
|
|
|
|
fprintf(VarTerm, "%-*s ", width-1, cp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct bits {
|
1997-11-15 02:15:56 +00:00
|
|
|
u_long b_mask;
|
1997-08-25 00:29:32 +00:00
|
|
|
char b_val;
|
1997-11-15 02:15:56 +00:00
|
|
|
} bits[] = {
|
|
|
|
|
|
|
|
{ RTF_UP, 'U' },
|
|
|
|
{ RTF_GATEWAY, 'G' },
|
|
|
|
{ RTF_HOST, 'H' },
|
|
|
|
{ RTF_REJECT, 'R' },
|
|
|
|
{ RTF_DYNAMIC, 'D' },
|
|
|
|
{ RTF_MODIFIED, 'M' },
|
|
|
|
{ RTF_DONE, 'd' },
|
|
|
|
{ RTF_CLONING, 'C' },
|
|
|
|
{ RTF_XRESOLVE, 'X' },
|
|
|
|
{ RTF_LLINFO, 'L' },
|
|
|
|
{ RTF_STATIC, 'S' },
|
|
|
|
{ RTF_PROTO1, '1' },
|
|
|
|
{ RTF_PROTO2, '2' },
|
|
|
|
{ RTF_BLACKHOLE, 'B' },
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
{ RTF_WASCLONED, 'W' },
|
|
|
|
{ RTF_PRCLONING, 'c' },
|
|
|
|
{ RTF_PROTO3, '3' },
|
|
|
|
{ RTF_BROADCAST, 'b' },
|
|
|
|
#endif
|
|
|
|
{ 0, '\0' }
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
p_flags(u_long f, const char *format)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm) {
|
|
|
|
char name[33], *flags;
|
|
|
|
register struct bits *p = bits;
|
|
|
|
|
|
|
|
for (flags = name; p->b_mask; p++)
|
|
|
|
if (p->b_mask & f)
|
1997-08-25 00:29:32 +00:00
|
|
|
*flags++ = p->b_val;
|
1997-06-09 03:27:43 +00:00
|
|
|
*flags = '\0';
|
|
|
|
fprintf(VarTerm, format, name);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *
|
1997-11-15 02:15:56 +00:00
|
|
|
Index2Nam(int idx)
|
|
|
|
{
|
|
|
|
static char ifs[50][6];
|
|
|
|
static int nifs;
|
|
|
|
|
|
|
|
if (!nifs) {
|
|
|
|
int mib[6], needed, len;
|
|
|
|
char *buf, *ptr, *end;
|
|
|
|
struct if_msghdr *n;
|
|
|
|
struct sockaddr_dl *dl;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = 0;
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
|
|
|
LogPrintf(LogERROR, "Index2Nam: sysctl: estimate: %s\n", strerror(errno));
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
if ((buf = malloc(needed)) == NULL)
|
|
|
|
return "???";
|
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
|
|
|
free(buf);
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
end = buf + needed;
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
while (ptr < end) {
|
|
|
|
ifm = (struct if_msghdr *)ptr;
|
|
|
|
|
|
|
|
if (ifm->ifm_type != RTM_IFINFO) {
|
|
|
|
free(buf);
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
dl = (struct sockaddr_dl *)(ifm + 1);
|
|
|
|
ptr += ifm->ifm_msglen;
|
|
|
|
while (ptr < end) {
|
|
|
|
n = (struct if_msghdr *)ptr;
|
|
|
|
if (n->ifm_type != RTM_NEWADDR)
|
|
|
|
break;
|
|
|
|
ptr += n->ifm_msglen;
|
|
|
|
}
|
|
|
|
if ((len = dl->sdl_nlen) > sizeof(ifs[0])-1)
|
|
|
|
len = sizeof(ifs[0])-1;
|
|
|
|
strncpy(ifs[nifs], dl->sdl_data, len);
|
|
|
|
ifs[nifs++][len] = '\0';
|
|
|
|
if (nifs == sizeof(ifs)/sizeof(ifs[0]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
idx--; /* We start at 1, not 0 */
|
|
|
|
#endif
|
|
|
|
if (idx < 0 || idx >= nifs)
|
|
|
|
return "???";
|
|
|
|
return ifs[idx];
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
1997-11-22 03:37:54 +00:00
|
|
|
ShowRoute(struct cmdargs const *arg)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
1997-11-15 02:15:56 +00:00
|
|
|
struct sockaddr *sa_dst, *sa_gw, *sa_mask;
|
|
|
|
char *sp, *ep, *cp, *wp;
|
|
|
|
int needed;
|
1995-02-26 12:18:08 +00:00
|
|
|
int mib[6];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
if (!VarTerm)
|
|
|
|
return 1;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[4] = NET_RT_DUMP;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[5] = 0;
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "ShowRoute: sysctl: estimate: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (needed < 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-01-31 06:29:58 +00:00
|
|
|
sp = malloc(needed);
|
|
|
|
if (sp == NULL)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "ShowRoute: sysctl: getroute: %s\n", strerror(errno));
|
1996-10-12 16:20:34 +00:00
|
|
|
free(sp);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
ep = sp + needed;
|
|
|
|
|
1997-11-15 02:15:56 +00:00
|
|
|
fprintf(VarTerm, "%-20s%-20sFlags Netif\n", "Destination", "Gateway");
|
1995-01-31 06:29:58 +00:00
|
|
|
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
|
1997-08-25 00:29:32 +00:00
|
|
|
rtm = (struct rt_msghdr *) cp;
|
1997-11-15 02:15:56 +00:00
|
|
|
wp = (char *)(rtm+1);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
p_sockaddr(sa_dst, sa_mask, 20);
|
|
|
|
p_sockaddr(sa_gw, NULL, 20);
|
|
|
|
|
|
|
|
p_flags(rtm->rtm_flags, "%-6.6s ");
|
|
|
|
fprintf(VarTerm, "%s\n", Index2Nam(rtm->rtm_index));
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1996-10-12 16:20:34 +00:00
|
|
|
free(sp);
|
1997-06-09 03:27:43 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete routes associated with our interface
|
|
|
|
*/
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
DeleteIfRoutes(int all)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct sockaddr *sa;
|
1997-11-15 02:15:56 +00:00
|
|
|
struct in_addr sa_dst, sa_gw, sa_mask;
|
1995-01-31 06:29:58 +00:00
|
|
|
int needed;
|
|
|
|
char *sp, *cp, *ep;
|
|
|
|
u_char *wp;
|
1995-02-26 12:18:08 +00:00
|
|
|
int mib[6];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes (%d)\n", IfIndex);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[4] = NET_RT_DUMP;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[5] = 0;
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "DeleteIfRoutes: sysctl: estimate: %s\n",
|
|
|
|
strerror(errno));
|
1995-02-26 12:18:08 +00:00
|
|
|
return;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (needed < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sp = malloc(needed);
|
|
|
|
if (sp == NULL)
|
|
|
|
return;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "DeleteIfRoutes: sysctl: getroute: %s\n",
|
|
|
|
strerror(errno));
|
1995-01-31 06:29:58 +00:00
|
|
|
free(sp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ep = sp + needed;
|
|
|
|
|
|
|
|
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
|
1997-08-25 00:29:32 +00:00
|
|
|
rtm = (struct rt_msghdr *) cp;
|
|
|
|
sa = (struct sockaddr *) (rtm + 1);
|
1997-11-15 02:15:56 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: addrs: %x, Netif: %d (%s), flags: %x,"
|
|
|
|
" dst: %s ?\n", rtm->rtm_addrs, rtm->rtm_index,
|
|
|
|
Index2Nam(rtm->rtm_index), rtm->rtm_flags,
|
1997-08-25 00:29:32 +00:00
|
|
|
inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
|
1997-11-15 02:15:56 +00:00
|
|
|
if (rtm->rtm_addrs & RTA_DST && rtm->rtm_addrs & RTA_GATEWAY &&
|
|
|
|
rtm->rtm_index == IfIndex &&
|
1997-08-25 00:29:32 +00:00
|
|
|
(all || (rtm->rtm_flags & RTF_GATEWAY))) {
|
1997-11-15 02:15:56 +00:00
|
|
|
sa_dst.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
|
1997-08-25 00:29:32 +00:00
|
|
|
wp = (u_char *) cp + rtm->rtm_msglen;
|
1997-11-15 02:15:56 +00:00
|
|
|
sa = (struct sockaddr *)((char *)sa + sa->sa_len);
|
|
|
|
if (sa->sa_family == AF_INET) {
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Remove it\n");
|
|
|
|
sa_gw.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
|
|
|
|
sa = (struct sockaddr *)((char *)sa + sa->sa_len);
|
|
|
|
if (rtm->rtm_addrs & RTA_NETMASK)
|
|
|
|
sa_mask.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
|
|
|
|
else
|
|
|
|
sa_mask.s_addr = 0xffffffff;
|
|
|
|
if (sa_dst.s_addr == INADDR_ANY)
|
|
|
|
sa_mask.s_addr = INADDR_ANY;
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Dst: %s\n", inet_ntoa(sa_dst));
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Gw: %s\n", inet_ntoa(sa_gw));
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Index: %d\n", rtm->rtm_index);
|
|
|
|
OsSetRoute(RTM_DELETE, sa_dst, sa_gw, sa_mask);
|
|
|
|
} else
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Can't remove an AF_LINK !\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-08-25 00:29:32 +00:00
|
|
|
GetIfIndex(char *name)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-15 02:15:56 +00:00
|
|
|
int idx;
|
1997-11-22 03:37:54 +00:00
|
|
|
const char *got;
|
1997-11-15 02:15:56 +00:00
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
idx = 1; /* We start at 1, not 0 */
|
|
|
|
#else
|
|
|
|
idx = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (strcmp(got = Index2Nam(idx), "???"))
|
|
|
|
if (!strcmp(got, name))
|
|
|
|
return IfIndex = idx;
|
|
|
|
else
|
|
|
|
idx++;
|
|
|
|
return -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|