route.c revision 43313
1/* 2 * PPP Routing related Module 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * $Id: route.c,v 1.54 1998/10/22 02:32:50 brian Exp $ 21 * 22 */ 23 24#include <sys/param.h> 25#include <sys/socket.h> 26#include <net/if_types.h> 27#include <net/route.h> 28#include <net/if.h> 29#include <netinet/in.h> 30#include <arpa/inet.h> 31#include <net/if_dl.h> 32#include <netinet/in_systm.h> 33#include <netinet/ip.h> 34#include <sys/un.h> 35 36#include <errno.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <sys/sysctl.h> 41#include <termios.h> 42 43#include "defs.h" 44#include "command.h" 45#include "mbuf.h" 46#include "log.h" 47#include "iplist.h" 48#include "timer.h" 49#include "throughput.h" 50#include "lqr.h" 51#include "hdlc.h" 52#include "fsm.h" 53#include "lcp.h" 54#include "ccp.h" 55#include "link.h" 56#include "slcompress.h" 57#include "ipcp.h" 58#include "filter.h" 59#include "descriptor.h" 60#include "mp.h" 61#ifndef NORADIUS 62#include "radius.h" 63#endif 64#include "bundle.h" 65#include "route.h" 66#include "prompt.h" 67#include "iface.h" 68 69static void 70p_sockaddr(struct prompt *prompt, struct sockaddr *phost, 71 struct sockaddr *pmask, int width) 72{ 73 char buf[29]; 74 struct sockaddr_in *ihost = (struct sockaddr_in *)phost; 75 struct sockaddr_in *mask = (struct sockaddr_in *)pmask; 76 struct sockaddr_dl *dl = (struct sockaddr_dl *)phost; 77 78 switch (phost->sa_family) { 79 case AF_INET: 80 if (!phost) 81 buf[0] = '\0'; 82 else if (ihost->sin_addr.s_addr == INADDR_ANY) 83 strcpy(buf, "default"); 84 else if (!mask) 85 strcpy(buf, inet_ntoa(ihost->sin_addr)); 86 else { 87 u_int32_t msk = ntohl(mask->sin_addr.s_addr); 88 u_int32_t tst; 89 int bits; 90 int len; 91 struct sockaddr_in net; 92 93 for (tst = 1, bits=32; tst; tst <<= 1, bits--) 94 if (msk & tst) 95 break; 96 97 for (tst <<= 1; tst; tst <<= 1) 98 if (!(msk & tst)) 99 break; 100 101 net.sin_addr.s_addr = ihost->sin_addr.s_addr & mask->sin_addr.s_addr; 102 strcpy(buf, inet_ntoa(net.sin_addr)); 103 for (len = strlen(buf); len > 3; buf[len -= 2] = '\0') 104 if (strcmp(buf + len - 2, ".0")) 105 break; 106 107 if (tst) /* non-contiguous :-( */ 108 sprintf(buf + strlen(buf),"&0x%08lx", (u_long)msk); 109 else 110 sprintf(buf + strlen(buf), "/%d", bits); 111 } 112 break; 113 114 case AF_LINK: 115 if (dl->sdl_nlen) 116 snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data); 117 else if (dl->sdl_alen) { 118 if (dl->sdl_type == IFT_ETHER) { 119 if (dl->sdl_alen < sizeof buf / 3) { 120 int f; 121 u_char *MAC; 122 123 MAC = (u_char *)dl->sdl_data + dl->sdl_nlen; 124 for (f = 0; f < dl->sdl_alen; f++) 125 sprintf(buf+f*3, "%02x:", MAC[f]); 126 buf[f*3-1] = '\0'; 127 } else 128 strcpy(buf, "??:??:??:??:??:??"); 129 } else 130 sprintf(buf, "<IFT type %d>", dl->sdl_type); 131 } else if (dl->sdl_slen) 132 sprintf(buf, "<slen %d?>", dl->sdl_slen); 133 else 134 sprintf(buf, "link#%d", dl->sdl_index); 135 break; 136 137 default: 138 sprintf(buf, "<AF type %d>", phost->sa_family); 139 break; 140 } 141 142 prompt_Printf(prompt, "%-*s ", width-1, buf); 143} 144 145static struct bits { 146 u_int32_t b_mask; 147 char b_val; 148} bits[] = { 149 { RTF_UP, 'U' }, 150 { RTF_GATEWAY, 'G' }, 151 { RTF_HOST, 'H' }, 152 { RTF_REJECT, 'R' }, 153 { RTF_DYNAMIC, 'D' }, 154 { RTF_MODIFIED, 'M' }, 155 { RTF_DONE, 'd' }, 156 { RTF_CLONING, 'C' }, 157 { RTF_XRESOLVE, 'X' }, 158 { RTF_LLINFO, 'L' }, 159 { RTF_STATIC, 'S' }, 160 { RTF_PROTO1, '1' }, 161 { RTF_PROTO2, '2' }, 162 { RTF_BLACKHOLE, 'B' }, 163#ifdef RTF_WASCLONED 164 { RTF_WASCLONED, 'W' }, 165#endif 166#ifdef RTF_PRCLONING 167 { RTF_PRCLONING, 'c' }, 168#endif 169#ifdef RTF_PROTO3 170 { RTF_PROTO3, '3' }, 171#endif 172#ifdef RTF_BROADCAST 173 { RTF_BROADCAST, 'b' }, 174#endif 175 { 0, '\0' } 176}; 177 178#ifndef RTF_WASCLONED 179#define RTF_WASCLONED (0) 180#endif 181 182static void 183p_flags(struct prompt *prompt, u_int32_t f, int max) 184{ 185 char name[33], *flags; 186 register struct bits *p = bits; 187 188 if (max > sizeof name - 1) 189 max = sizeof name - 1; 190 191 for (flags = name; p->b_mask && flags - name < max; p++) 192 if (p->b_mask & f) 193 *flags++ = p->b_val; 194 *flags = '\0'; 195 prompt_Printf(prompt, "%-*.*s", max, max, name); 196} 197 198const char * 199Index2Nam(int idx) 200{ 201 /* 202 * XXX: Maybe we should select() on the routing socket so that we can 203 * notice interfaces that come & go (PCCARD support). 204 * Or we could even support a signal that resets these so that 205 * the PCCARD insert/remove events can signal ppp. 206 */ 207 static char **ifs; /* Figure these out once */ 208 static int nifs, debug_done; /* Figure out how many once, and debug once */ 209 210 if (!nifs) { 211 int mib[6], have, had; 212 size_t needed; 213 char *buf, *ptr, *end; 214 struct sockaddr_dl *dl; 215 struct if_msghdr *ifm; 216 217 mib[0] = CTL_NET; 218 mib[1] = PF_ROUTE; 219 mib[2] = 0; 220 mib[3] = 0; 221 mib[4] = NET_RT_IFLIST; 222 mib[5] = 0; 223 224 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 225 log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n", 226 strerror(errno)); 227 return "???"; 228 } 229 if ((buf = malloc(needed)) == NULL) 230 return "???"; 231 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 232 free(buf); 233 return "???"; 234 } 235 end = buf + needed; 236 237 have = 0; 238 for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) { 239 ifm = (struct if_msghdr *)ptr; 240 dl = (struct sockaddr_dl *)(ifm + 1); 241 if (ifm->ifm_index > 0) { 242 if (ifm->ifm_index > have) { 243 char **newifs; 244 245 had = have; 246 have = ifm->ifm_index + 5; 247 if (had) 248 newifs = (char **)realloc(ifs, sizeof(char *) * have); 249 else 250 newifs = (char **)malloc(sizeof(char *) * have); 251 if (!newifs) { 252 log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno)); 253 nifs = 0; 254 if (ifs) 255 free(ifs); 256 return "???"; 257 } 258 ifs = newifs; 259 memset(ifs + had, '\0', sizeof(char *) * (have - had)); 260 } 261 if (ifs[ifm->ifm_index-1] == NULL) { 262 ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1); 263 memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen); 264 ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0'; 265 if (nifs < ifm->ifm_index) 266 nifs = ifm->ifm_index; 267 } 268 } else if (log_IsKept(LogDEBUG)) 269 log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n", 270 ifm->ifm_index); 271 } 272 free(buf); 273 } 274 275 if (log_IsKept(LogDEBUG) && !debug_done) { 276 int f; 277 278 log_Printf(LogDEBUG, "Found the following interfaces:\n"); 279 for (f = 0; f < nifs; f++) 280 if (ifs[f] != NULL) 281 log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]); 282 debug_done = 1; 283 } 284 285 if (idx < 1 || idx > nifs || ifs[idx-1] == NULL) 286 return "???"; 287 288 return ifs[idx-1]; 289} 290 291int 292route_Show(struct cmdargs const *arg) 293{ 294 struct rt_msghdr *rtm; 295 struct sockaddr *sa_dst, *sa_gw, *sa_mask; 296 char *sp, *ep, *cp, *wp; 297 size_t needed; 298 int mib[6]; 299 300 mib[0] = CTL_NET; 301 mib[1] = PF_ROUTE; 302 mib[2] = 0; 303 mib[3] = 0; 304 mib[4] = NET_RT_DUMP; 305 mib[5] = 0; 306 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 307 log_Printf(LogERROR, "route_Show: sysctl: estimate: %s\n", strerror(errno)); 308 return (1); 309 } 310 sp = malloc(needed); 311 if (sp == NULL) 312 return (1); 313 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 314 log_Printf(LogERROR, "route_Show: sysctl: getroute: %s\n", strerror(errno)); 315 free(sp); 316 return (1); 317 } 318 ep = sp + needed; 319 320 prompt_Printf(arg->prompt, "%-20s%-20sFlags Netif\n", 321 "Destination", "Gateway"); 322 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 323 rtm = (struct rt_msghdr *) cp; 324 wp = (char *)(rtm+1); 325 326 if (rtm->rtm_addrs & RTA_DST) { 327 sa_dst = (struct sockaddr *)wp; 328 wp += sa_dst->sa_len; 329 } else 330 sa_dst = NULL; 331 332 if (rtm->rtm_addrs & RTA_GATEWAY) { 333 sa_gw = (struct sockaddr *)wp; 334 wp += sa_gw->sa_len; 335 } else 336 sa_gw = NULL; 337 338 if (rtm->rtm_addrs & RTA_NETMASK) { 339 sa_mask = (struct sockaddr *)wp; 340 wp += sa_mask->sa_len; 341 } else 342 sa_mask = NULL; 343 344 p_sockaddr(arg->prompt, sa_dst, sa_mask, 20); 345 p_sockaddr(arg->prompt, sa_gw, NULL, 20); 346 347 p_flags(arg->prompt, rtm->rtm_flags, 6); 348 prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index)); 349 } 350 free(sp); 351 return 0; 352} 353 354/* 355 * Delete routes associated with our interface 356 */ 357void 358route_IfDelete(struct bundle *bundle, int all) 359{ 360 struct rt_msghdr *rtm; 361 struct sockaddr *sa; 362 struct in_addr sa_dst, sa_none; 363 int pass; 364 size_t needed; 365 char *sp, *cp, *ep; 366 int mib[6]; 367 368 log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index); 369 sa_none.s_addr = INADDR_ANY; 370 371 mib[0] = CTL_NET; 372 mib[1] = PF_ROUTE; 373 mib[2] = 0; 374 mib[3] = 0; 375 mib[4] = NET_RT_DUMP; 376 mib[5] = 0; 377 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 378 log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n", 379 strerror(errno)); 380 return; 381 } 382 383 sp = malloc(needed); 384 if (sp == NULL) 385 return; 386 387 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 388 log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n", 389 strerror(errno)); 390 free(sp); 391 return; 392 } 393 ep = sp + needed; 394 395 for (pass = 0; pass < 2; pass++) { 396 /* 397 * We do 2 passes. The first deletes all cloned routes. The second 398 * deletes all non-cloned routes. This is necessary to avoid 399 * potential errors from trying to delete route X after route Y where 400 * route X was cloned from route Y (and is no longer there 'cos it 401 * may have gone with route Y). 402 */ 403 if (RTF_WASCLONED == 0 && pass == 0) 404 /* So we can't tell ! */ 405 continue; 406 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 407 rtm = (struct rt_msghdr *) cp; 408 sa = (struct sockaddr *) (rtm + 1); 409 log_Printf(LogDEBUG, "route_IfDelete: addrs: %x, Netif: %d (%s)," 410 " flags: %x, dst: %s ?\n", rtm->rtm_addrs, rtm->rtm_index, 411 Index2Nam(rtm->rtm_index), rtm->rtm_flags, 412 inet_ntoa(((struct sockaddr_in *) sa)->sin_addr)); 413 if (rtm->rtm_addrs & RTA_DST && rtm->rtm_addrs & RTA_GATEWAY && 414 rtm->rtm_index == bundle->iface->index && 415 (all || (rtm->rtm_flags & RTF_GATEWAY))) { 416 sa_dst.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 417 sa = (struct sockaddr *)((char *)sa + sa->sa_len); 418 if (sa->sa_family == AF_INET || sa->sa_family == AF_LINK) { 419 if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) || 420 (pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) { 421 log_Printf(LogDEBUG, "route_IfDelete: Remove it (pass %d)\n", pass); 422 bundle_SetRoute(bundle, RTM_DELETE, sa_dst, sa_none, sa_none, 0, 0); 423 } else 424 log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass); 425 } else 426 log_Printf(LogDEBUG, 427 "route_IfDelete: Can't remove routes of %d family !\n", 428 sa->sa_family); 429 } 430 } 431 } 432 free(sp); 433} 434 435int 436GetIfIndex(char *name) 437{ 438 int idx; 439 const char *got; 440 441 idx = 1; 442 while (strcmp(got = Index2Nam(idx), "???")) 443 if (!strcmp(got, name)) 444 return idx; 445 else 446 idx++; 447 return -1; 448} 449 450void 451route_Change(struct bundle *bundle, struct sticky_route *r, 452 struct in_addr me, struct in_addr peer) 453{ 454 struct in_addr none, del; 455 456 none.s_addr = INADDR_ANY; 457 for (; r; r = r->next) { 458 if ((r->type & ROUTE_DSTMYADDR) && r->dst.s_addr != me.s_addr) { 459 del.s_addr = r->dst.s_addr & r->mask.s_addr; 460 bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1, 0); 461 r->dst = me; 462 if (r->type & ROUTE_GWHISADDR) 463 r->gw = peer; 464 } else if ((r->type & ROUTE_DSTHISADDR) && r->dst.s_addr != peer.s_addr) { 465 del.s_addr = r->dst.s_addr & r->mask.s_addr; 466 bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1, 0); 467 r->dst = peer; 468 if (r->type & ROUTE_GWHISADDR) 469 r->gw = peer; 470 } else if ((r->type & ROUTE_GWHISADDR) && r->gw.s_addr != peer.s_addr) 471 r->gw = peer; 472 bundle_SetRoute(bundle, RTM_ADD, r->dst, r->gw, r->mask, 1, 0); 473 } 474} 475 476void 477route_Clean(struct bundle *bundle, struct sticky_route *r) 478{ 479 struct in_addr none, del; 480 481 none.s_addr = INADDR_ANY; 482 for (; r; r = r->next) { 483 del.s_addr = r->dst.s_addr & r->mask.s_addr; 484 bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1, 0); 485 } 486} 487 488void 489route_Add(struct sticky_route **rp, int type, struct in_addr dst, 490 struct in_addr mask, struct in_addr gw) 491{ 492 struct sticky_route *r; 493 int dsttype = type & ROUTE_DSTANY; 494 495 r = NULL; 496 while (*rp) { 497 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 498 (!dsttype && (*rp)->dst.s_addr == dst.s_addr)) { 499 /* Oops, we already have this route - unlink it */ 500 free(r); /* impossible really */ 501 r = *rp; 502 *rp = r->next; 503 } else 504 rp = &(*rp)->next; 505 } 506 507 if (!r) 508 r = (struct sticky_route *)malloc(sizeof(struct sticky_route)); 509 r->type = type; 510 r->next = NULL; 511 r->dst = dst; 512 r->mask = mask; 513 r->gw = gw; 514 *rp = r; 515} 516 517void 518route_Delete(struct sticky_route **rp, int type, struct in_addr dst) 519{ 520 struct sticky_route *r; 521 int dsttype = type & ROUTE_DSTANY; 522 523 for (; *rp; rp = &(*rp)->next) { 524 if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) || 525 (!dsttype && dst.s_addr == ((*rp)->dst.s_addr & (*rp)->mask.s_addr))) { 526 r = *rp; 527 *rp = r->next; 528 free(r); 529 break; 530 } 531 } 532} 533 534void 535route_DeleteAll(struct sticky_route **rp) 536{ 537 struct sticky_route *r, *rn; 538 539 for (r = *rp; r; r = rn) { 540 rn = r->next; 541 free(r); 542 } 543 *rp = NULL; 544} 545 546void 547route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag, 548 int indent) 549{ 550 int def; 551 int tlen = strlen(tag); 552 553 if (tlen + 2 > indent) 554 prompt_Printf(p, "%s:\n%*s", tag, indent, ""); 555 else 556 prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, ""); 557 558 for (; r; r = r->next) { 559 def = r->dst.s_addr == INADDR_ANY && r->mask.s_addr == INADDR_ANY; 560 561 prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, ""); 562 tlen = 0; 563 if (r->type & ROUTE_DSTMYADDR) 564 prompt_Printf(p, "MYADDR"); 565 else if (r->type & ROUTE_DSTHISADDR) 566 prompt_Printf(p, "HISADDR"); 567 else if (!def) 568 prompt_Printf(p, "%s", inet_ntoa(r->dst)); 569 570 if (def) 571 prompt_Printf(p, "default "); 572 else 573 prompt_Printf(p, " %s ", inet_ntoa(r->mask)); 574 575 if (r->type & ROUTE_GWHISADDR) 576 prompt_Printf(p, "HISADDR\n"); 577 else 578 prompt_Printf(p, "%s\n", inet_ntoa(r->gw)); 579 } 580} 581