]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
xshared: Simplify generic_opt_check()
authorPhil Sutter <phil@nwl.cc>
Fri, 17 Nov 2023 12:33:15 +0000 (13:33 +0100)
committerPhil Sutter <phil@nwl.cc>
Wed, 29 Nov 2023 01:33:02 +0000 (02:33 +0100)
The 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.

Signed-off-by: Phil Sutter <phil@nwl.cc>
iptables/xshared.c

index f939a988fa59decb9ac3658aaeba94b0402740b7..ca17479811df3f851babbb0183432f82d468cced 100644 (file)
@@ -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[NUMBER_OF_CMD][NUMBER_OF_OPT] =
-/* Well, it's better than "Re: Linux vs FreeBSD" */
-{
-       /*     -n  -s  -d  -p  -j  -v  -x  -i  -o --line -c -f 2 3 l 4 5 6 */
-/*INSERT*/    {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' ',' ',' ',' ',' ',' ',' '},
-/*DELETE*/    {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' ',' ',' ',' ',' ',' ',' '},
-/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x','x','x','x','x','x','x'},
-/*REPLACE*/   {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' ',' ',' ',' ',' ',' ',' '},
-/*APPEND*/    {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' ',' ',' ',' ',' ',' ',' '},
-/*LIST*/      {' ','x','x','x','x',' ',' ','x','x',' ','x','x','x','x','x','x','x','x'},
-/*FLUSH*/     {'x','x','x','x','x',' ','x','x','x','x','x','x','x','x','x','x','x','x'},
-/*ZERO*/      {'x','x','x','x','x',' ','x','x','x','x','x','x','x','x','x','x','x','x'},
-/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x','x','x','x','x','x','x'},
-/*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_IDRAC
 
 static 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));
        }
 }