1 /* vi: set sw=4 ts=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.
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
15 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
16 * initially integrated into busybox by Bernhard Reutner-Fischer
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <arpa/inet.h>
23 #include "ip_common.h" /* #include "libbb.h" is inside */
28 static void usage(void) __attribute__((noreturn));
30 static void usage(void)
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");
43 static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
44 struct nlmsghdr *n, void *arg UNUSED_PARAM)
46 struct rtmsg *r = NLMSG_DATA(n);
47 int len = n->nlmsg_len;
49 struct rtattr * tb[RTA_MAX+1];
53 if (n->nlmsg_type != RTM_NEWRULE)
56 len -= NLMSG_LENGTH(sizeof(*r));
60 memset(tb, 0, sizeof(tb));
61 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
63 if (r->rtm_family == AF_INET)
65 else if (r->rtm_family == AF_INET6)
67 /* else if (r->rtm_family == AF_DECnet)
69 else if (r->rtm_family == AF_IPX)
73 printf("%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
79 if (r->rtm_src_len != host_len) {
80 printf("%s/%u", rt_addr_n2a(r->rtm_family,
81 RTA_DATA(tb[RTA_SRC]),
86 fputs(format_host(r->rtm_family,
87 RTA_PAYLOAD(tb[RTA_SRC]),
88 RTA_DATA(tb[RTA_SRC]),
89 abuf, sizeof(abuf)), stdout);
91 } else if (r->rtm_src_len) {
92 printf("0/%d", r->rtm_src_len);
99 if (r->rtm_dst_len != host_len) {
100 printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
101 RTA_DATA(tb[RTA_DST]),
106 printf("to %s ", format_host(r->rtm_family,
107 RTA_PAYLOAD(tb[RTA_DST]),
108 RTA_DATA(tb[RTA_DST]),
109 abuf, sizeof(abuf)));
111 } else if (r->rtm_dst_len) {
112 printf("to 0/%d ", r->rtm_dst_len);
116 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1));
118 if (tb[RTA_PROTOINFO]) {
119 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
123 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
127 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1));
130 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
131 uint32_t from = to>>16;
135 rtnl_rtrealm_n2a(from, b1));
138 rtnl_rtrealm_n2a(to, b1));
141 if (r->rtm_type == RTN_NAT) {
142 if (tb[RTA_GATEWAY]) {
144 format_host(r->rtm_family,
145 RTA_PAYLOAD(tb[RTA_GATEWAY]),
146 RTA_DATA(tb[RTA_GATEWAY]),
147 abuf, sizeof(abuf)));
149 printf("masquerade");
150 } else if (r->rtm_type != RTN_UNICAST)
151 fputs(rtnl_rtntype_n2a(r->rtm_type, b1), stdout);
158 /* Return value becomes exitcode. It's okay to not return at all */
159 static int iprule_list(char **argv)
161 struct rtnl_handle rth;
162 int af = preferred_family;
168 //bb_error_msg("\"rule show\" needs no arguments");
169 bb_warn_ignoring_args(*argv);
175 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
176 xrtnl_dump_filter(&rth, print_rule, NULL);
181 /* Return value becomes exitcode. It's okay to not return at all */
182 static int iprule_modify(int cmd, char **argv)
184 static const char keywords[] ALIGN1 =
185 "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";
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
194 struct rtnl_handle rth;
202 memset(&req, 0, sizeof(req));
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;
211 req.r.rtm_type = RTN_UNSPEC;
213 if (cmd == RTM_NEWRULE) {
214 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
215 req.r.rtm_type = RTN_UNICAST;
219 key = index_in_substrings(keywords, *argv) + 1;
220 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) {
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);
228 } else if (key == ARG_to) {
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);
234 } else if (key == ARG_preference ||
240 pref = get_u32(*argv, "preference");
241 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
242 } else if (key == ARG_tos) {
245 if (rtnl_dsfield_a2n(&tos, *argv))
246 invarg(*argv, "TOS");
248 } else if (key == ARG_fwmark) {
251 fwmark = get_u32(*argv, "fwmark");
252 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
253 } else if (key == ARG_realms) {
256 if (get_rt_realms(&realm, *argv))
257 invarg(*argv, "realms");
258 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
259 } else if (key == ARG_table ||
264 if (rtnl_rttable_a2n(&tid, *argv))
265 invarg(*argv, "table ID");
266 req.r.rtm_table = tid;
268 } else if (key == ARG_dev ||
272 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
273 } else if (key == ARG_nat ||
277 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
278 req.r.rtm_type = RTN_NAT;
282 if (key == ARG_type) {
287 if (rtnl_rtntype_a2n(&type, *argv))
288 invarg(*argv, "type");
289 req.r.rtm_type = type;
294 if (req.r.rtm_family == AF_UNSPEC)
295 req.r.rtm_family = AF_INET;
297 if (!table_ok && cmd == RTM_NEWRULE)
298 req.r.rtm_table = RT_TABLE_MAIN;
302 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
308 /* Return value becomes exitcode. It's okay to not return at all */
309 int do_iprule(char **argv)
311 static const char ip_rule_commands[] ALIGN1 =
312 "add\0""delete\0""list\0""show\0";
313 int cmd = 2; /* list */
316 return iprule_list(argv);
318 cmd = index_in_substrings(ip_rule_commands, *argv);
328 return iprule_list(argv+1);
331 bb_error_msg_and_die("unknown command %s", *argv);
333 return iprule_modify(cmd, argv+1);