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