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 <netinet/in.h>
21 #include <netinet/ip.h>
22 #include <arpa/inet.h>
24 #include "ip_common.h" /* #include "libbb.h" is inside */
29 static void usage(void) __attribute__((noreturn));
31 static void usage(void)
33 fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
34 fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
35 fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
36 fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
37 fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
38 fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
39 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 fputs(format_host(r->rtm_family,
90 RTA_PAYLOAD(tb[RTA_SRC]),
91 RTA_DATA(tb[RTA_SRC]),
92 abuf, sizeof(abuf)), fp);
94 } else if (r->rtm_src_len) {
95 fprintf(fp, "0/%d", r->rtm_src_len);
102 if (r->rtm_dst_len != host_len) {
103 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
104 RTA_PAYLOAD(tb[RTA_DST]),
105 RTA_DATA(tb[RTA_DST]),
110 fprintf(fp, "to %s ", format_host(r->rtm_family,
111 RTA_PAYLOAD(tb[RTA_DST]),
112 RTA_DATA(tb[RTA_DST]),
113 abuf, sizeof(abuf)));
115 } else if (r->rtm_dst_len) {
116 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
120 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
122 if (tb[RTA_PROTOINFO]) {
123 fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
127 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
131 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
134 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
135 uint32_t from = to>>16;
138 fprintf(fp, "realms %s/",
139 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
142 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
145 if (r->rtm_type == RTN_NAT) {
146 if (tb[RTA_GATEWAY]) {
147 fprintf(fp, "map-to %s ",
148 format_host(r->rtm_family,
149 RTA_PAYLOAD(tb[RTA_GATEWAY]),
150 RTA_DATA(tb[RTA_GATEWAY]),
151 abuf, sizeof(abuf)));
153 fprintf(fp, "masquerade");
154 } else if (r->rtm_type != RTN_UNICAST)
155 fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), fp);
162 /* Return value becomes exitcode. It's okay to not return at all */
163 static int iprule_list(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(1);
179 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
180 xrtnl_dump_filter(&rth, print_rule, stdout);
185 /* Return value becomes exitcode. It's okay to not return at all */
186 static int iprule_modify(int cmd, char **argv)
188 static const char keywords[] ALIGN1 =
189 "from\0""to\0""preference\0""order\0""priority\0"
190 "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
191 "iif\0""nat\0""map-to\0""type\0""help\0";
193 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
194 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
195 ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
198 struct rtnl_handle rth;
206 memset(&req, 0, sizeof(req));
208 req.n.nlmsg_type = cmd;
209 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
210 req.n.nlmsg_flags = NLM_F_REQUEST;
211 req.r.rtm_family = preferred_family;
212 req.r.rtm_protocol = RTPROT_BOOT;
213 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
215 req.r.rtm_type = RTN_UNSPEC;
217 if (cmd == RTM_NEWRULE) {
218 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
219 req.r.rtm_type = RTN_UNICAST;
223 key = index_in_substrings(keywords, *argv) + 1;
224 if (key == 0) /* no match found in keywords array, bail out. */
225 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
226 if (key == ARG_from) {
229 get_prefix(&dst, *argv, req.r.rtm_family);
230 req.r.rtm_src_len = dst.bitlen;
231 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
232 } else if (key == ARG_to) {
235 get_prefix(&dst, *argv, req.r.rtm_family);
236 req.r.rtm_dst_len = dst.bitlen;
237 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
238 } else if (key == ARG_preference ||
240 key == ARG_priority) {
243 if (get_u32(&pref, *argv, 0))
244 invarg(*argv, "preference");
245 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
246 } else if (key == ARG_tos) {
249 if (rtnl_dsfield_a2n(&tos, *argv))
250 invarg(*argv, "TOS");
252 } else if (key == ARG_fwmark) {
255 if (get_u32(&fwmark, *argv, 0))
256 invarg(*argv, "fwmark");
257 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
258 } else if (key == ARG_realms) {
261 if (get_rt_realms(&realm, *argv))
262 invarg(*argv, "realms");
263 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
264 } else if (key == ARG_table ||
268 if (rtnl_rttable_a2n(&tid, *argv))
269 invarg(*argv, "table ID");
270 req.r.rtm_table = tid;
272 } else if (key == ARG_dev ||
275 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
276 } else if (key == ARG_nat ||
279 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
280 req.r.rtm_type = RTN_NAT;
284 if (key == ARG_type) {
289 if (rtnl_rtntype_a2n(&type, *argv))
290 invarg(*argv, "type");
291 req.r.rtm_type = type;
296 if (req.r.rtm_family == AF_UNSPEC)
297 req.r.rtm_family = AF_INET;
299 if (!table_ok && cmd == RTM_NEWRULE)
300 req.r.rtm_table = RT_TABLE_MAIN;
304 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
310 /* Return value becomes exitcode. It's okay to not return at all */
311 int do_iprule(char **argv)
313 static const char ip_rule_commands[] ALIGN1 =
314 "add\0""delete\0""list\0""show\0";
315 int cmd = 2; /* list */
318 return iprule_list(argv);
320 cmd = index_in_substrings(ip_rule_commands, *argv);
330 return iprule_list(argv+1);
333 bb_error_msg_and_die("unknown command %s", *argv);
335 return iprule_modify(cmd, argv+1);