]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
744d6d88e3433525b7a739ee5fcfbfc09979a64e
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(const struct sockaddr_nl
*who
, 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 0x%x", mark
);
267 print_0xhex(PRINT_ANY
, "fwmask", "/0x%x ", mask
);
269 print_0xhex(PRINT_ANY
, "fwmark", "fwmark 0x%x ", 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(const struct sockaddr_nl
*who
,
446 struct nlmsghdr
*n
, void *arg
)
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");
456 return ret
== n
->nlmsg_len
? 0 : ret
;
459 static int flush_rule(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
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];
467 len
-= NLMSG_LENGTH(sizeof(*frh
));
471 parse_rtattr(tb
, FRA_MAX
, RTM_RTA(frh
), len
);
473 if (tb
[FRA_PROTOCOL
]) {
474 __u8 protocol
= rta_getattr_u8(tb
[FRA_PROTOCOL
]);
476 if ((filter
.protocol
^ protocol
) & filter
.protocolmask
)
480 if (tb
[FRA_PRIORITY
]) {
481 n
->nlmsg_type
= RTM_DELRULE
;
482 n
->nlmsg_flags
= NLM_F_REQUEST
;
484 if (rtnl_open(&rth2
, 0) < 0)
487 if (rtnl_talk(&rth2
, n
, NULL
) < 0)
496 static int iprule_list_flush_or_save(int argc
, char **argv
, int action
)
498 rtnl_filter_t filter_fn
;
499 int af
= preferred_family
;
504 if (action
== IPRULE_SAVE
&& argc
> 0) {
505 fprintf(stderr
, "\"ip rule save\" does not take any arguments.\n");
511 if (save_rule_prep())
513 filter_fn
= save_rule
;
516 filter_fn
= flush_rule
;
519 filter_fn
= print_rule
;
522 memset(&filter
, 0, sizeof(filter
));
525 if (matches(*argv
, "preference") == 0 ||
526 matches(*argv
, "order") == 0 ||
527 matches(*argv
, "priority") == 0) {
531 if (get_u32(&pref
, *argv
, 0))
532 invarg("preference value is invalid\n", *argv
);
535 } else if (strcmp(*argv
, "not") == 0) {
537 } else if (strcmp(*argv
, "tos") == 0) {
541 if (rtnl_dsfield_a2n(&tos
, *argv
))
542 invarg("TOS value is invalid\n", *argv
);
545 } else if (strcmp(*argv
, "fwmark") == 0) {
547 __u32 fwmark
, fwmask
;
550 slash
= strchr(*argv
, '/');
553 if (get_u32(&fwmark
, *argv
, 0))
554 invarg("fwmark value is invalid\n", *argv
);
555 filter
.fwmark
= fwmark
;
557 if (get_u32(&fwmask
, slash
+1, 0))
558 invarg("fwmask value is invalid\n",
560 filter
.fwmask
= fwmask
;
562 } else if (strcmp(*argv
, "dev") == 0 ||
563 strcmp(*argv
, "iif") == 0) {
565 if (get_ifname(filter
.iif
, *argv
))
566 invarg("\"iif\"/\"dev\" not a valid ifname", *argv
);
568 } else if (strcmp(*argv
, "oif") == 0) {
570 if (get_ifname(filter
.oif
, *argv
))
571 invarg("\"oif\" not a valid ifname", *argv
);
573 } else if (strcmp(*argv
, "l3mdev") == 0) {
575 } else if (strcmp(*argv
, "uidrange") == 0) {
578 if (sscanf(*argv
, "%u-%u",
580 &filter
.range
.end
) != 2)
581 invarg("invalid UID range\n", *argv
);
583 } else if (matches(*argv
, "lookup") == 0 ||
584 matches(*argv
, "table") == 0) {
588 if (rtnl_rttable_a2n(&tid
, *argv
))
589 invarg("table id value is invalid\n", *argv
);
591 } else if (matches(*argv
, "from") == 0 ||
592 matches(*argv
, "src") == 0) {
594 if (get_prefix(&filter
.src
, *argv
, af
))
595 invarg("from value is invalid\n", *argv
);
596 } else if (matches(*argv
, "protocol") == 0) {
599 filter
.protocolmask
= -1;
600 if (rtnl_rtprot_a2n(&prot
, *argv
)) {
601 if (strcmp(*argv
, "all") != 0)
602 invarg("invalid \"protocol\"\n", *argv
);
604 filter
.protocolmask
= 0;
606 filter
.protocol
= prot
;
608 if (matches(*argv
, "dst") == 0 ||
609 matches(*argv
, "to") == 0) {
612 if (get_prefix(&filter
.dst
, *argv
, af
))
613 invarg("to value is invalid\n", *argv
);
618 if (rtnl_wilddump_request(&rth
, af
, RTM_GETRULE
) < 0) {
619 perror("Cannot send dump request");
624 if (rtnl_dump_filter(&rth
, filter_fn
, stdout
) < 0) {
625 fprintf(stderr
, "Dump terminated\n");
633 static int rule_dump_check_magic(void)
638 if (isatty(STDIN_FILENO
)) {
639 fprintf(stderr
, "Can't restore rule dump from a terminal\n");
643 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
644 if (magic
!= rule_dump_magic
) {
645 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n",
653 static int restore_handler(const struct sockaddr_nl
*nl
,
654 struct rtnl_ctrl_data
*ctrl
,
655 struct nlmsghdr
*n
, void *arg
)
659 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
663 ret
= rtnl_talk(&rth
, n
, NULL
);
664 if ((ret
< 0) && (errno
== EEXIST
))
671 static int iprule_restore(void)
673 if (rule_dump_check_magic())
676 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
679 static int iprule_modify(int cmd
, int argc
, char **argv
)
686 struct fib_rule_hdr frh
;
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
,
696 if (cmd
== RTM_NEWRULE
) {
697 req
.n
.nlmsg_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
698 req
.frh
.action
= FR_ACT_TO_TBL
;
701 if (cmd
== RTM_DELRULE
&& argc
== 0) {
702 fprintf(stderr
, "\"ip rule del\" requires arguments.\n");
707 if (strcmp(*argv
, "not") == 0) {
708 req
.frh
.flags
|= FIB_RULE_INVERT
;
709 } else if (strcmp(*argv
, "from") == 0) {
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) {
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) {
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) {
739 if (rtnl_dsfield_a2n(&tos
, *argv
))
740 invarg("TOS value is invalid\n", *argv
);
742 } else if (strcmp(*argv
, "fwmark") == 0) {
744 __u32 fwmark
, fwmask
;
748 slash
= strchr(*argv
, '/');
751 if (get_u32(&fwmark
, *argv
, 0))
752 invarg("fwmark value is invalid\n", *argv
);
753 addattr32(&req
.n
, sizeof(req
), FRA_FWMARK
, fwmark
);
755 if (get_u32(&fwmask
, slash
+1, 0))
756 invarg("fwmask value is invalid\n",
758 addattr32(&req
.n
, sizeof(req
),
761 } else if (matches(*argv
, "realms") == 0) {
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) {
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) {
778 if (rtnl_rttable_a2n(&tid
, *argv
))
779 invarg("invalid table ID\n", *argv
);
783 req
.frh
.table
= RT_TABLE_UNSPEC
;
784 addattr32(&req
.n
, sizeof(req
), FRA_TABLE
, tid
);
787 } else if (matches(*argv
, "suppress_prefixlength") == 0 ||
788 strcmp(*argv
, "sup_pl") == 0) {
792 if (get_s32(&pl
, *argv
, 0) || pl
< 0)
793 invarg("suppress_prefixlength value is invalid\n",
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) {
802 if (rtnl_group_a2n(&group
, *argv
))
803 invarg("Invalid \"suppress_ifgroup\" value\n",
805 addattr32(&req
.n
, sizeof(req
),
806 FRA_SUPPRESS_IFGROUP
, group
);
807 } else if (strcmp(*argv
, "dev") == 0 ||
808 strcmp(*argv
, "iif") == 0) {
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) {
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);
824 } else if (strcmp(*argv
, "uidrange") == 0) {
825 struct fib_rule_uid_range r
;
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
,
832 } else if (strcmp(*argv
, "nat") == 0 ||
833 matches(*argv
, "map-to") == 0) {
835 fprintf(stderr
, "Warning: route NAT is deprecated\n");
836 addattr32(&req
.n
, sizeof(req
), RTA_GATEWAY
,
838 req
.frh
.action
= RTN_NAT
;
839 } else if (strcmp(*argv
, "ipproto") == 0) {
843 ipproto
= inet_proto_a2n(*argv
);
845 invarg("Invalid \"ipproto\" value\n",
847 addattr8(&req
.n
, sizeof(req
), FRA_IP_PROTO
, ipproto
);
848 } else if (strcmp(*argv
, "sport") == 0) {
849 struct fib_rule_port_range r
;
853 ret
= sscanf(*argv
, "%hu-%hu", &r
.start
, &r
.end
);
857 invarg("invalid port range\n", *argv
);
858 addattr_l(&req
.n
, sizeof(req
), FRA_SPORT_RANGE
, &r
,
860 } else if (strcmp(*argv
, "dport") == 0) {
861 struct fib_rule_port_range r
;
865 ret
= sscanf(*argv
, "%hu-%hu", &r
.start
, &r
.end
);
869 invarg("invalid dport range\n", *argv
);
870 addattr_l(&req
.n
, sizeof(req
), FRA_DPORT_RANGE
, &r
,
875 if (strcmp(*argv
, "type") == 0)
878 if (matches(*argv
, "help") == 0)
880 else if (matches(*argv
, "goto") == 0) {
885 if (get_u32(&target
, *argv
, 0))
886 invarg("invalid target\n", *argv
);
887 addattr32(&req
.n
, sizeof(req
),
889 } else if (matches(*argv
, "nop") == 0)
891 else if (rtnl_rtntype_a2n(&type
, *argv
))
892 invarg("Failed to parse rule type", *argv
);
893 req
.frh
.action
= type
;
900 if (l3mdev_rule
&& tid
!= 0) {
902 "table can not be specified for l3mdev rules\n");
906 if (req
.frh
.family
== AF_UNSPEC
)
907 req
.frh
.family
= AF_INET
;
909 if (!table_ok
&& cmd
== RTM_NEWRULE
)
910 req
.frh
.table
= RT_TABLE_MAIN
;
912 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
918 int do_iprule(int argc
, char **argv
)
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)
940 "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv
);
944 int do_multirule(int argc
, char **argv
)
946 switch (preferred_family
) {
949 preferred_family
= RTNL_FAMILY_IPMR
;
952 preferred_family
= RTNL_FAMILY_IP6MR
;
954 case RTNL_FAMILY_IPMR
:
955 case RTNL_FAMILY_IP6MR
:
959 "Multicast rules are only supported for IPv4/IPv6, was: %i\n",
964 return do_iprule(argc
, argv
);