route.c revision 93463
159769Sgrog/*- 259769Sgrog * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 324424Swosch * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 424424Swosch * Internet Initiative Japan, Inc (IIJ) 524424Swosch * All rights reserved. 624424Swosch * 724424Swosch * Redistribution and use in source and binary forms, with or without 824424Swosch * modification, are permitted provided that the following conditions 924424Swosch * are met: 1024424Swosch * 1. Redistributions of source code must retain the above copyright 1124424Swosch * notice, this list of conditions and the following disclaimer. 1224424Swosch * 2. Redistributions in binary form must reproduce the above copyright 1324424Swosch * notice, this list of conditions and the following disclaimer in the 1424424Swosch * documentation and/or other materials provided with the distribution. 1542704Swosch * 1642704Swosch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1742704Swosch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1824424Swosch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1942704Swosch * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2042704Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2142704Swosch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2242704Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2342704Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2442704Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2542704Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2642704Swosch * SUCH DAMAGE. 2742704Swosch * 2842704Swosch * $FreeBSD: head/usr.sbin/ppp/route.c 93463 2002-03-31 01:51:15Z brian $ 2942704Swosch */ 3059769Sgrog 3159769Sgrog#include <sys/param.h> 3259769Sgrog#include <sys/socket.h> 3359769Sgrog#include <net/if_types.h> 3459769Sgrog#include <net/route.h> 3559769Sgrog#include <net/if.h> 3659769Sgrog#include <netinet/in.h> 3759769Sgrog#include <arpa/inet.h> 3859769Sgrog#include <net/if_dl.h> 3924424Swosch#include <netinet/in_systm.h> 4042704Swosch#include <netinet/ip.h> 4124424Swosch#include <sys/un.h> 4242704Swosch#include <netdb.h> 4324424Swosch 4442704Swosch#include <errno.h> 4524424Swosch#include <stdio.h> 4624424Swosch#include <stdlib.h> 4724424Swosch#include <string.h> 4842704Swosch#include <sys/sysctl.h> 4925031Swosch#include <termios.h> 5059156Swosch#include <unistd.h> 5125031Swosch 5225031Swosch#include "layer.h" 5324424Swosch#include "defs.h" 5424424Swosch#include "command.h" 5524424Swosch#include "mbuf.h" 5624424Swosch#include "log.h" 5771231Sitojun#include "iplist.h" 5824424Swosch#include "timer.h" 5971231Sitojun#include "throughput.h" 6025031Swosch#include "lqr.h" 6171231Sitojun#include "hdlc.h" 6224424Swosch#include "fsm.h" 6325031Swosch#include "lcp.h" 6425031Swosch#include "ccp.h" 6571231Sitojun#include "link.h" 6625031Swosch#include "slcompress.h" 6771231Sitojun#include "ncpaddr.h" 6870110Swosch#include "ip.h" 6970110Swosch#include "ipcp.h" 7070110Swosch#include "filter.h" 7170110Swosch#include "descriptor.h" 7270110Swosch#include "mp.h" 7370110Swosch#ifndef NORADIUS 7470110Swosch#include "radius.h" 7570110Swosch#endif 7670110Swosch#include "ipv6cp.h" 7770110Swosch#include "ncp.h" 7870110Swosch#include "bundle.h" 7980675Sasmodai#include "route.h" 8080675Sasmodai#include "prompt.h" 8180675Sasmodai#include "iface.h" 8280675Sasmodai#include "id.h" 8380675Sasmodai 8480675Sasmodai 8580675Sasmodaistatic void 8680675Sasmodaip_sockaddr(struct prompt *prompt, struct sockaddr *phost, 8780675Sasmodai struct sockaddr *pmask, int width) 8880675Sasmodai{ 8980675Sasmodai struct ncprange range; 9080675Sasmodai char buf[29]; 9180675Sasmodai struct sockaddr_dl *dl = (struct sockaddr_dl *)phost; 9280675Sasmodai 9380675Sasmodai if (log_IsKept(LogDEBUG)) { 9480675Sasmodai char tmp[50]; 9580675Sasmodai 9680675Sasmodai log_Printf(LogDEBUG, "Found the following sockaddr:\n"); 9780675Sasmodai log_Printf(LogDEBUG, " Family %d, len %d\n", 9880675Sasmodai (int)phost->sa_family, (int)phost->sa_len); 9980675Sasmodai inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp); 10080675Sasmodai log_Printf(LogDEBUG, " Addr %s\n", tmp); 10180675Sasmodai if (pmask) { 10280675Sasmodai inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp); 10380675Sasmodai log_Printf(LogDEBUG, " Mask %s\n", tmp); 10480675Sasmodai } 10580675Sasmodai } 10680675Sasmodai 10780675Sasmodai switch (phost->sa_family) { 10880675Sasmodai case AF_INET: 10980675Sasmodai#ifndef NOINET6 11080675Sasmodai case AF_INET6: 11180675Sasmodai#endif 11280675Sasmodai ncprange_setsa(&range, phost, pmask); 11380675Sasmodai if (ncprange_isdefault(&range)) 11480675Sasmodai prompt_Printf(prompt, "%-*s ", width - 1, "default"); 11580675Sasmodai else 11680675Sasmodai prompt_Printf(prompt, "%-*s ", width - 1, ncprange_ntoa(&range)); 11780675Sasmodai return; 11880675Sasmodai 11980675Sasmodai case AF_LINK: 12080675Sasmodai if (dl->sdl_nlen) 12180675Sasmodai snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data); 12280675Sasmodai else if (dl->sdl_alen) { 12380675Sasmodai if (dl->sdl_type == IFT_ETHER) { 12480675Sasmodai if (dl->sdl_alen < sizeof buf / 3) { 12580675Sasmodai int f; 12680675Sasmodai u_char *MAC; 12780675Sasmodai 12880675Sasmodai MAC = (u_char *)dl->sdl_data + dl->sdl_nlen; 12980675Sasmodai for (f = 0; f < dl->sdl_alen; f++) 13080675Sasmodai sprintf(buf+f*3, "%02x:", MAC[f]); 13180675Sasmodai buf[f*3-1] = '\0'; 13280675Sasmodai } else 13380675Sasmodai strcpy(buf, "??:??:??:??:??:??"); 13480675Sasmodai } else 13580675Sasmodai sprintf(buf, "<IFT type %d>", dl->sdl_type); 13680675Sasmodai } else if (dl->sdl_slen) 13780675Sasmodai sprintf(buf, "<slen %d?>", dl->sdl_slen); 138101401Swosch else 13980675Sasmodai sprintf(buf, "link#%d", dl->sdl_index); 14087200Swosch break; 14187200Swosch 14287200Swosch default: 14380675Sasmodai sprintf(buf, "<AF type %d>", phost->sa_family); 14480675Sasmodai break; 145101401Swosch } 14680675Sasmodai 14784087Swosch prompt_Printf(prompt, "%-*s ", width-1, buf); 14884087Swosch} 14987200Swosch 150101332Swoschstatic struct bits { 151101332Swosch u_int32_t b_mask; 152101332Swosch char b_val; 153101332Swosch} bits[] = { 154101332Swosch { RTF_UP, 'U' }, 155104659Smurray { RTF_GATEWAY, 'G' }, 156101332Swosch { RTF_HOST, 'H' }, 157101332Swosch { RTF_REJECT, 'R' }, 15880675Sasmodai { RTF_DYNAMIC, 'D' }, 15980675Sasmodai { RTF_MODIFIED, 'M' }, 16080675Sasmodai { RTF_DONE, 'd' }, 161104659Smurray { RTF_CLONING, 'C' }, 16280675Sasmodai { RTF_XRESOLVE, 'X' }, 16324424Swosch { RTF_LLINFO, 'L' }, 16424424Swosch { RTF_STATIC, 'S' }, 16524424Swosch { RTF_PROTO1, '1' }, 16624424Swosch { RTF_PROTO2, '2' }, 16769277Sasmodai { RTF_BLACKHOLE, 'B' }, 16869277Sasmodai#ifdef RTF_WASCLONED 16924424Swosch { RTF_WASCLONED, 'W' }, 17025031Swosch#endif 17125031Swosch#ifdef RTF_PRCLONING 17225031Swosch { RTF_PRCLONING, 'c' }, 17380675Sasmodai#endif 17425031Swosch#ifdef RTF_PROTO3 17525031Swosch { RTF_PROTO3, '3' }, 17625031Swosch#endif 17725031Swosch#ifdef RTF_BROADCAST 17825031Swosch { RTF_BROADCAST, 'b' }, 17925031Swosch#endif 18025031Swosch { 0, '\0' } 18125031Swosch}; 18225031Swosch 18325031Swosch#ifndef RTF_WASCLONED 18425031Swosch#define RTF_WASCLONED (0) 18525031Swosch#endif 18638440Sjkh 18745349Swoschstatic void 18845349Swoschp_flags(struct prompt *prompt, u_int32_t f, int max) 18942704Swosch{ 19070110Swosch char name[33], *flags; 19125031Swosch register struct bits *p = bits; 19224424Swosch 19359769Sgrog if (max > sizeof name - 1) 19425031Swosch max = sizeof name - 1; 19525031Swosch 19625031Swosch for (flags = name; p->b_mask && flags - name < max; p++) 19725031Swosch if (p->b_mask & f) 19859769Sgrog *flags++ = p->b_val; 19925031Swosch *flags = '\0'; 20025031Swosch prompt_Printf(prompt, "%-*.*s", max, max, name); 20125031Swosch} 20225031Swosch 20324424Swoschconst char * 20425031SwoschIndex2Nam(int idx) 20525031Swosch{ 20625031Swosch /* 20725031Swosch * XXX: Maybe we should select() on the routing socket so that we can 20825031Swosch * notice interfaces that come & go (PCCARD support). 20959769Sgrog * Or we could even support a signal that resets these so that 21059769Sgrog * the PCCARD insert/remove events can signal ppp. 21142704Swosch */ 21242704Swosch static char **ifs; /* Figure these out once */ 21342704Swosch static int nifs, debug_done; /* Figure out how many once, and debug once */ 21470110Swosch 21542704Swosch if (idx > nifs || (idx > 0 && ifs[idx-1] == NULL)) { 21642704Swosch int mib[6], have, had; 21725031Swosch size_t needed; 21825031Swosch char *buf, *ptr, *end; 21924424Swosch struct sockaddr_dl *dl; 22025031Swosch struct if_msghdr *ifm; 22125031Swosch 22225031Swosch if (ifs) { 22325031Swosch free(ifs); 22425031Swosch ifs = NULL; 22525031Swosch nifs = 0; 22625031Swosch } 22725031Swosch debug_done = 0; 22825031Swosch 22924424Swosch mib[0] = CTL_NET; 23025031Swosch mib[1] = PF_ROUTE; 23125031Swosch mib[2] = 0; 23225031Swosch mib[3] = 0; 23325031Swosch mib[4] = NET_RT_IFLIST; 23425031Swosch mib[5] = 0; 23525031Swosch 23625031Swosch if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 23725031Swosch log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n", 23859156Swosch strerror(errno)); 23925031Swosch return NumStr(idx, NULL, 0); 24025031Swosch } 24125031Swosch if ((buf = malloc(needed)) == NULL) 24225031Swosch return NumStr(idx, NULL, 0); 24325031Swosch if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 24425031Swosch free(buf); 24589981Sjoe return NumStr(idx, NULL, 0); 24625031Swosch } 24725031Swosch end = buf + needed; 24825031Swosch 24924424Swosch have = 0; 25025031Swosch for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) { 25125031Swosch ifm = (struct if_msghdr *)ptr; 25289981Sjoe if (ifm->ifm_type != RTM_IFINFO) 25325031Swosch continue; 25489981Sjoe dl = (struct sockaddr_dl *)(ifm + 1); 25589981Sjoe if (ifm->ifm_index > 0) { 25625031Swosch if (ifm->ifm_index > have) { 25789981Sjoe char **newifs; 25889981Sjoe 25989981Sjoe had = have; 26070110Swosch have = ifm->ifm_index + 5; 26171231Sitojun if (had) 26270110Swosch newifs = (char **)realloc(ifs, sizeof(char *) * have); 26325031Swosch else 26471231Sitojun newifs = (char **)malloc(sizeof(char *) * have); 26571231Sitojun if (!newifs) { 26669278Sasmodai log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno)); 26725031Swosch nifs = 0; 26871231Sitojun if (ifs) { 26970110Swosch free(ifs); 27071231Sitojun ifs = NULL; 27170110Swosch } 27270110Swosch free(buf); 27371231Sitojun return NumStr(idx, NULL, 0); 27470110Swosch } 27557000Swosch ifs = newifs; 27625031Swosch memset(ifs + had, '\0', sizeof(char *) * (have - had)); 27745349Swosch } 27878270Snik if (ifs[ifm->ifm_index-1] == NULL) { 27971231Sitojun ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1); 28025031Swosch memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen); 28138440Sjkh ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0'; 28238440Sjkh if (nifs < ifm->ifm_index) 28349392Swosch nifs = ifm->ifm_index; 28457000Swosch } 28538440Sjkh } else if (log_IsKept(LogDEBUG)) 28638440Sjkh log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n", 28770110Swosch ifm->ifm_index); 28869278Sasmodai } 28970110Swosch free(buf); 29025031Swosch } 29125031Swosch 29269278Sasmodai if (log_IsKept(LogDEBUG) && !debug_done) { 29345349Swosch int f; 29470110Swosch 29569278Sasmodai log_Printf(LogDEBUG, "Found the following interfaces:\n"); 29645349Swosch for (f = 0; f < nifs; f++) 29745349Swosch if (ifs[f] != NULL) 29869278Sasmodai log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]); 29969278Sasmodai debug_done = 1; 30080675Sasmodai } 30169278Sasmodai 30270110Swosch if (idx < 1 || idx > nifs || ifs[idx-1] == NULL) 30369278Sasmodai return NumStr(idx, NULL, 0); 30469278Sasmodai 30569278Sasmodai return ifs[idx-1]; 30657000Swosch} 30745349Swosch 30869277Sasmodaivoid 30945349Swoschroute_ParseHdr(struct rt_msghdr *rtm, struct sockaddr *sa[RTAX_MAX]) 31066542Sitojun{ 31169277Sasmodai char *wp; 31257000Swosch int rtax; 31370110Swosch 31445349Swosch wp = (char *)(rtm + 1); 31557000Swosch 31669277Sasmodai for (rtax = 0; rtax < RTAX_MAX; rtax++) 31770110Swosch if (rtm->rtm_addrs & (1 << rtax)) { 31870110Swosch sa[rtax] = (struct sockaddr *)wp; 31942589Swosch wp += ROUNDUP(sa[rtax]->sa_len); 32070110Swosch if (sa[rtax]->sa_family == 0) 32170110Swosch sa[rtax] = NULL; /* ??? */ 32246321Swosch } else 32345349Swosch sa[rtax] = NULL; 32445349Swosch} 32557000Swosch 32646318Swoschint 32770110Swoschroute_Show(struct cmdargs const *arg) 32856406Swosch{ 32955389Sbillf struct rt_msghdr *rtm; 33055389Sbillf struct sockaddr *sa[RTAX_MAX]; 33157000Swosch char *sp, *ep, *cp; 33255389Sbillf size_t needed; 33355389Sbillf int mib[6]; 33455389Sbillf 33570110Swosch mib[0] = CTL_NET; 33658448Swosch mib[1] = PF_ROUTE; 33758448Swosch mib[2] = 0; 33865412Swosch mib[3] = 0; 33964612Salex mib[4] = NET_RT_DUMP; 34064612Salex mib[5] = 0; 34165411Swosch if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 34265974Swosch log_Printf(LogERROR, "route_Show: sysctl: estimate: %s\n", strerror(errno)); 34369277Sasmodai return (1); 34469277Sasmodai } 34570119Swosch sp = malloc(needed); 34669277Sasmodai if (sp == NULL) 34770111Swosch return (1); 34880675Sasmodai if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 34975833Swosch log_Printf(LogERROR, "route_Show: sysctl: getroute: %s\n", strerror(errno)); 35078867Sitojun free(sp); 35179603Sitojun return (1); 35284087Swosch } 35383686Sobrien ep = sp + needed; 35480675Sasmodai 35587201Swosch prompt_Printf(arg->prompt, "%-20s%-20sFlags Netif\n", 35692013Swosch "Destination", "Gateway"); 357101331Swosch for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 358101331Swosch rtm = (struct rt_msghdr *)cp; 359101332Swosch 360104659Smurray route_ParseHdr(rtm, sa); 36124424Swosch 36224424Swosch if (sa[RTAX_DST] && sa[RTAX_GATEWAY]) { 36324424Swosch p_sockaddr(arg->prompt, sa[RTAX_DST], sa[RTAX_NETMASK], 20); 36424424Swosch p_sockaddr(arg->prompt, sa[RTAX_GATEWAY], NULL, 20); 36524424Swosch 36624424Swosch p_flags(arg->prompt, rtm->rtm_flags, 6); 36724424Swosch prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index)); 36824424Swosch } else 36924424Swosch prompt_Printf(arg->prompt, "<can't parse routing entry>\n"); 37024424Swosch } 37124424Swosch free(sp); 37224424Swosch return 0; 37324424Swosch} 37424424Swosch 37524424Swosch/* 37625031Swosch * Delete routes associated with our interface 37725031Swosch */ 37824424Swoschvoid 37924424Swoschroute_IfDelete(struct bundle *bundle, int all) 38024424Swosch{ 38124424Swosch struct rt_msghdr *rtm; 38259769Sgrog struct sockaddr *sa[RTAX_MAX]; 38379514Sitojun struct ncprange range; 38424424Swosch int pass; 38525031Swosch size_t needed; 38625031Swosch char *sp, *cp, *ep; 38725031Swosch int mib[6]; 38825031Swosch 38959769Sgrog log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index); 39025031Swosch 39131658Swosch mib[0] = CTL_NET; 39279514Sitojun mib[1] = PF_ROUTE; 39331658Swosch mib[2] = 0; 39459769Sgrog mib[3] = 0; 39579514Sitojun mib[4] = NET_RT_DUMP; 39659769Sgrog mib[5] = 0; 39765415Swosch if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 39879514Sitojun log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 39965415Swosch strerror(errno)); 40075834Swosch return; 40179514Sitojun } 40275834Swosch 40375834Swosch sp = malloc(needed); 40475834Swosch if (sp == NULL) 40525031Swosch return; 40625031Swosch 40725031Swosch if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 40859769Sgrog log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 40925031Swosch strerror(errno)); 41025031Swosch free(sp); 41131658Swosch return; 41225031Swosch } 41324424Swosch ep = sp + needed; 41472877Swosch 41567388Swosch for (pass = 0; pass < 2; pass++) { 41642589Swosch /* 41750970Speter * We do 2 passes. The first deletes all cloned routes. The second 418 * deletes all non-cloned routes. This is done to avoid 419 * potential errors from trying to delete route X after route Y where 420 * route X was cloned from route Y (and is no longer there 'cos it 421 * may have gone with route Y). 422 */ 423 if (RTF_WASCLONED == 0 && pass == 0) 424 /* So we can't tell ! */ 425 continue; 426 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 427 rtm = (struct rt_msghdr *)cp; 428 route_ParseHdr(rtm, sa); 429 if (rtm->rtm_index == bundle->iface->index && 430 sa[RTAX_DST] && sa[RTAX_GATEWAY] && 431 (sa[RTAX_DST]->sa_family == AF_INET 432#ifndef NOINET6 433 || sa[RTAX_DST]->sa_family == AF_INET6 434#endif 435 ) && 436 (all || (rtm->rtm_flags & RTF_GATEWAY))) { 437 if (log_IsKept(LogDEBUG)) { 438 char gwstr[41]; 439 struct ncpaddr gw; 440 ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]); 441 ncpaddr_setsa(&gw, sa[RTAX_GATEWAY]); 442 snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(&gw)); 443 log_Printf(LogDEBUG, "Found %s %s\n", ncprange_ntoa(&range), gwstr); 444 } 445 if (sa[RTAX_GATEWAY]->sa_family == AF_INET || 446#ifndef NOINET6 447 sa[RTAX_GATEWAY]->sa_family == AF_INET6 || 448#endif 449 sa[RTAX_GATEWAY]->sa_family == AF_LINK) { 450 if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) || 451 (pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) { 452 ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]); 453 rt_Set(bundle, RTM_DELETE, &range, NULL, 0, 0); 454 } else 455 log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass); 456 } else 457 log_Printf(LogDEBUG, 458 "route_IfDelete: Can't remove routes for family %d\n", 459 sa[RTAX_GATEWAY]->sa_family); 460 } 461 } 462 } 463 free(sp); 464} 465 466 467/* 468 * Update the MTU on all routes for the given interface 469 */ 470void 471route_UpdateMTU(struct bundle *bundle) 472{ 473 struct rt_msghdr *rtm; 474 struct sockaddr *sa[RTAX_MAX]; 475 struct ncprange dst; 476 size_t needed; 477 char *sp, *cp, *ep; 478 int mib[6]; 479 480 log_Printf(LogDEBUG, "route_UpdateMTU (%d)\n", bundle->iface->index); 481 482 mib[0] = CTL_NET; 483 mib[1] = PF_ROUTE; 484 mib[2] = 0; 485 mib[3] = 0; 486 mib[4] = NET_RT_DUMP; 487 mib[5] = 0; 488 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 489 log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 490 strerror(errno)); 491 return; 492 } 493 494 sp = malloc(needed); 495 if (sp == NULL) 496 return; 497 498 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 499 log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 500 strerror(errno)); 501 free(sp); 502 return; 503 } 504 ep = sp + needed; 505 506 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 507 rtm = (struct rt_msghdr *)cp; 508 route_ParseHdr(rtm, sa); 509 if (sa[RTAX_DST] && (sa[RTAX_DST]->sa_family == AF_INET 510#ifndef NOINET6 511 || sa[RTAX_DST]->sa_family == AF_INET6 512#endif 513 ) && 514 sa[RTAX_GATEWAY] && rtm->rtm_index == bundle->iface->index) { 515 if (log_IsKept(LogTCPIP)) { 516 ncprange_setsa(&dst, sa[RTAX_DST], sa[RTAX_NETMASK]); 517 log_Printf(LogTCPIP, "route_UpdateMTU: Netif: %d (%s), dst %s," 518 " mtu %d\n", rtm->rtm_index, Index2Nam(rtm->rtm_index), 519 ncprange_ntoa(&dst), bundle->iface->mtu); 520 } 521 rt_Update(bundle, sa[RTAX_DST], sa[RTAX_GATEWAY], sa[RTAX_NETMASK]); 522 } 523 } 524 525 free(sp); 526} 527 528int 529GetIfIndex(char *name) 530{ 531 int idx; 532 const char *got; 533 534 idx = 1; 535 while (strcmp(got = Index2Nam(idx), "???")) 536 if (!strcmp(got, name)) 537 return idx; 538 else 539 idx++; 540 return -1; 541} 542 543void 544route_Change(struct bundle *bundle, struct sticky_route *r, 545 const struct ncpaddr *me, const struct ncpaddr *peer) 546{ 547 struct ncpaddr dst; 548 549 for (; r; r = r->next) { 550 ncprange_getaddr(&r->dst, &dst); 551 if (ncpaddr_family(me) == AF_INET) { 552 if ((r->type & ROUTE_DSTMYADDR) && !ncpaddr_equal(&dst, me)) { 553 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 554 ncprange_sethost(&r->dst, me); 555 if (r->type & ROUTE_GWHISADDR) 556 ncpaddr_copy(&r->gw, peer); 557 } else if ((r->type & ROUTE_DSTHISADDR) && !ncpaddr_equal(&dst, peer)) { 558 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 559 ncprange_sethost(&r->dst, peer); 560 if (r->type & ROUTE_GWHISADDR) 561 ncpaddr_copy(&r->gw, peer); 562 } else if ((r->type & ROUTE_DSTDNS0) && !ncpaddr_equal(&dst, peer)) { 563 if (bundle->ncp.ipcp.ns.dns[0].s_addr == INADDR_NONE) 564 continue; 565 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 566 if (r->type & ROUTE_GWHISADDR) 567 ncpaddr_copy(&r->gw, peer); 568 } else if ((r->type & ROUTE_DSTDNS1) && !ncpaddr_equal(&dst, peer)) { 569 if (bundle->ncp.ipcp.ns.dns[1].s_addr == INADDR_NONE) 570 continue; 571 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 572 if (r->type & ROUTE_GWHISADDR) 573 ncpaddr_copy(&r->gw, peer); 574 } else if ((r->type & ROUTE_GWHISADDR) && !ncpaddr_equal(&r->gw, peer)) 575 ncpaddr_copy(&r->gw, peer); 576#ifndef NOINET6 577 } else if (ncpaddr_family(me) == AF_INET6) { 578 if ((r->type & ROUTE_DSTMYADDR6) && !ncpaddr_equal(&dst, me)) { 579 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 580 ncprange_sethost(&r->dst, me); 581 if (r->type & ROUTE_GWHISADDR) 582 ncpaddr_copy(&r->gw, peer); 583 } else if ((r->type & ROUTE_DSTHISADDR6) && !ncpaddr_equal(&dst, peer)) { 584 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 585 ncprange_sethost(&r->dst, peer); 586 if (r->type & ROUTE_GWHISADDR) 587 ncpaddr_copy(&r->gw, peer); 588 } else if ((r->type & ROUTE_GWHISADDR6) && !ncpaddr_equal(&r->gw, peer)) 589 ncpaddr_copy(&r->gw, peer); 590#endif 591 } 592 rt_Set(bundle, RTM_ADD, &r->dst, &r->gw, 1, 0); 593 } 594} 595 596void 597route_Add(struct sticky_route **rp, int type, const struct ncprange *dst, 598 const struct ncpaddr *gw) 599{ 600 struct sticky_route *r; 601 int dsttype = type & ROUTE_DSTANY; 602 603 r = NULL; 604 while (*rp) { 605 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 606 (!dsttype && ncprange_equal(&(*rp)->dst, dst))) { 607 /* Oops, we already have this route - unlink it */ 608 free(r); /* impossible really */ 609 r = *rp; 610 *rp = r->next; 611 } else 612 rp = &(*rp)->next; 613 } 614 615 if (!r) 616 r = (struct sticky_route *)malloc(sizeof(struct sticky_route)); 617 r->type = type; 618 r->next = NULL; 619 ncprange_copy(&r->dst, dst); 620 ncpaddr_copy(&r->gw, gw); 621 *rp = r; 622} 623 624void 625route_Delete(struct sticky_route **rp, int type, const struct ncprange *dst) 626{ 627 struct sticky_route *r; 628 int dsttype = type & ROUTE_DSTANY; 629 630 for (; *rp; rp = &(*rp)->next) { 631 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 632 (!dsttype && ncprange_equal(dst, &(*rp)->dst))) { 633 r = *rp; 634 *rp = r->next; 635 free(r); 636 break; 637 } 638 } 639} 640 641void 642route_DeleteAll(struct sticky_route **rp) 643{ 644 struct sticky_route *r, *rn; 645 646 for (r = *rp; r; r = rn) { 647 rn = r->next; 648 free(r); 649 } 650 *rp = NULL; 651} 652 653void 654route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag, 655 int indent) 656{ 657 int tlen = strlen(tag); 658 659 if (tlen + 2 > indent) 660 prompt_Printf(p, "%s:\n%*s", tag, indent, ""); 661 else 662 prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, ""); 663 664 for (; r; r = r->next) { 665 prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, ""); 666 tlen = 0; 667 if (r->type & ROUTE_DSTMYADDR) 668 prompt_Printf(p, "MYADDR"); 669 else if (r->type & ROUTE_DSTMYADDR6) 670 prompt_Printf(p, "MYADDR6"); 671 else if (r->type & ROUTE_DSTHISADDR) 672 prompt_Printf(p, "HISADDR"); 673 else if (r->type & ROUTE_DSTHISADDR6) 674 prompt_Printf(p, "HISADDR6"); 675 else if (r->type & ROUTE_DSTDNS0) 676 prompt_Printf(p, "DNS0"); 677 else if (r->type & ROUTE_DSTDNS1) 678 prompt_Printf(p, "DNS1"); 679 else if (ncprange_isdefault(&r->dst)) 680 prompt_Printf(p, "default"); 681 else 682 prompt_Printf(p, "%s", ncprange_ntoa(&r->dst)); 683 684 if (r->type & ROUTE_GWHISADDR) 685 prompt_Printf(p, " HISADDR\n"); 686 else if (r->type & ROUTE_GWHISADDR6) 687 prompt_Printf(p, " HISADDR6\n"); 688 else 689 prompt_Printf(p, " %s\n", ncpaddr_ntoa(&r->gw)); 690 } 691} 692 693struct rtmsg { 694 struct rt_msghdr m_rtm; 695 char m_space[256]; 696}; 697 698static size_t 699memcpy_roundup(char *cp, const void *data, size_t len) 700{ 701 size_t padlen; 702 703 padlen = ROUNDUP(len); 704 memcpy(cp, data, len); 705 if (padlen > len) 706 memset(cp + len, '\0', padlen - len); 707 708 return padlen; 709} 710 711int 712rt_Set(struct bundle *bundle, int cmd, const struct ncprange *dst, 713 const struct ncpaddr *gw, int bang, int quiet) 714{ 715 struct rtmsg rtmes; 716 int s, nb, wb; 717 char *cp; 718 const char *cmdstr; 719 struct sockaddr_storage sadst, samask, sagw; 720 int result = 1; 721 722 if (bang) 723 cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 724 else 725 cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 726 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 727 if (s < 0) { 728 log_Printf(LogERROR, "rt_Set: socket(): %s\n", strerror(errno)); 729 return result; 730 } 731 memset(&rtmes, '\0', sizeof rtmes); 732 rtmes.m_rtm.rtm_version = RTM_VERSION; 733 rtmes.m_rtm.rtm_type = cmd; 734 rtmes.m_rtm.rtm_addrs = RTA_DST; 735 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 736 rtmes.m_rtm.rtm_pid = getpid(); 737 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 738 739 if (cmd == RTM_ADD) { 740 if (bundle->ncp.cfg.sendpipe > 0) { 741 rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe; 742 rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 743 } 744 if (bundle->ncp.cfg.recvpipe > 0) { 745 rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe; 746 rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 747 } 748 } 749 750 ncprange_getsa(dst, &sadst, &samask); 751 752 cp = rtmes.m_space; 753 cp += memcpy_roundup(cp, &sadst, sadst.ss_len); 754 if (cmd == RTM_ADD) { 755 if (gw == NULL) { 756 log_Printf(LogERROR, "rt_Set: Program error\n"); 757 close(s); 758 return result; 759 } 760 ncpaddr_getsa(gw, &sagw); 761 if (ncpaddr_isdefault(gw)) { 762 if (!quiet) 763 log_Printf(LogERROR, "rt_Set: Cannot add a route with" 764 " gateway 0.0.0.0\n"); 765 close(s); 766 return result; 767 } else { 768 cp += memcpy_roundup(cp, &sagw, sagw.ss_len); 769 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 770 } 771 } 772 773 if (!ncprange_ishost(dst)) { 774 cp += memcpy_roundup(cp, &samask, samask.ss_len); 775 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 776 } 777 778 nb = cp - (char *)&rtmes; 779 rtmes.m_rtm.rtm_msglen = nb; 780 wb = ID0write(s, &rtmes, nb); 781 if (wb < 0) { 782 log_Printf(LogTCPIP, "rt_Set failure:\n"); 783 log_Printf(LogTCPIP, "rt_Set: Cmd = %s\n", cmdstr); 784 log_Printf(LogTCPIP, "rt_Set: Dst = %s\n", ncprange_ntoa(dst)); 785 if (gw != NULL) 786 log_Printf(LogTCPIP, "rt_Set: Gateway = %s\n", ncpaddr_ntoa(gw)); 787failed: 788 if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 789 (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 790 if (!bang) { 791 log_Printf(LogWARN, "Add route failed: %s already exists\n", 792 ncprange_ntoa(dst)); 793 result = 0; /* Don't add to our dynamic list */ 794 } else { 795 rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 796 if ((wb = ID0write(s, &rtmes, nb)) < 0) 797 goto failed; 798 } 799 } else if (cmd == RTM_DELETE && 800 (rtmes.m_rtm.rtm_errno == ESRCH || 801 (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 802 if (!bang) 803 log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 804 ncprange_ntoa(dst)); 805 } else if (rtmes.m_rtm.rtm_errno == 0) { 806 if (!quiet || errno != ENETUNREACH) 807 log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 808 ncprange_ntoa(dst), strerror(errno)); 809 } else 810 log_Printf(LogWARN, "%s route failed: %s: %s\n", 811 cmdstr, ncprange_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 812 } 813 814 if (log_IsKept(LogDEBUG)) { 815 char gwstr[40]; 816 817 if (gw) 818 snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(gw)); 819 else 820 snprintf(gwstr, sizeof gwstr, "<none>"); 821 log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %s, gateway = %s\n", 822 wb, cmdstr, ncprange_ntoa(dst), gwstr); 823 } 824 close(s); 825 826 return result; 827} 828 829void 830rt_Update(struct bundle *bundle, const struct sockaddr *dst, 831 const struct sockaddr *gw, const struct sockaddr *mask) 832{ 833 struct ncprange ncpdst; 834 struct rtmsg rtmes; 835 char *p; 836 int s, wb; 837 838 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 839 if (s < 0) { 840 log_Printf(LogERROR, "rt_Update: socket(): %s\n", strerror(errno)); 841 return; 842 } 843 844 memset(&rtmes, '\0', sizeof rtmes); 845 rtmes.m_rtm.rtm_version = RTM_VERSION; 846 rtmes.m_rtm.rtm_type = RTM_CHANGE; 847 rtmes.m_rtm.rtm_addrs = 0; 848 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 849 rtmes.m_rtm.rtm_pid = getpid(); 850 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_STATIC; 851 852 if (bundle->ncp.cfg.sendpipe > 0) { 853 rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe; 854 rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 855 } 856 857 if (bundle->ncp.cfg.recvpipe > 0) { 858 rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe; 859 rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 860 } 861 862 rtmes.m_rtm.rtm_rmx.rmx_mtu = bundle->iface->mtu; 863 rtmes.m_rtm.rtm_inits |= RTV_MTU; 864 p = rtmes.m_space; 865 866 if (dst) { 867 rtmes.m_rtm.rtm_addrs |= RTA_DST; 868 p += memcpy_roundup(p, dst, dst->sa_len); 869 } 870 871 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 872 p += memcpy_roundup(p, gw, gw->sa_len); 873 if (mask) { 874 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 875 p += memcpy_roundup(p, mask, mask->sa_len); 876 } 877 878 rtmes.m_rtm.rtm_msglen = p - (char *)&rtmes; 879 880 wb = ID0write(s, &rtmes, rtmes.m_rtm.rtm_msglen); 881 if (wb < 0) { 882 ncprange_setsa(&ncpdst, dst, mask); 883 884 log_Printf(LogTCPIP, "rt_Update failure:\n"); 885 log_Printf(LogTCPIP, "rt_Update: Dst = %s\n", ncprange_ntoa(&ncpdst)); 886 887 if (rtmes.m_rtm.rtm_errno == 0) 888 log_Printf(LogWARN, "%s: Change route failed: errno: %s\n", 889 ncprange_ntoa(&ncpdst), strerror(errno)); 890 else 891 log_Printf(LogWARN, "%s: Change route failed: %s\n", 892 ncprange_ntoa(&ncpdst), strerror(rtmes.m_rtm.rtm_errno)); 893 } 894 close(s); 895} 896