blob: 835529ec486c0e38a62b471afe40d1b990c3f1db [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*/
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +020072 printf("%u:\t", tb[RTA_PRIORITY] ?
73 *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
74 : 0);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000075 printf("from ");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000076 if (tb[RTA_SRC]) {
77 if (r->rtm_src_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000078 printf("%s/%u", rt_addr_n2a(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000079 RTA_DATA(tb[RTA_SRC]),
80 abuf, sizeof(abuf)),
81 r->rtm_src_len
82 );
83 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000084 fputs(format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000085 RTA_PAYLOAD(tb[RTA_SRC]),
86 RTA_DATA(tb[RTA_SRC]),
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000087 abuf, sizeof(abuf)), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000088 }
89 } else if (r->rtm_src_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000090 printf("0/%d", r->rtm_src_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000091 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000092 printf("all");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000093 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000094 bb_putchar(' ');
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000095
96 if (tb[RTA_DST]) {
97 if (r->rtm_dst_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000098 printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000099 RTA_DATA(tb[RTA_DST]),
100 abuf, sizeof(abuf)),
101 r->rtm_dst_len
102 );
103 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000104 printf("to %s ", format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000105 RTA_PAYLOAD(tb[RTA_DST]),
106 RTA_DATA(tb[RTA_DST]),
107 abuf, sizeof(abuf)));
108 }
109 } else if (r->rtm_dst_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000110 printf("to 0/%d ", r->rtm_dst_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000111 }
112
113 if (r->rtm_tos) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200114 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000115 }
116 if (tb[RTA_PROTOINFO]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000117 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000118 }
119
120 if (tb[RTA_IIF]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000121 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000122 }
123
124 if (r->rtm_table)
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200125 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000126
127 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000128 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
129 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000130 to &= 0xFFFF;
131 if (from) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000132 printf("realms %s/",
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200133 rtnl_rtrealm_n2a(from, b1));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000134 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000135 printf("%s ",
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200136 rtnl_rtrealm_n2a(to, b1));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000137 }
138
139 if (r->rtm_type == RTN_NAT) {
140 if (tb[RTA_GATEWAY]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000141 printf("map-to %s ",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000142 format_host(r->rtm_family,
143 RTA_PAYLOAD(tb[RTA_GATEWAY]),
144 RTA_DATA(tb[RTA_GATEWAY]),
145 abuf, sizeof(abuf)));
146 } else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000147 printf("masquerade");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000148 } else if (r->rtm_type != RTN_UNICAST)
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200149 fputs(rtnl_rtntype_n2a(r->rtm_type, b1), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000150
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000151 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100152 /*fflush_all();*/
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000153 return 0;
154}
155
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000156/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000157static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000158{
159 struct rtnl_handle rth;
160 int af = preferred_family;
161
162 if (af == AF_UNSPEC)
163 af = AF_INET;
164
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000165 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000166 //bb_error_msg("\"rule show\" needs no arguments");
Denys Vlasenkoa355da02010-01-04 13:16:08 +0100167 bb_warn_ignoring_args(*argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000168 return -1;
169 }
170
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000171 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000172
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000173 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000174 xrtnl_dump_filter(&rth, print_rule, NULL);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000175
176 return 0;
177}
178
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000179/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000180static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000181{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000182 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000183 "from\0""to\0""preference\0""order\0""priority\0"
184 "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
185 "iif\0""nat\0""map-to\0""type\0""help\0";
186 enum {
187 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
188 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
189 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
190 };
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000191 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000192 struct rtnl_handle rth;
193 struct {
194 struct nlmsghdr n;
195 struct rtmsg r;
196 char buf[1024];
197 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000198 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000199
200 memset(&req, 0, sizeof(req));
201
202 req.n.nlmsg_type = cmd;
203 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
204 req.n.nlmsg_flags = NLM_F_REQUEST;
205 req.r.rtm_family = preferred_family;
206 req.r.rtm_protocol = RTPROT_BOOT;
207 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
208 req.r.rtm_table = 0;
209 req.r.rtm_type = RTN_UNSPEC;
210
211 if (cmd == RTM_NEWRULE) {
212 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
213 req.r.rtm_type = RTN_UNICAST;
214 }
215
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000216 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000217 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000218 if (key == 0) /* no match found in keywords array, bail out. */
219 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
220 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000221 inet_prefix dst;
222 NEXT_ARG();
223 get_prefix(&dst, *argv, req.r.rtm_family);
224 req.r.rtm_src_len = dst.bitlen;
225 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000226 } else if (key == ARG_to) {
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_dst_len = dst.bitlen;
231 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000232 } else if (key == ARG_preference ||
233 key == ARG_order ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100234 key == ARG_priority
235 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000236 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000237 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000238 pref = get_u32(*argv, "preference");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000239 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000240 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000241 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000242 NEXT_ARG();
243 if (rtnl_dsfield_a2n(&tos, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000244 invarg(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000245 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000246 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000247 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000248 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000249 fwmark = get_u32(*argv, "fwmark");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000250 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000251 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000252 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000253 NEXT_ARG();
254 if (get_rt_realms(&realm, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000255 invarg(*argv, "realms");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000256 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000257 } else if (key == ARG_table ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100258 key == ARG_lookup
259 ) {
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 ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100267 key == ARG_iif
268 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000269 NEXT_ARG();
270 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000271 } else if (key == ARG_nat ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100272 key == ARG_map_to
273 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000274 NEXT_ARG();
275 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
276 req.r.rtm_type = RTN_NAT;
277 } else {
278 int type;
279
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000280 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000281 NEXT_ARG();
282 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000283 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000284 bb_show_usage();
285 if (rtnl_rtntype_a2n(&type, *argv))
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000286 invarg(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000287 req.r.rtm_type = type;
288 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000289 argv++;
290 }
291
292 if (req.r.rtm_family == AF_UNSPEC)
293 req.r.rtm_family = AF_INET;
294
295 if (!table_ok && cmd == RTM_NEWRULE)
296 req.r.rtm_table = RT_TABLE_MAIN;
297
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000298 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000299
300 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
301 return 2;
302
303 return 0;
304}
305
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000306/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000307int do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000308{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000309 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000310 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200311 if (*argv) {
312 smalluint cmd = index_in_substrings(ip_rule_commands, *argv);
313 if (cmd > 3)
314 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
315 argv++;
316 if (cmd < 2)
317 return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000318 }
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200319 return iprule_list(argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000320}