1 /* 2 * iproute.c "ip route". 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet (at) ms2.inr.ac.ru> 10 * 11 */ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <unistd.h> 16 #include <syslog.h> 17 #include <fcntl.h> 18 #include <string.h> 19 #include <time.h> 20 #include <sys/time.h> 21 #include <sys/socket.h> 22 #include <netinet/in.h> 23 #include <netinet/ip.h> 24 #include <arpa/inet.h> 25 #include <linux/in_route.h> 26 #include <errno.h> 27 28 #include "rt_names.h" 29 #include "utils.h" 30 #include "ip_common.h" 31 32 #ifndef RTAX_RTTVAR 33 #define RTAX_RTTVAR RTAX_HOPS 34 #endif 35 36 enum list_action { 37 IPROUTE_LIST, 38 IPROUTE_FLUSH, 39 IPROUTE_SAVE, 40 }; 41 static const char *mx_names[RTAX_MAX+1] = { 42 [RTAX_MTU] = "mtu", 43 [RTAX_WINDOW] = "window", 44 [RTAX_RTT] = "rtt", 45 [RTAX_RTTVAR] = "rttvar", 46 [RTAX_SSTHRESH] = "ssthresh", 47 [RTAX_CWND] = "cwnd", 48 [RTAX_ADVMSS] = "advmss", 49 [RTAX_REORDERING]="reordering", 50 [RTAX_HOPLIMIT] = "hoplimit", 51 [RTAX_INITCWND] = "initcwnd", 52 [RTAX_FEATURES] = "features", 53 [RTAX_RTO_MIN] = "rto_min", 54 [RTAX_INITRWND] = "initrwnd", 55 }; 56 static void usage(void) __attribute__((noreturn)); 57 58 static void usage(void) 59 { 60 fprintf(stderr, "Usage: ip route { list | flush } SELECTOR\n"); 61 fprintf(stderr, " ip route save SELECTOR\n"); 62 fprintf(stderr, " ip route restore\n"); 63 fprintf(stderr, " ip route get ADDRESS [ from ADDRESS iif STRING ]\n"); 64 fprintf(stderr, " [ oif STRING ] [ tos TOS ]\n"); 65 fprintf(stderr, " [ mark NUMBER ]\n"); 66 fprintf(stderr, " ip route { add | del | change | append | replace } ROUTE\n"); 67 fprintf(stderr, "SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]\n"); 68 fprintf(stderr, " [ table TABLE_ID ] [ proto RTPROTO ]\n"); 69 fprintf(stderr, " [ type TYPE ] [ scope SCOPE ]\n"); 70 fprintf(stderr, "ROUTE := NODE_SPEC [ INFO_SPEC ]\n"); 71 fprintf(stderr, "NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]\n"); 72 fprintf(stderr, " [ table TABLE_ID ] [ proto RTPROTO ]\n"); 73 fprintf(stderr, " [ scope SCOPE ] [ metric METRIC ]\n"); 74 fprintf(stderr, "INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...\n"); 75 fprintf(stderr, "NH := [ via ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS\n"); 76 fprintf(stderr, "OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ]\n"); 77 fprintf(stderr, " [ rtt TIME ] [ rttvar TIME ] [reordering NUMBER ]\n"); 78 fprintf(stderr, " [ window NUMBER] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n"); 79 fprintf(stderr, " [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]\n"); 80 fprintf(stderr, " [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]\n"); 81 fprintf(stderr, "TYPE := [ unicast | local | broadcast | multicast | throw |\n"); 82 fprintf(stderr, " unreachable | prohibit | blackhole | nat ]\n"); 83 fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n"); 84 fprintf(stderr, "SCOPE := [ host | link | global | NUMBER ]\n"); 85 fprintf(stderr, "MP_ALGO := { rr | drr | random | wrandom }\n"); 86 fprintf(stderr, "NHFLAGS := [ onlink | pervasive ]\n"); 87 fprintf(stderr, "RTPROTO := [ kernel | boot | static | NUMBER ]\n"); 88 fprintf(stderr, "TIME := NUMBER[s|ms]\n"); 89 exit(-1); 90 } 91 92 93 static struct 94 { 95 int tb; 96 int cloned; 97 int flushed; 98 char *flushb; 99 int flushp; 100 int flushe; 101 int protocol, protocolmask; 102 int scope, scopemask; 103 int type, typemask; 104 int tos, tosmask; 105 int iif, iifmask; 106 int oif, oifmask; 107 int mark, markmask; 108 int realm, realmmask; 109 inet_prefix rprefsrc; 110 inet_prefix rvia; 111 inet_prefix rdst; 112 inet_prefix mdst; 113 inet_prefix rsrc; 114 inet_prefix msrc; 115 } filter; 116 117 static int flush_update(void) 118 { 119 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { 120 perror("Failed to send flush request"); 121 return -1; 122 } 123 filter.flushp = 0; 124 return 0; 125 } 126 127 int filter_nlmsg(struct nlmsghdr *n, struct rtattr **tb, int host_len) 128 { 129 struct rtmsg *r = NLMSG_DATA(n); 130 inet_prefix dst; 131 inet_prefix src; 132 inet_prefix via; 133 inet_prefix prefsrc; 134 __u32 table; 135 static int ip6_multiple_tables; 136 137 table = rtm_get_table(r, tb); 138 139 if (r->rtm_family == AF_INET6 && table != RT_TABLE_MAIN) 140 ip6_multiple_tables = 1; 141 142 if (filter.cloned == !(r->rtm_flags&RTM_F_CLONED)) 143 return 0; 144 145 if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) { 146 if (filter.tb) { 147 if (filter.tb == RT_TABLE_LOCAL) { 148 if (r->rtm_type != RTN_LOCAL) 149 return 0; 150 } else if (filter.tb == RT_TABLE_MAIN) { 151 if (r->rtm_type == RTN_LOCAL) 152 return 0; 153 } else { 154 return 0; 155 } 156 } 157 } else { 158 if (filter.tb > 0 && filter.tb != table) 159 return 0; 160 } 161 if ((filter.protocol^r->rtm_protocol)&filter.protocolmask) 162 return 0; 163 if ((filter.scope^r->rtm_scope)&filter.scopemask) 164 return 0; 165 if ((filter.type^r->rtm_type)&filter.typemask) 166 return 0; 167 if ((filter.tos^r->rtm_tos)&filter.tosmask) 168 return 0; 169 if (filter.rdst.family && 170 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) 171 return 0; 172 if (filter.mdst.family && 173 (r->rtm_family != filter.mdst.family || 174 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) 175 return 0; 176 if (filter.rsrc.family && 177 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) 178 return 0; 179 if (filter.msrc.family && 180 (r->rtm_family != filter.msrc.family || 181 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) 182 return 0; 183 if (filter.rvia.family && r->rtm_family != filter.rvia.family) 184 return 0; 185 if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family) 186 return 0; 187 188 memset(&dst, 0, sizeof(dst)); 189 dst.family = r->rtm_family; 190 if (tb[RTA_DST]) 191 memcpy(&dst.data, RTA_DATA(tb[RTA_DST]), (r->rtm_dst_len+7)/8); 192 if (filter.rsrc.family || filter.msrc.family) { 193 memset(&src, 0, sizeof(src)); 194 src.family = r->rtm_family; 195 if (tb[RTA_SRC]) 196 memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8); 197 } 198 if (filter.rvia.bitlen>0) { 199 memset(&via, 0, sizeof(via)); 200 via.family = r->rtm_family; 201 if (tb[RTA_GATEWAY]) 202 memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8); 203 } 204 if (filter.rprefsrc.bitlen>0) { 205 memset(&prefsrc, 0, sizeof(prefsrc)); 206 prefsrc.family = r->rtm_family; 207 if (tb[RTA_PREFSRC]) 208 memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8); 209 } 210 211 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen)) 212 return 0; 213 if (filter.mdst.family && filter.mdst.bitlen >= 0 && 214 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len)) 215 return 0; 216 217 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen)) 218 return 0; 219 if (filter.msrc.family && filter.msrc.bitlen >= 0 && 220 inet_addr_match(&src, &filter.msrc, r->rtm_src_len)) 221 return 0; 222 223 if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen)) 224 return 0; 225 if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen)) 226 return 0; 227 if (filter.realmmask) { 228 __u32 realms = 0; 229 if (tb[RTA_FLOW]) 230 realms = rta_getattr_u32(tb[RTA_FLOW]); 231 if ((realms^filter.realm)&filter.realmmask) 232 return 0; 233 } 234 if (filter.iifmask) { 235 int iif = 0; 236 if (tb[RTA_IIF]) 237 iif = *(int*)RTA_DATA(tb[RTA_IIF]); 238 if ((iif^filter.iif)&filter.iifmask) 239 return 0; 240 } 241 if (filter.oifmask) { 242 int oif = 0; 243 if (tb[RTA_OIF]) 244 oif = *(int*)RTA_DATA(tb[RTA_OIF]); 245 if ((oif^filter.oif)&filter.oifmask) 246 return 0; 247 } 248 if (filter.markmask) { 249 int mark = 0; 250 if (tb[RTA_MARK]) 251 mark = *(int *)RTA_DATA(tb[RTA_MARK]); 252 if ((mark ^ filter.mark) & filter.markmask) 253 return 0; 254 } 255 if (filter.flushb && 256 r->rtm_family == AF_INET6 && 257 r->rtm_dst_len == 0 && 258 r->rtm_type == RTN_UNREACHABLE && 259 tb[RTA_PRIORITY] && 260 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1) 261 return 0; 262 263 return 1; 264 } 265 266 int calc_host_len(struct rtmsg *r) 267 { 268 if (r->rtm_family == AF_INET6) 269 return 128; 270 else if (r->rtm_family == AF_INET) 271 return 32; 272 else if (r->rtm_family == AF_DECnet) 273 return 16; 274 else if (r->rtm_family == AF_IPX) 275 return 80; 276 else 277 return -1; 278 } 279 280 int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) 281 { 282 FILE *fp = (FILE*)arg; 283 struct rtmsg *r = NLMSG_DATA(n); 284 int len = n->nlmsg_len; 285 struct rtattr * tb[RTA_MAX+1]; 286 char abuf[256]; 287 int host_len = -1; 288 __u32 table; 289 SPRINT_BUF(b1); 290 static int hz; 291 292 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) { 293 fprintf(stderr, "Not a route: %08x %08x %08x\n", 294 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); 295 return 0; 296 } 297 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE) 298 return 0; 299 len -= NLMSG_LENGTH(sizeof(*r)); 300 if (len < 0) { 301 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); 302 return -1; 303 } 304 305 host_len = calc_host_len(r); 306 307 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); 308 table = rtm_get_table(r, tb); 309 310 if (!filter_nlmsg(n, tb, host_len)) 311 return 0; 312 313 if (filter.flushb) { 314 struct nlmsghdr *fn; 315 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { 316 if (flush_update()) 317 return -1; 318 } 319 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); 320 memcpy(fn, n, n->nlmsg_len); 321 fn->nlmsg_type = RTM_DELROUTE; 322 fn->nlmsg_flags = NLM_F_REQUEST; 323 fn->nlmsg_seq = ++rth.seq; 324 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; 325 filter.flushed++; 326 if (show_stats < 2) 327 return 0; 328 } 329 330 if (n->nlmsg_type == RTM_DELROUTE) 331 fprintf(fp, "Deleted "); 332 if (r->rtm_type != RTN_UNICAST && !filter.type) 333 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); 334 335 if (tb[RTA_DST]) { 336 if (r->rtm_dst_len != host_len) { 337 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family, 338 RTA_PAYLOAD(tb[RTA_DST]), 339 RTA_DATA(tb[RTA_DST]), 340 abuf, sizeof(abuf)), 341 r->rtm_dst_len 342 ); 343 } else { 344 fprintf(fp, "%s ", format_host(r->rtm_family, 345 RTA_PAYLOAD(tb[RTA_DST]), 346 RTA_DATA(tb[RTA_DST]), 347 abuf, sizeof(abuf)) 348 ); 349 } 350 } else if (r->rtm_dst_len) { 351 fprintf(fp, "0/%d ", r->rtm_dst_len); 352 } else { 353 fprintf(fp, "default "); 354 } 355 if (tb[RTA_SRC]) { 356 if (r->rtm_src_len != host_len) { 357 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family, 358 RTA_PAYLOAD(tb[RTA_SRC]), 359 RTA_DATA(tb[RTA_SRC]), 360 abuf, sizeof(abuf)), 361 r->rtm_src_len 362 ); 363 } else { 364 fprintf(fp, "from %s ", format_host(r->rtm_family, 365 RTA_PAYLOAD(tb[RTA_SRC]), 366 RTA_DATA(tb[RTA_SRC]), 367 abuf, sizeof(abuf)) 368 ); 369 } 370 } else if (r->rtm_src_len) { 371 fprintf(fp, "from 0/%u ", r->rtm_src_len); 372 } 373 if (r->rtm_tos && filter.tosmask != -1) { 374 SPRINT_BUF(b1); 375 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1))); 376 } 377 378 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) { 379 fprintf(fp, "via %s ", 380 format_host(r->rtm_family, 381 RTA_PAYLOAD(tb[RTA_GATEWAY]), 382 RTA_DATA(tb[RTA_GATEWAY]), 383 abuf, sizeof(abuf))); 384 } 385 if (tb[RTA_OIF] && filter.oifmask != -1) 386 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF]))); 387 388 if (!(r->rtm_flags&RTM_F_CLONED)) { 389 if (table != RT_TABLE_MAIN && !filter.tb) 390 fprintf(fp, " table %s ", rtnl_rttable_n2a(table, b1, sizeof(b1))); 391 if (r->rtm_protocol != RTPROT_BOOT && filter.protocolmask != -1) 392 fprintf(fp, " proto %s ", rtnl_rtprot_n2a(r->rtm_protocol, b1, sizeof(b1))); 393 if (r->rtm_scope != RT_SCOPE_UNIVERSE && filter.scopemask != -1) 394 fprintf(fp, " scope %s ", rtnl_rtscope_n2a(r->rtm_scope, b1, sizeof(b1))); 395 } 396 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) { 397 /* Do not use format_host(). It is our local addr 398 and symbolic name will not be useful. 399 */ 400 fprintf(fp, " src %s ", 401 rt_addr_n2a(r->rtm_family, 402 RTA_PAYLOAD(tb[RTA_PREFSRC]), 403 RTA_DATA(tb[RTA_PREFSRC]), 404 abuf, sizeof(abuf))); 405 } 406 if (tb[RTA_PRIORITY]) 407 fprintf(fp, " metric %u ", rta_getattr_u32(tb[RTA_PRIORITY])); 408 if (r->rtm_flags & RTNH_F_DEAD) 409 fprintf(fp, "dead "); 410 if (r->rtm_flags & RTNH_F_ONLINK) 411 fprintf(fp, "onlink "); 412 if (r->rtm_flags & RTNH_F_PERVASIVE) 413 fprintf(fp, "pervasive "); 414 if (r->rtm_flags & RTM_F_NOTIFY) 415 fprintf(fp, "notify "); 416 if (tb[RTA_MARK]) { 417 unsigned int mark = *(unsigned int*)RTA_DATA(tb[RTA_MARK]); 418 if (mark) { 419 if (mark >= 16) 420 fprintf(fp, " mark 0x%x", mark); 421 else 422 fprintf(fp, " mark %u", mark); 423 } 424 } 425 426 if (tb[RTA_FLOW] && filter.realmmask != ~0U) { 427 __u32 to = rta_getattr_u32(tb[RTA_FLOW]); 428 __u32 from = to>>16; 429 to &= 0xFFFF; 430 fprintf(fp, "realm%s ", from ? "s" : ""); 431 if (from) { 432 fprintf(fp, "%s/", 433 rtnl_rtrealm_n2a(from, b1, sizeof(b1))); 434 } 435 fprintf(fp, "%s ", 436 rtnl_rtrealm_n2a(to, b1, sizeof(b1))); 437 } 438 if ((r->rtm_flags&RTM_F_CLONED) && r->rtm_family == AF_INET) { 439 __u32 flags = r->rtm_flags&~0xFFFF; 440 int first = 1; 441 442 fprintf(fp, "%s cache ", _SL_); 443 444 #define PRTFL(fl,flname) if (flags&RTCF_##fl) { \ 445 flags &= ~RTCF_##fl; \ 446 fprintf(fp, "%s" flname "%s", first ? "<" : "", flags ? "," : "> "); \ 447 first = 0; } 448 PRTFL(LOCAL, "local"); 449 PRTFL(REJECT, "reject"); 450 PRTFL(MULTICAST, "mc"); 451 PRTFL(BROADCAST, "brd"); 452 PRTFL(DNAT, "dst-nat"); 453 PRTFL(SNAT, "src-nat"); 454 PRTFL(MASQ, "masq"); 455 PRTFL(DIRECTDST, "dst-direct"); 456 PRTFL(DIRECTSRC, "src-direct"); 457 PRTFL(REDIRECTED, "redirected"); 458 PRTFL(DOREDIRECT, "redirect"); 459 PRTFL(FAST, "fastroute"); 460 PRTFL(NOTIFY, "notify"); 461 PRTFL(TPROXY, "proxy"); 462 463 if (flags) 464 fprintf(fp, "%s%x> ", first ? "<" : "", flags); 465 if (tb[RTA_CACHEINFO]) { 466 struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]); 467 if (!hz) 468 hz = get_user_hz(); 469 if (ci->rta_expires != 0) 470 fprintf(fp, " expires %dsec", ci->rta_expires/hz); 471 if (ci->rta_error != 0) 472 fprintf(fp, " error %d", ci->rta_error); 473 if (show_stats) { 474 if (ci->rta_clntref) 475 fprintf(fp, " users %d", ci->rta_clntref); 476 if (ci->rta_used != 0) 477 fprintf(fp, " used %d", ci->rta_used); 478 if (ci->rta_lastuse != 0) 479 fprintf(fp, " age %dsec", ci->rta_lastuse/hz); 480 } 481 if (ci->rta_id) 482 fprintf(fp, " ipid 0x%04x", ci->rta_id); 483 if (ci->rta_ts || ci->rta_tsage) 484 fprintf(fp, " ts 0x%x tsage %dsec", 485 ci->rta_ts, ci->rta_tsage); 486 } 487 } else if (r->rtm_family == AF_INET6) { 488 struct rta_cacheinfo *ci = NULL; 489 if (tb[RTA_CACHEINFO]) 490 ci = RTA_DATA(tb[RTA_CACHEINFO]); 491 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) { 492 if (!hz) 493 hz = get_user_hz(); 494 if (r->rtm_flags & RTM_F_CLONED) 495 fprintf(fp, "%s cache ", _SL_); 496 if (ci->rta_expires) 497 fprintf(fp, " expires %dsec", ci->rta_expires/hz); 498 if (ci->rta_error != 0) 499 fprintf(fp, " error %d", ci->rta_error); 500 if (show_stats) { 501 if (ci->rta_clntref) 502 fprintf(fp, " users %d", ci->rta_clntref); 503 if (ci->rta_used != 0) 504 fprintf(fp, " used %d", ci->rta_used); 505 if (ci->rta_lastuse != 0) 506 fprintf(fp, " age %dsec", ci->rta_lastuse/hz); 507 } 508 } else if (ci) { 509 if (ci->rta_error != 0) 510 fprintf(fp, " error %d", ci->rta_error); 511 } 512 } 513 if (tb[RTA_METRICS]) { 514 int i; 515 unsigned mxlock = 0; 516 struct rtattr *mxrta[RTAX_MAX+1]; 517 518 parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), 519 RTA_PAYLOAD(tb[RTA_METRICS])); 520 if (mxrta[RTAX_LOCK]) 521 mxlock = *(unsigned*)RTA_DATA(mxrta[RTAX_LOCK]); 522 523 for (i=2; i<= RTAX_MAX; i++) { 524 unsigned val; 525 526 if (mxrta[i] == NULL) 527 continue; 528 529 if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i]) 530 fprintf(fp, " %s", mx_names[i]); 531 else 532 fprintf(fp, " metric %d", i); 533 if (mxlock & (1<<i)) 534 fprintf(fp, " lock"); 535 536 val = *(unsigned*)RTA_DATA(mxrta[i]); 537 switch (i) { 538 case RTAX_HOPLIMIT: 539 if ((int)val == -1) 540 val = 0; 541 /* fall through */ 542 default: 543 fprintf(fp, " %u", val); 544 break; 545 546 case RTAX_RTT: 547 case RTAX_RTTVAR: 548 case RTAX_RTO_MIN: 549 if (i == RTAX_RTT) 550 val /= 8; 551 else if (i == RTAX_RTTVAR) 552 val /= 4; 553 554 if (val >= 1000) 555 fprintf(fp, " %gs", val/1e3); 556 else 557 fprintf(fp, " %ums", val); 558 } 559 } 560 } 561 if (tb[RTA_IIF] && filter.iifmask != -1) { 562 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF]))); 563 } 564 if (tb[RTA_MULTIPATH]) { 565 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]); 566 int first = 0; 567 568 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); 569 570 for (;;) { 571 if (len < sizeof(*nh)) 572 break; 573 if (nh->rtnh_len > len) 574 break; 575 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) { 576 if (first) 577 fprintf(fp, " Oifs:"); 578 else 579 fprintf(fp, " "); 580 } else 581 fprintf(fp, "%s\tnexthop", _SL_); 582 if (nh->rtnh_len > sizeof(*nh)) { 583 parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh)); 584 if (tb[RTA_GATEWAY]) { 585 fprintf(fp, " via %s ", 586 format_host(r->rtm_family, 587 RTA_PAYLOAD(tb[RTA_GATEWAY]), 588 RTA_DATA(tb[RTA_GATEWAY]), 589 abuf, sizeof(abuf))); 590 } 591 if (tb[RTA_FLOW]) { 592 __u32 to = rta_getattr_u32(tb[RTA_FLOW]); 593 __u32 from = to>>16; 594 to &= 0xFFFF; 595 fprintf(fp, " realm%s ", from ? "s" : ""); 596 if (from) { 597 fprintf(fp, "%s/", 598 rtnl_rtrealm_n2a(from, b1, sizeof(b1))); 599 } 600 fprintf(fp, "%s", 601 rtnl_rtrealm_n2a(to, b1, sizeof(b1))); 602 } 603 } 604 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) { 605 fprintf(fp, " %s", ll_index_to_name(nh->rtnh_ifindex)); 606 if (nh->rtnh_hops != 1) 607 fprintf(fp, "(ttl>%d)", nh->rtnh_hops); 608 } else { 609 fprintf(fp, " dev %s", ll_index_to_name(nh->rtnh_ifindex)); 610 fprintf(fp, " weight %d", nh->rtnh_hops+1); 611 } 612 if (nh->rtnh_flags & RTNH_F_DEAD) 613 fprintf(fp, " dead"); 614 if (nh->rtnh_flags & RTNH_F_ONLINK) 615 fprintf(fp, " onlink"); 616 if (nh->rtnh_flags & RTNH_F_PERVASIVE) 617 fprintf(fp, " pervasive"); 618 len -= NLMSG_ALIGN(nh->rtnh_len); 619 nh = RTNH_NEXT(nh); 620 } 621 } 622 fprintf(fp, "\n"); 623 fflush(fp); 624 return 0; 625 } 626 627 628 int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char ***argvp) 629 { 630 int argc = *argcp; 631 char **argv = *argvp; 632 633 while (++argv, --argc > 0) { 634 if (strcmp(*argv, "via") == 0) { 635 NEXT_ARG(); 636 rta_addattr32(rta, 4096, RTA_GATEWAY, get_addr32(*argv)); 637 rtnh->rtnh_len += sizeof(struct rtattr) + 4; 638 } else if (strcmp(*argv, "dev") == 0) { 639 NEXT_ARG(); 640 if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) { 641 fprintf(stderr, "Cannot find device \"%s\"\n", *argv); 642 exit(1); 643 } 644 } else if (strcmp(*argv, "weight") == 0) { 645 unsigned w; 646 NEXT_ARG(); 647 if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256) 648 invarg("\"weight\" is invalid\n", *argv); 649 rtnh->rtnh_hops = w - 1; 650 } else if (strcmp(*argv, "onlink") == 0) { 651 rtnh->rtnh_flags |= RTNH_F_ONLINK; 652 } else if (matches(*argv, "realms") == 0) { 653 __u32 realm; 654 NEXT_ARG(); 655 if (get_rt_realms(&realm, *argv)) 656 invarg("\"realm\" value is invalid\n", *argv); 657 rta_addattr32(rta, 4096, RTA_FLOW, realm); 658 rtnh->rtnh_len += sizeof(struct rtattr) + 4; 659 } else 660 break; 661 } 662 *argcp = argc; 663 *argvp = argv; 664 return 0; 665 } 666 667 int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, int argc, char **argv) 668 { 669 char buf[1024]; 670 struct rtattr *rta = (void*)buf; 671 struct rtnexthop *rtnh; 672 673 rta->rta_type = RTA_MULTIPATH; 674 rta->rta_len = RTA_LENGTH(0); 675 rtnh = RTA_DATA(rta); 676 677 while (argc > 0) { 678 if (strcmp(*argv, "nexthop") != 0) { 679 fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv); 680 exit(-1); 681 } 682 if (argc <= 1) { 683 fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n"); 684 exit(-1); 685 } 686 memset(rtnh, 0, sizeof(*rtnh)); 687 rtnh->rtnh_len = sizeof(*rtnh); 688 rta->rta_len += rtnh->rtnh_len; 689 parse_one_nh(rta, rtnh, &argc, &argv); 690 rtnh = RTNH_NEXT(rtnh); 691 } 692 693 if (rta->rta_len > RTA_LENGTH(0)) 694 addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta)); 695 return 0; 696 } 697 698 699 int iproute_modify(int cmd, unsigned flags, int argc, char **argv) 700 { 701 struct { 702 struct nlmsghdr n; 703 struct rtmsg r; 704 char buf[1024]; 705 } req; 706 char mxbuf[256]; 707 struct rtattr * mxrta = (void*)mxbuf; 708 unsigned mxlock = 0; 709 char *d = NULL; 710 int gw_ok = 0; 711 int dst_ok = 0; 712 int nhs_ok = 0; 713 int scope_ok = 0; 714 int table_ok = 0; 715 int raw = 0; 716 717 memset(&req, 0, sizeof(req)); 718 719 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); 720 req.n.nlmsg_flags = NLM_F_REQUEST|flags; 721 req.n.nlmsg_type = cmd; 722 req.r.rtm_family = preferred_family; 723 req.r.rtm_table = RT_TABLE_MAIN; 724 req.r.rtm_scope = RT_SCOPE_NOWHERE; 725 726 if (cmd != RTM_DELROUTE) { 727 req.r.rtm_protocol = RTPROT_BOOT; 728 req.r.rtm_scope = RT_SCOPE_UNIVERSE; 729 req.r.rtm_type = RTN_UNICAST; 730 } 731 732 mxrta->rta_type = RTA_METRICS; 733 mxrta->rta_len = RTA_LENGTH(0); 734 735 while (argc > 0) { 736 if (strcmp(*argv, "src") == 0) { 737 inet_prefix addr; 738 NEXT_ARG(); 739 get_addr(&addr, *argv, req.r.rtm_family); 740 if (req.r.rtm_family == AF_UNSPEC) 741 req.r.rtm_family = addr.family; 742 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen); 743 } else if (strcmp(*argv, "via") == 0) { 744 inet_prefix addr; 745 gw_ok = 1; 746 NEXT_ARG(); 747 get_addr(&addr, *argv, req.r.rtm_family); 748 if (req.r.rtm_family == AF_UNSPEC) 749 req.r.rtm_family = addr.family; 750 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen); 751 } else if (strcmp(*argv, "from") == 0) { 752 inet_prefix addr; 753 NEXT_ARG(); 754 get_prefix(&addr, *argv, req.r.rtm_family); 755 if (req.r.rtm_family == AF_UNSPEC) 756 req.r.rtm_family = addr.family; 757 if (addr.bytelen) 758 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); 759 req.r.rtm_src_len = addr.bitlen; 760 } else if (strcmp(*argv, "tos") == 0 || 761 matches(*argv, "dsfield") == 0) { 762 __u32 tos; 763 NEXT_ARG(); 764 if (rtnl_dsfield_a2n(&tos, *argv)) 765 invarg("\"tos\" value is invalid\n", *argv); 766 req.r.rtm_tos = tos; 767 } else if (matches(*argv, "metric") == 0 || 768 matches(*argv, "priority") == 0 || 769 matches(*argv, "preference") == 0) { 770 __u32 metric; 771 NEXT_ARG(); 772 if (get_u32(&metric, *argv, 0)) 773 invarg("\"metric\" value is invalid\n", *argv); 774 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric); 775 } else if (strcmp(*argv, "scope") == 0) { 776 __u32 scope = 0; 777 NEXT_ARG(); 778 if (rtnl_rtscope_a2n(&scope, *argv)) 779 invarg("invalid \"scope\" value\n", *argv); 780 req.r.rtm_scope = scope; 781 scope_ok = 1; 782 } else if (strcmp(*argv, "mtu") == 0) { 783 unsigned mtu; 784 NEXT_ARG(); 785 if (strcmp(*argv, "lock") == 0) { 786 mxlock |= (1<<RTAX_MTU); 787 NEXT_ARG(); 788 } 789 if (get_unsigned(&mtu, *argv, 0)) 790 invarg("\"mtu\" value is invalid\n", *argv); 791 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu); 792 } else if (strcmp(*argv, "hoplimit") == 0) { 793 unsigned hoplimit; 794 NEXT_ARG(); 795 if (strcmp(*argv, "lock") == 0) { 796 mxlock |= (1<<RTAX_HOPLIMIT); 797 NEXT_ARG(); 798 } 799 if (get_unsigned(&hoplimit, *argv, 0)) 800 invarg("\"hoplimit\" value is invalid\n", *argv); 801 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit); 802 } else if (strcmp(*argv, "advmss") == 0) { 803 unsigned mss; 804 NEXT_ARG(); 805 if (strcmp(*argv, "lock") == 0) { 806 mxlock |= (1<<RTAX_ADVMSS); 807 NEXT_ARG(); 808 } 809 if (get_unsigned(&mss, *argv, 0)) 810 invarg("\"mss\" value is invalid\n", *argv); 811 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss); 812 } else if (matches(*argv, "reordering") == 0) { 813 unsigned reord; 814 NEXT_ARG(); 815 if (strcmp(*argv, "lock") == 0) { 816 mxlock |= (1<<RTAX_REORDERING); 817 NEXT_ARG(); 818 } 819 if (get_unsigned(&reord, *argv, 0)) 820 invarg("\"reordering\" value is invalid\n", *argv); 821 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord); 822 } else if (strcmp(*argv, "rtt") == 0) { 823 unsigned rtt; 824 NEXT_ARG(); 825 if (strcmp(*argv, "lock") == 0) { 826 mxlock |= (1<<RTAX_RTT); 827 NEXT_ARG(); 828 } 829 if (get_time_rtt(&rtt, *argv, &raw)) 830 invarg("\"rtt\" value is invalid\n", *argv); 831 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, 832 (raw) ? rtt : rtt * 8); 833 } else if (strcmp(*argv, "rto_min") == 0) { 834 unsigned rto_min; 835 NEXT_ARG(); 836 mxlock |= (1<<RTAX_RTO_MIN); 837 if (get_time_rtt(&rto_min, *argv, &raw)) 838 invarg("\"rto_min\" value is invalid\n", 839 *argv); 840 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN, 841 rto_min); 842 } else if (matches(*argv, "window") == 0) { 843 unsigned win; 844 NEXT_ARG(); 845 if (strcmp(*argv, "lock") == 0) { 846 mxlock |= (1<<RTAX_WINDOW); 847 NEXT_ARG(); 848 } 849 if (get_unsigned(&win, *argv, 0)) 850 invarg("\"window\" value is invalid\n", *argv); 851 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win); 852 } else if (matches(*argv, "cwnd") == 0) { 853 unsigned win; 854 NEXT_ARG(); 855 if (strcmp(*argv, "lock") == 0) { 856 mxlock |= (1<<RTAX_CWND); 857 NEXT_ARG(); 858 } 859 if (get_unsigned(&win, *argv, 0)) 860 invarg("\"cwnd\" value is invalid\n", *argv); 861 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win); 862 } else if (matches(*argv, "initcwnd") == 0) { 863 unsigned win; 864 NEXT_ARG(); 865 if (strcmp(*argv, "lock") == 0) { 866 mxlock |= (1<<RTAX_INITCWND); 867 NEXT_ARG(); 868 } 869 if (get_unsigned(&win, *argv, 0)) 870 invarg("\"initcwnd\" value is invalid\n", *argv); 871 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win); 872 } else if (matches(*argv, "initrwnd") == 0) { 873 unsigned win; 874 NEXT_ARG(); 875 if (strcmp(*argv, "lock") == 0) { 876 mxlock |= (1<<RTAX_INITRWND); 877 NEXT_ARG(); 878 } 879 if (get_unsigned(&win, *argv, 0)) 880 invarg("\"initrwnd\" value is invalid\n", *argv); 881 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win); 882 } else if (matches(*argv, "rttvar") == 0) { 883 unsigned win; 884 NEXT_ARG(); 885 if (strcmp(*argv, "lock") == 0) { 886 mxlock |= (1<<RTAX_RTTVAR); 887 NEXT_ARG(); 888 } 889 if (get_time_rtt(&win, *argv, &raw)) 890 invarg("\"rttvar\" value is invalid\n", *argv); 891 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR, 892 (raw) ? win : win * 4); 893 } else if (matches(*argv, "ssthresh") == 0) { 894 unsigned win; 895 NEXT_ARG(); 896 if (strcmp(*argv, "lock") == 0) { 897 mxlock |= (1<<RTAX_SSTHRESH); 898 NEXT_ARG(); 899 } 900 if (get_unsigned(&win, *argv, 0)) 901 invarg("\"ssthresh\" value is invalid\n", *argv); 902 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win); 903 } else if (matches(*argv, "realms") == 0) { 904 __u32 realm; 905 NEXT_ARG(); 906 if (get_rt_realms(&realm, *argv)) 907 invarg("\"realm\" value is invalid\n", *argv); 908 addattr32(&req.n, sizeof(req), RTA_FLOW, realm); 909 } else if (strcmp(*argv, "onlink") == 0) { 910 req.r.rtm_flags |= RTNH_F_ONLINK; 911 } else if (strcmp(*argv, "nexthop") == 0) { 912 nhs_ok = 1; 913 break; 914 } else if (matches(*argv, "protocol") == 0) { 915 __u32 prot; 916 NEXT_ARG(); 917 if (rtnl_rtprot_a2n(&prot, *argv)) 918 invarg("\"protocol\" value is invalid\n", *argv); 919 req.r.rtm_protocol = prot; 920 } else if (matches(*argv, "table") == 0) { 921 __u32 tid; 922 NEXT_ARG(); 923 if (rtnl_rttable_a2n(&tid, *argv)) 924 invarg("\"table\" value is invalid\n", *argv); 925 if (tid < 256) 926 req.r.rtm_table = tid; 927 else { 928 req.r.rtm_table = RT_TABLE_UNSPEC; 929 addattr32(&req.n, sizeof(req), RTA_TABLE, tid); 930 } 931 table_ok = 1; 932 } else if (strcmp(*argv, "dev") == 0 || 933 strcmp(*argv, "oif") == 0) { 934 NEXT_ARG(); 935 d = *argv; 936 } else { 937 int type; 938 inet_prefix dst; 939 940 if (strcmp(*argv, "to") == 0) { 941 NEXT_ARG(); 942 } 943 if ((**argv < '0' || **argv > '9') && 944 rtnl_rtntype_a2n(&type, *argv) == 0) { 945 NEXT_ARG(); 946 req.r.rtm_type = type; 947 } 948 949 if (matches(*argv, "help") == 0) 950 usage(); 951 if (dst_ok) 952 duparg2("to", *argv); 953 get_prefix(&dst, *argv, req.r.rtm_family); 954 if (req.r.rtm_family == AF_UNSPEC) 955 req.r.rtm_family = dst.family; 956 req.r.rtm_dst_len = dst.bitlen; 957 dst_ok = 1; 958 if (dst.bytelen) 959 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); 960 } 961 argc--; argv++; 962 } 963 964 if (d || nhs_ok) { 965 int idx; 966 967 ll_init_map(&rth); 968 969 if (d) { 970 if ((idx = ll_name_to_index(d)) == 0) { 971 fprintf(stderr, "Cannot find device \"%s\"\n", d); 972 return -1; 973 } 974 addattr32(&req.n, sizeof(req), RTA_OIF, idx); 975 } 976 } 977 978 if (mxrta->rta_len > RTA_LENGTH(0)) { 979 if (mxlock) 980 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock); 981 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta)); 982 } 983 984 if (nhs_ok) 985 parse_nexthops(&req.n, &req.r, argc, argv); 986 987 if (!table_ok) { 988 if (req.r.rtm_type == RTN_LOCAL || 989 req.r.rtm_type == RTN_BROADCAST || 990 req.r.rtm_type == RTN_NAT || 991 req.r.rtm_type == RTN_ANYCAST) 992 req.r.rtm_table = RT_TABLE_LOCAL; 993 } 994 if (!scope_ok) { 995 if (req.r.rtm_type == RTN_LOCAL || 996 req.r.rtm_type == RTN_NAT) 997 req.r.rtm_scope = RT_SCOPE_HOST; 998 else if (req.r.rtm_type == RTN_BROADCAST || 999 req.r.rtm_type == RTN_MULTICAST || 1000 req.r.rtm_type == RTN_ANYCAST) 1001 req.r.rtm_scope = RT_SCOPE_LINK; 1002 else if (req.r.rtm_type == RTN_UNICAST || 1003 req.r.rtm_type == RTN_UNSPEC) { 1004 if (cmd == RTM_DELROUTE) 1005 req.r.rtm_scope = RT_SCOPE_NOWHERE; 1006 else if (!gw_ok && !nhs_ok) 1007 req.r.rtm_scope = RT_SCOPE_LINK; 1008 } 1009 } 1010 1011 if (req.r.rtm_family == AF_UNSPEC) 1012 req.r.rtm_family = AF_INET; 1013 1014 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) 1015 exit(2); 1016 1017 return 0; 1018 } 1019 1020 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family) 1021 { 1022 struct { 1023 struct nlmsghdr nlh; 1024 struct rtmsg rtm; 1025 } req; 1026 struct sockaddr_nl nladdr; 1027 1028 memset(&nladdr, 0, sizeof(nladdr)); 1029 memset(&req, 0, sizeof(req)); 1030 nladdr.nl_family = AF_NETLINK; 1031 1032 req.nlh.nlmsg_len = sizeof(req); 1033 req.nlh.nlmsg_type = RTM_GETROUTE; 1034 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST; 1035 req.nlh.nlmsg_pid = 0; 1036 req.nlh.nlmsg_seq = rth->dump = ++rth->seq; 1037 req.rtm.rtm_family = family; 1038 req.rtm.rtm_flags |= RTM_F_CLONED; 1039 1040 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr)); 1041 } 1042 1043 static int iproute_flush_cache(void) 1044 { 1045 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush" 1046 1047 int len; 1048 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY); 1049 char *buffer = "-1"; 1050 1051 if (flush_fd < 0) { 1052 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH); 1053 return -1; 1054 } 1055 1056 len = strlen (buffer); 1057 1058 if ((write (flush_fd, (void *)buffer, len)) < len) { 1059 fprintf (stderr, "Cannot flush routing cache\n"); 1060 close(flush_fd); 1061 return -1; 1062 } 1063 close(flush_fd); 1064 return 0; 1065 } 1066 1067 int save_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) 1068 { 1069 int ret; 1070 int len = n->nlmsg_len; 1071 struct rtmsg *r = NLMSG_DATA(n); 1072 struct rtattr *tb[RTA_MAX+1]; 1073 int host_len = -1; 1074 1075 if (isatty(STDOUT_FILENO)) { 1076 fprintf(stderr, "Not sending binary stream to stdout\n"); 1077 return -1; 1078 } 1079 1080 host_len = calc_host_len(r); 1081 len -= NLMSG_LENGTH(sizeof(*r)); 1082 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); 1083 1084 if (!filter_nlmsg(n, tb, host_len)) 1085 return 0; 1086 1087 ret = write(STDOUT_FILENO, n, n->nlmsg_len); 1088 if ((ret > 0) && (ret != n->nlmsg_len)) { 1089 fprintf(stderr, "Short write while saving nlmsg\n"); 1090 ret = -EIO; 1091 } 1092 1093 return ret == n->nlmsg_len ? 0 : ret; 1094 } 1095 1096 static int iproute_list_flush_or_save(int argc, char **argv, int action) 1097 { 1098 int do_ipv6 = preferred_family; 1099 char *id = NULL; 1100 char *od = NULL; 1101 unsigned int mark = 0; 1102 rtnl_filter_t filter_fn; 1103 1104 if (action == IPROUTE_SAVE) 1105 filter_fn = save_route; 1106 else 1107 filter_fn = print_route; 1108 1109 iproute_reset_filter(); 1110 filter.tb = RT_TABLE_MAIN; 1111 1112 if ((action == IPROUTE_FLUSH) && argc <= 0) { 1113 fprintf(stderr, "\"ip route flush\" requires arguments.\n"); 1114 return -1; 1115 } 1116 1117 while (argc > 0) { 1118 if (matches(*argv, "table") == 0) { 1119 __u32 tid; 1120 NEXT_ARG(); 1121 if (rtnl_rttable_a2n(&tid, *argv)) { 1122 if (strcmp(*argv, "all") == 0) { 1123 filter.tb = 0; 1124 } else if (strcmp(*argv, "cache") == 0) { 1125 filter.cloned = 1; 1126 } else if (strcmp(*argv, "help") == 0) { 1127 usage(); 1128 } else { 1129 invarg("table id value is invalid\n", *argv); 1130 } 1131 } else 1132 filter.tb = tid; 1133 } else if (matches(*argv, "cached") == 0 || 1134 matches(*argv, "cloned") == 0) { 1135 filter.cloned = 1; 1136 } else if (strcmp(*argv, "tos") == 0 || 1137 matches(*argv, "dsfield") == 0) { 1138 __u32 tos; 1139 NEXT_ARG(); 1140 if (rtnl_dsfield_a2n(&tos, *argv)) 1141 invarg("TOS value is invalid\n", *argv); 1142 filter.tos = tos; 1143 filter.tosmask = -1; 1144 } else if (matches(*argv, "protocol") == 0) { 1145 __u32 prot = 0; 1146 NEXT_ARG(); 1147 filter.protocolmask = -1; 1148 if (rtnl_rtprot_a2n(&prot, *argv)) { 1149 if (strcmp(*argv, "all") != 0) 1150 invarg("invalid \"protocol\"\n", *argv); 1151 prot = 0; 1152 filter.protocolmask = 0; 1153 } 1154 filter.protocol = prot; 1155 } else if (matches(*argv, "scope") == 0) { 1156 __u32 scope = 0; 1157 NEXT_ARG(); 1158 filter.scopemask = -1; 1159 if (rtnl_rtscope_a2n(&scope, *argv)) { 1160 if (strcmp(*argv, "all") != 0) 1161 invarg("invalid \"scope\"\n", *argv); 1162 scope = RT_SCOPE_NOWHERE; 1163 filter.scopemask = 0; 1164 } 1165 filter.scope = scope; 1166 } else if (matches(*argv, "type") == 0) { 1167 int type; 1168 NEXT_ARG(); 1169 filter.typemask = -1; 1170 if (rtnl_rtntype_a2n(&type, *argv)) 1171 invarg("node type value is invalid\n", *argv); 1172 filter.type = type; 1173 } else if (strcmp(*argv, "dev") == 0 || 1174 strcmp(*argv, "oif") == 0) { 1175 NEXT_ARG(); 1176 od = *argv; 1177 } else if (strcmp(*argv, "iif") == 0) { 1178 NEXT_ARG(); 1179 id = *argv; 1180 } else if (strcmp(*argv, "mark") == 0) { 1181 NEXT_ARG(); 1182 get_unsigned(&mark, *argv, 0); 1183 filter.markmask = -1; 1184 } else if (strcmp(*argv, "via") == 0) { 1185 NEXT_ARG(); 1186 get_prefix(&filter.rvia, *argv, do_ipv6); 1187 } else if (strcmp(*argv, "src") == 0) { 1188 NEXT_ARG(); 1189 get_prefix(&filter.rprefsrc, *argv, do_ipv6); 1190 } else if (matches(*argv, "realms") == 0) { 1191 __u32 realm; 1192 NEXT_ARG(); 1193 if (get_rt_realms(&realm, *argv)) 1194 invarg("invalid realms\n", *argv); 1195 filter.realm = realm; 1196 filter.realmmask = ~0U; 1197 if ((filter.realm&0xFFFF) == 0 && 1198 (*argv)[strlen(*argv) - 1] == '/') 1199 filter.realmmask &= ~0xFFFF; 1200 if ((filter.realm&0xFFFF0000U) == 0 && 1201 (strchr(*argv, '/') == NULL || 1202 (*argv)[0] == '/')) 1203 filter.realmmask &= ~0xFFFF0000U; 1204 } else if (matches(*argv, "from") == 0) { 1205 NEXT_ARG(); 1206 if (matches(*argv, "root") == 0) { 1207 NEXT_ARG(); 1208 get_prefix(&filter.rsrc, *argv, do_ipv6); 1209 } else if (matches(*argv, "match") == 0) { 1210 NEXT_ARG(); 1211 get_prefix(&filter.msrc, *argv, do_ipv6); 1212 } else { 1213 if (matches(*argv, "exact") == 0) { 1214 NEXT_ARG(); 1215 } 1216 get_prefix(&filter.msrc, *argv, do_ipv6); 1217 filter.rsrc = filter.msrc; 1218 } 1219 } else { 1220 if (matches(*argv, "to") == 0) { 1221 NEXT_ARG(); 1222 } 1223 if (matches(*argv, "root") == 0) { 1224 NEXT_ARG(); 1225 get_prefix(&filter.rdst, *argv, do_ipv6); 1226 } else if (matches(*argv, "match") == 0) { 1227 NEXT_ARG(); 1228 get_prefix(&filter.mdst, *argv, do_ipv6); 1229 } else { 1230 if (matches(*argv, "exact") == 0) { 1231 NEXT_ARG(); 1232 } 1233 get_prefix(&filter.mdst, *argv, do_ipv6); 1234 filter.rdst = filter.mdst; 1235 } 1236 } 1237 argc--; argv++; 1238 } 1239 1240 if (do_ipv6 == AF_UNSPEC && filter.tb) 1241 do_ipv6 = AF_INET; 1242 1243 ll_init_map(&rth); 1244 1245 if (id || od) { 1246 int idx; 1247 1248 if (id) { 1249 if ((idx = ll_name_to_index(id)) == 0) { 1250 fprintf(stderr, "Cannot find device \"%s\"\n", id); 1251 return -1; 1252 } 1253 filter.iif = idx; 1254 filter.iifmask = -1; 1255 } 1256 if (od) { 1257 if ((idx = ll_name_to_index(od)) == 0) { 1258 fprintf(stderr, "Cannot find device \"%s\"\n", od); 1259 return -1; 1260 } 1261 filter.oif = idx; 1262 filter.oifmask = -1; 1263 } 1264 } 1265 filter.mark = mark; 1266 1267 if (action == IPROUTE_FLUSH) { 1268 int round = 0; 1269 char flushb[4096-512]; 1270 time_t start = time(0); 1271 1272 if (filter.cloned) { 1273 if (do_ipv6 != AF_INET6) { 1274 iproute_flush_cache(); 1275 if (show_stats) 1276 printf("*** IPv4 routing cache is flushed.\n"); 1277 } 1278 if (do_ipv6 == AF_INET) 1279 return 0; 1280 } 1281 1282 filter.flushb = flushb; 1283 filter.flushp = 0; 1284 filter.flushe = sizeof(flushb); 1285 1286 for (;;) { 1287 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { 1288 perror("Cannot send dump request"); 1289 exit(1); 1290 } 1291 filter.flushed = 0; 1292 if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) { 1293 fprintf(stderr, "Flush terminated\n"); 1294 exit(1); 1295 } 1296 if (filter.flushed == 0) { 1297 if (show_stats) { 1298 if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6)) 1299 printf("Nothing to flush.\n"); 1300 else 1301 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); 1302 } 1303 fflush(stdout); 1304 return 0; 1305 } 1306 round++; 1307 if (flush_update() < 0) 1308 exit(1); 1309 1310 if (time(0) - start > 30) { 1311 printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n", 1312 time(0) - start, filter.flushed); 1313 exit(1); 1314 } 1315 1316 if (show_stats) { 1317 printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed); 1318 fflush(stdout); 1319 } 1320 } 1321 } 1322 1323 if (!filter.cloned) { 1324 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { 1325 perror("Cannot send dump request"); 1326 exit(1); 1327 } 1328 } else { 1329 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { 1330 perror("Cannot send dump request"); 1331 exit(1); 1332 } 1333 } 1334 1335 if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) { 1336 fprintf(stderr, "Dump terminated\n"); 1337 exit(1); 1338 } 1339 1340 exit(0); 1341 } 1342 1343 1344 int iproute_get(int argc, char **argv) 1345 { 1346 struct { 1347 struct nlmsghdr n; 1348 struct rtmsg r; 1349 char buf[1024]; 1350 } req; 1351 char *idev = NULL; 1352 char *odev = NULL; 1353 int connected = 0; 1354 int from_ok = 0; 1355 unsigned int mark = 0; 1356 1357 memset(&req, 0, sizeof(req)); 1358 1359 iproute_reset_filter(); 1360 filter.cloned = 2; 1361 1362 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); 1363 req.n.nlmsg_flags = NLM_F_REQUEST; 1364 req.n.nlmsg_type = RTM_GETROUTE; 1365 req.r.rtm_family = preferred_family; 1366 req.r.rtm_table = 0; 1367 req.r.rtm_protocol = 0; 1368 req.r.rtm_scope = 0; 1369 req.r.rtm_type = 0; 1370 req.r.rtm_src_len = 0; 1371 req.r.rtm_dst_len = 0; 1372 req.r.rtm_tos = 0; 1373 1374 while (argc > 0) { 1375 if (strcmp(*argv, "tos") == 0 || 1376 matches(*argv, "dsfield") == 0) { 1377 __u32 tos; 1378 NEXT_ARG(); 1379 if (rtnl_dsfield_a2n(&tos, *argv)) 1380 invarg("TOS value is invalid\n", *argv); 1381 req.r.rtm_tos = tos; 1382 } else if (matches(*argv, "from") == 0) { 1383 inet_prefix addr; 1384 NEXT_ARG(); 1385 if (matches(*argv, "help") == 0) 1386 usage(); 1387 from_ok = 1; 1388 get_prefix(&addr, *argv, req.r.rtm_family); 1389 if (req.r.rtm_family == AF_UNSPEC) 1390 req.r.rtm_family = addr.family; 1391 if (addr.bytelen) 1392 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); 1393 req.r.rtm_src_len = addr.bitlen; 1394 } else if (matches(*argv, "iif") == 0) { 1395 NEXT_ARG(); 1396 idev = *argv; 1397 } else if (matches(*argv, "mark") == 0) { 1398 NEXT_ARG(); 1399 get_unsigned(&mark, *argv, 0); 1400 } else if (matches(*argv, "oif") == 0 || 1401 strcmp(*argv, "dev") == 0) { 1402 NEXT_ARG(); 1403 odev = *argv; 1404 } else if (matches(*argv, "notify") == 0) { 1405 req.r.rtm_flags |= RTM_F_NOTIFY; 1406 } else if (matches(*argv, "connected") == 0) { 1407 connected = 1; 1408 } else { 1409 inet_prefix addr; 1410 if (strcmp(*argv, "to") == 0) { 1411 NEXT_ARG(); 1412 } 1413 if (matches(*argv, "help") == 0) 1414 usage(); 1415 get_prefix(&addr, *argv, req.r.rtm_family); 1416 if (req.r.rtm_family == AF_UNSPEC) 1417 req.r.rtm_family = addr.family; 1418 if (addr.bytelen) 1419 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen); 1420 req.r.rtm_dst_len = addr.bitlen; 1421 } 1422 argc--; argv++; 1423 } 1424 1425 if (req.r.rtm_dst_len == 0) { 1426 fprintf(stderr, "need at least destination address\n"); 1427 exit(1); 1428 } 1429 1430 ll_init_map(&rth); 1431 1432 if (idev || odev) { 1433 int idx; 1434 1435 if (idev) { 1436 if ((idx = ll_name_to_index(idev)) == 0) { 1437 fprintf(stderr, "Cannot find device \"%s\"\n", idev); 1438 return -1; 1439 } 1440 addattr32(&req.n, sizeof(req), RTA_IIF, idx); 1441 } 1442 if (odev) { 1443 if ((idx = ll_name_to_index(odev)) == 0) { 1444 fprintf(stderr, "Cannot find device \"%s\"\n", odev); 1445 return -1; 1446 } 1447 addattr32(&req.n, sizeof(req), RTA_OIF, idx); 1448 } 1449 } 1450 if (mark) 1451 addattr32(&req.n, sizeof(req), RTA_MARK, mark); 1452 1453 if (req.r.rtm_family == AF_UNSPEC) 1454 req.r.rtm_family = AF_INET; 1455 1456 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) 1457 exit(2); 1458 1459 if (connected && !from_ok) { 1460 struct rtmsg *r = NLMSG_DATA(&req.n); 1461 int len = req.n.nlmsg_len; 1462 struct rtattr * tb[RTA_MAX+1]; 1463 1464 if (print_route(NULL, &req.n, (void*)stdout) < 0) { 1465 fprintf(stderr, "An error :-)\n"); 1466 exit(1); 1467 } 1468 1469 if (req.n.nlmsg_type != RTM_NEWROUTE) { 1470 fprintf(stderr, "Not a route?\n"); 1471 return -1; 1472 } 1473 len -= NLMSG_LENGTH(sizeof(*r)); 1474 if (len < 0) { 1475 fprintf(stderr, "Wrong len %d\n", len); 1476 return -1; 1477 } 1478 1479 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); 1480 1481 if (tb[RTA_PREFSRC]) { 1482 tb[RTA_PREFSRC]->rta_type = RTA_SRC; 1483 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]); 1484 } else if (!tb[RTA_SRC]) { 1485 fprintf(stderr, "Failed to connect the route\n"); 1486 return -1; 1487 } 1488 if (!odev && tb[RTA_OIF]) 1489 tb[RTA_OIF]->rta_type = 0; 1490 if (tb[RTA_GATEWAY]) 1491 tb[RTA_GATEWAY]->rta_type = 0; 1492 if (!idev && tb[RTA_IIF]) 1493 tb[RTA_IIF]->rta_type = 0; 1494 req.n.nlmsg_flags = NLM_F_REQUEST; 1495 req.n.nlmsg_type = RTM_GETROUTE; 1496 1497 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) 1498 exit(2); 1499 } 1500 1501 if (print_route(NULL, &req.n, (void*)stdout) < 0) { 1502 fprintf(stderr, "An error :-)\n"); 1503 exit(1); 1504 } 1505 1506 exit(0); 1507 } 1508 1509 int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) 1510 { 1511 int ret; 1512 1513 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; 1514 1515 ll_init_map(&rth); 1516 1517 ret = rtnl_talk(&rth, n, 0, 0, n); 1518 if ((ret < 0) && (errno == EEXIST)) 1519 ret = 0; 1520 1521 return ret; 1522 } 1523 1524 int iproute_restore(void) 1525 { 1526 exit(rtnl_from_file(stdin, &restore_handler, NULL)); 1527 } 1528 1529 void iproute_reset_filter() 1530 { 1531 memset(&filter, 0, sizeof(filter)); 1532 filter.mdst.bitlen = -1; 1533 filter.msrc.bitlen = -1; 1534 } 1535 1536 int do_iproute(int argc, char **argv) 1537 { 1538 if (argc < 1) 1539 return iproute_list_flush_or_save(0, NULL, IPROUTE_LIST); 1540 1541 if (matches(*argv, "add") == 0) 1542 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL, 1543 argc-1, argv+1); 1544 if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0) 1545 return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE, 1546 argc-1, argv+1); 1547 if (matches(*argv, "replace") == 0) 1548 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE, 1549 argc-1, argv+1); 1550 if (matches(*argv, "prepend") == 0) 1551 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE, 1552 argc-1, argv+1); 1553 if (matches(*argv, "append") == 0) 1554 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND, 1555 argc-1, argv+1); 1556 if (matches(*argv, "test") == 0) 1557 return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL, 1558 argc-1, argv+1); 1559 if (matches(*argv, "delete") == 0) 1560 return iproute_modify(RTM_DELROUTE, 0, 1561 argc-1, argv+1); 1562 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 1563 || matches(*argv, "lst") == 0) 1564 return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_LIST); 1565 if (matches(*argv, "get") == 0) 1566 return iproute_get(argc-1, argv+1); 1567 if (matches(*argv, "flush") == 0) 1568 return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_FLUSH); 1569 if (matches(*argv, "save") == 0) 1570 return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_SAVE); 1571 if (matches(*argv, "restore") == 0) 1572 return iproute_restore(); 1573 if (matches(*argv, "help") == 0) 1574 usage(); 1575 fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv); 1576 exit(-1); 1577 } 1578 1579