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.
|
|
|
|
*
|
1998-04-25 00:20:23 +00:00
|
|
|
* $Id: route.c,v 1.43 1998/03/13 01:36:10 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.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-12-13 02:37:33 +00:00
|
|
|
#include "os.h"
|
|
|
|
#include "ipcp.h"
|
|
|
|
#include "iplist.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,
|
1997-12-30 02:45:48 +00:00
|
|
|
struct in_addr mask,
|
|
|
|
int bang)
|
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
|
|
|
struct sockaddr_in rtdata;
|
|
|
|
|
1997-12-30 02:45:48 +00:00
|
|
|
if (bang)
|
|
|
|
cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
|
|
|
|
else
|
|
|
|
cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
|
1997-11-09 06:22:49 +00:00
|
|
|
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-12-24 09:29:17 +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-12-07 04:09:15 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs = RTA_DST;
|
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-12-24 09:29:17 +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;
|
1998-03-13 01:36:10 +00:00
|
|
|
if (cmd == RTM_ADD) {
|
1997-12-07 04:09:15 +00:00
|
|
|
if (gateway.s_addr == INADDR_ANY) {
|
|
|
|
/* Add a route through the interface */
|
|
|
|
struct sockaddr_dl dl;
|
|
|
|
const char *iname;
|
|
|
|
int ilen;
|
|
|
|
|
|
|
|
iname = Index2Nam(IfIndex);
|
|
|
|
ilen = strlen(iname);
|
1997-12-24 09:29:17 +00:00
|
|
|
dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen;
|
1997-12-07 04:09:15 +00:00
|
|
|
dl.sdl_family = AF_LINK;
|
|
|
|
dl.sdl_index = IfIndex;
|
|
|
|
dl.sdl_type = 0;
|
|
|
|
dl.sdl_nlen = ilen;
|
|
|
|
dl.sdl_alen = 0;
|
|
|
|
dl.sdl_slen = 0;
|
1997-12-24 09:29:17 +00:00
|
|
|
strncpy(dl.sdl_data, iname, sizeof dl.sdl_data);
|
1997-12-07 04:09:15 +00:00
|
|
|
|
|
|
|
memcpy(cp, &dl, dl.sdl_len);
|
|
|
|
cp += dl.sdl_len;
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
|
|
|
|
} else {
|
|
|
|
rtdata.sin_addr = gateway;
|
|
|
|
memcpy(cp, &rtdata, 16);
|
|
|
|
cp += 16;
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
|
|
|
|
}
|
1998-03-13 01:36:10 +00:00
|
|
|
}
|
1997-12-07 04:09:15 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (dst.s_addr == INADDR_ANY)
|
|
|
|
mask.s_addr = INADDR_ANY;
|
|
|
|
|
1997-12-07 04:09:15 +00:00
|
|
|
if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
|
|
|
|
rtdata.sin_addr = mask;
|
|
|
|
memcpy(cp, &rtdata, 16);
|
|
|
|
cp += 16;
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
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;
|
1997-12-27 19:23:13 +00:00
|
|
|
wb = ID0write(s, &rtmes, nb);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (wb < 0) {
|
1997-12-15 20:21:15 +00:00
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute failure:\n");
|
1998-04-25 00:20:23 +00:00
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Cmd = %s\n", cmdstr);
|
1997-12-15 20:21:15 +00:00
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Dst = %s\n", inet_ntoa(dst));
|
1997-06-13 03:59:36 +00:00
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Gateway = %s\n", inet_ntoa(gateway));
|
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Mask = %s\n", inet_ntoa(mask));
|
1997-12-30 02:45:48 +00:00
|
|
|
failed:
|
|
|
|
if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
|
1998-03-13 01:36:10 +00:00
|
|
|
(rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
|
1997-12-30 02:45:48 +00:00
|
|
|
if (!bang)
|
|
|
|
LogPrintf(LogWARN, "Add route failed: %s already exists\n",
|
|
|
|
inet_ntoa(dst));
|
|
|
|
else {
|
|
|
|
rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
|
|
|
|
if ((wb = ID0write(s, &rtmes, nb)) < 0)
|
|
|
|
goto failed;
|
|
|
|
}
|
1998-03-13 01:36:10 +00:00
|
|
|
} else if (cmd == RTM_DELETE &&
|
1997-12-30 02:45:48 +00:00
|
|
|
(rtmes.m_rtm.rtm_errno == ESRCH ||
|
|
|
|
(rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
|
|
|
|
if (!bang)
|
|
|
|
LogPrintf(LogWARN, "Del route failed: %s: Non-existent\n",
|
|
|
|
inet_ntoa(dst));
|
|
|
|
} else if (rtmes.m_rtm.rtm_errno == 0)
|
1997-12-15 20:21:15 +00:00
|
|
|
LogPrintf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
|
|
|
|
inet_ntoa(dst), strerror(errno));
|
1997-12-30 02:45:48 +00:00
|
|
|
else
|
1997-12-15 20:21:15 +00:00
|
|
|
LogPrintf(LogWARN, "%s route failed: %s: %s\n",
|
|
|
|
cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
|
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];
|
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)
|
1997-12-04 18:49:39 +00:00
|
|
|
buf[0] = '\0';
|
1997-11-15 02:15:56 +00:00
|
|
|
else if (ihost->sin_addr.s_addr == INADDR_ANY)
|
1997-12-04 18:49:39 +00:00
|
|
|
strcpy(buf, "default");
|
1997-11-15 02:15:56 +00:00
|
|
|
else if (!mask)
|
1997-12-04 18:49:39 +00:00
|
|
|
strcpy(buf, inet_ntoa(ihost->sin_addr));
|
1997-11-15 02:15:56 +00:00
|
|
|
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;
|
1997-12-17 00:19:25 +00:00
|
|
|
strcpy(buf, inet_ntoa(net.sin_addr));
|
1997-11-15 02:15:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_LINK:
|
1997-12-04 18:49:39 +00:00
|
|
|
if (dl->sdl_nlen)
|
|
|
|
snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data);
|
1998-03-13 01:36:10 +00:00
|
|
|
else if (dl->sdl_alen) {
|
|
|
|
if (dl->sdl_type == IFT_ETHER) {
|
1997-12-24 09:29:17 +00:00
|
|
|
if (dl->sdl_alen < sizeof buf / 3) {
|
1997-12-04 18:49:39 +00:00
|
|
|
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';
|
|
|
|
} else
|
1997-12-17 00:19:25 +00:00
|
|
|
strcpy(buf, "??:??:??:??:??:??");
|
1998-03-13 01:36:10 +00:00
|
|
|
} else
|
1997-12-04 18:49:39 +00:00
|
|
|
sprintf(buf, "<IFT type %d>", dl->sdl_type);
|
1998-03-13 01:36:10 +00:00
|
|
|
} else if (dl->sdl_slen)
|
1997-12-04 18:49:39 +00:00
|
|
|
sprintf(buf, "<slen %d?>", dl->sdl_slen);
|
|
|
|
else
|
1997-11-15 02:15:56 +00:00
|
|
|
sprintf(buf, "link#%d", dl->sdl_index);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-12-04 18:49:39 +00:00
|
|
|
sprintf(buf, "<AF type %d>", phost->sa_family);
|
1997-11-15 02:15:56 +00:00
|
|
|
break;
|
1997-06-09 03:27:43 +00:00
|
|
|
}
|
1997-11-15 02:15:56 +00:00
|
|
|
|
1997-12-04 18:49:39 +00:00
|
|
|
fprintf(VarTerm, "%-*s ", width-1, buf);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-01-21 02:15:33 +00:00
|
|
|
static 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' },
|
1997-12-15 20:21:15 +00:00
|
|
|
#ifdef RTF_WASCLONED
|
1997-11-15 02:15:56 +00:00
|
|
|
{ RTF_WASCLONED, 'W' },
|
1997-12-15 20:21:15 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RTF_PRCLONING
|
1997-11-15 02:15:56 +00:00
|
|
|
{ RTF_PRCLONING, 'c' },
|
1997-12-15 20:21:15 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RTF_PROTO3
|
1997-11-15 02:15:56 +00:00
|
|
|
{ RTF_PROTO3, '3' },
|
1997-12-15 20:21:15 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RTF_BROADCAST
|
1997-11-15 02:15:56 +00:00
|
|
|
{ RTF_BROADCAST, 'b' },
|
|
|
|
#endif
|
|
|
|
{ 0, '\0' }
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
1997-12-15 20:21:15 +00:00
|
|
|
#ifndef RTF_WASCLONED
|
|
|
|
#define RTF_WASCLONED (0)
|
|
|
|
#endif
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
static void
|
1997-12-17 21:22:05 +00:00
|
|
|
p_flags(u_long f, int max)
|
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;
|
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
if (max > sizeof name - 1)
|
|
|
|
max = sizeof name - 1;
|
1997-12-17 21:22:05 +00:00
|
|
|
|
|
|
|
for (flags = name; p->b_mask && flags - name < max; p++)
|
1997-06-09 03:27:43 +00:00
|
|
|
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';
|
1997-12-17 21:22:05 +00:00
|
|
|
fprintf(VarTerm, "%-*.*s", max, max, name);
|
1997-06-09 03:27:43 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-01-19 02:59:34 +00:00
|
|
|
const char *
|
1997-11-15 02:15:56 +00:00
|
|
|
Index2Nam(int idx)
|
|
|
|
{
|
1998-01-11 04:45:36 +00:00
|
|
|
static char **ifs;
|
1997-11-22 13:46:02 +00:00
|
|
|
static int nifs, debug_done;
|
1997-11-15 02:15:56 +00:00
|
|
|
|
|
|
|
if (!nifs) {
|
1998-01-11 04:45:36 +00:00
|
|
|
int mib[6], have, had;
|
1997-12-27 13:45:57 +00:00
|
|
|
size_t needed;
|
1997-11-15 02:15:56 +00:00
|
|
|
char *buf, *ptr, *end;
|
|
|
|
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;
|
|
|
|
|
1998-01-11 04:45:36 +00:00
|
|
|
have = 0;
|
1997-11-22 13:46:02 +00:00
|
|
|
for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
|
1997-11-15 02:15:56 +00:00
|
|
|
ifm = (struct if_msghdr *)ptr;
|
|
|
|
dl = (struct sockaddr_dl *)(ifm + 1);
|
1998-01-11 04:45:36 +00:00
|
|
|
if (ifm->ifm_index > 0) {
|
|
|
|
if (ifm->ifm_index > have) {
|
|
|
|
had = have;
|
|
|
|
have = ifm->ifm_index + 5;
|
|
|
|
if (had)
|
|
|
|
ifs = (char **)realloc(ifs, sizeof(char *) * have);
|
|
|
|
else
|
|
|
|
ifs = (char **)malloc(sizeof(char *) * have);
|
|
|
|
if (!ifs) {
|
|
|
|
LogPrintf(LogDEBUG, "Index2Nam: %s\n", strerror(errno));
|
|
|
|
nifs = 0;
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
memset(ifs + had, '\0', sizeof(char *) * (have - had));
|
|
|
|
}
|
|
|
|
if (ifs[ifm->ifm_index-1] == NULL) {
|
|
|
|
ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1);
|
|
|
|
memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen);
|
|
|
|
ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0';
|
|
|
|
if (nifs < ifm->ifm_index)
|
|
|
|
nifs = ifm->ifm_index;
|
|
|
|
}
|
1997-11-22 13:46:02 +00:00
|
|
|
} else if (LogIsKept(LogDEBUG))
|
|
|
|
LogPrintf(LogDEBUG, "Skipping out-of-range interface %d!\n",
|
|
|
|
ifm->ifm_index);
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
1997-11-22 13:46:02 +00:00
|
|
|
if (LogIsKept(LogDEBUG) && !debug_done) {
|
|
|
|
int f;
|
|
|
|
|
|
|
|
LogPrintf(LogDEBUG, "Found the following interfaces:\n");
|
|
|
|
for (f = 0; f < nifs; f++)
|
1998-01-11 04:45:36 +00:00
|
|
|
if (ifs[f] != NULL)
|
1997-11-22 13:46:02 +00:00
|
|
|
LogPrintf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]);
|
|
|
|
debug_done = 1;
|
|
|
|
}
|
|
|
|
|
1998-01-11 04:45:36 +00:00
|
|
|
if (idx < 1 || idx > nifs || ifs[idx-1] == NULL)
|
1997-11-15 02:15:56 +00:00
|
|
|
return "???";
|
1997-11-22 13:46:02 +00:00
|
|
|
|
|
|
|
return ifs[idx-1];
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
|
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;
|
1997-12-27 13:45:57 +00:00
|
|
|
size_t 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);
|
|
|
|
|
1997-12-17 21:22:05 +00:00
|
|
|
p_flags(rtm->rtm_flags, 6);
|
|
|
|
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-12-07 04:09:15 +00:00
|
|
|
struct in_addr sa_dst, sa_none;
|
1997-12-27 13:45:57 +00:00
|
|
|
int pass;
|
|
|
|
size_t needed;
|
1995-01-31 06:29:58 +00:00
|
|
|
char *sp, *cp, *ep;
|
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);
|
1997-12-07 04:09:15 +00:00
|
|
|
sa_none.s_addr = INADDR_ANY;
|
1997-06-09 03:27:43 +00:00
|
|
|
|
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;
|
|
|
|
|
1997-12-15 20:21:15 +00:00
|
|
|
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
|
|
|
|
* potential errors from trying to delete route X after route Y where
|
|
|
|
* route X was cloned from route Y (which is no longer there).
|
|
|
|
*/
|
|
|
|
if (RTF_WASCLONED == 0 && pass == 0)
|
|
|
|
/* 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);
|
|
|
|
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,
|
|
|
|
inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
|
|
|
|
if (rtm->rtm_addrs & RTA_DST && rtm->rtm_addrs & RTA_GATEWAY &&
|
|
|
|
rtm->rtm_index == IfIndex &&
|
|
|
|
(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))) {
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Remove it (pass %d)\n", pass);
|
1997-12-30 02:45:48 +00:00
|
|
|
OsSetRoute(RTM_DELETE, sa_dst, sa_none, sa_none, 0);
|
1997-12-15 20:21:15 +00:00
|
|
|
} else
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Skip it (pass %d)\n", pass);
|
|
|
|
} else
|
|
|
|
LogPrintf(LogDEBUG,
|
|
|
|
"DeleteIfRoutes: Can't remove routes of %d family !\n",
|
|
|
|
sa->sa_family);
|
|
|
|
}
|
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
|
|
|
|
1997-11-22 13:46:02 +00:00
|
|
|
idx = 1;
|
1997-11-15 02:15:56 +00:00
|
|
|
while (strcmp(got = Index2Nam(idx), "???"))
|
|
|
|
if (!strcmp(got, name))
|
|
|
|
return IfIndex = idx;
|
|
|
|
else
|
|
|
|
idx++;
|
|
|
|
return -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-12-13 02:37:33 +00:00
|
|
|
|
|
|
|
struct in_addr
|
|
|
|
ChooseHisAddr(const struct in_addr gw)
|
|
|
|
{
|
|
|
|
struct in_addr try;
|
|
|
|
int f;
|
|
|
|
|
|
|
|
for (f = 0; f < DefHisChoice.nItems; f++) {
|
|
|
|
try = iplist_next(&DefHisChoice);
|
|
|
|
LogPrintf(LogDEBUG, "ChooseHisAddr: Check item %d (%s)\n",
|
|
|
|
f, inet_ntoa(try));
|
|
|
|
if (OsTrySetIpaddress(gw, try) == 0) {
|
|
|
|
LogPrintf(LogIPCP, "ChooseHisAddr: Selected IP address %s\n",
|
|
|
|
inet_ntoa(try));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f == DefHisChoice.nItems) {
|
|
|
|
LogPrintf(LogDEBUG, "ChooseHisAddr: All addresses in use !\n");
|
|
|
|
try.s_addr = INADDR_ANY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return try;
|
|
|
|
}
|