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