ip rule: merger iprule_flush and add selector support
Subsystems (2)
cover ip rule: merger iprule_flush and add selector support
When merge iprule_flush() and iprule_list_or_save(). Renamed rtnl_filter_t filter to filter_fn because we want to use global variable 'filter' to filter nlmsg in the next patch.
Hangbin Liu (2):
ip rule: merge ip rule flush and list, save together ip rule: add selector support
ip/iprule.c | 295 +++++++++++++++++++++++++++++++++++++++++------------ man/man8/ip-rule.8 | 6 +- 2 files changed, 231 insertions(+), 70 deletions(-)
Signature · 1 line
2.5.5
PATCH 1/2 ip rule: merge ip rule flush and list, save together
iprule_flush() and iprule_list_or_save() both call function rtnl_wilddump_request() and rtnl_dump_filter(). So merge them together just like other files do.
--- ip/iprule.c | 121 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 54 insertions(+), 67 deletions(-)
ip/iprule.c +54 -67
@@ -27,6 +27,12 @@#include "utils.h" #include "ip_common.h" +enum list_action { +IPRULE_LIST , +IPRULE_FLUSH , +IPRULE_SAVE , +}; +extern struct rtnl_handle rth;static void usage (void ) __attribute__((noreturn));
@@ -243,24 +249,61 @@ static int save_rule(const struct sockaddr_nl *who,return ret == n->nlmsg_len ?0 : ret; } -static int iprule_list_or_save (int argc,char **argv,int save) +static int flush_rule (const struct sockaddr_nl *who,struct nlmsghdr *n, +void *arg) +{ +struct rtnl_handle rth2; +struct rtmsg *r =NLMSG_DATA (n); +int len = n->nlmsg_len; +struct rtattr *tb[FRA_MAX +1 ]; + + len -=NLMSG_LENGTH (sizeof (*r)); +if (len <0 ) +return -1 ; + +parse_rtattr (tb,FRA_MAX ,RTM_RTA (r), len); + +if (tb[FRA_PRIORITY ]) { + n->nlmsg_type =RTM_DELRULE ; + n->nlmsg_flags =NLM_F_REQUEST ; + +if (rtnl_open (&rth2,0 ) <0 ) +return -1 ; + +if (rtnl_talk (&rth2, n,NULL ,0 ) <0 ) +return -2 ; + +rtnl_close (&rth2); + } + +return 0 ; +} + +static int iprule_list_flush_or_save (int argc,char **argv,int action) { -rtnl_filter_t filter = print_rule; +rtnl_filter_t filter_fn;int af = preferred_family;if (af ==AF_UNSPEC ) af =AF_INET ;if (argc >0 ) { -fprintf (stderr,"\"ip rule %s\" does not take any arguments.\n" , - save ?"save" :"show" ); +fprintf (stderr, +"\"ip rule list/flush/save\" does not take any arguments\n" );return -1 ; } -if (save) { +switch (action) { +case IPRULE_SAVE :if (save_rule_prep ())return -1 ; - filter = save_rule; + filter_fn = save_rule; +break ; +case IPRULE_FLUSH : + filter_fn = flush_rule; +break ; +default : + filter_fn = print_rule; }if (rtnl_wilddump_request (&rth, af,RTM_GETRULE ) <0 ) {
@@ -268,7 +311,7 @@ static int iprule_list_or_save(int argc, char **argv, int save)return 1 ; } -if (rtnl_dump_filter (&rth, filter, stdout) <0 ) { +if (rtnl_dump_filter (&rth, filter_fn, stdout) <0 ) {fprintf (stderr,"Dump terminated\n" );return 1 ; }
@@ -511,72 +554,16 @@ static int iprule_modify(int cmd, int argc, char **argv)return 0 ; } - -static int flush_rule (const struct sockaddr_nl *who,struct nlmsghdr *n, -void *arg) -{ -struct rtnl_handle rth2; -struct rtmsg *r =NLMSG_DATA (n); -int len = n->nlmsg_len; -struct rtattr *tb[FRA_MAX +1 ]; - - len -=NLMSG_LENGTH (sizeof (*r)); -if (len <0 ) -return -1 ; - -parse_rtattr (tb,FRA_MAX ,RTM_RTA (r), len); - -if (tb[FRA_PRIORITY ]) { - n->nlmsg_type =RTM_DELRULE ; - n->nlmsg_flags =NLM_F_REQUEST ; - -if (rtnl_open (&rth2,0 ) <0 ) -return -1 ; - -if (rtnl_talk (&rth2, n,NULL ,0 ) <0 ) -return -2 ; - -rtnl_close (&rth2); - } - -return 0 ; -} - -static int iprule_flush (int argc,char **argv) -{ -int af = preferred_family; - -if (af ==AF_UNSPEC ) - af =AF_INET ; - -if (argc >0 ) { -fprintf (stderr,"\"ip rule flush\" does not allow arguments\n" ); -return -1 ; - } - -if (rtnl_wilddump_request (&rth, af,RTM_GETRULE ) <0 ) { -perror ("Cannot send dump request" ); -return 1 ; - } - -if (rtnl_dump_filter (&rth, flush_rule,NULL ) <0 ) { -fprintf (stderr,"Flush terminated\n" ); -return 1 ; - } - -return 0 ; -} -int do_iprule (int argc,char **argv) {if (argc <1 ) { -return iprule_list_or_save (0 ,NULL ,0 ); +return iprule_list_flush_or_save (0 ,NULL ,IPRULE_LIST ); }else if (matches (argv[0 ],"list" ) ==0 ||matches (argv[0 ],"lst" ) ==0 ||matches (argv[0 ],"show" ) ==0 ) { -return iprule_list_or_save (argc-1 , argv+1 ,0 ); +return iprule_list_flush_or_save (argc-1 , argv+1 ,IPRULE_LIST ); }else if (matches (argv[0 ],"save" ) ==0 ) { -return iprule_list_or_save (argc-1 , argv+1 ,1 ); +return iprule_list_flush_or_save (argc-1 , argv+1 ,IPRULE_SAVE ); }else if (matches (argv[0 ],"restore" ) ==0 ) {return iprule_restore (); }else if (matches (argv[0 ],"add" ) ==0 ) {
@@ -584,7 +571,7 @@ int do_iprule(int argc, char **argv) }else if (matches (argv[0 ],"delete" ) ==0 ) {return iprule_modify (RTM_DELRULE , argc-1 , argv+1 ); }else if (matches (argv[0 ],"flush" ) ==0 ) { -return iprule_flush (argc-1 , argv+1 ); +return iprule_list_flush_or_save (argc-1 , argv+1 ,IPRULE_FLUSH ); }else if (matches (argv[0 ],"help" ) ==0 )usage ();
Signature · 1 line
2.5.5
PATCH 2/2 ip rule: add selector support
In reply to Hangbin Liu--- ip/iprule.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++-- man/man8/ip-rule.8 | 6 +- 2 files changed, 180 insertions(+), 6 deletions(-)
ip/iprule.c +176 -4
@@ -20,6 +20,7 @@#include <netinet/ip.h> #include <arpa/inet.h> #include <string.h> +#include <linux/if.h> #include <linux/fib_rules.h> #include <errno.h>
@@ -41,7 +42,7 @@ static void usage(void) {fprintf (stderr,"Usage: ip rule { add | del } SELECTOR ACTION\n" );fprintf (stderr," ip rule { flush | save | restore }\n" ); -fprintf (stderr," ip rule [ list ]\n" ); +fprintf (stderr," ip rule [ list [ SELECTOR ]]\n" );fprintf (stderr,"SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n" );fprintf (stderr," [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]\n" );fprintf (stderr,"ACTION := [ table TABLE_ID ]\n" );
@@ -55,6 +56,105 @@ static void usage(void)exit (-1 ); } +static struct +{ +int not; +int l3mdev; +int iifmask, oifmask; +unsigned int tb; +unsigned int tos, tosmask; +unsigned int pref, prefmask; +unsigned int fwmark, fwmask; +char iif[IFNAMSIZ ]; +char oif[IFNAMSIZ ]; +inet_prefix src; +inet_prefix dst; +} filter; + +static bool filter_nlmsg (struct nlmsghdr *n,struct rtattr **tb,int host_len) +{ +struct rtmsg *r =NLMSG_DATA (n); +inet_prefix src = { .family = r->rtm_family }; +inet_prefix dst = { .family = r->rtm_family }; +__u32 table; + +if (preferred_family !=AF_UNSPEC && r->rtm_family != preferred_family) +return false; + +if (filter.prefmask && + filter.pref ^ (tb[FRA_PRIORITY ] ?rta_getattr_u32 (tb[FRA_PRIORITY ]) :0 )) +return false; +if (filter.not && !(r->rtm_flags &FIB_RULE_INVERT )) +return false; + +if (filter.src.family) { +if (tb[FRA_SRC ]) { +memcpy (&src.data,RTA_DATA (tb[FRA_SRC ]), + (r->rtm_src_len +7 ) /8 ); + } +if (filter.src.family != r->rtm_family || + filter.src.bitlen > r->rtm_src_len || +inet_addr_match (&src, &filter.src, filter.src.bitlen)) +return false; + } + +if (filter.dst.family) { +if (tb[FRA_DST ]) { +memcpy (&dst.data,RTA_DATA (tb[FRA_DST ]), + (r->rtm_dst_len +7 ) /8 ); + } +if (filter.dst.family != r->rtm_family || + filter.dst.bitlen > r->rtm_dst_len || +inet_addr_match (&dst, &filter.dst, filter.dst.bitlen)) +return false; + } + +if (filter.tosmask && filter.tos ^ r->rtm_tos) +return false; + +if (filter.fwmark) { +__u32 mark =0 ; +if (tb[FRA_FWMARK ]) + mark =rta_getattr_u32 (tb[FRA_FWMARK ]); +if (filter.fwmark ^ mark) +return false; + } +if (filter.fwmask) { +__u32 mask =0 ; +if (tb[FRA_FWMASK ]) + mask =rta_getattr_u32 (tb[FRA_FWMASK ]); +if (filter.fwmask ^ mask) +return false; + } + +if (filter.iifmask) { +if (tb[FRA_IFNAME ]) { +if (strcmp (filter.iif,rta_getattr_str (tb[FRA_IFNAME ])) !=0 ) +return false; + }else { +return false; + } + } + +if (filter.oifmask) { +if (tb[FRA_OIFNAME ]) { +if (strcmp (filter.oif,rta_getattr_str (tb[FRA_OIFNAME ])) !=0 ) +return false; + }else { +return false; + } + } + +if (filter.l3mdev && !(tb[FRA_L3MDEV ] &&rta_getattr_u8 (tb[FRA_L3MDEV ]))) +return false; + + table =rtm_get_table (r, tb); +if (filter.tb >0 && filter.tb ^ table) +return false; + +return true; +} +int print_rule (const struct sockaddr_nl *who,struct nlmsghdr *n,void *arg) {FILE *fp = (FILE *)arg;
@@ -77,6 +177,9 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) host_len =af_bit_len (r->rtm_family); +if (!filter_nlmsg (n, tb, host_len)) +return 0 ; +if (n->nlmsg_type ==RTM_DELRULE )fprintf (fp,"Deleted " );
@@ -287,9 +390,9 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)if (af ==AF_UNSPEC ) af =AF_INET ; -if (argc >0 ) { -fprintf (stderr, -"\"ip rule list/flush/save\" does not take any arguments\n" ); +if (action !=IPRULE_LIST && argc >0 ) { +fprintf (stderr,"\"ip rule %s\" does not take any arguments.\n" , + action ==IPRULE_SAVE ?"save" :"flush" );return -1 ; }
@@ -306,6 +409,75 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action) filter_fn = print_rule; } +memset (&filter,0 ,sizeof (filter)); + +while (argc >0 ) { +if (matches (*argv,"preference" ) ==0 || +matches (*argv,"order" ) ==0 || +matches (*argv,"priority" ) ==0 ) { +__u32 pref; +NEXT_ARG (); +if (get_u32 (&pref, *argv,0 )) +invarg ("preference value is invalid\n" , *argv); + filter.pref = pref; + filter.prefmask =1 ; + }else if (strcmp (*argv,"not" ) ==0 ) { + filter.not =1 ; + }else if (strcmp (*argv,"tos" ) ==0 ) { +__u32 tos; +NEXT_ARG (); +if (rtnl_dsfield_a2n (&tos, *argv)) +invarg ("TOS value is invalid\n" , *argv); + filter.tos = tos; + filter.tosmask =1 ; + }else if (strcmp (*argv,"fwmark" ) ==0 ) { +char *slash; +__u32 fwmark, fwmask; +NEXT_ARG (); + slash =strchr (*argv,'/' ); +if (slash !=NULL ) + *slash ='\0' ; +if (get_u32 (&fwmark, *argv,0 )) +invarg ("fwmark value is invalid\n" , *argv); + filter.fwmark = fwmark; +if (slash) { +if (get_u32 (&fwmask, slash+1 ,0 )) +invarg ("fwmask value is invalid\n" , + slash+1 ); + filter.fwmask = fwmask; + } + }else if (strcmp (*argv,"dev" ) ==0 || +strcmp (*argv,"iif" ) ==0 ) { +NEXT_ARG (); +strncpy (filter.iif, *argv,IFNAMSIZ ); + filter.iifmask =1 ; + }else if (strcmp (*argv,"oif" ) ==0 ) { +NEXT_ARG (); +strncpy (filter.oif, *argv,IFNAMSIZ ); + filter.oifmask =1 ; + }else if (strcmp (*argv,"l3mdev" ) ==0 ) { + filter.l3mdev =1 ; + }else if (matches (*argv,"lookup" ) ==0 || +matches (*argv,"table" ) ==0 ) { +__u32 tid; +NEXT_ARG (); +if (rtnl_rttable_a2n (&tid, *argv)) +invarg ("table id value is invalid\n" , *argv); + filter.tb = tid; + }else if (matches (*argv,"from" ) ==0 || +matches (*argv,"src" ) ==0 ) { +NEXT_ARG (); +get_prefix (&filter.src, *argv, af); + }else { +if (matches (*argv,"dst" ) ==0 || +matches (*argv,"to" ) ==0 ) { +NEXT_ARG (); + } +get_prefix (&filter.dst, *argv, af); + } + argc--; argv++; + } +if (rtnl_wilddump_request (&rth, af,RTM_GETRULE ) <0 ) {perror ("Cannot send dump request" );return 1 ;
man/man8/ip-rule.8 +4 -2
@@ -15,7 +15,8 @@ ip-rule \- routing policy database management .ti -8 .B ip rule -.RB "[ " list " ]" +.RB "[ " list +.I "[ " SELECTOR " ]]" .ti -8 .B ip rule
@@ -42,7 +43,8 @@ ip-rule \- routing policy database management .B oif .IR STRING " ] [ " .B pref -.IR NUMBER " ]" +.IR NUMBER " ] [ " +.BR l3mdev " ]" .ti -8 .IR ACTION " := [ "
Signature · 1 line
2.5.5
Re: [PATCH iproute2 2/2] ip rule: add selector support
On Thu, Sep 22, 2016 at 02:28:49PM +0800, Hangbin Liu wrote:
[...]
man/man8/ip-rule.8 ip-rule \- routing policy database management · line 19
Quoted 11 lines
man/man8/ip-rule.8 +2
-1@@ -15,7 +15,8 @@ ip-rule \- routing policy database management .ti -8 .B ip rule -.RB "[ " list " ]" +.RB "[ " list +.I "[ " SELECTOR " ]]"
This makes the brackets cursive, too. Better use this instead:
| .RI "[ " SELECTOR " ]]"
Apart from that, LGTM!
Thanks, Phil
Re: [PATCH iproute2 2/2] ip rule: add selector support
2016-09-22 16:45 GMT+08:00 Phil Sutter <[email protected]>:
Quoted 16 lines · Hangbin Liu
[...]
man/man8/ip-rule.8 ip-rule \- routing policy database management · line 19
Quoted 11 lines
man/man8/ip-rule.8 +2
-1@@ -15,7 +15,8 @@ ip-rule \- routing policy database management .ti -8 .B ip rule -.RB "[ " list " ]" +.RB "[ " list +.I "[ " SELECTOR " ]]"This makes the brackets cursive, too. Better use this instead:
Quoted 1 line
| .RI "[ " SELECTOR " ]]"
Thanks Phil, I'm not familiar with man doc syntax :)
Cheers
Hangbin
Quoted 4 lines · On Thu, 22 Sep 2016 14:28:47 +0800 Hangbin Liu <[email protected]> wrote:
When merge
iprule_flush()andiprule_list_or_save(). Renamedrtnl_filter_tfilter tofilter_fnbecause we want to use global variable 'filter' to filter nlmsg in the next patch.
Quoted 8 lines
Hangbin Liu (2):
ip rule: merge ip rule flush and list, save together ip rule: add selector supportip/iprule.c | 295 +++++++++++++++++++++++++++++++++++++++++------------ man/man8/ip-rule.8 | 6 +- 2 files changed, 231 insertions(+), 70 deletions(-)
Fix Phil's man page comment and resubmit.
Stephen Hemminger
AppliedQuoted 4 lines · On Thu, 22 Sep 2016 14:28:47 +0800 Hangbin Liu <[email protected]> wrote:
When merge
iprule_flush()andiprule_list_or_save(). Renamedrtnl_filter_tfilter tofilter_fnbecause we want to use global variable 'filter' to filter nlmsg in the next patch.
Quoted 9 lines
Hangbin Liu (2):
ip rule: merge ip rule flush and list, save together ip rule: add selector supportip/iprule.c | 295 +++++++++++++++++++++++++++++++++++++++++------------ man/man8/ip-rule.8 | 6 +- 2 files changed, 231 insertions(+), 70 deletions(-)
Applied to net-next (for 4.9)