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
20 //#include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <arpa/inet.h>
25 #include "ip_common.h" /* #include "libbb.h" is inside */
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");
45 static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
46 struct nlmsghdr *n, void *arg)
48 FILE *fp = (FILE*)arg;
49 struct rtmsg *r = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
52 struct rtattr * tb[RTA_MAX+1];
56 if (n->nlmsg_type != RTM_NEWRULE)
59 len -= NLMSG_LENGTH(sizeof(*r));
63 memset(tb, 0, sizeof(tb));
64 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
66 if (r->rtm_family == AF_INET)
68 else if (r->rtm_family == AF_INET6)
70 /* else if (r->rtm_family == AF_DECnet)
72 else if (r->rtm_family == AF_IPX)
76 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
82 if (r->rtm_src_len != host_len) {
83 fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
84 RTA_PAYLOAD(tb[RTA_SRC]),
85 RTA_DATA(tb[RTA_SRC]),
90 fprintf(fp, "%s", format_host(r->rtm_family,
91 RTA_PAYLOAD(tb[RTA_SRC]),
92 RTA_DATA(tb[RTA_SRC]),
96 } else if (r->rtm_src_len) {
97 fprintf(fp, "0/%d", r->rtm_src_len);
104 if (r->rtm_dst_len != host_len) {
105 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
106 RTA_PAYLOAD(tb[RTA_DST]),
107 RTA_DATA(tb[RTA_DST]),
112 fprintf(fp, "to %s ", format_host(r->rtm_family,
113 RTA_PAYLOAD(tb[RTA_DST]),
114 RTA_DATA(tb[RTA_DST]),
115 abuf, sizeof(abuf)));
117 } else if (r->rtm_dst_len) {
118 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
122 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
124 if (tb[RTA_PROTOINFO]) {
125 fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
129 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
133 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
136 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
137 uint32_t from = to>>16;
140 fprintf(fp, "realms %s/",
141 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
144 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
147 if (r->rtm_type == RTN_NAT) {
148 if (tb[RTA_GATEWAY]) {
149 fprintf(fp, "map-to %s ",
150 format_host(r->rtm_family,
151 RTA_PAYLOAD(tb[RTA_GATEWAY]),
152 RTA_DATA(tb[RTA_GATEWAY]),
153 abuf, sizeof(abuf)));
155 fprintf(fp, "masquerade");
156 } else if (r->rtm_type != RTN_UNICAST)
157 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
164 /* Return value becomes exitcode. It's okay to not return at all */
165 static int iprule_list(int argc, char **argv)
167 struct rtnl_handle rth;
168 int af = preferred_family;
174 //bb_error_msg("\"rule show\" needs no arguments");
175 bb_warn_ignoring_args(argc);
181 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
182 xrtnl_dump_filter(&rth, print_rule, stdout);
188 /* Return value becomes exitcode. It's okay to not return at all */
189 static int iprule_modify(int cmd, int argc, char **argv)
192 struct rtnl_handle rth;
199 memset(&req, 0, sizeof(req));
201 req.n.nlmsg_type = cmd;
202 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
203 req.n.nlmsg_flags = NLM_F_REQUEST;
204 req.r.rtm_family = preferred_family;
205 req.r.rtm_protocol = RTPROT_BOOT;
206 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
208 req.r.rtm_type = RTN_UNSPEC;
210 if (cmd == RTM_NEWRULE) {
211 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
212 req.r.rtm_type = RTN_UNICAST;
216 if (strcmp(*argv, "from") == 0) {
219 get_prefix(&dst, *argv, req.r.rtm_family);
220 req.r.rtm_src_len = dst.bitlen;
221 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
222 } else if (strcmp(*argv, "to") == 0) {
225 get_prefix(&dst, *argv, req.r.rtm_family);
226 req.r.rtm_dst_len = dst.bitlen;
227 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
228 } else if (matches(*argv, "preference") == 0 ||
229 matches(*argv, "order") == 0 ||
230 matches(*argv, "priority") == 0) {
233 if (get_u32(&pref, *argv, 0))
234 invarg("preference value", *argv);
235 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
236 } else if (strcmp(*argv, "tos") == 0) {
239 if (rtnl_dsfield_a2n(&tos, *argv))
240 invarg("TOS value", *argv);
242 } else if (strcmp(*argv, "fwmark") == 0) {
245 if (get_u32(&fwmark, *argv, 0))
246 invarg("fwmark value", *argv);
247 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
248 } else if (matches(*argv, "realms") == 0) {
251 if (get_rt_realms(&realm, *argv))
252 invarg("realms", *argv);
253 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
254 } else if (matches(*argv, "table") == 0 ||
255 strcmp(*argv, "lookup") == 0) {
258 if (rtnl_rttable_a2n(&tid, *argv))
259 invarg("table ID", *argv);
260 req.r.rtm_table = tid;
262 } else if (strcmp(*argv, "dev") == 0 ||
263 strcmp(*argv, "iif") == 0) {
265 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
266 } else if (strcmp(*argv, "nat") == 0 ||
267 matches(*argv, "map-to") == 0) {
269 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
270 req.r.rtm_type = RTN_NAT;
274 if (strcmp(*argv, "type") == 0) {
277 if (matches(*argv, "help") == 0)
279 if (rtnl_rtntype_a2n(&type, *argv))
280 // bogus-looking error message "invalid argument 'cannot parse rule type' to '<*argv>'"
281 invarg("cannot parse rule type", *argv);
282 req.r.rtm_type = type;
288 if (req.r.rtm_family == AF_UNSPEC)
289 req.r.rtm_family = AF_INET;
291 if (!table_ok && cmd == RTM_NEWRULE)
292 req.r.rtm_table = RT_TABLE_MAIN;
296 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
302 /* Return value becomes exitcode. It's okay to not return at all */
303 int do_iprule(int argc, char **argv)
305 static const char * const ip_rule_commands[] =
306 {"add", "delete", "list", "show", 0};
307 int cmd = 2; /* list */
310 return iprule_list(0, NULL);
312 cmd = index_in_substr_array(ip_rule_commands, *argv);
323 return iprule_list(argc-1, argv+1);
326 bb_error_msg_and_die("unknown command %s", *argv);
328 return iprule_modify(cmd, argc-1, argv+1);