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>
27 #include <linux/fib_rules.h>
31 #include "ip_common.h"
33 extern struct rtnl_handle rth
;
35 static void usage(void) __attribute__((noreturn
));
37 static void usage(void)
39 fprintf(stderr
, "Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION\n");
40 fprintf(stderr
, "SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n");
41 fprintf(stderr
, " [ dev STRING ] [ pref NUMBER ]\n");
42 fprintf(stderr
, "ACTION := [ table TABLE_ID ]\n");
43 fprintf(stderr
, " [ prohibit | reject | unreachable ]\n");
44 fprintf(stderr
, " [ realms [SRCREALM/]DSTREALM ]\n");
45 fprintf(stderr
, " [ goto NUMBER ]\n");
46 fprintf(stderr
, "TABLE_ID := [ local | main | default | NUMBER ]\n");
50 int print_rule(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
52 FILE *fp
= (FILE*)arg
;
53 struct rtmsg
*r
= NLMSG_DATA(n
);
54 int len
= n
->nlmsg_len
;
57 struct rtattr
* tb
[FRA_MAX
+1];
61 if (n
->nlmsg_type
!= RTM_NEWRULE
&& n
->nlmsg_type
!= RTM_DELRULE
)
64 len
-= NLMSG_LENGTH(sizeof(*r
));
68 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(r
), len
);
70 if (r
->rtm_family
== AF_INET
)
72 else if (r
->rtm_family
== AF_INET6
)
74 else if (r
->rtm_family
== AF_DECnet
)
76 else if (r
->rtm_family
== AF_IPX
)
79 if (n
->nlmsg_type
== RTM_DELRULE
)
80 fprintf(fp
, "Deleted ");
83 fprintf(fp
, "%u:\t", *(unsigned*)RTA_DATA(tb
[FRA_PRIORITY
]));
87 if (r
->rtm_flags
& FIB_RULE_INVERT
)
91 if (r
->rtm_src_len
!= host_len
) {
92 fprintf(fp
, "from %s/%u ", rt_addr_n2a(r
->rtm_family
,
93 RTA_PAYLOAD(tb
[FRA_SRC
]),
94 RTA_DATA(tb
[FRA_SRC
]),
99 fprintf(fp
, "from %s ", format_host(r
->rtm_family
,
100 RTA_PAYLOAD(tb
[FRA_SRC
]),
101 RTA_DATA(tb
[FRA_SRC
]),
105 } else if (r
->rtm_src_len
) {
106 fprintf(fp
, "from 0/%d ", r
->rtm_src_len
);
108 fprintf(fp
, "from all ");
112 if (r
->rtm_dst_len
!= host_len
) {
113 fprintf(fp
, "to %s/%u ", rt_addr_n2a(r
->rtm_family
,
114 RTA_PAYLOAD(tb
[FRA_DST
]),
115 RTA_DATA(tb
[FRA_DST
]),
120 fprintf(fp
, "to %s ", format_host(r
->rtm_family
,
121 RTA_PAYLOAD(tb
[FRA_DST
]),
122 RTA_DATA(tb
[FRA_DST
]),
123 abuf
, sizeof(abuf
)));
125 } else if (r
->rtm_dst_len
) {
126 fprintf(fp
, "to 0/%d ", r
->rtm_dst_len
);
131 fprintf(fp
, "tos %s ", rtnl_dsfield_n2a(r
->rtm_tos
, b1
, sizeof(b1
)));
134 if (tb
[FRA_FWMARK
] || tb
[FRA_FWMASK
]) {
135 __u32 mark
= 0, mask
= 0;
138 mark
= *(__u32
*)RTA_DATA(tb
[FRA_FWMARK
]);
140 if (tb
[FRA_FWMASK
] &&
141 (mask
= *(__u32
*)RTA_DATA(tb
[FRA_FWMASK
])) != 0xFFFFFFFF)
142 fprintf(fp
, "fwmark 0x%x/0x%x ", mark
, mask
);
144 fprintf(fp
, "fwmark 0x%x ", mark
);
147 if (tb
[FRA_IFNAME
]) {
148 fprintf(fp
, "iif %s ", (char*)RTA_DATA(tb
[FRA_IFNAME
]));
149 if (r
->rtm_flags
& FIB_RULE_DEV_DETACHED
)
150 fprintf(fp
, "[detached] ");
153 table
= rtm_get_table(r
, tb
);
155 fprintf(fp
, "lookup %s ", rtnl_rttable_n2a(table
, b1
, sizeof(b1
)));
158 __u32 to
= *(__u32
*)RTA_DATA(tb
[FRA_FLOW
]);
162 fprintf(fp
, "realms %s/",
163 rtnl_rtrealm_n2a(from
, b1
, sizeof(b1
)));
166 rtnl_rtrealm_n2a(to
, b1
, sizeof(b1
)));
169 if (r
->rtm_type
== RTN_NAT
) {
170 if (tb
[RTA_GATEWAY
]) {
171 fprintf(fp
, "map-to %s ",
172 format_host(r
->rtm_family
,
173 RTA_PAYLOAD(tb
[RTA_GATEWAY
]),
174 RTA_DATA(tb
[RTA_GATEWAY
]),
175 abuf
, sizeof(abuf
)));
177 fprintf(fp
, "masquerade");
178 } else if (r
->rtm_type
== FR_ACT_GOTO
) {
179 fprintf(fp
, "goto ");
181 fprintf(fp
, "%u", *(__u32
*) RTA_DATA(tb
[FRA_GOTO
]));
184 if (r
->rtm_flags
& FIB_RULE_UNRESOLVED
)
185 fprintf(fp
, " [unresolved]");
186 } else if (r
->rtm_type
== FR_ACT_NOP
)
188 else if (r
->rtm_type
!= RTN_UNICAST
)
189 fprintf(fp
, "%s", rtnl_rtntype_n2a(r
->rtm_type
, b1
, sizeof(b1
)));
196 static int iprule_list(int argc
, char **argv
)
198 int af
= preferred_family
;
204 fprintf(stderr
, "\"ip rule show\" does not take any arguments.\n");
208 if (rtnl_wilddump_request(&rth
, af
, RTM_GETRULE
) < 0) {
209 perror("Cannot send dump request");
213 if (rtnl_dump_filter(&rth
, print_rule
, stdout
, NULL
, NULL
) < 0) {
214 fprintf(stderr
, "Dump terminated\n");
222 static int iprule_modify(int cmd
, int argc
, char **argv
)
231 memset(&req
, 0, sizeof(req
));
233 req
.n
.nlmsg_type
= cmd
;
234 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
235 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
236 req
.r
.rtm_family
= preferred_family
;
237 req
.r
.rtm_protocol
= RTPROT_BOOT
;
238 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
240 req
.r
.rtm_type
= RTN_UNSPEC
;
243 if (cmd
== RTM_NEWRULE
) {
244 req
.n
.nlmsg_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
245 req
.r
.rtm_type
= RTN_UNICAST
;
249 if (strcmp(*argv
, "not") == 0) {
250 req
.r
.rtm_flags
|= FIB_RULE_INVERT
;
251 } else if (strcmp(*argv
, "from") == 0) {
254 get_prefix(&dst
, *argv
, req
.r
.rtm_family
);
255 req
.r
.rtm_src_len
= dst
.bitlen
;
256 addattr_l(&req
.n
, sizeof(req
), FRA_SRC
, &dst
.data
, dst
.bytelen
);
257 } else if (strcmp(*argv
, "to") == 0) {
260 get_prefix(&dst
, *argv
, req
.r
.rtm_family
);
261 req
.r
.rtm_dst_len
= dst
.bitlen
;
262 addattr_l(&req
.n
, sizeof(req
), FRA_DST
, &dst
.data
, dst
.bytelen
);
263 } else if (matches(*argv
, "preference") == 0 ||
264 matches(*argv
, "order") == 0 ||
265 matches(*argv
, "priority") == 0) {
268 if (get_u32(&pref
, *argv
, 0))
269 invarg("preference value is invalid\n", *argv
);
270 addattr32(&req
.n
, sizeof(req
), FRA_PRIORITY
, pref
);
271 } else if (strcmp(*argv
, "tos") == 0) {
274 if (rtnl_dsfield_a2n(&tos
, *argv
))
275 invarg("TOS value is invalid\n", *argv
);
277 } else if (strcmp(*argv
, "fwmark") == 0) {
279 __u32 fwmark
, fwmask
;
281 if ((slash
= strchr(*argv
, '/')) != NULL
)
283 if (get_u32(&fwmark
, *argv
, 0))
284 invarg("fwmark value is invalid\n", *argv
);
285 addattr32(&req
.n
, sizeof(req
), FRA_FWMARK
, fwmark
);
287 if (get_u32(&fwmask
, slash
+1, 0))
288 invarg("fwmask value is invalid\n", slash
+1);
289 addattr32(&req
.n
, sizeof(req
), FRA_FWMASK
, fwmask
);
291 } else if (matches(*argv
, "realms") == 0) {
294 if (get_rt_realms(&realm
, *argv
))
295 invarg("invalid realms\n", *argv
);
296 addattr32(&req
.n
, sizeof(req
), FRA_FLOW
, realm
);
297 } else if (matches(*argv
, "table") == 0 ||
298 strcmp(*argv
, "lookup") == 0) {
301 if (rtnl_rttable_a2n(&tid
, *argv
))
302 invarg("invalid table ID\n", *argv
);
304 req
.r
.rtm_table
= tid
;
306 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
307 addattr32(&req
.n
, sizeof(req
), FRA_TABLE
, tid
);
310 } else if (strcmp(*argv
, "dev") == 0 ||
311 strcmp(*argv
, "iif") == 0) {
313 addattr_l(&req
.n
, sizeof(req
), FRA_IFNAME
, *argv
, strlen(*argv
)+1);
314 } else if (strcmp(*argv
, "nat") == 0 ||
315 matches(*argv
, "map-to") == 0) {
317 fprintf(stderr
, "Warning: route NAT is deprecated\n");
318 addattr32(&req
.n
, sizeof(req
), RTA_GATEWAY
, get_addr32(*argv
));
319 req
.r
.rtm_type
= RTN_NAT
;
323 if (strcmp(*argv
, "type") == 0) {
326 if (matches(*argv
, "help") == 0)
328 else if (matches(*argv
, "goto") == 0) {
332 if (get_u32(&target
, *argv
, 0))
333 invarg("invalid target\n", *argv
);
334 addattr32(&req
.n
, sizeof(req
), FRA_GOTO
, target
);
335 } else if (matches(*argv
, "nop") == 0)
337 else if (rtnl_rtntype_a2n(&type
, *argv
))
338 invarg("Failed to parse rule type", *argv
);
339 req
.r
.rtm_type
= type
;
346 if (req
.r
.rtm_family
== AF_UNSPEC
)
347 req
.r
.rtm_family
= AF_INET
;
349 if (!table_ok
&& cmd
== RTM_NEWRULE
)
350 req
.r
.rtm_table
= RT_TABLE_MAIN
;
352 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
359 static int flush_rule(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
361 struct rtnl_handle rth2
;
362 struct rtmsg
*r
= NLMSG_DATA(n
);
363 int len
= n
->nlmsg_len
;
364 struct rtattr
* tb
[FRA_MAX
+1];
366 len
-= NLMSG_LENGTH(sizeof(*r
));
370 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(r
), len
);
372 if (tb
[FRA_PRIORITY
]) {
373 n
->nlmsg_type
= RTM_DELRULE
;
374 n
->nlmsg_flags
= NLM_F_REQUEST
;
376 if (rtnl_open(&rth2
, 0) < 0)
379 if (rtnl_talk(&rth2
, n
, 0, 0, NULL
, NULL
, NULL
) < 0)
388 static int iprule_flush(int argc
, char **argv
)
390 int af
= preferred_family
;
396 fprintf(stderr
, "\"ip rule flush\" does not allow arguments\n");
400 if (rtnl_wilddump_request(&rth
, af
, RTM_GETRULE
) < 0) {
401 perror("Cannot send dump request");
405 if (rtnl_dump_filter(&rth
, flush_rule
, NULL
, NULL
, NULL
) < 0) {
406 fprintf(stderr
, "Flush terminated\n");
413 int do_iprule(int argc
, char **argv
)
416 return iprule_list(0, NULL
);
417 } else if (matches(argv
[0], "list") == 0 ||
418 matches(argv
[0], "lst") == 0 ||
419 matches(argv
[0], "show") == 0) {
420 return iprule_list(argc
-1, argv
+1);
421 } else if (matches(argv
[0], "add") == 0) {
422 return iprule_modify(RTM_NEWRULE
, argc
-1, argv
+1);
423 } else if (matches(argv
[0], "delete") == 0) {
424 return iprule_modify(RTM_DELRULE
, argc
-1, argv
+1);
425 } else if (matches(argv
[0], "flush") == 0) {
426 return iprule_flush(argc
-1, argv
+1);
427 } else if (matches(argv
[0], "help") == 0)
430 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv
);