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