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 ] [ uid 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_UID]) 427 fprintf(fp, " uid %u ", rta_getattr_u32(tb[RTA_UID])); 428 429 if (tb[RTA_FLOW] && filter.realmmask != ~0U) { 430 __u32 to = rta_getattr_u32(tb[RTA_FLOW]); 431 __u32 from = to>>16; 432 to &= 0xFFFF; 433 fprintf(fp, "realm%s ", from ? "s" : ""); 434 if (from) { 435 fprintf(fp, "%s/", 436 rtnl_rtrealm_n2a(from, b1, sizeof(b1))); 437 } 438 fprintf(fp, "%s ", 439 rtnl_rtrealm_n2a(to, b1, sizeof(b1))); 440 } 441 if ((r->rtm_flags&RTM_F_CLONED) && r->rtm_family == AF_INET) { 442 __u32 flags = r->rtm_flags&~0xFFFF; 443 int first = 1; 444 445 fprintf(fp, "%s cache ", _SL_); 446 447 #define PRTFL(fl,flname) if (flags&RTCF_##fl) { \ 448 flags &= ~RTCF_##fl; \ 449 fprintf(fp, "%s" flname "%s", first ? "<" : "", flags ? "," : "> "); \ 450 first = 0; } 451 PRTFL(LOCAL, "local"); 452 PRTFL(REJECT, "reject"); 453 PRTFL(MULTICAST, "mc"); 454 PRTFL(BROADCAST, "brd"); 455 PRTFL(DNAT, "dst-nat"); 456 PRTFL(SNAT, "src-nat"); 457 PRTFL(MASQ, "masq"); 458 PRTFL(DIRECTDST, "dst-direct"); 459 PRTFL(DIRECTSRC, "src-direct"); 460 PRTFL(REDIRECTED, "redirected"); 461 PRTFL(DOREDIRECT, "redirect"); 462 PRTFL(FAST, "fastroute"); 463 PRTFL(NOTIFY, "notify"); 464 PRTFL(TPROXY, "proxy"); 465 466 if (flags) 467 fprintf(fp, "%s%x> ", first ? "<" : "", flags); 468 if (tb[RTA_CACHEINFO]) { 469 struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]); 470 if (!hz) 471 hz = get_user_hz(); 472 if (ci->rta_expires != 0) 473 fprintf(fp, " expires %dsec", ci->rta_expires/hz); 474 if (ci->rta_error != 0) 475 fprintf(fp, " error %d", ci->rta_error); 476 if (show_stats) { 477 if (ci->rta_clntref) 478 fprintf(fp, " users %d", ci->rta_clntref); 479 if (ci->rta_used != 0) 480 fprintf(fp, " used %d", ci->rta_used); 481 if (ci->rta_lastuse != 0) 482 fprintf(fp, " age %dsec", ci->rta_lastuse/hz); 483 } 484 if (ci->rta_id) 485 fprintf(fp, " ipid 0x%04x", ci->rta_id); 486 if (ci->rta_ts || ci->rta_tsage) 487 fprintf(fp, " ts 0x%x tsage %dsec", 488 ci->rta_ts, ci->rta_tsage); 489 } 490 } else if (r->rtm_family == AF_INET6) { 491 struct rta_cacheinfo *ci = NULL; 492 if (tb[RTA_CACHEINFO]) 493 ci = RTA_DATA(tb[RTA_CACHEINFO]); 494 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) { 495 if (!hz) 496 hz = get_user_hz(); 497 if (r->rtm_flags & RTM_F_CLONED) 498 fprintf(fp, "%s cache ", _SL_); 499 if (ci->rta_expires) 500 fprintf(fp, " expires %dsec", ci->rta_expires/hz); 501 if (ci->rta_error != 0) 502 fprintf(fp, " error %d", ci->rta_error); 503 if (show_stats) { 504 if (ci->rta_clntref) 505 fprintf(fp, " users %d", ci->rta_clntref); 506 if (ci->rta_used != 0) 507 fprintf(fp, " used %d", ci->rta_used); 508 if (ci->rta_lastuse != 0) 509 fprintf(fp, " age %dsec", ci->rta_lastuse/hz); 510 } 511 } else if (ci) { 512 if (ci->rta_error != 0) 513 fprintf(fp, " error %d", ci->rta_error); 514 } 515 } 516 if (tb[RTA_METRICS]) { 517 int i; 518 unsigned mxlock = 0; 519 struct rtattr *mxrta[RTAX_MAX+1]; 520 521 parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), 522 RTA_PAYLOAD(tb[RTA_METRICS])); 523 if (mxrta[RTAX_LOCK]) 524 mxlock = *(unsigned*)RTA_DATA(mxrta[RTAX_LOCK]); 525 526 for (i=2; i<= RTAX_MAX; i++) { 527 unsigned val; 528 529 if (mxrta[i] == NULL) 530 continue; 531 532 if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i]) 533 fprintf(fp, " %s", mx_names[i]); 534 else 535 fprintf(fp, " metric %d", i); 536 if (mxlock & (1<<i)) 537 fprintf(fp, " lock"); 538 539 val = *(unsigned*)RTA_DATA(mxrta[i]); 540 switch (i) { 541 case RTAX_HOPLIMIT: 542 if ((int)val == -1) 543 val = 0; 544 /* fall through */ 545 default: 546 fprintf(fp, " %u", val); 547 break; 548 549 case RTAX_RTT: 550 case RTAX_RTTVAR: 551 case RTAX_RTO_MIN: 552 if (i == RTAX_RTT) 553 val /= 8; 554 else if (i == RTAX_RTTVAR) 555 val /= 4; 556 557 if (val >= 1000) 558 fprintf(fp, " %gs", val/1e3); 559 else 560 fprintf(fp, " %ums", val); 561 } 562 } 563 } 564 if (tb[RTA_IIF] && filter.iifmask != -1) { 565 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF]))); 566 } 567 if (tb[RTA_MULTIPATH]) { 568 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]); 569 int first = 0; 570 571 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); 572 573 for (;;) { 574 if (len < sizeof(*nh)) 575 break; 576 if (nh->rtnh_len > len) 577 break; 578 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) { 579 if (first) 580 fprintf(fp, " Oifs:"); 581 else 582 fprintf(fp, " "); 583 } else 584 fprintf(fp, "%s\tnexthop", _SL_); 585 if (nh->rtnh_len > sizeof(*nh)) { 586 parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh)); 587 if (tb[RTA_GATEWAY]) { 588 fprintf(fp, " via %s ", 589 format_host(r->rtm_family, 590 RTA_PAYLOAD(tb[RTA_GATEWAY]), 591 RTA_DATA(tb[RTA_GATEWAY]), 592 abuf, sizeof(abuf))); 593 } 594 if (tb[RTA_FLOW]) { 595 __u32 to = rta_getattr_u32(tb[RTA_FLOW]); 596 __u32 from = to>>16; 597 to &= 0xFFFF; 598 fprintf(fp, " realm%s ", from ? "s" : ""); 599 if (from) { 600 fprintf(fp, "%s/", 601 rtnl_rtrealm_n2a(from, b1, sizeof(b1))); 602 } 603 fprintf(fp, "%s", 604 rtnl_rtrealm_n2a(to, b1, sizeof(b1))); 605 } 606 } 607 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) { 608 fprintf(fp, " %s", ll_index_to_name(nh->rtnh_ifindex)); 609 if (nh->rtnh_hops != 1) 610 fprintf(fp, "(ttl>%d)", nh->rtnh_hops); 611 } else { 612 fprintf(fp, " dev %s", ll_index_to_name(nh->rtnh_ifindex)); 613 fprintf(fp, " weight %d", nh->rtnh_hops+1); 614 } 615 if (nh->rtnh_flags & RTNH_F_DEAD) 616 fprintf(fp, " dead"); 617 if (nh->rtnh_flags & RTNH_F_ONLINK) 618 fprintf(fp, " onlink"); 619 if (nh->rtnh_flags & RTNH_F_PERVASIVE) 620 fprintf(fp, " pervasive"); 621 len -= NLMSG_ALIGN(nh->rtnh_len); 622 nh = RTNH_NEXT(nh); 623 } 624 } 625 fprintf(fp, "\n"); 626 fflush(fp); 627 return 0; 628 } 629 630 631 int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char ***argvp) 632 { 633 int argc = *argcp; 634 char **argv = *argvp; 635 636 while (++argv, --argc > 0) { 637 if (strcmp(*argv, "via") == 0) { 638 NEXT_ARG(); 639 rta_addattr32(rta, 4096, RTA_GATEWAY, get_addr32(*argv)); 640 rtnh->rtnh_len += sizeof(struct rtattr) + 4; 641 } else if (strcmp(*argv, "dev") == 0) { 642 NEXT_ARG(); 643 if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) { 644 fprintf(stderr, "Cannot find device \"%s\"\n", *argv); 645 exit(1); 646 } 647 } else if (strcmp(*argv, "weight") == 0) { 648 unsigned w; 649 NEXT_ARG(); 650 if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256) 651 invarg("\"weight\" is invalid\n", *argv); 652 rtnh->rtnh_hops = w - 1; 653 } else if (strcmp(*argv, "onlink") == 0) { 654 rtnh->rtnh_flags |= RTNH_F_ONLINK; 655 } else if (matches(*argv, "realms") == 0) { 656 __u32 realm; 657 NEXT_ARG(); 658 if (get_rt_realms(&realm, *argv)) 659 invarg("\"realm\" value is invalid\n", *argv); 660 rta_addattr32(rta, 4096, RTA_FLOW, realm); 661 rtnh->rtnh_len += sizeof(struct rtattr) + 4; 662 } else 663 break; 664 } 665 *argcp = argc; 666 *argvp = argv; 667 return 0; 668 } 669 670 int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, int argc, char **argv) 671 { 672 char buf[1024]; 673 struct rtattr *rta = (void*)buf; 674 struct rtnexthop *rtnh; 675 676 rta->rta_type = RTA_MULTIPATH; 677 rta->rta_len = RTA_LENGTH(0); 678 rtnh = RTA_DATA(rta); 679 680 while (argc > 0) { 681 if (strcmp(*argv, "nexthop") != 0) { 682 fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv); 683 exit(-1); 684 } 685 if (argc <= 1) { 686 fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n"); 687 exit(-1); 688 } 689 memset(rtnh, 0, sizeof(*rtnh)); 690 rtnh->rtnh_len = sizeof(*rtnh); 691 rta->rta_len += rtnh->rtnh_len; 692 parse_one_nh(rta, rtnh, &argc, &argv); 693 rtnh = RTNH_NEXT(rtnh); 694 } 695 696 if (rta->rta_len > RTA_LENGTH(0)) 697 addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta)); 698 return 0; 699 } 700 701 702 int iproute_modify(int cmd, unsigned flags, int argc, char **argv) 703 { 704 struct { 705 struct nlmsghdr n; 706 struct rtmsg r; 707 char buf[1024]; 708 } req; 709 char mxbuf[256]; 710 struct rtattr * mxrta = (void*)mxbuf; 711 unsigned mxlock = 0; 712 char *d = NULL; 713 int gw_ok = 0; 714 int dst_ok = 0; 715 int nhs_ok = 0; 716 int scope_ok = 0; 717 int table_ok = 0; 718 int raw = 0; 719 720 memset(&req, 0, sizeof(req)); 721 722 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); 723 req.n.nlmsg_flags = NLM_F_REQUEST|flags; 724 req.n.nlmsg_type = cmd; 725 req.r.rtm_family = preferred_family; 726 req.r.rtm_table = RT_TABLE_MAIN; 727 req.r.rtm_scope = RT_SCOPE_NOWHERE; 728 729 if (cmd != RTM_DELROUTE) { 730 req.r.rtm_protocol = RTPROT_BOOT; 731 req.r.rtm_scope = RT_SCOPE_UNIVERSE; 732 req.r.rtm_type = RTN_UNICAST; 733 } 734 735 mxrta->rta_type = RTA_METRICS; 736 mxrta->rta_len = RTA_LENGTH(0); 737 738 while (argc > 0) { 739 if (strcmp(*argv, "src") == 0) { 740 inet_prefix addr; 741 NEXT_ARG(); 742 get_addr(&addr, *argv, req.r.rtm_family); 743 if (req.r.rtm_family == AF_UNSPEC) 744 req.r.rtm_family = addr.family; 745 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen); 746 } else if (strcmp(*argv, "via") == 0) { 747 inet_prefix addr; 748 gw_ok = 1; 749 NEXT_ARG(); 750 get_addr(&addr, *argv, req.r.rtm_family); 751 if (req.r.rtm_family == AF_UNSPEC) 752 req.r.rtm_family = addr.family; 753 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen); 754 } else if (strcmp(*argv, "from") == 0) { 755 inet_prefix addr; 756 NEXT_ARG(); 757 get_prefix(&addr, *argv, req.r.rtm_family); 758 if (req.r.rtm_family == AF_UNSPEC) 759 req.r.rtm_family = addr.family; 760 if (addr.bytelen) 761 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); 762 req.r.rtm_src_len = addr.bitlen; 763 } else if (strcmp(*argv, "tos") == 0 || 764 matches(*argv, "dsfield") == 0) { 765 __u32 tos; 766 NEXT_ARG(); 767 if (rtnl_dsfield_a2n(&tos, *argv)) 768 invarg("\"tos\" value is invalid\n", *argv); 769 req.r.rtm_tos = tos; 770 } else if (matches(*argv, "metric") == 0 || 771 matches(*argv, "priority") == 0 || 772 matches(*argv, "preference") == 0) { 773 __u32 metric; 774 NEXT_ARG(); 775 if (get_u32(&metric, *argv, 0)) 776 invarg("\"metric\" value is invalid\n", *argv); 777 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric); 778 } else if (strcmp(*argv, "scope") == 0) { 779 __u32 scope = 0; 780 NEXT_ARG(); 781 if (rtnl_rtscope_a2n(&scope, *argv)) 782 invarg("invalid \"scope\" value\n", *argv); 783 req.r.rtm_scope = scope; 784 scope_ok = 1; 785 } else if (strcmp(*argv, "mtu") == 0) { 786 unsigned mtu; 787 NEXT_ARG(); 788 if (strcmp(*argv, "lock") == 0) { 789 mxlock |= (1<<RTAX_MTU); 790 NEXT_ARG(); 791 } 792 if (get_unsigned(&mtu, *argv, 0)) 793 invarg("\"mtu\" value is invalid\n", *argv); 794 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu); 795 } else if (strcmp(*argv, "hoplimit") == 0) { 796 unsigned hoplimit; 797 NEXT_ARG(); 798 if (strcmp(*argv, "lock") == 0) { 799 mxlock |= (1<<RTAX_HOPLIMIT); 800 NEXT_ARG(); 801 } 802 if (get_unsigned(&hoplimit, *argv, 0)) 803 invarg("\"hoplimit\" value is invalid\n", *argv); 804 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit); 805 } else if (strcmp(*argv, "advmss") == 0) { 806 unsigned mss; 807 NEXT_ARG(); 808 if (strcmp(*argv, "lock") == 0) { 809 mxlock |= (1<<RTAX_ADVMSS); 810 NEXT_ARG(); 811 } 812 if (get_unsigned(&mss, *argv, 0)) 813 invarg("\"mss\" value is invalid\n", *argv); 814 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss); 815 } else if (matches(*argv, "reordering") == 0) { 816 unsigned reord; 817 NEXT_ARG(); 818 if (strcmp(*argv, "lock") == 0) { 819 mxlock |= (1<<RTAX_REORDERING); 820 NEXT_ARG(); 821 } 822 if (get_unsigned(&reord, *argv, 0)) 823 invarg("\"reordering\" value is invalid\n", *argv); 824 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord); 825 } else if (strcmp(*argv, "rtt") == 0) { 826 unsigned rtt; 827 NEXT_ARG(); 828 if (strcmp(*argv, "lock") == 0) { 829 mxlock |= (1<<RTAX_RTT); 830 NEXT_ARG(); 831 } 832 if (get_time_rtt(&rtt, *argv, &raw)) 833 invarg("\"rtt\" value is invalid\n", *argv); 834 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, 835 (raw) ? rtt : rtt * 8); 836 } else if (strcmp(*argv, "rto_min") == 0) { 837 unsigned rto_min; 838 NEXT_ARG(); 839 mxlock |= (1<<RTAX_RTO_MIN); 840 if (get_time_rtt(&rto_min, *argv, &raw)) 841 invarg("\"rto_min\" value is invalid\n", 842 *argv); 843 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN, 844 rto_min); 845 } else if (matches(*argv, "window") == 0) { 846 unsigned win; 847 NEXT_ARG(); 848 if (strcmp(*argv, "lock") == 0) { 849 mxlock |= (1<<RTAX_WINDOW); 850 NEXT_ARG(); 851 } 852 if (get_unsigned(&win, *argv, 0)) 853 invarg("\"window\" value is invalid\n", *argv); 854 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win); 855 } else if (matches(*argv, "cwnd") == 0) { 856 unsigned win; 857 NEXT_ARG(); 858 if (strcmp(*argv, "lock") == 0) { 859 mxlock |= (1<<RTAX_CWND); 860 NEXT_ARG(); 861 } 862 if (get_unsigned(&win, *argv, 0)) 863 invarg("\"cwnd\" value is invalid\n", *argv); 864 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win); 865 } else if (matches(*argv, "initcwnd") == 0) { 866 unsigned win; 867 NEXT_ARG(); 868 if (strcmp(*argv, "lock") == 0) { 869 mxlock |= (1<<RTAX_INITCWND); 870 NEXT_ARG(); 871 } 872 if (get_unsigned(&win, *argv, 0)) 873 invarg("\"initcwnd\" value is invalid\n", *argv); 874 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win); 875 } else if (matches(*argv, "initrwnd") == 0) { 876 unsigned win; 877 NEXT_ARG(); 878 if (strcmp(*argv, "lock") == 0) { 879 mxlock |= (1<<RTAX_INITRWND); 880 NEXT_ARG(); 881 } 882 if (get_unsigned(&win, *argv, 0)) 883 invarg("\"initrwnd\" value is invalid\n", *argv); 884 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win); 885 } else if (matches(*argv, "rttvar") == 0) { 886 unsigned win; 887 NEXT_ARG(); 888 if (strcmp(*argv, "lock") == 0) { 889 mxlock |= (1<<RTAX_RTTVAR); 890 NEXT_ARG(); 891 } 892 if (get_time_rtt(&win, *argv, &raw)) 893 invarg("\"rttvar\" value is invalid\n", *argv); 894 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR, 895 (raw) ? win : win * 4); 896 } else if (matches(*argv, "ssthresh") == 0) { 897 unsigned win; 898 NEXT_ARG(); 899 if (strcmp(*argv, "lock") == 0) { 900 mxlock |= (1<<RTAX_SSTHRESH); 901 NEXT_ARG(); 902 } 903 if (get_unsigned(&win, *argv, 0)) 904 invarg("\"ssthresh\" value is invalid\n", *argv); 905 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win); 906 } else if (matches(*argv, "realms") == 0) { 907 __u32 realm; 908 NEXT_ARG(); 909 if (get_rt_realms(&realm, *argv)) 910 invarg("\"realm\" value is invalid\n", *argv); 911 addattr32(&req.n, sizeof(req), RTA_FLOW, realm); 912 } else if (strcmp(*argv, "onlink") == 0) { 913 req.r.rtm_flags |= RTNH_F_ONLINK; 914 } else if (strcmp(*argv, "nexthop") == 0) { 915 nhs_ok = 1; 916 break; 917 } else if (matches(*argv, "protocol") == 0) { 918 __u32 prot; 919 NEXT_ARG(); 920 if (rtnl_rtprot_a2n(&prot, *argv)) 921 invarg("\"protocol\" value is invalid\n", *argv); 922 req.r.rtm_protocol = prot; 923 } else if (matches(*argv, "table") == 0) { 924 __u32 tid; 925 NEXT_ARG(); 926 if (rtnl_rttable_a2n(&tid, *argv)) 927 invarg("\"table\" value is invalid\n", *argv); 928 if (tid < 256) 929 req.r.rtm_table = tid; 930 else { 931 req.r.rtm_table = RT_TABLE_UNSPEC; 932 addattr32(&req.n, sizeof(req), RTA_TABLE, tid); 933 } 934 table_ok = 1; 935 } else if (strcmp(*argv, "dev") == 0 || 936 strcmp(*argv, "oif") == 0) { 937 NEXT_ARG(); 938 d = *argv; 939 } else { 940 int type; 941 inet_prefix dst; 942 943 if (strcmp(*argv, "to") == 0) { 944 NEXT_ARG(); 945 } 946 if ((**argv < '0' || **argv > '9') && 947 rtnl_rtntype_a2n(&type, *argv) == 0) { 948 NEXT_ARG(); 949 req.r.rtm_type = type; 950 } 951 952 if (matches(*argv, "help") == 0) 953 usage(); 954 if (dst_ok) 955 duparg2("to", *argv); 956 get_prefix(&dst, *argv, req.r.rtm_family); 957 if (req.r.rtm_family == AF_UNSPEC) 958 req.r.rtm_family = dst.family; 959 req.r.rtm_dst_len = dst.bitlen; 960 dst_ok = 1; 961 if (dst.bytelen) 962 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); 963 } 964 argc--; argv++; 965 } 966 967 if (d || nhs_ok) { 968 int idx; 969 970 ll_init_map(&rth); 971 972 if (d) { 973 if ((idx = ll_name_to_index(d)) == 0) { 974 fprintf(stderr, "Cannot find device \"%s\"\n", d); 975 return -1; 976 } 977 addattr32(&req.n, sizeof(req), RTA_OIF, idx); 978 } 979 } 980 981 if (mxrta->rta_len > RTA_LENGTH(0)) { 982 if (mxlock) 983 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock); 984 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta)); 985 } 986 987 if (nhs_ok) 988 parse_nexthops(&req.n, &req.r, argc, argv); 989 990 if (!table_ok) { 991 if (req.r.rtm_type == RTN_LOCAL || 992 req.r.rtm_type == RTN_BROADCAST || 993 req.r.rtm_type == RTN_NAT || 994 req.r.rtm_type == RTN_ANYCAST) 995 req.r.rtm_table = RT_TABLE_LOCAL; 996 } 997 if (!scope_ok) { 998 if (req.r.rtm_type == RTN_LOCAL || 999 req.r.rtm_type == RTN_NAT) 1000 req.r.rtm_scope = RT_SCOPE_HOST; 1001 else if (req.r.rtm_type == RTN_BROADCAST || 1002 req.r.rtm_type == RTN_MULTICAST || 1003 req.r.rtm_type == RTN_ANYCAST) 1004 req.r.rtm_scope = RT_SCOPE_LINK; 1005 else if (req.r.rtm_type == RTN_UNICAST || 1006 req.r.rtm_type == RTN_UNSPEC) { 1007 if (cmd == RTM_DELROUTE) 1008 req.r.rtm_scope = RT_SCOPE_NOWHERE; 1009 else if (!gw_ok && !nhs_ok) 1010 req.r.rtm_scope = RT_SCOPE_LINK; 1011 } 1012 } 1013 1014 if (req.r.rtm_family == AF_UNSPEC) 1015 req.r.rtm_family = AF_INET; 1016 1017 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) 1018 exit(2); 1019 1020 return 0; 1021 } 1022 1023 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family) 1024 { 1025 struct { 1026 struct nlmsghdr nlh; 1027 struct rtmsg rtm; 1028 } req; 1029 struct sockaddr_nl nladdr; 1030 1031 memset(&nladdr, 0, sizeof(nladdr)); 1032 memset(&req, 0, sizeof(req)); 1033 nladdr.nl_family = AF_NETLINK; 1034 1035 req.nlh.nlmsg_len = sizeof(req); 1036 req.nlh.nlmsg_type = RTM_GETROUTE; 1037 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST; 1038 req.nlh.nlmsg_pid = 0; 1039 req.nlh.nlmsg_seq = rth->dump = ++rth->seq; 1040 req.rtm.rtm_family = family; 1041 req.rtm.rtm_flags |= RTM_F_CLONED; 1042 1043 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr)); 1044 } 1045 1046 static int iproute_flush_cache(void) 1047 { 1048 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush" 1049 1050 int len; 1051 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY); 1052 char *buffer = "-1"; 1053 1054 if (flush_fd < 0) { 1055 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH); 1056 return -1; 1057 } 1058 1059 len = strlen (buffer); 1060 1061 if ((write (flush_fd, (void *)buffer, len)) < len) { 1062 fprintf (stderr, "Cannot flush routing cache\n"); 1063 close(flush_fd); 1064 return -1; 1065 } 1066 close(flush_fd); 1067 return 0; 1068 } 1069 1070 int save_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) 1071 { 1072 int ret; 1073 int len = n->nlmsg_len; 1074 struct rtmsg *r = NLMSG_DATA(n); 1075 struct rtattr *tb[RTA_MAX+1]; 1076 int host_len = -1; 1077 1078 if (isatty(STDOUT_FILENO)) { 1079 fprintf(stderr, "Not sending binary stream to stdout\n"); 1080 return -1; 1081 } 1082 1083 host_len = calc_host_len(r); 1084 len -= NLMSG_LENGTH(sizeof(*r)); 1085 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); 1086 1087 if (!filter_nlmsg(n, tb, host_len)) 1088 return 0; 1089 1090 ret = write(STDOUT_FILENO, n, n->nlmsg_len); 1091 if ((ret > 0) && (ret != n->nlmsg_len)) { 1092 fprintf(stderr, "Short write while saving nlmsg\n"); 1093 ret = -EIO; 1094 } 1095 1096 return ret == n->nlmsg_len ? 0 : ret; 1097 } 1098 1099 static int iproute_list_flush_or_save(int argc, char **argv, int action) 1100 { 1101 int do_ipv6 = preferred_family; 1102 char *id = NULL; 1103 char *od = NULL; 1104 unsigned int mark = 0; 1105 rtnl_filter_t filter_fn; 1106 1107 if (action == IPROUTE_SAVE) 1108 filter_fn = save_route; 1109 else 1110 filter_fn = print_route; 1111 1112 iproute_reset_filter(); 1113 filter.tb = RT_TABLE_MAIN; 1114 1115 if ((action == IPROUTE_FLUSH) && argc <= 0) { 1116 fprintf(stderr, "\"ip route flush\" requires arguments.\n"); 1117 return -1; 1118 } 1119 1120 while (argc > 0) { 1121 if (matches(*argv, "table") == 0) { 1122 __u32 tid; 1123 NEXT_ARG(); 1124 if (rtnl_rttable_a2n(&tid, *argv)) { 1125 if (strcmp(*argv, "all") == 0) { 1126 filter.tb = 0; 1127 } else if (strcmp(*argv, "cache") == 0) { 1128 filter.cloned = 1; 1129 } else if (strcmp(*argv, "help") == 0) { 1130 usage(); 1131 } else { 1132 invarg("table id value is invalid\n", *argv); 1133 } 1134 } else 1135 filter.tb = tid; 1136 } else if (matches(*argv, "cached") == 0 || 1137 matches(*argv, "cloned") == 0) { 1138 filter.cloned = 1; 1139 } else if (strcmp(*argv, "tos") == 0 || 1140 matches(*argv, "dsfield") == 0) { 1141 __u32 tos; 1142 NEXT_ARG(); 1143 if (rtnl_dsfield_a2n(&tos, *argv)) 1144 invarg("TOS value is invalid\n", *argv); 1145 filter.tos = tos; 1146 filter.tosmask = -1; 1147 } else if (matches(*argv, "protocol") == 0) { 1148 __u32 prot = 0; 1149 NEXT_ARG(); 1150 filter.protocolmask = -1; 1151 if (rtnl_rtprot_a2n(&prot, *argv)) { 1152 if (strcmp(*argv, "all") != 0) 1153 invarg("invalid \"protocol\"\n", *argv); 1154 prot = 0; 1155 filter.protocolmask = 0; 1156 } 1157 filter.protocol = prot; 1158 } else if (matches(*argv, "scope") == 0) { 1159 __u32 scope = 0; 1160 NEXT_ARG(); 1161 filter.scopemask = -1; 1162 if (rtnl_rtscope_a2n(&scope, *argv)) { 1163 if (strcmp(*argv, "all") != 0) 1164 invarg("invalid \"scope\"\n", *argv); 1165 scope = RT_SCOPE_NOWHERE; 1166 filter.scopemask = 0; 1167 } 1168 filter.scope = scope; 1169 } else if (matches(*argv, "type") == 0) { 1170 int type; 1171 NEXT_ARG(); 1172 filter.typemask = -1; 1173 if (rtnl_rtntype_a2n(&type, *argv)) 1174 invarg("node type value is invalid\n", *argv); 1175 filter.type = type; 1176 } else if (strcmp(*argv, "dev") == 0 || 1177 strcmp(*argv, "oif") == 0) { 1178 NEXT_ARG(); 1179 od = *argv; 1180 } else if (strcmp(*argv, "iif") == 0) { 1181 NEXT_ARG(); 1182 id = *argv; 1183 } else if (strcmp(*argv, "mark") == 0) { 1184 NEXT_ARG(); 1185 get_unsigned(&mark, *argv, 0); 1186 filter.markmask = -1; 1187 } else if (strcmp(*argv, "via") == 0) { 1188 NEXT_ARG(); 1189 get_prefix(&filter.rvia, *argv, do_ipv6); 1190 } else if (strcmp(*argv, "src") == 0) { 1191 NEXT_ARG(); 1192 get_prefix(&filter.rprefsrc, *argv, do_ipv6); 1193 } else if (matches(*argv, "realms") == 0) { 1194 __u32 realm; 1195 NEXT_ARG(); 1196 if (get_rt_realms(&realm, *argv)) 1197 invarg("invalid realms\n", *argv); 1198 filter.realm = realm; 1199 filter.realmmask = ~0U; 1200 if ((filter.realm&0xFFFF) == 0 && 1201 (*argv)[strlen(*argv) - 1] == '/') 1202 filter.realmmask &= ~0xFFFF; 1203 if ((filter.realm&0xFFFF0000U) == 0 && 1204 (strchr(*argv, '/') == NULL || 1205 (*argv)[0] == '/')) 1206 filter.realmmask &= ~0xFFFF0000U; 1207 } else if (matches(*argv, "from") == 0) { 1208 NEXT_ARG(); 1209 if (matches(*argv, "root") == 0) { 1210 NEXT_ARG(); 1211 get_prefix(&filter.rsrc, *argv, do_ipv6); 1212 } else if (matches(*argv, "match") == 0) { 1213 NEXT_ARG(); 1214 get_prefix(&filter.msrc, *argv, do_ipv6); 1215 } else { 1216 if (matches(*argv, "exact") == 0) { 1217 NEXT_ARG(); 1218 } 1219 get_prefix(&filter.msrc, *argv, do_ipv6); 1220 filter.rsrc = filter.msrc; 1221 } 1222 } else { 1223 if (matches(*argv, "to") == 0) { 1224 NEXT_ARG(); 1225 } 1226 if (matches(*argv, "root") == 0) { 1227 NEXT_ARG(); 1228 get_prefix(&filter.rdst, *argv, do_ipv6); 1229 } else if (matches(*argv, "match") == 0) { 1230 NEXT_ARG(); 1231 get_prefix(&filter.mdst, *argv, do_ipv6); 1232 } else { 1233 if (matches(*argv, "exact") == 0) { 1234 NEXT_ARG(); 1235 } 1236 get_prefix(&filter.mdst, *argv, do_ipv6); 1237 filter.rdst = filter.mdst; 1238 } 1239 } 1240 argc--; argv++; 1241 } 1242 1243 if (do_ipv6 == AF_UNSPEC && filter.tb) 1244 do_ipv6 = AF_INET; 1245 1246 ll_init_map(&rth); 1247 1248 if (id || od) { 1249 int idx; 1250 1251 if (id) { 1252 if ((idx = ll_name_to_index(id)) == 0) { 1253 fprintf(stderr, "Cannot find device \"%s\"\n", id); 1254 return -1; 1255 } 1256 filter.iif = idx; 1257 filter.iifmask = -1; 1258 } 1259 if (od) { 1260 if ((idx = ll_name_to_index(od)) == 0) { 1261 fprintf(stderr, "Cannot find device \"%s\"\n", od); 1262 return -1; 1263 } 1264 filter.oif = idx; 1265 filter.oifmask = -1; 1266 } 1267 } 1268 filter.mark = mark; 1269 1270 if (action == IPROUTE_FLUSH) { 1271 int round = 0; 1272 char flushb[4096-512]; 1273 time_t start = time(0); 1274 1275 if (filter.cloned) { 1276 if (do_ipv6 != AF_INET6) { 1277 iproute_flush_cache(); 1278 if (show_stats) 1279 printf("*** IPv4 routing cache is flushed.\n"); 1280 } 1281 if (do_ipv6 == AF_INET) 1282 return 0; 1283 } 1284 1285 filter.flushb = flushb; 1286 filter.flushp = 0; 1287 filter.flushe = sizeof(flushb); 1288 1289 for (;;) { 1290 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { 1291 perror("Cannot send dump request"); 1292 exit(1); 1293 } 1294 filter.flushed = 0; 1295 if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) { 1296 fprintf(stderr, "Flush terminated\n"); 1297 exit(1); 1298 } 1299 if (filter.flushed == 0) { 1300 if (show_stats) { 1301 if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6)) 1302 printf("Nothing to flush.\n"); 1303 else 1304 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); 1305 } 1306 fflush(stdout); 1307 return 0; 1308 } 1309 round++; 1310 if (flush_update() < 0) 1311 exit(1); 1312 1313 if (time(0) - start > 30) { 1314 printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n", 1315 time(0) - start, filter.flushed); 1316 exit(1); 1317 } 1318 1319 if (show_stats) { 1320 printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed); 1321 fflush(stdout); 1322 } 1323 } 1324 } 1325 1326 if (!filter.cloned) { 1327 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { 1328 perror("Cannot send dump request"); 1329 exit(1); 1330 } 1331 } else { 1332 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { 1333 perror("Cannot send dump request"); 1334 exit(1); 1335 } 1336 } 1337 1338 if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) { 1339 fprintf(stderr, "Dump terminated\n"); 1340 exit(1); 1341 } 1342 1343 exit(0); 1344 } 1345 1346 1347 int iproute_get(int argc, char **argv) 1348 { 1349 struct { 1350 struct nlmsghdr n; 1351 struct rtmsg r; 1352 char buf[1024]; 1353 } req; 1354 char *idev = NULL; 1355 char *odev = NULL; 1356 int connected = 0; 1357 int from_ok = 0; 1358 unsigned int mark = 0; 1359 1360 memset(&req, 0, sizeof(req)); 1361 1362 iproute_reset_filter(); 1363 filter.cloned = 2; 1364 1365 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); 1366 req.n.nlmsg_flags = NLM_F_REQUEST; 1367 req.n.nlmsg_type = RTM_GETROUTE; 1368 req.r.rtm_family = preferred_family; 1369 req.r.rtm_table = 0; 1370 req.r.rtm_protocol = 0; 1371 req.r.rtm_scope = 0; 1372 req.r.rtm_type = 0; 1373 req.r.rtm_src_len = 0; 1374 req.r.rtm_dst_len = 0; 1375 req.r.rtm_tos = 0; 1376 1377 while (argc > 0) { 1378 if (strcmp(*argv, "tos") == 0 || 1379 matches(*argv, "dsfield") == 0) { 1380 __u32 tos; 1381 NEXT_ARG(); 1382 if (rtnl_dsfield_a2n(&tos, *argv)) 1383 invarg("TOS value is invalid\n", *argv); 1384 req.r.rtm_tos = tos; 1385 } else if (matches(*argv, "from") == 0) { 1386 inet_prefix addr; 1387 NEXT_ARG(); 1388 if (matches(*argv, "help") == 0) 1389 usage(); 1390 from_ok = 1; 1391 get_prefix(&addr, *argv, req.r.rtm_family); 1392 if (req.r.rtm_family == AF_UNSPEC) 1393 req.r.rtm_family = addr.family; 1394 if (addr.bytelen) 1395 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); 1396 req.r.rtm_src_len = addr.bitlen; 1397 } else if (matches(*argv, "iif") == 0) { 1398 NEXT_ARG(); 1399 idev = *argv; 1400 } else if (matches(*argv, "mark") == 0) { 1401 NEXT_ARG(); 1402 get_unsigned(&mark, *argv, 0); 1403 } else if (matches(*argv, "oif") == 0 || 1404 strcmp(*argv, "dev") == 0) { 1405 NEXT_ARG(); 1406 odev = *argv; 1407 } else if (matches(*argv, "uid") == 0) { 1408 uid_t uid; 1409 NEXT_ARG(); 1410 get_unsigned(&uid, *argv, 0); 1411 addattr32(&req.n, sizeof(req), RTA_UID, uid); 1412 } else if (matches(*argv, "notify") == 0) { 1413 req.r.rtm_flags |= RTM_F_NOTIFY; 1414 } else if (matches(*argv, "connected") == 0) { 1415 connected = 1; 1416 } else { 1417 inet_prefix addr; 1418 if (strcmp(*argv, "to") == 0) { 1419 NEXT_ARG(); 1420 } 1421 if (matches(*argv, "help") == 0) 1422 usage(); 1423 get_prefix(&addr, *argv, req.r.rtm_family); 1424 if (req.r.rtm_family == AF_UNSPEC) 1425 req.r.rtm_family = addr.family; 1426 if (addr.bytelen) 1427 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen); 1428 req.r.rtm_dst_len = addr.bitlen; 1429 } 1430 argc--; argv++; 1431 } 1432 1433 if (req.r.rtm_dst_len == 0) { 1434 fprintf(stderr, "need at least destination address\n"); 1435 exit(1); 1436 } 1437 1438 ll_init_map(&rth); 1439 1440 if (idev || odev) { 1441 int idx; 1442 1443 if (idev) { 1444 if ((idx = ll_name_to_index(idev)) == 0) { 1445 fprintf(stderr, "Cannot find device \"%s\"\n", idev); 1446 return -1; 1447 } 1448 addattr32(&req.n, sizeof(req), RTA_IIF, idx); 1449 } 1450 if (odev) { 1451 if ((idx = ll_name_to_index(odev)) == 0) { 1452 fprintf(stderr, "Cannot find device \"%s\"\n", odev); 1453 return -1; 1454 } 1455 addattr32(&req.n, sizeof(req), RTA_OIF, idx); 1456 } 1457 } 1458 if (mark) 1459 addattr32(&req.n, sizeof(req), RTA_MARK, mark); 1460 1461 if (req.r.rtm_family == AF_UNSPEC) 1462 req.r.rtm_family = AF_INET; 1463 1464 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) 1465 exit(2); 1466 1467 if (connected && !from_ok) { 1468 struct rtmsg *r = NLMSG_DATA(&req.n); 1469 int len = req.n.nlmsg_len; 1470 struct rtattr * tb[RTA_MAX+1]; 1471 1472 if (print_route(NULL, &req.n, (void*)stdout) < 0) { 1473 fprintf(stderr, "An error :-)\n"); 1474 exit(1); 1475 } 1476 1477 if (req.n.nlmsg_type != RTM_NEWROUTE) { 1478 fprintf(stderr, "Not a route?\n"); 1479 return -1; 1480 } 1481 len -= NLMSG_LENGTH(sizeof(*r)); 1482 if (len < 0) { 1483 fprintf(stderr, "Wrong len %d\n", len); 1484 return -1; 1485 } 1486 1487 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); 1488 1489 if (tb[RTA_PREFSRC]) { 1490 tb[RTA_PREFSRC]->rta_type = RTA_SRC; 1491 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]); 1492 } else if (!tb[RTA_SRC]) { 1493 fprintf(stderr, "Failed to connect the route\n"); 1494 return -1; 1495 } 1496 if (!odev && tb[RTA_OIF]) 1497 tb[RTA_OIF]->rta_type = 0; 1498 if (tb[RTA_GATEWAY]) 1499 tb[RTA_GATEWAY]->rta_type = 0; 1500 if (!idev && tb[RTA_IIF]) 1501 tb[RTA_IIF]->rta_type = 0; 1502 req.n.nlmsg_flags = NLM_F_REQUEST; 1503 req.n.nlmsg_type = RTM_GETROUTE; 1504 1505 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) 1506 exit(2); 1507 } 1508 1509 if (print_route(NULL, &req.n, (void*)stdout) < 0) { 1510 fprintf(stderr, "An error :-)\n"); 1511 exit(1); 1512 } 1513 1514 exit(0); 1515 } 1516 1517 int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) 1518 { 1519 int ret; 1520 1521 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; 1522 1523 ll_init_map(&rth); 1524 1525 ret = rtnl_talk(&rth, n, 0, 0, n); 1526 if ((ret < 0) && (errno == EEXIST)) 1527 ret = 0; 1528 1529 return ret; 1530 } 1531 1532 int iproute_restore(void) 1533 { 1534 exit(rtnl_from_file(stdin, &restore_handler, NULL)); 1535 } 1536 1537 void iproute_reset_filter() 1538 { 1539 memset(&filter, 0, sizeof(filter)); 1540 filter.mdst.bitlen = -1; 1541 filter.msrc.bitlen = -1; 1542 } 1543 1544 int do_iproute(int argc, char **argv) 1545 { 1546 if (argc < 1) 1547 return iproute_list_flush_or_save(0, NULL, IPROUTE_LIST); 1548 1549 if (matches(*argv, "add") == 0) 1550 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL, 1551 argc-1, argv+1); 1552 if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0) 1553 return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE, 1554 argc-1, argv+1); 1555 if (matches(*argv, "replace") == 0) 1556 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE, 1557 argc-1, argv+1); 1558 if (matches(*argv, "prepend") == 0) 1559 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE, 1560 argc-1, argv+1); 1561 if (matches(*argv, "append") == 0) 1562 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND, 1563 argc-1, argv+1); 1564 if (matches(*argv, "test") == 0) 1565 return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL, 1566 argc-1, argv+1); 1567 if (matches(*argv, "delete") == 0) 1568 return iproute_modify(RTM_DELROUTE, 0, 1569 argc-1, argv+1); 1570 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 1571 || matches(*argv, "lst") == 0) 1572 return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_LIST); 1573 if (matches(*argv, "get") == 0) 1574 return iproute_get(argc-1, argv+1); 1575 if (matches(*argv, "flush") == 0) 1576 return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_FLUSH); 1577 if (matches(*argv, "save") == 0) 1578 return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_SAVE); 1579 if (matches(*argv, "restore") == 0) 1580 return iproute_restore(); 1581 if (matches(*argv, "help") == 0) 1582 usage(); 1583 fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv); 1584 exit(-1); 1585 } 1586 1587