1 /*- 2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 4 * Internet Initiative Japan, Inc (IIJ) 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/socket.h> 33 #include <net/if_types.h> 34 #include <net/route.h> 35 #include <net/if.h> 36 #include <netinet/in.h> 37 #include <arpa/inet.h> 38 #include <net/if_dl.h> 39 #include <netinet/in_systm.h> 40 #include <netinet/ip.h> 41 #include <sys/un.h> 42 43 #include <errno.h> 44 #include <stdarg.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <sys/sysctl.h> 49 #include <termios.h> 50 #include <unistd.h> 51 52 #include "layer.h" 53 #include "defs.h" 54 #include "command.h" 55 #include "mbuf.h" 56 #include "log.h" 57 #include "iplist.h" 58 #include "timer.h" 59 #include "throughput.h" 60 #include "lqr.h" 61 #include "hdlc.h" 62 #include "fsm.h" 63 #include "lcp.h" 64 #include "ccp.h" 65 #include "link.h" 66 #include "slcompress.h" 67 #include "ncpaddr.h" 68 #include "ipcp.h" 69 #include "filter.h" 70 #include "descriptor.h" 71 #include "mp.h" 72 #ifndef NORADIUS 73 #include "radius.h" 74 #endif 75 #include "ipv6cp.h" 76 #include "ncp.h" 77 #include "bundle.h" 78 #include "route.h" 79 #include "prompt.h" 80 #include "iface.h" 81 #include "id.h" 82 83 84 static void 85 p_sockaddr(struct prompt *prompt, struct sockaddr *phost, 86 struct sockaddr *pmask, int width) 87 { 88 struct ncprange range; 89 char buf[29]; 90 struct sockaddr_dl *dl = (struct sockaddr_dl *)phost; 91 92 if (log_IsKept(LogDEBUG)) { 93 char tmp[50]; 94 95 log_Printf(LogDEBUG, "Found the following sockaddr:\n"); 96 log_Printf(LogDEBUG, " Family %d, len %d\n", 97 (int)phost->sa_family, (int)phost->sa_len); 98 inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp); 99 log_Printf(LogDEBUG, " Addr %s\n", tmp); 100 if (pmask) { 101 inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp); 102 log_Printf(LogDEBUG, " Mask %s\n", tmp); 103 } 104 } 105 106 switch (phost->sa_family) { 107 case AF_INET: 108 #ifndef NOINET6 109 case AF_INET6: 110 #endif 111 ncprange_setsa(&range, phost, pmask); 112 if (ncprange_isdefault(&range)) 113 prompt_Printf(prompt, "%-*s ", width - 1, "default"); 114 else 115 prompt_Printf(prompt, "%-*s ", width - 1, ncprange_ntoa(&range)); 116 return; 117 118 case AF_LINK: 119 if (dl->sdl_nlen) 120 snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data); 121 else if (dl->sdl_alen) { 122 if (dl->sdl_type == IFT_ETHER) { 123 if (dl->sdl_alen < sizeof buf / 3) { 124 int f; 125 u_char *MAC; 126 127 MAC = (u_char *)dl->sdl_data + dl->sdl_nlen; 128 for (f = 0; f < dl->sdl_alen; f++) 129 sprintf(buf+f*3, "%02x:", MAC[f]); 130 buf[f*3-1] = '\0'; 131 } else 132 strcpy(buf, "??:??:??:??:??:??"); 133 } else 134 sprintf(buf, "<IFT type %d>", dl->sdl_type); 135 } else if (dl->sdl_slen) 136 sprintf(buf, "<slen %d?>", dl->sdl_slen); 137 else 138 sprintf(buf, "link#%d", dl->sdl_index); 139 break; 140 141 default: 142 sprintf(buf, "<AF type %d>", phost->sa_family); 143 break; 144 } 145 146 prompt_Printf(prompt, "%-*s ", width-1, buf); 147 } 148 149 static struct bits { 150 u_int32_t b_mask; 151 char b_val; 152 } bits[] = { 153 { RTF_UP, 'U' }, 154 { RTF_GATEWAY, 'G' }, 155 { RTF_HOST, 'H' }, 156 { RTF_REJECT, 'R' }, 157 { RTF_DYNAMIC, 'D' }, 158 { RTF_MODIFIED, 'M' }, 159 { RTF_DONE, 'd' }, 160 { RTF_CLONING, 'C' }, 161 { RTF_XRESOLVE, 'X' }, 162 { RTF_LLINFO, 'L' }, 163 { RTF_STATIC, 'S' }, 164 { RTF_PROTO1, '1' }, 165 { RTF_PROTO2, '2' }, 166 { RTF_BLACKHOLE, 'B' }, 167 #ifdef RTF_WASCLONED 168 { RTF_WASCLONED, 'W' }, 169 #endif 170 #ifdef RTF_PRCLONING 171 { RTF_PRCLONING, 'c' }, 172 #endif 173 #ifdef RTF_PROTO3 174 { RTF_PROTO3, '3' }, 175 #endif 176 #ifdef RTF_BROADCAST 177 { RTF_BROADCAST, 'b' }, 178 #endif 179 { 0, '\0' } 180 }; 181 182 #ifndef RTF_WASCLONED 183 #define RTF_WASCLONED (0) 184 #endif 185 186 static void 187 p_flags(struct prompt *prompt, u_int32_t f, unsigned max) 188 { 189 char name[33], *flags; 190 register struct bits *p = bits; 191 192 if (max > sizeof name - 1) 193 max = sizeof name - 1; 194 195 for (flags = name; p->b_mask && flags - name < (int)max; p++) 196 if (p->b_mask & f) 197 *flags++ = p->b_val; 198 *flags = '\0'; 199 prompt_Printf(prompt, "%-*.*s", (int)max, (int)max, name); 200 } 201 202 static int route_nifs = -1; 203 204 const char * 205 Index2Nam(int idx) 206 { 207 /* 208 * XXX: Maybe we should select() on the routing socket so that we can 209 * notice interfaces that come & go (PCCARD support). 210 * Or we could even support a signal that resets these so that 211 * the PCCARD insert/remove events can signal ppp. 212 */ 213 static char **ifs; /* Figure these out once */ 214 static int debug_done; /* Debug once */ 215 216 if (idx > route_nifs || (idx > 0 && ifs[idx-1] == NULL)) { 217 int mib[6], have, had; 218 size_t needed; 219 char *buf, *ptr, *end; 220 struct sockaddr_dl *dl; 221 struct if_msghdr *ifm; 222 223 if (ifs) { 224 free(ifs); 225 ifs = NULL; 226 route_nifs = 0; 227 } 228 debug_done = 0; 229 230 mib[0] = CTL_NET; 231 mib[1] = PF_ROUTE; 232 mib[2] = 0; 233 mib[3] = 0; 234 mib[4] = NET_RT_IFLIST; 235 mib[5] = 0; 236 237 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 238 log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n", 239 strerror(errno)); 240 return NumStr(idx, NULL, 0); 241 } 242 if ((buf = malloc(needed)) == NULL) 243 return NumStr(idx, NULL, 0); 244 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 245 free(buf); 246 return NumStr(idx, NULL, 0); 247 } 248 end = buf + needed; 249 250 have = 0; 251 for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) { 252 ifm = (struct if_msghdr *)ptr; 253 if (ifm->ifm_type != RTM_IFINFO) 254 continue; 255 dl = (struct sockaddr_dl *)(ifm + 1); 256 if (ifm->ifm_index > 0) { 257 if (ifm->ifm_index > have) { 258 char **newifs; 259 260 had = have; 261 have = ifm->ifm_index + 5; 262 if (had) 263 newifs = (char **)realloc(ifs, sizeof(char *) * have); 264 else 265 newifs = (char **)malloc(sizeof(char *) * have); 266 if (!newifs) { 267 log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno)); 268 route_nifs = 0; 269 if (ifs) { 270 free(ifs); 271 ifs = NULL; 272 } 273 free(buf); 274 return NumStr(idx, NULL, 0); 275 } 276 ifs = newifs; 277 memset(ifs + had, '\0', sizeof(char *) * (have - had)); 278 } 279 if (ifs[ifm->ifm_index-1] == NULL) { 280 ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1); 281 if (ifs[ifm->ifm_index-1] == NULL) 282 log_Printf(LogDEBUG, "Skipping interface %d: Out of memory\n", 283 ifm->ifm_index); 284 else { 285 memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen); 286 ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0'; 287 if (route_nifs < ifm->ifm_index) 288 route_nifs = ifm->ifm_index; 289 } 290 } 291 } else if (log_IsKept(LogDEBUG)) 292 log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n", 293 ifm->ifm_index); 294 } 295 free(buf); 296 } 297 298 if (log_IsKept(LogDEBUG) && !debug_done) { 299 int f; 300 301 log_Printf(LogDEBUG, "Found the following interfaces:\n"); 302 for (f = 0; f < route_nifs; f++) 303 if (ifs[f] != NULL) 304 log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]); 305 debug_done = 1; 306 } 307 308 if (idx < 1 || idx > route_nifs || ifs[idx-1] == NULL) 309 return NumStr(idx, NULL, 0); 310 311 return ifs[idx-1]; 312 } 313 314 void 315 route_ParseHdr(struct rt_msghdr *rtm, struct sockaddr *sa[RTAX_MAX]) 316 { 317 char *wp; 318 int rtax; 319 320 wp = (char *)(rtm + 1); 321 322 for (rtax = 0; rtax < RTAX_MAX; rtax++) 323 if (rtm->rtm_addrs & (1 << rtax)) { 324 sa[rtax] = (struct sockaddr *)wp; 325 wp += ROUNDUP(sa[rtax]->sa_len); 326 if (sa[rtax]->sa_family == 0) 327 sa[rtax] = NULL; /* ??? */ 328 } else 329 sa[rtax] = NULL; 330 } 331 332 int 333 route_Show(struct cmdargs const *arg) 334 { 335 struct rt_msghdr *rtm; 336 struct sockaddr *sa[RTAX_MAX]; 337 char *sp, *ep, *cp; 338 size_t needed; 339 int mib[6]; 340 341 mib[0] = CTL_NET; 342 mib[1] = PF_ROUTE; 343 mib[2] = 0; 344 mib[3] = 0; 345 mib[4] = NET_RT_DUMP; 346 mib[5] = 0; 347 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 348 log_Printf(LogERROR, "route_Show: sysctl: estimate: %s\n", strerror(errno)); 349 return (1); 350 } 351 sp = malloc(needed); 352 if (sp == NULL) 353 return (1); 354 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 355 log_Printf(LogERROR, "route_Show: sysctl: getroute: %s\n", strerror(errno)); 356 free(sp); 357 return (1); 358 } 359 ep = sp + needed; 360 361 prompt_Printf(arg->prompt, "%-20s%-20sFlags Netif\n", 362 "Destination", "Gateway"); 363 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 364 rtm = (struct rt_msghdr *)cp; 365 366 route_ParseHdr(rtm, sa); 367 368 if (sa[RTAX_DST] && sa[RTAX_GATEWAY]) { 369 p_sockaddr(arg->prompt, sa[RTAX_DST], sa[RTAX_NETMASK], 20); 370 p_sockaddr(arg->prompt, sa[RTAX_GATEWAY], NULL, 20); 371 372 p_flags(arg->prompt, rtm->rtm_flags, 6); 373 prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index)); 374 } else 375 prompt_Printf(arg->prompt, "<can't parse routing entry>\n"); 376 } 377 free(sp); 378 return 0; 379 } 380 381 /* 382 * Delete routes associated with our interface 383 */ 384 void 385 route_IfDelete(struct bundle *bundle, int all) 386 { 387 struct rt_msghdr *rtm; 388 struct sockaddr *sa[RTAX_MAX]; 389 struct ncprange range; 390 int pass; 391 size_t needed; 392 char *sp, *cp, *ep; 393 int mib[6]; 394 395 log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index); 396 397 mib[0] = CTL_NET; 398 mib[1] = PF_ROUTE; 399 mib[2] = 0; 400 mib[3] = 0; 401 mib[4] = NET_RT_DUMP; 402 mib[5] = 0; 403 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 404 log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 405 strerror(errno)); 406 return; 407 } 408 409 sp = malloc(needed); 410 if (sp == NULL) 411 return; 412 413 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 414 log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 415 strerror(errno)); 416 free(sp); 417 return; 418 } 419 ep = sp + needed; 420 421 for (pass = 0; pass < 2; pass++) { 422 /* 423 * We do 2 passes. The first deletes all cloned routes. The second 424 * deletes all non-cloned routes. This is done to avoid 425 * potential errors from trying to delete route X after route Y where 426 * route X was cloned from route Y (and is no longer there 'cos it 427 * may have gone with route Y). 428 */ 429 if (RTF_WASCLONED == 0 && pass == 0) 430 /* So we can't tell ! */ 431 continue; 432 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 433 rtm = (struct rt_msghdr *)cp; 434 route_ParseHdr(rtm, sa); 435 if (rtm->rtm_index == bundle->iface->index && 436 sa[RTAX_DST] && sa[RTAX_GATEWAY] && 437 (sa[RTAX_DST]->sa_family == AF_INET 438 #ifndef NOINET6 439 || sa[RTAX_DST]->sa_family == AF_INET6 440 #endif 441 ) && 442 (all || (rtm->rtm_flags & RTF_GATEWAY))) { 443 if (log_IsKept(LogDEBUG)) { 444 char gwstr[41]; 445 struct ncpaddr gw; 446 ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]); 447 ncpaddr_setsa(&gw, sa[RTAX_GATEWAY]); 448 snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(&gw)); 449 log_Printf(LogDEBUG, "Found %s %s\n", ncprange_ntoa(&range), gwstr); 450 } 451 if (sa[RTAX_GATEWAY]->sa_family == AF_INET || 452 #ifndef NOINET6 453 sa[RTAX_GATEWAY]->sa_family == AF_INET6 || 454 #endif 455 sa[RTAX_GATEWAY]->sa_family == AF_LINK) { 456 if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) || 457 (pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) { 458 ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]); 459 rt_Set(bundle, RTM_DELETE, &range, NULL, 0, 0); 460 } else 461 log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass); 462 } else 463 log_Printf(LogDEBUG, 464 "route_IfDelete: Can't remove routes for family %d\n", 465 sa[RTAX_GATEWAY]->sa_family); 466 } 467 } 468 } 469 free(sp); 470 } 471 472 473 /* 474 * Update the MTU on all routes for the given interface 475 */ 476 void 477 route_UpdateMTU(struct bundle *bundle) 478 { 479 struct rt_msghdr *rtm; 480 struct sockaddr *sa[RTAX_MAX]; 481 struct ncprange dst; 482 size_t needed; 483 char *sp, *cp, *ep; 484 int mib[6]; 485 486 log_Printf(LogDEBUG, "route_UpdateMTU (%d)\n", bundle->iface->index); 487 488 mib[0] = CTL_NET; 489 mib[1] = PF_ROUTE; 490 mib[2] = 0; 491 mib[3] = 0; 492 mib[4] = NET_RT_DUMP; 493 mib[5] = 0; 494 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 495 log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 496 strerror(errno)); 497 return; 498 } 499 500 sp = malloc(needed); 501 if (sp == NULL) 502 return; 503 504 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 505 log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 506 strerror(errno)); 507 free(sp); 508 return; 509 } 510 ep = sp + needed; 511 512 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 513 rtm = (struct rt_msghdr *)cp; 514 route_ParseHdr(rtm, sa); 515 if (sa[RTAX_DST] && (sa[RTAX_DST]->sa_family == AF_INET 516 #ifndef NOINET6 517 || sa[RTAX_DST]->sa_family == AF_INET6 518 #endif 519 ) && 520 sa[RTAX_GATEWAY] && rtm->rtm_index == bundle->iface->index) { 521 if (log_IsKept(LogTCPIP)) { 522 ncprange_setsa(&dst, sa[RTAX_DST], sa[RTAX_NETMASK]); 523 log_Printf(LogTCPIP, "route_UpdateMTU: Netif: %d (%s), dst %s," 524 " mtu %lu\n", rtm->rtm_index, Index2Nam(rtm->rtm_index), 525 ncprange_ntoa(&dst), bundle->iface->mtu); 526 } 527 rt_Update(bundle, sa[RTAX_DST], sa[RTAX_GATEWAY], sa[RTAX_NETMASK]); 528 } 529 } 530 531 free(sp); 532 } 533 534 int 535 GetIfIndex(char *name) 536 { 537 int idx; 538 539 idx = 1; 540 while (route_nifs == -1 || idx < route_nifs) 541 if (strcmp(Index2Nam(idx), name) == 0) 542 return idx; 543 else 544 idx++; 545 return -1; 546 } 547 548 void 549 route_Change(struct bundle *bundle, struct sticky_route *r, 550 const struct ncpaddr *me, const struct ncpaddr *peer) 551 { 552 struct ncpaddr dst; 553 554 for (; r; r = r->next) { 555 ncprange_getaddr(&r->dst, &dst); 556 if (ncpaddr_family(me) == AF_INET) { 557 if ((r->type & ROUTE_DSTMYADDR) && !ncpaddr_equal(&dst, me)) { 558 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 559 ncprange_sethost(&r->dst, me); 560 if (r->type & ROUTE_GWHISADDR) 561 ncpaddr_copy(&r->gw, peer); 562 } else if ((r->type & ROUTE_DSTHISADDR) && !ncpaddr_equal(&dst, peer)) { 563 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 564 ncprange_sethost(&r->dst, peer); 565 if (r->type & ROUTE_GWHISADDR) 566 ncpaddr_copy(&r->gw, peer); 567 } else if ((r->type & ROUTE_DSTDNS0) && !ncpaddr_equal(&dst, peer)) { 568 if (bundle->ncp.ipcp.ns.dns[0].s_addr == INADDR_NONE) 569 continue; 570 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 571 if (r->type & ROUTE_GWHISADDR) 572 ncpaddr_copy(&r->gw, peer); 573 } else if ((r->type & ROUTE_DSTDNS1) && !ncpaddr_equal(&dst, peer)) { 574 if (bundle->ncp.ipcp.ns.dns[1].s_addr == INADDR_NONE) 575 continue; 576 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 577 if (r->type & ROUTE_GWHISADDR) 578 ncpaddr_copy(&r->gw, peer); 579 } else if ((r->type & ROUTE_GWHISADDR) && !ncpaddr_equal(&r->gw, peer)) 580 ncpaddr_copy(&r->gw, peer); 581 #ifndef NOINET6 582 } else if (ncpaddr_family(me) == AF_INET6) { 583 if ((r->type & ROUTE_DSTMYADDR6) && !ncpaddr_equal(&dst, me)) { 584 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 585 ncprange_sethost(&r->dst, me); 586 if (r->type & ROUTE_GWHISADDR) 587 ncpaddr_copy(&r->gw, peer); 588 } else if ((r->type & ROUTE_DSTHISADDR6) && !ncpaddr_equal(&dst, peer)) { 589 rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0); 590 ncprange_sethost(&r->dst, peer); 591 if (r->type & ROUTE_GWHISADDR) 592 ncpaddr_copy(&r->gw, peer); 593 } else if ((r->type & ROUTE_GWHISADDR6) && !ncpaddr_equal(&r->gw, peer)) 594 ncpaddr_copy(&r->gw, peer); 595 #endif 596 } 597 rt_Set(bundle, RTM_ADD, &r->dst, &r->gw, 1, 0); 598 } 599 } 600 601 void 602 route_Add(struct sticky_route **rp, int type, const struct ncprange *dst, 603 const struct ncpaddr *gw) 604 { 605 struct sticky_route *r; 606 int dsttype = type & ROUTE_DSTANY; 607 608 r = NULL; 609 while (*rp) { 610 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 611 (!dsttype && ncprange_equal(&(*rp)->dst, dst))) { 612 /* Oops, we already have this route - unlink it */ 613 free(r); /* impossible really */ 614 r = *rp; 615 *rp = r->next; 616 } else 617 rp = &(*rp)->next; 618 } 619 620 if (r == NULL) { 621 r = (struct sticky_route *)malloc(sizeof(struct sticky_route)); 622 if (r == NULL) { 623 log_Printf(LogERROR, "route_Add: Out of memory!\n"); 624 return; 625 } 626 } 627 r->type = type; 628 r->next = NULL; 629 ncprange_copy(&r->dst, dst); 630 ncpaddr_copy(&r->gw, gw); 631 *rp = r; 632 } 633 634 void 635 route_Delete(struct sticky_route **rp, int type, const struct ncprange *dst) 636 { 637 struct sticky_route *r; 638 int dsttype = type & ROUTE_DSTANY; 639 640 for (; *rp; rp = &(*rp)->next) { 641 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 642 (!dsttype && ncprange_equal(dst, &(*rp)->dst))) { 643 r = *rp; 644 *rp = r->next; 645 free(r); 646 break; 647 } 648 } 649 } 650 651 void 652 route_DeleteAll(struct sticky_route **rp) 653 { 654 struct sticky_route *r, *rn; 655 656 for (r = *rp; r; r = rn) { 657 rn = r->next; 658 free(r); 659 } 660 *rp = NULL; 661 } 662 663 void 664 route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag, 665 int indent) 666 { 667 int tlen = strlen(tag); 668 669 if (tlen + 2 > indent) 670 prompt_Printf(p, "%s:\n%*s", tag, indent, ""); 671 else 672 prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, ""); 673 674 for (; r; r = r->next) { 675 prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, ""); 676 tlen = 0; 677 if (r->type & ROUTE_DSTMYADDR) 678 prompt_Printf(p, "MYADDR"); 679 else if (r->type & ROUTE_DSTMYADDR6) 680 prompt_Printf(p, "MYADDR6"); 681 else if (r->type & ROUTE_DSTHISADDR) 682 prompt_Printf(p, "HISADDR"); 683 else if (r->type & ROUTE_DSTHISADDR6) 684 prompt_Printf(p, "HISADDR6"); 685 else if (r->type & ROUTE_DSTDNS0) 686 prompt_Printf(p, "DNS0"); 687 else if (r->type & ROUTE_DSTDNS1) 688 prompt_Printf(p, "DNS1"); 689 else if (ncprange_isdefault(&r->dst)) 690 prompt_Printf(p, "default"); 691 else 692 prompt_Printf(p, "%s", ncprange_ntoa(&r->dst)); 693 694 if (r->type & ROUTE_GWHISADDR) 695 prompt_Printf(p, " HISADDR\n"); 696 else if (r->type & ROUTE_GWHISADDR6) 697 prompt_Printf(p, " HISADDR6\n"); 698 else 699 prompt_Printf(p, " %s\n", ncpaddr_ntoa(&r->gw)); 700 } 701 } 702 703 struct rtmsg { 704 struct rt_msghdr m_rtm; 705 char m_space[256]; 706 }; 707 708 static size_t 709 memcpy_roundup(char *cp, const void *data, size_t len) 710 { 711 size_t padlen; 712 713 padlen = ROUNDUP(len); 714 memcpy(cp, data, len); 715 if (padlen > len) 716 memset(cp + len, '\0', padlen - len); 717 718 return padlen; 719 } 720 721 #if defined(__KAME__) && !defined(NOINET6) 722 static void 723 add_scope(struct sockaddr *sa, int ifindex) 724 { 725 struct sockaddr_in6 *sa6; 726 727 if (sa->sa_family != AF_INET6) 728 return; 729 sa6 = (struct sockaddr_in6 *)sa; 730 if (!IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) && 731 !IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) 732 return; 733 if (*(u_int16_t *)&sa6->sin6_addr.s6_addr[2] != 0) 734 return; 735 *(u_int16_t *)&sa6->sin6_addr.s6_addr[2] = htons(ifindex); 736 } 737 #endif 738 739 int 740 rt_Set(struct bundle *bundle, int cmd, const struct ncprange *dst, 741 const struct ncpaddr *gw, int bang, int quiet) 742 { 743 struct rtmsg rtmes; 744 int s, nb, wb; 745 char *cp; 746 const char *cmdstr; 747 struct sockaddr_storage sadst, samask, sagw; 748 int result = 1; 749 750 if (bang) 751 cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 752 else 753 cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 754 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 755 if (s < 0) { 756 log_Printf(LogERROR, "rt_Set: socket(): %s\n", strerror(errno)); 757 return result; 758 } 759 memset(&rtmes, '\0', sizeof rtmes); 760 rtmes.m_rtm.rtm_version = RTM_VERSION; 761 rtmes.m_rtm.rtm_type = cmd; 762 rtmes.m_rtm.rtm_addrs = RTA_DST; 763 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 764 rtmes.m_rtm.rtm_pid = getpid(); 765 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 766 767 if (cmd == RTM_ADD) { 768 if (bundle->ncp.cfg.sendpipe > 0) { 769 rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe; 770 rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 771 } 772 if (bundle->ncp.cfg.recvpipe > 0) { 773 rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe; 774 rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 775 } 776 } 777 778 ncprange_getsa(dst, &sadst, &samask); 779 #if defined(__KAME__) && !defined(NOINET6) 780 add_scope((struct sockaddr *)&sadst, bundle->iface->index); 781 #endif 782 783 cp = rtmes.m_space; 784 cp += memcpy_roundup(cp, &sadst, sadst.ss_len); 785 if (cmd == RTM_ADD) { 786 if (gw == NULL) { 787 log_Printf(LogERROR, "rt_Set: Program error\n"); 788 close(s); 789 return result; 790 } 791 ncpaddr_getsa(gw, &sagw); 792 #if defined(__KAME__) && !defined(NOINET6) 793 add_scope((struct sockaddr *)&sagw, bundle->iface->index); 794 #endif 795 if (ncpaddr_isdefault(gw)) { 796 if (!quiet) 797 log_Printf(LogERROR, "rt_Set: Cannot add a route with" 798 " gateway 0.0.0.0\n"); 799 close(s); 800 return result; 801 } else { 802 cp += memcpy_roundup(cp, &sagw, sagw.ss_len); 803 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 804 } 805 } 806 807 if (!ncprange_ishost(dst)) { 808 cp += memcpy_roundup(cp, &samask, samask.ss_len); 809 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 810 } 811 812 nb = cp - (char *)&rtmes; 813 rtmes.m_rtm.rtm_msglen = nb; 814 wb = ID0write(s, &rtmes, nb); 815 if (wb < 0) { 816 log_Printf(LogTCPIP, "rt_Set failure:\n"); 817 log_Printf(LogTCPIP, "rt_Set: Cmd = %s\n", cmdstr); 818 log_Printf(LogTCPIP, "rt_Set: Dst = %s\n", ncprange_ntoa(dst)); 819 if (gw != NULL) 820 log_Printf(LogTCPIP, "rt_Set: Gateway = %s\n", ncpaddr_ntoa(gw)); 821 failed: 822 if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 823 (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 824 if (!bang) { 825 log_Printf(LogWARN, "Add route failed: %s already exists\n", 826 ncprange_ntoa(dst)); 827 result = 0; /* Don't add to our dynamic list */ 828 } else { 829 rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 830 if ((wb = ID0write(s, &rtmes, nb)) < 0) 831 goto failed; 832 } 833 } else if (cmd == RTM_DELETE && 834 (rtmes.m_rtm.rtm_errno == ESRCH || 835 (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 836 if (!bang) 837 log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 838 ncprange_ntoa(dst)); 839 } else if (rtmes.m_rtm.rtm_errno == 0) { 840 if (!quiet || errno != ENETUNREACH) 841 log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 842 ncprange_ntoa(dst), strerror(errno)); 843 } else 844 log_Printf(LogWARN, "%s route failed: %s: %s\n", 845 cmdstr, ncprange_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 846 } 847 848 if (log_IsKept(LogDEBUG)) { 849 char gwstr[40]; 850 851 if (gw) 852 snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(gw)); 853 else 854 snprintf(gwstr, sizeof gwstr, "<none>"); 855 log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %s, gateway = %s\n", 856 wb, cmdstr, ncprange_ntoa(dst), gwstr); 857 } 858 close(s); 859 860 return result; 861 } 862 863 void 864 rt_Update(struct bundle *bundle, const struct sockaddr *dst, 865 const struct sockaddr *gw, const struct sockaddr *mask) 866 { 867 struct ncprange ncpdst; 868 struct rtmsg rtmes; 869 char *p; 870 int s, wb; 871 872 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 873 if (s < 0) { 874 log_Printf(LogERROR, "rt_Update: socket(): %s\n", strerror(errno)); 875 return; 876 } 877 878 memset(&rtmes, '\0', sizeof rtmes); 879 rtmes.m_rtm.rtm_version = RTM_VERSION; 880 rtmes.m_rtm.rtm_type = RTM_CHANGE; 881 rtmes.m_rtm.rtm_addrs = 0; 882 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 883 rtmes.m_rtm.rtm_pid = getpid(); 884 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_STATIC; 885 886 if (bundle->ncp.cfg.sendpipe > 0) { 887 rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe; 888 rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 889 } 890 891 if (bundle->ncp.cfg.recvpipe > 0) { 892 rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe; 893 rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 894 } 895 896 rtmes.m_rtm.rtm_rmx.rmx_mtu = bundle->iface->mtu; 897 rtmes.m_rtm.rtm_inits |= RTV_MTU; 898 p = rtmes.m_space; 899 900 if (dst) { 901 rtmes.m_rtm.rtm_addrs |= RTA_DST; 902 p += memcpy_roundup(p, dst, dst->sa_len); 903 } 904 905 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 906 p += memcpy_roundup(p, gw, gw->sa_len); 907 if (mask) { 908 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 909 p += memcpy_roundup(p, mask, mask->sa_len); 910 } 911 912 rtmes.m_rtm.rtm_msglen = p - (char *)&rtmes; 913 914 wb = ID0write(s, &rtmes, rtmes.m_rtm.rtm_msglen); 915 if (wb < 0) { 916 ncprange_setsa(&ncpdst, dst, mask); 917 918 log_Printf(LogTCPIP, "rt_Update failure:\n"); 919 log_Printf(LogTCPIP, "rt_Update: Dst = %s\n", ncprange_ntoa(&ncpdst)); 920 921 if (rtmes.m_rtm.rtm_errno == 0) 922 log_Printf(LogWARN, "%s: Change route failed: errno: %s\n", 923 ncprange_ntoa(&ncpdst), strerror(errno)); 924 else 925 log_Printf(LogWARN, "%s: Change route failed: %s\n", 926 ncprange_ntoa(&ncpdst), strerror(rtmes.m_rtm.rtm_errno)); 927 } 928 close(s); 929 } 930