]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
Merge /tmp/iproute2
[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
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <arpa/inet.h>
22 #include <string.h>
23 #include <linux/fib_rules.h>
24
25 #include "rt_names.h"
26 #include "utils.h"
27 #include "ip_common.h"
28
29 extern struct rtnl_handle rth;
30
31 static void usage(void) __attribute__((noreturn));
32
33 static void usage(void)
34 {
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, " SUPPRESSOR\n");
43 fprintf(stderr, "SUPPRESSOR := [ suppress_prefixlength NUMBER ]\n");
44 fprintf(stderr, " [ suppress_ifgroup DEVGROUP ]\n");
45 fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
46 exit(-1);
47 }
48
49 int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
50 {
51 FILE *fp = (FILE*)arg;
52 struct rtmsg *r = NLMSG_DATA(n);
53 int len = n->nlmsg_len;
54 int host_len = -1;
55 __u32 table;
56 struct rtattr * tb[FRA_MAX+1];
57 char abuf[256];
58 SPRINT_BUF(b1);
59
60 if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
61 return 0;
62
63 len -= NLMSG_LENGTH(sizeof(*r));
64 if (len < 0)
65 return -1;
66
67 parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
68
69 if (r->rtm_family == AF_INET)
70 host_len = 32;
71 else if (r->rtm_family == AF_INET6)
72 host_len = 128;
73 else if (r->rtm_family == AF_DECnet)
74 host_len = 16;
75 else if (r->rtm_family == AF_IPX)
76 host_len = 80;
77
78 if (n->nlmsg_type == RTM_DELRULE)
79 fprintf(fp, "Deleted ");
80
81 if (tb[FRA_PRIORITY])
82 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[FRA_PRIORITY]));
83 else
84 fprintf(fp, "0:\t");
85
86 if (r->rtm_flags & FIB_RULE_INVERT)
87 fprintf(fp, "not ");
88
89 if (tb[FRA_SRC]) {
90 if (r->rtm_src_len != host_len) {
91 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
92 RTA_PAYLOAD(tb[FRA_SRC]),
93 RTA_DATA(tb[FRA_SRC]),
94 abuf, sizeof(abuf)),
95 r->rtm_src_len
96 );
97 } else {
98 fprintf(fp, "from %s ", format_host(r->rtm_family,
99 RTA_PAYLOAD(tb[FRA_SRC]),
100 RTA_DATA(tb[FRA_SRC]),
101 abuf, sizeof(abuf))
102 );
103 }
104 } else if (r->rtm_src_len) {
105 fprintf(fp, "from 0/%d ", r->rtm_src_len);
106 } else {
107 fprintf(fp, "from all ");
108 }
109
110 if (tb[FRA_DST]) {
111 if (r->rtm_dst_len != host_len) {
112 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
113 RTA_PAYLOAD(tb[FRA_DST]),
114 RTA_DATA(tb[FRA_DST]),
115 abuf, sizeof(abuf)),
116 r->rtm_dst_len
117 );
118 } else {
119 fprintf(fp, "to %s ", format_host(r->rtm_family,
120 RTA_PAYLOAD(tb[FRA_DST]),
121 RTA_DATA(tb[FRA_DST]),
122 abuf, sizeof(abuf)));
123 }
124 } else if (r->rtm_dst_len) {
125 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
126 }
127
128 if (r->rtm_tos) {
129 SPRINT_BUF(b1);
130 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
131 }
132
133 if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
134 __u32 mark = 0, mask = 0;
135
136 if (tb[FRA_FWMARK])
137 mark = rta_getattr_u32(tb[FRA_FWMARK]);
138
139 if (tb[FRA_FWMASK] &&
140 (mask = rta_getattr_u32(tb[FRA_FWMASK])) != 0xFFFFFFFF)
141 fprintf(fp, "fwmark 0x%x/0x%x ", mark, mask);
142 else
143 fprintf(fp, "fwmark 0x%x ", mark);
144 }
145
146 if (tb[FRA_IFNAME]) {
147 fprintf(fp, "iif %s ", rta_getattr_str(tb[FRA_IFNAME]));
148 if (r->rtm_flags & FIB_RULE_IIF_DETACHED)
149 fprintf(fp, "[detached] ");
150 }
151
152 if (tb[FRA_OIFNAME]) {
153 fprintf(fp, "oif %s ", rta_getattr_str(tb[FRA_OIFNAME]));
154 if (r->rtm_flags & FIB_RULE_OIF_DETACHED)
155 fprintf(fp, "[detached] ");
156 }
157
158 table = rtm_get_table(r, tb);
159 if (table) {
160 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
161
162 if (tb[FRA_SUPPRESS_PREFIXLEN]) {
163 int pl = rta_getattr_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
164 if (pl != -1) {
165 fprintf(fp, "suppress_prefixlength %d ", pl);
166 }
167 }
168 if (tb[FRA_SUPPRESS_IFGROUP]) {
169 int group = rta_getattr_u32(tb[FRA_SUPPRESS_IFGROUP]);
170 if (group != -1) {
171 SPRINT_BUF(b1);
172 fprintf(fp, "suppress_ifgroup %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
173 }
174 }
175 }
176
177 if (tb[FRA_FLOW]) {
178 __u32 to = rta_getattr_u32(tb[FRA_FLOW]);
179 __u32 from = to>>16;
180 to &= 0xFFFF;
181 if (from) {
182 fprintf(fp, "realms %s/",
183 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
184 }
185 fprintf(fp, "%s ",
186 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
187 }
188
189 if (r->rtm_type == RTN_NAT) {
190 if (tb[RTA_GATEWAY]) {
191 fprintf(fp, "map-to %s ",
192 format_host(r->rtm_family,
193 RTA_PAYLOAD(tb[RTA_GATEWAY]),
194 RTA_DATA(tb[RTA_GATEWAY]),
195 abuf, sizeof(abuf)));
196 } else
197 fprintf(fp, "masquerade");
198 } else if (r->rtm_type == FR_ACT_GOTO) {
199 fprintf(fp, "goto ");
200 if (tb[FRA_GOTO])
201 fprintf(fp, "%u", rta_getattr_u32(tb[FRA_GOTO]));
202 else
203 fprintf(fp, "none");
204 if (r->rtm_flags & FIB_RULE_UNRESOLVED)
205 fprintf(fp, " [unresolved]");
206 } else if (r->rtm_type == FR_ACT_NOP)
207 fprintf(fp, "nop");
208 else if (r->rtm_type != RTN_UNICAST)
209 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
210
211 fprintf(fp, "\n");
212 fflush(fp);
213 return 0;
214 }
215
216 static int iprule_list(int argc, char **argv)
217 {
218 int af = preferred_family;
219
220 if (af == AF_UNSPEC)
221 af = AF_INET;
222
223 if (argc > 0) {
224 fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
225 return -1;
226 }
227
228 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
229 perror("Cannot send dump request");
230 return 1;
231 }
232
233 if (rtnl_dump_filter(&rth, print_rule, stdout) < 0) {
234 fprintf(stderr, "Dump terminated\n");
235 return 1;
236 }
237
238 return 0;
239 }
240
241
242 static int iprule_modify(int cmd, int argc, char **argv)
243 {
244 int table_ok = 0;
245 struct {
246 struct nlmsghdr n;
247 struct rtmsg r;
248 char buf[1024];
249 } req;
250
251 memset(&req, 0, sizeof(req));
252
253 req.n.nlmsg_type = cmd;
254 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
255 req.n.nlmsg_flags = NLM_F_REQUEST;
256 req.r.rtm_family = preferred_family;
257 req.r.rtm_protocol = RTPROT_BOOT;
258 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
259 req.r.rtm_table = 0;
260 req.r.rtm_type = RTN_UNSPEC;
261 req.r.rtm_flags = 0;
262
263 if (cmd == RTM_NEWRULE) {
264 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
265 req.r.rtm_type = RTN_UNICAST;
266 }
267
268 while (argc > 0) {
269 if (strcmp(*argv, "not") == 0) {
270 req.r.rtm_flags |= FIB_RULE_INVERT;
271 } else if (strcmp(*argv, "from") == 0) {
272 inet_prefix dst;
273 NEXT_ARG();
274 get_prefix(&dst, *argv, req.r.rtm_family);
275 req.r.rtm_src_len = dst.bitlen;
276 addattr_l(&req.n, sizeof(req), FRA_SRC, &dst.data, dst.bytelen);
277 } else if (strcmp(*argv, "to") == 0) {
278 inet_prefix dst;
279 NEXT_ARG();
280 get_prefix(&dst, *argv, req.r.rtm_family);
281 req.r.rtm_dst_len = dst.bitlen;
282 addattr_l(&req.n, sizeof(req), FRA_DST, &dst.data, dst.bytelen);
283 } else if (matches(*argv, "preference") == 0 ||
284 matches(*argv, "order") == 0 ||
285 matches(*argv, "priority") == 0) {
286 __u32 pref;
287 NEXT_ARG();
288 if (get_u32(&pref, *argv, 0))
289 invarg("preference value is invalid\n", *argv);
290 addattr32(&req.n, sizeof(req), FRA_PRIORITY, pref);
291 } else if (strcmp(*argv, "tos") == 0 ||
292 matches(*argv, "dsfield") == 0) {
293 __u32 tos;
294 NEXT_ARG();
295 if (rtnl_dsfield_a2n(&tos, *argv))
296 invarg("TOS value is invalid\n", *argv);
297 req.r.rtm_tos = tos;
298 } else if (strcmp(*argv, "fwmark") == 0) {
299 char *slash;
300 __u32 fwmark, fwmask;
301 NEXT_ARG();
302 if ((slash = strchr(*argv, '/')) != NULL)
303 *slash = '\0';
304 if (get_u32(&fwmark, *argv, 0))
305 invarg("fwmark value is invalid\n", *argv);
306 addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
307 if (slash) {
308 if (get_u32(&fwmask, slash+1, 0))
309 invarg("fwmask value is invalid\n", slash+1);
310 addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask);
311 }
312 } else if (matches(*argv, "realms") == 0) {
313 __u32 realm;
314 NEXT_ARG();
315 if (get_rt_realms(&realm, *argv))
316 invarg("invalid realms\n", *argv);
317 addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
318 } else if (matches(*argv, "table") == 0 ||
319 strcmp(*argv, "lookup") == 0) {
320 __u32 tid;
321 NEXT_ARG();
322 if (rtnl_rttable_a2n(&tid, *argv))
323 invarg("invalid table ID\n", *argv);
324 if (tid < 256)
325 req.r.rtm_table = tid;
326 else {
327 req.r.rtm_table = RT_TABLE_UNSPEC;
328 addattr32(&req.n, sizeof(req), FRA_TABLE, tid);
329 }
330 table_ok = 1;
331 } else if (matches(*argv, "suppress_prefixlength") == 0 ||
332 strcmp(*argv, "sup_pl") == 0) {
333 int pl;
334 NEXT_ARG();
335 if (get_s32(&pl, *argv, 0) || pl < 0)
336 invarg("suppress_prefixlength value is invalid\n", *argv);
337 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, pl);
338 } else if (matches(*argv, "suppress_ifgroup") == 0 ||
339 strcmp(*argv, "sup_group") == 0) {
340 NEXT_ARG();
341 int group;
342 if (rtnl_group_a2n(&group, *argv))
343 invarg("Invalid \"suppress_ifgroup\" value\n", *argv);
344 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, group);
345 } else if (strcmp(*argv, "dev") == 0 ||
346 strcmp(*argv, "iif") == 0) {
347 NEXT_ARG();
348 addattr_l(&req.n, sizeof(req), FRA_IFNAME, *argv, strlen(*argv)+1);
349 } else if (strcmp(*argv, "oif") == 0) {
350 NEXT_ARG();
351 addattr_l(&req.n, sizeof(req), FRA_OIFNAME, *argv, strlen(*argv)+1);
352 } else if (strcmp(*argv, "nat") == 0 ||
353 matches(*argv, "map-to") == 0) {
354 NEXT_ARG();
355 fprintf(stderr, "Warning: route NAT is deprecated\n");
356 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
357 req.r.rtm_type = RTN_NAT;
358 } else {
359 int type;
360
361 if (strcmp(*argv, "type") == 0) {
362 NEXT_ARG();
363 }
364 if (matches(*argv, "help") == 0)
365 usage();
366 else if (matches(*argv, "goto") == 0) {
367 __u32 target;
368 type = FR_ACT_GOTO;
369 NEXT_ARG();
370 if (get_u32(&target, *argv, 0))
371 invarg("invalid target\n", *argv);
372 addattr32(&req.n, sizeof(req), FRA_GOTO, target);
373 } else if (matches(*argv, "nop") == 0)
374 type = FR_ACT_NOP;
375 else if (rtnl_rtntype_a2n(&type, *argv))
376 invarg("Failed to parse rule type", *argv);
377 req.r.rtm_type = type;
378 table_ok = 1;
379 }
380 argc--;
381 argv++;
382 }
383
384 if (req.r.rtm_family == AF_UNSPEC)
385 req.r.rtm_family = AF_INET;
386
387 if (!table_ok && cmd == RTM_NEWRULE)
388 req.r.rtm_table = RT_TABLE_MAIN;
389
390 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
391 return 2;
392
393 return 0;
394 }
395
396
397 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
398 {
399 struct rtnl_handle rth2;
400 struct rtmsg *r = NLMSG_DATA(n);
401 int len = n->nlmsg_len;
402 struct rtattr * tb[FRA_MAX+1];
403
404 len -= NLMSG_LENGTH(sizeof(*r));
405 if (len < 0)
406 return -1;
407
408 parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
409
410 if (tb[FRA_PRIORITY]) {
411 n->nlmsg_type = RTM_DELRULE;
412 n->nlmsg_flags = NLM_F_REQUEST;
413
414 if (rtnl_open(&rth2, 0) < 0)
415 return -1;
416
417 if (rtnl_talk(&rth2, n, 0, 0, NULL) < 0)
418 return -2;
419
420 rtnl_close(&rth2);
421 }
422
423 return 0;
424 }
425
426 static int iprule_flush(int argc, char **argv)
427 {
428 int af = preferred_family;
429
430 if (af == AF_UNSPEC)
431 af = AF_INET;
432
433 if (argc > 0) {
434 fprintf(stderr, "\"ip rule flush\" does not allow arguments\n");
435 return -1;
436 }
437
438 if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
439 perror("Cannot send dump request");
440 return 1;
441 }
442
443 if (rtnl_dump_filter(&rth, flush_rule, NULL) < 0) {
444 fprintf(stderr, "Flush terminated\n");
445 return 1;
446 }
447
448 return 0;
449 }
450
451 int do_iprule(int argc, char **argv)
452 {
453 if (argc < 1) {
454 return iprule_list(0, NULL);
455 } else if (matches(argv[0], "list") == 0 ||
456 matches(argv[0], "lst") == 0 ||
457 matches(argv[0], "show") == 0) {
458 return iprule_list(argc-1, argv+1);
459 } else if (matches(argv[0], "add") == 0) {
460 return iprule_modify(RTM_NEWRULE, argc-1, argv+1);
461 } else if (matches(argv[0], "delete") == 0) {
462 return iprule_modify(RTM_DELRULE, argc-1, argv+1);
463 } else if (matches(argv[0], "flush") == 0) {
464 return iprule_flush(argc-1, argv+1);
465 } else if (matches(argv[0], "help") == 0)
466 usage();
467
468 fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);
469 exit(-1);
470 }
471
472 int do_multirule(int argc, char **argv)
473 {
474 switch (preferred_family) {
475 case AF_UNSPEC:
476 case AF_INET:
477 preferred_family = RTNL_FAMILY_IPMR;
478 break;
479 case AF_INET6:
480 preferred_family = RTNL_FAMILY_IP6MR;
481 break;
482 case RTNL_FAMILY_IPMR:
483 case RTNL_FAMILY_IP6MR:
484 break;
485 default:
486 fprintf(stderr, "Multicast rules are only supported for IPv4/IPv6, was: %i\n",
487 preferred_family);
488 exit(-1);
489 }
490
491 return do_iprule(argc, argv);
492 }