xshared: Review option parsing
Phil Sutter
reviewercover xshared: Review option parsing
Faced with the need to extend optflags, inverse_for_options and commands_v_options arrays in order to integrate ebtables-specific options, I chose to get rid of them all instead.
Patch 1 replaces opt2char() function which didn't work well since not every option has printable short-option character. The callback-based replacement returns the long-option and should make error messages more readable this way.
Patch 2 elminates the inverse_for_options array along with the loop turning a given option into its bit's position for use in the array. The switch statement in the replacing callback is much easier to maintain and extend.
Patch 3 makes use of the fact that no command has a mandatory option anymore. So every combination is either allowed or not, and a single bit may indicate that.
Phil Sutter (3):
xshared: Introduce xt_cmd_parse_ops::option_name
xshared: Introduce xt_cmd_parse_ops::option_invert
xshared: Simplify generic_opt_check()
iptables/ip6tables.c | 2 + iptables/iptables.c | 2 + iptables/nft-arp.c | 32 ++++++ iptables/nft-ipv4.c | 2 + iptables/nft-ipv6.c | 2 + iptables/xshared.c | 249 ++++++++++++++++++++----------------------- iptables/xshared.h | 6 ++ 7 files changed, 159 insertions(+), 136 deletions(-)
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 1/3 xshared: Introduce xt_cmd_parse_ops::option_name
The old opt2char() function was flawed: Since not every field in optflags contains a printable character, typical use of its return value in print statements could lead to garbage on screen.
Replace this by a mechanism to retrieve an option's long name which supports family-specific overrides. and get rid of optflags field altogether and define NUMBER_OF_OPT similar to NUMBER_OF_CMD.
--- iptables/ip6tables.c | 1 + iptables/iptables.c | 1 + iptables/nft-arp.c | 18 ++++++ iptables/nft-ipv4.c | 1 + iptables/nft-ipv6.c | 1 + iptables/xshared.c | 140 +++++++++++++++++++++++-------------------- iptables/xshared.h | 4 ++ 7 files changed, 100 insertions(+), 66 deletions(-)
iptables/ip6tables.c +1 -0
@@ -669,6 +669,7 @@ int do_command6(int argc, char *argv[], char **table,struct xt_cmd_parse_ops cmd_parse_ops = { .proto_parse = ipv6_proto_parse, .post_parse = ipv6_post_parse, + .option_name = ip46t_option_name, };struct xt_cmd_parse p = { .table = *table,
iptables/iptables.c +1 -0
@@ -663,6 +663,7 @@ int do_command4(int argc, char *argv[], char **table,struct xt_cmd_parse_ops cmd_parse_ops = { .proto_parse = ipv4_proto_parse, .post_parse = ipv4_post_parse, + .option_name = ip46t_option_name, };struct xt_cmd_parse p = { .table = *table,
iptables/nft-arp.c +18 -0
@@ -815,6 +815,23 @@ static int nft_arp_xlate(const struct iptables_command_state *cs,return xlate_action (cs, false, xl); } +static const char *nft_arp_option_name (int option) +{ +switch (option) { +default :return ip46t_option_name (option); +/* different name than iptables */ +case OPT_SOURCE :return "--source-ip" ; +case OPT_DESTINATION :return "--destination-ip" ; +/* arptables specific ones */ +case OPT_S_MAC :return "--source-mac" ; +case OPT_D_MAC :return "--destination-mac" ; +case OPT_H_LENGTH :return "--h-length" ; +case OPT_OPCODE :return "--opcode" ; +case OPT_H_TYPE :return "--h-type" ; +case OPT_P_TYPE :return "--proto-type" ; + } +} +struct nft_family_ops nft_family_ops_arp = { .add = nft_arp_add, .is_same = nft_arp_is_same,
@@ -826,6 +843,7 @@ struct nft_family_ops nft_family_ops_arp = { .rule_parse = &nft_ruleparse_ops_arp, .cmd_parse = { .post_parse = nft_arp_post_parse, + .option_name = nft_arp_option_name, }, .rule_to_cs = nft_rule_to_iptables_command_state, .init_cs = nft_arp_init_cs,
iptables/nft-ipv4.c +1 -0
@@ -353,6 +353,7 @@ struct nft_family_ops nft_family_ops_ipv4 = { .cmd_parse = { .proto_parse = ipv4_proto_parse, .post_parse = ipv4_post_parse, + .option_name = ip46t_option_name, }, .rule_to_cs = nft_rule_to_iptables_command_state, .clear_cs = xtables_clear_iptables_command_state,
iptables/nft-ipv6.c +1 -0
@@ -344,6 +344,7 @@ struct nft_family_ops nft_family_ops_ipv6 = { .cmd_parse = { .proto_parse = ipv6_proto_parse, .post_parse = ipv6_post_parse, + .option_name = ip46t_option_name, }, .rule_to_cs = nft_rule_to_iptables_command_state, .clear_cs = xtables_clear_iptables_command_state,
iptables/xshared.c +74 -66
319 lines not on this page · raw message
iptables/xshared.h +4 -0
@@ -50,6 +50,7 @@ enum {OPT_COMMAND =1 <<20 , OPT_ZERO = 1 << 21, }; +#define NUMBER_OF_OPT 23enum { CMD_NONE = 0,
@@ -272,6 +273,7 @@ struct xt_cmd_parse_ops {void (*post_parse)(int command,struct iptables_command_state *cs,struct xtables_args *args); +const char *(*option_name)(int option); };struct xt_cmd_parse {
@@ -287,6 +289,8 @@ struct xt_cmd_parse {struct xt_cmd_parse_ops *ops; }; +const char *ip46t_option_name (int option); +void do_parse (int argc,char *argv[],struct xt_cmd_parse *p,struct iptables_command_state *cs,struct xtables_args *args);
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 2/3 xshared: Introduce xt_cmd_parse_ops::option_invert
In reply to Phil SutterReplace the awkward inverse_for_options array with basically a few switch() statements clearly identifying the relation between option and inverse values and relieve callers from having to find the option flag bit's position.
--- iptables/ip6tables.c | 1 + iptables/iptables.c | 1 + iptables/nft-arp.c | 14 ++++++++++++++ iptables/nft-ipv4.c | 1 + iptables/nft-ipv6.c | 1 + iptables/xshared.c | 38 ++++++++++++++------------------------ iptables/xshared.h | 2 ++ 7 files changed, 34 insertions(+), 24 deletions(-)
iptables/ip6tables.c +1 -0
@@ -670,6 +670,7 @@ int do_command6(int argc, char *argv[], char **table, .proto_parse = ipv6_proto_parse, .post_parse = ipv6_post_parse, .option_name = ip46t_option_name, + .option_invert = ip46t_option_invert, };struct xt_cmd_parse p = { .table = *table,
iptables/iptables.c +1 -0
@@ -664,6 +664,7 @@ int do_command4(int argc, char *argv[], char **table, .proto_parse = ipv4_proto_parse, .post_parse = ipv4_post_parse, .option_name = ip46t_option_name, + .option_invert = ip46t_option_invert, };struct xt_cmd_parse p = { .table = *table,
iptables/nft-arp.c +14 -0
@@ -832,6 +832,19 @@ static const char *nft_arp_option_name(int option) } } +static int nft_arp_option_invert (int option) +{ +switch (option) { +case OPT_S_MAC :return IPT_INV_SRCDEVADDR ; +case OPT_D_MAC :return IPT_INV_TGTDEVADDR ; +case OPT_H_LENGTH :return IPT_INV_ARPHLN ; +case OPT_OPCODE :return IPT_INV_ARPOP ; +case OPT_H_TYPE :return IPT_INV_ARPHRD ; +case OPT_P_TYPE :return IPT_INV_PROTO ; +default :return ip46t_option_invert (option); + } +} +struct nft_family_ops nft_family_ops_arp = { .add = nft_arp_add, .is_same = nft_arp_is_same,
@@ -844,6 +857,7 @@ struct nft_family_ops nft_family_ops_arp = { .cmd_parse = { .post_parse = nft_arp_post_parse, .option_name = nft_arp_option_name, + .option_invert = nft_arp_option_invert, }, .rule_to_cs = nft_rule_to_iptables_command_state, .init_cs = nft_arp_init_cs,
iptables/nft-ipv4.c +1 -0
@@ -354,6 +354,7 @@ struct nft_family_ops nft_family_ops_ipv4 = { .proto_parse = ipv4_proto_parse, .post_parse = ipv4_post_parse, .option_name = ip46t_option_name, + .option_invert = ip46t_option_invert, }, .rule_to_cs = nft_rule_to_iptables_command_state, .clear_cs = xtables_clear_iptables_command_state,
iptables/nft-ipv6.c +1 -0
@@ -345,6 +345,7 @@ struct nft_family_ops nft_family_ops_ipv6 = { .proto_parse = ipv6_proto_parse, .post_parse = ipv6_post_parse, .option_name = ip46t_option_name, + .option_invert = ip46t_option_invert, }, .rule_to_cs = nft_rule_to_iptables_command_state, .clear_cs = xtables_clear_iptables_command_state,
iptables/xshared.c +14 -24
@@ -1003,27 +1003,18 @@ const char *ip46t_option_name(int option) } } -static const int inverse_for_options[NUMBER_OF_OPT ] = +int ip46t_option_invert (int option) { -/* -n */ 0 , -/* -s */ IPT_INV_SRCIP , -/* -d */ IPT_INV_DSTIP , -/* -p */ XT_INV_PROTO , -/* -j */ 0 , -/* -v */ 0 , -/* -x */ 0 , -/* -i */ IPT_INV_VIA_IN , -/* -o */ IPT_INV_VIA_OUT , -/*--line*/ 0 , -/* -c */ 0 , -/* -f */ IPT_INV_FRAG , -/* 2 */ IPT_INV_SRCDEVADDR , -/* 3 */ IPT_INV_TGTDEVADDR , -/* -l */ IPT_INV_ARPHLN , -/* 4 */ IPT_INV_ARPOP , -/* 5 */ IPT_INV_ARPHRD , -/* 6 */ IPT_INV_PROTO , -}; +switch (option) { +case OPT_SOURCE :return IPT_INV_SRCIP ; +case OPT_DESTINATION :return IPT_INV_DSTIP ; +case OPT_PROTOCOL :return XT_INV_PROTO ; +case OPT_VIANAMEIN :return IPT_INV_VIA_IN ; +case OPT_VIANAMEOUT :return IPT_INV_VIA_OUT ; +case OPT_FRAGMENT :return IPT_INV_FRAG ; +default :return -1 ; + } +} static void set_option(struct xt_cmd_parse_ops *ops,
@@ -1037,14 +1028,13 @@ set_option(struct xt_cmd_parse_ops *ops, *options |= option;if (invert) { -unsigned int i; -for (i =0 ;1 << i != option; i++); +int invopt = ops->option_invert (option); -if (!inverse_for_options[i]) +if (invopt <0 )xtables_error (PARAMETER_PROBLEM ,"cannot have ! before %s" , ops->option_name (option)); - *invflg |= inverse_for_options[i]; + *invflg |= invopt; } }
iptables/xshared.h +2 -0
@@ -274,6 +274,7 @@ struct xt_cmd_parse_ops {struct iptables_command_state *cs, struct xtables_args *args);const char *(*option_name)(int option); +int (*option_invert)(int option); };struct xt_cmd_parse {
@@ -290,6 +291,7 @@ struct xt_cmd_parse { };const char *ip46t_option_name (int option); +int ip46t_option_invert (int option); void do_parse(int argc, char *argv[], struct xt_cmd_parse *p, struct iptables_command_state *cs,
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 3/3 xshared: Simplify generic_opt_check()
In reply to Phil SutterThe option/command matrix does not contain any '+' entries anymore, so each option/command combination is either allowed (and optional) or not.
Reduce the matrix to an array of unsigned ints which specify the commands a given option is allowed with.
--- iptables/xshared.c | 77 +++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 49 deletions(-)
iptables/xshared.c +28 -49
@@ -920,67 +920,46 @@ static int parse_rulenumber(const char *rule)return rulenum; } -/* Table of legal combinations of commands and options. If any of the -* given commands make an option legal, that option is legal (applies to -* CMD_LIST and CMD_ZERO only). -* Key: -* + compulsory -* x illegal -* optional -*/ -static const char commands_v_options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*DEL_CHAIN*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*SET_POLICY*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*RENAME*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*LIST_RULES*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*ZERO_NUM*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*CHECK*/ {'x' ,' ' ,' ' ,' ' ,' ' ,' ' ,'x' ,' ' ,' ' ,'x' ,'x' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, +/* list the commands an option is allowed with */ +#define CMD_IDRAC CMD_INSERT | CMD_DELETE | CMD_REPLACE | \ + CMD_APPEND | CMD_CHECK +static const unsigned int options_v_commands[NUMBER_OF_OPT ] = { +/*OPT_NUMERIC*/ CMD_LIST , +/*OPT_SOURCE*/ CMD_IDRAC , +/*OPT_DESTINATION*/ CMD_IDRAC , +/*OPT_PROTOCOL*/ CMD_IDRAC , +/*OPT_JUMP*/ CMD_IDRAC , +/*OPT_VERBOSE*/ UINT_MAX , +/*OPT_EXPANDED*/ CMD_LIST , +/*OPT_VIANAMEIN*/ CMD_IDRAC , +/*OPT_VIANAMEOUT*/ CMD_IDRAC , +/*OPT_LINENUMBERS*/ CMD_LIST , +/*OPT_COUNTERS*/ CMD_INSERT |CMD_REPLACE |CMD_APPEND |CMD_SET_POLICY , +/*OPT_FRAGMENT*/ CMD_IDRAC , +/*OPT_S_MAC*/ CMD_IDRAC , +/*OPT_D_MAC*/ CMD_IDRAC , +/*OPT_H_LENGTH*/ CMD_IDRAC , +/*OPT_OPCODE*/ CMD_IDRAC , +/*OPT_H_TYPE*/ CMD_IDRAC , +/*OPT_P_TYPE*/ CMD_IDRAC , }; +#undef CMD_IDRACstatic void generic_opt_check (struct xt_cmd_parse_ops *ops,int command,int options) { -int i, j, legal =0 ; +int i, optval;/* Check that commands are valid with options. Complicated by the * fact that if an option is legal with *any* command given, it is * legal overall (ie. -z and -l). */ -for (i =0 ; i <NUMBER_OF_OPT ; i++) { - legal =0 ;/* -1 => illegal, 1 => legal, 0 => undecided. */ - -for (j =0 ; j <NUMBER_OF_CMD ; j++) { -if (!(command & (1 <<j))) -continue ; - -if (!(options & (1 <<i))) { -if (commands_v_options[j][i] =='+' ) -xtables_error (PARAMETER_PROBLEM , -"You need to supply the `%s' option for this command" , - ops->option_name (1 <<i)); - }else { -if (commands_v_options[j][i] !='x' ) - legal =1 ; -else if (legal ==0 ) - legal =-1 ; - } - } -if (legal ==-1 ) +for (i =0 , optval =1 ; i <NUMBER_OF_OPT ; optval = (1 << ++i)) { +if ((options & optval) && + (options_v_commands[i] & command) != command)xtables_error (PARAMETER_PROBLEM ,"Illegal option `%s' with this command" , - ops->option_name (1 <<i)); + ops->option_name (optval)); } }
Signature · 1 line
2.41.0
Phil Sutter
reviewerQuoted 14 lines · Phil Sutter
Faced with the need to extend optflags,
inverse_for_optionsandcommands_v_optionsarrays in order to integrate ebtables-specific options, I chose to get rid of them all instead.Patch 1 replaces
opt2char()function which didn't work well since not every option has printable short-option character. The callback-based replacement returns the long-option and should make error messages more readable this way.Patch 2 elminates the
inverse_for_optionsarray along with the loop turning a given option into its bit's position for use in the array. The switch statement in the replacing callback is much easier to maintain and extend.
Quoted 8 lines
Patch 3 makes use of the fact that no command has a mandatory option anymore. So every combination is either allowed or not, and a single bit may indicate that.
Phil Sutter (3):
xshared: Introducext_cmd_parse_ops::option_name
xshared: Introducext_cmd_parse_ops::option_invert
xshared: Simplifygeneric_opt_check()
Series applied.