]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
Revert "Remove bogus reference to tc-filters(8) from tc(8) manpage."
[mirror_iproute2.git] / ip / iprule.c
1 /*
2 * iprule.c "ip rule".
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 *
12 * Changes:
13 *
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <arpa/inet.h>
26 #include <string.h>
27 #include <linux/fib_rules.h>
28
29 #include "rt_names.h"
30 #include "utils.h"
31 #include "ip_common.h"
32
33 extern struct rtnl_handle rth;
34
35 static void usage(void) __attribute__((noreturn));
36
37 static void usage(void)
38 {
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");
47 exit(-1);
48 }
49
50 int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
51 {
52 FILE *fp = (FILE*)arg;
53 struct rtmsg *r = NLMSG_DATA(n);
54 int len = n->nlmsg_len;
55 int host_len = -1;
56 __u32 table;
57 struct rtattr * tb[FRA_MAX+1];
58 char abuf[256];
59 SPRINT_BUF(b1);
60
61 if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
62 return 0;
63
64 len -= NLMSG_LENGTH(sizeof(*r));
65 if (len < 0)
66 return -1;
67
68 parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
69
70 if (r->rtm_family == AF_INET)
71 host_len = 32;
72 else if (r->rtm_family == AF_INET6)
73 host_len = 128;
74 else if (r->rtm_family == AF_DECnet)
75 host_len = 16;
76 else if (r->rtm_family == AF_IPX)
77 host_len = 80;
78
79 if (n->nlmsg_type == RTM_DELRULE)
80 fprintf(fp, "Deleted ");
81
82 if (tb[FRA_PRIORITY])
83 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[FRA_PRIORITY]));
84 else
85 fprintf(fp, "0:\t");
86
87 if (r->rtm_flags & FIB_RULE_INVERT)
88 fprintf(fp, "not ");
89
90 if (tb[FRA_SRC]) {
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]),
95 abuf, sizeof(abuf)),
96 r->rtm_src_len
97 );
98 } else {
99 fprintf(fp, "from %s ", format_host(r->rtm_family,
100 RTA_PAYLOAD(tb[FRA_SRC]),
101 RTA_DATA(tb[FRA_SRC]),
102 abuf, sizeof(abuf))
103 );
104 }
105 } else if (r->rtm_src_len) {
106 fprintf(fp, "from 0/%d ", r->rtm_src_len);
107 } else {
108 fprintf(fp, "from all ");
109 }
110
111 if (tb[FRA_DST]) {
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]),
116 abuf, sizeof(abuf)),
117 r->rtm_dst_len
118 );
119 } else {
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)));
124 }
125 } else if (r->rtm_dst_len) {
126 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
127 }
128
129 if (r->rtm_tos) {
130 SPRINT_BUF(b1);
131 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
132 }
133
134 if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
135 __u32 mark = 0, mask = 0;
136
137 if (tb[FRA_FWMARK])
138 mark = *(__u32*)RTA_DATA(tb[FRA_FWMARK]);
139
140 if (tb[FRA_FWMASK] &&
141 (mask = *(__u32*)RTA_DATA(tb[FRA_FWMASK])) != 0xFFFFFFFF)
142 fprintf(fp, "fwmark 0x%x/0x%x ", mark, mask);
143 else
144 fprintf(fp, "fwmark 0x%x ", mark);
145 }
146
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] ");
151 }
152
153 table = rtm_get_table(r, tb);
154 if (table)
155 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
156
157 if (tb[FRA_FLOW]) {
158 __u32 to = *(__u32*)RTA_DATA(tb[FRA_FLOW]);
159 __u32 from = to>>16;
160 to &= 0xFFFF;
161 if (from) {
162 fprintf(fp, "realms %s/",
163 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
164 }
165 fprintf(fp, "%s ",
166 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
167 }
168
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)));
176 } else
177 fprintf(fp, "masquerade");
178 } else if (r->rtm_type == FR_ACT_GOTO) {
179 fprintf(fp, "goto ");
180 if (tb[FRA_GOTO])
181 fprintf(fp, "%u", *(__u32 *) RTA_DATA(tb[FRA_GOTO]));
182 else
183 fprintf(fp, "none");
184 if (r->rtm_flags & FIB_RULE_UNRESOLVED)
185 fprintf(fp, " [unresolved]");
186 } else if (r->rtm_type == FR_ACT_NOP)
187 fprintf(fp, "nop");
188 else if (r->rtm_type != RTN_UNICAST)
189 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
190
191 fprintf(fp, "\n");
192 fflush(fp);
193 return 0;
194 }
195
196 static int iprule_list(int argc, char **argv)
197 {
198 int af = preferred_family;
199
200 if (af == AF_UNSPEC)
201 af = AF_INET;
202
203 if (argc > 0) {
204 fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
205 return -1;
206 }
207
208 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
209 perror("Cannot send dump request");
210 return 1;
211 }
212
213 if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
214 fprintf(stderr, "Dump terminated\n");
215 return 1;
216 }
217
218 return 0;
219 }
220
221
222 static int iprule_modify(int cmd, int argc, char **argv)
223 {
224 int table_ok = 0;
225 struct {
226 struct nlmsghdr n;
227 struct rtmsg r;
228 char buf[1024];
229 } req;
230
231 memset(&req, 0, sizeof(req));
232
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;
239 req.r.rtm_table = 0;
240 req.r.rtm_type = RTN_UNSPEC;
241 req.r.rtm_flags = 0;
242
243 if (cmd == RTM_NEWRULE) {
244 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
245 req.r.rtm_type = RTN_UNICAST;
246 }
247
248 while (argc > 0) {
249 if (strcmp(*argv, "not") == 0) {
250 req.r.rtm_flags |= FIB_RULE_INVERT;
251 } else if (strcmp(*argv, "from") == 0) {
252 inet_prefix dst;
253 NEXT_ARG();
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) {
258 inet_prefix dst;
259 NEXT_ARG();
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) {
266 __u32 pref;
267 NEXT_ARG();
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) {
272 __u32 tos;
273 NEXT_ARG();
274 if (rtnl_dsfield_a2n(&tos, *argv))
275 invarg("TOS value is invalid\n", *argv);
276 req.r.rtm_tos = tos;
277 } else if (strcmp(*argv, "fwmark") == 0) {
278 char *slash;
279 __u32 fwmark, fwmask;
280 NEXT_ARG();
281 if ((slash = strchr(*argv, '/')) != NULL)
282 *slash = '\0';
283 if (get_u32(&fwmark, *argv, 0))
284 invarg("fwmark value is invalid\n", *argv);
285 addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
286 if (slash) {
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);
290 }
291 } else if (matches(*argv, "realms") == 0) {
292 __u32 realm;
293 NEXT_ARG();
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) {
299 __u32 tid;
300 NEXT_ARG();
301 if (rtnl_rttable_a2n(&tid, *argv))
302 invarg("invalid table ID\n", *argv);
303 if (tid < 256)
304 req.r.rtm_table = tid;
305 else {
306 req.r.rtm_table = RT_TABLE_UNSPEC;
307 addattr32(&req.n, sizeof(req), FRA_TABLE, tid);
308 }
309 table_ok = 1;
310 } else if (strcmp(*argv, "dev") == 0 ||
311 strcmp(*argv, "iif") == 0) {
312 NEXT_ARG();
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) {
316 NEXT_ARG();
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;
320 } else {
321 int type;
322
323 if (strcmp(*argv, "type") == 0) {
324 NEXT_ARG();
325 }
326 if (matches(*argv, "help") == 0)
327 usage();
328 else if (matches(*argv, "goto") == 0) {
329 __u32 target;
330 type = FR_ACT_GOTO;
331 NEXT_ARG();
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)
336 type = FR_ACT_NOP;
337 else if (rtnl_rtntype_a2n(&type, *argv))
338 invarg("Failed to parse rule type", *argv);
339 req.r.rtm_type = type;
340 table_ok = 1;
341 }
342 argc--;
343 argv++;
344 }
345
346 if (req.r.rtm_family == AF_UNSPEC)
347 req.r.rtm_family = AF_INET;
348
349 if (!table_ok && cmd == RTM_NEWRULE)
350 req.r.rtm_table = RT_TABLE_MAIN;
351
352 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
353 return 2;
354
355 return 0;
356 }
357
358
359 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
360 {
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];
365
366 len -= NLMSG_LENGTH(sizeof(*r));
367 if (len < 0)
368 return -1;
369
370 parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
371
372 if (tb[FRA_PRIORITY]) {
373 n->nlmsg_type = RTM_DELRULE;
374 n->nlmsg_flags = NLM_F_REQUEST;
375
376 if (rtnl_open(&rth2, 0) < 0)
377 return -1;
378
379 if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
380 return -2;
381
382 rtnl_close(&rth2);
383 }
384
385 return 0;
386 }
387
388 static int iprule_flush(int argc, char **argv)
389 {
390 int af = preferred_family;
391
392 if (af == AF_UNSPEC)
393 af = AF_INET;
394
395 if (argc > 0) {
396 fprintf(stderr, "\"ip rule flush\" does not allow arguments\n");
397 return -1;
398 }
399
400 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
401 perror("Cannot send dump request");
402 return 1;
403 }
404
405 if (rtnl_dump_filter(&rth, flush_rule, NULL, NULL, NULL) < 0) {
406 fprintf(stderr, "Flush terminated\n");
407 return 1;
408 }
409
410 return 0;
411 }
412
413 int do_iprule(int argc, char **argv)
414 {
415 if (argc < 1) {
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)
428 usage();
429
430 fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);
431 exit(-1);
432 }
433