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