route.c revision 256281
1114902Sscottl/*- 2114902Sscottl * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 3114902Sscottl * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 4114902Sscottl * Internet Initiative Japan, Inc (IIJ) 5114902Sscottl * All rights reserved. 6114902Sscottl * 7114902Sscottl * Redistribution and use in source and binary forms, with or without 8114902Sscottl * modification, are permitted provided that the following conditions 9114902Sscottl * are met: 10114902Sscottl * 1. Redistributions of source code must retain the above copyright 11114902Sscottl * notice, this list of conditions and the following disclaimer. 12114902Sscottl * 2. Redistributions in binary form must reproduce the above copyright 13114902Sscottl * notice, this list of conditions and the following disclaimer in the 14114902Sscottl * documentation and/or other materials provided with the distribution. 15114902Sscottl * 16114902Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17114902Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18114902Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19114902Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20114902Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21114902Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22114902Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23114902Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24114902Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25114902Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26114902Sscottl * SUCH DAMAGE. 27114902Sscottl * 28114902Sscottl * $FreeBSD: stable/10/usr.sbin/ppp/route.c 191316 2009-04-20 14:38:48Z bz $ 29114902Sscottl */ 30114902Sscottl 31114902Sscottl#include <sys/param.h> 32114902Sscottl#include <sys/socket.h> 33114902Sscottl#include <net/if_types.h> 34114902Sscottl#include <net/route.h> 35129879Sphk#include <net/if.h> 36114902Sscottl#include <netinet/in.h> 37114902Sscottl#include <arpa/inet.h> 38114902Sscottl#include <net/if_dl.h> 39114902Sscottl#include <netinet/in_systm.h> 40114902Sscottl#include <netinet/ip.h> 41114902Sscottl#include <sys/un.h> 42114902Sscottl 43126364Sscottl#include <errno.h> 44122999Smbr#include <stdarg.h> 45114902Sscottl#include <stdio.h> 46114902Sscottl#include <stdlib.h> 47114902Sscottl#include <string.h> 48114902Sscottl#include <sys/sysctl.h> 49114902Sscottl#include <termios.h> 50119280Simp#include <unistd.h> 51119280Simp 52114902Sscottl#include "layer.h" 53129859Sscottl#include "defs.h" 54129859Sscottl#include "command.h" 55114902Sscottl#include "mbuf.h" 56114902Sscottl#include "log.h" 57114902Sscottl#include "iplist.h" 58114902Sscottl#include "timer.h" 59127205Sscottl#include "throughput.h" 60114902Sscottl#include "lqr.h" 61114902Sscottl#include "hdlc.h" 62127487Sscottl#include "fsm.h" 63114902Sscottl#include "lcp.h" 64114902Sscottl#include "ccp.h" 65114902Sscottl#include "link.h" 66114902Sscottl#include "slcompress.h" 67114902Sscottl#include "ncpaddr.h" 68114902Sscottl#include "ipcp.h" 69114902Sscottl#include "filter.h" 70114902Sscottl#include "descriptor.h" 71114902Sscottl#include "mp.h" 72114902Sscottl#ifndef NORADIUS 73140923Sscottl#include "radius.h" 74114902Sscottl#endif 75114902Sscottl#include "ipv6cp.h" 76114902Sscottl#include "ncp.h" 77114902Sscottl#include "bundle.h" 78114902Sscottl#include "route.h" 79140923Sscottl#include "prompt.h" 80114902Sscottl#include "iface.h" 81114902Sscottl#include "id.h" 82114902Sscottl 83114902Sscottl 84114902Sscottlstatic void 85114902Sscottlp_sockaddr(struct prompt *prompt, struct sockaddr *phost, 86114902Sscottl struct sockaddr *pmask, int width) 87114902Sscottl{ 88114902Sscottl struct ncprange range; 89114902Sscottl char buf[29]; 90114902Sscottl struct sockaddr_dl *dl = (struct sockaddr_dl *)phost; 91114902Sscottl 92122999Smbr if (log_IsKept(LogDEBUG)) { 93114902Sscottl char tmp[50]; 94114902Sscottl 95114902Sscottl log_Printf(LogDEBUG, "Found the following sockaddr:\n"); 96114902Sscottl log_Printf(LogDEBUG, " Family %d, len %d\n", 97114902Sscottl (int)phost->sa_family, (int)phost->sa_len); 98114902Sscottl inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp); 99114902Sscottl log_Printf(LogDEBUG, " Addr %s\n", tmp); 100114902Sscottl if (pmask) { 101114902Sscottl inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp); 102114902Sscottl log_Printf(LogDEBUG, " Mask %s\n", tmp); 103114902Sscottl } 104114902Sscottl } 105114902Sscottl 106114902Sscottl switch (phost->sa_family) { 107114902Sscottl case AF_INET: 108114902Sscottl#ifndef NOINET6 109114902Sscottl case AF_INET6: 110114902Sscottl#endif 111114902Sscottl ncprange_setsa(&range, phost, pmask); 112114902Sscottl if (ncprange_isdefault(&range)) 113114902Sscottl prompt_Printf(prompt, "%-*s ", width - 1, "default"); 114114902Sscottl else 115114902Sscottl prompt_Printf(prompt, "%-*s ", width - 1, ncprange_ntoa(&range)); 116114902Sscottl return; 117114902Sscottl 118114902Sscottl case AF_LINK: 119114902Sscottl if (dl->sdl_nlen) 120114902Sscottl snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data); 121114902Sscottl else if (dl->sdl_alen) { 122114902Sscottl if (dl->sdl_type == IFT_ETHER) { 123114902Sscottl if (dl->sdl_alen < sizeof buf / 3) { 124114902Sscottl int f; 125114902Sscottl u_char *MAC; 126114902Sscottl 127114902Sscottl MAC = (u_char *)dl->sdl_data + dl->sdl_nlen; 128114902Sscottl for (f = 0; f < dl->sdl_alen; f++) 129114902Sscottl sprintf(buf+f*3, "%02x:", MAC[f]); 130114902Sscottl buf[f*3-1] = '\0'; 131114902Sscottl } else 132114902Sscottl strcpy(buf, "??:??:??:??:??:??"); 133114902Sscottl } else 134114902Sscottl sprintf(buf, "<IFT type %d>", dl->sdl_type); 135114902Sscottl } else if (dl->sdl_slen) 136114902Sscottl sprintf(buf, "<slen %d?>", dl->sdl_slen); 137114902Sscottl else 138114902Sscottl sprintf(buf, "link#%d", dl->sdl_index); 139114902Sscottl break; 140114902Sscottl 141114902Sscottl default: 142114902Sscottl sprintf(buf, "<AF type %d>", phost->sa_family); 143114902Sscottl break; 144114902Sscottl } 145114902Sscottl 146114902Sscottl prompt_Printf(prompt, "%-*s ", width-1, buf); 147114902Sscottl} 148114902Sscottl 149114902Sscottlstatic struct bits { 150114902Sscottl u_int32_t b_mask; 151122999Smbr char b_val; 152114902Sscottl} bits[] = { 153150535Sscottl { RTF_UP, 'U' }, 154114902Sscottl { RTF_GATEWAY, 'G' }, 155150535Sscottl { RTF_HOST, 'H' }, 156150535Sscottl { RTF_REJECT, 'R' }, 157150535Sscottl { RTF_DYNAMIC, 'D' }, 158150535Sscottl { RTF_MODIFIED, 'M' }, 159150535Sscottl { RTF_DONE, 'd' }, 160150535Sscottl { RTF_XRESOLVE, 'X' }, 161150535Sscottl#ifdef RTF_CLONING 162150535Sscottl { RTF_CLONING, 'C' }, 163150535Sscottl#endif 164150535Sscottl { RTF_STATIC, 'S' }, 165150535Sscottl { RTF_PROTO1, '1' }, 166150535Sscottl { RTF_PROTO2, '2' }, 167150535Sscottl { RTF_BLACKHOLE, 'B' }, 168114902Sscottl 169150535Sscottl#ifdef RTF_LLINFO 170150535Sscottl { RTF_LLINFO, 'L' }, 171150535Sscottl#endif 172150535Sscottl#ifdef RTF_CLONING 173150535Sscottl { RTF_CLONING, 'C' }, 174150535Sscottl#endif 175150535Sscottl#ifdef RTF_WASCLONED 176150535Sscottl { RTF_WASCLONED, 'W' }, 177122999Smbr#endif 178114902Sscottl#ifdef RTF_PRCLONING 179122999Smbr { RTF_PRCLONING, 'c' }, 180114902Sscottl#endif 181122999Smbr#ifdef RTF_PROTO3 182122999Smbr { RTF_PROTO3, '3' }, 183122999Smbr#endif 184122999Smbr#ifdef RTF_BROADCAST 185122999Smbr { RTF_BROADCAST, 'b' }, 186122999Smbr#endif 187122999Smbr { 0, '\0' } 188122999Smbr}; 189122999Smbr 190122999Smbr#ifndef RTF_WASCLONED 191122999Smbr#define RTF_WASCLONED (0) 192122999Smbr#endif 193122999Smbr 194122999Smbrstatic void 195122999Smbrp_flags(struct prompt *prompt, u_int32_t f, unsigned max) 196122999Smbr{ 197122999Smbr char name[33], *flags; 198122999Smbr register struct bits *p = bits; 199122999Smbr 200122999Smbr if (max > sizeof name - 1) 201122999Smbr max = sizeof name - 1; 202122999Smbr 203122999Smbr for (flags = name; p->b_mask && flags - name < (int)max; p++) 204122999Smbr if (p->b_mask & f) 205122999Smbr *flags++ = p->b_val; 206122999Smbr *flags = '\0'; 207122999Smbr prompt_Printf(prompt, "%-*.*s", (int)max, (int)max, name); 208122999Smbr} 209122999Smbr 210122999Smbrstatic int route_nifs = -1; 211122999Smbr 212122999Smbrconst char * 213114902SscottlIndex2Nam(int idx) 214114902Sscottl{ 215114902Sscottl /* 216114902Sscottl * XXX: Maybe we should select() on the routing socket so that we can 217114902Sscottl * notice interfaces that come & go (PCCARD support). 218114902Sscottl * Or we could even support a signal that resets these so that 219114902Sscottl * the PCCARD insert/remove events can signal ppp. 220114902Sscottl */ 221114902Sscottl static char **ifs; /* Figure these out once */ 222114902Sscottl static int debug_done; /* Debug once */ 223114902Sscottl 224114902Sscottl if (idx > route_nifs || (idx > 0 && ifs[idx-1] == NULL)) { 225114902Sscottl int mib[6], have, had; 226114902Sscottl size_t needed; 227114902Sscottl char *buf, *ptr, *end; 228114902Sscottl struct sockaddr_dl *dl; 229114902Sscottl struct if_msghdr *ifm; 230150535Sscottl 231114902Sscottl if (ifs) { 232150535Sscottl free(ifs); 233150535Sscottl ifs = NULL; 234150535Sscottl route_nifs = 0; 235150535Sscottl } 236150535Sscottl debug_done = 0; 237114902Sscottl 238114902Sscottl mib[0] = CTL_NET; 239114902Sscottl mib[1] = PF_ROUTE; 240114902Sscottl mib[2] = 0; 241114902Sscottl mib[3] = 0; 242114902Sscottl mib[4] = NET_RT_IFLIST; 243114902Sscottl mib[5] = 0; 244150535Sscottl 245114902Sscottl if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 246114902Sscottl log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n", 247114902Sscottl strerror(errno)); 248114902Sscottl return NumStr(idx, NULL, 0); 249114902Sscottl } 250114902Sscottl if ((buf = malloc(needed)) == NULL) 251114902Sscottl return NumStr(idx, NULL, 0); 252114902Sscottl if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 253114902Sscottl free(buf); 254114902Sscottl return NumStr(idx, NULL, 0); 255114902Sscottl } 256114902Sscottl end = buf + needed; 257114902Sscottl 258114902Sscottl have = 0; 259114902Sscottl for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) { 260114902Sscottl ifm = (struct if_msghdr *)ptr; 261114902Sscottl if (ifm->ifm_type != RTM_IFINFO) 262114902Sscottl continue; 263114902Sscottl dl = (struct sockaddr_dl *)(ifm + 1); 264114902Sscottl if (ifm->ifm_index > 0) { 265114902Sscottl if (ifm->ifm_index > have) { 266114902Sscottl char **newifs; 267114902Sscottl 268114902Sscottl had = have; 269114902Sscottl have = ifm->ifm_index + 5; 270114902Sscottl if (had) 271114902Sscottl newifs = (char **)realloc(ifs, sizeof(char *) * have); 272114902Sscottl else 273114902Sscottl newifs = (char **)malloc(sizeof(char *) * have); 274114902Sscottl if (!newifs) { 275114902Sscottl log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno)); 276114902Sscottl route_nifs = 0; 277114902Sscottl if (ifs) { 278114902Sscottl free(ifs); 279114902Sscottl ifs = NULL; 280114902Sscottl } 281114902Sscottl free(buf); 282114902Sscottl return NumStr(idx, NULL, 0); 283114902Sscottl } 284114902Sscottl ifs = newifs; 285114902Sscottl memset(ifs + had, '\0', sizeof(char *) * (have - had)); 286114902Sscottl } 287114902Sscottl if (ifs[ifm->ifm_index-1] == NULL) { 288114902Sscottl ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1); 289114902Sscottl if (ifs[ifm->ifm_index-1] == NULL) 290114902Sscottl log_Printf(LogDEBUG, "Skipping interface %d: Out of memory\n", 291114902Sscottl ifm->ifm_index); 292114902Sscottl else { 293114902Sscottl memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen); 294114902Sscottl ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0'; 295114902Sscottl if (route_nifs < ifm->ifm_index) 296114902Sscottl route_nifs = ifm->ifm_index; 297114902Sscottl } 298114902Sscottl } 299114902Sscottl } else if (log_IsKept(LogDEBUG)) 300114902Sscottl log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n", 301114902Sscottl ifm->ifm_index); 302122999Smbr } 303122999Smbr free(buf); 304122999Smbr } 305122999Smbr 306122999Smbr if (log_IsKept(LogDEBUG) && !debug_done) { 307122999Smbr int f; 308122999Smbr 309122999Smbr log_Printf(LogDEBUG, "Found the following interfaces:\n"); 310122999Smbr for (f = 0; f < route_nifs; f++) 311122999Smbr if (ifs[f] != NULL) 312122999Smbr log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]); 313122999Smbr debug_done = 1; 314122999Smbr } 315122999Smbr 316122999Smbr if (idx < 1 || idx > route_nifs || ifs[idx-1] == NULL) 317122999Smbr return NumStr(idx, NULL, 0); 318114902Sscottl 319114902Sscottl return ifs[idx-1]; 320114902Sscottl} 321114902Sscottl 322114902Sscottlvoid 323114902Sscottlroute_ParseHdr(struct rt_msghdr *rtm, struct sockaddr *sa[RTAX_MAX]) 324114902Sscottl{ 325114902Sscottl char *wp; 326114902Sscottl int rtax; 327114902Sscottl 328114902Sscottl wp = (char *)(rtm + 1); 329114902Sscottl 330114902Sscottl for (rtax = 0; rtax < RTAX_MAX; rtax++) 331114902Sscottl if (rtm->rtm_addrs & (1 << rtax)) { 332114902Sscottl sa[rtax] = (struct sockaddr *)wp; 333114902Sscottl wp += ROUNDUP(sa[rtax]->sa_len); 334114902Sscottl if (sa[rtax]->sa_family == 0) 335114902Sscottl sa[rtax] = NULL; /* ??? */ 336114902Sscottl } else 337114902Sscottl sa[rtax] = NULL; 338114902Sscottl} 339114902Sscottl 340114902Sscottlint 341114902Sscottlroute_Show(struct cmdargs const *arg) 342114902Sscottl{ 343114902Sscottl struct rt_msghdr *rtm; 344114902Sscottl struct sockaddr *sa[RTAX_MAX]; 345114902Sscottl char *sp, *ep, *cp; 346114902Sscottl size_t needed; 347114902Sscottl int mib[6]; 348114902Sscottl 349114902Sscottl mib[0] = CTL_NET; 350114902Sscottl mib[1] = PF_ROUTE; 351114902Sscottl mib[2] = 0; 352114902Sscottl mib[3] = 0; 353114902Sscottl mib[4] = NET_RT_DUMP; 354114902Sscottl mib[5] = 0; 355114902Sscottl if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 356114902Sscottl log_Printf(LogERROR, "route_Show: sysctl: estimate: %s\n", strerror(errno)); 357114902Sscottl return (1); 358114902Sscottl } 359114902Sscottl sp = malloc(needed); 360114902Sscottl if (sp == NULL) 361114902Sscottl return (1); 362114902Sscottl if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 363114902Sscottl log_Printf(LogERROR, "route_Show: sysctl: getroute: %s\n", strerror(errno)); 364114902Sscottl free(sp); 365114902Sscottl return (1); 366114902Sscottl } 367114902Sscottl ep = sp + needed; 368114902Sscottl 369114902Sscottl prompt_Printf(arg->prompt, "%-20s%-20sFlags Netif\n", 370114902Sscottl "Destination", "Gateway"); 371114902Sscottl for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 372114902Sscottl rtm = (struct rt_msghdr *)cp; 373114902Sscottl 374114902Sscottl route_ParseHdr(rtm, sa); 375114902Sscottl 376114902Sscottl if (sa[RTAX_DST] && sa[RTAX_GATEWAY]) { 377114902Sscottl p_sockaddr(arg->prompt, sa[RTAX_DST], sa[RTAX_NETMASK], 20); 378114902Sscottl p_sockaddr(arg->prompt, sa[RTAX_GATEWAY], NULL, 20); 379114902Sscottl 380114902Sscottl p_flags(arg->prompt, rtm->rtm_flags, 6); 381114902Sscottl prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index)); 382114902Sscottl } else 383114902Sscottl prompt_Printf(arg->prompt, "<can't parse routing entry>\n"); 384114902Sscottl } 385114902Sscottl free(sp); 386114902Sscottl return 0; 387114902Sscottl} 388114902Sscottl 389114902Sscottl/* 390114902Sscottl * Delete routes associated with our interface 391114902Sscottl */ 392114902Sscottlvoid 393114902Sscottlroute_IfDelete(struct bundle *bundle, int all) 394114902Sscottl{ 395114902Sscottl struct rt_msghdr *rtm; 396114902Sscottl struct sockaddr *sa[RTAX_MAX]; 397114902Sscottl struct ncprange range; 398114902Sscottl int pass; 399114902Sscottl size_t needed; 400114902Sscottl char *sp, *cp, *ep; 401114902Sscottl int mib[6]; 402114902Sscottl 403114902Sscottl log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index); 404114902Sscottl 405114902Sscottl mib[0] = CTL_NET; 406114902Sscottl mib[1] = PF_ROUTE; 407114902Sscottl mib[2] = 0; 408140923Sscottl mib[3] = 0; 409140923Sscottl mib[4] = NET_RT_DUMP; 410114902Sscottl mib[5] = 0; 411114902Sscottl if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 412114902Sscottl log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 413114902Sscottl strerror(errno)); 414114902Sscottl return; 415114902Sscottl } 416140923Sscottl 417114902Sscottl sp = malloc(needed); 418114902Sscottl if (sp == NULL) 419114902Sscottl return; 420114902Sscottl 421114902Sscottl if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 422114902Sscottl log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 423119997Sps strerror(errno)); 424116852Sscottl free(sp); 425116852Sscottl return; 426114902Sscottl } 427114902Sscottl ep = sp + needed; 428114902Sscottl 429114902Sscottl for (pass = 0; pass < 2; pass++) { 430114902Sscottl /* 431114902Sscottl * We do 2 passes. The first deletes all cloned routes. The second 432114902Sscottl * deletes all non-cloned routes. This is done to avoid 433114902Sscottl * potential errors from trying to delete route X after route Y where 434114902Sscottl * route X was cloned from route Y (and is no longer there 'cos it 435114902Sscottl * may have gone with route Y). 436130585Sphk */ 437114902Sscottl if (RTF_WASCLONED == 0 && pass == 0) 438122999Smbr /* So we can't tell ! */ 439114902Sscottl continue; 440114902Sscottl for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 441114902Sscottl rtm = (struct rt_msghdr *)cp; 442114902Sscottl route_ParseHdr(rtm, sa); 443122999Smbr if (rtm->rtm_index == bundle->iface->index && 444122999Smbr sa[RTAX_DST] && sa[RTAX_GATEWAY] && 445114902Sscottl (sa[RTAX_DST]->sa_family == AF_INET 446114902Sscottl#ifndef NOINET6 447114902Sscottl || sa[RTAX_DST]->sa_family == AF_INET6 448140923Sscottl#endif 449140923Sscottl ) && 450114902Sscottl (all || (rtm->rtm_flags & RTF_GATEWAY))) { 451114902Sscottl if (log_IsKept(LogDEBUG)) { 452114902Sscottl char gwstr[41]; 453114902Sscottl struct ncpaddr gw; 454114902Sscottl ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]); 455141062Sscottl ncpaddr_setsa(&gw, sa[RTAX_GATEWAY]); 456114902Sscottl snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(&gw)); 457126364Sscottl log_Printf(LogDEBUG, "Found %s %s\n", ncprange_ntoa(&range), gwstr); 458126364Sscottl } 459140923Sscottl if (sa[RTAX_GATEWAY]->sa_family == AF_INET || 460126364Sscottl#ifndef NOINET6 461114902Sscottl sa[RTAX_GATEWAY]->sa_family == AF_INET6 || 462114902Sscottl#endif 463114902Sscottl sa[RTAX_GATEWAY]->sa_family == AF_LINK) { 464114902Sscottl if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) || 465114902Sscottl (pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) { 466114902Sscottl ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]); 467114902Sscottl rt_Set(bundle, RTM_DELETE, &range, NULL, 0, 0); 468114902Sscottl } else 469114902Sscottl log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass); 470114902Sscottl } else 471126364Sscottl log_Printf(LogDEBUG, 472114902Sscottl "route_IfDelete: Can't remove routes for family %d\n", 473114902Sscottl sa[RTAX_GATEWAY]->sa_family); 474122999Smbr } 475114902Sscottl } 476114902Sscottl } 477114902Sscottl free(sp); 478114902Sscottl} 479140923Sscottl 480114902Sscottl 481114902Sscottl/* 482114902Sscottl * Update the MTU on all routes for the given interface 483114902Sscottl */ 484114902Sscottlvoid 485114902Sscottlroute_UpdateMTU(struct bundle *bundle) 486141062Sscottl{ 487114902Sscottl struct rt_msghdr *rtm; 488114902Sscottl struct sockaddr *sa[RTAX_MAX]; 489114902Sscottl struct ncprange dst; 490141062Sscottl size_t needed; 491114902Sscottl char *sp, *cp, *ep; 492 int mib[6]; 493 494 log_Printf(LogDEBUG, "route_UpdateMTU (%d)\n", bundle->iface->index); 495 496 mib[0] = CTL_NET; 497 mib[1] = PF_ROUTE; 498 mib[2] = 0; 499 mib[3] = 0; 500 mib[4] = NET_RT_DUMP; 501 mib[5] = 0; 502 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 503 log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 504 strerror(errno)); 505 return; 506 } 507 508 sp = malloc(needed); 509 if (sp == NULL) 510 return; 511 512 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 513 log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 514 strerror(errno)); 515 free(sp); 516 return; 517 } 518 ep = sp + needed; 519 520 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 521 rtm = (struct rt_msghdr *)cp; 522 route_ParseHdr(rtm, sa); 523 if (sa[RTAX_DST] && (sa[RTAX_DST]->sa_family == AF_INET 524#ifndef NOINET6 525 || sa[RTAX_DST]->sa_family == AF_INET6 526#endif 527 ) && 528 sa[RTAX_GATEWAY] && rtm->rtm_index == bundle->iface->index) { 529 if (log_IsKept(LogTCPIP)) { 530 ncprange_setsa(&dst, sa[RTAX_DST], sa[RTAX_NETMASK]); 531 log_Printf(LogTCPIP, "route_UpdateMTU: Netif: %d (%s), dst %s," 532 " mtu %lu\n", rtm->rtm_index, Index2Nam(rtm->rtm_index), 533 ncprange_ntoa(&dst), bundle->iface->mtu); 534 } 535 rt_Update(bundle, sa[RTAX_DST], sa[RTAX_GATEWAY], sa[RTAX_NETMASK], 536 sa[RTAX_IFP], sa[RTAX_IFA]); 537 } 538 } 539 540 free(sp); 541} 542 543int 544GetIfIndex(char *name) 545{ 546 int idx; 547 548 idx = 1; 549 while (route_nifs == -1 || idx < route_nifs) 550 if (strcmp(Index2Nam(idx), name) == 0) 551 return idx; 552 else 553 idx++; 554 return -1; 555} 556 557void 558route_Change(struct bundle *bundle, struct sticky_route *r, 559 const struct ncpaddr *me, const struct ncpaddr *peer) 560{ 561 struct ncpaddr dst; 562 563 for (; r; r = r->next) { 564 ncprange_getaddr(&r->dst, &dst); 565 if (ncpaddr_family(me) == AF_INET) { 566 if ((r->type & ROUTE_DSTMYADDR) && !ncpaddr_equal(&dst, me)) { 567 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 568 ncprange_sethost(&r->dst, me); 569 if (r->type & ROUTE_GWHISADDR) 570 ncpaddr_copy(&r->gw, peer); 571 } else if ((r->type & ROUTE_DSTHISADDR) && !ncpaddr_equal(&dst, peer)) { 572 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 573 ncprange_sethost(&r->dst, peer); 574 if (r->type & ROUTE_GWHISADDR) 575 ncpaddr_copy(&r->gw, peer); 576 } else if ((r->type & ROUTE_DSTDNS0) && !ncpaddr_equal(&dst, peer)) { 577 if (bundle->ncp.ipcp.ns.dns[0].s_addr == INADDR_NONE) 578 continue; 579 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 580 if (r->type & ROUTE_GWHISADDR) 581 ncpaddr_copy(&r->gw, peer); 582 } else if ((r->type & ROUTE_DSTDNS1) && !ncpaddr_equal(&dst, peer)) { 583 if (bundle->ncp.ipcp.ns.dns[1].s_addr == INADDR_NONE) 584 continue; 585 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 586 if (r->type & ROUTE_GWHISADDR) 587 ncpaddr_copy(&r->gw, peer); 588 } else if ((r->type & ROUTE_GWHISADDR) && !ncpaddr_equal(&r->gw, peer)) 589 ncpaddr_copy(&r->gw, peer); 590#ifndef NOINET6 591 } else if (ncpaddr_family(me) == AF_INET6) { 592 if ((r->type & ROUTE_DSTMYADDR6) && !ncpaddr_equal(&dst, me)) { 593 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 594 ncprange_sethost(&r->dst, me); 595 if (r->type & ROUTE_GWHISADDR) 596 ncpaddr_copy(&r->gw, peer); 597 } else if ((r->type & ROUTE_DSTHISADDR6) && !ncpaddr_equal(&dst, peer)) { 598 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 599 ncprange_sethost(&r->dst, peer); 600 if (r->type & ROUTE_GWHISADDR) 601 ncpaddr_copy(&r->gw, peer); 602 } else if ((r->type & ROUTE_GWHISADDR6) && !ncpaddr_equal(&r->gw, peer)) 603 ncpaddr_copy(&r->gw, peer); 604#endif 605 } 606 rt_Set(bundle, RTM_ADD, &r->dst, &r->gw, 1, 0); 607 } 608} 609 610void 611route_Add(struct sticky_route **rp, int type, const struct ncprange *dst, 612 const struct ncpaddr *gw) 613{ 614 struct sticky_route *r; 615 int dsttype = type & ROUTE_DSTANY; 616 617 r = NULL; 618 while (*rp) { 619 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 620 (!dsttype && ncprange_equal(&(*rp)->dst, dst))) { 621 /* Oops, we already have this route - unlink it */ 622 free(r); /* impossible really */ 623 r = *rp; 624 *rp = r->next; 625 } else 626 rp = &(*rp)->next; 627 } 628 629 if (r == NULL) { 630 r = (struct sticky_route *)malloc(sizeof(struct sticky_route)); 631 if (r == NULL) { 632 log_Printf(LogERROR, "route_Add: Out of memory!\n"); 633 return; 634 } 635 } 636 r->type = type; 637 r->next = NULL; 638 ncprange_copy(&r->dst, dst); 639 ncpaddr_copy(&r->gw, gw); 640 *rp = r; 641} 642 643void 644route_Delete(struct sticky_route **rp, int type, const struct ncprange *dst) 645{ 646 struct sticky_route *r; 647 int dsttype = type & ROUTE_DSTANY; 648 649 for (; *rp; rp = &(*rp)->next) { 650 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 651 (!dsttype && ncprange_equal(dst, &(*rp)->dst))) { 652 r = *rp; 653 *rp = r->next; 654 free(r); 655 break; 656 } 657 } 658} 659 660void 661route_DeleteAll(struct sticky_route **rp) 662{ 663 struct sticky_route *r, *rn; 664 665 for (r = *rp; r; r = rn) { 666 rn = r->next; 667 free(r); 668 } 669 *rp = NULL; 670} 671 672void 673route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag, 674 int indent) 675{ 676 int tlen = strlen(tag); 677 678 if (tlen + 2 > indent) 679 prompt_Printf(p, "%s:\n%*s", tag, indent, ""); 680 else 681 prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, ""); 682 683 for (; r; r = r->next) { 684 prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, ""); 685 tlen = 0; 686 if (r->type & ROUTE_DSTMYADDR) 687 prompt_Printf(p, "MYADDR"); 688 else if (r->type & ROUTE_DSTMYADDR6) 689 prompt_Printf(p, "MYADDR6"); 690 else if (r->type & ROUTE_DSTHISADDR) 691 prompt_Printf(p, "HISADDR"); 692 else if (r->type & ROUTE_DSTHISADDR6) 693 prompt_Printf(p, "HISADDR6"); 694 else if (r->type & ROUTE_DSTDNS0) 695 prompt_Printf(p, "DNS0"); 696 else if (r->type & ROUTE_DSTDNS1) 697 prompt_Printf(p, "DNS1"); 698 else if (ncprange_isdefault(&r->dst)) 699 prompt_Printf(p, "default"); 700 else 701 prompt_Printf(p, "%s", ncprange_ntoa(&r->dst)); 702 703 if (r->type & ROUTE_GWHISADDR) 704 prompt_Printf(p, " HISADDR\n"); 705 else if (r->type & ROUTE_GWHISADDR6) 706 prompt_Printf(p, " HISADDR6\n"); 707 else 708 prompt_Printf(p, " %s\n", ncpaddr_ntoa(&r->gw)); 709 } 710} 711 712struct rtmsg { 713 struct rt_msghdr m_rtm; 714 char m_space[256]; 715}; 716 717static size_t 718memcpy_roundup(char *cp, const void *data, size_t len) 719{ 720 size_t padlen; 721 722 padlen = ROUNDUP(len); 723 memcpy(cp, data, len); 724 if (padlen > len) 725 memset(cp + len, '\0', padlen - len); 726 727 return padlen; 728} 729 730#if defined(__KAME__) && !defined(NOINET6) 731static void 732add_scope(struct sockaddr *sa, int ifindex) 733{ 734 struct sockaddr_in6 *sa6; 735 736 if (sa->sa_family != AF_INET6) 737 return; 738 sa6 = (struct sockaddr_in6 *)sa; 739 if (!IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) && 740 !IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) 741 return; 742 if (*(u_int16_t *)&sa6->sin6_addr.s6_addr[2] != 0) 743 return; 744 *(u_int16_t *)&sa6->sin6_addr.s6_addr[2] = htons(ifindex); 745} 746#endif 747 748int 749rt_Set(struct bundle *bundle, int cmd, const struct ncprange *dst, 750 const struct ncpaddr *gw, int bang, int quiet) 751{ 752 struct rtmsg rtmes; 753 int s, nb, wb; 754 char *cp; 755 const char *cmdstr; 756 struct sockaddr_storage sadst, samask, sagw; 757 int result = 1; 758 759 if (bang) 760 cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 761 else 762 cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 763 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 764 if (s < 0) { 765 log_Printf(LogERROR, "rt_Set: socket(): %s\n", strerror(errno)); 766 return result; 767 } 768 memset(&rtmes, '\0', sizeof rtmes); 769 rtmes.m_rtm.rtm_version = RTM_VERSION; 770 rtmes.m_rtm.rtm_type = cmd; 771 rtmes.m_rtm.rtm_addrs = RTA_DST; 772 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 773 rtmes.m_rtm.rtm_pid = getpid(); 774 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 775 776 if (cmd == RTM_ADD) { 777 if (bundle->ncp.cfg.sendpipe > 0) { 778 rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe; 779 rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 780 } 781 if (bundle->ncp.cfg.recvpipe > 0) { 782 rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe; 783 rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 784 } 785 } 786 787 ncprange_getsa(dst, &sadst, &samask); 788#if defined(__KAME__) && !defined(NOINET6) 789 add_scope((struct sockaddr *)&sadst, bundle->iface->index); 790#endif 791 792 cp = rtmes.m_space; 793 cp += memcpy_roundup(cp, &sadst, sadst.ss_len); 794 if (cmd == RTM_ADD) { 795 if (gw == NULL) { 796 log_Printf(LogERROR, "rt_Set: Program error\n"); 797 close(s); 798 return result; 799 } 800 ncpaddr_getsa(gw, &sagw); 801#if defined(__KAME__) && !defined(NOINET6) 802 add_scope((struct sockaddr *)&sagw, bundle->iface->index); 803#endif 804 if (ncpaddr_isdefault(gw)) { 805 if (!quiet) 806 log_Printf(LogERROR, "rt_Set: Cannot add a route with" 807 " gateway 0.0.0.0\n"); 808 close(s); 809 return result; 810 } else { 811 cp += memcpy_roundup(cp, &sagw, sagw.ss_len); 812 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 813 } 814 } 815 816 if (!ncprange_ishost(dst)) { 817 cp += memcpy_roundup(cp, &samask, samask.ss_len); 818 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 819 } 820 821 nb = cp - (char *)&rtmes; 822 rtmes.m_rtm.rtm_msglen = nb; 823 wb = ID0write(s, &rtmes, nb); 824 if (wb < 0) { 825 log_Printf(LogTCPIP, "rt_Set failure:\n"); 826 log_Printf(LogTCPIP, "rt_Set: Cmd = %s\n", cmdstr); 827 log_Printf(LogTCPIP, "rt_Set: Dst = %s\n", ncprange_ntoa(dst)); 828 if (gw != NULL) 829 log_Printf(LogTCPIP, "rt_Set: Gateway = %s\n", ncpaddr_ntoa(gw)); 830failed: 831 if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 832 (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 833 if (!bang) { 834 log_Printf(LogWARN, "Add route failed: %s already exists\n", 835 ncprange_ntoa(dst)); 836 result = 0; /* Don't add to our dynamic list */ 837 } else { 838 rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 839 if ((wb = ID0write(s, &rtmes, nb)) < 0) 840 goto failed; 841 } 842 } else if (cmd == RTM_DELETE && 843 (rtmes.m_rtm.rtm_errno == ESRCH || 844 (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 845 if (!bang) 846 log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 847 ncprange_ntoa(dst)); 848 } else if (rtmes.m_rtm.rtm_errno == 0) { 849 if (!quiet || errno != ENETUNREACH) 850 log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 851 ncprange_ntoa(dst), strerror(errno)); 852 } else 853 log_Printf(LogWARN, "%s route failed: %s: %s\n", 854 cmdstr, ncprange_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 855 } 856 857 if (log_IsKept(LogDEBUG)) { 858 char gwstr[40]; 859 860 if (gw) 861 snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(gw)); 862 else 863 snprintf(gwstr, sizeof gwstr, "<none>"); 864 log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %s, gateway = %s\n", 865 wb, cmdstr, ncprange_ntoa(dst), gwstr); 866 } 867 close(s); 868 869 return result; 870} 871 872void 873rt_Update(struct bundle *bundle, const struct sockaddr *dst, 874 const struct sockaddr *gw, const struct sockaddr *mask, 875 const struct sockaddr *ifp, const struct sockaddr *ifa) 876{ 877 struct ncprange ncpdst; 878 struct rtmsg rtmes; 879 char *p; 880 int s, wb; 881 882 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 883 if (s < 0) { 884 log_Printf(LogERROR, "rt_Update: socket(): %s\n", strerror(errno)); 885 return; 886 } 887 888 memset(&rtmes, '\0', sizeof rtmes); 889 rtmes.m_rtm.rtm_version = RTM_VERSION; 890 rtmes.m_rtm.rtm_type = RTM_CHANGE; 891 rtmes.m_rtm.rtm_addrs = 0; 892 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 893 rtmes.m_rtm.rtm_pid = getpid(); 894 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_STATIC; 895 896 if (bundle->ncp.cfg.sendpipe > 0) { 897 rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe; 898 rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 899 } 900 901 if (bundle->ncp.cfg.recvpipe > 0) { 902 rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe; 903 rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 904 } 905 906 rtmes.m_rtm.rtm_rmx.rmx_mtu = bundle->iface->mtu; 907 rtmes.m_rtm.rtm_inits |= RTV_MTU; 908 p = rtmes.m_space; 909 910 if (dst) { 911 rtmes.m_rtm.rtm_addrs |= RTA_DST; 912 p += memcpy_roundup(p, dst, dst->sa_len); 913 } 914 915 if (gw) { 916 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 917 p += memcpy_roundup(p, gw, gw->sa_len); 918 } 919 920 if (mask) { 921 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 922 p += memcpy_roundup(p, mask, mask->sa_len); 923 } 924 925 if (ifa && ifp && ifp->sa_family == AF_LINK) { 926 rtmes.m_rtm.rtm_addrs |= RTA_IFP; 927 p += memcpy_roundup(p, ifp, ifp->sa_len); 928 rtmes.m_rtm.rtm_addrs |= RTA_IFA; 929 p += memcpy_roundup(p, ifa, ifa->sa_len); 930 } 931 932 rtmes.m_rtm.rtm_msglen = p - (char *)&rtmes; 933 934 wb = ID0write(s, &rtmes, rtmes.m_rtm.rtm_msglen); 935 if (wb < 0) { 936 ncprange_setsa(&ncpdst, dst, mask); 937 938 log_Printf(LogTCPIP, "rt_Update failure:\n"); 939 log_Printf(LogTCPIP, "rt_Update: Dst = %s\n", ncprange_ntoa(&ncpdst)); 940 941 if (rtmes.m_rtm.rtm_errno == 0) 942 log_Printf(LogWARN, "%s: Change route failed: errno: %s\n", 943 ncprange_ntoa(&ncpdst), strerror(errno)); 944 else 945 log_Printf(LogWARN, "%s: Change route failed: %s\n", 946 ncprange_ntoa(&ncpdst), strerror(rtmes.m_rtm.rtm_errno)); 947 } 948 close(s); 949} 950