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