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