blob: 0f2b89682d2e92b50bec8bce1327aceeb2d78ae7 [file] [log] [blame]
Bernhard Reutner-Fischerd1d23a62006-01-12 12:08:46 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Changes:
8 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02009 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010 * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
11 */
12
Denys Vlasenkofb132e42010-10-29 11:46:52 +020013#include "ip_common.h" /* #include "libbb.h" is inside */
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020014#include "common_bufsiz.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "rt_names.h"
16#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#ifndef RTAX_RTTVAR
19#define RTAX_RTTVAR RTAX_HOPS
20#endif
21
22
Denys Vlasenko36659fd2010-02-05 14:40:23 +010023struct filter_t {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024 int tb;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000025 smallint flushed;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000026 char *flushb;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027 int flushp;
28 int flushe;
29 struct rtnl_handle *rth;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010030 //int protocol, protocolmask; - write-only fields?!
31 //int scope, scopemask; - unused
32 //int type; - read-only
33 //int typemask; - unused
34 //int tos, tosmask; - unused
Denys Vlasenkof1334712011-02-09 04:39:09 +010035 int iif;
36 int oif;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010037 //int realm, realmmask; - unused
38 //inet_prefix rprefsrc; - read-only
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039 inet_prefix rvia;
40 inet_prefix rdst;
41 inet_prefix mdst;
42 inet_prefix rsrc;
43 inet_prefix msrc;
Denys Vlasenko36659fd2010-02-05 14:40:23 +010044} FIX_ALIASING;
45typedef struct filter_t filter_t;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000046
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020047#define G_filter (*(filter_t*)bb_common_bufsiz1)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020048#define INIT_G() do { setup_common_bufsiz(); } while (0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000050static int flush_update(void)
51{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010052 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +010053 bb_perror_msg("can't send flush request");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000054 return -1;
55 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010056 G_filter.flushp = 0;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000057 return 0;
58}
59
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020060static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 struct rtmsg *r = NLMSG_DATA(n);
64 int len = n->nlmsg_len;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +010065 struct rtattr *tb[RTA_MAX+1];
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +000066 inet_prefix dst;
67 inet_prefix src;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068 int host_len = -1;
Lukasz Nowakb42107f2016-12-13 12:58:31 +000069 uint32_t tid;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
72 fprintf(stderr, "Not a route: %08x %08x %08x\n",
73 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
74 return 0;
75 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010076 if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000077 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078 len -= NLMSG_LENGTH(sizeof(*r));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000079 if (len < 0)
80 bb_error_msg_and_die("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081
Lukasz Nowakb42107f2016-12-13 12:58:31 +000082 memset(tb, 0, sizeof(tb));
83 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
84
85 if (tb[RTA_TABLE])
86 tid = *(uint32_t *)RTA_DATA(tb[RTA_TABLE]);
87 else
88 tid = r->rtm_table;
89
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 if (r->rtm_family == AF_INET6)
91 host_len = 128;
92 else if (r->rtm_family == AF_INET)
93 host_len = 32;
94
95 if (r->rtm_family == AF_INET6) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010096 if (G_filter.tb) {
97 if (G_filter.tb < 0) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +000098 if (!(r->rtm_flags & RTM_F_CLONED)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000100 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000102 if (r->rtm_flags & RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000103 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000104 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100105 if (G_filter.tb == RT_TABLE_LOCAL) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000106 if (r->rtm_type != RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000108 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100109 } else if (G_filter.tb == RT_TABLE_MAIN) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000110 if (r->rtm_type == RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000112 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 } else {
114 return 0;
115 }
116 }
117 }
118 } else {
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000119 if (G_filter.tb > 0 && G_filter.tb != tid) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000121 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100123 if (G_filter.rdst.family
124 && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
125 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000127 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100128 if (G_filter.mdst.family
129 && (r->rtm_family != G_filter.mdst.family
130 || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
131 )
132 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000134 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100135 if (G_filter.rsrc.family
136 && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
137 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000139 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100140 if (G_filter.msrc.family
141 && (r->rtm_family != G_filter.msrc.family
142 || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
143 )
144 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000146 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000147
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100148 memset(&src, 0, sizeof(src));
149 memset(&dst, 0, sizeof(dst));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100151 if (tb[RTA_SRC]) {
152 src.bitlen = r->rtm_src_len;
153 src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
154 memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen);
155 }
156 if (tb[RTA_DST]) {
157 dst.bitlen = r->rtm_dst_len;
158 dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
159 memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen);
160 }
161
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100162 if (G_filter.rdst.family
163 && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
164 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000165 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100166 }
167 if (G_filter.mdst.family
168 && G_filter.mdst.bitlen >= 0
169 && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
170 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000171 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100172 }
173 if (G_filter.rsrc.family
174 && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
175 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000176 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100177 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100178 if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
179 && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
180 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000181 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100182 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100183 if (G_filter.oif != 0) {
184 if (!tb[RTA_OIF])
185 return 0;
186 if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF]))
187 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100188 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000189
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100190 if (G_filter.flushb) {
Denys Vlasenkoe3ece782011-02-10 09:50:07 +0100191 struct nlmsghdr *fn;
192
Denys Vlasenkof1334712011-02-09 04:39:09 +0100193 /* We are creating route flush commands */
194
195 if (r->rtm_family == AF_INET6
196 && r->rtm_dst_len == 0
197 && r->rtm_type == RTN_UNREACHABLE
198 && tb[RTA_PRIORITY]
199 && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
200 ) {
201 return 0;
202 }
203
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100204 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000205 if (flush_update())
Curt Brune69934702015-10-14 12:53:47 +0200206 xfunc_die();
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000207 }
Denys Vlasenkoe3ece782011-02-10 09:50:07 +0100208 fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000209 memcpy(fn, n, n->nlmsg_len);
210 fn->nlmsg_type = RTM_DELROUTE;
211 fn->nlmsg_flags = NLM_F_REQUEST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100212 fn->nlmsg_seq = ++G_filter.rth->seq;
213 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
214 G_filter.flushed = 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000215 return 0;
216 }
217
Denys Vlasenkof1334712011-02-09 04:39:09 +0100218 /* We are printing routes */
219
Glenn L McGrath16528552002-11-28 11:17:19 +0000220 if (n->nlmsg_type == RTM_DELROUTE) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000221 printf("Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000222 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100223 if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200224 printf("%s ", rtnl_rtntype_n2a(r->rtm_type));
Glenn L McGrath16528552002-11-28 11:17:19 +0000225 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226
227 if (tb[RTA_DST]) {
228 if (r->rtm_dst_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200229 printf("%s/%u ",
230 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])),
231 r->rtm_dst_len
232 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000233 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000234 printf("%s ", format_host(r->rtm_family,
235 RTA_PAYLOAD(tb[RTA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200236 RTA_DATA(tb[RTA_DST]))
237 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238 }
239 } else if (r->rtm_dst_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000240 printf("0/%d ", r->rtm_dst_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000242 printf("default ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 }
244 if (tb[RTA_SRC]) {
245 if (r->rtm_src_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200246 printf("from %s/%u ",
247 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
248 r->rtm_src_len
249 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000251 printf("from %s ", format_host(r->rtm_family,
252 RTA_PAYLOAD(tb[RTA_SRC]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200253 RTA_DATA(tb[RTA_SRC]))
254 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 }
256 } else if (r->rtm_src_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000257 printf("from 0/%u ", r->rtm_src_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000258 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100259 if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000260 printf("via %s ", format_host(r->rtm_family,
261 RTA_PAYLOAD(tb[RTA_GATEWAY]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200262 RTA_DATA(tb[RTA_GATEWAY]))
263 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100265 if (tb[RTA_OIF]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000266 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000267 }
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000268#if ENABLE_FEATURE_IP_RULE
269 if (tid && tid != RT_TABLE_MAIN && !G_filter.tb)
270 printf("table %s ", rtnl_rttable_n2a(tid));
271#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272
Denys Vlasenkof1334712011-02-09 04:39:09 +0100273 /* Todo: parse & show "proto kernel", "scope link" here */
274
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100275 if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 /* Do not use format_host(). It is our local addr
277 and symbolic name will not be useful.
278 */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000279 printf(" src %s ", rt_addr_n2a(r->rtm_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200280 RTA_DATA(tb[RTA_PREFSRC])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000282 if (tb[RTA_PRIORITY]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000283 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000284 }
Michael Tokarev1a114392014-07-28 10:05:41 +0400285 if (r->rtm_flags & RTNH_F_DEAD) {
286 printf("dead ");
287 }
288 if (r->rtm_flags & RTNH_F_ONLINK) {
289 printf("onlink ");
290 }
291 if (r->rtm_flags & RTNH_F_PERVASIVE) {
292 printf("pervasive ");
293 }
294 if (r->rtm_flags & RTM_F_NOTIFY) {
295 printf("notify ");
296 }
297
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298 if (r->rtm_family == AF_INET6) {
299 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000300 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000301 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000302 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000304 if (r->rtm_flags & RTM_F_CLONED) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000305 printf("%c cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000306 }
307 if (ci->rta_expires) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000308 printf(" expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000309 }
310 if (ci->rta_error != 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000311 printf(" error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000312 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000313 } else if (ci) {
314 if (ci->rta_error != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000315 printf(" error %d", ci->rta_error);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316 }
317 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100318 if (tb[RTA_IIF] && G_filter.iif == 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000319 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000320 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000321 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000322 return 0;
323}
324
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000325/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000326static int iproute_modify(int cmd, unsigned flags, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000327{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000328 static const char keywords[] ALIGN1 =
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100329 "src\0""via\0""mtu\0""lock\0""scope\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
Michael Tokarev1a114392014-07-28 10:05:41 +0400330 "dev\0""oif\0""to\0""metric\0""onlink\0";
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000331 enum {
332 ARG_src,
333 ARG_via,
334 ARG_mtu, PARM_lock,
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100335 ARG_scope,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000336 ARG_protocol,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000337IF_FEATURE_IP_RULE(ARG_table,)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000338 ARG_dev,
339 ARG_oif,
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000340 ARG_to,
341 ARG_metric,
Michael Tokarev1a114392014-07-28 10:05:41 +0400342 ARG_onlink,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000343 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000344 enum {
345 gw_ok = 1 << 0,
346 dst_ok = 1 << 1,
347 proto_ok = 1 << 2,
348 type_ok = 1 << 3
349 };
350 struct rtnl_handle rth;
351 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200352 struct nlmsghdr n;
353 struct rtmsg r;
354 char buf[1024];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000355 } req;
356 char mxbuf[256];
357 struct rtattr * mxrta = (void*)mxbuf;
358 unsigned mxlock = 0;
359 char *d = NULL;
360 smalluint ok = 0;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100361 smalluint scope_ok = 0;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000362 int arg;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000363
364 memset(&req, 0, sizeof(req));
365
366 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000367 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000368 req.n.nlmsg_type = cmd;
369 req.r.rtm_family = preferred_family;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100370 if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000371 req.r.rtm_table = RT_TABLE_MAIN;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100372 if (RT_SCOPE_NOWHERE != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000373 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374
375 if (cmd != RTM_DELROUTE) {
Denys Vlasenko34ecc3b2016-08-14 01:30:34 +0200376 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100377 if (RTPROT_BOOT != 0)
378 req.r.rtm_protocol = RTPROT_BOOT;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100379 if (RTN_UNICAST != 0)
380 req.r.rtm_type = RTN_UNICAST;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000381 }
382
383 mxrta->rta_type = RTA_METRICS;
384 mxrta->rta_len = RTA_LENGTH(0);
385
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000386 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000387 arg = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000388 if (arg == ARG_src) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000389 inet_prefix addr;
390 NEXT_ARG();
391 get_addr(&addr, *argv, req.r.rtm_family);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000392 if (req.r.rtm_family == AF_UNSPEC)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000393 req.r.rtm_family = addr.family;
394 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000395 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000396 inet_prefix addr;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000397 ok |= gw_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000398 NEXT_ARG();
399 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000400 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000402 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000403 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000404 } else if (arg == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000405 unsigned mtu;
406 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000407 if (index_in_strings(keywords, *argv) == PARM_lock) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000408 mxlock |= (1 << RTAX_MTU);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000409 NEXT_ARG();
410 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000411 mtu = get_unsigned(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000412 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100413 } else if (arg == ARG_scope) {
414 uint32_t scope;
415 NEXT_ARG();
416 if (rtnl_rtscope_a2n(&scope, *argv))
417 invarg_1_to_2(*argv, "scope");
418 req.r.rtm_scope = scope;
419 scope_ok = 1;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000420 } else if (arg == ARG_protocol) {
Eric Andersend78aea82006-01-30 18:00:02 +0000421 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000422 NEXT_ARG();
423 if (rtnl_rtprot_a2n(&prot, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200424 invarg_1_to_2(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000425 req.r.rtm_protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000426 ok |= proto_ok;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000427#if ENABLE_FEATURE_IP_RULE
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000428 } else if (arg == ARG_table) {
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000429 uint32_t tid;
430 NEXT_ARG();
431 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200432 invarg_1_to_2(*argv, "table");
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000433 if (tid < 256)
434 req.r.rtm_table = tid;
435 else {
436 req.r.rtm_table = RT_TABLE_UNSPEC;
437 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
438 }
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000439#endif
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000440 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000441 NEXT_ARG();
442 d = *argv;
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000443 } else if (arg == ARG_metric) {
444 uint32_t metric;
445 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000446 metric = get_u32(*argv, "metric");
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000447 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
Michael Tokarev1a114392014-07-28 10:05:41 +0400448 } else if (arg == ARG_onlink) {
449 req.r.rtm_flags |= RTNH_F_ONLINK;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000450 } else {
451 int type;
452 inet_prefix dst;
453
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000454 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000455 NEXT_ARG();
456 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000457 if ((**argv < '0' || **argv > '9')
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100458 && rtnl_rtntype_a2n(&type, *argv) == 0
459 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000460 NEXT_ARG();
461 req.r.rtm_type = type;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000462 ok |= type_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000463 }
464
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000465 if (ok & dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000466 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000467 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000468 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000469 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000470 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000471 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000472 req.r.rtm_dst_len = dst.bitlen;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000473 ok |= dst_ok;
Glenn L McGrath16528552002-11-28 11:17:19 +0000474 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000475 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000476 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000477 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000478 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000479 }
480
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000481 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000482
Eric Andersen5780adb2002-11-15 09:12:47 +0000483 if (d) {
484 int idx;
485
486 ll_init_map(&rth);
487
488 if (d) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000489 idx = xll_name_to_index(d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000490 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
491 }
492 }
493
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000494 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000495 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000496 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000497 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000498 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
499 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000500
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100501 if (!scope_ok) {
502 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
503 req.r.rtm_scope = RT_SCOPE_HOST;
504 else
505 if (req.r.rtm_type == RTN_BROADCAST
506 || req.r.rtm_type == RTN_MULTICAST
507 || req.r.rtm_type == RTN_ANYCAST
508 ) {
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000509 req.r.rtm_scope = RT_SCOPE_LINK;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100510 }
511 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
512 if (cmd == RTM_DELROUTE)
513 req.r.rtm_scope = RT_SCOPE_NOWHERE;
514 else if (!(ok & gw_ok))
515 req.r.rtm_scope = RT_SCOPE_LINK;
516 }
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000517 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000518
Glenn L McGrath16528552002-11-28 11:17:19 +0000519 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000520 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000521 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000522
Glenn L McGrath16528552002-11-28 11:17:19 +0000523 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000524 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000525 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000526
527 return 0;
528}
529
530static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
531{
532 struct {
533 struct nlmsghdr nlh;
534 struct rtmsg rtm;
535 } req;
536 struct sockaddr_nl nladdr;
537
538 memset(&nladdr, 0, sizeof(nladdr));
539 memset(&req, 0, sizeof(req));
540 nladdr.nl_family = AF_NETLINK;
541
542 req.nlh.nlmsg_len = sizeof(req);
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000543 if (RTM_GETROUTE)
544 req.nlh.nlmsg_type = RTM_GETROUTE;
545 if (NLM_F_ROOT | NLM_F_REQUEST)
546 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
547 /*req.nlh.nlmsg_pid = 0; - memset did it already */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000548 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
549 req.rtm.rtm_family = family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000550 if (RTM_F_CLONED)
551 req.rtm.rtm_flags = RTM_F_CLONED;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000552
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000553 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000554}
555
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000556static void iproute_flush_cache(void)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000557{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000558 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000559 int flush_fd = open_or_warn(fn, O_WRONLY);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000560
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000561 if (flush_fd < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000562 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000563 }
564
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000565 if (write(flush_fd, "-1", 2) < 2) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100566 bb_perror_msg("can't flush routing cache");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000567 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000568 }
569 close(flush_fd);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000570}
571
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000572static void iproute_reset_filter(void)
573{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100574 memset(&G_filter, 0, sizeof(G_filter));
575 G_filter.mdst.bitlen = -1;
576 G_filter.msrc.bitlen = -1;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000577}
578
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000579/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000580static int iproute_list_or_flush(char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000581{
582 int do_ipv6 = preferred_family;
583 struct rtnl_handle rth;
584 char *id = NULL;
585 char *od = NULL;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000586 static const char keywords[] ALIGN1 =
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000587 /* "ip route list/flush" parameters: */
588 "protocol\0" "dev\0" "oif\0" "iif\0"
589 "via\0" "table\0" "cache\0"
590 "from\0" "to\0"
591 /* and possible further keywords */
592 "all\0"
593 "root\0"
594 "match\0"
595 "exact\0"
596 "main\0"
597 ;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000598 enum {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000599 KW_proto, KW_dev, KW_oif, KW_iif,
600 KW_via, KW_table, KW_cache,
601 KW_from, KW_to,
602 /* */
603 KW_all,
604 KW_root,
605 KW_match,
606 KW_exact,
607 KW_main,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000608 };
609 int arg, parm;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000610
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000611 iproute_reset_filter();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100612 G_filter.tb = RT_TABLE_MAIN;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000613
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000614 if (flush && !*argv)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000615 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000616
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000617 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000618 arg = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000619 if (arg == KW_proto) {
Eric Andersend78aea82006-01-30 18:00:02 +0000620 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000621 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100622 //G_filter.protocolmask = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000623 if (rtnl_rtprot_a2n(&prot, *argv)) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000624 if (index_in_strings(keywords, *argv) != KW_all)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200625 invarg_1_to_2(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000626 prot = 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100627 //G_filter.protocolmask = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000628 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100629 //G_filter.protocol = prot;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000630 } else if (arg == KW_dev || arg == KW_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000631 NEXT_ARG();
632 od = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000633 } else if (arg == KW_iif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000634 NEXT_ARG();
635 id = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000636 } else if (arg == KW_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000637 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100638 get_prefix(&G_filter.rvia, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000639 } else if (arg == KW_table) { /* table all/cache/main */
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000640 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000641 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000642 if (parm == KW_cache)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100643 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000644 else if (parm == KW_all)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100645 G_filter.tb = 0;
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000646 else if (parm != KW_main) {
647#if ENABLE_FEATURE_IP_RULE
648 uint32_t tid;
649 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200650 invarg_1_to_2(*argv, "table");
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100651 G_filter.tb = tid;
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000652#else
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200653 invarg_1_to_2(*argv, "table");
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000654#endif
655 }
Denis Vlasenko79c69042007-11-27 09:42:33 +0000656 } else if (arg == KW_cache) {
657 /* The command 'ip route flush cache' is used by OpenSWAN.
658 * Assuming it's a synonym for 'ip route flush table cache' */
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100659 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000660 } else if (arg == KW_from) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000661 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000662 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000663 if (parm == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000664 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100665 get_prefix(&G_filter.rsrc, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000666 } else if (parm == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000667 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100668 get_prefix(&G_filter.msrc, *argv, do_ipv6);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000669 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000670 if (parm == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000671 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100672 get_prefix(&G_filter.msrc, *argv, do_ipv6);
673 G_filter.rsrc = G_filter.msrc;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000674 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000675 } else { /* "to" is the default parameter */
676 if (arg == KW_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000677 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000678 arg = index_in_substrings(keywords, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000679 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000680 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
681 if (arg == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000682 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100683 get_prefix(&G_filter.rdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000684 } else if (arg == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000685 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100686 get_prefix(&G_filter.mdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000687 } else { /* "to exact" is the default */
688 if (arg == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000689 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100690 get_prefix(&G_filter.mdst, *argv, do_ipv6);
691 G_filter.rdst = G_filter.mdst;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000692 }
693 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000694 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000695 }
696
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100697 if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000698 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000699 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000700
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000701 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000702 ll_init_map(&rth);
703
704 if (id || od) {
705 int idx;
706
707 if (id) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000708 idx = xll_name_to_index(id);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100709 G_filter.iif = idx;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000710 }
711 if (od) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000712 idx = xll_name_to_index(od);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100713 G_filter.oif = idx;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000714 }
715 }
716
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000717 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000718 char flushb[4096-512];
719
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100720 if (G_filter.tb == -1) { /* "flush table cache" */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000721 if (do_ipv6 != AF_INET6)
722 iproute_flush_cache();
723 if (do_ipv6 == AF_INET)
724 return 0;
725 }
726
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100727 G_filter.flushb = flushb;
728 G_filter.flushp = 0;
729 G_filter.flushe = sizeof(flushb);
730 G_filter.rth = &rth;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000731
732 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000733 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100734 G_filter.flushed = 0;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000735 xrtnl_dump_filter(&rth, print_route, NULL);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100736 if (G_filter.flushed == 0)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000737 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000738 if (flush_update())
739 return 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000740 }
741 }
742
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100743 if (G_filter.tb != -1) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000744 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000745 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100746 bb_perror_msg_and_die("can't send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000747 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000748 xrtnl_dump_filter(&rth, print_route, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000749
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000750 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000751}
752
753
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000754/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000755static int iproute_get(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000756{
757 struct rtnl_handle rth;
758 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000759 struct nlmsghdr n;
760 struct rtmsg r;
761 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000762 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000763 char *idev = NULL;
764 char *odev = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000765 bool connected = 0;
766 bool from_ok = 0;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000767 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000768 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000769
770 memset(&req, 0, sizeof(req));
771
772 iproute_reset_filter();
773
774 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000775 if (NLM_F_REQUEST)
776 req.n.nlmsg_flags = NLM_F_REQUEST;
777 if (RTM_GETROUTE)
778 req.n.nlmsg_type = RTM_GETROUTE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000779 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000780 /*req.r.rtm_table = 0; - memset did this already */
781 /*req.r.rtm_protocol = 0;*/
782 /*req.r.rtm_scope = 0;*/
783 /*req.r.rtm_type = 0;*/
784 /*req.r.rtm_src_len = 0;*/
785 /*req.r.rtm_dst_len = 0;*/
786 /*req.r.rtm_tos = 0;*/
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000787
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000788 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000789 switch (index_in_strings(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000790 case 0: /* from */
791 {
792 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000793 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000794 from_ok = 1;
795 get_prefix(&addr, *argv, req.r.rtm_family);
796 if (req.r.rtm_family == AF_UNSPEC) {
797 req.r.rtm_family = addr.family;
798 }
799 if (addr.bytelen) {
800 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
801 }
802 req.r.rtm_src_len = addr.bitlen;
803 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000804 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000805 case 1: /* iif */
806 NEXT_ARG();
807 idev = *argv;
808 break;
809 case 2: /* oif */
810 case 3: /* dev */
811 NEXT_ARG();
812 odev = *argv;
813 break;
814 case 4: /* notify */
815 req.r.rtm_flags |= RTM_F_NOTIFY;
816 break;
817 case 5: /* connected */
818 connected = 1;
819 break;
820 case 6: /* to */
821 NEXT_ARG();
822 default:
823 {
824 inet_prefix addr;
825 get_prefix(&addr, *argv, req.r.rtm_family);
826 if (req.r.rtm_family == AF_UNSPEC) {
827 req.r.rtm_family = addr.family;
828 }
829 if (addr.bytelen) {
830 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
831 }
832 req.r.rtm_dst_len = addr.bitlen;
833 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000834 }
Christian Hornung3bbfb582010-11-03 14:08:00 +0100835 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000836 }
837
838 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000839 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000840 }
841
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000842 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000843
844 ll_init_map(&rth);
845
846 if (idev || odev) {
847 int idx;
848
849 if (idev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000850 idx = xll_name_to_index(idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000851 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
852 }
853 if (odev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000854 idx = xll_name_to_index(odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000855 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
856 }
857 }
858
Glenn L McGrath16528552002-11-28 11:17:19 +0000859 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000860 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000861 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000862
Glenn L McGrath16528552002-11-28 11:17:19 +0000863 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000864 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000865 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000866
867 if (connected && !from_ok) {
868 struct rtmsg *r = NLMSG_DATA(&req.n);
869 int len = req.n.nlmsg_len;
870 struct rtattr * tb[RTA_MAX+1];
871
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000872 print_route(NULL, &req.n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000873
874 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000875 bb_error_msg_and_die("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000876 }
877 len -= NLMSG_LENGTH(sizeof(*r));
878 if (len < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000879 bb_error_msg_and_die("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000880 }
881
882 memset(tb, 0, sizeof(tb));
883 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
884
885 if (tb[RTA_PREFSRC]) {
886 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
887 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
888 } else if (!tb[RTA_SRC]) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100889 bb_error_msg_and_die("can't connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000890 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000891 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000892 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000893 }
894 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000895 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000896 }
897 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000898 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000899 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000900 req.n.nlmsg_flags = NLM_F_REQUEST;
901 req.n.nlmsg_type = RTM_GETROUTE;
902
Glenn L McGrath16528552002-11-28 11:17:19 +0000903 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000904 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000905 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000906 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000907 print_route(NULL, &req.n, NULL);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000908 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000909}
910
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000911/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200912int FAST_FUNC do_iproute(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000913{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000914 static const char ip_route_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000915 /*0-3*/ "add\0""append\0""change\0""chg\0"
916 /*4-7*/ "delete\0""get\0""list\0""show\0"
917 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
Denis Vlasenko83c44222008-01-04 15:28:28 +0000918 int command_num;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000919 unsigned flags = 0;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000920 int cmd = RTM_NEWROUTE;
921
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200922 INIT_G();
923
Denis Vlasenko83c44222008-01-04 15:28:28 +0000924 if (!*argv)
925 return iproute_list_or_flush(argv, 0);
926
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000927 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
928 /* It probably means that it is using "first match" rule */
Denis Vlasenko83c44222008-01-04 15:28:28 +0000929 command_num = index_in_substrings(ip_route_commands, *argv);
930
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000931 switch (command_num) {
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000932 case 0: /* add */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000933 flags = NLM_F_CREATE|NLM_F_EXCL;
934 break;
935 case 1: /* append */
936 flags = NLM_F_CREATE|NLM_F_APPEND;
937 break;
938 case 2: /* change */
939 case 3: /* chg */
940 flags = NLM_F_REPLACE;
941 break;
942 case 4: /* delete */
943 cmd = RTM_DELROUTE;
944 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000945 case 5: /* get */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000946 return iproute_get(argv+1);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000947 case 6: /* list */
948 case 7: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000949 return iproute_list_or_flush(argv+1, 0);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000950 case 8: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000951 flags = NLM_F_CREATE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000952 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000953 case 9: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000954 flags = NLM_F_CREATE|NLM_F_REPLACE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000955 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000956 case 10: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000957 flags = NLM_F_EXCL;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000958 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000959 case 11: /* flush */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000960 return iproute_list_or_flush(argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000961 default:
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200962 invarg_1_to_2(*argv, applet_name);
Glenn L McGrath16528552002-11-28 11:17:19 +0000963 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000964
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000965 return iproute_modify(cmd, flags, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000966}