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