]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
Merge branch 'master' into iproute2-next
[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 <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netinet/ip.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <linux/if.h>
23 #include <linux/fib_rules.h>
24 #include <errno.h>
25
26 #include "rt_names.h"
27 #include "utils.h"
28 #include "ip_common.h"
29 #include "json_print.h"
30
31 enum list_action {
32 IPRULE_LIST,
33 IPRULE_FLUSH,
34 IPRULE_SAVE,
35 };
36
37 extern struct rtnl_handle rth;
38
39 static void usage(void) __attribute__((noreturn));
40
41 static void usage(void)
42 {
43 fprintf(stderr,
44 "Usage: ip rule { add | del } SELECTOR ACTION\n"
45 " ip rule { flush | save | restore }\n"
46 " ip rule [ list [ SELECTOR ]]\n"
47 "SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n"
48 " [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]\n"
49 " [ uidrange NUMBER-NUMBER ]\n"
50 " [ ipproto PROTOCOL ]\n"
51 " [ sport [ NUMBER | NUMBER-NUMBER ]\n"
52 " [ dport [ NUMBER | NUMBER-NUMBER ] ]\n"
53 "ACTION := [ table TABLE_ID ]\n"
54 " [ protocol PROTO ]\n"
55 " [ nat ADDRESS ]\n"
56 " [ realms [SRCREALM/]DSTREALM ]\n"
57 " [ goto NUMBER ]\n"
58 " SUPPRESSOR\n"
59 "SUPPRESSOR := [ suppress_prefixlength NUMBER ]\n"
60 " [ suppress_ifgroup DEVGROUP ]\n"
61 "TABLE_ID := [ local | main | default | NUMBER ]\n");
62 exit(-1);
63 }
64
65 static struct
66 {
67 int not;
68 int l3mdev;
69 int iifmask, oifmask, uidrange;
70 unsigned int tb;
71 unsigned int tos, tosmask;
72 unsigned int pref, prefmask;
73 unsigned int fwmark, fwmask;
74 char iif[IFNAMSIZ];
75 char oif[IFNAMSIZ];
76 struct fib_rule_uid_range range;
77 inet_prefix src;
78 inet_prefix dst;
79 int protocol;
80 int protocolmask;
81 } filter;
82
83 static inline int frh_get_table(struct fib_rule_hdr *frh, struct rtattr **tb)
84 {
85 __u32 table = frh->table;
86 if (tb[RTA_TABLE])
87 table = rta_getattr_u32(tb[RTA_TABLE]);
88 return table;
89 }
90
91 static bool filter_nlmsg(struct nlmsghdr *n, struct rtattr **tb, int host_len)
92 {
93 struct fib_rule_hdr *frh = NLMSG_DATA(n);
94 __u32 table;
95
96 if (preferred_family != AF_UNSPEC && frh->family != preferred_family)
97 return false;
98
99 if (filter.prefmask &&
100 filter.pref ^ (tb[FRA_PRIORITY] ? rta_getattr_u32(tb[FRA_PRIORITY]) : 0))
101 return false;
102 if (filter.not && !(frh->flags & FIB_RULE_INVERT))
103 return false;
104
105 if (filter.src.family) {
106 inet_prefix *f_src = &filter.src;
107
108 if (f_src->family != frh->family ||
109 f_src->bitlen > frh->src_len)
110 return false;
111
112 if (inet_addr_match_rta(f_src, tb[FRA_SRC]))
113 return false;
114 }
115
116 if (filter.dst.family) {
117 inet_prefix *f_dst = &filter.dst;
118
119 if (f_dst->family != frh->family ||
120 f_dst->bitlen > frh->dst_len)
121 return false;
122
123 if (inet_addr_match_rta(f_dst, tb[FRA_DST]))
124 return false;
125 }
126
127 if (filter.tosmask && filter.tos ^ frh->tos)
128 return false;
129
130 if (filter.fwmark) {
131 __u32 mark = 0;
132
133 if (tb[FRA_FWMARK])
134 mark = rta_getattr_u32(tb[FRA_FWMARK]);
135 if (filter.fwmark ^ mark)
136 return false;
137 }
138 if (filter.fwmask) {
139 __u32 mask = 0;
140
141 if (tb[FRA_FWMASK])
142 mask = rta_getattr_u32(tb[FRA_FWMASK]);
143 if (filter.fwmask ^ mask)
144 return false;
145 }
146
147 if (filter.iifmask) {
148 if (tb[FRA_IFNAME]) {
149 if (strcmp(filter.iif, rta_getattr_str(tb[FRA_IFNAME])) != 0)
150 return false;
151 } else {
152 return false;
153 }
154 }
155
156 if (filter.oifmask) {
157 if (tb[FRA_OIFNAME]) {
158 if (strcmp(filter.oif, rta_getattr_str(tb[FRA_OIFNAME])) != 0)
159 return false;
160 } else {
161 return false;
162 }
163 }
164
165 if (filter.l3mdev && !(tb[FRA_L3MDEV] && rta_getattr_u8(tb[FRA_L3MDEV])))
166 return false;
167
168 if (filter.uidrange) {
169 struct fib_rule_uid_range *r = RTA_DATA(tb[FRA_UID_RANGE]);
170
171 if (!tb[FRA_UID_RANGE] ||
172 r->start != filter.range.start ||
173 r->end != filter.range.end)
174 return false;
175 }
176
177 table = frh_get_table(frh, tb);
178 if (filter.tb > 0 && filter.tb ^ table)
179 return false;
180
181 return true;
182 }
183
184 int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
185 {
186 FILE *fp = arg;
187 struct fib_rule_hdr *frh = NLMSG_DATA(n);
188 int len = n->nlmsg_len;
189 int host_len = -1;
190 __u32 table, prio = 0;
191 struct rtattr *tb[FRA_MAX+1];
192 SPRINT_BUF(b1);
193
194 if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
195 return 0;
196
197 len -= NLMSG_LENGTH(sizeof(*frh));
198 if (len < 0)
199 return -1;
200
201 parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
202
203 host_len = af_bit_len(frh->family);
204
205 if (!filter_nlmsg(n, tb, host_len))
206 return 0;
207
208 open_json_object(NULL);
209 if (n->nlmsg_type == RTM_DELRULE)
210 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
211
212 if (tb[FRA_PRIORITY])
213 prio = rta_getattr_u32(tb[FRA_PRIORITY]);
214
215 print_uint(PRINT_ANY, "priority", "%u:\t", prio);
216
217 if (frh->flags & FIB_RULE_INVERT)
218 print_null(PRINT_ANY, "not", "not ", NULL);
219
220 if (tb[FRA_SRC]) {
221 const char *src = rt_addr_n2a_rta(frh->family, tb[FRA_SRC]);
222
223 print_string(PRINT_FP, NULL, "from ", NULL);
224 print_color_string(PRINT_ANY, ifa_family_color(frh->family),
225 "src", "%s", src);
226 if (frh->src_len != host_len)
227 print_uint(PRINT_ANY, "srclen", "/%u ", frh->src_len);
228 else
229 print_string(PRINT_FP, NULL, " ", NULL);
230 } else if (frh->src_len) {
231 print_string(PRINT_ANY, "src", "from %s", "0");
232 print_uint(PRINT_ANY, "srclen", "/%u ", frh->src_len);
233 } else {
234 print_string(PRINT_ANY, "src", "from %s ", "all");
235 }
236
237 if (tb[FRA_DST]) {
238 const char *dst = rt_addr_n2a_rta(frh->family, tb[FRA_DST]);
239
240 print_string(PRINT_FP, NULL, "to ", NULL);
241 print_color_string(PRINT_ANY, ifa_family_color(frh->family),
242 "dst", "%s", dst);
243 if (frh->dst_len != host_len)
244 print_uint(PRINT_ANY, "dstlen", "/%u ", frh->dst_len);
245 else
246 print_string(PRINT_FP, NULL, " ", NULL);
247 } else if (frh->dst_len) {
248 print_string(PRINT_ANY, "dst", "to %s", "0");
249 print_uint(PRINT_ANY, "dstlen", "/%u ", frh->dst_len);
250 }
251
252 if (frh->tos) {
253 print_string(PRINT_ANY, "tos",
254 "tos %s ",
255 rtnl_dsfield_n2a(frh->tos, b1, sizeof(b1)));
256 }
257
258 if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
259 __u32 mark = 0, mask = 0;
260
261 if (tb[FRA_FWMARK])
262 mark = rta_getattr_u32(tb[FRA_FWMARK]);
263
264 if (tb[FRA_FWMASK] &&
265 (mask = rta_getattr_u32(tb[FRA_FWMASK])) != 0xFFFFFFFF) {
266 print_0xhex(PRINT_ANY, "fwmark", "fwmark 0x%x", mark);
267 print_0xhex(PRINT_ANY, "fwmask", "/0x%x ", mask);
268 } else {
269 print_0xhex(PRINT_ANY, "fwmark", "fwmark 0x%x ", mark);
270 }
271 }
272
273 if (tb[FRA_IFNAME]) {
274 if (!is_json_context())
275 fprintf(fp, "iif ");
276 print_color_string(PRINT_ANY, COLOR_IFNAME,
277 "iif", "%s ",
278 rta_getattr_str(tb[FRA_IFNAME]));
279
280 if (frh->flags & FIB_RULE_IIF_DETACHED)
281 print_null(PRINT_ANY, "iif_detached", "[detached] ",
282 NULL);
283 }
284
285 if (tb[FRA_OIFNAME]) {
286 if (!is_json_context())
287 fprintf(fp, "oif ");
288
289 print_color_string(PRINT_ANY, COLOR_IFNAME, "oif", "%s ",
290 rta_getattr_str(tb[FRA_OIFNAME]));
291
292 if (frh->flags & FIB_RULE_OIF_DETACHED)
293 print_null(PRINT_ANY, "oif_detached", "[detached] ",
294 NULL);
295 }
296
297 if (tb[FRA_L3MDEV]) {
298 __u8 mdev = rta_getattr_u8(tb[FRA_L3MDEV]);
299
300 if (mdev)
301 print_null(PRINT_ANY, "l3mdev",
302 "lookup [l3mdev-table] ", NULL);
303 }
304
305 if (tb[FRA_UID_RANGE]) {
306 struct fib_rule_uid_range *r = RTA_DATA(tb[FRA_UID_RANGE]);
307
308 print_uint(PRINT_ANY, "uid_start", "uidrange %u", r->start);
309 print_uint(PRINT_ANY, "uid_end", "-%u ", r->end);
310 }
311
312 if (tb[FRA_IP_PROTO]) {
313 SPRINT_BUF(pbuf);
314 print_string(PRINT_ANY, "ipproto", "ipproto %s ",
315 inet_proto_n2a(rta_getattr_u8(tb[FRA_IP_PROTO]),
316 pbuf, sizeof(pbuf)));
317 }
318
319 if (tb[FRA_SPORT_RANGE]) {
320 struct fib_rule_port_range *r = RTA_DATA(tb[FRA_SPORT_RANGE]);
321
322 if (r->start == r->end) {
323 print_uint(PRINT_ANY, "sport", "sport %u ", r->start);
324 } else {
325 print_uint(PRINT_ANY, "sport_start", "sport %u",
326 r->start);
327 print_uint(PRINT_ANY, "sport_end", "-%u ", r->end);
328 }
329 }
330
331 if (tb[FRA_DPORT_RANGE]) {
332 struct fib_rule_port_range *r = RTA_DATA(tb[FRA_DPORT_RANGE]);
333
334 if (r->start == r->end) {
335 print_uint(PRINT_ANY, "dport", "dport %u ", r->start);
336 } else {
337 print_uint(PRINT_ANY, "dport_start", "dport %u",
338 r->start);
339 print_uint(PRINT_ANY, "dport_end", "-%u ", r->end);
340 }
341 }
342
343 table = frh_get_table(frh, tb);
344 if (table) {
345 print_string(PRINT_ANY, "table",
346 "lookup %s ",
347 rtnl_rttable_n2a(table, b1, sizeof(b1)));
348
349 if (tb[FRA_SUPPRESS_PREFIXLEN]) {
350 int pl = rta_getattr_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
351
352 if (pl != -1)
353 print_int(PRINT_ANY, "suppress_prefixlen",
354 "suppress_prefixlength %d ", pl);
355 }
356
357 if (tb[FRA_SUPPRESS_IFGROUP]) {
358 int group = rta_getattr_u32(tb[FRA_SUPPRESS_IFGROUP]);
359
360 if (group != -1) {
361 const char *grname
362 = rtnl_group_n2a(group, b1, sizeof(b1));
363
364 print_string(PRINT_ANY, "suppress_ifgroup",
365 "suppress_ifgroup %s ", grname);
366 }
367 }
368 }
369
370 if (tb[FRA_FLOW]) {
371 __u32 to = rta_getattr_u32(tb[FRA_FLOW]);
372 __u32 from = to>>16;
373
374 to &= 0xFFFF;
375 if (from)
376 print_string(PRINT_ANY,
377 "flow_from", "realms %s/",
378 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
379
380 print_string(PRINT_ANY, "flow_to", "%s ",
381 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
382 }
383
384 if (frh->action == RTN_NAT) {
385 if (tb[RTA_GATEWAY]) {
386 const char *gateway;
387
388 gateway = format_host_rta(frh->family, tb[RTA_GATEWAY]);
389
390 print_string(PRINT_ANY, "nat_gateway",
391 "map-to %s ", gateway);
392 } else {
393 print_null(PRINT_ANY, "masquerade", "masquerade", NULL);
394 }
395 } else if (frh->action == FR_ACT_GOTO) {
396 if (tb[FRA_GOTO])
397 print_uint(PRINT_ANY, "goto", "goto %u",
398 rta_getattr_u32(tb[FRA_GOTO]));
399 else
400 print_string(PRINT_ANY, "goto", "goto %s", "none");
401
402 if (frh->flags & FIB_RULE_UNRESOLVED)
403 print_null(PRINT_ANY, "unresolved", "unresolved", NULL);
404 } else if (frh->action == FR_ACT_NOP) {
405 print_null(PRINT_ANY, "nop", "nop", NULL);
406 } else if (frh->action != FR_ACT_TO_TBL) {
407 print_string(PRINT_ANY, "to_tbl", "%s",
408 rtnl_rtntype_n2a(frh->action, b1, sizeof(b1)));
409 }
410
411 if (tb[FRA_PROTOCOL]) {
412 __u8 protocol = rta_getattr_u8(tb[FRA_PROTOCOL]);
413
414 if ((protocol && protocol != RTPROT_KERNEL) || show_details > 0) {
415 print_string(PRINT_ANY, "protocol", " proto %s ",
416 rtnl_rtprot_n2a(protocol, b1, sizeof(b1)));
417 }
418 }
419 print_string(PRINT_FP, NULL, "\n", "");
420 close_json_object();
421 fflush(fp);
422 return 0;
423 }
424
425 static __u32 rule_dump_magic = 0x71706986;
426
427 static int save_rule_prep(void)
428 {
429 int ret;
430
431 if (isatty(STDOUT_FILENO)) {
432 fprintf(stderr, "Not sending a binary stream to stdout\n");
433 return -1;
434 }
435
436 ret = write(STDOUT_FILENO, &rule_dump_magic, sizeof(rule_dump_magic));
437 if (ret != sizeof(rule_dump_magic)) {
438 fprintf(stderr, "Can't write magic to dump file\n");
439 return -1;
440 }
441
442 return 0;
443 }
444
445 static int save_rule(const struct sockaddr_nl *who,
446 struct nlmsghdr *n, void *arg)
447 {
448 int ret;
449
450 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
451 if ((ret > 0) && (ret != n->nlmsg_len)) {
452 fprintf(stderr, "Short write while saving nlmsg\n");
453 ret = -EIO;
454 }
455
456 return ret == n->nlmsg_len ? 0 : ret;
457 }
458
459 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n,
460 void *arg)
461 {
462 struct rtnl_handle rth2;
463 struct fib_rule_hdr *frh = NLMSG_DATA(n);
464 int len = n->nlmsg_len;
465 struct rtattr *tb[FRA_MAX+1];
466
467 len -= NLMSG_LENGTH(sizeof(*frh));
468 if (len < 0)
469 return -1;
470
471 parse_rtattr(tb, FRA_MAX, RTM_RTA(frh), len);
472
473 if (tb[FRA_PROTOCOL]) {
474 __u8 protocol = rta_getattr_u8(tb[FRA_PROTOCOL]);
475
476 if ((filter.protocol ^ protocol) & filter.protocolmask)
477 return 0;
478 }
479
480 if (tb[FRA_PRIORITY]) {
481 n->nlmsg_type = RTM_DELRULE;
482 n->nlmsg_flags = NLM_F_REQUEST;
483
484 if (rtnl_open(&rth2, 0) < 0)
485 return -1;
486
487 if (rtnl_talk(&rth2, n, NULL) < 0)
488 return -2;
489
490 rtnl_close(&rth2);
491 }
492
493 return 0;
494 }
495
496 static int iprule_list_flush_or_save(int argc, char **argv, int action)
497 {
498 rtnl_filter_t filter_fn;
499 int af = preferred_family;
500
501 if (af == AF_UNSPEC)
502 af = AF_INET;
503
504 if (action == IPRULE_SAVE && argc > 0) {
505 fprintf(stderr, "\"ip rule save\" does not take any arguments.\n");
506 return -1;
507 }
508
509 switch (action) {
510 case IPRULE_SAVE:
511 if (save_rule_prep())
512 return -1;
513 filter_fn = save_rule;
514 break;
515 case IPRULE_FLUSH:
516 filter_fn = flush_rule;
517 break;
518 default:
519 filter_fn = print_rule;
520 }
521
522 memset(&filter, 0, sizeof(filter));
523
524 while (argc > 0) {
525 if (matches(*argv, "preference") == 0 ||
526 matches(*argv, "order") == 0 ||
527 matches(*argv, "priority") == 0) {
528 __u32 pref;
529
530 NEXT_ARG();
531 if (get_u32(&pref, *argv, 0))
532 invarg("preference value is invalid\n", *argv);
533 filter.pref = pref;
534 filter.prefmask = 1;
535 } else if (strcmp(*argv, "not") == 0) {
536 filter.not = 1;
537 } else if (strcmp(*argv, "tos") == 0) {
538 __u32 tos;
539
540 NEXT_ARG();
541 if (rtnl_dsfield_a2n(&tos, *argv))
542 invarg("TOS value is invalid\n", *argv);
543 filter.tos = tos;
544 filter.tosmask = 1;
545 } else if (strcmp(*argv, "fwmark") == 0) {
546 char *slash;
547 __u32 fwmark, fwmask;
548
549 NEXT_ARG();
550 slash = strchr(*argv, '/');
551 if (slash != NULL)
552 *slash = '\0';
553 if (get_u32(&fwmark, *argv, 0))
554 invarg("fwmark value is invalid\n", *argv);
555 filter.fwmark = fwmark;
556 if (slash) {
557 if (get_u32(&fwmask, slash+1, 0))
558 invarg("fwmask value is invalid\n",
559 slash+1);
560 filter.fwmask = fwmask;
561 }
562 } else if (strcmp(*argv, "dev") == 0 ||
563 strcmp(*argv, "iif") == 0) {
564 NEXT_ARG();
565 if (get_ifname(filter.iif, *argv))
566 invarg("\"iif\"/\"dev\" not a valid ifname", *argv);
567 filter.iifmask = 1;
568 } else if (strcmp(*argv, "oif") == 0) {
569 NEXT_ARG();
570 if (get_ifname(filter.oif, *argv))
571 invarg("\"oif\" not a valid ifname", *argv);
572 filter.oifmask = 1;
573 } else if (strcmp(*argv, "l3mdev") == 0) {
574 filter.l3mdev = 1;
575 } else if (strcmp(*argv, "uidrange") == 0) {
576 NEXT_ARG();
577 filter.uidrange = 1;
578 if (sscanf(*argv, "%u-%u",
579 &filter.range.start,
580 &filter.range.end) != 2)
581 invarg("invalid UID range\n", *argv);
582
583 } else if (matches(*argv, "lookup") == 0 ||
584 matches(*argv, "table") == 0) {
585 __u32 tid;
586
587 NEXT_ARG();
588 if (rtnl_rttable_a2n(&tid, *argv))
589 invarg("table id value is invalid\n", *argv);
590 filter.tb = tid;
591 } else if (matches(*argv, "from") == 0 ||
592 matches(*argv, "src") == 0) {
593 NEXT_ARG();
594 if (get_prefix(&filter.src, *argv, af))
595 invarg("from value is invalid\n", *argv);
596 } else if (matches(*argv, "protocol") == 0) {
597 __u32 prot;
598 NEXT_ARG();
599 filter.protocolmask = -1;
600 if (rtnl_rtprot_a2n(&prot, *argv)) {
601 if (strcmp(*argv, "all") != 0)
602 invarg("invalid \"protocol\"\n", *argv);
603 prot = 0;
604 filter.protocolmask = 0;
605 }
606 filter.protocol = prot;
607 } else{
608 if (matches(*argv, "dst") == 0 ||
609 matches(*argv, "to") == 0) {
610 NEXT_ARG();
611 }
612 if (get_prefix(&filter.dst, *argv, af))
613 invarg("to value is invalid\n", *argv);
614 }
615 argc--; argv++;
616 }
617
618 if (rtnl_ruledump_req(&rth, af) < 0) {
619 perror("Cannot send dump request");
620 return 1;
621 }
622
623 new_json_obj(json);
624 if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
625 fprintf(stderr, "Dump terminated\n");
626 return 1;
627 }
628 delete_json_obj();
629
630 return 0;
631 }
632
633 static int rule_dump_check_magic(void)
634 {
635 int ret;
636 __u32 magic = 0;
637
638 if (isatty(STDIN_FILENO)) {
639 fprintf(stderr, "Can't restore rule dump from a terminal\n");
640 return -1;
641 }
642
643 ret = fread(&magic, sizeof(magic), 1, stdin);
644 if (magic != rule_dump_magic) {
645 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n",
646 ret, magic);
647 return -1;
648 }
649
650 return 0;
651 }
652
653 static int restore_handler(const struct sockaddr_nl *nl,
654 struct rtnl_ctrl_data *ctrl,
655 struct nlmsghdr *n, void *arg)
656 {
657 int ret;
658
659 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
660
661 ll_init_map(&rth);
662
663 ret = rtnl_talk(&rth, n, NULL);
664 if ((ret < 0) && (errno == EEXIST))
665 ret = 0;
666
667 return ret;
668 }
669
670
671 static int iprule_restore(void)
672 {
673 if (rule_dump_check_magic())
674 exit(-1);
675
676 exit(rtnl_from_file(stdin, &restore_handler, NULL));
677 }
678
679 static int iprule_modify(int cmd, int argc, char **argv)
680 {
681 int l3mdev_rule = 0;
682 int table_ok = 0;
683 __u32 tid = 0;
684 struct {
685 struct nlmsghdr n;
686 struct fib_rule_hdr frh;
687 char buf[1024];
688 } req = {
689 .n.nlmsg_type = cmd,
690 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
691 .n.nlmsg_flags = NLM_F_REQUEST,
692 .frh.family = preferred_family,
693 .frh.action = FR_ACT_UNSPEC,
694 };
695
696 if (cmd == RTM_NEWRULE) {
697 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
698 req.frh.action = FR_ACT_TO_TBL;
699 }
700
701 if (cmd == RTM_DELRULE && argc == 0) {
702 fprintf(stderr, "\"ip rule del\" requires arguments.\n");
703 return -1;
704 }
705
706 while (argc > 0) {
707 if (strcmp(*argv, "not") == 0) {
708 req.frh.flags |= FIB_RULE_INVERT;
709 } else if (strcmp(*argv, "from") == 0) {
710 inet_prefix dst;
711
712 NEXT_ARG();
713 get_prefix(&dst, *argv, req.frh.family);
714 req.frh.src_len = dst.bitlen;
715 addattr_l(&req.n, sizeof(req), FRA_SRC,
716 &dst.data, dst.bytelen);
717 } else if (strcmp(*argv, "to") == 0) {
718 inet_prefix dst;
719
720 NEXT_ARG();
721 get_prefix(&dst, *argv, req.frh.family);
722 req.frh.dst_len = dst.bitlen;
723 addattr_l(&req.n, sizeof(req), FRA_DST,
724 &dst.data, dst.bytelen);
725 } else if (matches(*argv, "preference") == 0 ||
726 matches(*argv, "order") == 0 ||
727 matches(*argv, "priority") == 0) {
728 __u32 pref;
729
730 NEXT_ARG();
731 if (get_u32(&pref, *argv, 0))
732 invarg("preference value is invalid\n", *argv);
733 addattr32(&req.n, sizeof(req), FRA_PRIORITY, pref);
734 } else if (strcmp(*argv, "tos") == 0 ||
735 matches(*argv, "dsfield") == 0) {
736 __u32 tos;
737
738 NEXT_ARG();
739 if (rtnl_dsfield_a2n(&tos, *argv))
740 invarg("TOS value is invalid\n", *argv);
741 req.frh.tos = tos;
742 } else if (strcmp(*argv, "fwmark") == 0) {
743 char *slash;
744 __u32 fwmark, fwmask;
745
746 NEXT_ARG();
747
748 slash = strchr(*argv, '/');
749 if (slash != NULL)
750 *slash = '\0';
751 if (get_u32(&fwmark, *argv, 0))
752 invarg("fwmark value is invalid\n", *argv);
753 addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
754 if (slash) {
755 if (get_u32(&fwmask, slash+1, 0))
756 invarg("fwmask value is invalid\n",
757 slash+1);
758 addattr32(&req.n, sizeof(req),
759 FRA_FWMASK, fwmask);
760 }
761 } else if (matches(*argv, "realms") == 0) {
762 __u32 realm;
763
764 NEXT_ARG();
765 if (get_rt_realms_or_raw(&realm, *argv))
766 invarg("invalid realms\n", *argv);
767 addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
768 } else if (matches(*argv, "protocol") == 0) {
769 __u32 proto;
770
771 NEXT_ARG();
772 if (rtnl_rtprot_a2n(&proto, *argv))
773 invarg("\"protocol\" value is invalid\n", *argv);
774 addattr8(&req.n, sizeof(req), FRA_PROTOCOL, proto);
775 } else if (matches(*argv, "table") == 0 ||
776 strcmp(*argv, "lookup") == 0) {
777 NEXT_ARG();
778 if (rtnl_rttable_a2n(&tid, *argv))
779 invarg("invalid table ID\n", *argv);
780 if (tid < 256)
781 req.frh.table = tid;
782 else {
783 req.frh.table = RT_TABLE_UNSPEC;
784 addattr32(&req.n, sizeof(req), FRA_TABLE, tid);
785 }
786 table_ok = 1;
787 } else if (matches(*argv, "suppress_prefixlength") == 0 ||
788 strcmp(*argv, "sup_pl") == 0) {
789 int pl;
790
791 NEXT_ARG();
792 if (get_s32(&pl, *argv, 0) || pl < 0)
793 invarg("suppress_prefixlength value is invalid\n",
794 *argv);
795 addattr32(&req.n, sizeof(req),
796 FRA_SUPPRESS_PREFIXLEN, pl);
797 } else if (matches(*argv, "suppress_ifgroup") == 0 ||
798 strcmp(*argv, "sup_group") == 0) {
799 NEXT_ARG();
800 int group;
801
802 if (rtnl_group_a2n(&group, *argv))
803 invarg("Invalid \"suppress_ifgroup\" value\n",
804 *argv);
805 addattr32(&req.n, sizeof(req),
806 FRA_SUPPRESS_IFGROUP, group);
807 } else if (strcmp(*argv, "dev") == 0 ||
808 strcmp(*argv, "iif") == 0) {
809 NEXT_ARG();
810 if (check_ifname(*argv))
811 invarg("\"iif\"/\"dev\" not a valid ifname", *argv);
812 addattr_l(&req.n, sizeof(req), FRA_IFNAME,
813 *argv, strlen(*argv)+1);
814 } else if (strcmp(*argv, "oif") == 0) {
815 NEXT_ARG();
816 if (check_ifname(*argv))
817 invarg("\"oif\" not a valid ifname", *argv);
818 addattr_l(&req.n, sizeof(req), FRA_OIFNAME,
819 *argv, strlen(*argv)+1);
820 } else if (strcmp(*argv, "l3mdev") == 0) {
821 addattr8(&req.n, sizeof(req), FRA_L3MDEV, 1);
822 table_ok = 1;
823 l3mdev_rule = 1;
824 } else if (strcmp(*argv, "uidrange") == 0) {
825 struct fib_rule_uid_range r;
826
827 NEXT_ARG();
828 if (sscanf(*argv, "%u-%u", &r.start, &r.end) != 2)
829 invarg("invalid UID range\n", *argv);
830 addattr_l(&req.n, sizeof(req), FRA_UID_RANGE, &r,
831 sizeof(r));
832 } else if (strcmp(*argv, "nat") == 0 ||
833 matches(*argv, "map-to") == 0) {
834 NEXT_ARG();
835 fprintf(stderr, "Warning: route NAT is deprecated\n");
836 addattr32(&req.n, sizeof(req), RTA_GATEWAY,
837 get_addr32(*argv));
838 req.frh.action = RTN_NAT;
839 } else if (strcmp(*argv, "ipproto") == 0) {
840 int ipproto;
841
842 NEXT_ARG();
843 ipproto = inet_proto_a2n(*argv);
844 if (ipproto < 0)
845 invarg("Invalid \"ipproto\" value\n",
846 *argv);
847 addattr8(&req.n, sizeof(req), FRA_IP_PROTO, ipproto);
848 } else if (strcmp(*argv, "sport") == 0) {
849 struct fib_rule_port_range r;
850 int ret = 0;
851
852 NEXT_ARG();
853 ret = sscanf(*argv, "%hu-%hu", &r.start, &r.end);
854 if (ret == 1)
855 r.end = r.start;
856 else if (ret != 2)
857 invarg("invalid port range\n", *argv);
858 addattr_l(&req.n, sizeof(req), FRA_SPORT_RANGE, &r,
859 sizeof(r));
860 } else if (strcmp(*argv, "dport") == 0) {
861 struct fib_rule_port_range r;
862 int ret = 0;
863
864 NEXT_ARG();
865 ret = sscanf(*argv, "%hu-%hu", &r.start, &r.end);
866 if (ret == 1)
867 r.end = r.start;
868 else if (ret != 2)
869 invarg("invalid dport range\n", *argv);
870 addattr_l(&req.n, sizeof(req), FRA_DPORT_RANGE, &r,
871 sizeof(r));
872 } else {
873 int type;
874
875 if (strcmp(*argv, "type") == 0)
876 NEXT_ARG();
877
878 if (matches(*argv, "help") == 0)
879 usage();
880 else if (matches(*argv, "goto") == 0) {
881 __u32 target;
882
883 type = FR_ACT_GOTO;
884 NEXT_ARG();
885 if (get_u32(&target, *argv, 0))
886 invarg("invalid target\n", *argv);
887 addattr32(&req.n, sizeof(req),
888 FRA_GOTO, target);
889 } else if (matches(*argv, "nop") == 0)
890 type = FR_ACT_NOP;
891 else if (rtnl_rtntype_a2n(&type, *argv))
892 invarg("Failed to parse rule type", *argv);
893 req.frh.action = type;
894 table_ok = 1;
895 }
896 argc--;
897 argv++;
898 }
899
900 if (l3mdev_rule && tid != 0) {
901 fprintf(stderr,
902 "table can not be specified for l3mdev rules\n");
903 return -EINVAL;
904 }
905
906 if (req.frh.family == AF_UNSPEC)
907 req.frh.family = AF_INET;
908
909 if (!table_ok && cmd == RTM_NEWRULE)
910 req.frh.table = RT_TABLE_MAIN;
911
912 if (rtnl_talk(&rth, &req.n, NULL) < 0)
913 return -2;
914
915 return 0;
916 }
917
918 int do_iprule(int argc, char **argv)
919 {
920 if (argc < 1) {
921 return iprule_list_flush_or_save(0, NULL, IPRULE_LIST);
922 } else if (matches(argv[0], "list") == 0 ||
923 matches(argv[0], "lst") == 0 ||
924 matches(argv[0], "show") == 0) {
925 return iprule_list_flush_or_save(argc-1, argv+1, IPRULE_LIST);
926 } else if (matches(argv[0], "save") == 0) {
927 return iprule_list_flush_or_save(argc-1, argv+1, IPRULE_SAVE);
928 } else if (matches(argv[0], "restore") == 0) {
929 return iprule_restore();
930 } else if (matches(argv[0], "add") == 0) {
931 return iprule_modify(RTM_NEWRULE, argc-1, argv+1);
932 } else if (matches(argv[0], "delete") == 0) {
933 return iprule_modify(RTM_DELRULE, argc-1, argv+1);
934 } else if (matches(argv[0], "flush") == 0) {
935 return iprule_list_flush_or_save(argc-1, argv+1, IPRULE_FLUSH);
936 } else if (matches(argv[0], "help") == 0)
937 usage();
938
939 fprintf(stderr,
940 "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);
941 exit(-1);
942 }
943
944 int do_multirule(int argc, char **argv)
945 {
946 switch (preferred_family) {
947 case AF_UNSPEC:
948 case AF_INET:
949 preferred_family = RTNL_FAMILY_IPMR;
950 break;
951 case AF_INET6:
952 preferred_family = RTNL_FAMILY_IP6MR;
953 break;
954 case RTNL_FAMILY_IPMR:
955 case RTNL_FAMILY_IP6MR:
956 break;
957 default:
958 fprintf(stderr,
959 "Multicast rules are only supported for IPv4/IPv6, was: %i\n",
960 preferred_family);
961 exit(-1);
962 }
963
964 return do_iprule(argc, argv);
965 }