2001-06-13 21:52:19 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
|
|
|
|
* Internet Initiative Japan, Inc (IIJ)
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.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>
|
2002-08-27 20:11:58 +00:00
|
|
|
#include <stdarg.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/sysctl.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <termios.h>
|
2001-04-05 02:23:48 +00:00
|
|
|
#include <unistd.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
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"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ncpaddr.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"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ipv6cp.h"
|
|
|
|
#include "ncp.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"
|
1998-10-22 02:32:50 +00:00
|
|
|
#include "iface.h"
|
2001-04-05 02:23:48 +00:00
|
|
|
#include "id.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2000-03-14 01:46:44 +00:00
|
|
|
|
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
|
|
|
{
|
2001-08-14 16:05:52 +00:00
|
|
|
struct ncprange range;
|
1997-11-22 03:37:54 +00:00
|
|
|
char buf[29];
|
1997-11-15 02:15:56 +00:00
|
|
|
struct sockaddr_dl *dl = (struct sockaddr_dl *)phost;
|
|
|
|
|
2000-03-14 01:46:38 +00:00
|
|
|
if (log_IsKept(LogDEBUG)) {
|
|
|
|
char tmp[50];
|
|
|
|
|
|
|
|
log_Printf(LogDEBUG, "Found the following sockaddr:\n");
|
|
|
|
log_Printf(LogDEBUG, " Family %d, len %d\n",
|
|
|
|
(int)phost->sa_family, (int)phost->sa_len);
|
2000-03-14 01:46:44 +00:00
|
|
|
inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp);
|
2000-03-14 01:46:38 +00:00
|
|
|
log_Printf(LogDEBUG, " Addr %s\n", tmp);
|
|
|
|
if (pmask) {
|
2000-03-14 01:46:44 +00:00
|
|
|
inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp);
|
2000-03-14 01:46:38 +00:00
|
|
|
log_Printf(LogDEBUG, " Mask %s\n", tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-15 02:15:56 +00:00
|
|
|
switch (phost->sa_family) {
|
|
|
|
case AF_INET:
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
case AF_INET6:
|
|
|
|
#endif
|
|
|
|
ncprange_setsa(&range, phost, pmask);
|
|
|
|
if (ncprange_isdefault(&range))
|
|
|
|
prompt_Printf(prompt, "%-*s ", width - 1, "default");
|
|
|
|
else
|
|
|
|
prompt_Printf(prompt, "%-*s ", width - 1, ncprange_ntoa(&range));
|
|
|
|
return;
|
1997-11-15 02:15:56 +00:00
|
|
|
|
|
|
|
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
|
2001-04-05 02:23:48 +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_XRESOLVE, 'X' },
|
|
|
|
{ RTF_STATIC, 'S' },
|
|
|
|
{ RTF_PROTO1, '1' },
|
|
|
|
{ RTF_PROTO2, '2' },
|
|
|
|
{ RTF_BLACKHOLE, 'B' },
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
#ifdef RTF_LLINFO
|
|
|
|
{ RTF_LLINFO, 'L' },
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_CLONING
|
|
|
|
{ RTF_CLONING, 'C' },
|
|
|
|
#endif
|
1997-12-15 20:21:15 +00:00
|
|
|
#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
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2004-09-05 01:46:52 +00:00
|
|
|
p_flags(struct prompt *prompt, u_int32_t f, unsigned 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;
|
|
|
|
|
2004-09-05 01:46:52 +00:00
|
|
|
for (flags = name; p->b_mask && flags - name < (int)max; p++)
|
1998-02-10 03:23:50 +00:00
|
|
|
if (p->b_mask & f)
|
|
|
|
*flags++ = p->b_val;
|
|
|
|
*flags = '\0';
|
2005-01-10 11:12:36 +00:00
|
|
|
prompt_Printf(prompt, "%-*.*s", (int)max, (int)max, name);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
2002-05-14 12:55:39 +00:00
|
|
|
static int route_nifs = -1;
|
|
|
|
|
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 */
|
2002-05-14 12:55:39 +00:00
|
|
|
static int debug_done; /* Debug once */
|
1997-11-15 02:15:56 +00:00
|
|
|
|
2002-05-14 12:55:39 +00:00
|
|
|
if (idx > route_nifs || (idx > 0 && ifs[idx-1] == NULL)) {
|
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;
|
|
|
|
|
1999-08-26 20:01:20 +00:00
|
|
|
if (ifs) {
|
|
|
|
free(ifs);
|
|
|
|
ifs = NULL;
|
2002-05-14 12:55:39 +00:00
|
|
|
route_nifs = 0;
|
1999-08-26 20:01:20 +00:00
|
|
|
}
|
|
|
|
debug_done = 0;
|
|
|
|
|
1997-11-15 02:15:56 +00:00
|
|
|
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));
|
2000-03-14 01:46:54 +00:00
|
|
|
return NumStr(idx, NULL, 0);
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
if ((buf = malloc(needed)) == NULL)
|
2000-03-14 01:46:54 +00:00
|
|
|
return NumStr(idx, NULL, 0);
|
1997-11-15 02:15:56 +00:00
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
|
|
|
free(buf);
|
2000-03-14 01:46:54 +00:00
|
|
|
return NumStr(idx, NULL, 0);
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
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;
|
2000-03-14 01:46:44 +00:00
|
|
|
if (ifm->ifm_type != RTM_IFINFO)
|
2000-03-14 01:46:56 +00:00
|
|
|
continue;
|
1997-11-15 02:15:56 +00:00
|
|
|
dl = (struct sockaddr_dl *)(ifm + 1);
|
1998-01-11 04:45:36 +00:00
|
|
|
if (ifm->ifm_index > 0) {
|
|
|
|
if (ifm->ifm_index > have) {
|
1998-08-17 06:42:40 +00:00
|
|
|
char **newifs;
|
|
|
|
|
1998-01-11 04:45:36 +00:00
|
|
|
had = have;
|
|
|
|
have = ifm->ifm_index + 5;
|
|
|
|
if (had)
|
1998-08-17 06:42:40 +00:00
|
|
|
newifs = (char **)realloc(ifs, sizeof(char *) * have);
|
1998-01-11 04:45:36 +00:00
|
|
|
else
|
1998-08-17 06:42:40 +00:00
|
|
|
newifs = (char **)malloc(sizeof(char *) * have);
|
|
|
|
if (!newifs) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno));
|
2002-05-14 12:55:39 +00:00
|
|
|
route_nifs = 0;
|
1999-08-26 20:01:20 +00:00
|
|
|
if (ifs) {
|
1998-08-17 06:42:40 +00:00
|
|
|
free(ifs);
|
1999-08-26 20:01:20 +00:00
|
|
|
ifs = NULL;
|
|
|
|
}
|
|
|
|
free(buf);
|
2000-03-14 01:46:54 +00:00
|
|
|
return NumStr(idx, NULL, 0);
|
1998-01-11 04:45:36 +00:00
|
|
|
}
|
1998-08-17 06:42:40 +00:00
|
|
|
ifs = newifs;
|
1998-01-11 04:45:36 +00:00
|
|
|
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);
|
2004-10-11 09:45:58 +00:00
|
|
|
if (ifs[ifm->ifm_index-1] == NULL)
|
|
|
|
log_Printf(LogDEBUG, "Skipping interface %d: Out of memory\n",
|
|
|
|
ifm->ifm_index);
|
|
|
|
else {
|
|
|
|
memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen);
|
|
|
|
ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0';
|
|
|
|
if (route_nifs < ifm->ifm_index)
|
|
|
|
route_nifs = ifm->ifm_index;
|
|
|
|
}
|
1998-01-11 04:45:36 +00:00
|
|
|
}
|
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");
|
2002-05-14 12:55:39 +00:00
|
|
|
for (f = 0; f < route_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;
|
|
|
|
}
|
|
|
|
|
2002-05-14 12:55:39 +00:00
|
|
|
if (idx < 1 || idx > route_nifs || ifs[idx-1] == NULL)
|
2000-03-14 01:46:54 +00:00
|
|
|
return NumStr(idx, NULL, 0);
|
1997-11-22 13:46:02 +00:00
|
|
|
|
|
|
|
return ifs[idx-1];
|
1997-11-15 02:15:56 +00:00
|
|
|
}
|
|
|
|
|
2000-03-14 01:46:44 +00:00
|
|
|
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);
|
2002-03-31 01:51:15 +00:00
|
|
|
if (sa[rtax]->sa_family == 0)
|
2001-11-23 17:19:27 +00:00
|
|
|
sa[rtax] = NULL; /* ??? */
|
2000-03-14 01:46:44 +00:00
|
|
|
} else
|
|
|
|
sa[rtax] = NULL;
|
|
|
|
}
|
|
|
|
|
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;
|
2000-03-14 01:46:44 +00:00
|
|
|
struct sockaddr *sa[RTAX_MAX];
|
|
|
|
char *sp, *ep, *cp;
|
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) {
|
2000-03-14 01:46:44 +00:00
|
|
|
rtm = (struct rt_msghdr *)cp;
|
2000-03-14 01:46:38 +00:00
|
|
|
|
2000-03-14 01:46:44 +00:00
|
|
|
route_ParseHdr(rtm, sa);
|
1997-11-15 02:15:56 +00:00
|
|
|
|
2000-03-14 01:46:44 +00:00
|
|
|
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);
|
1997-11-15 02:15:56 +00:00
|
|
|
|
2000-03-14 01:46:38 +00:00
|
|
|
p_flags(arg->prompt, rtm->rtm_flags, 6);
|
|
|
|
prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index));
|
|
|
|
} else
|
|
|
|
prompt_Printf(arg->prompt, "<can't parse routing entry>\n");
|
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;
|
2000-03-14 01:46:44 +00:00
|
|
|
struct sockaddr *sa[RTAX_MAX];
|
2001-08-14 16:05:52 +00:00
|
|
|
struct ncprange range;
|
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-10-22 02:32:50 +00:00
|
|
|
log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index);
|
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",
|
2001-04-05 02:23:48 +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",
|
2001-04-05 02:23:48 +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
|
2000-03-14 01:46:44 +00:00
|
|
|
* deletes all non-cloned routes. This is done to avoid
|
1997-12-15 20:21:15 +00:00
|
|
|
* 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
|
|
|
*/
|
2014-03-15 06:49:32 +00:00
|
|
|
if (pass == 0)
|
1997-12-15 20:21:15 +00:00
|
|
|
/* So we can't tell ! */
|
|
|
|
continue;
|
|
|
|
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
|
2000-03-14 01:46:44 +00:00
|
|
|
rtm = (struct rt_msghdr *)cp;
|
|
|
|
route_ParseHdr(rtm, sa);
|
2001-08-14 16:05:52 +00:00
|
|
|
if (rtm->rtm_index == bundle->iface->index &&
|
|
|
|
sa[RTAX_DST] && sa[RTAX_GATEWAY] &&
|
|
|
|
(sa[RTAX_DST]->sa_family == AF_INET
|
|
|
|
#ifndef NOINET6
|
|
|
|
|| sa[RTAX_DST]->sa_family == AF_INET6
|
|
|
|
#endif
|
|
|
|
) &&
|
|
|
|
(all || (rtm->rtm_flags & RTF_GATEWAY))) {
|
|
|
|
if (log_IsKept(LogDEBUG)) {
|
2018-02-10 17:09:51 +00:00
|
|
|
char gwstr[NCP_ASCIIBUFFERSIZE];
|
2001-08-14 16:05:52 +00:00
|
|
|
struct ncpaddr gw;
|
|
|
|
ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]);
|
|
|
|
ncpaddr_setsa(&gw, sa[RTAX_GATEWAY]);
|
|
|
|
snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(&gw));
|
|
|
|
log_Printf(LogDEBUG, "Found %s %s\n", ncprange_ntoa(&range), gwstr);
|
2000-03-14 01:46:44 +00:00
|
|
|
}
|
2001-08-14 16:05:52 +00:00
|
|
|
if (sa[RTAX_GATEWAY]->sa_family == AF_INET ||
|
|
|
|
#ifndef NOINET6
|
|
|
|
sa[RTAX_GATEWAY]->sa_family == AF_INET6 ||
|
|
|
|
#endif
|
|
|
|
sa[RTAX_GATEWAY]->sa_family == AF_LINK) {
|
2014-03-15 06:49:32 +00:00
|
|
|
if (pass == 1) {
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]);
|
|
|
|
rt_Set(bundle, RTM_DELETE, &range, NULL, 0, 0);
|
|
|
|
} else
|
|
|
|
log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass);
|
|
|
|
} else
|
|
|
|
log_Printf(LogDEBUG,
|
|
|
|
"route_IfDelete: Can't remove routes for family %d\n",
|
|
|
|
sa[RTAX_GATEWAY]->sa_family);
|
1997-12-15 20:21:15 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(sp);
|
|
|
|
}
|
|
|
|
|
2001-04-05 02:23:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the MTU on all routes for the given interface
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
route_UpdateMTU(struct bundle *bundle)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct sockaddr *sa[RTAX_MAX];
|
2001-08-14 16:05:52 +00:00
|
|
|
struct ncprange dst;
|
2001-04-05 02:23:48 +00:00
|
|
|
size_t needed;
|
|
|
|
char *sp, *cp, *ep;
|
|
|
|
int mib[6];
|
|
|
|
|
|
|
|
log_Printf(LogDEBUG, "route_UpdateMTU (%d)\n", bundle->iface->index);
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
|
|
|
mib[4] = NET_RT_DUMP;
|
|
|
|
mib[5] = 0;
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
|
|
|
log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp = malloc(needed);
|
|
|
|
if (sp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
|
|
|
|
log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
free(sp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ep = sp + needed;
|
|
|
|
|
|
|
|
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
|
|
|
|
rtm = (struct rt_msghdr *)cp;
|
|
|
|
route_ParseHdr(rtm, sa);
|
2001-08-18 12:31:54 +00:00
|
|
|
if (sa[RTAX_DST] && (sa[RTAX_DST]->sa_family == AF_INET
|
2001-08-16 02:01:05 +00:00
|
|
|
#ifndef NOINET6
|
2001-08-18 12:31:54 +00:00
|
|
|
|| sa[RTAX_DST]->sa_family == AF_INET6
|
2001-08-16 02:01:05 +00:00
|
|
|
#endif
|
|
|
|
) &&
|
|
|
|
sa[RTAX_GATEWAY] && rtm->rtm_index == bundle->iface->index) {
|
|
|
|
if (log_IsKept(LogTCPIP)) {
|
|
|
|
ncprange_setsa(&dst, sa[RTAX_DST], sa[RTAX_NETMASK]);
|
|
|
|
log_Printf(LogTCPIP, "route_UpdateMTU: Netif: %d (%s), dst %s,"
|
2004-09-05 01:46:52 +00:00
|
|
|
" mtu %lu\n", rtm->rtm_index, Index2Nam(rtm->rtm_index),
|
2001-08-16 02:01:05 +00:00
|
|
|
ncprange_ntoa(&dst), bundle->iface->mtu);
|
|
|
|
}
|
2009-04-20 14:38:48 +00:00
|
|
|
rt_Update(bundle, sa[RTAX_DST], sa[RTAX_GATEWAY], sa[RTAX_NETMASK],
|
|
|
|
sa[RTAX_IFP], sa[RTAX_IFA]);
|
2001-04-05 02:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sp);
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
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 13:46:02 +00:00
|
|
|
idx = 1;
|
2002-05-14 12:55:39 +00:00
|
|
|
while (route_nifs == -1 || idx < route_nifs)
|
|
|
|
if (strcmp(Index2Nam(idx), name) == 0)
|
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,
|
2001-08-14 16:05:52 +00:00
|
|
|
const struct ncpaddr *me, const struct ncpaddr *peer)
|
1998-05-05 23:30:13 +00:00
|
|
|
{
|
2001-08-14 16:05:52 +00:00
|
|
|
struct ncpaddr dst;
|
1998-05-05 23:30:13 +00:00
|
|
|
|
|
|
|
for (; r; r = r->next) {
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_getaddr(&r->dst, &dst);
|
|
|
|
if (ncpaddr_family(me) == AF_INET) {
|
|
|
|
if ((r->type & ROUTE_DSTMYADDR) && !ncpaddr_equal(&dst, me)) {
|
|
|
|
rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
|
|
|
|
ncprange_sethost(&r->dst, me);
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
} else if ((r->type & ROUTE_DSTHISADDR) && !ncpaddr_equal(&dst, peer)) {
|
|
|
|
rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
|
|
|
|
ncprange_sethost(&r->dst, peer);
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
} else if ((r->type & ROUTE_DSTDNS0) && !ncpaddr_equal(&dst, peer)) {
|
|
|
|
if (bundle->ncp.ipcp.ns.dns[0].s_addr == INADDR_NONE)
|
|
|
|
continue;
|
|
|
|
rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
} else if ((r->type & ROUTE_DSTDNS1) && !ncpaddr_equal(&dst, peer)) {
|
|
|
|
if (bundle->ncp.ipcp.ns.dns[1].s_addr == INADDR_NONE)
|
|
|
|
continue;
|
|
|
|
rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
} else if ((r->type & ROUTE_GWHISADDR) && !ncpaddr_equal(&r->gw, peer))
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
#ifndef NOINET6
|
|
|
|
} else if (ncpaddr_family(me) == AF_INET6) {
|
|
|
|
if ((r->type & ROUTE_DSTMYADDR6) && !ncpaddr_equal(&dst, me)) {
|
|
|
|
rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
|
|
|
|
ncprange_sethost(&r->dst, me);
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
} else if ((r->type & ROUTE_DSTHISADDR6) && !ncpaddr_equal(&dst, peer)) {
|
|
|
|
rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
|
|
|
|
ncprange_sethost(&r->dst, peer);
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
} else if ((r->type & ROUTE_GWHISADDR6) && !ncpaddr_equal(&r->gw, peer))
|
|
|
|
ncpaddr_copy(&r->gw, peer);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
rt_Set(bundle, RTM_ADD, &r->dst, &r->gw, 1, 0);
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-08-14 16:05:52 +00:00
|
|
|
route_Add(struct sticky_route **rp, int type, const struct ncprange *dst,
|
|
|
|
const struct ncpaddr *gw)
|
1998-05-05 23:30:13 +00:00
|
|
|
{
|
1999-01-28 01:56:34 +00:00
|
|
|
struct sticky_route *r;
|
|
|
|
int dsttype = type & ROUTE_DSTANY;
|
1998-05-05 23:30:13 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
r = NULL;
|
|
|
|
while (*rp) {
|
|
|
|
if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
|
2001-08-14 16:05:52 +00:00
|
|
|
(!dsttype && ncprange_equal(&(*rp)->dst, dst))) {
|
1999-01-28 01:56:34 +00:00
|
|
|
/* Oops, we already have this route - unlink it */
|
|
|
|
free(r); /* impossible really */
|
|
|
|
r = *rp;
|
|
|
|
*rp = r->next;
|
|
|
|
} else
|
|
|
|
rp = &(*rp)->next;
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
1999-01-28 01:56:34 +00:00
|
|
|
|
2004-10-11 09:45:58 +00:00
|
|
|
if (r == NULL) {
|
1999-01-28 01:56:34 +00:00
|
|
|
r = (struct sticky_route *)malloc(sizeof(struct sticky_route));
|
2004-10-11 09:45:58 +00:00
|
|
|
if (r == NULL) {
|
|
|
|
log_Printf(LogERROR, "route_Add: Out of memory!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1999-01-28 01:56:34 +00:00
|
|
|
r->type = type;
|
|
|
|
r->next = NULL;
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_copy(&r->dst, dst);
|
|
|
|
ncpaddr_copy(&r->gw, gw);
|
2001-08-15 12:47:51 +00:00
|
|
|
*rp = r;
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-08-14 16:05:52 +00:00
|
|
|
route_Delete(struct sticky_route **rp, int type, const struct ncprange *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)) ||
|
2001-08-14 16:05:52 +00:00
|
|
|
(!dsttype && ncprange_equal(dst, &(*rp)->dst))) {
|
1998-05-05 23:30:13 +00:00
|
|
|
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
|
1999-01-28 01:56:34 +00:00
|
|
|
route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag,
|
|
|
|
int indent)
|
1998-05-05 23:30:13 +00:00
|
|
|
{
|
1999-01-28 01:56:34 +00:00
|
|
|
int tlen = strlen(tag);
|
|
|
|
|
|
|
|
if (tlen + 2 > indent)
|
|
|
|
prompt_Printf(p, "%s:\n%*s", tag, indent, "");
|
|
|
|
else
|
|
|
|
prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, "");
|
1998-05-05 23:30:13 +00:00
|
|
|
|
|
|
|
for (; r; r = r->next) {
|
1999-01-28 01:56:34 +00:00
|
|
|
prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, "");
|
|
|
|
tlen = 0;
|
1998-05-05 23:30:13 +00:00
|
|
|
if (r->type & ROUTE_DSTMYADDR)
|
|
|
|
prompt_Printf(p, "MYADDR");
|
2001-08-14 16:05:52 +00:00
|
|
|
else if (r->type & ROUTE_DSTMYADDR6)
|
|
|
|
prompt_Printf(p, "MYADDR6");
|
1998-05-05 23:30:13 +00:00
|
|
|
else if (r->type & ROUTE_DSTHISADDR)
|
|
|
|
prompt_Printf(p, "HISADDR");
|
2001-08-14 16:05:52 +00:00
|
|
|
else if (r->type & ROUTE_DSTHISADDR6)
|
|
|
|
prompt_Printf(p, "HISADDR6");
|
2000-03-14 01:47:27 +00:00
|
|
|
else if (r->type & ROUTE_DSTDNS0)
|
|
|
|
prompt_Printf(p, "DNS0");
|
|
|
|
else if (r->type & ROUTE_DSTDNS1)
|
|
|
|
prompt_Printf(p, "DNS1");
|
2001-08-14 16:05:52 +00:00
|
|
|
else if (ncprange_isdefault(&r->dst))
|
|
|
|
prompt_Printf(p, "default");
|
1998-05-05 23:30:13 +00:00
|
|
|
else
|
2001-08-14 16:05:52 +00:00
|
|
|
prompt_Printf(p, "%s", ncprange_ntoa(&r->dst));
|
1998-05-05 23:30:13 +00:00
|
|
|
|
|
|
|
if (r->type & ROUTE_GWHISADDR)
|
2001-08-14 16:05:52 +00:00
|
|
|
prompt_Printf(p, " HISADDR\n");
|
|
|
|
else if (r->type & ROUTE_GWHISADDR6)
|
|
|
|
prompt_Printf(p, " HISADDR6\n");
|
1998-05-05 23:30:13 +00:00
|
|
|
else
|
2001-08-14 16:05:52 +00:00
|
|
|
prompt_Printf(p, " %s\n", ncpaddr_ntoa(&r->gw));
|
1998-05-05 23:30:13 +00:00
|
|
|
}
|
1997-12-13 02:37:33 +00:00
|
|
|
}
|
2001-04-05 02:23:48 +00:00
|
|
|
|
|
|
|
struct rtmsg {
|
|
|
|
struct rt_msghdr m_rtm;
|
2001-08-14 16:05:52 +00:00
|
|
|
char m_space[256];
|
2001-04-05 02:23:48 +00:00
|
|
|
};
|
|
|
|
|
2001-11-23 17:19:36 +00:00
|
|
|
static size_t
|
|
|
|
memcpy_roundup(char *cp, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
size_t padlen;
|
|
|
|
|
|
|
|
padlen = ROUNDUP(len);
|
|
|
|
memcpy(cp, data, len);
|
|
|
|
if (padlen > len)
|
|
|
|
memset(cp + len, '\0', padlen - len);
|
|
|
|
|
|
|
|
return padlen;
|
|
|
|
}
|
|
|
|
|
2003-03-25 16:49:08 +00:00
|
|
|
#if defined(__KAME__) && !defined(NOINET6)
|
|
|
|
static void
|
|
|
|
add_scope(struct sockaddr *sa, int ifindex)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sa6;
|
|
|
|
|
|
|
|
if (sa->sa_family != AF_INET6)
|
|
|
|
return;
|
|
|
|
sa6 = (struct sockaddr_in6 *)sa;
|
|
|
|
if (!IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) &&
|
|
|
|
!IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr))
|
|
|
|
return;
|
|
|
|
if (*(u_int16_t *)&sa6->sin6_addr.s6_addr[2] != 0)
|
|
|
|
return;
|
|
|
|
*(u_int16_t *)&sa6->sin6_addr.s6_addr[2] = htons(ifindex);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-04-05 02:23:48 +00:00
|
|
|
int
|
2001-08-14 16:05:52 +00:00
|
|
|
rt_Set(struct bundle *bundle, int cmd, const struct ncprange *dst,
|
|
|
|
const struct ncpaddr *gw, int bang, int quiet)
|
2001-04-05 02:23:48 +00:00
|
|
|
{
|
|
|
|
struct rtmsg rtmes;
|
2001-11-23 15:47:04 +00:00
|
|
|
int s, nb, wb;
|
2001-04-05 02:23:48 +00:00
|
|
|
char *cp;
|
|
|
|
const char *cmdstr;
|
2001-08-14 16:05:52 +00:00
|
|
|
struct sockaddr_storage sadst, samask, sagw;
|
2001-04-05 02:23:48 +00:00
|
|
|
int result = 1;
|
|
|
|
|
|
|
|
if (bang)
|
|
|
|
cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
|
|
|
|
else
|
|
|
|
cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
|
2002-01-16 14:03:52 +00:00
|
|
|
s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
|
2001-04-05 02:23:48 +00:00
|
|
|
if (s < 0) {
|
|
|
|
log_Printf(LogERROR, "rt_Set: socket(): %s\n", strerror(errno));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
memset(&rtmes, '\0', sizeof rtmes);
|
|
|
|
rtmes.m_rtm.rtm_version = RTM_VERSION;
|
|
|
|
rtmes.m_rtm.rtm_type = cmd;
|
|
|
|
rtmes.m_rtm.rtm_addrs = RTA_DST;
|
|
|
|
rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
|
|
|
|
rtmes.m_rtm.rtm_pid = getpid();
|
|
|
|
rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
|
|
|
|
|
|
|
|
if (cmd == RTM_ADD) {
|
2001-08-14 16:05:52 +00:00
|
|
|
if (bundle->ncp.cfg.sendpipe > 0) {
|
|
|
|
rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe;
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
|
|
|
|
}
|
2001-08-14 16:05:52 +00:00
|
|
|
if (bundle->ncp.cfg.recvpipe > 0) {
|
|
|
|
rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe;
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_getsa(dst, &sadst, &samask);
|
2003-03-25 16:49:08 +00:00
|
|
|
#if defined(__KAME__) && !defined(NOINET6)
|
|
|
|
add_scope((struct sockaddr *)&sadst, bundle->iface->index);
|
|
|
|
#endif
|
2001-04-05 02:23:48 +00:00
|
|
|
|
|
|
|
cp = rtmes.m_space;
|
2001-11-23 17:19:36 +00:00
|
|
|
cp += memcpy_roundup(cp, &sadst, sadst.ss_len);
|
2001-04-05 02:23:48 +00:00
|
|
|
if (cmd == RTM_ADD) {
|
2001-08-14 16:05:52 +00:00
|
|
|
if (gw == NULL) {
|
|
|
|
log_Printf(LogERROR, "rt_Set: Program error\n");
|
|
|
|
close(s);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
ncpaddr_getsa(gw, &sagw);
|
2003-03-25 16:49:08 +00:00
|
|
|
#if defined(__KAME__) && !defined(NOINET6)
|
|
|
|
add_scope((struct sockaddr *)&sagw, bundle->iface->index);
|
|
|
|
#endif
|
2001-08-14 16:05:52 +00:00
|
|
|
if (ncpaddr_isdefault(gw)) {
|
|
|
|
if (!quiet)
|
2001-04-05 02:23:48 +00:00
|
|
|
log_Printf(LogERROR, "rt_Set: Cannot add a route with"
|
2001-09-04 22:07:56 +00:00
|
|
|
" gateway 0.0.0.0\n");
|
2001-04-05 02:23:48 +00:00
|
|
|
close(s);
|
|
|
|
return result;
|
|
|
|
} else {
|
2001-11-23 17:19:36 +00:00
|
|
|
cp += memcpy_roundup(cp, &sagw, sagw.ss_len);
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-23 12:39:20 +00:00
|
|
|
if (!ncprange_ishost(dst)) {
|
2001-11-23 17:19:36 +00:00
|
|
|
cp += memcpy_roundup(cp, &samask, samask.ss_len);
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
|
2017-10-30 15:19:28 +00:00
|
|
|
} else
|
2017-10-30 15:02:42 +00:00
|
|
|
rtmes.m_rtm.rtm_flags |= RTF_HOST;
|
2001-04-05 02:23:48 +00:00
|
|
|
|
|
|
|
nb = cp - (char *)&rtmes;
|
|
|
|
rtmes.m_rtm.rtm_msglen = nb;
|
|
|
|
wb = ID0write(s, &rtmes, nb);
|
|
|
|
if (wb < 0) {
|
|
|
|
log_Printf(LogTCPIP, "rt_Set failure:\n");
|
|
|
|
log_Printf(LogTCPIP, "rt_Set: Cmd = %s\n", cmdstr);
|
2001-08-14 16:05:52 +00:00
|
|
|
log_Printf(LogTCPIP, "rt_Set: Dst = %s\n", ncprange_ntoa(dst));
|
|
|
|
if (gw != NULL)
|
|
|
|
log_Printf(LogTCPIP, "rt_Set: Gateway = %s\n", ncpaddr_ntoa(gw));
|
2001-04-05 02:23:48 +00:00
|
|
|
failed:
|
|
|
|
if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
|
|
|
|
(rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
|
|
|
|
if (!bang) {
|
|
|
|
log_Printf(LogWARN, "Add route failed: %s already exists\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_ntoa(dst));
|
2001-04-05 02:23:48 +00:00
|
|
|
result = 0; /* Don't add to our dynamic list */
|
|
|
|
} else {
|
|
|
|
rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
|
|
|
|
if ((wb = ID0write(s, &rtmes, nb)) < 0)
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
} else if (cmd == RTM_DELETE &&
|
|
|
|
(rtmes.m_rtm.rtm_errno == ESRCH ||
|
|
|
|
(rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
|
|
|
|
if (!bang)
|
|
|
|
log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_ntoa(dst));
|
2001-04-05 02:23:48 +00:00
|
|
|
} else if (rtmes.m_rtm.rtm_errno == 0) {
|
2001-08-14 16:05:52 +00:00
|
|
|
if (!quiet || errno != ENETUNREACH)
|
2001-04-05 02:23:48 +00:00
|
|
|
log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
|
2001-08-14 16:05:52 +00:00
|
|
|
ncprange_ntoa(dst), strerror(errno));
|
2001-04-05 02:23:48 +00:00
|
|
|
} else
|
|
|
|
log_Printf(LogWARN, "%s route failed: %s: %s\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
cmdstr, ncprange_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
|
2001-04-05 02:23:48 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (log_IsKept(LogDEBUG)) {
|
2018-02-10 17:09:51 +00:00
|
|
|
char gwstr[NCP_ASCIIBUFFERSIZE];
|
2001-08-14 16:05:52 +00:00
|
|
|
|
|
|
|
if (gw)
|
|
|
|
snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(gw));
|
|
|
|
else
|
|
|
|
snprintf(gwstr, sizeof gwstr, "<none>");
|
|
|
|
log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %s, gateway = %s\n",
|
|
|
|
wb, cmdstr, ncprange_ntoa(dst), gwstr);
|
|
|
|
}
|
2001-04-05 02:23:48 +00:00
|
|
|
close(s);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-08-16 02:01:05 +00:00
|
|
|
rt_Update(struct bundle *bundle, const struct sockaddr *dst,
|
2009-04-20 14:38:48 +00:00
|
|
|
const struct sockaddr *gw, const struct sockaddr *mask,
|
|
|
|
const struct sockaddr *ifp, const struct sockaddr *ifa)
|
2001-04-05 02:23:48 +00:00
|
|
|
{
|
2001-08-16 02:01:05 +00:00
|
|
|
struct ncprange ncpdst;
|
2001-04-05 02:23:48 +00:00
|
|
|
struct rtmsg rtmes;
|
2001-08-16 02:01:05 +00:00
|
|
|
char *p;
|
2001-04-05 02:23:48 +00:00
|
|
|
int s, wb;
|
|
|
|
|
2002-01-16 14:03:52 +00:00
|
|
|
s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
|
2001-04-05 02:23:48 +00:00
|
|
|
if (s < 0) {
|
|
|
|
log_Printf(LogERROR, "rt_Update: socket(): %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&rtmes, '\0', sizeof rtmes);
|
|
|
|
rtmes.m_rtm.rtm_version = RTM_VERSION;
|
|
|
|
rtmes.m_rtm.rtm_type = RTM_CHANGE;
|
2001-08-20 00:46:33 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs = 0;
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
|
|
|
|
rtmes.m_rtm.rtm_pid = getpid();
|
2002-03-30 12:50:49 +00:00
|
|
|
rtmes.m_rtm.rtm_flags = RTF_UP | RTF_STATIC;
|
2001-04-05 02:23:48 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (bundle->ncp.cfg.sendpipe > 0) {
|
|
|
|
rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe;
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
|
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (bundle->ncp.cfg.recvpipe > 0) {
|
|
|
|
rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe;
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
|
|
|
|
}
|
|
|
|
|
2001-06-18 14:59:36 +00:00
|
|
|
rtmes.m_rtm.rtm_rmx.rmx_mtu = bundle->iface->mtu;
|
2001-04-05 02:23:48 +00:00
|
|
|
rtmes.m_rtm.rtm_inits |= RTV_MTU;
|
2001-08-16 02:01:05 +00:00
|
|
|
p = rtmes.m_space;
|
2001-04-05 02:23:48 +00:00
|
|
|
|
2001-08-16 02:01:05 +00:00
|
|
|
if (dst) {
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_DST;
|
2001-11-23 17:19:36 +00:00
|
|
|
p += memcpy_roundup(p, dst, dst->sa_len);
|
2001-08-16 02:01:05 +00:00
|
|
|
}
|
2001-04-05 02:23:48 +00:00
|
|
|
|
2009-04-20 11:51:35 +00:00
|
|
|
if (gw) {
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
|
|
|
|
p += memcpy_roundup(p, gw, gw->sa_len);
|
|
|
|
}
|
|
|
|
|
2002-03-30 12:50:49 +00:00
|
|
|
if (mask) {
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
|
|
|
|
p += memcpy_roundup(p, mask, mask->sa_len);
|
2017-10-30 15:19:28 +00:00
|
|
|
} else
|
2017-10-30 15:02:42 +00:00
|
|
|
rtmes.m_rtm.rtm_flags |= RTF_HOST;
|
2002-03-30 12:50:49 +00:00
|
|
|
|
2009-04-20 14:38:48 +00:00
|
|
|
if (ifa && ifp && ifp->sa_family == AF_LINK) {
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_IFP;
|
|
|
|
p += memcpy_roundup(p, ifp, ifp->sa_len);
|
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_IFA;
|
|
|
|
p += memcpy_roundup(p, ifa, ifa->sa_len);
|
|
|
|
}
|
|
|
|
|
2001-08-16 02:01:05 +00:00
|
|
|
rtmes.m_rtm.rtm_msglen = p - (char *)&rtmes;
|
2001-04-05 02:23:48 +00:00
|
|
|
|
|
|
|
wb = ID0write(s, &rtmes, rtmes.m_rtm.rtm_msglen);
|
|
|
|
if (wb < 0) {
|
2001-08-16 02:01:05 +00:00
|
|
|
ncprange_setsa(&ncpdst, dst, mask);
|
|
|
|
|
2001-04-05 02:23:48 +00:00
|
|
|
log_Printf(LogTCPIP, "rt_Update failure:\n");
|
2001-08-16 02:01:05 +00:00
|
|
|
log_Printf(LogTCPIP, "rt_Update: Dst = %s\n", ncprange_ntoa(&ncpdst));
|
2001-04-05 02:23:48 +00:00
|
|
|
|
|
|
|
if (rtmes.m_rtm.rtm_errno == 0)
|
|
|
|
log_Printf(LogWARN, "%s: Change route failed: errno: %s\n",
|
2001-08-16 02:01:05 +00:00
|
|
|
ncprange_ntoa(&ncpdst), strerror(errno));
|
2001-04-05 02:23:48 +00:00
|
|
|
else
|
|
|
|
log_Printf(LogWARN, "%s: Change route failed: %s\n",
|
2001-08-16 02:01:05 +00:00
|
|
|
ncprange_ntoa(&ncpdst), strerror(rtmes.m_rtm.rtm_errno));
|
2001-04-05 02:23:48 +00:00
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|