4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <arpa/inet.h>
31 static void usage(void) __attribute__((noreturn));
33 static void usage(void)
35 fprintf(stderr, "Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION\n");
36 fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
37 fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
38 fprintf(stderr, "ACTION := [ table TABLE_ID ]\n");
39 fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
40 fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
41 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
45 static int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
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 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]));
80 if (r->rtm_src_len != host_len) {
81 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
82 RTA_PAYLOAD(tb[RTA_SRC]),
83 RTA_DATA(tb[RTA_SRC]),
88 fprintf(fp, "from %s ", format_host(r->rtm_family,
89 RTA_PAYLOAD(tb[RTA_SRC]),
90 RTA_DATA(tb[RTA_SRC]),
94 } else if (r->rtm_src_len) {
95 fprintf(fp, "from 0/%d ", r->rtm_src_len);
97 fprintf(fp, "from all ");
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);
120 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
122 if (tb[RTA_PROTOINFO]) {
123 fprintf(fp, "fwmark %#x ", *(__u32*)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 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
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 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
162 static int iprule_list(int argc, char **argv)
164 struct rtnl_handle rth;
165 int af = preferred_family;
171 fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
175 if (rtnl_open(&rth, 0) < 0)
178 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
179 perror("Cannot send dump request");
183 if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
184 fprintf(stderr, "Dump terminated\n");
192 static int iprule_modify(int cmd, int argc, char **argv)
195 struct rtnl_handle rth;
202 memset(&req, 0, sizeof(req));
204 req.n.nlmsg_type = cmd;
205 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
206 req.n.nlmsg_flags = NLM_F_REQUEST;
207 req.r.rtm_family = preferred_family;
208 req.r.rtm_protocol = RTPROT_BOOT;
209 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
211 req.r.rtm_type = RTN_UNSPEC;
213 if (cmd == RTM_NEWRULE) {
214 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
215 req.r.rtm_type = RTN_UNICAST;
219 if (strcmp(*argv, "from") == 0) {
222 get_prefix(&dst, *argv, req.r.rtm_family);
223 req.r.rtm_src_len = dst.bitlen;
224 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
225 } else if (strcmp(*argv, "to") == 0) {
228 get_prefix(&dst, *argv, req.r.rtm_family);
229 req.r.rtm_dst_len = dst.bitlen;
230 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
231 } else if (matches(*argv, "preference") == 0 ||
232 matches(*argv, "order") == 0 ||
233 matches(*argv, "priority") == 0) {
236 if (get_u32(&pref, *argv, 0))
237 invarg("preference value is invalid\n", *argv);
238 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
239 } else if (strcmp(*argv, "tos") == 0) {
242 if (rtnl_dsfield_a2n(&tos, *argv))
243 invarg("TOS value is invalid\n", *argv);
245 } else if (strcmp(*argv, "fwmark") == 0) {
248 if (get_u32(&fwmark, *argv, 0))
249 invarg("fwmark value is invalid\n", *argv);
250 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
251 } else if (matches(*argv, "realms") == 0) {
254 if (get_rt_realms(&realm, *argv))
255 invarg("invalid realms\n", *argv);
256 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
257 } else if (matches(*argv, "table") == 0 ||
258 strcmp(*argv, "lookup") == 0) {
261 if (rtnl_rttable_a2n(&tid, *argv))
262 invarg("invalid table ID\n", *argv);
263 req.r.rtm_table = tid;
265 } else if (strcmp(*argv, "dev") == 0 ||
266 strcmp(*argv, "iif") == 0) {
268 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
269 } else if (strcmp(*argv, "nat") == 0 ||
270 matches(*argv, "map-to") == 0) {
272 fprintf(stderr, "Warning: route NAT is deprecated\n");
273 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
274 req.r.rtm_type = RTN_NAT;
278 if (strcmp(*argv, "type") == 0) {
281 if (matches(*argv, "help") == 0)
283 if (rtnl_rtntype_a2n(&type, *argv))
284 invarg("Failed to parse rule type", *argv);
285 req.r.rtm_type = type;
291 if (req.r.rtm_family == AF_UNSPEC)
292 req.r.rtm_family = AF_INET;
294 if (!table_ok && cmd == RTM_NEWRULE)
295 req.r.rtm_table = RT_TABLE_MAIN;
297 if (rtnl_open(&rth, 0) < 0)
300 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
307 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
309 struct rtnl_handle rth;
310 struct rtmsg *r = NLMSG_DATA(n);
311 int len = n->nlmsg_len;
312 struct rtattr * tb[RTA_MAX+1];
314 len -= NLMSG_LENGTH(sizeof(*r));
318 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
320 if (tb[RTA_PRIORITY]) {
321 n->nlmsg_type = RTM_DELRULE;
322 n->nlmsg_flags = NLM_F_REQUEST;
324 if (rtnl_open(&rth, 0) < 0)
327 if (rtnl_talk(&rth, n, 0, 0, NULL, NULL, NULL) < 0)
334 static int iprule_flush(int argc, char **argv)
336 struct rtnl_handle rth;
337 int af = preferred_family;
343 fprintf(stderr, "\"ip rule flush\" need not any arguments.\n");
347 if (rtnl_open(&rth, 0) < 0)
350 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
351 perror("Cannot send dump request");
355 if (rtnl_dump_filter(&rth, flush_rule, NULL, NULL, NULL) < 0) {
356 fprintf(stderr, "Flush terminated\n");
363 int do_iprule(int argc, char **argv)
366 return iprule_list(0, NULL);
367 } else if (matches(argv[0], "list") == 0 ||
368 matches(argv[0], "lst") == 0 ||
369 matches(argv[0], "show") == 0) {
370 return iprule_list(argc-1, argv+1);
371 } else if (matches(argv[0], "add") == 0) {
372 return iprule_modify(RTM_NEWRULE, argc-1, argv+1);
373 } else if (matches(argv[0], "delete") == 0) {
374 return iprule_modify(RTM_DELRULE, argc-1, argv+1);
375 } else if (matches(argv[0], "flush") == 0) {
376 return iprule_flush(argc-1, argv+1);
377 } else if (matches(argv[0], "help") == 0)
380 fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);