blob: bec530dcba979ecd55a5ca47bae2bd6bef0428bd [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-Fischer6c4dade2008-09-25 12:13:34 +000016 * initially integrated into busybox by Bernhard Reutner-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
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020043static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000045{
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000046 struct rtmsg *r = NLMSG_DATA(n);
47 int len = n->nlmsg_len;
48 int host_len = -1;
49 struct rtattr * tb[RTA_MAX+1];
50 char abuf[256];
51 SPRINT_BUF(b1);
52
53 if (n->nlmsg_type != RTM_NEWRULE)
54 return 0;
55
56 len -= NLMSG_LENGTH(sizeof(*r));
57 if (len < 0)
58 return -1;
59
60 memset(tb, 0, sizeof(tb));
61 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
62
63 if (r->rtm_family == AF_INET)
64 host_len = 32;
65 else if (r->rtm_family == AF_INET6)
66 host_len = 128;
67/* else if (r->rtm_family == AF_DECnet)
68 host_len = 16;
69 else if (r->rtm_family == AF_IPX)
70 host_len = 80;
71*/
72 if (tb[RTA_PRIORITY])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000073 printf("%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000074 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000075 printf("0:\t");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000076
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000077 printf("from ");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000078 if (tb[RTA_SRC]) {
79 if (r->rtm_src_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000080 printf("%s/%u", rt_addr_n2a(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000081 RTA_DATA(tb[RTA_SRC]),
82 abuf, sizeof(abuf)),
83 r->rtm_src_len
84 );
85 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000086 fputs(format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000087 RTA_PAYLOAD(tb[RTA_SRC]),
88 RTA_DATA(tb[RTA_SRC]),
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000089 abuf, sizeof(abuf)), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000090 }
91 } else if (r->rtm_src_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000092 printf("0/%d", r->rtm_src_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000093 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000094 printf("all");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000095 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000096 bb_putchar(' ');
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000097
98 if (tb[RTA_DST]) {
99 if (r->rtm_dst_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000100 printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000101 RTA_DATA(tb[RTA_DST]),
102 abuf, sizeof(abuf)),
103 r->rtm_dst_len
104 );
105 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000106 printf("to %s ", format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000107 RTA_PAYLOAD(tb[RTA_DST]),
108 RTA_DATA(tb[RTA_DST]),
109 abuf, sizeof(abuf)));
110 }
111 } else if (r->rtm_dst_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000112 printf("to 0/%d ", r->rtm_dst_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000113 }
114
115 if (r->rtm_tos) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000116 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000117 }
118 if (tb[RTA_PROTOINFO]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000119 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000120 }
121
122 if (tb[RTA_IIF]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000123 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000124 }
125
126 if (r->rtm_table)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000127 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000128
129 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000130 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
131 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000132 to &= 0xFFFF;
133 if (from) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000134 printf("realms %s/",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000135 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
136 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000137 printf("%s ",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000138 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
139 }
140
141 if (r->rtm_type == RTN_NAT) {
142 if (tb[RTA_GATEWAY]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000143 printf("map-to %s ",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000144 format_host(r->rtm_family,
145 RTA_PAYLOAD(tb[RTA_GATEWAY]),
146 RTA_DATA(tb[RTA_GATEWAY]),
147 abuf, sizeof(abuf)));
148 } else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000149 printf("masquerade");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000150 } else if (r->rtm_type != RTN_UNICAST)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000151 fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000152
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000153 bb_putchar('\n');
154 /*fflush(stdout);*/
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000155 return 0;
156}
157
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000158/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000159static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000160{
161 struct rtnl_handle rth;
162 int af = preferred_family;
163
164 if (af == AF_UNSPEC)
165 af = AF_INET;
166
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000167 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000168 //bb_error_msg("\"rule show\" needs no arguments");
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000169 bb_warn_ignoring_args(1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000170 return -1;
171 }
172
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000173 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000174
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000175 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000176 xrtnl_dump_filter(&rth, print_rule, NULL);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000177
178 return 0;
179}
180
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000181/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000182static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000183{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000184 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000185 "from\0""to\0""preference\0""order\0""priority\0"
186 "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
187 "iif\0""nat\0""map-to\0""type\0""help\0";
188 enum {
189 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
190 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
191 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
192 };
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000193 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000194 struct rtnl_handle rth;
195 struct {
196 struct nlmsghdr n;
197 struct rtmsg r;
198 char buf[1024];
199 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000200 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000201
202 memset(&req, 0, sizeof(req));
203
204 req.n.nlmsg_type = cmd;
205 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
206 req.n.nlmsg_flags = NLM_F_REQUEST;
207 req.r.rtm_family = preferred_family;
208 req.r.rtm_protocol = RTPROT_BOOT;
209 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
210 req.r.rtm_table = 0;
211 req.r.rtm_type = RTN_UNSPEC;
212
213 if (cmd == RTM_NEWRULE) {
214 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
215 req.r.rtm_type = RTN_UNICAST;
216 }
217
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000218 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000219 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000220 if (key == 0) /* no match found in keywords array, bail out. */
221 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
222 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000223 inet_prefix dst;
224 NEXT_ARG();
225 get_prefix(&dst, *argv, req.r.rtm_family);
226 req.r.rtm_src_len = dst.bitlen;
227 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000228 } else if (key == ARG_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000229 inet_prefix dst;
230 NEXT_ARG();
231 get_prefix(&dst, *argv, req.r.rtm_family);
232 req.r.rtm_dst_len = dst.bitlen;
233 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000234 } else if (key == ARG_preference ||
235 key == ARG_order ||
236 key == ARG_priority) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000237 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000238 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000239 pref = get_u32(*argv, "preference");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000240 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000241 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000242 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000243 NEXT_ARG();
244 if (rtnl_dsfield_a2n(&tos, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000245 invarg(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000246 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000247 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000248 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000249 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000250 fwmark = get_u32(*argv, "fwmark");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000251 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000252 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000253 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000254 NEXT_ARG();
255 if (get_rt_realms(&realm, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000256 invarg(*argv, "realms");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000257 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000258 } else if (key == ARG_table ||
259 key == ARG_lookup) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000260 uint32_t tid;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000261 NEXT_ARG();
262 if (rtnl_rttable_a2n(&tid, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000263 invarg(*argv, "table ID");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000264 req.r.rtm_table = tid;
265 table_ok = 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000266 } else if (key == ARG_dev ||
267 key == ARG_iif) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000268 NEXT_ARG();
269 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000270 } else if (key == ARG_nat ||
271 key == ARG_map_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000272 NEXT_ARG();
273 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
274 req.r.rtm_type = RTN_NAT;
275 } else {
276 int type;
277
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000278 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000279 NEXT_ARG();
280 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000281 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000282 bb_show_usage();
283 if (rtnl_rtntype_a2n(&type, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000284 invarg(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000285 req.r.rtm_type = type;
286 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000287 argv++;
288 }
289
290 if (req.r.rtm_family == AF_UNSPEC)
291 req.r.rtm_family = AF_INET;
292
293 if (!table_ok && cmd == RTM_NEWRULE)
294 req.r.rtm_table = RT_TABLE_MAIN;
295
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000296 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000297
298 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
299 return 2;
300
301 return 0;
302}
303
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000304/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000305int do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000306{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000307 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000308 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000309 int cmd = 2; /* list */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000310
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000311 if (!*argv)
312 return iprule_list(argv);
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000313
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000314 cmd = index_in_substrings(ip_rule_commands, *argv);
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000315 switch (cmd) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000316 case 0: /* add */
317 cmd = RTM_NEWRULE;
318 break;
319 case 1: /* delete */
320 cmd = RTM_DELRULE;
321 break;
322 case 2: /* list */
323 case 3: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000324 return iprule_list(argv+1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000325 break;
326 default:
327 bb_error_msg_and_die("unknown command %s", *argv);
328 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000329 return iprule_modify(cmd, argv+1);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000330}