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>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <arpa/inet.h>
23 #include <linux/fib_rules.h>
27 #include "ip_common.h"
29 extern struct rtnl_handle rth;
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 := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n");
37 fprintf(stderr, " [ iif STRING ] [ oif 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, " [ goto NUMBER ]\n");
42 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
46 int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
48 FILE *fp = (FILE*)arg;
49 struct rtmsg *r = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
53 struct rtattr * tb[FRA_MAX+1];
57 if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
60 len -= NLMSG_LENGTH(sizeof(*r));
64 parse_rtattr(tb, FRA_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)
75 if (n->nlmsg_type == RTM_DELRULE)
76 fprintf(fp, "Deleted ");
79 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[FRA_PRIORITY]));
83 if (r->rtm_flags & FIB_RULE_INVERT)
87 if (r->rtm_src_len != host_len) {
88 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
89 RTA_PAYLOAD(tb[FRA_SRC]),
90 RTA_DATA(tb[FRA_SRC]),
95 fprintf(fp, "from %s ", format_host(r->rtm_family,
96 RTA_PAYLOAD(tb[FRA_SRC]),
97 RTA_DATA(tb[FRA_SRC]),
101 } else if (r->rtm_src_len) {
102 fprintf(fp, "from 0/%d ", r->rtm_src_len);
104 fprintf(fp, "from all ");
108 if (r->rtm_dst_len != host_len) {
109 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
110 RTA_PAYLOAD(tb[FRA_DST]),
111 RTA_DATA(tb[FRA_DST]),
116 fprintf(fp, "to %s ", format_host(r->rtm_family,
117 RTA_PAYLOAD(tb[FRA_DST]),
118 RTA_DATA(tb[FRA_DST]),
119 abuf, sizeof(abuf)));
121 } else if (r->rtm_dst_len) {
122 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
127 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
130 if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
131 __u32 mark = 0, mask = 0;
134 mark = *(__u32*)RTA_DATA(tb[FRA_FWMARK]);
136 if (tb[FRA_FWMASK] &&
137 (mask = *(__u32*)RTA_DATA(tb[FRA_FWMASK])) != 0xFFFFFFFF)
138 fprintf(fp, "fwmark 0x%x/0x%x ", mark, mask);
140 fprintf(fp, "fwmark 0x%x ", mark);
143 if (tb[FRA_IFNAME]) {
144 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[FRA_IFNAME]));
145 if (r->rtm_flags & FIB_RULE_IIF_DETACHED)
146 fprintf(fp, "[detached] ");
149 if (tb[FRA_OIFNAME]) {
150 fprintf(fp, "oif %s ", (char*)RTA_DATA(tb[FRA_OIFNAME]));
151 if (r->rtm_flags & FIB_RULE_OIF_DETACHED)
152 fprintf(fp, "[detached] ");
155 table = rtm_get_table(r, tb);
157 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
160 __u32 to = *(__u32*)RTA_DATA(tb[FRA_FLOW]);
164 fprintf(fp, "realms %s/",
165 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
168 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
171 if (r->rtm_type == RTN_NAT) {
172 if (tb[RTA_GATEWAY]) {
173 fprintf(fp, "map-to %s ",
174 format_host(r->rtm_family,
175 RTA_PAYLOAD(tb[RTA_GATEWAY]),
176 RTA_DATA(tb[RTA_GATEWAY]),
177 abuf, sizeof(abuf)));
179 fprintf(fp, "masquerade");
180 } else if (r->rtm_type == FR_ACT_GOTO) {
181 fprintf(fp, "goto ");
183 fprintf(fp, "%u", *(__u32 *) RTA_DATA(tb[FRA_GOTO]));
186 if (r->rtm_flags & FIB_RULE_UNRESOLVED)
187 fprintf(fp, " [unresolved]");
188 } else if (r->rtm_type == FR_ACT_NOP)
190 else if (r->rtm_type != RTN_UNICAST)
191 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
198 static int iprule_list(int argc, char **argv)
200 int af = preferred_family;
206 fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
210 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
211 perror("Cannot send dump request");
215 if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
216 fprintf(stderr, "Dump terminated\n");
224 static int iprule_modify(int cmd, int argc, char **argv)
233 memset(&req, 0, sizeof(req));
235 req.n.nlmsg_type = cmd;
236 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
237 req.n.nlmsg_flags = NLM_F_REQUEST;
238 req.r.rtm_family = preferred_family;
239 req.r.rtm_protocol = RTPROT_BOOT;
240 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
242 req.r.rtm_type = RTN_UNSPEC;
245 if (cmd == RTM_NEWRULE) {
246 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
247 req.r.rtm_type = RTN_UNICAST;
251 if (strcmp(*argv, "not") == 0) {
252 req.r.rtm_flags |= FIB_RULE_INVERT;
253 } else if (strcmp(*argv, "from") == 0) {
256 get_prefix(&dst, *argv, req.r.rtm_family);
257 req.r.rtm_src_len = dst.bitlen;
258 addattr_l(&req.n, sizeof(req), FRA_SRC, &dst.data, dst.bytelen);
259 } else if (strcmp(*argv, "to") == 0) {
262 get_prefix(&dst, *argv, req.r.rtm_family);
263 req.r.rtm_dst_len = dst.bitlen;
264 addattr_l(&req.n, sizeof(req), FRA_DST, &dst.data, dst.bytelen);
265 } else if (matches(*argv, "preference") == 0 ||
266 matches(*argv, "order") == 0 ||
267 matches(*argv, "priority") == 0) {
270 if (get_u32(&pref, *argv, 0))
271 invarg("preference value is invalid\n", *argv);
272 addattr32(&req.n, sizeof(req), FRA_PRIORITY, pref);
273 } else if (strcmp(*argv, "tos") == 0 ||
274 matches(*argv, "dsfield") == 0) {
277 if (rtnl_dsfield_a2n(&tos, *argv))
278 invarg("TOS value is invalid\n", *argv);
280 } else if (strcmp(*argv, "fwmark") == 0) {
282 __u32 fwmark, fwmask;
284 if ((slash = strchr(*argv, '/')) != NULL)
286 if (get_u32(&fwmark, *argv, 0))
287 invarg("fwmark value is invalid\n", *argv);
288 addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
290 if (get_u32(&fwmask, slash+1, 0))
291 invarg("fwmask value is invalid\n", slash+1);
292 addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask);
294 } else if (matches(*argv, "realms") == 0) {
297 if (get_rt_realms(&realm, *argv))
298 invarg("invalid realms\n", *argv);
299 addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
300 } else if (matches(*argv, "table") == 0 ||
301 strcmp(*argv, "lookup") == 0) {
304 if (rtnl_rttable_a2n(&tid, *argv))
305 invarg("invalid table ID\n", *argv);
307 req.r.rtm_table = tid;
309 req.r.rtm_table = RT_TABLE_UNSPEC;
310 addattr32(&req.n, sizeof(req), FRA_TABLE, tid);
313 } else if (strcmp(*argv, "dev") == 0 ||
314 strcmp(*argv, "iif") == 0) {
316 addattr_l(&req.n, sizeof(req), FRA_IFNAME, *argv, strlen(*argv)+1);
317 } else if (strcmp(*argv, "oif") == 0) {
319 addattr_l(&req.n, sizeof(req), FRA_OIFNAME, *argv, strlen(*argv)+1);
320 } else if (strcmp(*argv, "nat") == 0 ||
321 matches(*argv, "map-to") == 0) {
323 fprintf(stderr, "Warning: route NAT is deprecated\n");
324 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
325 req.r.rtm_type = RTN_NAT;
329 if (strcmp(*argv, "type") == 0) {
332 if (matches(*argv, "help") == 0)
334 else if (matches(*argv, "goto") == 0) {
338 if (get_u32(&target, *argv, 0))
339 invarg("invalid target\n", *argv);
340 addattr32(&req.n, sizeof(req), FRA_GOTO, target);
341 } else if (matches(*argv, "nop") == 0)
343 else if (rtnl_rtntype_a2n(&type, *argv))
344 invarg("Failed to parse rule type", *argv);
345 req.r.rtm_type = type;
352 if (req.r.rtm_family == AF_UNSPEC)
353 req.r.rtm_family = AF_INET;
355 if (!table_ok && cmd == RTM_NEWRULE)
356 req.r.rtm_table = RT_TABLE_MAIN;
358 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
365 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
367 struct rtnl_handle rth2;
368 struct rtmsg *r = NLMSG_DATA(n);
369 int len = n->nlmsg_len;
370 struct rtattr * tb[FRA_MAX+1];
372 len -= NLMSG_LENGTH(sizeof(*r));
376 parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
378 if (tb[FRA_PRIORITY]) {
379 n->nlmsg_type = RTM_DELRULE;
380 n->nlmsg_flags = NLM_F_REQUEST;
382 if (rtnl_open(&rth2, 0) < 0)
385 if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
394 static int iprule_flush(int argc, char **argv)
396 int af = preferred_family;
402 fprintf(stderr, "\"ip rule flush\" does not allow arguments\n");
406 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
407 perror("Cannot send dump request");
411 if (rtnl_dump_filter(&rth, flush_rule, NULL, NULL, NULL) < 0) {
412 fprintf(stderr, "Flush terminated\n");
419 int do_iprule(int argc, char **argv)
422 return iprule_list(0, NULL);
423 } else if (matches(argv[0], "list") == 0 ||
424 matches(argv[0], "lst") == 0 ||
425 matches(argv[0], "show") == 0) {
426 return iprule_list(argc-1, argv+1);
427 } else if (matches(argv[0], "add") == 0) {
428 return iprule_modify(RTM_NEWRULE, argc-1, argv+1);
429 } else if (matches(argv[0], "delete") == 0) {
430 return iprule_modify(RTM_DELRULE, argc-1, argv+1);
431 } else if (matches(argv[0], "flush") == 0) {
432 return iprule_flush(argc-1, argv+1);
433 } else if (matches(argv[0], "help") == 0)
436 fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);
440 int do_multirule(int argc, char **argv)
442 switch (preferred_family) {
445 preferred_family = RTNL_FAMILY_IPMR;
448 preferred_family = RTNL_FAMILY_IP6MR;
450 case RTNL_FAMILY_IPMR:
451 case RTNL_FAMILY_IP6MR:
454 fprintf(stderr, "Multicast rules are only supported for IPv4/IPv6, was: %i\n",
459 return do_iprule(argc, argv);