]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
0f8fc6d99136ef647c323c5e34f95f27dc1b2e1f
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
;
76 struct fib_rule_uid_range range
;
83 static inline int frh_get_table(struct fib_rule_hdr
*frh
, struct rtattr
**tb
)
85 __u32 table
= frh
->table
;
87 table
= rta_getattr_u32(tb
[RTA_TABLE
]);
91 static bool filter_nlmsg(struct nlmsghdr
*n
, struct rtattr
**tb
, int host_len
)
93 struct fib_rule_hdr
*frh
= NLMSG_DATA(n
);
96 if (preferred_family
!= AF_UNSPEC
&& frh
->family
!= preferred_family
)
99 if (filter
.prefmask
&&
100 filter
.pref
^ (tb
[FRA_PRIORITY
] ? rta_getattr_u32(tb
[FRA_PRIORITY
]) : 0))
102 if (filter
.not && !(frh
->flags
& FIB_RULE_INVERT
))
105 if (filter
.src
.family
) {
106 inet_prefix
*f_src
= &filter
.src
;
108 if (f_src
->family
!= frh
->family
||
109 f_src
->bitlen
> frh
->src_len
)
112 if (inet_addr_match_rta(f_src
, tb
[FRA_SRC
]))
116 if (filter
.dst
.family
) {
117 inet_prefix
*f_dst
= &filter
.dst
;
119 if (f_dst
->family
!= frh
->family
||
120 f_dst
->bitlen
> frh
->dst_len
)
123 if (inet_addr_match_rta(f_dst
, tb
[FRA_DST
]))
127 if (filter
.tosmask
&& filter
.tos
^ frh
->tos
)
134 mark
= rta_getattr_u32(tb
[FRA_FWMARK
]);
135 if (filter
.fwmark
^ mark
)
142 mask
= rta_getattr_u32(tb
[FRA_FWMASK
]);
143 if (filter
.fwmask
^ mask
)
147 if (filter
.iifmask
) {
148 if (tb
[FRA_IFNAME
]) {
149 if (strcmp(filter
.iif
, rta_getattr_str(tb
[FRA_IFNAME
])) != 0)
156 if (filter
.oifmask
) {
157 if (tb
[FRA_OIFNAME
]) {
158 if (strcmp(filter
.oif
, rta_getattr_str(tb
[FRA_OIFNAME
])) != 0)
165 if (filter
.l3mdev
&& !(tb
[FRA_L3MDEV
] && rta_getattr_u8(tb
[FRA_L3MDEV
])))
168 if (filter
.uidrange
) {
169 struct fib_rule_uid_range
*r
= RTA_DATA(tb
[FRA_UID_RANGE
]);
171 if (!tb
[FRA_UID_RANGE
] ||
172 r
->start
!= filter
.range
.start
||
173 r
->end
!= filter
.range
.end
)
177 table
= frh_get_table(frh
, tb
);
178 if (filter
.tb
> 0 && filter
.tb
^ table
)
184 int print_rule(struct nlmsghdr
*n
, void *arg
)
187 struct fib_rule_hdr
*frh
= NLMSG_DATA(n
);
188 int len
= n
->nlmsg_len
;
190 __u32 table
, prio
= 0;
191 struct rtattr
*tb
[FRA_MAX
+1];
194 if (n
->nlmsg_type
!= RTM_NEWRULE
&& n
->nlmsg_type
!= RTM_DELRULE
)
197 len
-= NLMSG_LENGTH(sizeof(*frh
));
201 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(frh
), len
);
203 host_len
= af_bit_len(frh
->family
);
205 if (!filter_nlmsg(n
, tb
, host_len
))
208 open_json_object(NULL
);
209 if (n
->nlmsg_type
== RTM_DELRULE
)
210 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
212 if (tb
[FRA_PRIORITY
])
213 prio
= rta_getattr_u32(tb
[FRA_PRIORITY
]);
215 print_uint(PRINT_ANY
, "priority", "%u:\t", prio
);
217 if (frh
->flags
& FIB_RULE_INVERT
)
218 print_null(PRINT_ANY
, "not", "not ", NULL
);
221 const char *src
= rt_addr_n2a_rta(frh
->family
, tb
[FRA_SRC
]);
223 print_string(PRINT_FP
, NULL
, "from ", NULL
);
224 print_color_string(PRINT_ANY
, ifa_family_color(frh
->family
),
226 if (frh
->src_len
!= host_len
)
227 print_uint(PRINT_ANY
, "srclen", "/%u ", frh
->src_len
);
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
);
234 print_string(PRINT_ANY
, "src", "from %s ", "all");
238 const char *dst
= rt_addr_n2a_rta(frh
->family
, tb
[FRA_DST
]);
240 print_string(PRINT_FP
, NULL
, "to ", NULL
);
241 print_color_string(PRINT_ANY
, ifa_family_color(frh
->family
),
243 if (frh
->dst_len
!= host_len
)
244 print_uint(PRINT_ANY
, "dstlen", "/%u ", frh
->dst_len
);
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
);
253 print_string(PRINT_ANY
, "tos",
255 rtnl_dsfield_n2a(frh
->tos
, b1
, sizeof(b1
)));
258 if (tb
[FRA_FWMARK
] || tb
[FRA_FWMASK
]) {
259 __u32 mark
= 0, mask
= 0;
262 mark
= rta_getattr_u32(tb
[FRA_FWMARK
]);
264 if (tb
[FRA_FWMASK
] &&
265 (mask
= rta_getattr_u32(tb
[FRA_FWMASK
])) != 0xFFFFFFFF) {
266 print_0xhex(PRINT_ANY
, "fwmark", "fwmark %#llx", mark
);
267 print_0xhex(PRINT_ANY
, "fwmask", "/%#llx ", mask
);
269 print_0xhex(PRINT_ANY
, "fwmark", "fwmark %#llx ", mark
);
273 if (tb
[FRA_IFNAME
]) {
274 if (!is_json_context())
276 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
278 rta_getattr_str(tb
[FRA_IFNAME
]));
280 if (frh
->flags
& FIB_RULE_IIF_DETACHED
)
281 print_null(PRINT_ANY
, "iif_detached", "[detached] ",
285 if (tb
[FRA_OIFNAME
]) {
286 if (!is_json_context())
289 print_color_string(PRINT_ANY
, COLOR_IFNAME
, "oif", "%s ",
290 rta_getattr_str(tb
[FRA_OIFNAME
]));
292 if (frh
->flags
& FIB_RULE_OIF_DETACHED
)
293 print_null(PRINT_ANY
, "oif_detached", "[detached] ",
297 if (tb
[FRA_L3MDEV
]) {
298 __u8 mdev
= rta_getattr_u8(tb
[FRA_L3MDEV
]);
301 print_null(PRINT_ANY
, "l3mdev",
302 "lookup [l3mdev-table] ", NULL
);
305 if (tb
[FRA_UID_RANGE
]) {
306 struct fib_rule_uid_range
*r
= RTA_DATA(tb
[FRA_UID_RANGE
]);
308 print_uint(PRINT_ANY
, "uid_start", "uidrange %u", r
->start
);
309 print_uint(PRINT_ANY
, "uid_end", "-%u ", r
->end
);
312 if (tb
[FRA_IP_PROTO
]) {
314 print_string(PRINT_ANY
, "ipproto", "ipproto %s ",
315 inet_proto_n2a(rta_getattr_u8(tb
[FRA_IP_PROTO
]),
316 pbuf
, sizeof(pbuf
)));
319 if (tb
[FRA_SPORT_RANGE
]) {
320 struct fib_rule_port_range
*r
= RTA_DATA(tb
[FRA_SPORT_RANGE
]);
322 if (r
->start
== r
->end
) {
323 print_uint(PRINT_ANY
, "sport", "sport %u ", r
->start
);
325 print_uint(PRINT_ANY
, "sport_start", "sport %u",
327 print_uint(PRINT_ANY
, "sport_end", "-%u ", r
->end
);
331 if (tb
[FRA_DPORT_RANGE
]) {
332 struct fib_rule_port_range
*r
= RTA_DATA(tb
[FRA_DPORT_RANGE
]);
334 if (r
->start
== r
->end
) {
335 print_uint(PRINT_ANY
, "dport", "dport %u ", r
->start
);
337 print_uint(PRINT_ANY
, "dport_start", "dport %u",
339 print_uint(PRINT_ANY
, "dport_end", "-%u ", r
->end
);
343 table
= frh_get_table(frh
, tb
);
345 print_string(PRINT_ANY
, "table",
347 rtnl_rttable_n2a(table
, b1
, sizeof(b1
)));
349 if (tb
[FRA_SUPPRESS_PREFIXLEN
]) {
350 int pl
= rta_getattr_u32(tb
[FRA_SUPPRESS_PREFIXLEN
]);
353 print_int(PRINT_ANY
, "suppress_prefixlen",
354 "suppress_prefixlength %d ", pl
);
357 if (tb
[FRA_SUPPRESS_IFGROUP
]) {
358 int group
= rta_getattr_u32(tb
[FRA_SUPPRESS_IFGROUP
]);
362 = rtnl_group_n2a(group
, b1
, sizeof(b1
));
364 print_string(PRINT_ANY
, "suppress_ifgroup",
365 "suppress_ifgroup %s ", grname
);
371 __u32 to
= rta_getattr_u32(tb
[FRA_FLOW
]);
376 print_string(PRINT_ANY
,
377 "flow_from", "realms %s/",
378 rtnl_rtrealm_n2a(from
, b1
, sizeof(b1
)));
380 print_string(PRINT_ANY
, "flow_to", "%s ",
381 rtnl_rtrealm_n2a(to
, b1
, sizeof(b1
)));
384 if (frh
->action
== RTN_NAT
) {
385 if (tb
[RTA_GATEWAY
]) {
388 gateway
= format_host_rta(frh
->family
, tb
[RTA_GATEWAY
]);
390 print_string(PRINT_ANY
, "nat_gateway",
391 "map-to %s ", gateway
);
393 print_null(PRINT_ANY
, "masquerade", "masquerade", NULL
);
395 } else if (frh
->action
== FR_ACT_GOTO
) {
397 print_uint(PRINT_ANY
, "goto", "goto %u",
398 rta_getattr_u32(tb
[FRA_GOTO
]));
400 print_string(PRINT_ANY
, "goto", "goto %s", "none");
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
)));
411 if (tb
[FRA_PROTOCOL
]) {
412 __u8 protocol
= rta_getattr_u8(tb
[FRA_PROTOCOL
]);
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
)));
419 print_string(PRINT_FP
, NULL
, "\n", "");
425 static __u32 rule_dump_magic
= 0x71706986;
427 static int save_rule_prep(void)
431 if (isatty(STDOUT_FILENO
)) {
432 fprintf(stderr
, "Not sending a binary stream to stdout\n");
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");
445 static int save_rule(struct nlmsghdr
*n
, void *arg
)
449 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
450 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
451 fprintf(stderr
, "Short write while saving nlmsg\n");
455 return ret
== n
->nlmsg_len
? 0 : ret
;
458 static int flush_rule(struct nlmsghdr
*n
, void *arg
)
460 struct rtnl_handle rth2
;
461 struct fib_rule_hdr
*frh
= NLMSG_DATA(n
);
462 int len
= n
->nlmsg_len
;
463 struct rtattr
*tb
[FRA_MAX
+1];
466 len
-= NLMSG_LENGTH(sizeof(*frh
));
470 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(frh
), len
);
472 host_len
= af_bit_len(frh
->family
);
473 if (!filter_nlmsg(n
, tb
, host_len
))
476 if (tb
[FRA_PROTOCOL
]) {
477 __u8 protocol
= rta_getattr_u8(tb
[FRA_PROTOCOL
]);
479 if ((filter
.protocol
^ protocol
) & filter
.protocolmask
)
483 if (tb
[FRA_PRIORITY
]) {
484 n
->nlmsg_type
= RTM_DELRULE
;
485 n
->nlmsg_flags
= NLM_F_REQUEST
;
487 if (rtnl_open(&rth2
, 0) < 0)
490 if (rtnl_talk(&rth2
, n
, NULL
) < 0)
499 static int iprule_list_flush_or_save(int argc
, char **argv
, int action
)
501 rtnl_filter_t filter_fn
;
502 int af
= preferred_family
;
507 if (action
== IPRULE_SAVE
&& argc
> 0) {
508 fprintf(stderr
, "\"ip rule save\" does not take any arguments.\n");
514 if (save_rule_prep())
516 filter_fn
= save_rule
;
519 filter_fn
= flush_rule
;
522 filter_fn
= print_rule
;
525 memset(&filter
, 0, sizeof(filter
));
528 if (matches(*argv
, "preference") == 0 ||
529 matches(*argv
, "order") == 0 ||
530 matches(*argv
, "priority") == 0) {
534 if (get_u32(&pref
, *argv
, 0))
535 invarg("preference value is invalid\n", *argv
);
538 } else if (strcmp(*argv
, "not") == 0) {
540 } else if (strcmp(*argv
, "tos") == 0) {
544 if (rtnl_dsfield_a2n(&tos
, *argv
))
545 invarg("TOS value is invalid\n", *argv
);
548 } else if (strcmp(*argv
, "fwmark") == 0) {
550 __u32 fwmark
, fwmask
;
553 slash
= strchr(*argv
, '/');
556 if (get_u32(&fwmark
, *argv
, 0))
557 invarg("fwmark value is invalid\n", *argv
);
558 filter
.fwmark
= fwmark
;
560 if (get_u32(&fwmask
, slash
+1, 0))
561 invarg("fwmask value is invalid\n",
563 filter
.fwmask
= fwmask
;
565 } else if (strcmp(*argv
, "dev") == 0 ||
566 strcmp(*argv
, "iif") == 0) {
568 if (get_ifname(filter
.iif
, *argv
))
569 invarg("\"iif\"/\"dev\" not a valid ifname", *argv
);
571 } else if (strcmp(*argv
, "oif") == 0) {
573 if (get_ifname(filter
.oif
, *argv
))
574 invarg("\"oif\" not a valid ifname", *argv
);
576 } else if (strcmp(*argv
, "l3mdev") == 0) {
578 } else if (strcmp(*argv
, "uidrange") == 0) {
581 if (sscanf(*argv
, "%u-%u",
583 &filter
.range
.end
) != 2)
584 invarg("invalid UID range\n", *argv
);
586 } else if (matches(*argv
, "lookup") == 0 ||
587 matches(*argv
, "table") == 0) {
591 if (rtnl_rttable_a2n(&tid
, *argv
))
592 invarg("table id value is invalid\n", *argv
);
594 } else if (matches(*argv
, "from") == 0 ||
595 matches(*argv
, "src") == 0) {
597 if (get_prefix(&filter
.src
, *argv
, af
))
598 invarg("from value is invalid\n", *argv
);
599 } else if (matches(*argv
, "protocol") == 0) {
602 filter
.protocolmask
= -1;
603 if (rtnl_rtprot_a2n(&prot
, *argv
)) {
604 if (strcmp(*argv
, "all") != 0)
605 invarg("invalid \"protocol\"\n", *argv
);
607 filter
.protocolmask
= 0;
609 filter
.protocol
= prot
;
611 if (matches(*argv
, "dst") == 0 ||
612 matches(*argv
, "to") == 0) {
615 if (get_prefix(&filter
.dst
, *argv
, af
))
616 invarg("to value is invalid\n", *argv
);
621 if (rtnl_ruledump_req(&rth
, af
) < 0) {
622 perror("Cannot send dump request");
627 if (rtnl_dump_filter(&rth
, filter_fn
, stdout
) < 0) {
628 fprintf(stderr
, "Dump terminated\n");
636 static int rule_dump_check_magic(void)
641 if (isatty(STDIN_FILENO
)) {
642 fprintf(stderr
, "Can't restore rule dump from a terminal\n");
646 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
647 if (magic
!= rule_dump_magic
) {
648 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n",
656 static int restore_handler(struct rtnl_ctrl_data
*ctrl
,
657 struct nlmsghdr
*n
, void *arg
)
661 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
665 ret
= rtnl_talk(&rth
, n
, NULL
);
666 if ((ret
< 0) && (errno
== EEXIST
))
673 static int iprule_restore(void)
675 if (rule_dump_check_magic())
678 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
681 static int iprule_modify(int cmd
, int argc
, char **argv
)
688 struct fib_rule_hdr frh
;
692 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
693 .n
.nlmsg_flags
= NLM_F_REQUEST
,
694 .frh
.family
= preferred_family
,
695 .frh
.action
= FR_ACT_UNSPEC
,
698 if (cmd
== RTM_NEWRULE
) {
701 "\"ip rule add\" requires arguments.\n");
704 req
.n
.nlmsg_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
705 req
.frh
.action
= FR_ACT_TO_TBL
;
708 if (cmd
== RTM_DELRULE
&& argc
== 0) {
709 fprintf(stderr
, "\"ip rule del\" requires arguments.\n");
714 if (strcmp(*argv
, "not") == 0) {
715 req
.frh
.flags
|= FIB_RULE_INVERT
;
716 } else if (strcmp(*argv
, "from") == 0) {
720 get_prefix(&dst
, *argv
, req
.frh
.family
);
721 req
.frh
.src_len
= dst
.bitlen
;
722 addattr_l(&req
.n
, sizeof(req
), FRA_SRC
,
723 &dst
.data
, dst
.bytelen
);
724 } else if (strcmp(*argv
, "to") == 0) {
728 get_prefix(&dst
, *argv
, req
.frh
.family
);
729 req
.frh
.dst_len
= dst
.bitlen
;
730 addattr_l(&req
.n
, sizeof(req
), FRA_DST
,
731 &dst
.data
, dst
.bytelen
);
732 } else if (matches(*argv
, "preference") == 0 ||
733 matches(*argv
, "order") == 0 ||
734 matches(*argv
, "priority") == 0) {
738 if (get_u32(&pref
, *argv
, 0))
739 invarg("preference value is invalid\n", *argv
);
740 addattr32(&req
.n
, sizeof(req
), FRA_PRIORITY
, pref
);
741 } else if (strcmp(*argv
, "tos") == 0 ||
742 matches(*argv
, "dsfield") == 0) {
746 if (rtnl_dsfield_a2n(&tos
, *argv
))
747 invarg("TOS value is invalid\n", *argv
);
749 } else if (strcmp(*argv
, "fwmark") == 0) {
751 __u32 fwmark
, fwmask
;
755 slash
= strchr(*argv
, '/');
758 if (get_u32(&fwmark
, *argv
, 0))
759 invarg("fwmark value is invalid\n", *argv
);
760 addattr32(&req
.n
, sizeof(req
), FRA_FWMARK
, fwmark
);
762 if (get_u32(&fwmask
, slash
+1, 0))
763 invarg("fwmask value is invalid\n",
765 addattr32(&req
.n
, sizeof(req
),
768 } else if (matches(*argv
, "realms") == 0) {
772 if (get_rt_realms_or_raw(&realm
, *argv
))
773 invarg("invalid realms\n", *argv
);
774 addattr32(&req
.n
, sizeof(req
), FRA_FLOW
, realm
);
775 } else if (matches(*argv
, "protocol") == 0) {
779 if (rtnl_rtprot_a2n(&proto
, *argv
))
780 invarg("\"protocol\" value is invalid\n", *argv
);
781 addattr8(&req
.n
, sizeof(req
), FRA_PROTOCOL
, proto
);
782 } else if (matches(*argv
, "table") == 0 ||
783 strcmp(*argv
, "lookup") == 0) {
785 if (rtnl_rttable_a2n(&tid
, *argv
))
786 invarg("invalid table ID\n", *argv
);
790 req
.frh
.table
= RT_TABLE_UNSPEC
;
791 addattr32(&req
.n
, sizeof(req
), FRA_TABLE
, tid
);
794 } else if (matches(*argv
, "suppress_prefixlength") == 0 ||
795 strcmp(*argv
, "sup_pl") == 0) {
799 if (get_s32(&pl
, *argv
, 0) || pl
< 0)
800 invarg("suppress_prefixlength value is invalid\n",
802 addattr32(&req
.n
, sizeof(req
),
803 FRA_SUPPRESS_PREFIXLEN
, pl
);
804 } else if (matches(*argv
, "suppress_ifgroup") == 0 ||
805 strcmp(*argv
, "sup_group") == 0) {
809 if (rtnl_group_a2n(&group
, *argv
))
810 invarg("Invalid \"suppress_ifgroup\" value\n",
812 addattr32(&req
.n
, sizeof(req
),
813 FRA_SUPPRESS_IFGROUP
, group
);
814 } else if (strcmp(*argv
, "dev") == 0 ||
815 strcmp(*argv
, "iif") == 0) {
817 if (check_ifname(*argv
))
818 invarg("\"iif\"/\"dev\" not a valid ifname", *argv
);
819 addattr_l(&req
.n
, sizeof(req
), FRA_IFNAME
,
820 *argv
, strlen(*argv
)+1);
821 } else if (strcmp(*argv
, "oif") == 0) {
823 if (check_ifname(*argv
))
824 invarg("\"oif\" not a valid ifname", *argv
);
825 addattr_l(&req
.n
, sizeof(req
), FRA_OIFNAME
,
826 *argv
, strlen(*argv
)+1);
827 } else if (strcmp(*argv
, "l3mdev") == 0) {
828 addattr8(&req
.n
, sizeof(req
), FRA_L3MDEV
, 1);
831 } else if (strcmp(*argv
, "uidrange") == 0) {
832 struct fib_rule_uid_range r
;
835 if (sscanf(*argv
, "%u-%u", &r
.start
, &r
.end
) != 2)
836 invarg("invalid UID range\n", *argv
);
837 addattr_l(&req
.n
, sizeof(req
), FRA_UID_RANGE
, &r
,
839 } else if (strcmp(*argv
, "nat") == 0 ||
840 matches(*argv
, "map-to") == 0) {
842 fprintf(stderr
, "Warning: route NAT is deprecated\n");
843 addattr32(&req
.n
, sizeof(req
), RTA_GATEWAY
,
845 req
.frh
.action
= RTN_NAT
;
846 } else if (strcmp(*argv
, "ipproto") == 0) {
850 ipproto
= inet_proto_a2n(*argv
);
852 invarg("Invalid \"ipproto\" value\n",
854 addattr8(&req
.n
, sizeof(req
), FRA_IP_PROTO
, ipproto
);
855 } else if (strcmp(*argv
, "sport") == 0) {
856 struct fib_rule_port_range r
;
860 ret
= sscanf(*argv
, "%hu-%hu", &r
.start
, &r
.end
);
864 invarg("invalid port range\n", *argv
);
865 addattr_l(&req
.n
, sizeof(req
), FRA_SPORT_RANGE
, &r
,
867 } else if (strcmp(*argv
, "dport") == 0) {
868 struct fib_rule_port_range r
;
872 ret
= sscanf(*argv
, "%hu-%hu", &r
.start
, &r
.end
);
876 invarg("invalid dport range\n", *argv
);
877 addattr_l(&req
.n
, sizeof(req
), FRA_DPORT_RANGE
, &r
,
882 if (strcmp(*argv
, "type") == 0)
885 if (matches(*argv
, "help") == 0)
887 else if (matches(*argv
, "goto") == 0) {
892 if (get_u32(&target
, *argv
, 0))
893 invarg("invalid target\n", *argv
);
894 addattr32(&req
.n
, sizeof(req
),
896 } else if (matches(*argv
, "nop") == 0)
898 else if (rtnl_rtntype_a2n(&type
, *argv
))
899 invarg("Failed to parse rule type", *argv
);
900 req
.frh
.action
= type
;
907 if (l3mdev_rule
&& tid
!= 0) {
909 "table can not be specified for l3mdev rules\n");
913 if (req
.frh
.family
== AF_UNSPEC
)
914 req
.frh
.family
= AF_INET
;
916 if (!table_ok
&& cmd
== RTM_NEWRULE
)
917 req
.frh
.table
= RT_TABLE_MAIN
;
919 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
925 int do_iprule(int argc
, char **argv
)
928 return iprule_list_flush_or_save(0, NULL
, IPRULE_LIST
);
929 } else if (matches(argv
[0], "list") == 0 ||
930 matches(argv
[0], "lst") == 0 ||
931 matches(argv
[0], "show") == 0) {
932 return iprule_list_flush_or_save(argc
-1, argv
+1, IPRULE_LIST
);
933 } else if (matches(argv
[0], "save") == 0) {
934 return iprule_list_flush_or_save(argc
-1, argv
+1, IPRULE_SAVE
);
935 } else if (matches(argv
[0], "restore") == 0) {
936 return iprule_restore();
937 } else if (matches(argv
[0], "add") == 0) {
938 return iprule_modify(RTM_NEWRULE
, argc
-1, argv
+1);
939 } else if (matches(argv
[0], "delete") == 0) {
940 return iprule_modify(RTM_DELRULE
, argc
-1, argv
+1);
941 } else if (matches(argv
[0], "flush") == 0) {
942 return iprule_list_flush_or_save(argc
-1, argv
+1, IPRULE_FLUSH
);
943 } else if (matches(argv
[0], "help") == 0)
947 "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv
);
951 int do_multirule(int argc
, char **argv
)
953 switch (preferred_family
) {
956 preferred_family
= RTNL_FAMILY_IPMR
;
959 preferred_family
= RTNL_FAMILY_IP6MR
;
961 case RTNL_FAMILY_IPMR
:
962 case RTNL_FAMILY_IP6MR
:
966 "Multicast rules are only supported for IPv4/IPv6, was: %i\n",
971 return do_iprule(argc
, argv
);