blob: a62eae1fe6ea9a87c2787fd44ad163087836e156 [file] [log] [blame]
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +00001/* vi: set sw=4 ts=4: */
2/*
3 * iprule.c "ip rule".
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 *
12 *
13 * Changes:
14 *
15 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +000016 * initially integrated into busybox by Bernhard Fischer
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000017 */
18
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000019#include <syslog.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000020//#include <sys/socket.h>
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000021#include <netinet/in.h>
22#include <netinet/ip.h>
23#include <arpa/inet.h>
24
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000025#include "ip_common.h" /* #include "libbb.h" is inside */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000026#include "rt_names.h"
27#include "utils.h"
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000028
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000029/*
30static void usage(void) __attribute__((noreturn));
31
32static void usage(void)
33{
34 fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
35 fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
36 fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
37 fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
38 fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
39 fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
40 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
41 exit(-1);
42}
43*/
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000044
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000045static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
46 struct nlmsghdr *n, void *arg)
47{
48 FILE *fp = (FILE*)arg;
49 struct rtmsg *r = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
51 int host_len = -1;
52 struct rtattr * tb[RTA_MAX+1];
53 char abuf[256];
54 SPRINT_BUF(b1);
55
56 if (n->nlmsg_type != RTM_NEWRULE)
57 return 0;
58
59 len -= NLMSG_LENGTH(sizeof(*r));
60 if (len < 0)
61 return -1;
62
63 memset(tb, 0, sizeof(tb));
64 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
65
66 if (r->rtm_family == AF_INET)
67 host_len = 32;
68 else if (r->rtm_family == AF_INET6)
69 host_len = 128;
70/* else if (r->rtm_family == AF_DECnet)
71 host_len = 16;
72 else if (r->rtm_family == AF_IPX)
73 host_len = 80;
74*/
75 if (tb[RTA_PRIORITY])
76 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
77 else
78 fprintf(fp, "0:\t");
79
80 fprintf(fp, "from ");
81 if (tb[RTA_SRC]) {
82 if (r->rtm_src_len != host_len) {
83 fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
84 RTA_PAYLOAD(tb[RTA_SRC]),
85 RTA_DATA(tb[RTA_SRC]),
86 abuf, sizeof(abuf)),
87 r->rtm_src_len
88 );
89 } else {
90 fprintf(fp, "%s", format_host(r->rtm_family,
91 RTA_PAYLOAD(tb[RTA_SRC]),
92 RTA_DATA(tb[RTA_SRC]),
93 abuf, sizeof(abuf))
94 );
95 }
96 } else if (r->rtm_src_len) {
97 fprintf(fp, "0/%d", r->rtm_src_len);
98 } else {
99 fprintf(fp, "all");
100 }
101 fprintf(fp, " ");
102
103 if (tb[RTA_DST]) {
104 if (r->rtm_dst_len != host_len) {
105 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
106 RTA_PAYLOAD(tb[RTA_DST]),
107 RTA_DATA(tb[RTA_DST]),
108 abuf, sizeof(abuf)),
109 r->rtm_dst_len
110 );
111 } else {
112 fprintf(fp, "to %s ", format_host(r->rtm_family,
113 RTA_PAYLOAD(tb[RTA_DST]),
114 RTA_DATA(tb[RTA_DST]),
115 abuf, sizeof(abuf)));
116 }
117 } else if (r->rtm_dst_len) {
118 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
119 }
120
121 if (r->rtm_tos) {
122 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
123 }
124 if (tb[RTA_PROTOINFO]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000125 fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000126 }
127
128 if (tb[RTA_IIF]) {
129 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
130 }
131
132 if (r->rtm_table)
133 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
134
135 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000136 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
137 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000138 to &= 0xFFFF;
139 if (from) {
140 fprintf(fp, "realms %s/",
141 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
142 }
143 fprintf(fp, "%s ",
144 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
145 }
146
147 if (r->rtm_type == RTN_NAT) {
148 if (tb[RTA_GATEWAY]) {
149 fprintf(fp, "map-to %s ",
150 format_host(r->rtm_family,
151 RTA_PAYLOAD(tb[RTA_GATEWAY]),
152 RTA_DATA(tb[RTA_GATEWAY]),
153 abuf, sizeof(abuf)));
154 } else
155 fprintf(fp, "masquerade");
156 } else if (r->rtm_type != RTN_UNICAST)
157 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
158
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000159 fputc('\n', fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000160 fflush(fp);
161 return 0;
162}
163
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000164/* Return value becomes exitcode. It's okay to not return at all */
Bernhard Reutner-Fischer620e57b2007-01-22 17:42:37 +0000165static int iprule_list(int argc, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000166{
167 struct rtnl_handle rth;
168 int af = preferred_family;
169
170 if (af == AF_UNSPEC)
171 af = AF_INET;
172
173 if (argc > 0) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000174 //bb_error_msg("\"rule show\" needs no arguments");
175 bb_warn_ignoring_args(argc);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000176 return -1;
177 }
178
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000179 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000180
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000181 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
182 xrtnl_dump_filter(&rth, print_rule, stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000183
184 return 0;
185}
186
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000187/* Return value becomes exitcode. It's okay to not return at all */
Bernhard Reutner-Fischer620e57b2007-01-22 17:42:37 +0000188static int iprule_modify(int cmd, int argc, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000189{
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000190 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000191 struct rtnl_handle rth;
192 struct {
193 struct nlmsghdr n;
194 struct rtmsg r;
195 char buf[1024];
196 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000197 static const char * const keywords[] =
198 { "from", "to", "preference", "order", "priority", "tos", "fwmark",
199 "realms", "table", "lookup", "dev", "iif", "nat", "map-to", "type",
200 "help", NULL};
201 enum { ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
202 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
203 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help };
204 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000205
206 memset(&req, 0, sizeof(req));
207
208 req.n.nlmsg_type = cmd;
209 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
210 req.n.nlmsg_flags = NLM_F_REQUEST;
211 req.r.rtm_family = preferred_family;
212 req.r.rtm_protocol = RTPROT_BOOT;
213 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
214 req.r.rtm_table = 0;
215 req.r.rtm_type = RTN_UNSPEC;
216
217 if (cmd == RTM_NEWRULE) {
218 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
219 req.r.rtm_type = RTN_UNICAST;
220 }
221
222 while (argc > 0) {
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000223 key = index_in_substr_array(keywords, *argv) + 1;
224 if (key == 0) /* no match found in keywords array, bail out. */
225 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
226 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000227 inet_prefix dst;
228 NEXT_ARG();
229 get_prefix(&dst, *argv, req.r.rtm_family);
230 req.r.rtm_src_len = dst.bitlen;
231 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000232 } else if (key == ARG_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000233 inet_prefix dst;
234 NEXT_ARG();
235 get_prefix(&dst, *argv, req.r.rtm_family);
236 req.r.rtm_dst_len = dst.bitlen;
237 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000238 } else if (key == ARG_preference ||
239 key == ARG_order ||
240 key == ARG_priority) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000241 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000242 NEXT_ARG();
243 if (get_u32(&pref, *argv, 0))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000244 invarg(*argv, "preference");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000245 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000246 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000247 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000248 NEXT_ARG();
249 if (rtnl_dsfield_a2n(&tos, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000250 invarg(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000251 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000252 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000253 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000254 NEXT_ARG();
255 if (get_u32(&fwmark, *argv, 0))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000256 invarg(*argv, "fwmark");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000257 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000258 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000259 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000260 NEXT_ARG();
261 if (get_rt_realms(&realm, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000262 invarg(*argv, "realms");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000263 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000264 } else if (key == ARG_table ||
265 key == ARG_lookup) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000266 uint32_t tid;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000267 NEXT_ARG();
268 if (rtnl_rttable_a2n(&tid, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000269 invarg(*argv, "table ID");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000270 req.r.rtm_table = tid;
271 table_ok = 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000272 } else if (key == ARG_dev ||
273 key == ARG_iif) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000274 NEXT_ARG();
275 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000276 } else if (key == ARG_nat ||
277 key == ARG_map_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000278 NEXT_ARG();
279 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
280 req.r.rtm_type = RTN_NAT;
281 } else {
282 int type;
283
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000284 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000285 NEXT_ARG();
286 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000287 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000288 bb_show_usage();
289 if (rtnl_rtntype_a2n(&type, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000290 invarg(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000291 req.r.rtm_type = type;
292 }
293 argc--;
294 argv++;
295 }
296
297 if (req.r.rtm_family == AF_UNSPEC)
298 req.r.rtm_family = AF_INET;
299
300 if (!table_ok && cmd == RTM_NEWRULE)
301 req.r.rtm_table = RT_TABLE_MAIN;
302
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000303 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000304
305 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
306 return 2;
307
308 return 0;
309}
310
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000311/* Return value becomes exitcode. It's okay to not return at all */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000312int do_iprule(int argc, char **argv)
313{
Bernhard Reutner-Fischer2806b232007-01-28 11:14:26 +0000314 static const char * const ip_rule_commands[] =
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000315 {"add", "delete", "list", "show", 0};
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000316 int cmd = 2; /* list */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000317
318 if (argc < 1)
319 return iprule_list(0, NULL);
320 if (*argv)
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000321 cmd = index_in_substr_array(ip_rule_commands, *argv);
322
323 switch (cmd) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000324 case 0: /* add */
325 cmd = RTM_NEWRULE;
326 break;
327 case 1: /* delete */
328 cmd = RTM_DELRULE;
329 break;
330 case 2: /* list */
331 case 3: /* show */
332 return iprule_list(argc-1, argv+1);
333 break;
334 default:
335 bb_error_msg_and_die("unknown command %s", *argv);
336 }
337 return iprule_modify(cmd, argc-1, argv+1);
338}