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