blob: 3f9007ee953dc7e7307894ac764f5509eb2bac19 [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 <netinet/in.h>
20#include <netinet/ip.h>
21#include <arpa/inet.h>
22
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000023#include "ip_common.h" /* #include "libbb.h" is inside */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000024#include "rt_names.h"
25#include "utils.h"
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000026
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000027/*
28static void usage(void) __attribute__((noreturn));
29
30static void usage(void)
31{
32 fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
33 fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
34 fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
35 fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
36 fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
37 fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
38 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
39 exit(-1);
40}
41*/
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000042
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000043static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
44 struct nlmsghdr *n, void *arg)
45{
46 FILE *fp = (FILE*)arg;
47 struct rtmsg *r = NLMSG_DATA(n);
48 int len = n->nlmsg_len;
49 int host_len = -1;
50 struct rtattr * tb[RTA_MAX+1];
51 char abuf[256];
52 SPRINT_BUF(b1);
53
54 if (n->nlmsg_type != RTM_NEWRULE)
55 return 0;
56
57 len -= NLMSG_LENGTH(sizeof(*r));
58 if (len < 0)
59 return -1;
60
61 memset(tb, 0, sizeof(tb));
62 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
63
64 if (r->rtm_family == AF_INET)
65 host_len = 32;
66 else if (r->rtm_family == AF_INET6)
67 host_len = 128;
68/* else if (r->rtm_family == AF_DECnet)
69 host_len = 16;
70 else if (r->rtm_family == AF_IPX)
71 host_len = 80;
72*/
73 if (tb[RTA_PRIORITY])
74 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
75 else
76 fprintf(fp, "0:\t");
77
78 fprintf(fp, "from ");
79 if (tb[RTA_SRC]) {
80 if (r->rtm_src_len != host_len) {
81 fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
82 RTA_PAYLOAD(tb[RTA_SRC]),
83 RTA_DATA(tb[RTA_SRC]),
84 abuf, sizeof(abuf)),
85 r->rtm_src_len
86 );
87 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000088 fputs(format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000089 RTA_PAYLOAD(tb[RTA_SRC]),
90 RTA_DATA(tb[RTA_SRC]),
Denis Vlasenko605b20e2007-09-30 16:22:36 +000091 abuf, sizeof(abuf)), fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000092 }
93 } else if (r->rtm_src_len) {
94 fprintf(fp, "0/%d", r->rtm_src_len);
95 } else {
96 fprintf(fp, "all");
97 }
98 fprintf(fp, " ");
99
100 if (tb[RTA_DST]) {
101 if (r->rtm_dst_len != host_len) {
102 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
103 RTA_PAYLOAD(tb[RTA_DST]),
104 RTA_DATA(tb[RTA_DST]),
105 abuf, sizeof(abuf)),
106 r->rtm_dst_len
107 );
108 } else {
109 fprintf(fp, "to %s ", format_host(r->rtm_family,
110 RTA_PAYLOAD(tb[RTA_DST]),
111 RTA_DATA(tb[RTA_DST]),
112 abuf, sizeof(abuf)));
113 }
114 } else if (r->rtm_dst_len) {
115 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
116 }
117
118 if (r->rtm_tos) {
119 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
120 }
121 if (tb[RTA_PROTOINFO]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000122 fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000123 }
124
125 if (tb[RTA_IIF]) {
126 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
127 }
128
129 if (r->rtm_table)
130 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
131
132 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000133 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
134 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000135 to &= 0xFFFF;
136 if (from) {
137 fprintf(fp, "realms %s/",
138 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
139 }
140 fprintf(fp, "%s ",
141 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
142 }
143
144 if (r->rtm_type == RTN_NAT) {
145 if (tb[RTA_GATEWAY]) {
146 fprintf(fp, "map-to %s ",
147 format_host(r->rtm_family,
148 RTA_PAYLOAD(tb[RTA_GATEWAY]),
149 RTA_DATA(tb[RTA_GATEWAY]),
150 abuf, sizeof(abuf)));
151 } else
152 fprintf(fp, "masquerade");
153 } else if (r->rtm_type != RTN_UNICAST)
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000154 fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000155
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000156 fputc('\n', fp);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000157 fflush(fp);
158 return 0;
159}
160
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000161/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000162static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000163{
164 struct rtnl_handle rth;
165 int af = preferred_family;
166
167 if (af == AF_UNSPEC)
168 af = AF_INET;
169
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000170 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000171 //bb_error_msg("\"rule show\" needs no arguments");
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000172 bb_warn_ignoring_args(1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000173 return -1;
174 }
175
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000176 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000177
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000178 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
179 xrtnl_dump_filter(&rth, print_rule, stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000180
181 return 0;
182}
183
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000184/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000185static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000186{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000187 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000188 "from\0""to\0""preference\0""order\0""priority\0"
189 "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
190 "iif\0""nat\0""map-to\0""type\0""help\0";
191 enum {
192 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
193 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
194 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
195 };
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000196 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000197 struct rtnl_handle rth;
198 struct {
199 struct nlmsghdr n;
200 struct rtmsg r;
201 char buf[1024];
202 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000203 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000204
205 memset(&req, 0, sizeof(req));
206
207 req.n.nlmsg_type = cmd;
208 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
209 req.n.nlmsg_flags = NLM_F_REQUEST;
210 req.r.rtm_family = preferred_family;
211 req.r.rtm_protocol = RTPROT_BOOT;
212 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
213 req.r.rtm_table = 0;
214 req.r.rtm_type = RTN_UNSPEC;
215
216 if (cmd == RTM_NEWRULE) {
217 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
218 req.r.rtm_type = RTN_UNICAST;
219 }
220
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000221 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000222 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000223 if (key == 0) /* no match found in keywords array, bail out. */
224 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
225 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000226 inet_prefix dst;
227 NEXT_ARG();
228 get_prefix(&dst, *argv, req.r.rtm_family);
229 req.r.rtm_src_len = dst.bitlen;
230 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000231 } else if (key == ARG_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000232 inet_prefix dst;
233 NEXT_ARG();
234 get_prefix(&dst, *argv, req.r.rtm_family);
235 req.r.rtm_dst_len = dst.bitlen;
236 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000237 } else if (key == ARG_preference ||
238 key == ARG_order ||
239 key == ARG_priority) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000240 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000241 NEXT_ARG();
242 if (get_u32(&pref, *argv, 0))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000243 invarg(*argv, "preference");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000244 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000245 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000246 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000247 NEXT_ARG();
248 if (rtnl_dsfield_a2n(&tos, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000249 invarg(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000250 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000251 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000252 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000253 NEXT_ARG();
254 if (get_u32(&fwmark, *argv, 0))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000255 invarg(*argv, "fwmark");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000256 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000257 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000258 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000259 NEXT_ARG();
260 if (get_rt_realms(&realm, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000261 invarg(*argv, "realms");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000262 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000263 } else if (key == ARG_table ||
264 key == ARG_lookup) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000265 uint32_t tid;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000266 NEXT_ARG();
267 if (rtnl_rttable_a2n(&tid, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000268 invarg(*argv, "table ID");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000269 req.r.rtm_table = tid;
270 table_ok = 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000271 } else if (key == ARG_dev ||
272 key == ARG_iif) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000273 NEXT_ARG();
274 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000275 } else if (key == ARG_nat ||
276 key == ARG_map_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000277 NEXT_ARG();
278 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
279 req.r.rtm_type = RTN_NAT;
280 } else {
281 int type;
282
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000283 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000284 NEXT_ARG();
285 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000286 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000287 bb_show_usage();
288 if (rtnl_rtntype_a2n(&type, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000289 invarg(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000290 req.r.rtm_type = type;
291 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000292 argv++;
293 }
294
295 if (req.r.rtm_family == AF_UNSPEC)
296 req.r.rtm_family = AF_INET;
297
298 if (!table_ok && cmd == RTM_NEWRULE)
299 req.r.rtm_table = RT_TABLE_MAIN;
300
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000301 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000302
303 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
304 return 2;
305
306 return 0;
307}
308
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000309/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000310int do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000311{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000312 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000313 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000314 int cmd = 2; /* list */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000315
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000316 if (!*argv)
317 return iprule_list(argv);
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000318
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000319 cmd = index_in_substrings(ip_rule_commands, *argv);
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000320 switch (cmd) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000321 case 0: /* add */
322 cmd = RTM_NEWRULE;
323 break;
324 case 1: /* delete */
325 cmd = RTM_DELRULE;
326 break;
327 case 2: /* list */
328 case 3: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000329 return iprule_list(argv+1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000330 break;
331 default:
332 bb_error_msg_and_die("unknown command %s", *argv);
333 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000334 return iprule_modify(cmd, argv+1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000335}