Reduce code size around arptables-nft
Phil Sutter
reviewercover Reduce code size around arptables-nft
A review of xtables-arp.c exposed a significant amount of dead, needless or duplicated code. This series deals with some low hanging fruits. Most of the changes affect xtables-arp.c and nft-arp.c only, but where common issues existed or code was to be shared, other files are touched as well.
Phil Sutter (10):
ip6tables, xtables-arp: Drop unused struct pprot
xshared: Share a common add_command() implementation
xshared: Share a common implementation of parse_rulenumber() Merge CMD_* defines
xtables-arp: Drop generic_opt_check()
Replace TRUE/FALSE with true/false
xtables-arp: Integrate OPT_* defines into xshared.h
xtables-arp: Drop some unused variables
xtables-arp: Use xtables_parse_interface()
nft-arp: Use xtables_print_mac_and_mask()
iptables/ip6tables.c | 73 +------------ iptables/iptables.c | 64 +---------- iptables/nft-arp.c | 31 +----- iptables/nft-shared.h | 17 --- iptables/xshared.c | 39 +++++++ iptables/xshared.h | 32 ++++++ iptables/xtables-arp.c | 237 +++-------------------------------------- iptables/xtables.c | 48 +-------- 8 files changed, 97 insertions(+), 444 deletions(-)
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 1/10 ip6tables, xtables-arp: Drop unused struct pprot
These seem like leftovers when changing code to use xtables_chain_protos as struct xtables_pprot is identical to struct pprot removed here.
--- iptables/ip6tables.c | 6 ------ iptables/xtables-arp.c | 7 ------- 2 files changed, 13 deletions(-)
iptables/ip6tables.c +0 -6
@@ -175,12 +175,6 @@ static const unsigned int inverse_for_options[NUMBER_OF_OPT] =#define opts ip6tables_globals.opts#define prog_name ip6tables_globals.program_name#define prog_vers ip6tables_globals.program_version -/* A few hardcoded protocols for 'all' and in case the user has no -/etc/protocols */ -struct pprot { -const char *name; -uint8_t num; -};static void __attribute__((noreturn))exit_tryhelp (int status)
iptables/xtables-arp.c +0 -7
@@ -209,13 +209,6 @@ static int inverse_for_options[NUMBER_OF_OPT] = /* -c */ 0, }; -/* A few hardcoded protocols for 'all' and in case the user has no -/etc/protocols */ -struct pprot { -char *name; -u_int8_t num; -}; -/* Primitive headers... */ /* defined in netinet/in.h */ #if 0
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 2/10 xshared: Share a common add_command() implementation
In reply to Phil SutterThe shared definition of cmdflags is a super set of the previous one in xtables-arp.c so while not being identical, they're compatible.
Avoid accidental array overstep in cmd2char() by incrementing an index variable and checking its final value before using it as such.
--- iptables/ip6tables.c | 23 ----------------------- iptables/iptables.c | 23 ----------------------- iptables/xshared.c | 27 +++++++++++++++++++++++++++ iptables/xshared.h | 4 ++++ iptables/xtables-arp.c | 22 ---------------------- iptables/xtables.c | 23 ----------------------- 6 files changed, 31 insertions(+), 91 deletions(-)
iptables/ip6tables.c +0 -23
@@ -69,8 +69,6 @@#define CMD_ZERO_NUM 0x2000U#define CMD_CHECK 0x4000U#define NUMBER_OF_CMD 16 -static const char cmdflags[] = {'I' ,'D' ,'D' ,'R' ,'A' ,'L' ,'F' ,'Z' , -'N' ,'X' ,'P' ,'E' ,'S' ,'Z' ,'C' };#define NUMBER_OF_OPT ARRAY_SIZE(optflags)static const char optflags[]
@@ -336,27 +334,6 @@ opt2char(int option)return *ptr; } -static char -cmd2char (int option) -{ -const char *ptr; -for (ptr = cmdflags; option >1 ; option >>=1 , ptr++); - -return *ptr; -} - -static void -add_command (unsigned int *cmd,const int newcmd,const int othercmds, -int invert) -{ -if (invert) -xtables_error (PARAMETER_PROBLEM ,"unexpected '!' flag" ); -if (*cmd & (~othercmds)) -xtables_error (PARAMETER_PROBLEM ,"Cannot use -%c with -%c\n" , -cmd2char (newcmd),cmd2char (*cmd & (~othercmds))); - *cmd |= newcmd; -} -/* * All functions starting with "parse" should succeed, otherwise * the program fails.
iptables/iptables.c +0 -23
@@ -65,8 +65,6 @@#define CMD_ZERO_NUM 0x2000U#define CMD_CHECK 0x4000U#define NUMBER_OF_CMD 16 -static const char cmdflags[] = {'I' ,'D' ,'D' ,'R' ,'A' ,'L' ,'F' ,'Z' , -'N' ,'X' ,'P' ,'E' ,'S' ,'Z' ,'C' };#define OPT_FRAGMENT 0x00800U#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
@@ -335,27 +333,6 @@ opt2char(int option)return *ptr; } -static char -cmd2char (int option) -{ -const char *ptr; -for (ptr = cmdflags; option >1 ; option >>=1 , ptr++); - -return *ptr; -} - -static void -add_command (unsigned int *cmd,const int newcmd,const int othercmds, -int invert) -{ -if (invert) -xtables_error (PARAMETER_PROBLEM ,"unexpected ! flag" ); -if (*cmd & (~othercmds)) -xtables_error (PARAMETER_PROBLEM ,"Cannot use -%c with -%c\n" , -cmd2char (newcmd),cmd2char (*cmd & (~othercmds))); - *cmd |= newcmd; -} -/* * All functions starting with "parse" should succeed, otherwise * the program fails.
iptables/xshared.c +27 -0
@@ -732,3 +732,30 @@ void command_jump(struct iptables_command_state *cs, const char *jumpto)xtables_error (OTHER_PROBLEM ,"can't alloc memory!" ); xt_params->opts = opts; } + +char cmd2char (int option) +{ +/* cmdflags index corresponds with position of bit in CMD_* values */ +static const char cmdflags[] = {'I' ,'D' ,'D' ,'R' ,'A' ,'L' ,'F' ,'Z' , +'N' ,'X' ,'P' ,'E' ,'S' ,'Z' ,'C' }; +int i; + +for (i =0 ; option >1 ; option >>=1 , i++) + ; +if (i >=ARRAY_SIZE (cmdflags)) +xtables_error (OTHER_PROBLEM , +"cmd2char(): Invalid command number %u.\n" , +1 << i); +return cmdflags[i]; +} + +void add_command (unsigned int *cmd,const int newcmd, +const int othercmds,int invert) +{ +if (invert) +xtables_error (PARAMETER_PROBLEM ,"unexpected '!' flag" ); +if (*cmd & (~othercmds)) +xtables_error (PARAMETER_PROBLEM ,"Cannot use -%c with -%c\n" , +cmd2char (newcmd),cmd2char (*cmd & (~othercmds))); + *cmd |= newcmd; +}
iptables/xshared.h +4 -0
@@ -183,4 +183,8 @@ void command_match(struct iptables_command_state *cs);const char *xt_parse_target (const char *targetname);void command_jump (struct iptables_command_state *cs,const char *jumpto); +char cmd2char (int option); +void add_command (unsigned int *cmd,const int newcmd, +const int othercmds,int invert); +#endif /* IPTABLES_XSHARED_H */
iptables/xtables-arp.c +0 -22
@@ -81,8 +81,6 @@ typedef char arpt_chainlabel[32];#define CMD_CHECK 0x0800U#define CMD_RENAME_CHAIN 0x1000U#define NUMBER_OF_CMD 13 -static const char cmdflags[] = {'I' ,'D' ,'D' ,'R' ,'A' ,'L' ,'F' ,'Z' , -'N' ,'X' ,'P' ,'E' };#define OPTION_OFFSET 256
@@ -462,26 +460,6 @@ opt2char(int option)return *ptr; } -static char -cmd2char (int option) -{ -const char *ptr; -for (ptr = cmdflags; option >1 ; option >>=1 , ptr++); - -return *ptr; -} - -static void -add_command (unsigned int *cmd,const int newcmd,const unsigned int othercmds,int invert) -{ -if (invert) -xtables_error (PARAMETER_PROBLEM ,"unexpected ! flag" ); -if (*cmd & (~othercmds)) -xtables_error (PARAMETER_PROBLEM ,"Can't use -%c with -%c\n" , -cmd2char (newcmd),cmd2char (*cmd & (~othercmds))); - *cmd |= newcmd; -} -static int check_inverse (const char option[],int *invert,int *optidx,int argc) {
iptables/xtables.c +0 -23
@@ -51,8 +51,6 @@#endif #define NUMBER_OF_CMD 16 -static const char cmdflags[] = {'I' ,'D' ,'D' ,'R' ,'A' ,'L' ,'F' ,'Z' , -'N' ,'X' ,'P' ,'E' ,'S' ,'Z' ,'C' };#define OPT_FRAGMENT 0x00800U#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
@@ -319,27 +317,6 @@ opt2char(int option)return *ptr; } -static char -cmd2char (int option) -{ -const char *ptr; -for (ptr = cmdflags; option >1 ; option >>=1 , ptr++); - -return *ptr; -} - -static void -add_command (unsigned int *cmd,const int newcmd,const int othercmds, -int invert) -{ -if (invert) -xtables_error (PARAMETER_PROBLEM ,"unexpected ! flag" ); -if (*cmd & (~othercmds)) -xtables_error (PARAMETER_PROBLEM ,"Cannot use -%c with -%c\n" , -cmd2char (newcmd),cmd2char (*cmd & (~othercmds))); - *cmd |= newcmd; -} -/* * All functions starting with "parse" should succeed, otherwise * the program fails.
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 3/10 xshared: Share a common implementation of parse_rulenumber()
In reply to Phil SutterThe function is really small, but still copied four times.
--- iptables/ip6tables.c | 13 ------------- iptables/iptables.c | 12 ------------ iptables/xshared.c | 12 ++++++++++++ iptables/xshared.h | 1 + iptables/xtables-arp.c | 13 ------------- iptables/xtables.c | 12 ------------ 6 files changed, 13 insertions(+), 50 deletions(-)
iptables/ip6tables.c +0 -13
@@ -352,19 +352,6 @@ static int is_exthdr(uint16_t proto) proto == IPPROTO_DSTOPTS); } -/* Can't be zero. */ -static int -parse_rulenumber (const char *rule) -{ -unsigned int rulenum; - -if (!xtables_strtoui (rule,NULL , &rulenum,1 ,INT_MAX )) -xtables_error (PARAMETER_PROBLEM , -"Invalid rule number `%s'" , rule); - -return rulenum; -} -static void parse_chain (const char *chainname) {
iptables/iptables.c +0 -12
@@ -343,18 +343,6 @@ opt2char(int option)*/ /* Christophe Burki wants `-p 6' to imply `-m tcp'. */ -/* Can't be zero. */ -static int -parse_rulenumber (const char *rule) -{ -unsigned int rulenum; - -if (!xtables_strtoui (rule,NULL , &rulenum,1 ,INT_MAX )) -xtables_error (PARAMETER_PROBLEM , -"Invalid rule number `%s'" , rule); - -return rulenum; -}static void parse_chain (const char *chainname)
iptables/xshared.c +12 -0
@@ -759,3 +759,15 @@ void add_command(unsigned int *cmd, const int newcmd, cmd2char(newcmd), cmd2char(*cmd & (~othercmds))); *cmd |= newcmd; } + +/* Can't be zero. */ +int parse_rulenumber (const char *rule) +{ +unsigned int rulenum; + +if (!xtables_strtoui (rule,NULL , &rulenum,1 ,INT_MAX )) +xtables_error (PARAMETER_PROBLEM , +"Invalid rule number `%s'" , rule); + +return rulenum; +}
iptables/xshared.h +1 -0
@@ -186,5 +186,6 @@ void command_jump(struct iptables_command_state *cs, const char *jumpto);char cmd2char (int option);void add_command (unsigned int *cmd,const int newcmd,const int othercmds,int invert); +int parse_rulenumber (const char *rule);#endif /* IPTABLES_XSHARED_H */
iptables/xtables-arp.c +0 -13
@@ -518,19 +518,6 @@ parse_interface(const char *arg, char *vianame, unsigned char *mask) } } -/* Can't be zero. */ -static int -parse_rulenumber (const char *rule) -{ -unsigned int rulenum; - -if (!xtables_strtoui (rule,NULL , &rulenum,1 ,INT_MAX )) -xtables_error (PARAMETER_PROBLEM , -"Invalid rule number `%s'" , rule); - -return rulenum; -} -static void set_option (unsigned int *options,unsigned int option,u_int16_t *invflg,int invert)
iptables/xtables.c +0 -12
@@ -327,18 +327,6 @@ opt2char(int option)*/ /* Christophe Burki wants `-p 6' to imply `-m tcp'. */ -/* Can't be zero. */ -static int -parse_rulenumber (const char *rule) -{ -unsigned int rulenum; - -if (!xtables_strtoui (rule,NULL , &rulenum,1 ,INT_MAX )) -xtables_error (PARAMETER_PROBLEM , -"Invalid rule number `%s'" , rule); - -return rulenum; -} static void set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 4/10 Merge CMD_* defines
In reply to Phil SutterThey are mostly identical, just xtables-arp ones differ slightly. Though since they are internal use only and their actual value doesn't matter (as long as it's a distinct bit), they can be merged anyway.
--- iptables/ip6tables.c | 17 ----------------- iptables/iptables.c | 17 ----------------- iptables/nft-shared.h | 17 ----------------- iptables/xshared.h | 20 ++++++++++++++++++++ iptables/xtables-arp.c | 20 -------------------- iptables/xtables.c | 2 -- 6 files changed, 20 insertions(+), 73 deletions(-)
iptables/ip6tables.c +0 -17
@@ -52,23 +52,6 @@#define FALSE 0#endif -#define CMD_NONE 0x0000U -#define CMD_INSERT 0x0001U -#define CMD_DELETE 0x0002U -#define CMD_DELETE_NUM 0x0004U -#define CMD_REPLACE 0x0008U -#define CMD_APPEND 0x0010U -#define CMD_LIST 0x0020U -#define CMD_FLUSH 0x0040U -#define CMD_ZERO 0x0080U -#define CMD_NEW_CHAIN 0x0100U -#define CMD_DELETE_CHAIN 0x0200U -#define CMD_SET_POLICY 0x0400U -#define CMD_RENAME_CHAIN 0x0800U -#define CMD_LIST_RULES 0x1000U -#define CMD_ZERO_NUM 0x2000U -#define CMD_CHECK 0x4000U -#define NUMBER_OF_CMD 16#define NUMBER_OF_OPT ARRAY_SIZE(optflags)static const char optflags[]
iptables/iptables.c +0 -17
@@ -48,23 +48,6 @@#define FALSE 0#endif -#define CMD_NONE 0x0000U -#define CMD_INSERT 0x0001U -#define CMD_DELETE 0x0002U -#define CMD_DELETE_NUM 0x0004U -#define CMD_REPLACE 0x0008U -#define CMD_APPEND 0x0010U -#define CMD_LIST 0x0020U -#define CMD_FLUSH 0x0040U -#define CMD_ZERO 0x0080U -#define CMD_NEW_CHAIN 0x0100U -#define CMD_DELETE_CHAIN 0x0200U -#define CMD_SET_POLICY 0x0400U -#define CMD_RENAME_CHAIN 0x0800U -#define CMD_LIST_RULES 0x1000U -#define CMD_ZERO_NUM 0x2000U -#define CMD_CHECK 0x4000U -#define NUMBER_OF_CMD 16#define OPT_FRAGMENT 0x00800U#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
iptables/nft-shared.h +0 -17
@@ -199,23 +199,6 @@ struct xtables_args {unsigned long long pcnt_cnt, bcnt_cnt; }; -#define CMD_NONE 0x0000U -#define CMD_INSERT 0x0001U -#define CMD_DELETE 0x0002U -#define CMD_DELETE_NUM 0x0004U -#define CMD_REPLACE 0x0008U -#define CMD_APPEND 0x0010U -#define CMD_LIST 0x0020U -#define CMD_FLUSH 0x0040U -#define CMD_ZERO 0x0080U -#define CMD_NEW_CHAIN 0x0100U -#define CMD_DELETE_CHAIN 0x0200U -#define CMD_SET_POLICY 0x0400U -#define CMD_RENAME_CHAIN 0x0800U -#define CMD_LIST_RULES 0x1000U -#define CMD_ZERO_NUM 0x2000U -#define CMD_CHECK 0x4000U -struct nft_xt_cmd_parse {unsigned int command;unsigned int rulenum;
iptables/xshared.h +20 -0
@@ -31,6 +31,26 @@ enum { OPT_COUNTERS = 1 << 10, }; +enum { +CMD_NONE =0 , +CMD_INSERT =1 <<0 , +CMD_DELETE =1 <<1 , +CMD_DELETE_NUM =1 <<2 , +CMD_REPLACE =1 <<3 , +CMD_APPEND =1 <<4 , +CMD_LIST =1 <<5 , +CMD_FLUSH =1 <<6 , +CMD_ZERO =1 <<7 , +CMD_NEW_CHAIN =1 <<8 , +CMD_DELETE_CHAIN =1 <<9 , +CMD_SET_POLICY =1 <<10 , +CMD_RENAME_CHAIN =1 <<11 , +CMD_LIST_RULES =1 <<12 , +CMD_ZERO_NUM =1 <<13 , +CMD_CHECK =1 <<14 , +}; +#define NUMBER_OF_CMD 16 +struct xtables_globals ;struct xtables_rule_match ;struct xtables_target ;
iptables/xtables-arp.c +0 -20
@@ -62,26 +62,6 @@ typedef char arpt_chainlabel[32];#define FALSE 0#endif -/* XXX: command defined by nft-shared.h do not overlap with these two */ -#undef CMD_CHECK -#undef CMD_RENAME_CHAIN - -#define CMD_NONE 0x0000U -#define CMD_INSERT 0x0001U -#define CMD_DELETE 0x0002U -#define CMD_DELETE_NUM 0x0004U -#define CMD_REPLACE 0x0008U -#define CMD_APPEND 0x0010U -#define CMD_LIST 0x0020U -#define CMD_FLUSH 0x0040U -#define CMD_ZERO 0x0080U -#define CMD_NEW_CHAIN 0x0100U -#define CMD_DELETE_CHAIN 0x0200U -#define CMD_SET_POLICY 0x0400U -#define CMD_CHECK 0x0800U -#define CMD_RENAME_CHAIN 0x1000U -#define NUMBER_OF_CMD 13 -#define OPTION_OFFSET 256#define OPT_NONE 0x00000U
iptables/xtables.c +0 -2
@@ -50,8 +50,6 @@#define FALSE 0#endif -#define NUMBER_OF_CMD 16 -#define OPT_FRAGMENT 0x00800U#define NUMBER_OF_OPT ARRAY_SIZE(optflags)static const char optflags[]
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 5/10 xtables-arp: Drop generic_opt_check()
In reply to Phil SutterWith all fields in commands_v_options[][] being whitespace, the function is effectively a noop.
--- iptables/xtables-arp.c | 66 ------------------------------------------ 1 file changed, 66 deletions(-)
iptables/xtables-arp.c +0 -66
@@ -139,34 +139,6 @@ struct xtables_globals arptables_globals = { .compat_rev = nft_compatible_revision, }; -/* 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 char commands_v_options[NUMBER_OF_CMD ][NUMBER_OF_OPT ] = -/* Well, it's better than "Re: Linux vs FreeBSD" */ -{ -/* -n -s -d -p -j -v -x -i -o -f --line */ -/*INSERT*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*DELETE*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*DELETE_NUM*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*REPLACE*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*APPEND*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*LIST*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*FLUSH*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*ZERO*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*NEW_CHAIN*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*DEL_CHAIN*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*SET_POLICY*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*CHECK*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' }, -/*RENAME*/ {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' } -}; -static int inverse_for_options[NUMBER_OF_OPT ] = {/* -n */ 0 ,
@@ -395,42 +367,6 @@ exit_printhelp(void)exit (0 ); } -static void -generic_opt_check (int command,int options) -{ -int i, j, legal =0 ; - -/* 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 `-%c' " -"option for this command\n" , - optflags[i]); - }else { -if (commands_v_options[j][i] !='x' ) - legal =1 ; -else if (legal ==0 ) - legal =-1 ; - } - } -if (legal ==-1 ) -xtables_error (PARAMETER_PROBLEM , -"Illegal option `-%c' with this command\n" , - optflags[i]); - } -} -static char opt2char (int option) {
@@ -1059,8 +995,6 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table,xtables_error (PARAMETER_PROBLEM ,"Replacement rule does not " "specify a unique address" ); -generic_opt_check (command, options); -if (chain &&strlen (chain) >ARPT_FUNCTION_MAXNAMELEN ) xtables_error(PARAMETER_PROBLEM, "chain name `%s' too long (must be under %i chars)",
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 6/10 Replace TRUE/FALSE with true/false
In reply to Phil SutterAnd drop the conditional defines.
--- iptables/ip6tables.c | 14 +++----------- iptables/iptables.c | 12 ++---------- iptables/xtables-arp.c | 17 +++++------------ iptables/xtables.c | 11 ++--------- 4 files changed, 12 insertions(+), 42 deletions(-)
iptables/ip6tables.c +3 -11
@@ -45,14 +45,6 @@#include "ip6tables-multi.h" #include "xshared.h" -#ifndef TRUE -#define TRUE 1 -#endif -#ifndef FALSE -#define FALSE 0 -#endif - -#define NUMBER_OF_OPT ARRAY_SIZE(optflags)static const char optflags[] = {'n' ,'s' ,'d' ,'p' ,'j' ,'v' ,'x' ,'i' ,'o' ,'0' ,'c' };
@@ -1525,7 +1517,7 @@ int do_command6(int argc, char *argv[], char **table,xtables_error (PARAMETER_PROBLEM ,"multiple consecutive ! not" " allowed" ); - cs.invert = TRUE; + cs.invert = true; optarg[0 ] ='\0' ;continue ; }
@@ -1537,12 +1529,12 @@ int do_command6(int argc, char *argv[], char **table,/* * If new options were loaded, we must retry * getopt immediately and not allow -* cs.invert=FALSE to be executed. +* cs.invert=false to be executed. */ continue ;break ; } - cs.invert = FALSE; + cs.invert = false; }if (!wait && wait_interval_set)
iptables/iptables.c +2 -10
@@ -41,14 +41,6 @@#include <fcntl.h> #include "xshared.h" -#ifndef TRUE -#define TRUE 1 -#endif -#ifndef FALSE -#define FALSE 0 -#endif - -#define OPT_FRAGMENT 0x00800U#define NUMBER_OF_OPT ARRAY_SIZE(optflags)static const char optflags[]
@@ -1518,7 +1510,7 @@ int do_command4(int argc, char *argv[], char **table,xtables_error (PARAMETER_PROBLEM ,"multiple consecutive ! not" " allowed" ); - cs.invert = TRUE; + cs.invert = true; optarg[0 ] ='\0' ;continue ; }
@@ -1531,7 +1523,7 @@ int do_command4(int argc, char *argv[], char **table,continue ;break ; } - cs.invert = FALSE; + cs.invert = false; }if (!wait && wait_interval_set)
iptables/xtables-arp.c +5 -12
@@ -55,13 +55,6 @@typedef char arpt_chainlabel [32 ]; -#ifndef TRUE -#define TRUE 1 -#endif -#ifndef FALSE -#define FALSE 0 -#endif -#define OPTION_OFFSET 256#define OPT_NONE 0x00000U
@@ -383,7 +376,7 @@ check_inverse(const char option[], int *invert, int *optidx, int argc)if (*invert)xtables_error (PARAMETER_PROBLEM ,"Multiple `!' flags not allowed" ); - *invert = TRUE; + *invert = true;if (optidx) { *optidx = *optidx+1 ;if (argc && *optidx > argc)
@@ -391,9 +384,9 @@ check_inverse(const char option[], int *invert, int *optidx, int argc) "no argument following `!'"); } -return TRUE; +return true; } -return FALSE; +return false; } static void
@@ -942,7 +935,7 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table,xtables_error (PARAMETER_PROBLEM ,"multiple consecutive ! not" " allowed" ); - invert = TRUE; + invert = true; optarg[0 ] ='\0' ;continue ; }
@@ -956,7 +949,7 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table, }break ; } - invert = FALSE; + invert = false; }if (cs.target)
iptables/xtables.c +2 -9
@@ -43,13 +43,6 @@#include "nft-shared.h" #include "nft.h" -#ifndef TRUE -#define TRUE 1 -#endif -#ifndef FALSE -#define FALSE 0 -#endif -#define OPT_FRAGMENT 0x00800U#define NUMBER_OF_OPT ARRAY_SIZE(optflags)static const char optflags[]
@@ -952,7 +945,7 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],xtables_error (PARAMETER_PROBLEM ,"multiple consecutive ! not" " allowed" ); - cs->invert = TRUE; + cs->invert = true; optarg[0 ] ='\0' ;continue ; }
@@ -965,7 +958,7 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],continue ;break ; } - cs->invert = FALSE; + cs->invert = false; } if (strcmp(p->table, "nat") == 0 &&
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 7/10 xtables-arp: Integrate OPT_* defines into xshared.h
In reply to Phil SutterThese defines are internal use only, so their actual value doesn't matter as long as they're unique.
--- iptables/xshared.h | 7 +++++++ iptables/xtables-arp.c | 25 ++++--------------------- 2 files changed, 11 insertions(+), 21 deletions(-)
iptables/xshared.h +7 -0
@@ -29,6 +29,13 @@ enum {OPT_VIANAMEOUT =1 <<8 ,OPT_LINENUMBERS =1 <<9 ,OPT_COUNTERS =1 <<10 , +/* below are for arptables only */ +OPT_S_MAC =1 <<11 , +OPT_D_MAC =1 <<12 , +OPT_H_LENGTH =1 <<13 , +OPT_OPCODE =1 <<14 , +OPT_H_TYPE =1 <<15 , + OPT_P_TYPE = 1 << 16, }; enum {
iptables/xtables-arp.c +4 -21
@@ -57,23 +57,6 @@ typedef char arpt_chainlabel[32];#define OPTION_OFFSET 256 -#define OPT_NONE 0x00000U -#define OPT_NUMERIC 0x00001U -#define OPT_S_IP 0x00002U -#define OPT_D_IP 0x00004U -#define OPT_S_MAC 0x00008U -#define OPT_D_MAC 0x00010U -#define OPT_H_LENGTH 0x00020U -#define OPT_P_LENGTH 0x00040U -#define OPT_OPCODE 0x00080U -#define OPT_H_TYPE 0x00100U -#define OPT_P_TYPE 0x00200U -#define OPT_JUMP 0x00400U -#define OPT_VERBOSE 0x00800U -#define OPT_VIANAMEIN 0x01000U -#define OPT_VIANAMEOUT 0x02000U -#define OPT_LINENUMBERS 0x04000U -#define OPT_COUNTERS 0x08000U#define NUMBER_OF_OPT 16static const char optflags[NUMBER_OF_OPT ] = {'n' ,'s' ,'d' ,2 ,3 ,7 ,8 ,4 ,5 ,6 ,'j' ,'v' ,'i' ,'o' ,'0' ,'c' };
@@ -747,14 +730,14 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table,break ;case 's' :check_inverse (optarg, &invert, &optind, argc); -set_option (&options,OPT_S_IP , &cs.arp.arp.invflags, +set_option (&options,OPT_SOURCE , &cs.arp.arp.invflags, invert); shostnetworkmask = argv[optind-1 ];break ;case 'd' :check_inverse (optarg, &invert, &optind, argc); -set_option (&options,OPT_D_IP , &cs.arp.arp.invflags, +set_option (&options,OPT_DESTINATION , &cs.arp.arp.invflags, invert); dhostnetworkmask = argv[optind-1 ];break ;
@@ -965,9 +948,9 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table, "nothing appropriate following !");if (command & (CMD_REPLACE |CMD_INSERT |CMD_DELETE |CMD_APPEND )) { -if (!(options &OPT_D_IP )) +if (!(options &OPT_DESTINATION )) dhostnetworkmask ="0.0.0.0/0" ; -if (!(options &OPT_S_IP )) +if (!(options &OPT_SOURCE )) shostnetworkmask ="0.0.0.0/0" ; }
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 8/10 xtables-arp: Drop some unused variables
In reply to Phil Sutter--- iptables/xtables-arp.c | 17 ----------------- 1 file changed, 17 deletions(-)
iptables/xtables-arp.c +0 -17
@@ -53,10 +53,6 @@#include "nft-arp.h" #include <linux/netfilter_arp/arp_tables.h> -typedef char arpt_chainlabel [32 ]; - -#define OPTION_OFFSET 256 -#define NUMBER_OF_OPT 16static const char optflags[NUMBER_OF_OPT ] = {'n' ,'s' ,'d' ,2 ,3 ,7 ,8 ,4 ,5 ,6 ,'j' ,'v' ,'i' ,'o' ,'0' ,'c' };
@@ -102,8 +98,6 @@ static struct option original_opts[] = { { 0 } }; -int RUNTIME_NF_ARP_NUMHOOKS =3 ; -#define opts xt_params->optsextern void xtables_exit_error (enum xtables_exittype status,const char *msg, ...) __attribute__((noreturn,format (printf,2 ,3 )));
@@ -135,17 +129,6 @@ static int inverse_for_options[NUMBER_OF_OPT] = /* -c */ 0, }; -/* Primitive headers... */ -/* defined in netinet/in.h */ -#if 0 -#ifndef IPPROTO_ESP -#define IPPROTO_ESP 50 -#endif -#ifndef IPPROTO_AH -#define IPPROTO_AH 51 -#endif -#endif -/***********************************************/ /* ARPTABLES SPECIFIC NEW FUNCTIONS ADDED HERE */ /***********************************************/
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 9/10 xtables-arp: Use xtables_parse_interface()
In reply to Phil SutterThe local implementation differs just slightly but libxtables version seems more correct (no needless memsetting of mask, more relevant illegal character checking) so use that one.
--- iptables/xtables-arp.c | 50 +++++------------------------------------- 1 file changed, 6 insertions(+), 44 deletions(-)
iptables/xtables-arp.c +6 -44
@@ -355,44 +355,6 @@ check_inverse(const char option[], int *invert, int *optidx, int argc)return false; } -static void -parse_interface (const char *arg,char *vianame,unsigned char *mask) -{ -int vialen =strlen (arg); -unsigned int i; - -memset (mask,0 ,IFNAMSIZ ); -memset (vianame,0 ,IFNAMSIZ ); - -if (vialen +1 >IFNAMSIZ ) -xtables_error (PARAMETER_PROBLEM , -"interface name `%s' must be shorter than IFNAMSIZ" -" (%i)" , arg,IFNAMSIZ -1 ); - -strcpy (vianame, arg); -if (vialen ==0 ) -memset (mask,0 ,IFNAMSIZ ); -else if (vianame[vialen -1 ] =='+' ) { -memset (mask,0xFF , vialen -1 ); -memset (mask + vialen -1 ,0 ,IFNAMSIZ - vialen +1 ); -/* Don't remove `+' here! -HW */ - }else { -/* Include nul-terminator in match */ -memset (mask,0xFF , vialen +1 ); -memset (mask + vialen +1 ,0 ,IFNAMSIZ - vialen -1 ); -for (i =0 ; vianame[i]; i++) { -if (!isalnum (vianame[i]) - && vianame[i] !='_' - && vianame[i] !='.' ) { -printf ("Warning: weird character in interface" -" `%s' (No aliases, :, ! or *).\n" , - vianame); -break ; - } - } - } -} -static void set_option (unsigned int *options,unsigned int option,u_int16_t *invflg,int invert)
@@ -814,18 +776,18 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table,check_inverse (optarg, &invert, &optind, argc);set_option (&options,OPT_VIANAMEIN , &cs.arp.arp.invflags, invert); -parse_interface (argv[optind-1 ], - cs.arp.arp.iniface, - cs.arp.arp.iniface_mask); +xtables_parse_interface (argv[optind-1 ], + cs.arp.arp.iniface, + cs.arp.arp.iniface_mask);break ;case 'o' :check_inverse (optarg, &invert, &optind, argc);set_option (&options,OPT_VIANAMEOUT , &cs.arp.arp.invflags, invert); -parse_interface (argv[optind-1 ], - cs.arp.arp.outiface, - cs.arp.arp.outiface_mask); +xtables_parse_interface (argv[optind-1 ], + cs.arp.arp.outiface, + cs.arp.arp.outiface_mask);break ;case 'v' :
Signature · 1 line
2.23.0
Phil Sutter
reviewerPATCH 10/10 nft-arp: Use xtables_print_mac_and_mask()
In reply to Phil SutterThis libxtables function does exactly what the local implementation did. The only noteworthy difference is that it assumes MAC/mask lengths, but the local implementation was passed ETH_ALEN in each invocation, so no practical difference.
--- iptables/nft-arp.c | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-)
iptables/nft-arp.c +4 -27
@@ -114,29 +114,6 @@ mask_to_dotted(const struct in_addr *mask)return buf; } -static void print_mac (const unsigned char *mac,int l) -{ -int j; - -for (j =0 ; j < l; j++) -printf ("%02x%s" , mac[j], - (j==l-1 ) ?"" :":" ); -} - -static void print_mac_and_mask (const unsigned char *mac,const unsigned char *mask,int l) -{ -int i; - -print_mac (mac, l); -for (i =0 ; i < l ; i++) -if (mask[i] !=255 ) -break ; -if (i == l) -return ; -printf ("/" ); -print_mac (mask, l); -} -static bool need_devaddr (struct arpt_devaddr_info *info) {int i;
@@ -506,8 +483,8 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,printf ("%s%s" , sep, fw->arp.invflags &ARPT_INV_SRCDEVADDR ?"! " :"" );printf ("--src-mac " ); -print_mac_and_mask ((unsigned char *)fw->arp.src_devaddr.addr, - (unsigned char *)fw->arp.src_devaddr.mask,ETH_ALEN ); +xtables_print_mac_and_mask ((unsigned char *)fw->arp.src_devaddr.addr, + (unsigned char *)fw->arp.src_devaddr.mask); sep =" " ; after_devsrc:
@@ -532,8 +509,8 @@ after_devsrc:printf ("%s%s" , sep, fw->arp.invflags &ARPT_INV_TGTDEVADDR ?"! " :"" );printf ("--dst-mac " ); -print_mac_and_mask ((unsigned char *)fw->arp.tgt_devaddr.addr, - (unsigned char *)fw->arp.tgt_devaddr.mask,ETH_ALEN ); +xtables_print_mac_and_mask ((unsigned char *)fw->arp.tgt_devaddr.addr, + (unsigned char *)fw->arp.tgt_devaddr.mask); sep =" " ; after_devdst:
Signature · 1 line
2.23.0
Quoted 5 lines · Phil Sutter
A review of
xtables-arp.cexposed a significant amount of dead, needless or duplicated code. This series deals with some low hanging fruits. Most of the changes affectxtables-arp.candnft-arp.conly, but where common issues existed or code was to be shared, other files are touched as well.
Phil Sutter
reviewerQuoted 8 lines · Pablo Neira Ayuso
Quoted 5 lines · Phil Sutter
A review of
xtables-arp.cexposed a significant amount of dead, needless or duplicated code. This series deals with some low hanging fruits. Most of the changes affectxtables-arp.candnft-arp.conly, but where common issues existed or code was to be shared, other files are touched as well.
I'll have to respin, patch 7 breaks arptables-nft for inverted options - I missed how the actual values are used in inverse_for_options variable and ran 'iptables-test.py -n' just now.
Sorry for the noise!
Cheers, Phil