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