Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add packet length matching support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 8 | #include <xtables.h> |
| 9 | #include <linux/netfilter/xt_length.h> |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 10 | |
| 11 | /* Function which prints out usage message. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 12 | static void length_help(void) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 13 | { |
| 14 | printf( |
| 15 | "length v%s options:\n" |
| 16 | "[!] --length length[:length] Match packet length against value or range\n" |
| 17 | " of values (inclusive)\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 18 | IPTABLES_VERSION); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 19 | |
| 20 | } |
| 21 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 22 | static const struct option length_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 23 | { "length", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 24 | { .name = NULL } |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 27 | static u_int16_t |
| 28 | parse_length(const char *s) |
| 29 | { |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 30 | unsigned int len; |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 31 | |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 32 | if (string_to_number(s, 0, 0xFFFF, &len) == -1) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 33 | exit_error(PARAMETER_PROBLEM, "length invalid: `%s'\n", s); |
| 34 | else |
| 35 | return (u_int16_t )len; |
| 36 | } |
| 37 | |
| 38 | /* If a single value is provided, min and max are both set to the value */ |
| 39 | static void |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 40 | parse_lengths(const char *s, struct xt_length_info *info) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 41 | { |
| 42 | char *buffer; |
| 43 | char *cp; |
| 44 | |
| 45 | buffer = strdup(s); |
| 46 | if ((cp = strchr(buffer, ':')) == NULL) |
| 47 | info->min = info->max = parse_length(buffer); |
| 48 | else { |
| 49 | *cp = '\0'; |
| 50 | cp++; |
| 51 | |
| 52 | info->min = buffer[0] ? parse_length(buffer) : 0; |
| 53 | info->max = cp[0] ? parse_length(cp) : 0xFFFF; |
| 54 | } |
| 55 | free(buffer); |
| 56 | |
| 57 | if (info->min > info->max) |
| 58 | exit_error(PARAMETER_PROBLEM, |
| 59 | "length min. range value `%u' greater than max. " |
| 60 | "range value `%u'", info->min, info->max); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /* Function which parses command options; returns true if it |
| 65 | ate an option */ |
| 66 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 67 | length_parse(int c, char **argv, int invert, unsigned int *flags, |
| 68 | const void *entry, struct xt_entry_match **match) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 69 | { |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 70 | struct xt_length_info *info = (struct xt_length_info *)(*match)->data; |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 71 | |
| 72 | switch (c) { |
| 73 | case '1': |
| 74 | if (*flags) |
| 75 | exit_error(PARAMETER_PROBLEM, |
| 76 | "length: `--length' may only be " |
| 77 | "specified once"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 78 | check_inverse(optarg, &invert, &optind, 0); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 79 | parse_lengths(argv[optind-1], info); |
| 80 | if (invert) |
| 81 | info->invert = 1; |
| 82 | *flags = 1; |
| 83 | break; |
| 84 | |
| 85 | default: |
| 86 | return 0; |
| 87 | } |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | /* Final check; must have specified --length. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 92 | static void length_check(unsigned int flags) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 93 | { |
| 94 | if (!flags) |
| 95 | exit_error(PARAMETER_PROBLEM, |
| 96 | "length: You must specify `--length'"); |
| 97 | } |
| 98 | |
| 99 | /* Common match printing code. */ |
| 100 | static void |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 101 | print_length(struct xt_length_info *info) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 102 | { |
| 103 | if (info->invert) |
Gerry Skerbitz | c8c0f40 | 2002-12-05 20:37:22 +0000 | [diff] [blame] | 104 | printf("! "); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 105 | |
| 106 | if (info->max == info->min) |
| 107 | printf("%u ", info->min); |
| 108 | else |
| 109 | printf("%u:%u ", info->min, info->max); |
| 110 | } |
| 111 | |
| 112 | /* Prints out the matchinfo. */ |
| 113 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 114 | length_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 115 | { |
| 116 | printf("length "); |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 117 | print_length((struct xt_length_info *)match->data); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 121 | static void length_save(const void *ip, const struct xt_entry_match *match) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 122 | { |
| 123 | printf("--length "); |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 124 | print_length((struct xt_length_info *)match->data); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 127 | static struct xtables_match length_match = { |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 128 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 129 | .name = "length", |
| 130 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 131 | .size = XT_ALIGN(sizeof(struct xt_length_info)), |
| 132 | .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 133 | .help = length_help, |
| 134 | .parse = length_parse, |
| 135 | .final_check = length_check, |
| 136 | .print = length_print, |
| 137 | .save = length_save, |
| 138 | .extra_opts = length_opts, |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 141 | static struct xtables_match length_match6 = { |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 142 | .family = AF_INET6, |
| 143 | .name = "length", |
| 144 | .version = IPTABLES_VERSION, |
| 145 | .size = XT_ALIGN(sizeof(struct xt_length_info)), |
| 146 | .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 147 | .help = length_help, |
| 148 | .parse = length_parse, |
| 149 | .final_check = length_check, |
| 150 | .print = length_print, |
| 151 | .save = length_save, |
| 152 | .extra_opts = length_opts, |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | void _init(void) |
| 156 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 157 | xtables_register_match(&length_match); |
| 158 | xtables_register_match(&length_match6); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 159 | } |