blob: 6b22061a7ae0e84463f9fc56020322e6ab232a98 [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>
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000020#include <netinet/in.h>
21#include <netinet/ip.h>
22#include <arpa/inet.h>
23
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000024#include "ip_common.h" /* #include "libbb.h" is inside */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000025#include "rt_names.h"
26#include "utils.h"
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000027
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000028/*
29static void usage(void) __attribute__((noreturn));
30
31static void usage(void)
32{
33 fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
34 fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
35 fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
36 fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
37 fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
38 fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
39 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
40 exit(-1);
41}
42*/
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000043
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000044static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
45 struct nlmsghdr *n, void *arg)
46{
47 FILE *fp = (FILE*)arg;
48 struct rtmsg *r = NLMSG_DATA(n);
49 int len = n->nlmsg_len;
50 int host_len = -1;
51 struct rtattr * tb[RTA_MAX+1];
52 char abuf[256];
53 SPRINT_BUF(b1);
54
55 if (n->nlmsg_type != RTM_NEWRULE)
56 return 0;
57
58 len -= NLMSG_LENGTH(sizeof(*r));
59 if (len < 0)
60 return -1;
61
62 memset(tb, 0, sizeof(tb));
63 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
64
65 if (r->rtm_family == AF_INET)
66 host_len = 32;
67 else if (r->rtm_family == AF_INET6)
68 host_len = 128;
69/* else if (r->rtm_family == AF_DECnet)
70 host_len = 16;
71 else if (r->rtm_family == AF_IPX)
72 host_len = 80;
73*/
74 if (tb[RTA_PRIORITY])
75 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
76 else
77 fprintf(fp, "0:\t");
78
79 fprintf(fp, "from ");
80 if (tb[RTA_SRC]) {
81 if (r->rtm_src_len != host_len) {
82 fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
83 RTA_PAYLOAD(tb[RTA_SRC]),
84 RTA_DATA(tb[RTA_SRC]),
85 abuf, sizeof(abuf)),
86 r->rtm_src_len
87 );
88 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000089 fputs(format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000090 RTA_PAYLOAD(tb[RTA_SRC]),
91 RTA_DATA(tb[RTA_SRC]),
Denis Vlasenko605b20e2007-09-30 16:22:36 +000092 abuf, sizeof(abuf)), fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000093 }
94 } else if (r->rtm_src_len) {
95 fprintf(fp, "0/%d", r->rtm_src_len);
96 } else {
97 fprintf(fp, "all");
98 }
99 fprintf(fp, " ");
100
101 if (tb[RTA_DST]) {
102 if (r->rtm_dst_len != host_len) {
103 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
104 RTA_PAYLOAD(tb[RTA_DST]),
105 RTA_DATA(tb[RTA_DST]),
106 abuf, sizeof(abuf)),
107 r->rtm_dst_len
108 );
109 } else {
110 fprintf(fp, "to %s ", format_host(r->rtm_family,
111 RTA_PAYLOAD(tb[RTA_DST]),
112 RTA_DATA(tb[RTA_DST]),
113 abuf, sizeof(abuf)));
114 }
115 } else if (r->rtm_dst_len) {
116 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
117 }
118
119 if (r->rtm_tos) {
120 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
121 }
122 if (tb[RTA_PROTOINFO]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000123 fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000124 }
125
126 if (tb[RTA_IIF]) {
127 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
128 }
129
130 if (r->rtm_table)
131 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
132
133 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000134 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
135 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000136 to &= 0xFFFF;
137 if (from) {
138 fprintf(fp, "realms %s/",
139 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
140 }
141 fprintf(fp, "%s ",
142 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
143 }
144
145 if (r->rtm_type == RTN_NAT) {
146 if (tb[RTA_GATEWAY]) {
147 fprintf(fp, "map-to %s ",
148 format_host(r->rtm_family,
149 RTA_PAYLOAD(tb[RTA_GATEWAY]),
150 RTA_DATA(tb[RTA_GATEWAY]),
151 abuf, sizeof(abuf)));
152 } else
153 fprintf(fp, "masquerade");
154 } else if (r->rtm_type != RTN_UNICAST)
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000155 fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000156
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000157 fputc('\n', fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000158 fflush(fp);
159 return 0;
160}
161
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000162/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000163static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000164{
165 struct rtnl_handle rth;
166 int af = preferred_family;
167
168 if (af == AF_UNSPEC)
169 af = AF_INET;
170
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000171 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000172 //bb_error_msg("\"rule show\" needs no arguments");
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000173 bb_warn_ignoring_args(1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000174 return -1;
175 }
176
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000177 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000178
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000179 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
180 xrtnl_dump_filter(&rth, print_rule, stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000181
182 return 0;
183}
184
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000185/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000186static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000187{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000188 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000189 "from\0""to\0""preference\0""order\0""priority\0"
190 "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
191 "iif\0""nat\0""map-to\0""type\0""help\0";
192 enum {
193 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
194 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
195 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
196 };
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000197 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000198 struct rtnl_handle rth;
199 struct {
200 struct nlmsghdr n;
201 struct rtmsg r;
202 char buf[1024];
203 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000204 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
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000222 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000223 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000224 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 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000293 argv++;
294 }
295
296 if (req.r.rtm_family == AF_UNSPEC)
297 req.r.rtm_family = AF_INET;
298
299 if (!table_ok && cmd == RTM_NEWRULE)
300 req.r.rtm_table = RT_TABLE_MAIN;
301
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000302 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000303
304 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
305 return 2;
306
307 return 0;
308}
309
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000310/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000311int do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000312{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000313 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000314 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000315 int cmd = 2; /* list */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000316
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000317 if (!*argv)
318 return iprule_list(argv);
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000319
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000320 cmd = index_in_substrings(ip_rule_commands, *argv);
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000321 switch (cmd) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000322 case 0: /* add */
323 cmd = RTM_NEWRULE;
324 break;
325 case 1: /* delete */
326 cmd = RTM_DELRULE;
327 break;
328 case 2: /* list */
329 case 3: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000330 return iprule_list(argv+1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000331 break;
332 default:
333 bb_error_msg_and_die("unknown command %s", *argv);
334 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000335 return iprule_modify(cmd, argv+1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000336}