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 Fischer
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <arpa/inet.h>
28 #include "ip_common.h"
30 static void usage(void) __attribute__((noreturn));
32 static void usage(void)
34 fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
35 fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
36 fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
37 fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
38 fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
39 fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
40 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
44 static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
45 struct nlmsghdr *n, void *arg)
47 FILE *fp = (FILE*)arg;
48 struct rtmsg *r = NLMSG_DATA(n);
49 int len = n->nlmsg_len;
51 struct rtattr * tb[RTA_MAX+1];
55 if (n->nlmsg_type != RTM_NEWRULE)
58 len -= NLMSG_LENGTH(sizeof(*r));
62 memset(tb, 0, sizeof(tb));
63 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
65 if (r->rtm_family == AF_INET)
67 else if (r->rtm_family == AF_INET6)
69 /* else if (r->rtm_family == AF_DECnet)
71 else if (r->rtm_family == AF_IPX)
75 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
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]),
89 fprintf(fp, "%s", format_host(r->rtm_family,
90 RTA_PAYLOAD(tb[RTA_SRC]),
91 RTA_DATA(tb[RTA_SRC]),
95 } else if (r->rtm_src_len) {
96 fprintf(fp, "0/%d", r->rtm_src_len);
103 if (r->rtm_dst_len != host_len) {
104 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
105 RTA_PAYLOAD(tb[RTA_DST]),
106 RTA_DATA(tb[RTA_DST]),
111 fprintf(fp, "to %s ", format_host(r->rtm_family,
112 RTA_PAYLOAD(tb[RTA_DST]),
113 RTA_DATA(tb[RTA_DST]),
114 abuf, sizeof(abuf)));
116 } else if (r->rtm_dst_len) {
117 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
121 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
123 if (tb[RTA_PROTOINFO]) {
124 fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
128 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
132 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
135 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
136 uint32_t from = to>>16;
139 fprintf(fp, "realms %s/",
140 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
143 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
146 if (r->rtm_type == RTN_NAT) {
147 if (tb[RTA_GATEWAY]) {
148 fprintf(fp, "map-to %s ",
149 format_host(r->rtm_family,
150 RTA_PAYLOAD(tb[RTA_GATEWAY]),
151 RTA_DATA(tb[RTA_GATEWAY]),
152 abuf, sizeof(abuf)));
154 fprintf(fp, "masquerade");
155 } else if (r->rtm_type != RTN_UNICAST)
156 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
163 static int iprule_list(int argc, char **argv)
165 struct rtnl_handle rth;
166 int af = preferred_family;
172 //bb_error_msg("\"rule show\" needs no arguments");
173 bb_warn_ignoring_args(argc);
177 if (rtnl_open(&rth, 0) < 0)
180 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
181 bb_perror_msg("Cannot send dump request");
185 if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
186 bb_error_msg("Dump terminated");
194 static int iprule_modify(int cmd, int argc, char **argv)
197 struct rtnl_handle rth;
204 memset(&req, 0, sizeof(req));
206 req.n.nlmsg_type = cmd;
207 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
208 req.n.nlmsg_flags = NLM_F_REQUEST;
209 req.r.rtm_family = preferred_family;
210 req.r.rtm_protocol = RTPROT_BOOT;
211 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
213 req.r.rtm_type = RTN_UNSPEC;
215 if (cmd == RTM_NEWRULE) {
216 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
217 req.r.rtm_type = RTN_UNICAST;
221 if (strcmp(*argv, "from") == 0) {
224 get_prefix(&dst, *argv, req.r.rtm_family);
225 req.r.rtm_src_len = dst.bitlen;
226 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
227 } else if (strcmp(*argv, "to") == 0) {
230 get_prefix(&dst, *argv, req.r.rtm_family);
231 req.r.rtm_dst_len = dst.bitlen;
232 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
233 } else if (matches(*argv, "preference") == 0 ||
234 matches(*argv, "order") == 0 ||
235 matches(*argv, "priority") == 0) {
238 if (get_u32(&pref, *argv, 0))
239 invarg("preference value", *argv);
240 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
241 } else if (strcmp(*argv, "tos") == 0) {
244 if (rtnl_dsfield_a2n(&tos, *argv))
245 invarg("TOS value", *argv);
247 } else if (strcmp(*argv, "fwmark") == 0) {
250 if (get_u32(&fwmark, *argv, 0))
251 invarg("fwmark value", *argv);
252 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
253 } else if (matches(*argv, "realms") == 0) {
256 if (get_rt_realms(&realm, *argv))
257 invarg("realms", *argv);
258 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
259 } else if (matches(*argv, "table") == 0 ||
260 strcmp(*argv, "lookup") == 0) {
263 if (rtnl_rttable_a2n(&tid, *argv))
264 invarg("table ID", *argv);
265 req.r.rtm_table = tid;
267 } else if (strcmp(*argv, "dev") == 0 ||
268 strcmp(*argv, "iif") == 0) {
270 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
271 } else if (strcmp(*argv, "nat") == 0 ||
272 matches(*argv, "map-to") == 0) {
274 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
275 req.r.rtm_type = RTN_NAT;
279 if (strcmp(*argv, "type") == 0) {
282 if (matches(*argv, "help") == 0)
284 if (rtnl_rtntype_a2n(&type, *argv))
285 invarg("Failed to parse rule type", *argv);
286 req.r.rtm_type = type;
292 if (req.r.rtm_family == AF_UNSPEC)
293 req.r.rtm_family = AF_INET;
295 if (!table_ok && cmd == RTM_NEWRULE)
296 req.r.rtm_table = RT_TABLE_MAIN;
298 if (rtnl_open(&rth, 0) < 0)
301 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
307 int do_iprule(int argc, char **argv)
309 const char * const ip_rule_commands[] =
310 {"add", "delete", "list", "show", 0};
311 int cmd = 2; /* list */
314 return iprule_list(0, NULL);
316 cmd = index_in_substr_array(ip_rule_commands, *argv);
327 return iprule_list(argc-1, argv+1);
330 bb_error_msg_and_die("unknown command %s", *argv);
332 return iprule_modify(cmd, argc-1, argv+1);