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
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <arpa/inet.h>
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");
42 static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
43 struct nlmsghdr *n, void *arg)
45 FILE *fp = (FILE*)arg;
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 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
79 if (r->rtm_src_len != host_len) {
80 fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
81 RTA_PAYLOAD(tb[RTA_SRC]),
82 RTA_DATA(tb[RTA_SRC]),
87 fprintf(fp, "%s", format_host(r->rtm_family,
88 RTA_PAYLOAD(tb[RTA_SRC]),
89 RTA_DATA(tb[RTA_SRC]),
93 } else if (r->rtm_src_len) {
94 fprintf(fp, "0/%d", r->rtm_src_len);
101 if (r->rtm_dst_len != host_len) {
102 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
103 RTA_PAYLOAD(tb[RTA_DST]),
104 RTA_DATA(tb[RTA_DST]),
109 fprintf(fp, "to %s ", format_host(r->rtm_family,
110 RTA_PAYLOAD(tb[RTA_DST]),
111 RTA_DATA(tb[RTA_DST]),
112 abuf, sizeof(abuf)));
114 } else if (r->rtm_dst_len) {
115 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
119 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
121 if (tb[RTA_PROTOINFO]) {
122 fprintf(fp, "fwmark %#x ", *(__u32*)RTA_DATA(tb[RTA_PROTOINFO]));
126 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
130 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
133 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
137 fprintf(fp, "realms %s/",
138 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
141 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
144 if (r->rtm_type == RTN_NAT) {
145 if (tb[RTA_GATEWAY]) {
146 fprintf(fp, "map-to %s ",
147 format_host(r->rtm_family,
148 RTA_PAYLOAD(tb[RTA_GATEWAY]),
149 RTA_DATA(tb[RTA_GATEWAY]),
150 abuf, sizeof(abuf)));
152 fprintf(fp, "masquerade");
153 } else if (r->rtm_type != RTN_UNICAST)
154 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
161 int iprule_list(int argc, char **argv)
163 struct rtnl_handle rth;
164 int af = preferred_family;
170 bb_error_msg("\"rule show\" needs no arguments");
174 if (rtnl_open(&rth, 0) < 0)
177 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
178 bb_perror_msg("Cannot send dump request");
182 if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
183 bb_error_msg("Dump terminated");
191 int iprule_modify(int cmd, int argc, char **argv)
194 struct rtnl_handle rth;
201 memset(&req, 0, sizeof(req));
203 req.n.nlmsg_type = cmd;
204 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
205 req.n.nlmsg_flags = NLM_F_REQUEST;
206 req.r.rtm_family = preferred_family;
207 req.r.rtm_protocol = RTPROT_BOOT;
208 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
210 req.r.rtm_type = RTN_UNSPEC;
212 if (cmd == RTM_NEWRULE) {
213 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
214 req.r.rtm_type = RTN_UNICAST;
218 if (strcmp(*argv, "from") == 0) {
221 get_prefix(&dst, *argv, req.r.rtm_family);
222 req.r.rtm_src_len = dst.bitlen;
223 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
224 } else if (strcmp(*argv, "to") == 0) {
227 get_prefix(&dst, *argv, req.r.rtm_family);
228 req.r.rtm_dst_len = dst.bitlen;
229 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
230 } else if (matches(*argv, "preference") == 0 ||
231 matches(*argv, "order") == 0 ||
232 matches(*argv, "priority") == 0) {
235 if (get_u32(&pref, *argv, 0))
236 invarg("preference value", *argv);
237 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
238 } else if (strcmp(*argv, "tos") == 0) {
241 if (rtnl_dsfield_a2n(&tos, *argv))
242 invarg("TOS value", *argv);
244 } else if (strcmp(*argv, "fwmark") == 0) {
247 if (get_u32(&fwmark, *argv, 0))
248 invarg("fwmark value", *argv);
249 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
250 } else if (matches(*argv, "realms") == 0) {
253 if (get_rt_realms(&realm, *argv))
254 invarg("realms", *argv);
255 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
256 } else if (matches(*argv, "table") == 0 ||
257 strcmp(*argv, "lookup") == 0) {
260 if (rtnl_rttable_a2n(&tid, *argv))
261 invarg("table ID", *argv);
262 req.r.rtm_table = tid;
264 } else if (strcmp(*argv, "dev") == 0 ||
265 strcmp(*argv, "iif") == 0) {
267 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
268 } else if (strcmp(*argv, "nat") == 0 ||
269 matches(*argv, "map-to") == 0) {
271 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
272 req.r.rtm_type = RTN_NAT;
276 if (strcmp(*argv, "type") == 0) {
279 if (matches(*argv, "help") == 0)
281 if (rtnl_rtntype_a2n(&type, *argv))
282 invarg("Failed to parse rule type", *argv);
283 req.r.rtm_type = type;
289 if (req.r.rtm_family == AF_UNSPEC)
290 req.r.rtm_family = AF_INET;
292 if (!table_ok && cmd == RTM_NEWRULE)
293 req.r.rtm_table = RT_TABLE_MAIN;
295 if (rtnl_open(&rth, 0) < 0)
298 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
304 int do_iprule(int argc, char **argv)
306 static const char * const ip_rule_commands[] =
307 {"add", "delete", "list", "show", 0};
312 return iprule_list(0, NULL);
314 command_num = index_in_substr_array(ip_rule_commands, *argv);
315 switch (command_num) {
324 return iprule_list(argc-1, argv+1);
327 bb_error_msg_and_die("unknown command %s", *argv);
329 return iprule_modify(cmd, argc-1, argv+1);