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-07-28 21:54:54 +00:00
|
|
|
* $Id: route.c,v 1.51 1998/06/27 23:48:53 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
|
|
|
|
1998-04-07 00:54:26 +00:00
|
|
|
#include <sys/types.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>
|
1998-03-16 22:54:35 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/sysctl.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-06-15 19:06:25 +00:00
|
|
|
#include "defs.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-12-13 02:37:33 +00:00
|
|
|
#include "iplist.h"
|
1998-01-29 23:11:44 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "throughput.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-01-30 19:46:07 +00:00
|
|
|
#include "hdlc.h"
|
1998-01-31 02:48:30 +00:00
|
|
|
#include "fsm.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "lcp.h"
|
|
|
|
#include "ccp.h"
|
|
|
|
#include "link.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1998-01-29 23:11:44 +00:00
|
|
|
#include "ipcp.h"
|
1998-03-16 22:52:54 +00:00
|
|
|
#include "filter.h"
|
1998-03-20 19:48:28 +00:00
|
|
|
#include "descriptor.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "mp.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "route.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "prompt.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static void
|
1998-04-03 19:26:02 +00:00
|
|
|
p_sockaddr(struct prompt *prompt, 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 {
|
1998-06-27 23:48:54 +00:00
|
|
|
u_int32_t msk = ntohl(mask->sin_addr.s_addr);
|
|
|
|
u_int32_t tst;
|
1997-11-15 02:15:56 +00:00
|
|
|
int bits;
|
|
|
|
int len;
|
|
|
|
struct sockaddr_in net;
|
|
|
|
|
|
|
|
for (tst = 1, bits=32; tst; tst <<= 1, bits--)
|
|
|
|
if (msk & tst)
|
|
|
|
break;
|
|
|
|
|
1998-06-27 23:48:54 +00:00
|
|
|
for (tst <<= 1; tst; tst <<= 1)
|
1997-11-15 02:15:56 +00:00
|
|
|
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));
|
1998-06-27 23:48:54 +00:00
|
|
|
for (len = strlen(buf); len > 3; buf[len -= 2] = '\0')
|
|
|
|
if (strcmp(buf + len - 2, ".0"))
|
1997-11-15 02:15:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (tst) /* non-contiguous :-( */
|
1998-06-27 23:48:54 +00:00
|
|
|
sprintf(buf + strlen(buf),"&0x%08lx", (u_long)msk);
|
1997-11-15 02:15:56 +00:00
|
|
|
else
|
1998-06-27 23:48:54 +00:00
|
|
|
sprintf(buf + strlen(buf), "/%d", bits);
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
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-04-25 10:49:52 +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
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "%-*s ", width-1, buf);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-01-21 02:15:33 +00:00
|
|
|
static struct bits {
|
1998-06-27 12:03:50 +00:00
|
|
|
u_int32_t 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
|
1998-06-27 12:03:50 +00:00
|
|
|
p_flags(struct prompt *prompt, u_int32_t f, int max)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-02-10 03:23:50 +00:00
|
|
|
char name[33], *flags;
|
|
|
|
register struct bits *p = bits;
|
|
|
|
|
|
|
|
if (max > sizeof name - 1)
|
|
|
|
max = sizeof name - 1;
|
|
|
|
|
|
|
|
for (flags = name; p->b_mask && flags - name < max; p++)
|
|
|
|
if (p->b_mask & f)
|
|
|
|
*flags++ = p->b_val;
|
|
|
|
*flags = '\0';
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "%-*.*s", max, max, name);
|
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-06-15 19:06:58 +00:00
|
|
|
/*
|
|
|
|
* XXX: Maybe we should select() on the routing socket so that we can
|
|
|
|
* notice interfaces that come & go (PCCARD support).
|
|
|
|
* Or we could even support a signal that resets these so that
|
|
|
|
* the PCCARD insert/remove events can signal ppp.
|
|
|
|
*/
|
|
|
|
static char **ifs; /* Figure these out once */
|
|
|
|
static int nifs, debug_done; /* Figure out how many once, and debug once */
|
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) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n",
|
|
|
|
strerror(errno));
|
1997-11-15 02:15:56 +00:00
|
|
|
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) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno));
|
1998-01-11 04:45:36 +00:00
|
|
|
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;
|
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
} else if (log_IsKept(LogDEBUG))
|
|
|
|
log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n",
|
1997-11-22 13:46:02 +00:00
|
|
|
ifm->ifm_index);
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogDEBUG) && !debug_done) {
|
1997-11-22 13:46:02 +00:00
|
|
|
int f;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Found the following interfaces:\n");
|
1997-11-22 13:46:02 +00:00
|
|
|
for (f = 0; f < nifs; f++)
|
1998-01-11 04:45:36 +00:00
|
|
|
if (ifs[f] != NULL)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]);
|
1997-11-22 13:46:02 +00:00
|
|
|
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
|
1998-05-01 19:26:12 +00:00
|
|
|
route_Show(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
|
|
|
|
|
|
|
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) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "route_Show: 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
|
|
|
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) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "route_Show: 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;
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(arg->prompt, "%-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;
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
p_sockaddr(arg->prompt, sa_dst, sa_mask, 20);
|
|
|
|
p_sockaddr(arg->prompt, sa_gw, NULL, 20);
|
1997-11-15 02:15:56 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
p_flags(arg->prompt, rtm->rtm_flags, 6);
|
|
|
|
prompt_Printf(arg->prompt, " %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
|
1998-05-01 19:26:12 +00:00
|
|
|
route_IfDelete(struct bundle *bundle, 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
|
|
|
|
1998-05-06 23:50:25 +00:00
|
|
|
log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->ifp.Index);
|
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) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n",
|
1997-08-31 22:59:49 +00:00
|
|
|
strerror(errno));
|
1995-02-26 12:18:08 +00:00
|
|
|
return;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
sp = malloc(needed);
|
|
|
|
if (sp == NULL)
|
|
|
|
return;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n",
|
1997-08-31 22:59:49 +00:00
|
|
|
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
|
1998-04-19 03:41:01 +00:00
|
|
|
* route X was cloned from route Y (and is no longer there 'cos it
|
|
|
|
* may have gone with route Y).
|
1997-12-15 20:21:15 +00:00
|
|
|
*/
|
|
|
|
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);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "route_IfDelete: addrs: %x, Netif: %d (%s),"
|
1997-12-15 20:21:15 +00:00
|
|
|
" 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 &&
|
1998-05-06 23:50:25 +00:00
|
|
|
rtm->rtm_index == bundle->ifp.Index &&
|
1997-12-15 20:21:15 +00:00
|
|
|
(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))) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "route_IfDelete: Remove it (pass %d)\n", pass);
|
1998-07-28 21:54:54 +00:00
|
|
|
bundle_SetRoute(bundle, RTM_DELETE, sa_dst, sa_none, sa_none, 0, 0);
|
1997-12-15 20:21:15 +00:00
|
|
|
} else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass);
|
1997-12-15 20:21:15 +00:00
|
|
|
} else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG,
|
|
|
|
"route_IfDelete: Can't remove routes of %d family !\n",
|
1997-12-15 20:21:15 +00:00
|
|
|
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))
|
1998-02-02 19:32:16 +00:00
|
|
|
return idx;
|
1997-11-15 02:15:56 +00:00
|
|
|
else
|
|
|
|
idx++;
|
|
|
|
return -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-12-13 02:37:33 +00:00
|
|
|
|
1998-05-05 23:30:13 +00:00
|
|
|
void
|
|
|
|
route_Change(struct bundle *bundle, struct sticky_route *r,
|
|
|
|
struct in_addr me, struct in_addr peer)
|
|
|
|
{
|
|
|
|
struct in_addr none, del;
|
|
|
|
|
|
|
|
none.s_addr = INADDR_ANY;
|
|
|
|
for (; r; r = r->next) {
|
|
|
|
if ((r->type & ROUTE_DSTMYADDR) && r->dst.s_addr != me.s_addr) {
|
|
|
|
del.s_addr = r->dst.s_addr & r->mask.s_addr;
|
1998-07-28 21:54:54 +00:00
|
|
|
bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1, 0);
|
1998-05-05 23:30:13 +00:00
|
|
|
r->dst = me;
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
r->gw = peer;
|
|
|
|
} else if ((r->type & ROUTE_DSTHISADDR) && r->dst.s_addr != peer.s_addr) {
|
|
|
|
del.s_addr = r->dst.s_addr & r->mask.s_addr;
|
1998-07-28 21:54:54 +00:00
|
|
|
bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1, 0);
|
1998-05-05 23:30:13 +00:00
|
|
|
r->dst = peer;
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
r->gw = peer;
|
|
|
|
} else if ((r->type & ROUTE_GWHISADDR) && r->gw.s_addr != peer.s_addr)
|
|
|
|
r->gw = peer;
|
1998-07-28 21:54:54 +00:00
|
|
|
bundle_SetRoute(bundle, RTM_ADD, r->dst, r->gw, r->mask, 1, 0);
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
route_Clean(struct bundle *bundle, struct sticky_route *r)
|
|
|
|
{
|
|
|
|
struct in_addr none, del;
|
|
|
|
|
|
|
|
none.s_addr = INADDR_ANY;
|
|
|
|
for (; r; r = r->next) {
|
|
|
|
del.s_addr = r->dst.s_addr & r->mask.s_addr;
|
1998-07-28 21:54:54 +00:00
|
|
|
bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1, 0);
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
route_Add(struct sticky_route **rp, int type, struct in_addr dst,
|
|
|
|
struct in_addr mask, struct in_addr gw)
|
|
|
|
{
|
|
|
|
if (type != ROUTE_STATIC) {
|
|
|
|
struct sticky_route *r;
|
|
|
|
int dsttype = type & ROUTE_DSTANY;
|
|
|
|
|
|
|
|
r = NULL;
|
|
|
|
while (*rp) {
|
1998-05-06 00:38:55 +00:00
|
|
|
if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
|
|
|
|
(!dsttype && (*rp)->dst.s_addr == dst.s_addr)) {
|
1998-05-05 23:30:13 +00:00
|
|
|
r = *rp;
|
|
|
|
*rp = r->next;
|
|
|
|
} else
|
|
|
|
rp = &(*rp)->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
r = (struct sticky_route *)malloc(sizeof(struct sticky_route));
|
|
|
|
r->type = type;
|
|
|
|
r->next = NULL;
|
|
|
|
r->dst = dst;
|
|
|
|
r->mask = mask;
|
|
|
|
r->gw = gw;
|
|
|
|
*rp = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
route_Delete(struct sticky_route **rp, int type, struct in_addr dst)
|
1997-12-13 02:37:33 +00:00
|
|
|
{
|
1998-05-05 23:30:13 +00:00
|
|
|
struct sticky_route *r;
|
|
|
|
int dsttype = type & ROUTE_DSTANY;
|
|
|
|
|
|
|
|
for (; *rp; rp = &(*rp)->next) {
|
|
|
|
if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
|
|
|
|
(!dsttype && dst.s_addr == ((*rp)->dst.s_addr & (*rp)->mask.s_addr))) {
|
|
|
|
r = *rp;
|
|
|
|
*rp = r->next;
|
|
|
|
free(r);
|
1997-12-13 02:37:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
route_DeleteAll(struct sticky_route **rp)
|
|
|
|
{
|
|
|
|
struct sticky_route *r, *rn;
|
1997-12-13 02:37:33 +00:00
|
|
|
|
1998-05-05 23:30:13 +00:00
|
|
|
for (r = *rp; r; r = rn) {
|
|
|
|
rn = r->next;
|
|
|
|
free(r);
|
1997-12-13 02:37:33 +00:00
|
|
|
}
|
1998-05-05 23:30:13 +00:00
|
|
|
*rp = NULL;
|
|
|
|
}
|
1997-12-13 02:37:33 +00:00
|
|
|
|
1998-05-05 23:30:13 +00:00
|
|
|
void
|
|
|
|
route_ShowSticky(struct prompt *p, struct sticky_route *r)
|
|
|
|
{
|
|
|
|
int def;
|
|
|
|
|
|
|
|
prompt_Printf(p, "Sticky routes:\n");
|
|
|
|
for (; r; r = r->next) {
|
|
|
|
def = r->dst.s_addr == INADDR_ANY && r->mask.s_addr == INADDR_ANY;
|
|
|
|
|
|
|
|
prompt_Printf(p, " add ");
|
|
|
|
if (r->type & ROUTE_DSTMYADDR)
|
|
|
|
prompt_Printf(p, "MYADDR");
|
|
|
|
else if (r->type & ROUTE_DSTHISADDR)
|
|
|
|
prompt_Printf(p, "HISADDR");
|
|
|
|
else if (!def)
|
|
|
|
prompt_Printf(p, "%s", inet_ntoa(r->dst));
|
|
|
|
|
|
|
|
if (def)
|
|
|
|
prompt_Printf(p, "default ");
|
|
|
|
else
|
|
|
|
prompt_Printf(p, " %s ", inet_ntoa(r->mask));
|
|
|
|
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
prompt_Printf(p, "HISADDR\n");
|
|
|
|
else
|
|
|
|
prompt_Printf(p, "%s\n", inet_ntoa(r->gw));
|
|
|
|
}
|
1997-12-13 02:37:33 +00:00
|
|
|
}
|