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 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
| 7 | * Changes: |
| 8 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 9 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 10 | * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized |
| 11 | */ |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 12 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 13 | #include "common_bufsiz.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 14 | #include "rt_names.h" |
| 15 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 16 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | #ifndef RTAX_RTTVAR |
| 18 | #define RTAX_RTTVAR RTAX_HOPS |
| 19 | #endif |
| 20 | |
| 21 | |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 22 | struct filter_t { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 23 | int tb; |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 24 | smallint flushed; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 25 | char *flushb; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 26 | int flushp; |
| 27 | int flushe; |
| 28 | struct rtnl_handle *rth; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 29 | //int protocol, protocolmask; - write-only fields?! |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 30 | int scope, scopemask; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 31 | //int type; - read-only |
| 32 | //int typemask; - unused |
| 33 | //int tos, tosmask; - unused |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 34 | int iif; |
| 35 | int oif; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 36 | //int realm, realmmask; - unused |
| 37 | //inet_prefix rprefsrc; - read-only |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | inet_prefix rvia; |
| 39 | inet_prefix rdst; |
| 40 | inet_prefix mdst; |
| 41 | inet_prefix rsrc; |
| 42 | inet_prefix msrc; |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 43 | } FIX_ALIASING; |
| 44 | typedef struct filter_t filter_t; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 45 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 46 | #define G_filter (*(filter_t*)bb_common_bufsiz1) |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 47 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
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 | { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 51 | 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] | 52 | bb_perror_msg("can't send flush request"); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 55 | G_filter.flushp = 0; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 59 | 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] | 60 | struct nlmsghdr *n, void *arg UNUSED_PARAM) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 62 | struct rtmsg *r = NLMSG_DATA(n); |
| 63 | int len = n->nlmsg_len; |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 64 | struct rtattr *tb[RTA_MAX+1]; |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 65 | inet_prefix dst; |
| 66 | inet_prefix src; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 67 | int host_len = -1; |
Lukasz Nowak | b42107f | 2016-12-13 12:58:31 +0000 | [diff] [blame] | 68 | uint32_t tid; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 69 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 70 | if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) { |
| 71 | fprintf(stderr, "Not a route: %08x %08x %08x\n", |
| 72 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 73 | return 0; |
| 74 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 75 | if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE) |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 76 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 77 | len -= NLMSG_LENGTH(sizeof(*r)); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 78 | if (len < 0) |
| 79 | bb_error_msg_and_die("wrong nlmsg len %d", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 80 | |
Lukasz Nowak | b42107f | 2016-12-13 12:58:31 +0000 | [diff] [blame] | 81 | memset(tb, 0, sizeof(tb)); |
| 82 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 83 | |
| 84 | if (tb[RTA_TABLE]) |
| 85 | tid = *(uint32_t *)RTA_DATA(tb[RTA_TABLE]); |
| 86 | else |
| 87 | tid = r->rtm_table; |
| 88 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 89 | if (r->rtm_family == AF_INET6) |
| 90 | host_len = 128; |
| 91 | else if (r->rtm_family == AF_INET) |
| 92 | host_len = 32; |
| 93 | |
| 94 | if (r->rtm_family == AF_INET6) { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 95 | if (G_filter.tb) { |
| 96 | if (G_filter.tb < 0) { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 97 | if (!(r->rtm_flags & RTM_F_CLONED)) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 98 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 99 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 100 | } else { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 101 | if (r->rtm_flags & RTM_F_CLONED) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 102 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 103 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 104 | if (G_filter.tb == RT_TABLE_LOCAL) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 105 | if (r->rtm_type != RTN_LOCAL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 106 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 107 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 108 | } else if (G_filter.tb == RT_TABLE_MAIN) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 109 | if (r->rtm_type == RTN_LOCAL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 111 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 112 | } else { |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } else { |
Lukasz Nowak | b42107f | 2016-12-13 12:58:31 +0000 | [diff] [blame] | 118 | if (G_filter.tb > 0 && G_filter.tb != tid) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 120 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 121 | } |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 122 | if ((G_filter.scope ^ r->rtm_scope) & G_filter.scopemask) |
| 123 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 124 | if (G_filter.rdst.family |
| 125 | && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len) |
| 126 | ) { |
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 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 129 | if (G_filter.mdst.family |
| 130 | && (r->rtm_family != G_filter.mdst.family |
| 131 | || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len) |
| 132 | ) |
| 133 | ) { |
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 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 136 | if (G_filter.rsrc.family |
| 137 | && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len) |
| 138 | ) { |
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 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 141 | if (G_filter.msrc.family |
| 142 | && (r->rtm_family != G_filter.msrc.family |
| 143 | || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len) |
| 144 | ) |
| 145 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 146 | return 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 147 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 148 | |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 149 | memset(&src, 0, sizeof(src)); |
| 150 | memset(&dst, 0, sizeof(dst)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 151 | |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 152 | if (tb[RTA_SRC]) { |
| 153 | src.bitlen = r->rtm_src_len; |
| 154 | src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4); |
| 155 | memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen); |
| 156 | } |
| 157 | if (tb[RTA_DST]) { |
| 158 | dst.bitlen = r->rtm_dst_len; |
| 159 | dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4); |
| 160 | memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen); |
| 161 | } |
| 162 | |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 163 | if (G_filter.rdst.family |
| 164 | && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen) |
| 165 | ) { |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 166 | return 0; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 167 | } |
| 168 | if (G_filter.mdst.family |
| 169 | && G_filter.mdst.bitlen >= 0 |
| 170 | && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len) |
| 171 | ) { |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 172 | return 0; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 173 | } |
| 174 | if (G_filter.rsrc.family |
| 175 | && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen) |
| 176 | ) { |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 177 | return 0; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 178 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 179 | if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0 |
| 180 | && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len) |
| 181 | ) { |
Glenn L McGrath | fbf0b8a | 2003-04-26 02:22:19 +0000 | [diff] [blame] | 182 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 183 | } |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 184 | if (G_filter.oif != 0) { |
| 185 | if (!tb[RTA_OIF]) |
| 186 | return 0; |
| 187 | if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF])) |
| 188 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 189 | } |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 190 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 191 | if (G_filter.flushb) { |
Denys Vlasenko | e3ece78 | 2011-02-10 09:50:07 +0100 | [diff] [blame] | 192 | struct nlmsghdr *fn; |
| 193 | |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 194 | /* We are creating route flush commands */ |
| 195 | |
| 196 | if (r->rtm_family == AF_INET6 |
| 197 | && r->rtm_dst_len == 0 |
| 198 | && r->rtm_type == RTN_UNREACHABLE |
| 199 | && tb[RTA_PRIORITY] |
| 200 | && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1 |
| 201 | ) { |
| 202 | return 0; |
| 203 | } |
| 204 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 205 | 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] | 206 | if (flush_update()) |
Curt Brune | 6993470 | 2015-10-14 12:53:47 +0200 | [diff] [blame] | 207 | xfunc_die(); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 208 | } |
Denys Vlasenko | e3ece78 | 2011-02-10 09:50:07 +0100 | [diff] [blame] | 209 | fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp)); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 210 | memcpy(fn, n, n->nlmsg_len); |
| 211 | fn->nlmsg_type = RTM_DELROUTE; |
| 212 | fn->nlmsg_flags = NLM_F_REQUEST; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 213 | fn->nlmsg_seq = ++G_filter.rth->seq; |
| 214 | G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb; |
| 215 | G_filter.flushed = 1; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 219 | /* We are printing routes */ |
| 220 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 221 | if (n->nlmsg_type == RTM_DELROUTE) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 222 | printf("Deleted "); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 223 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 224 | if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) { |
Denys Vlasenko | 3d8d5e8 | 2015-10-08 13:02:28 +0200 | [diff] [blame] | 225 | printf("%s ", rtnl_rtntype_n2a(r->rtm_type)); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 226 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 227 | |
| 228 | if (tb[RTA_DST]) { |
| 229 | if (r->rtm_dst_len != host_len) { |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 230 | printf("%s/%u ", |
| 231 | rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])), |
| 232 | r->rtm_dst_len |
| 233 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 234 | } else { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 235 | printf("%s ", format_host(r->rtm_family, |
| 236 | RTA_PAYLOAD(tb[RTA_DST]), |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 237 | RTA_DATA(tb[RTA_DST])) |
| 238 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 239 | } |
| 240 | } else if (r->rtm_dst_len) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 241 | printf("0/%d ", r->rtm_dst_len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 242 | } else { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 243 | printf("default "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 244 | } |
| 245 | if (tb[RTA_SRC]) { |
| 246 | if (r->rtm_src_len != host_len) { |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 247 | printf("from %s/%u ", |
| 248 | rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])), |
| 249 | r->rtm_src_len |
| 250 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 251 | } else { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 252 | printf("from %s ", format_host(r->rtm_family, |
| 253 | RTA_PAYLOAD(tb[RTA_SRC]), |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 254 | RTA_DATA(tb[RTA_SRC])) |
| 255 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 256 | } |
| 257 | } else if (r->rtm_src_len) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 258 | printf("from 0/%u ", r->rtm_src_len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 259 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 260 | if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 261 | printf("via %s ", format_host(r->rtm_family, |
| 262 | RTA_PAYLOAD(tb[RTA_GATEWAY]), |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 263 | RTA_DATA(tb[RTA_GATEWAY])) |
| 264 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 265 | } |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 266 | if (tb[RTA_OIF]) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 267 | 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] | 268 | } |
Lukasz Nowak | b42107f | 2016-12-13 12:58:31 +0000 | [diff] [blame] | 269 | #if ENABLE_FEATURE_IP_RULE |
| 270 | if (tid && tid != RT_TABLE_MAIN && !G_filter.tb) |
| 271 | printf("table %s ", rtnl_rttable_n2a(tid)); |
| 272 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 273 | |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 274 | /* Todo: parse & show "proto kernel" here */ |
| 275 | if (!(r->rtm_flags & RTM_F_CLONED)) { |
| 276 | if ((r->rtm_scope != RT_SCOPE_UNIVERSE) && G_filter.scopemask != -1) |
| 277 | printf("scope %s ", rtnl_rtscope_n2a(r->rtm_scope)); |
| 278 | } |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 279 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 280 | 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] | 281 | /* Do not use format_host(). It is our local addr |
| 282 | and symbolic name will not be useful. |
| 283 | */ |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 284 | printf(" src %s ", rt_addr_n2a(r->rtm_family, |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 285 | RTA_DATA(tb[RTA_PREFSRC]))); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 286 | } |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 287 | if (tb[RTA_PRIORITY]) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 288 | printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY])); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 289 | } |
Michael Tokarev | 1a11439 | 2014-07-28 10:05:41 +0400 | [diff] [blame] | 290 | if (r->rtm_flags & RTNH_F_DEAD) { |
| 291 | printf("dead "); |
| 292 | } |
| 293 | if (r->rtm_flags & RTNH_F_ONLINK) { |
| 294 | printf("onlink "); |
| 295 | } |
| 296 | if (r->rtm_flags & RTNH_F_PERVASIVE) { |
| 297 | printf("pervasive "); |
| 298 | } |
| 299 | if (r->rtm_flags & RTM_F_NOTIFY) { |
| 300 | printf("notify "); |
| 301 | } |
| 302 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 303 | if (r->rtm_family == AF_INET6) { |
| 304 | struct rta_cacheinfo *ci = NULL; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 305 | if (tb[RTA_CACHEINFO]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 306 | ci = RTA_DATA(tb[RTA_CACHEINFO]); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 307 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 308 | if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 309 | if (r->rtm_flags & RTM_F_CLONED) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 310 | printf("%c cache ", _SL_); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 311 | } |
| 312 | if (ci->rta_expires) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 313 | printf(" expires %dsec", ci->rta_expires / get_hz()); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 314 | } |
| 315 | if (ci->rta_error != 0) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 316 | printf(" error %d", ci->rta_error); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 317 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 318 | } else if (ci) { |
| 319 | if (ci->rta_error != 0) |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 320 | printf(" error %d", ci->rta_error); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 323 | if (tb[RTA_IIF] && G_filter.iif == 0) { |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 324 | 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] | 325 | } |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 326 | bb_putchar('\n'); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
Denys Vlasenko | d5342a1 | 2017-04-07 17:00:53 +0200 | [diff] [blame] | 330 | static int str_is_lock(const char *str) |
| 331 | { |
| 332 | return strcmp(str, "lock") == 0; |
| 333 | } |
| 334 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 335 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 336 | static int iproute_modify(int cmd, unsigned flags, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 337 | { |
Denys Vlasenko | 1140bf3 | 2017-04-06 17:54:38 +0200 | [diff] [blame] | 338 | /* If you add stuff here, update iproute_full_usage */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 339 | static const char keywords[] ALIGN1 = |
Denys Vlasenko | d5342a1 | 2017-04-07 17:00:53 +0200 | [diff] [blame] | 340 | "src\0""via\0" |
| 341 | "mtu\0""advmss\0" |
| 342 | "scope\0""protocol\0"IF_FEATURE_IP_RULE("table\0") |
Michael Tokarev | 1a11439 | 2014-07-28 10:05:41 +0400 | [diff] [blame] | 343 | "dev\0""oif\0""to\0""metric\0""onlink\0"; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 344 | #define keyword_via (keywords + sizeof("src")) |
| 345 | #define keyword_mtu (keyword_via + sizeof("via")) |
| 346 | #define keyword_advmss (keyword_mtu + sizeof("mtu")) |
| 347 | #define keyword_scope (keyword_advmss + sizeof("advmss")) |
| 348 | #define keyword_proto (keyword_scope + sizeof("scope")) |
| 349 | #define keyword_table (keyword_proto + sizeof("protocol")) |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 350 | enum { |
| 351 | ARG_src, |
| 352 | ARG_via, |
Denys Vlasenko | 1140bf3 | 2017-04-06 17:54:38 +0200 | [diff] [blame] | 353 | ARG_mtu, |
Denys Vlasenko | d5342a1 | 2017-04-07 17:00:53 +0200 | [diff] [blame] | 354 | ARG_advmss, |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 355 | ARG_scope, |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 356 | ARG_protocol, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 357 | IF_FEATURE_IP_RULE(ARG_table,) |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 358 | ARG_dev, |
| 359 | ARG_oif, |
Bernhard Reutner-Fischer | 578de86 | 2008-10-07 17:00:58 +0000 | [diff] [blame] | 360 | ARG_to, |
| 361 | ARG_metric, |
Michael Tokarev | 1a11439 | 2014-07-28 10:05:41 +0400 | [diff] [blame] | 362 | ARG_onlink, |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 363 | }; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 364 | enum { |
| 365 | gw_ok = 1 << 0, |
| 366 | dst_ok = 1 << 1, |
| 367 | proto_ok = 1 << 2, |
| 368 | type_ok = 1 << 3 |
| 369 | }; |
| 370 | struct rtnl_handle rth; |
| 371 | struct { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 372 | struct nlmsghdr n; |
| 373 | struct rtmsg r; |
| 374 | char buf[1024]; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 375 | } req; |
| 376 | char mxbuf[256]; |
| 377 | struct rtattr * mxrta = (void*)mxbuf; |
| 378 | unsigned mxlock = 0; |
| 379 | char *d = NULL; |
| 380 | smalluint ok = 0; |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 381 | smalluint scope_ok = 0; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 382 | int arg; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 383 | |
| 384 | memset(&req, 0, sizeof(req)); |
| 385 | |
| 386 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 387 | req.n.nlmsg_flags = NLM_F_REQUEST | flags; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 388 | req.n.nlmsg_type = cmd; |
| 389 | req.r.rtm_family = preferred_family; |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 390 | if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */ |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 391 | req.r.rtm_table = RT_TABLE_MAIN; |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 392 | if (RT_SCOPE_NOWHERE != 0) |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 393 | req.r.rtm_scope = RT_SCOPE_NOWHERE; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 394 | |
| 395 | if (cmd != RTM_DELROUTE) { |
Denys Vlasenko | 34ecc3b | 2016-08-14 01:30:34 +0200 | [diff] [blame] | 396 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 397 | if (RTPROT_BOOT != 0) |
| 398 | req.r.rtm_protocol = RTPROT_BOOT; |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 399 | if (RTN_UNICAST != 0) |
| 400 | req.r.rtm_type = RTN_UNICAST; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | mxrta->rta_type = RTA_METRICS; |
| 404 | mxrta->rta_len = RTA_LENGTH(0); |
| 405 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 406 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 407 | arg = index_in_substrings(keywords, *argv); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 408 | if (arg == ARG_src) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 409 | inet_prefix addr; |
| 410 | NEXT_ARG(); |
| 411 | get_addr(&addr, *argv, req.r.rtm_family); |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 412 | if (req.r.rtm_family == AF_UNSPEC) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 413 | req.r.rtm_family = addr.family; |
| 414 | 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] | 415 | } else if (arg == ARG_via) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 416 | inet_prefix addr; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 417 | ok |= gw_ok; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 418 | NEXT_ARG(); |
| 419 | get_addr(&addr, *argv, req.r.rtm_family); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 420 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 421 | req.r.rtm_family = addr.family; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 422 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 423 | 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] | 424 | } else if (arg == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 425 | unsigned mtu; |
| 426 | NEXT_ARG(); |
Denys Vlasenko | d5342a1 | 2017-04-07 17:00:53 +0200 | [diff] [blame] | 427 | if (str_is_lock(*argv)) { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 428 | mxlock |= (1 << RTAX_MTU); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 429 | NEXT_ARG(); |
| 430 | } |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 431 | mtu = get_unsigned(*argv, keyword_mtu); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 432 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu); |
Denys Vlasenko | d5342a1 | 2017-04-07 17:00:53 +0200 | [diff] [blame] | 433 | } else if (arg == ARG_advmss) { |
| 434 | unsigned mss; |
| 435 | NEXT_ARG(); |
| 436 | if (str_is_lock(*argv)) { |
| 437 | mxlock |= (1 << RTAX_ADVMSS); |
| 438 | NEXT_ARG(); |
| 439 | } |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 440 | mss = get_unsigned(*argv, keyword_advmss); |
Denys Vlasenko | d5342a1 | 2017-04-07 17:00:53 +0200 | [diff] [blame] | 441 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss); |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 442 | } else if (arg == ARG_scope) { |
| 443 | uint32_t scope; |
| 444 | NEXT_ARG(); |
| 445 | if (rtnl_rtscope_a2n(&scope, *argv)) |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 446 | invarg_1_to_2(*argv, keyword_scope); |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 447 | req.r.rtm_scope = scope; |
| 448 | scope_ok = 1; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 449 | } else if (arg == ARG_protocol) { |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 450 | uint32_t prot; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 451 | NEXT_ARG(); |
| 452 | if (rtnl_rtprot_a2n(&prot, *argv)) |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 453 | invarg_1_to_2(*argv, keyword_proto); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 454 | req.r.rtm_protocol = prot; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 455 | ok |= proto_ok; |
Bernhard Reutner-Fischer | ab51bf4 | 2007-04-16 14:56:01 +0000 | [diff] [blame] | 456 | #if ENABLE_FEATURE_IP_RULE |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 457 | } else if (arg == ARG_table) { |
Denis Vlasenko | cda6c63 | 2006-12-15 00:59:35 +0000 | [diff] [blame] | 458 | uint32_t tid; |
| 459 | NEXT_ARG(); |
| 460 | if (rtnl_rttable_a2n(&tid, *argv)) |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 461 | invarg_1_to_2(*argv, keyword_table); |
Lukasz Nowak | b42107f | 2016-12-13 12:58:31 +0000 | [diff] [blame] | 462 | if (tid < 256) |
| 463 | req.r.rtm_table = tid; |
| 464 | else { |
| 465 | req.r.rtm_table = RT_TABLE_UNSPEC; |
| 466 | addattr32(&req.n, sizeof(req), RTA_TABLE, tid); |
| 467 | } |
Bernhard Reutner-Fischer | ab51bf4 | 2007-04-16 14:56:01 +0000 | [diff] [blame] | 468 | #endif |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 469 | } else if (arg == ARG_dev || arg == ARG_oif) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 470 | NEXT_ARG(); |
| 471 | d = *argv; |
Bernhard Reutner-Fischer | 578de86 | 2008-10-07 17:00:58 +0000 | [diff] [blame] | 472 | } else if (arg == ARG_metric) { |
Denys Vlasenko | 1140bf3 | 2017-04-06 17:54:38 +0200 | [diff] [blame] | 473 | //TODO: "metric", "priority" and "preference" are synonyms |
Bernhard Reutner-Fischer | 578de86 | 2008-10-07 17:00:58 +0000 | [diff] [blame] | 474 | uint32_t metric; |
| 475 | NEXT_ARG(); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 476 | metric = get_u32(*argv, "metric"); |
Bernhard Reutner-Fischer | 578de86 | 2008-10-07 17:00:58 +0000 | [diff] [blame] | 477 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric); |
Michael Tokarev | 1a11439 | 2014-07-28 10:05:41 +0400 | [diff] [blame] | 478 | } else if (arg == ARG_onlink) { |
| 479 | req.r.rtm_flags |= RTNH_F_ONLINK; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 480 | } else { |
| 481 | int type; |
| 482 | inet_prefix dst; |
| 483 | |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 484 | if (arg == ARG_to) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 485 | NEXT_ARG(); |
| 486 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 487 | if ((**argv < '0' || **argv > '9') |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 488 | && rtnl_rtntype_a2n(&type, *argv) == 0 |
| 489 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 490 | NEXT_ARG(); |
| 491 | req.r.rtm_type = type; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 492 | ok |= type_ok; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 495 | if (ok & dst_ok) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 496 | duparg2("to", *argv); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 497 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 498 | get_prefix(&dst, *argv, req.r.rtm_family); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 499 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 500 | req.r.rtm_family = dst.family; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 501 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 502 | req.r.rtm_dst_len = dst.bitlen; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 503 | ok |= dst_ok; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 504 | if (dst.bytelen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 505 | 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] | 506 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 507 | } |
Denys Vlasenko | 1140bf3 | 2017-04-06 17:54:38 +0200 | [diff] [blame] | 508 | /* Other keywords recognized by iproute2-3.19.0: */ |
| 509 | #if 0 |
| 510 | } else if (strcmp(*argv, "from") == 0) { |
| 511 | inet_prefix addr; |
| 512 | NEXT_ARG(); |
| 513 | get_prefix(&addr, *argv, req.r.rtm_family); |
| 514 | if (req.r.rtm_family == AF_UNSPEC) |
| 515 | req.r.rtm_family = addr.family; |
| 516 | if (addr.bytelen) |
| 517 | addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); |
| 518 | req.r.rtm_src_len = addr.bitlen; |
| 519 | } else if (strcmp(*argv, "tos") == 0 || |
| 520 | matches(*argv, "dsfield") == 0) { |
| 521 | __u32 tos; |
| 522 | NEXT_ARG(); |
| 523 | if (rtnl_dsfield_a2n(&tos, *argv)) |
| 524 | invarg("\"tos\" value is invalid\n", *argv); |
| 525 | req.r.rtm_tos = tos; |
| 526 | } else if (strcmp(*argv, "hoplimit") == 0) { |
| 527 | unsigned hoplimit; |
| 528 | NEXT_ARG(); |
| 529 | if (strcmp(*argv, "lock") == 0) { |
| 530 | mxlock |= (1<<RTAX_HOPLIMIT); |
| 531 | NEXT_ARG(); |
| 532 | } |
| 533 | if (get_unsigned(&hoplimit, *argv, 0)) |
| 534 | invarg("\"hoplimit\" value is invalid\n", *argv); |
| 535 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit); |
Denys Vlasenko | 1140bf3 | 2017-04-06 17:54:38 +0200 | [diff] [blame] | 536 | } else if (matches(*argv, "reordering") == 0) { |
| 537 | unsigned reord; |
| 538 | NEXT_ARG(); |
| 539 | if (strcmp(*argv, "lock") == 0) { |
| 540 | mxlock |= (1<<RTAX_REORDERING); |
| 541 | NEXT_ARG(); |
| 542 | } |
| 543 | if (get_unsigned(&reord, *argv, 0)) |
| 544 | invarg("\"reordering\" value is invalid\n", *argv); |
| 545 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord); |
| 546 | } else if (strcmp(*argv, "rtt") == 0) { |
| 547 | unsigned rtt; |
| 548 | NEXT_ARG(); |
| 549 | if (strcmp(*argv, "lock") == 0) { |
| 550 | mxlock |= (1<<RTAX_RTT); |
| 551 | NEXT_ARG(); |
| 552 | } |
| 553 | if (get_time_rtt(&rtt, *argv, &raw)) |
| 554 | invarg("\"rtt\" value is invalid\n", *argv); |
| 555 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, |
| 556 | (raw) ? rtt : rtt * 8); |
| 557 | } else if (strcmp(*argv, "rto_min") == 0) { |
| 558 | unsigned rto_min; |
| 559 | NEXT_ARG(); |
| 560 | mxlock |= (1<<RTAX_RTO_MIN); |
| 561 | if (get_time_rtt(&rto_min, *argv, &raw)) |
| 562 | invarg("\"rto_min\" value is invalid\n", |
| 563 | *argv); |
| 564 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN, |
| 565 | rto_min); |
| 566 | } else if (matches(*argv, "window") == 0) { |
| 567 | unsigned win; |
| 568 | NEXT_ARG(); |
| 569 | if (strcmp(*argv, "lock") == 0) { |
| 570 | mxlock |= (1<<RTAX_WINDOW); |
| 571 | NEXT_ARG(); |
| 572 | } |
| 573 | if (get_unsigned(&win, *argv, 0)) |
| 574 | invarg("\"window\" value is invalid\n", *argv); |
| 575 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win); |
| 576 | } else if (matches(*argv, "cwnd") == 0) { |
| 577 | unsigned win; |
| 578 | NEXT_ARG(); |
| 579 | if (strcmp(*argv, "lock") == 0) { |
| 580 | mxlock |= (1<<RTAX_CWND); |
| 581 | NEXT_ARG(); |
| 582 | } |
| 583 | if (get_unsigned(&win, *argv, 0)) |
| 584 | invarg("\"cwnd\" value is invalid\n", *argv); |
| 585 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win); |
| 586 | } else if (matches(*argv, "initcwnd") == 0) { |
| 587 | unsigned win; |
| 588 | NEXT_ARG(); |
| 589 | if (strcmp(*argv, "lock") == 0) { |
| 590 | mxlock |= (1<<RTAX_INITCWND); |
| 591 | NEXT_ARG(); |
| 592 | } |
| 593 | if (get_unsigned(&win, *argv, 0)) |
| 594 | invarg("\"initcwnd\" value is invalid\n", *argv); |
| 595 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win); |
| 596 | } else if (matches(*argv, "initrwnd") == 0) { |
| 597 | unsigned win; |
| 598 | NEXT_ARG(); |
| 599 | if (strcmp(*argv, "lock") == 0) { |
| 600 | mxlock |= (1<<RTAX_INITRWND); |
| 601 | NEXT_ARG(); |
| 602 | } |
| 603 | if (get_unsigned(&win, *argv, 0)) |
| 604 | invarg("\"initrwnd\" value is invalid\n", *argv); |
| 605 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win); |
| 606 | } else if (matches(*argv, "features") == 0) { |
| 607 | unsigned int features = 0; |
| 608 | |
| 609 | while (argc > 0) { |
| 610 | NEXT_ARG(); |
| 611 | |
| 612 | if (strcmp(*argv, "ecn") == 0) |
| 613 | features |= RTAX_FEATURE_ECN; |
| 614 | else |
| 615 | invarg("\"features\" value not valid\n", *argv); |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_FEATURES, features); |
| 620 | } else if (matches(*argv, "quickack") == 0) { |
| 621 | unsigned quickack; |
| 622 | NEXT_ARG(); |
| 623 | if (get_unsigned(&quickack, *argv, 0)) |
| 624 | invarg("\"quickack\" value is invalid\n", *argv); |
| 625 | if (quickack != 1 && quickack != 0) |
| 626 | invarg("\"quickack\" value should be 0 or 1\n", *argv); |
| 627 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_QUICKACK, quickack); |
| 628 | } else if (matches(*argv, "rttvar") == 0) { |
| 629 | unsigned win; |
| 630 | NEXT_ARG(); |
| 631 | if (strcmp(*argv, "lock") == 0) { |
| 632 | mxlock |= (1<<RTAX_RTTVAR); |
| 633 | NEXT_ARG(); |
| 634 | } |
| 635 | if (get_time_rtt(&win, *argv, &raw)) |
| 636 | invarg("\"rttvar\" value is invalid\n", *argv); |
| 637 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR, |
| 638 | (raw) ? win : win * 4); |
| 639 | } else if (matches(*argv, "ssthresh") == 0) { |
| 640 | unsigned win; |
| 641 | NEXT_ARG(); |
| 642 | if (strcmp(*argv, "lock") == 0) { |
| 643 | mxlock |= (1<<RTAX_SSTHRESH); |
| 644 | NEXT_ARG(); |
| 645 | } |
| 646 | if (get_unsigned(&win, *argv, 0)) |
| 647 | invarg("\"ssthresh\" value is invalid\n", *argv); |
| 648 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win); |
| 649 | } else if (matches(*argv, "realms") == 0) { |
| 650 | __u32 realm; |
| 651 | NEXT_ARG(); |
| 652 | if (get_rt_realms(&realm, *argv)) |
| 653 | invarg("\"realm\" value is invalid\n", *argv); |
| 654 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); |
| 655 | } else if (strcmp(*argv, "nexthop") == 0) { |
| 656 | nhs_ok = 1; |
| 657 | break; |
| 658 | } |
| 659 | #endif |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 660 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 663 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 664 | |
Eric Andersen | 5780adb | 2002-11-15 09:12:47 +0000 | [diff] [blame] | 665 | if (d) { |
| 666 | int idx; |
| 667 | |
| 668 | ll_init_map(&rth); |
| 669 | |
| 670 | if (d) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 671 | idx = xll_name_to_index(d); |
Eric Andersen | 5780adb | 2002-11-15 09:12:47 +0000 | [diff] [blame] | 672 | addattr32(&req.n, sizeof(req), RTA_OIF, idx); |
| 673 | } |
| 674 | } |
| 675 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 676 | if (mxrta->rta_len > RTA_LENGTH(0)) { |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 677 | if (mxlock) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 678 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 679 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 680 | addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta)); |
| 681 | } |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 682 | |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 683 | if (!scope_ok) { |
| 684 | if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT) |
| 685 | req.r.rtm_scope = RT_SCOPE_HOST; |
| 686 | else |
| 687 | if (req.r.rtm_type == RTN_BROADCAST |
| 688 | || req.r.rtm_type == RTN_MULTICAST |
| 689 | || req.r.rtm_type == RTN_ANYCAST |
| 690 | ) { |
Bernhard Reutner-Fischer | 12c96a6 | 2007-04-11 16:23:57 +0000 | [diff] [blame] | 691 | req.r.rtm_scope = RT_SCOPE_LINK; |
Denys Vlasenko | ce4bc1e | 2015-12-30 17:32:51 +0100 | [diff] [blame] | 692 | } |
| 693 | else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) { |
| 694 | if (cmd == RTM_DELROUTE) |
| 695 | req.r.rtm_scope = RT_SCOPE_NOWHERE; |
| 696 | else if (!(ok & gw_ok)) |
| 697 | req.r.rtm_scope = RT_SCOPE_LINK; |
| 698 | } |
Bernhard Reutner-Fischer | 12c96a6 | 2007-04-11 16:23:57 +0000 | [diff] [blame] | 699 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 700 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 701 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 702 | req.r.rtm_family = AF_INET; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 703 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 704 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 705 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 706 | return 2; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 707 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int rtnl_rtcache_request(struct rtnl_handle *rth, int family) |
| 713 | { |
| 714 | struct { |
| 715 | struct nlmsghdr nlh; |
| 716 | struct rtmsg rtm; |
| 717 | } req; |
| 718 | struct sockaddr_nl nladdr; |
| 719 | |
| 720 | memset(&nladdr, 0, sizeof(nladdr)); |
| 721 | memset(&req, 0, sizeof(req)); |
| 722 | nladdr.nl_family = AF_NETLINK; |
| 723 | |
| 724 | req.nlh.nlmsg_len = sizeof(req); |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 725 | if (RTM_GETROUTE) |
| 726 | req.nlh.nlmsg_type = RTM_GETROUTE; |
| 727 | if (NLM_F_ROOT | NLM_F_REQUEST) |
| 728 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST; |
| 729 | /*req.nlh.nlmsg_pid = 0; - memset did it already */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 730 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 731 | req.rtm.rtm_family = family; |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 732 | if (RTM_F_CLONED) |
| 733 | req.rtm.rtm_flags = RTM_F_CLONED; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 734 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 735 | 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] | 736 | } |
| 737 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 738 | static void iproute_flush_cache(void) |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 739 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 740 | static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush"; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 741 | int flush_fd = open_or_warn(fn, O_WRONLY); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 742 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 743 | if (flush_fd < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 744 | return; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 747 | if (write(flush_fd, "-1", 2) < 2) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 748 | bb_perror_msg("can't flush routing cache"); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 749 | return; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 750 | } |
| 751 | close(flush_fd); |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 754 | static void iproute_reset_filter(void) |
| 755 | { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 756 | memset(&G_filter, 0, sizeof(G_filter)); |
| 757 | G_filter.mdst.bitlen = -1; |
| 758 | G_filter.msrc.bitlen = -1; |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 761 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 762 | static int iproute_list_or_flush(char **argv, int flush) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 763 | { |
| 764 | int do_ipv6 = preferred_family; |
| 765 | struct rtnl_handle rth; |
| 766 | char *id = NULL; |
| 767 | char *od = NULL; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 768 | static const char keywords[] ALIGN1 = |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 769 | /* If you add stuff here, update iproute_full_usage */ |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 770 | /* "ip route list/flush" parameters: */ |
| 771 | "protocol\0" "dev\0" "oif\0" "iif\0" |
| 772 | "via\0" "table\0" "cache\0" |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 773 | "from\0" "to\0" "scope\0" |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 774 | /* and possible further keywords */ |
| 775 | "all\0" |
| 776 | "root\0" |
| 777 | "match\0" |
| 778 | "exact\0" |
| 779 | "main\0" |
| 780 | ; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 781 | enum { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 782 | KW_proto, KW_dev, KW_oif, KW_iif, |
| 783 | KW_via, KW_table, KW_cache, |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 784 | KW_from, KW_to, KW_scope, |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 785 | /* */ |
| 786 | KW_all, |
| 787 | KW_root, |
| 788 | KW_match, |
| 789 | KW_exact, |
| 790 | KW_main, |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 791 | }; |
| 792 | int arg, parm; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 793 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 794 | iproute_reset_filter(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 795 | G_filter.tb = RT_TABLE_MAIN; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 796 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 797 | if (flush && !*argv) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 798 | 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] | 799 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 800 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 801 | arg = index_in_substrings(keywords, *argv); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 802 | if (arg == KW_proto) { |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 803 | uint32_t prot = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 804 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 805 | //G_filter.protocolmask = -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 806 | if (rtnl_rtprot_a2n(&prot, *argv)) { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 807 | if (index_in_strings(keywords, *argv) != KW_all) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 808 | invarg_1_to_2(*argv, "protocol"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 809 | prot = 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 810 | //G_filter.protocolmask = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 811 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 812 | //G_filter.protocol = prot; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 813 | } else if (arg == KW_dev || arg == KW_oif) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 814 | NEXT_ARG(); |
| 815 | od = *argv; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 816 | } else if (arg == KW_iif) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 817 | NEXT_ARG(); |
| 818 | id = *argv; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 819 | } else if (arg == KW_via) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 820 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 821 | get_prefix(&G_filter.rvia, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 822 | } else if (arg == KW_table) { /* table all/cache/main */ |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 823 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 824 | parm = index_in_substrings(keywords, *argv); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 825 | if (parm == KW_cache) |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 826 | G_filter.tb = -1; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 827 | else if (parm == KW_all) |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 828 | G_filter.tb = 0; |
Denis Vlasenko | 52a8d97 | 2008-06-08 00:25:55 +0000 | [diff] [blame] | 829 | else if (parm != KW_main) { |
| 830 | #if ENABLE_FEATURE_IP_RULE |
| 831 | uint32_t tid; |
| 832 | if (rtnl_rttable_a2n(&tid, *argv)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 833 | invarg_1_to_2(*argv, "table"); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 834 | G_filter.tb = tid; |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 835 | #else |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 836 | invarg_1_to_2(*argv, "table"); |
Denis Vlasenko | 52a8d97 | 2008-06-08 00:25:55 +0000 | [diff] [blame] | 837 | #endif |
| 838 | } |
Denis Vlasenko | 79c6904 | 2007-11-27 09:42:33 +0000 | [diff] [blame] | 839 | } else if (arg == KW_cache) { |
| 840 | /* The command 'ip route flush cache' is used by OpenSWAN. |
| 841 | * Assuming it's a synonym for 'ip route flush table cache' */ |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 842 | G_filter.tb = -1; |
André Draszik | 2f24d30 | 2017-06-13 19:59:59 +0200 | [diff] [blame] | 843 | } else if (arg == KW_scope) { |
| 844 | uint32_t scope; |
| 845 | NEXT_ARG(); |
| 846 | G_filter.scopemask = -1; |
| 847 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 848 | if (strcmp(*argv, "all") != 0) |
| 849 | invarg_1_to_2(*argv, "scope"); |
| 850 | scope = RT_SCOPE_NOWHERE; |
| 851 | G_filter.scopemask = 0; |
| 852 | } |
| 853 | G_filter.scope = scope; |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 854 | } else if (arg == KW_from) { |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 855 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 856 | parm = index_in_substrings(keywords, *argv); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 857 | if (parm == KW_root) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 858 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 859 | get_prefix(&G_filter.rsrc, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 860 | } else if (parm == KW_match) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 861 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 862 | get_prefix(&G_filter.msrc, *argv, do_ipv6); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 863 | } else { |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 864 | if (parm == KW_exact) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 865 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 866 | get_prefix(&G_filter.msrc, *argv, do_ipv6); |
| 867 | G_filter.rsrc = G_filter.msrc; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 868 | } |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 869 | } else { /* "to" is the default parameter */ |
| 870 | if (arg == KW_to) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 871 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 872 | arg = index_in_substrings(keywords, *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 873 | } |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 874 | /* parm = arg; - would be more plausible, but we reuse 'arg' here */ |
| 875 | if (arg == KW_root) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 876 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 877 | get_prefix(&G_filter.rdst, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 878 | } else if (arg == KW_match) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 879 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 880 | get_prefix(&G_filter.mdst, *argv, do_ipv6); |
Denis Vlasenko | 186c2b3 | 2007-11-26 18:29:52 +0000 | [diff] [blame] | 881 | } else { /* "to exact" is the default */ |
| 882 | if (arg == KW_exact) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 883 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 884 | get_prefix(&G_filter.mdst, *argv, do_ipv6); |
| 885 | G_filter.rdst = G_filter.mdst; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 886 | } |
| 887 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 888 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 891 | if (do_ipv6 == AF_UNSPEC && G_filter.tb) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 892 | do_ipv6 = AF_INET; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 893 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 894 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 895 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 896 | ll_init_map(&rth); |
| 897 | |
| 898 | if (id || od) { |
| 899 | int idx; |
| 900 | |
| 901 | if (id) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 902 | idx = xll_name_to_index(id); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 903 | G_filter.iif = idx; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 904 | } |
| 905 | if (od) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 906 | idx = xll_name_to_index(od); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 907 | G_filter.oif = idx; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 911 | if (flush) { |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 912 | char flushb[4096-512]; |
| 913 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 914 | if (G_filter.tb == -1) { /* "flush table cache" */ |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 915 | if (do_ipv6 != AF_INET6) |
| 916 | iproute_flush_cache(); |
| 917 | if (do_ipv6 == AF_INET) |
| 918 | return 0; |
| 919 | } |
| 920 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 921 | G_filter.flushb = flushb; |
| 922 | G_filter.flushp = 0; |
| 923 | G_filter.flushe = sizeof(flushb); |
| 924 | G_filter.rth = &rth; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 925 | |
| 926 | for (;;) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 927 | xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 928 | G_filter.flushed = 0; |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 929 | xrtnl_dump_filter(&rth, print_route, NULL); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 930 | if (G_filter.flushed == 0) |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 931 | return 0; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 932 | if (flush_update()) |
| 933 | return 1; |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 937 | if (G_filter.tb != -1) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 938 | xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 939 | } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 940 | bb_perror_msg_and_die("can't send dump request"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 941 | } |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 942 | xrtnl_dump_filter(&rth, print_route, NULL); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 943 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 944 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 948 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 949 | static int iproute_get(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 950 | { |
| 951 | struct rtnl_handle rth; |
| 952 | struct { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 953 | struct nlmsghdr n; |
| 954 | struct rtmsg r; |
| 955 | char buf[1024]; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 956 | } req; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 957 | char *idev = NULL; |
| 958 | char *odev = NULL; |
Bernhard Reutner-Fischer | c98c317 | 2007-04-12 11:36:56 +0000 | [diff] [blame] | 959 | bool connected = 0; |
| 960 | bool from_ok = 0; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 961 | static const char options[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 962 | "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] | 963 | |
| 964 | memset(&req, 0, sizeof(req)); |
| 965 | |
| 966 | iproute_reset_filter(); |
| 967 | |
| 968 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 969 | if (NLM_F_REQUEST) |
| 970 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 971 | if (RTM_GETROUTE) |
| 972 | req.n.nlmsg_type = RTM_GETROUTE; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 973 | req.r.rtm_family = preferred_family; |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 974 | /*req.r.rtm_table = 0; - memset did this already */ |
| 975 | /*req.r.rtm_protocol = 0;*/ |
| 976 | /*req.r.rtm_scope = 0;*/ |
| 977 | /*req.r.rtm_type = 0;*/ |
| 978 | /*req.r.rtm_src_len = 0;*/ |
| 979 | /*req.r.rtm_dst_len = 0;*/ |
| 980 | /*req.r.rtm_tos = 0;*/ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 981 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 982 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 983 | switch (index_in_strings(options, *argv)) { |
Glenn L McGrath | 18eae00 | 2002-12-02 00:54:10 +0000 | [diff] [blame] | 984 | case 0: /* from */ |
| 985 | { |
| 986 | inet_prefix addr; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 987 | NEXT_ARG(); |
Glenn L McGrath | 18eae00 | 2002-12-02 00:54:10 +0000 | [diff] [blame] | 988 | from_ok = 1; |
| 989 | get_prefix(&addr, *argv, req.r.rtm_family); |
| 990 | if (req.r.rtm_family == AF_UNSPEC) { |
| 991 | req.r.rtm_family = addr.family; |
| 992 | } |
| 993 | if (addr.bytelen) { |
| 994 | addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); |
| 995 | } |
| 996 | req.r.rtm_src_len = addr.bitlen; |
| 997 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 998 | } |
Glenn L McGrath | 18eae00 | 2002-12-02 00:54:10 +0000 | [diff] [blame] | 999 | case 1: /* iif */ |
| 1000 | NEXT_ARG(); |
| 1001 | idev = *argv; |
| 1002 | break; |
| 1003 | case 2: /* oif */ |
| 1004 | case 3: /* dev */ |
| 1005 | NEXT_ARG(); |
| 1006 | odev = *argv; |
| 1007 | break; |
| 1008 | case 4: /* notify */ |
| 1009 | req.r.rtm_flags |= RTM_F_NOTIFY; |
| 1010 | break; |
| 1011 | case 5: /* connected */ |
| 1012 | connected = 1; |
| 1013 | break; |
| 1014 | case 6: /* to */ |
| 1015 | NEXT_ARG(); |
| 1016 | default: |
| 1017 | { |
| 1018 | inet_prefix addr; |
| 1019 | get_prefix(&addr, *argv, req.r.rtm_family); |
| 1020 | if (req.r.rtm_family == AF_UNSPEC) { |
| 1021 | req.r.rtm_family = addr.family; |
| 1022 | } |
| 1023 | if (addr.bytelen) { |
| 1024 | addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen); |
| 1025 | } |
| 1026 | req.r.rtm_dst_len = addr.bitlen; |
| 1027 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1028 | } |
Christian Hornung | 3bbfb58 | 2010-11-03 14:08:00 +0100 | [diff] [blame] | 1029 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | if (req.r.rtm_dst_len == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1033 | bb_error_msg_and_die("need at least destination address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 1036 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1037 | |
| 1038 | ll_init_map(&rth); |
| 1039 | |
| 1040 | if (idev || odev) { |
| 1041 | int idx; |
| 1042 | |
| 1043 | if (idev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 1044 | idx = xll_name_to_index(idev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1045 | addattr32(&req.n, sizeof(req), RTA_IIF, idx); |
| 1046 | } |
| 1047 | if (odev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 1048 | idx = xll_name_to_index(odev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1049 | addattr32(&req.n, sizeof(req), RTA_OIF, idx); |
| 1050 | } |
| 1051 | } |
| 1052 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1053 | if (req.r.rtm_family == AF_UNSPEC) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1054 | req.r.rtm_family = AF_INET; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1055 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1056 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1057 | 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] | 1058 | return 2; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1059 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (connected && !from_ok) { |
| 1062 | struct rtmsg *r = NLMSG_DATA(&req.n); |
| 1063 | int len = req.n.nlmsg_len; |
| 1064 | struct rtattr * tb[RTA_MAX+1]; |
| 1065 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 1066 | print_route(NULL, &req.n, NULL); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (req.n.nlmsg_type != RTM_NEWROUTE) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 1069 | bb_error_msg_and_die("not a route?"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1070 | } |
| 1071 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 1072 | if (len < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 1073 | bb_error_msg_and_die("wrong len %d", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | memset(tb, 0, sizeof(tb)); |
| 1077 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 1078 | |
| 1079 | if (tb[RTA_PREFSRC]) { |
| 1080 | tb[RTA_PREFSRC]->rta_type = RTA_SRC; |
| 1081 | r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]); |
| 1082 | } else if (!tb[RTA_SRC]) { |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1083 | bb_error_msg_and_die("can't connect the route"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1084 | } |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1085 | if (!odev && tb[RTA_OIF]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1086 | tb[RTA_OIF]->rta_type = 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1087 | } |
| 1088 | if (tb[RTA_GATEWAY]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1089 | tb[RTA_GATEWAY]->rta_type = 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1090 | } |
| 1091 | if (!idev && tb[RTA_IIF]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1092 | tb[RTA_IIF]->rta_type = 0; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1093 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1094 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 1095 | req.n.nlmsg_type = RTM_GETROUTE; |
| 1096 | |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1097 | 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] | 1098 | return 2; |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1099 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1100 | } |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 1101 | print_route(NULL, &req.n, NULL); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 1102 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 1105 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 1106 | int FAST_FUNC do_iproute(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1107 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1108 | static const char ip_route_commands[] ALIGN1 = |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1109 | "a\0""add\0""append\0""change\0""chg\0" |
| 1110 | "delete\0""get\0""list\0""show\0" |
| 1111 | "prepend\0""replace\0""test\0""flush\0" |
| 1112 | ; |
| 1113 | enum { |
| 1114 | CMD_a = 0, CMD_add, CMD_append, CMD_change, CMD_chg, |
| 1115 | CMD_delete, CMD_get, CMD_list, CMD_show, |
| 1116 | CMD_prepend, CMD_replace, CMD_test, CMD_flush, |
| 1117 | }; |
Denis Vlasenko | 83c4422 | 2008-01-04 15:28:28 +0000 | [diff] [blame] | 1118 | int command_num; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1119 | unsigned flags = 0; |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1120 | int cmd = RTM_NEWROUTE; |
| 1121 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 1122 | INIT_G(); |
| 1123 | |
Denis Vlasenko | 83c4422 | 2008-01-04 15:28:28 +0000 | [diff] [blame] | 1124 | if (!*argv) |
| 1125 | return iproute_list_or_flush(argv, 0); |
| 1126 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 1127 | /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */ |
| 1128 | /* It probably means that it is using "first match" rule */ |
Denis Vlasenko | 83c4422 | 2008-01-04 15:28:28 +0000 | [diff] [blame] | 1129 | command_num = index_in_substrings(ip_route_commands, *argv); |
| 1130 | |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 1131 | switch (command_num) { |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1132 | case CMD_a: |
| 1133 | case CMD_add: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1134 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 1135 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1136 | case CMD_append: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1137 | flags = NLM_F_CREATE|NLM_F_APPEND; |
| 1138 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1139 | case CMD_change: |
| 1140 | case CMD_chg: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1141 | flags = NLM_F_REPLACE; |
| 1142 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1143 | case CMD_delete: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1144 | cmd = RTM_DELROUTE; |
| 1145 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1146 | case CMD_get: |
| 1147 | return iproute_get(argv + 1); |
| 1148 | case CMD_list: |
| 1149 | case CMD_show: |
| 1150 | return iproute_list_or_flush(argv + 1, 0); |
| 1151 | case CMD_prepend: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1152 | flags = NLM_F_CREATE; |
Denis Vlasenko | 1eecaf2 | 2007-09-30 16:04:21 +0000 | [diff] [blame] | 1153 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1154 | case CMD_replace: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1155 | flags = NLM_F_CREATE|NLM_F_REPLACE; |
Denis Vlasenko | 1eecaf2 | 2007-09-30 16:04:21 +0000 | [diff] [blame] | 1156 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1157 | case CMD_test: |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1158 | flags = NLM_F_EXCL; |
Denis Vlasenko | 1eecaf2 | 2007-09-30 16:04:21 +0000 | [diff] [blame] | 1159 | break; |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1160 | case CMD_flush: |
| 1161 | return iproute_list_or_flush(argv + 1, 1); |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1162 | default: |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 1163 | invarg_1_to_2(*argv, applet_name); |
Glenn L McGrath | 1652855 | 2002-11-28 11:17:19 +0000 | [diff] [blame] | 1164 | } |
Glenn L McGrath | c82f232 | 2002-12-02 00:35:23 +0000 | [diff] [blame] | 1165 | |
Denys Vlasenko | eb76abb | 2017-04-07 17:33:26 +0200 | [diff] [blame] | 1166 | return iproute_modify(cmd, flags, argv + 1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1167 | } |