Bernhard Reutner-Fischer | d1d23a6 | 2006-01-12 12:08:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * iproute.c "ip route". |
| 4 | * |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 5 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
| 7 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 8 | * |
| 9 | * |
| 10 | * Changes: |
| 11 | * |
| 12 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
| 13 | * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized |
| 14 | */ |
| 15 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 16 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | #include "rt_names.h" |
| 18 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 19 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | #ifndef RTAX_RTTVAR |
| 21 | #define RTAX_RTTVAR RTAX_HOPS |
| 22 | #endif |
| 23 | |
| 24 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 25 | typedef struct filter_t { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 26 | int tb; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 27 | int flushed; |
| 28 | char *flushb; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 29 | int flushp; |
| 30 | int flushe; |
| 31 | struct rtnl_handle *rth; |
| 32 | int protocol, protocolmask; |
| 33 | int scope, scopemask; |
| 34 | int type, typemask; |
| 35 | int tos, tosmask; |
| 36 | int iif, iifmask; |
| 37 | int oif, oifmask; |
| 38 | int realm, realmmask; |
| 39 | inet_prefix rprefsrc; |
| 40 | inet_prefix rvia; |
| 41 | inet_prefix rdst; |
| 42 | inet_prefix mdst; |
| 43 | inet_prefix rsrc; |
| 44 | inet_prefix msrc; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 45 | } filter_t; |
| 46 | |
| 47 | #define filter (*(filter_t*)&bb_common_bufsiz1) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 48 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 49 | static int flush_update(void) |
| 50 | { |
| 51 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 52 | bb_perror_msg("failed to send flush request"); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | filter.flushp = 0; |
| 56 | return 0; |
| 57 | } |
| 58 | |
Denis Vlasenko | c519320 | 2006-12-31 19:07:23 +0000 | [diff] [blame] | 59 | static unsigned get_hz(void) |
Denis Vlasenko | 13463af | 2006-12-31 18:58:32 +0000 | [diff] [blame] | 60 | { |
Denis Vlasenko | c519320 | 2006-12-31 19:07:23 +0000 | [diff] [blame] | 61 | static unsigned hz_internal; |
Denis Vlasenko | 13463af | 2006-12-31 18:58:32 +0000 | [diff] [blame] | 62 | FILE *fp; |
| 63 | |
| 64 | if (hz_internal) |
| 65 | return hz_internal; |
| 66 | |
| 67 | fp = fopen("/proc/net/psched", "r"); |
| 68 | if (fp) { |
| 69 | unsigned nom, denom; |
| 70 | |
| 71 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) |
| 72 | if (nom == 1000000) |
| 73 | hz_internal = denom; |
| 74 | fclose(fp); |
| 75 | } |
| 76 | if (!hz_internal) |
| 77 | hz_internal = sysconf(_SC_CLK_TCK); |
| 78 | return hz_internal; |
| 79 | } |
| 80 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 81 | static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED, |
Bernhard Reutner-Fischer | d1d23a6 | 2006-01-12 12:08:46 +0000 | [diff] [blame] | 82 | struct nlmsghdr *n, void *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 83 | { |
| 84 | FILE *fp = (FILE*)arg; |
| 85 | struct rtmsg *r = NLMSG_DATA(n); |
| 86 | int len = n->nlmsg_len; |
| 87 | struct rtattr * tb[RTA_MAX+1]; |
| 88 | char abuf[256]; |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 89 | inet_prefix dst; |
| 90 | inet_prefix src; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | int host_len = -1; |
| 92 | SPRINT_BUF(b1); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 93 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 94 | if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) { |
| 95 | fprintf(stderr, "Not a route: %08x %08x %08x\n", |
| 96 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 97 | return 0; |
| 98 | } |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 99 | if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE) |
| 100 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 101 | len -= NLMSG_LENGTH(sizeof(*r)); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 102 | if (len < 0) |
| 103 | bb_error_msg_and_die("wrong nlmsg len %d", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 104 | |
| 105 | if (r->rtm_family == AF_INET6) |
| 106 | host_len = 128; |
| 107 | else if (r->rtm_family == AF_INET) |
| 108 | host_len = 32; |
| 109 | |
| 110 | if (r->rtm_family == AF_INET6) { |
| 111 | if (filter.tb) { |
| 112 | if (filter.tb < 0) { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 113 | if (!(r->rtm_flags & RTM_F_CLONED)) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 114 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 115 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 116 | } else { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 117 | if (r->rtm_flags & RTM_F_CLONED) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 118 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 119 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 120 | if (filter.tb == RT_TABLE_LOCAL) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 121 | if (r->rtm_type != RTN_LOCAL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 122 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 123 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 124 | } else if (filter.tb == RT_TABLE_MAIN) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 125 | if (r->rtm_type == RTN_LOCAL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 127 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 128 | } else { |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } else { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 134 | if (filter.tb > 0 && filter.tb != r->rtm_table) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 135 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 136 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 137 | } |
| 138 | if (filter.rdst.family && |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 139 | (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 141 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 142 | if (filter.mdst.family && |
| 143 | (r->rtm_family != filter.mdst.family || |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 144 | (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 145 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 146 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 147 | if (filter.rsrc.family && |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 148 | (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 149 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 150 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 151 | if (filter.msrc.family && |
| 152 | (r->rtm_family != filter.msrc.family || |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 153 | (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 154 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 155 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 156 | |
| 157 | memset(tb, 0, sizeof(tb)); |
| 158 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 159 | |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 160 | if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen)) |
| 161 | return 0; |
| 162 | if (filter.mdst.family && filter.mdst.bitlen >= 0 && |
| 163 | inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len)) |
| 164 | return 0; |
| 165 | |
| 166 | if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen)) |
| 167 | return 0; |
| 168 | if (filter.msrc.family && filter.msrc.bitlen >= 0 && |
| 169 | inet_addr_match(&src, &filter.msrc, r->rtm_src_len)) |
| 170 | return 0; |
| 171 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 172 | if (filter.flushb && |
| 173 | r->rtm_family == AF_INET6 && |
| 174 | r->rtm_dst_len == 0 && |
| 175 | r->rtm_type == RTN_UNREACHABLE && |
| 176 | tb[RTA_PRIORITY] && |
| 177 | *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1) |
| 178 | return 0; |
| 179 | |
| 180 | if (filter.flushb) { |
| 181 | struct nlmsghdr *fn; |
| 182 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 183 | if (flush_update()) |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 184 | bb_error_msg_and_die("flush"); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 185 | } |
| 186 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 187 | memcpy(fn, n, n->nlmsg_len); |
| 188 | fn->nlmsg_type = RTM_DELROUTE; |
| 189 | fn->nlmsg_flags = NLM_F_REQUEST; |
| 190 | fn->nlmsg_seq = ++filter.rth->seq; |
| 191 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 192 | filter.flushed++; |
| 193 | return 0; |
| 194 | } |
| 195 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 196 | if (n->nlmsg_type == RTM_DELROUTE) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 197 | fprintf(fp, "Deleted "); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 198 | } |
| 199 | if (r->rtm_type != RTN_UNICAST && !filter.type) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 200 | fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 201 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | |
| 203 | if (tb[RTA_DST]) { |
| 204 | if (r->rtm_dst_len != host_len) { |
| 205 | fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family, |
| 206 | RTA_PAYLOAD(tb[RTA_DST]), |
| 207 | RTA_DATA(tb[RTA_DST]), |
| 208 | abuf, sizeof(abuf)), |
| 209 | r->rtm_dst_len |
| 210 | ); |
| 211 | } else { |
| 212 | fprintf(fp, "%s ", format_host(r->rtm_family, |
| 213 | RTA_PAYLOAD(tb[RTA_DST]), |
| 214 | RTA_DATA(tb[RTA_DST]), |
| 215 | abuf, sizeof(abuf)) |
| 216 | ); |
| 217 | } |
| 218 | } else if (r->rtm_dst_len) { |
| 219 | fprintf(fp, "0/%d ", r->rtm_dst_len); |
| 220 | } else { |
| 221 | fprintf(fp, "default "); |
| 222 | } |
| 223 | if (tb[RTA_SRC]) { |
| 224 | if (r->rtm_src_len != host_len) { |
| 225 | fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family, |
| 226 | RTA_PAYLOAD(tb[RTA_SRC]), |
| 227 | RTA_DATA(tb[RTA_SRC]), |
| 228 | abuf, sizeof(abuf)), |
| 229 | r->rtm_src_len |
| 230 | ); |
| 231 | } else { |
| 232 | fprintf(fp, "from %s ", format_host(r->rtm_family, |
| 233 | RTA_PAYLOAD(tb[RTA_SRC]), |
| 234 | RTA_DATA(tb[RTA_SRC]), |
| 235 | abuf, sizeof(abuf)) |
| 236 | ); |
| 237 | } |
| 238 | } else if (r->rtm_src_len) { |
| 239 | fprintf(fp, "from 0/%u ", r->rtm_src_len); |
| 240 | } |
| 241 | if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 242 | fprintf(fp, "via %s ", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 243 | format_host(r->rtm_family, |
| 244 | RTA_PAYLOAD(tb[RTA_GATEWAY]), |
| 245 | RTA_DATA(tb[RTA_GATEWAY]), |
| 246 | abuf, sizeof(abuf))); |
| 247 | } |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 248 | if (tb[RTA_OIF] && filter.oifmask != -1) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 249 | fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF]))); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 250 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 251 | |
| 252 | if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) { |
| 253 | /* Do not use format_host(). It is our local addr |
| 254 | and symbolic name will not be useful. |
| 255 | */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 256 | fprintf(fp, " src %s ", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | rt_addr_n2a(r->rtm_family, |
| 258 | RTA_PAYLOAD(tb[RTA_PREFSRC]), |
| 259 | RTA_DATA(tb[RTA_PREFSRC]), |
| 260 | abuf, sizeof(abuf))); |
| 261 | } |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 262 | if (tb[RTA_PRIORITY]) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 263 | fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY])); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 264 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 265 | if (r->rtm_family == AF_INET6) { |
| 266 | struct rta_cacheinfo *ci = NULL; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 267 | if (tb[RTA_CACHEINFO]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 268 | ci = RTA_DATA(tb[RTA_CACHEINFO]); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 269 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 271 | if (r->rtm_flags & RTM_F_CLONED) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 272 | fprintf(fp, "%c cache ", _SL_); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 273 | } |
| 274 | if (ci->rta_expires) { |
Denis Vlasenko | c519320 | 2006-12-31 19:07:23 +0000 | [diff] [blame] | 275 | fprintf(fp, " expires %dsec", ci->rta_expires / get_hz()); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 276 | } |
| 277 | if (ci->rta_error != 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | fprintf(fp, " error %d", ci->rta_error); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 279 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 280 | } else if (ci) { |
| 281 | if (ci->rta_error != 0) |
| 282 | fprintf(fp, " error %d", ci->rta_error); |
| 283 | } |
| 284 | } |
| 285 | if (tb[RTA_IIF] && filter.iifmask != -1) { |
| 286 | fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF]))); |
| 287 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 288 | fputc('\n', fp); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 289 | fflush(fp); |
| 290 | return 0; |
| 291 | } |
| 292 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 293 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 294 | static int iproute_modify(int cmd, unsigned flags, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 295 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 296 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 297 | "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0") |
| 298 | "dev\0""oif\0""to\0"; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 299 | enum { |
| 300 | ARG_src, |
| 301 | ARG_via, |
| 302 | ARG_mtu, PARM_lock, |
| 303 | ARG_protocol, |
Bernhard Reutner-Fischer | ab51bf4 | 2007-04-16 14:56:01 +0000 | [diff] [blame] | 304 | USE_FEATURE_IP_RULE(ARG_table,) |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 305 | ARG_dev, |
| 306 | ARG_oif, |
| 307 | ARG_to |
| 308 | }; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 309 | enum { |
| 310 | gw_ok = 1 << 0, |
| 311 | dst_ok = 1 << 1, |
| 312 | proto_ok = 1 << 2, |
| 313 | type_ok = 1 << 3 |
| 314 | }; |
| 315 | struct rtnl_handle rth; |
| 316 | struct { |
| 317 | struct nlmsghdr n; |
| 318 | struct rtmsg r; |
| 319 | char buf[1024]; |
| 320 | } req; |
| 321 | char mxbuf[256]; |
| 322 | struct rtattr * mxrta = (void*)mxbuf; |
| 323 | unsigned mxlock = 0; |
| 324 | char *d = NULL; |
| 325 | smalluint ok = 0; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 326 | int arg; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 327 | |
| 328 | memset(&req, 0, sizeof(req)); |
| 329 | |
| 330 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 331 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; |
| 332 | req.n.nlmsg_type = cmd; |
| 333 | req.r.rtm_family = preferred_family; |
| 334 | req.r.rtm_table = RT_TABLE_MAIN; |
| 335 | req.r.rtm_scope = RT_SCOPE_NOWHERE; |
| 336 | |
| 337 | if (cmd != RTM_DELROUTE) { |
| 338 | req.r.rtm_protocol = RTPROT_BOOT; |
| 339 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 340 | req.r.rtm_type = RTN_UNICAST; |
| 341 | } |
| 342 | |
| 343 | mxrta->rta_type = RTA_METRICS; |
| 344 | mxrta->rta_len = RTA_LENGTH(0); |
| 345 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 346 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 347 | arg = index_in_substrings(keywords, *argv); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 348 | if (arg == ARG_src) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 349 | inet_prefix addr; |
| 350 | NEXT_ARG(); |
| 351 | get_addr(&addr, *argv, req.r.rtm_family); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 352 | if (req.r.rtm_family == AF_UNSPEC) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 353 | req.r.rtm_family = addr.family; |
| 354 | addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 355 | } else if (arg == ARG_via) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 356 | inet_prefix addr; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 357 | ok |= gw_ok; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 358 | NEXT_ARG(); |
| 359 | get_addr(&addr, *argv, req.r.rtm_family); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 360 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 361 | req.r.rtm_family = addr.family; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 362 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 363 | addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 364 | } else if (arg == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 365 | unsigned mtu; |
| 366 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 367 | if (index_in_strings(keywords, *argv) == PARM_lock) { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 368 | mxlock |= (1 << RTAX_MTU); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 369 | NEXT_ARG(); |
| 370 | } |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 371 | if (get_unsigned(&mtu, *argv, 0)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 372 | invarg(*argv, "mtu"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 373 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 374 | } else if (arg == ARG_protocol) { |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 375 | uint32_t prot; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 376 | NEXT_ARG(); |
| 377 | if (rtnl_rtprot_a2n(&prot, *argv)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 378 | invarg(*argv, "protocol"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 379 | req.r.rtm_protocol = prot; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 380 | ok |= proto_ok; |
Bernhard Reutner-Fischer | ab51bf4 | 2007-04-16 14:56:01 +0000 | [diff] [blame] | 381 | #if ENABLE_FEATURE_IP_RULE |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 382 | } else if (arg == ARG_table) { |
Denis Vlasenko | cda6c63 | 2006-12-15 00:59:35 +0000 | [diff] [blame] | 383 | uint32_t tid; |
| 384 | NEXT_ARG(); |
| 385 | if (rtnl_rttable_a2n(&tid, *argv)) |
| 386 | invarg(*argv, "table"); |
| 387 | req.r.rtm_table = tid; |
Bernhard Reutner-Fischer | ab51bf4 | 2007-04-16 14:56:01 +0000 | [diff] [blame] | 388 | #endif |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 389 | } else if (arg == ARG_dev || arg == ARG_oif) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 390 | NEXT_ARG(); |
| 391 | d = *argv; |
| 392 | } else { |
| 393 | int type; |
| 394 | inet_prefix dst; |
| 395 | |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 396 | if (arg == ARG_to) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 397 | NEXT_ARG(); |
| 398 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 399 | if ((**argv < '0' || **argv > '9') |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 400 | && rtnl_rtntype_a2n(&type, *argv) == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 401 | NEXT_ARG(); |
| 402 | req.r.rtm_type = type; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 403 | ok |= type_ok; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 406 | if (ok & dst_ok) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 407 | duparg2("to", *argv); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 408 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 409 | get_prefix(&dst, *argv, req.r.rtm_family); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 410 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 411 | req.r.rtm_family = dst.family; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 412 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 413 | req.r.rtm_dst_len = dst.bitlen; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 414 | ok |= dst_ok; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 415 | if (dst.bytelen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 416 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 417 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 418 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 419 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 422 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 423 | |
Eric Andersen | 5780adb | 2002-11-15 09:12:47 +0000 | [diff] [blame] | 424 | if (d) { |
| 425 | int idx; |
| 426 | |
| 427 | ll_init_map(&rth); |
| 428 | |
| 429 | if (d) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 430 | idx = xll_name_to_index(d); |
Eric Andersen | 5780adb | 2002-11-15 09:12:47 +0000 | [diff] [blame] | 431 | addattr32(&req.n, sizeof(req), RTA_OIF, idx); |
| 432 | } |
| 433 | } |
| 434 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 435 | if (mxrta->rta_len > RTA_LENGTH(0)) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 436 | if (mxlock) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 437 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 438 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 439 | addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta)); |
| 440 | } |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 441 | |
Bernhard Reutner-Fischer | 12c96a6 | 2007-04-11 16:23:57 +0000 | [diff] [blame] | 442 | if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT) |
| 443 | req.r.rtm_scope = RT_SCOPE_HOST; |
| 444 | else if (req.r.rtm_type == RTN_BROADCAST || |
| 445 | req.r.rtm_type == RTN_MULTICAST || |
| 446 | req.r.rtm_type == RTN_ANYCAST) |
| 447 | req.r.rtm_scope = RT_SCOPE_LINK; |
| 448 | else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) { |
| 449 | if (cmd == RTM_DELROUTE) |
| 450 | req.r.rtm_scope = RT_SCOPE_NOWHERE; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 451 | else if (!(ok & gw_ok)) |
Bernhard Reutner-Fischer | 12c96a6 | 2007-04-11 16:23:57 +0000 | [diff] [blame] | 452 | req.r.rtm_scope = RT_SCOPE_LINK; |
| 453 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 454 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 455 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 456 | req.r.rtm_family = AF_INET; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 457 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 458 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 459 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 460 | return 2; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 461 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int rtnl_rtcache_request(struct rtnl_handle *rth, int family) |
| 467 | { |
| 468 | struct { |
| 469 | struct nlmsghdr nlh; |
| 470 | struct rtmsg rtm; |
| 471 | } req; |
| 472 | struct sockaddr_nl nladdr; |
| 473 | |
| 474 | memset(&nladdr, 0, sizeof(nladdr)); |
| 475 | memset(&req, 0, sizeof(req)); |
| 476 | nladdr.nl_family = AF_NETLINK; |
| 477 | |
| 478 | req.nlh.nlmsg_len = sizeof(req); |
| 479 | req.nlh.nlmsg_type = RTM_GETROUTE; |
| 480 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST; |
| 481 | req.nlh.nlmsg_pid = 0; |
| 482 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 483 | req.rtm.rtm_family = family; |
| 484 | req.rtm.rtm_flags |= RTM_F_CLONED; |
| 485 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 486 | return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 489 | static void iproute_flush_cache(void) |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 490 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 491 | static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush"; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 492 | int flush_fd = open_or_warn(fn, O_WRONLY); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 493 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 494 | if (flush_fd < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 495 | return; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 498 | if (write(flush_fd, "-1", 2) < 2) { |
| 499 | bb_perror_msg("cannot flush routing cache"); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 500 | return; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 501 | } |
| 502 | close(flush_fd); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 505 | static void iproute_reset_filter(void) |
| 506 | { |
| 507 | memset(&filter, 0, sizeof(filter)); |
| 508 | filter.mdst.bitlen = -1; |
| 509 | filter.msrc.bitlen = -1; |
| 510 | } |
| 511 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 512 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 513 | static int iproute_list_or_flush(char **argv, int flush) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 514 | { |
| 515 | int do_ipv6 = preferred_family; |
| 516 | struct rtnl_handle rth; |
| 517 | char *id = NULL; |
| 518 | char *od = NULL; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 519 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 520 | /* "ip route list/flush" parameters: */ |
| 521 | "protocol\0" "dev\0" "oif\0" "iif\0" |
| 522 | "via\0" "table\0" "cache\0" |
| 523 | "from\0" "to\0" |
| 524 | /* and possible further keywords */ |
| 525 | "all\0" |
| 526 | "root\0" |
| 527 | "match\0" |
| 528 | "exact\0" |
| 529 | "main\0" |
| 530 | ; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 531 | enum { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 532 | KW_proto, KW_dev, KW_oif, KW_iif, |
| 533 | KW_via, KW_table, KW_cache, |
| 534 | KW_from, KW_to, |
| 535 | /* */ |
| 536 | KW_all, |
| 537 | KW_root, |
| 538 | KW_match, |
| 539 | KW_exact, |
| 540 | KW_main, |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 541 | }; |
| 542 | int arg, parm; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 543 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 544 | iproute_reset_filter(); |
| 545 | filter.tb = RT_TABLE_MAIN; |
| 546 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 547 | if (flush && !*argv) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 548 | bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\""); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 549 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 550 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 551 | arg = index_in_substrings(keywords, *argv); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 552 | if (arg == KW_proto) { |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 553 | uint32_t prot = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 554 | NEXT_ARG(); |
| 555 | filter.protocolmask = -1; |
| 556 | if (rtnl_rtprot_a2n(&prot, *argv)) { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 557 | if (index_in_strings(keywords, *argv) != KW_all) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 558 | invarg(*argv, "protocol"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 559 | prot = 0; |
| 560 | filter.protocolmask = 0; |
| 561 | } |
| 562 | filter.protocol = prot; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 563 | } else if (arg == KW_dev || arg == KW_oif) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 564 | NEXT_ARG(); |
| 565 | od = *argv; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 566 | } else if (arg == KW_iif) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 567 | NEXT_ARG(); |
| 568 | id = *argv; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 569 | } else if (arg == KW_via) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 570 | NEXT_ARG(); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 571 | get_prefix(&filter.rvia, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 572 | } else if (arg == KW_table) { /* table all/cache/main */ |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 573 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 574 | parm = index_in_substrings(keywords, *argv); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 575 | if (parm == KW_cache) |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 576 | filter.tb = -1; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 577 | else if (parm == KW_all) |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 578 | filter.tb = 0; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 579 | else if (parm != KW_main) |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 580 | invarg(*argv, "table"); |
Denis Vlasenko | 79c6904 | 2007-11-27 09:42:33 +0000 | [diff] [blame] | 581 | } else if (arg == KW_cache) { |
| 582 | /* The command 'ip route flush cache' is used by OpenSWAN. |
| 583 | * Assuming it's a synonym for 'ip route flush table cache' */ |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 584 | filter.tb = -1; |
| 585 | } else if (arg == KW_from) { |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 586 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 587 | parm = index_in_substrings(keywords, *argv); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 588 | if (parm == KW_root) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 589 | NEXT_ARG(); |
| 590 | get_prefix(&filter.rsrc, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 591 | } else if (parm == KW_match) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 592 | NEXT_ARG(); |
| 593 | get_prefix(&filter.msrc, *argv, do_ipv6); |
| 594 | } else { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 595 | if (parm == KW_exact) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 596 | NEXT_ARG(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 597 | get_prefix(&filter.msrc, *argv, do_ipv6); |
| 598 | filter.rsrc = filter.msrc; |
| 599 | } |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 600 | } else { /* "to" is the default parameter */ |
| 601 | if (arg == KW_to) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 602 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 603 | arg = index_in_substrings(keywords, *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 604 | } |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 605 | /* parm = arg; - would be more plausible, but we reuse 'arg' here */ |
| 606 | if (arg == KW_root) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 607 | NEXT_ARG(); |
| 608 | get_prefix(&filter.rdst, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 609 | } else if (arg == KW_match) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 610 | NEXT_ARG(); |
| 611 | get_prefix(&filter.mdst, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 612 | } else { /* "to exact" is the default */ |
| 613 | if (arg == KW_exact) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 614 | NEXT_ARG(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 615 | get_prefix(&filter.mdst, *argv, do_ipv6); |
| 616 | filter.rdst = filter.mdst; |
| 617 | } |
| 618 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 619 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 622 | if (do_ipv6 == AF_UNSPEC && filter.tb) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 623 | do_ipv6 = AF_INET; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 624 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 625 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 626 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 627 | ll_init_map(&rth); |
| 628 | |
| 629 | if (id || od) { |
| 630 | int idx; |
| 631 | |
| 632 | if (id) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 633 | idx = xll_name_to_index(id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 634 | filter.iif = idx; |
| 635 | filter.iifmask = -1; |
| 636 | } |
| 637 | if (od) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 638 | idx = xll_name_to_index(od); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 639 | filter.oif = idx; |
| 640 | filter.oifmask = -1; |
| 641 | } |
| 642 | } |
| 643 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 644 | if (flush) { |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 645 | char flushb[4096-512]; |
| 646 | |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 647 | if (filter.tb == -1) { /* "flush table cache" */ |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 648 | if (do_ipv6 != AF_INET6) |
| 649 | iproute_flush_cache(); |
| 650 | if (do_ipv6 == AF_INET) |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | filter.flushb = flushb; |
| 655 | filter.flushp = 0; |
| 656 | filter.flushe = sizeof(flushb); |
| 657 | filter.rth = &rth; |
| 658 | |
| 659 | for (;;) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 660 | xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 661 | filter.flushed = 0; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 662 | xrtnl_dump_filter(&rth, print_route, stdout); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 663 | if (filter.flushed == 0) |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 664 | return 0; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 665 | if (flush_update()) |
| 666 | return 1; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 670 | if (filter.tb != -1) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 671 | xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 672 | } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { |
| 673 | bb_perror_msg_and_die("cannot send dump request"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 674 | } |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 675 | xrtnl_dump_filter(&rth, print_route, stdout); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 676 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 677 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 681 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 682 | static int iproute_get(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 683 | { |
| 684 | struct rtnl_handle rth; |
| 685 | struct { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 686 | struct nlmsghdr n; |
| 687 | struct rtmsg r; |
| 688 | char buf[1024]; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 689 | } req; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 690 | char *idev = NULL; |
| 691 | char *odev = NULL; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 692 | bool connected = 0; |
| 693 | bool from_ok = 0; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 694 | static const char options[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 695 | "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 696 | |
| 697 | memset(&req, 0, sizeof(req)); |
| 698 | |
| 699 | iproute_reset_filter(); |
| 700 | |
| 701 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 702 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 703 | req.n.nlmsg_type = RTM_GETROUTE; |
| 704 | req.r.rtm_family = preferred_family; |
| 705 | req.r.rtm_table = 0; |
| 706 | req.r.rtm_protocol = 0; |
| 707 | req.r.rtm_scope = 0; |
| 708 | req.r.rtm_type = 0; |
| 709 | req.r.rtm_src_len = 0; |
| 710 | req.r.rtm_dst_len = 0; |
| 711 | req.r.rtm_tos = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 712 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 713 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 714 | switch (index_in_strings(options, *argv)) { |
Glenn L McGrath | 18eae00 | 2002-12-02 00:54:10 +0000 | [diff] [blame] | 715 | case 0: /* from */ |
| 716 | { |
| 717 | inet_prefix addr; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 718 | NEXT_ARG(); |
Glenn L McGrath | 18eae00 | 2002-12-02 00:54:10 +0000 | [diff] [blame] | 719 | from_ok = 1; |
| 720 | get_prefix(&addr, *argv, req.r.rtm_family); |
| 721 | if (req.r.rtm_family == AF_UNSPEC) { |
| 722 | req.r.rtm_family = addr.family; |
| 723 | } |
| 724 | if (addr.bytelen) { |
| 725 | addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); |
| 726 | } |
| 727 | req.r.rtm_src_len = addr.bitlen; |
| 728 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 729 | } |
Glenn L McGrath | 18eae00 | 2002-12-02 00:54:10 +0000 | [diff] [blame] | 730 | case 1: /* iif */ |
| 731 | NEXT_ARG(); |
| 732 | idev = *argv; |
| 733 | break; |
| 734 | case 2: /* oif */ |
| 735 | case 3: /* dev */ |
| 736 | NEXT_ARG(); |
| 737 | odev = *argv; |
| 738 | break; |
| 739 | case 4: /* notify */ |
| 740 | req.r.rtm_flags |= RTM_F_NOTIFY; |
| 741 | break; |
| 742 | case 5: /* connected */ |
| 743 | connected = 1; |
| 744 | break; |
| 745 | case 6: /* to */ |
| 746 | NEXT_ARG(); |
| 747 | default: |
| 748 | { |
| 749 | inet_prefix addr; |
| 750 | get_prefix(&addr, *argv, req.r.rtm_family); |
| 751 | if (req.r.rtm_family == AF_UNSPEC) { |
| 752 | req.r.rtm_family = addr.family; |
| 753 | } |
| 754 | if (addr.bytelen) { |
| 755 | addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen); |
| 756 | } |
| 757 | req.r.rtm_dst_len = addr.bitlen; |
| 758 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 759 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 760 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | if (req.r.rtm_dst_len == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 764 | bb_error_msg_and_die("need at least destination address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 767 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 768 | |
| 769 | ll_init_map(&rth); |
| 770 | |
| 771 | if (idev || odev) { |
| 772 | int idx; |
| 773 | |
| 774 | if (idev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 775 | idx = xll_name_to_index(idev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 776 | addattr32(&req.n, sizeof(req), RTA_IIF, idx); |
| 777 | } |
| 778 | if (odev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 779 | idx = xll_name_to_index(odev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 780 | addattr32(&req.n, sizeof(req), RTA_OIF, idx); |
| 781 | } |
| 782 | } |
| 783 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 784 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 785 | req.r.rtm_family = AF_INET; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 786 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 787 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 788 | if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 789 | return 2; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 790 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 791 | |
| 792 | if (connected && !from_ok) { |
| 793 | struct rtmsg *r = NLMSG_DATA(&req.n); |
| 794 | int len = req.n.nlmsg_len; |
| 795 | struct rtattr * tb[RTA_MAX+1]; |
| 796 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 797 | print_route(NULL, &req.n, (void*)stdout); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 798 | |
| 799 | if (req.n.nlmsg_type != RTM_NEWROUTE) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 800 | bb_error_msg_and_die("not a route?"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 801 | } |
| 802 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 803 | if (len < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 804 | bb_error_msg_and_die("wrong len %d", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | memset(tb, 0, sizeof(tb)); |
| 808 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 809 | |
| 810 | if (tb[RTA_PREFSRC]) { |
| 811 | tb[RTA_PREFSRC]->rta_type = RTA_SRC; |
| 812 | r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]); |
| 813 | } else if (!tb[RTA_SRC]) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 814 | bb_error_msg_and_die("failed to connect the route"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 815 | } |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 816 | if (!odev && tb[RTA_OIF]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 817 | tb[RTA_OIF]->rta_type = 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 818 | } |
| 819 | if (tb[RTA_GATEWAY]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 820 | tb[RTA_GATEWAY]->rta_type = 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 821 | } |
| 822 | if (!idev && tb[RTA_IIF]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 823 | tb[RTA_IIF]->rta_type = 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 824 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 825 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 826 | req.n.nlmsg_type = RTM_GETROUTE; |
| 827 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 828 | if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 829 | return 2; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 830 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 831 | } |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 832 | print_route(NULL, &req.n, (void*)stdout); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 833 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 836 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 837 | int do_iproute(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 838 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 839 | static const char ip_route_commands[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 840 | /*0-3*/ "add\0""append\0""change\0""chg\0" |
| 841 | /*4-7*/ "delete\0""get\0""list\0""show\0" |
| 842 | /*8..*/ "prepend\0""replace\0""test\0""flush\0"; |
Denis Vlasenko | 83c4422 | 2008-01-04 15:28:28 +0000 | [diff] [blame] | 843 | int command_num; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 844 | unsigned flags = 0; |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 845 | int cmd = RTM_NEWROUTE; |
| 846 | |
Denis Vlasenko | 83c4422 | 2008-01-04 15:28:28 +0000 | [diff] [blame] | 847 | if (!*argv) |
| 848 | return iproute_list_or_flush(argv, 0); |
| 849 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 850 | /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */ |
| 851 | /* It probably means that it is using "first match" rule */ |
Denis Vlasenko | 83c4422 | 2008-01-04 15:28:28 +0000 | [diff] [blame] | 852 | command_num = index_in_substrings(ip_route_commands, *argv); |
| 853 | |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 854 | switch (command_num) { |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 855 | case 0: /* add */ |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 856 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 857 | break; |
| 858 | case 1: /* append */ |
| 859 | flags = NLM_F_CREATE|NLM_F_APPEND; |
| 860 | break; |
| 861 | case 2: /* change */ |
| 862 | case 3: /* chg */ |
| 863 | flags = NLM_F_REPLACE; |
| 864 | break; |
| 865 | case 4: /* delete */ |
| 866 | cmd = RTM_DELROUTE; |
| 867 | break; |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 868 | case 5: /* get */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 869 | return iproute_get(argv+1); |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 870 | case 6: /* list */ |
| 871 | case 7: /* show */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 872 | return iproute_list_or_flush(argv+1, 0); |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 873 | case 8: /* prepend */ |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 874 | flags = NLM_F_CREATE; |
Denis Vlasenko | 1eecaf2 | 2007-09-30 16:04:21 +0000 | [diff] [blame] | 875 | break; |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 876 | case 9: /* replace */ |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 877 | flags = NLM_F_CREATE|NLM_F_REPLACE; |
Denis Vlasenko | 1eecaf2 | 2007-09-30 16:04:21 +0000 | [diff] [blame] | 878 | break; |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 879 | case 10: /* test */ |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 880 | flags = NLM_F_EXCL; |
Denis Vlasenko | 1eecaf2 | 2007-09-30 16:04:21 +0000 | [diff] [blame] | 881 | break; |
Denis Vlasenko | fd94efa | 2007-01-27 13:13:45 +0000 | [diff] [blame] | 882 | case 11: /* flush */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 883 | return iproute_list_or_flush(argv+1, 1); |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 884 | default: |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 885 | bb_error_msg_and_die("unknown command %s", *argv); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 886 | } |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 887 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 888 | return iproute_modify(cmd, flags, argv+1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 889 | } |