]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netinet/ip.h>
20 #include <arpa/inet.h>
23 #include <linux/fib_rules.h>
28 #include "ip_common.h"
29 #include "json_print.h"
37 extern struct rtnl_handle rth
;
39 static void usage(void) __attribute__((noreturn
));
41 static void usage(void)
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"
56 " [ realms [SRCREALM/]DSTREALM ]\n"
59 "SUPPRESSOR := [ suppress_prefixlength NUMBER ]\n"
60 " [ suppress_ifgroup DEVGROUP ]\n"
61 "TABLE_ID := [ local | main | default | NUMBER ]\n");
69 int iifmask
, oifmask
, uidrange
;
71 unsigned int tos
, tosmask
;
72 unsigned int pref
, prefmask
;
73 unsigned int fwmark
, fwmask
;
77 struct fib_rule_uid_range range
;
84 static inline int frh_get_table(struct fib_rule_hdr
*frh
, struct rtattr
**tb
)
86 __u32 table
= frh
->table
;
88 table
= rta_getattr_u32(tb
[RTA_TABLE
]);
92 static bool filter_nlmsg(struct nlmsghdr
*n
, struct rtattr
**tb
, int host_len
)
94 struct fib_rule_hdr
*frh
= NLMSG_DATA(n
);
97 if (preferred_family
!= AF_UNSPEC
&& frh
->family
!= preferred_family
)
100 if (filter
.prefmask
&&
101 filter
.pref
^ (tb
[FRA_PRIORITY
] ? rta_getattr_u32(tb
[FRA_PRIORITY
]) : 0))
103 if (filter
.not && !(frh
->flags
& FIB_RULE_INVERT
))
106 if (filter
.src
.family
) {
107 inet_prefix
*f_src
= &filter
.src
;
109 if (f_src
->family
!= frh
->family
||
110 f_src
->bitlen
> frh
->src_len
)
113 if (inet_addr_match_rta(f_src
, tb
[FRA_SRC
]))
117 if (filter
.dst
.family
) {
118 inet_prefix
*f_dst
= &filter
.dst
;
120 if (f_dst
->family
!= frh
->family
||
121 f_dst
->bitlen
> frh
->dst_len
)
124 if (inet_addr_match_rta(f_dst
, tb
[FRA_DST
]))
128 if (filter
.tosmask
&& filter
.tos
^ frh
->tos
)
135 mark
= rta_getattr_u32(tb
[FRA_FWMARK
]);
136 if (filter
.fwmark
^ mark
)
143 mask
= rta_getattr_u32(tb
[FRA_FWMASK
]);
144 if (filter
.fwmask
^ mask
)
148 if (filter
.iifmask
) {
149 if (tb
[FRA_IFNAME
]) {
150 if (strcmp(filter
.iif
, rta_getattr_str(tb
[FRA_IFNAME
])) != 0)
157 if (filter
.oifmask
) {
158 if (tb
[FRA_OIFNAME
]) {
159 if (strcmp(filter
.oif
, rta_getattr_str(tb
[FRA_OIFNAME
])) != 0)
166 if (filter
.l3mdev
&& !(tb
[FRA_L3MDEV
] && rta_getattr_u8(tb
[FRA_L3MDEV
])))
169 if (filter
.uidrange
) {
170 struct fib_rule_uid_range
*r
= RTA_DATA(tb
[FRA_UID_RANGE
]);
172 if (!tb
[FRA_UID_RANGE
] ||
173 r
->start
!= filter
.range
.start
||
174 r
->end
!= filter
.range
.end
)
181 if (tb
[FRA_TUN_ID
]) {
182 tun_id
= ntohll(rta_getattr_u64(tb
[FRA_TUN_ID
]));
183 if (filter
.tun_id
!= tun_id
)
190 table
= frh_get_table(frh
, tb
);
191 if (filter
.tb
> 0 && filter
.tb
^ table
)
197 int print_rule(struct nlmsghdr
*n
, void *arg
)
200 struct fib_rule_hdr
*frh
= NLMSG_DATA(n
);
201 int len
= n
->nlmsg_len
;
203 __u32 table
, prio
= 0;
204 struct rtattr
*tb
[FRA_MAX
+1];
207 if (n
->nlmsg_type
!= RTM_NEWRULE
&& n
->nlmsg_type
!= RTM_DELRULE
)
210 len
-= NLMSG_LENGTH(sizeof(*frh
));
214 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(frh
), len
);
216 host_len
= af_bit_len(frh
->family
);
218 if (!filter_nlmsg(n
, tb
, host_len
))
221 open_json_object(NULL
);
222 if (n
->nlmsg_type
== RTM_DELRULE
)
223 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
225 if (tb
[FRA_PRIORITY
])
226 prio
= rta_getattr_u32(tb
[FRA_PRIORITY
]);
228 print_uint(PRINT_ANY
, "priority", "%u:\t", prio
);
230 if (frh
->flags
& FIB_RULE_INVERT
)
231 print_null(PRINT_ANY
, "not", "not ", NULL
);
234 const char *src
= rt_addr_n2a_rta(frh
->family
, tb
[FRA_SRC
]);
236 print_string(PRINT_FP
, NULL
, "from ", NULL
);
237 print_color_string(PRINT_ANY
, ifa_family_color(frh
->family
),
239 if (frh
->src_len
!= host_len
)
240 print_uint(PRINT_ANY
, "srclen", "/%u ", frh
->src_len
);
242 print_string(PRINT_FP
, NULL
, " ", NULL
);
243 } else if (frh
->src_len
) {
244 print_string(PRINT_ANY
, "src", "from %s", "0");
245 print_uint(PRINT_ANY
, "srclen", "/%u ", frh
->src_len
);
247 print_string(PRINT_ANY
, "src", "from %s ", "all");
251 const char *dst
= rt_addr_n2a_rta(frh
->family
, tb
[FRA_DST
]);
253 print_string(PRINT_FP
, NULL
, "to ", NULL
);
254 print_color_string(PRINT_ANY
, ifa_family_color(frh
->family
),
256 if (frh
->dst_len
!= host_len
)
257 print_uint(PRINT_ANY
, "dstlen", "/%u ", frh
->dst_len
);
259 print_string(PRINT_FP
, NULL
, " ", NULL
);
260 } else if (frh
->dst_len
) {
261 print_string(PRINT_ANY
, "dst", "to %s", "0");
262 print_uint(PRINT_ANY
, "dstlen", "/%u ", frh
->dst_len
);
266 print_string(PRINT_ANY
, "tos",
268 rtnl_dsfield_n2a(frh
->tos
, b1
, sizeof(b1
)));
271 if (tb
[FRA_FWMARK
] || tb
[FRA_FWMASK
]) {
272 __u32 mark
= 0, mask
= 0;
275 mark
= rta_getattr_u32(tb
[FRA_FWMARK
]);
277 if (tb
[FRA_FWMASK
] &&
278 (mask
= rta_getattr_u32(tb
[FRA_FWMASK
])) != 0xFFFFFFFF) {
279 print_0xhex(PRINT_ANY
, "fwmark", "fwmark %#llx", mark
);
280 print_0xhex(PRINT_ANY
, "fwmask", "/%#llx ", mask
);
282 print_0xhex(PRINT_ANY
, "fwmark", "fwmark %#llx ", mark
);
286 if (tb
[FRA_IFNAME
]) {
287 if (!is_json_context())
289 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
291 rta_getattr_str(tb
[FRA_IFNAME
]));
293 if (frh
->flags
& FIB_RULE_IIF_DETACHED
)
294 print_null(PRINT_ANY
, "iif_detached", "[detached] ",
298 if (tb
[FRA_OIFNAME
]) {
299 if (!is_json_context())
302 print_color_string(PRINT_ANY
, COLOR_IFNAME
, "oif", "%s ",
303 rta_getattr_str(tb
[FRA_OIFNAME
]));
305 if (frh
->flags
& FIB_RULE_OIF_DETACHED
)
306 print_null(PRINT_ANY
, "oif_detached", "[detached] ",
310 if (tb
[FRA_L3MDEV
]) {
311 __u8 mdev
= rta_getattr_u8(tb
[FRA_L3MDEV
]);
314 print_null(PRINT_ANY
, "l3mdev",
315 "lookup [l3mdev-table] ", NULL
);
318 if (tb
[FRA_UID_RANGE
]) {
319 struct fib_rule_uid_range
*r
= RTA_DATA(tb
[FRA_UID_RANGE
]);
321 print_uint(PRINT_ANY
, "uid_start", "uidrange %u", r
->start
);
322 print_uint(PRINT_ANY
, "uid_end", "-%u ", r
->end
);
325 if (tb
[FRA_IP_PROTO
]) {
327 print_string(PRINT_ANY
, "ipproto", "ipproto %s ",
328 inet_proto_n2a(rta_getattr_u8(tb
[FRA_IP_PROTO
]),
329 pbuf
, sizeof(pbuf
)));
332 if (tb
[FRA_SPORT_RANGE
]) {
333 struct fib_rule_port_range
*r
= RTA_DATA(tb
[FRA_SPORT_RANGE
]);
335 if (r
->start
== r
->end
) {
336 print_uint(PRINT_ANY
, "sport", "sport %u ", r
->start
);
338 print_uint(PRINT_ANY
, "sport_start", "sport %u",
340 print_uint(PRINT_ANY
, "sport_end", "-%u ", r
->end
);
344 if (tb
[FRA_DPORT_RANGE
]) {
345 struct fib_rule_port_range
*r
= RTA_DATA(tb
[FRA_DPORT_RANGE
]);
347 if (r
->start
== r
->end
) {
348 print_uint(PRINT_ANY
, "dport", "dport %u ", r
->start
);
350 print_uint(PRINT_ANY
, "dport_start", "dport %u",
352 print_uint(PRINT_ANY
, "dport_end", "-%u ", r
->end
);
356 if (tb
[FRA_TUN_ID
]) {
357 __u64 tun_id
= ntohll(rta_getattr_u64(tb
[FRA_TUN_ID
]));
359 print_u64(PRINT_ANY
, "tun_id", "tun_id %llu ", tun_id
);
362 table
= frh_get_table(frh
, tb
);
364 print_string(PRINT_ANY
, "table",
366 rtnl_rttable_n2a(table
, b1
, sizeof(b1
)));
368 if (tb
[FRA_SUPPRESS_PREFIXLEN
]) {
369 int pl
= rta_getattr_u32(tb
[FRA_SUPPRESS_PREFIXLEN
]);
372 print_int(PRINT_ANY
, "suppress_prefixlen",
373 "suppress_prefixlength %d ", pl
);
376 if (tb
[FRA_SUPPRESS_IFGROUP
]) {
377 int group
= rta_getattr_u32(tb
[FRA_SUPPRESS_IFGROUP
]);
381 = rtnl_group_n2a(group
, b1
, sizeof(b1
));
383 print_string(PRINT_ANY
, "suppress_ifgroup",
384 "suppress_ifgroup %s ", grname
);
390 __u32 to
= rta_getattr_u32(tb
[FRA_FLOW
]);
395 print_string(PRINT_ANY
,
396 "flow_from", "realms %s/",
397 rtnl_rtrealm_n2a(from
, b1
, sizeof(b1
)));
399 print_string(PRINT_ANY
, "flow_to", "%s ",
400 rtnl_rtrealm_n2a(to
, b1
, sizeof(b1
)));
403 if (frh
->action
== RTN_NAT
) {
404 if (tb
[RTA_GATEWAY
]) {
407 gateway
= format_host_rta(frh
->family
, tb
[RTA_GATEWAY
]);
409 print_string(PRINT_ANY
, "nat_gateway",
410 "map-to %s ", gateway
);
412 print_null(PRINT_ANY
, "masquerade", "masquerade", NULL
);
414 } else if (frh
->action
== FR_ACT_GOTO
) {
416 print_uint(PRINT_ANY
, "goto", "goto %u",
417 rta_getattr_u32(tb
[FRA_GOTO
]));
419 print_string(PRINT_ANY
, "goto", "goto %s", "none");
421 if (frh
->flags
& FIB_RULE_UNRESOLVED
)
422 print_null(PRINT_ANY
, "unresolved", "unresolved", NULL
);
423 } else if (frh
->action
== FR_ACT_NOP
) {
424 print_null(PRINT_ANY
, "nop", "nop", NULL
);
425 } else if (frh
->action
!= FR_ACT_TO_TBL
) {
426 print_string(PRINT_ANY
, "to_tbl", "%s",
427 rtnl_rtntype_n2a(frh
->action
, b1
, sizeof(b1
)));
430 if (tb
[FRA_PROTOCOL
]) {
431 __u8 protocol
= rta_getattr_u8(tb
[FRA_PROTOCOL
]);
433 if ((protocol
&& protocol
!= RTPROT_KERNEL
) || show_details
> 0) {
434 print_string(PRINT_ANY
, "protocol", " proto %s ",
435 rtnl_rtprot_n2a(protocol
, b1
, sizeof(b1
)));
438 print_string(PRINT_FP
, NULL
, "\n", "");
444 static __u32 rule_dump_magic
= 0x71706986;
446 static int save_rule_prep(void)
450 if (isatty(STDOUT_FILENO
)) {
451 fprintf(stderr
, "Not sending a binary stream to stdout\n");
455 ret
= write(STDOUT_FILENO
, &rule_dump_magic
, sizeof(rule_dump_magic
));
456 if (ret
!= sizeof(rule_dump_magic
)) {
457 fprintf(stderr
, "Can't write magic to dump file\n");
464 static int save_rule(struct nlmsghdr
*n
, void *arg
)
468 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
469 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
470 fprintf(stderr
, "Short write while saving nlmsg\n");
474 return ret
== n
->nlmsg_len
? 0 : ret
;
477 static int flush_rule(struct nlmsghdr
*n
, void *arg
)
479 struct rtnl_handle rth2
;
480 struct fib_rule_hdr
*frh
= NLMSG_DATA(n
);
481 int len
= n
->nlmsg_len
;
482 struct rtattr
*tb
[FRA_MAX
+1];
485 len
-= NLMSG_LENGTH(sizeof(*frh
));
489 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(frh
), len
);
491 host_len
= af_bit_len(frh
->family
);
492 if (!filter_nlmsg(n
, tb
, host_len
))
495 if (tb
[FRA_PROTOCOL
]) {
496 __u8 protocol
= rta_getattr_u8(tb
[FRA_PROTOCOL
]);
498 if ((filter
.protocol
^ protocol
) & filter
.protocolmask
)
502 if (tb
[FRA_PRIORITY
]) {
503 n
->nlmsg_type
= RTM_DELRULE
;
504 n
->nlmsg_flags
= NLM_F_REQUEST
;
506 if (rtnl_open(&rth2
, 0) < 0)
509 if (rtnl_talk(&rth2
, n
, NULL
) < 0)
518 static int iprule_list_flush_or_save(int argc
, char **argv
, int action
)
520 rtnl_filter_t filter_fn
;
521 int af
= preferred_family
;
526 if (action
== IPRULE_SAVE
&& argc
> 0) {
527 fprintf(stderr
, "\"ip rule save\" does not take any arguments.\n");
533 if (save_rule_prep())
535 filter_fn
= save_rule
;
538 filter_fn
= flush_rule
;
541 filter_fn
= print_rule
;
544 memset(&filter
, 0, sizeof(filter
));
547 if (matches(*argv
, "preference") == 0 ||
548 matches(*argv
, "order") == 0 ||
549 matches(*argv
, "priority") == 0) {
553 if (get_u32(&pref
, *argv
, 0))
554 invarg("preference value is invalid\n", *argv
);
557 } else if (strcmp(*argv
, "not") == 0) {
559 } else if (strcmp(*argv
, "tos") == 0) {
563 if (rtnl_dsfield_a2n(&tos
, *argv
))
564 invarg("TOS value is invalid\n", *argv
);
567 } else if (strcmp(*argv
, "fwmark") == 0) {
569 __u32 fwmark
, fwmask
;
572 slash
= strchr(*argv
, '/');
575 if (get_u32(&fwmark
, *argv
, 0))
576 invarg("fwmark value is invalid\n", *argv
);
577 filter
.fwmark
= fwmark
;
579 if (get_u32(&fwmask
, slash
+1, 0))
580 invarg("fwmask value is invalid\n",
582 filter
.fwmask
= fwmask
;
584 } else if (strcmp(*argv
, "dev") == 0 ||
585 strcmp(*argv
, "iif") == 0) {
587 if (get_ifname(filter
.iif
, *argv
))
588 invarg("\"iif\"/\"dev\" not a valid ifname", *argv
);
590 } else if (strcmp(*argv
, "oif") == 0) {
592 if (get_ifname(filter
.oif
, *argv
))
593 invarg("\"oif\" not a valid ifname", *argv
);
595 } else if (strcmp(*argv
, "l3mdev") == 0) {
597 } else if (strcmp(*argv
, "uidrange") == 0) {
600 if (sscanf(*argv
, "%u-%u",
602 &filter
.range
.end
) != 2)
603 invarg("invalid UID range\n", *argv
);
605 } else if (matches(*argv
, "tun_id") == 0) {
609 if (get_u64(&tun_id
, *argv
, 0))
610 invarg("\"tun_id\" value is invalid\n", *argv
);
611 filter
.tun_id
= tun_id
;
612 } else if (matches(*argv
, "lookup") == 0 ||
613 matches(*argv
, "table") == 0) {
617 if (rtnl_rttable_a2n(&tid
, *argv
))
618 invarg("table id value is invalid\n", *argv
);
620 } else if (matches(*argv
, "from") == 0 ||
621 matches(*argv
, "src") == 0) {
623 if (get_prefix(&filter
.src
, *argv
, af
))
624 invarg("from value is invalid\n", *argv
);
625 } else if (matches(*argv
, "protocol") == 0) {
628 filter
.protocolmask
= -1;
629 if (rtnl_rtprot_a2n(&prot
, *argv
)) {
630 if (strcmp(*argv
, "all") != 0)
631 invarg("invalid \"protocol\"\n", *argv
);
633 filter
.protocolmask
= 0;
635 filter
.protocol
= prot
;
637 if (matches(*argv
, "dst") == 0 ||
638 matches(*argv
, "to") == 0) {
641 if (get_prefix(&filter
.dst
, *argv
, af
))
642 invarg("to value is invalid\n", *argv
);
647 if (rtnl_ruledump_req(&rth
, af
) < 0) {
648 perror("Cannot send dump request");
653 if (rtnl_dump_filter(&rth
, filter_fn
, stdout
) < 0) {
654 fprintf(stderr
, "Dump terminated\n");
662 static int rule_dump_check_magic(void)
667 if (isatty(STDIN_FILENO
)) {
668 fprintf(stderr
, "Can't restore rule dump from a terminal\n");
672 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
673 if (magic
!= rule_dump_magic
) {
674 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n",
682 static int restore_handler(struct rtnl_ctrl_data
*ctrl
,
683 struct nlmsghdr
*n
, void *arg
)
687 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
691 ret
= rtnl_talk(&rth
, n
, NULL
);
692 if ((ret
< 0) && (errno
== EEXIST
))
699 static int iprule_restore(void)
701 if (rule_dump_check_magic())
704 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
707 static int iprule_modify(int cmd
, int argc
, char **argv
)
714 struct fib_rule_hdr frh
;
718 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
719 .n
.nlmsg_flags
= NLM_F_REQUEST
,
720 .frh
.family
= preferred_family
,
721 .frh
.action
= FR_ACT_UNSPEC
,
724 if (cmd
== RTM_NEWRULE
) {
727 "\"ip rule add\" requires arguments.\n");
730 req
.n
.nlmsg_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
731 req
.frh
.action
= FR_ACT_TO_TBL
;
734 if (cmd
== RTM_DELRULE
&& argc
== 0) {
735 fprintf(stderr
, "\"ip rule del\" requires arguments.\n");
740 if (strcmp(*argv
, "not") == 0) {
741 req
.frh
.flags
|= FIB_RULE_INVERT
;
742 } else if (strcmp(*argv
, "from") == 0) {
746 get_prefix(&dst
, *argv
, req
.frh
.family
);
747 req
.frh
.src_len
= dst
.bitlen
;
748 addattr_l(&req
.n
, sizeof(req
), FRA_SRC
,
749 &dst
.data
, dst
.bytelen
);
750 } else if (strcmp(*argv
, "to") == 0) {
754 get_prefix(&dst
, *argv
, req
.frh
.family
);
755 req
.frh
.dst_len
= dst
.bitlen
;
756 addattr_l(&req
.n
, sizeof(req
), FRA_DST
,
757 &dst
.data
, dst
.bytelen
);
758 } else if (matches(*argv
, "preference") == 0 ||
759 matches(*argv
, "order") == 0 ||
760 matches(*argv
, "priority") == 0) {
764 if (get_u32(&pref
, *argv
, 0))
765 invarg("preference value is invalid\n", *argv
);
766 addattr32(&req
.n
, sizeof(req
), FRA_PRIORITY
, pref
);
767 } else if (strcmp(*argv
, "tos") == 0 ||
768 matches(*argv
, "dsfield") == 0) {
772 if (rtnl_dsfield_a2n(&tos
, *argv
))
773 invarg("TOS value is invalid\n", *argv
);
775 } else if (strcmp(*argv
, "fwmark") == 0) {
777 __u32 fwmark
, fwmask
;
781 slash
= strchr(*argv
, '/');
784 if (get_u32(&fwmark
, *argv
, 0))
785 invarg("fwmark value is invalid\n", *argv
);
786 addattr32(&req
.n
, sizeof(req
), FRA_FWMARK
, fwmark
);
788 if (get_u32(&fwmask
, slash
+1, 0))
789 invarg("fwmask value is invalid\n",
791 addattr32(&req
.n
, sizeof(req
),
794 } else if (matches(*argv
, "realms") == 0) {
798 if (get_rt_realms_or_raw(&realm
, *argv
))
799 invarg("invalid realms\n", *argv
);
800 addattr32(&req
.n
, sizeof(req
), FRA_FLOW
, realm
);
801 } else if (matches(*argv
, "protocol") == 0) {
805 if (rtnl_rtprot_a2n(&proto
, *argv
))
806 invarg("\"protocol\" value is invalid\n", *argv
);
807 addattr8(&req
.n
, sizeof(req
), FRA_PROTOCOL
, proto
);
808 } else if (matches(*argv
, "tun_id") == 0) {
812 if (get_be64(&tun_id
, *argv
, 0))
813 invarg("\"tun_id\" value is invalid\n", *argv
);
814 addattr64(&req
.n
, sizeof(req
), FRA_TUN_ID
, tun_id
);
815 } else if (matches(*argv
, "table") == 0 ||
816 strcmp(*argv
, "lookup") == 0) {
818 if (rtnl_rttable_a2n(&tid
, *argv
))
819 invarg("invalid table ID\n", *argv
);
823 req
.frh
.table
= RT_TABLE_UNSPEC
;
824 addattr32(&req
.n
, sizeof(req
), FRA_TABLE
, tid
);
827 } else if (matches(*argv
, "suppress_prefixlength") == 0 ||
828 strcmp(*argv
, "sup_pl") == 0) {
832 if (get_s32(&pl
, *argv
, 0) || pl
< 0)
833 invarg("suppress_prefixlength value is invalid\n",
835 addattr32(&req
.n
, sizeof(req
),
836 FRA_SUPPRESS_PREFIXLEN
, pl
);
837 } else if (matches(*argv
, "suppress_ifgroup") == 0 ||
838 strcmp(*argv
, "sup_group") == 0) {
842 if (rtnl_group_a2n(&group
, *argv
))
843 invarg("Invalid \"suppress_ifgroup\" value\n",
845 addattr32(&req
.n
, sizeof(req
),
846 FRA_SUPPRESS_IFGROUP
, group
);
847 } else if (strcmp(*argv
, "dev") == 0 ||
848 strcmp(*argv
, "iif") == 0) {
850 if (check_ifname(*argv
))
851 invarg("\"iif\"/\"dev\" not a valid ifname", *argv
);
852 addattr_l(&req
.n
, sizeof(req
), FRA_IFNAME
,
853 *argv
, strlen(*argv
)+1);
854 } else if (strcmp(*argv
, "oif") == 0) {
856 if (check_ifname(*argv
))
857 invarg("\"oif\" not a valid ifname", *argv
);
858 addattr_l(&req
.n
, sizeof(req
), FRA_OIFNAME
,
859 *argv
, strlen(*argv
)+1);
860 } else if (strcmp(*argv
, "l3mdev") == 0) {
861 addattr8(&req
.n
, sizeof(req
), FRA_L3MDEV
, 1);
864 } else if (strcmp(*argv
, "uidrange") == 0) {
865 struct fib_rule_uid_range r
;
868 if (sscanf(*argv
, "%u-%u", &r
.start
, &r
.end
) != 2)
869 invarg("invalid UID range\n", *argv
);
870 addattr_l(&req
.n
, sizeof(req
), FRA_UID_RANGE
, &r
,
872 } else if (strcmp(*argv
, "nat") == 0 ||
873 matches(*argv
, "map-to") == 0) {
875 fprintf(stderr
, "Warning: route NAT is deprecated\n");
876 addattr32(&req
.n
, sizeof(req
), RTA_GATEWAY
,
878 req
.frh
.action
= RTN_NAT
;
879 } else if (strcmp(*argv
, "ipproto") == 0) {
883 ipproto
= inet_proto_a2n(*argv
);
885 invarg("Invalid \"ipproto\" value\n",
887 addattr8(&req
.n
, sizeof(req
), FRA_IP_PROTO
, ipproto
);
888 } else if (strcmp(*argv
, "sport") == 0) {
889 struct fib_rule_port_range r
;
893 ret
= sscanf(*argv
, "%hu-%hu", &r
.start
, &r
.end
);
897 invarg("invalid port range\n", *argv
);
898 addattr_l(&req
.n
, sizeof(req
), FRA_SPORT_RANGE
, &r
,
900 } else if (strcmp(*argv
, "dport") == 0) {
901 struct fib_rule_port_range r
;
905 ret
= sscanf(*argv
, "%hu-%hu", &r
.start
, &r
.end
);
909 invarg("invalid dport range\n", *argv
);
910 addattr_l(&req
.n
, sizeof(req
), FRA_DPORT_RANGE
, &r
,
915 if (strcmp(*argv
, "type") == 0)
918 if (matches(*argv
, "help") == 0)
920 else if (matches(*argv
, "goto") == 0) {
925 if (get_u32(&target
, *argv
, 0))
926 invarg("invalid target\n", *argv
);
927 addattr32(&req
.n
, sizeof(req
),
929 } else if (matches(*argv
, "nop") == 0)
931 else if (rtnl_rtntype_a2n(&type
, *argv
))
932 invarg("Failed to parse rule type", *argv
);
933 req
.frh
.action
= type
;
940 if (l3mdev_rule
&& tid
!= 0) {
942 "table can not be specified for l3mdev rules\n");
946 if (req
.frh
.family
== AF_UNSPEC
)
947 req
.frh
.family
= AF_INET
;
949 if (!table_ok
&& cmd
== RTM_NEWRULE
)
950 req
.frh
.table
= RT_TABLE_MAIN
;
952 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
958 int do_iprule(int argc
, char **argv
)
961 return iprule_list_flush_or_save(0, NULL
, IPRULE_LIST
);
962 } else if (matches(argv
[0], "list") == 0 ||
963 matches(argv
[0], "lst") == 0 ||
964 matches(argv
[0], "show") == 0) {
965 return iprule_list_flush_or_save(argc
-1, argv
+1, IPRULE_LIST
);
966 } else if (matches(argv
[0], "save") == 0) {
967 return iprule_list_flush_or_save(argc
-1, argv
+1, IPRULE_SAVE
);
968 } else if (matches(argv
[0], "restore") == 0) {
969 return iprule_restore();
970 } else if (matches(argv
[0], "add") == 0) {
971 return iprule_modify(RTM_NEWRULE
, argc
-1, argv
+1);
972 } else if (matches(argv
[0], "delete") == 0) {
973 return iprule_modify(RTM_DELRULE
, argc
-1, argv
+1);
974 } else if (matches(argv
[0], "flush") == 0) {
975 return iprule_list_flush_or_save(argc
-1, argv
+1, IPRULE_FLUSH
);
976 } else if (matches(argv
[0], "help") == 0)
980 "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv
);
984 int do_multirule(int argc
, char **argv
)
986 switch (preferred_family
) {
989 preferred_family
= RTNL_FAMILY_IPMR
;
992 preferred_family
= RTNL_FAMILY_IP6MR
;
994 case RTNL_FAMILY_IPMR
:
995 case RTNL_FAMILY_IP6MR
:
999 "Multicast rules are only supported for IPv4/IPv6, was: %i\n",
1004 return do_iprule(argc
, argv
);