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 fputs(format_host(r->rtm_family,
91 RTA_PAYLOAD(tb[RTA_SRC]),
92 RTA_DATA(tb[RTA_SRC]),
93 abuf, sizeof(abuf)), fp);
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 fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), fp);
163 /* Return value becomes exitcode. It's okay to not return at all */
164 static int iprule_list(char **argv)
166 struct rtnl_handle rth;
167 int af = preferred_family;
173 //bb_error_msg("\"rule show\" needs no arguments");
174 bb_warn_ignoring_args(1);
180 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
181 xrtnl_dump_filter(&rth, print_rule, stdout);
186 /* Return value becomes exitcode. It's okay to not return at all */
187 static int iprule_modify(int cmd, char **argv)
189 static const char keywords[] ALIGN1 =
190 "from\0""to\0""preference\0""order\0""priority\0"
191 "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
192 "iif\0""nat\0""map-to\0""type\0""help\0";
194 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
195 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
196 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
199 struct rtnl_handle rth;
207 memset(&req, 0, sizeof(req));
209 req.n.nlmsg_type = cmd;
210 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
211 req.n.nlmsg_flags = NLM_F_REQUEST;
212 req.r.rtm_family = preferred_family;
213 req.r.rtm_protocol = RTPROT_BOOT;
214 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
216 req.r.rtm_type = RTN_UNSPEC;
218 if (cmd == RTM_NEWRULE) {
219 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
220 req.r.rtm_type = RTN_UNICAST;
224 key = index_in_substrings(keywords, *argv) + 1;
225 if (key == 0) /* no match found in keywords array, bail out. */
226 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
227 if (key == ARG_from) {
230 get_prefix(&dst, *argv, req.r.rtm_family);
231 req.r.rtm_src_len = dst.bitlen;
232 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
233 } else if (key == ARG_to) {
236 get_prefix(&dst, *argv, req.r.rtm_family);
237 req.r.rtm_dst_len = dst.bitlen;
238 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
239 } else if (key == ARG_preference ||
241 key == ARG_priority) {
244 if (get_u32(&pref, *argv, 0))
245 invarg(*argv, "preference");
246 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
247 } else if (key == ARG_tos) {
250 if (rtnl_dsfield_a2n(&tos, *argv))
251 invarg(*argv, "TOS");
253 } else if (key == ARG_fwmark) {
256 if (get_u32(&fwmark, *argv, 0))
257 invarg(*argv, "fwmark");
258 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
259 } else if (key == ARG_realms) {
262 if (get_rt_realms(&realm, *argv))
263 invarg(*argv, "realms");
264 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
265 } else if (key == ARG_table ||
269 if (rtnl_rttable_a2n(&tid, *argv))
270 invarg(*argv, "table ID");
271 req.r.rtm_table = tid;
273 } else if (key == ARG_dev ||
276 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
277 } else if (key == ARG_nat ||
280 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
281 req.r.rtm_type = RTN_NAT;
285 if (key == ARG_type) {
290 if (rtnl_rtntype_a2n(&type, *argv))
291 invarg(*argv, "type");
292 req.r.rtm_type = type;
297 if (req.r.rtm_family == AF_UNSPEC)
298 req.r.rtm_family = AF_INET;
300 if (!table_ok && cmd == RTM_NEWRULE)
301 req.r.rtm_table = RT_TABLE_MAIN;
305 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
311 /* Return value becomes exitcode. It's okay to not return at all */
312 int do_iprule(char **argv)
314 static const char ip_rule_commands[] ALIGN1 =
315 "add\0""delete\0""list\0""show\0";
316 int cmd = 2; /* list */
319 return iprule_list(argv);
321 cmd = index_in_substrings(ip_rule_commands, *argv);
331 return iprule_list(argv+1);
334 bb_error_msg_and_die("unknown command %s", *argv);
336 return iprule_modify(cmd, argv+1);