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