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 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 11 | static void length_help(void) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 12 | { |
| 13 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 14 | "length match options:\n" |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 15 | "[!] --length length[:length] Match packet length against value or range\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 16 | " of values (inclusive)\n"); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 17 | } |
| 18 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 19 | static const struct option length_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 20 | { "length", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 21 | { .name = NULL } |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 24 | static u_int16_t |
| 25 | parse_length(const char *s) |
| 26 | { |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 27 | unsigned int len; |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 28 | |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 29 | if (string_to_number(s, 0, 0xFFFF, &len) == -1) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 30 | exit_error(PARAMETER_PROBLEM, "length invalid: `%s'\n", s); |
| 31 | else |
| 32 | return (u_int16_t )len; |
| 33 | } |
| 34 | |
| 35 | /* If a single value is provided, min and max are both set to the value */ |
| 36 | static void |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 37 | parse_lengths(const char *s, struct xt_length_info *info) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 38 | { |
| 39 | char *buffer; |
| 40 | char *cp; |
| 41 | |
| 42 | buffer = strdup(s); |
| 43 | if ((cp = strchr(buffer, ':')) == NULL) |
| 44 | info->min = info->max = parse_length(buffer); |
| 45 | else { |
| 46 | *cp = '\0'; |
| 47 | cp++; |
| 48 | |
| 49 | info->min = buffer[0] ? parse_length(buffer) : 0; |
| 50 | info->max = cp[0] ? parse_length(cp) : 0xFFFF; |
| 51 | } |
| 52 | free(buffer); |
| 53 | |
| 54 | if (info->min > info->max) |
| 55 | exit_error(PARAMETER_PROBLEM, |
| 56 | "length min. range value `%u' greater than max. " |
| 57 | "range value `%u'", info->min, info->max); |
| 58 | |
| 59 | } |
| 60 | |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 61 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 62 | length_parse(int c, char **argv, int invert, unsigned int *flags, |
| 63 | const void *entry, struct xt_entry_match **match) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 64 | { |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 65 | struct xt_length_info *info = (struct xt_length_info *)(*match)->data; |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 66 | |
| 67 | switch (c) { |
| 68 | case '1': |
| 69 | if (*flags) |
| 70 | exit_error(PARAMETER_PROBLEM, |
| 71 | "length: `--length' may only be " |
| 72 | "specified once"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 73 | check_inverse(optarg, &invert, &optind, 0); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 74 | parse_lengths(argv[optind-1], info); |
| 75 | if (invert) |
| 76 | info->invert = 1; |
| 77 | *flags = 1; |
| 78 | break; |
| 79 | |
| 80 | default: |
| 81 | return 0; |
| 82 | } |
| 83 | return 1; |
| 84 | } |
| 85 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 86 | static void length_check(unsigned int flags) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 87 | { |
| 88 | if (!flags) |
| 89 | exit_error(PARAMETER_PROBLEM, |
| 90 | "length: You must specify `--length'"); |
| 91 | } |
| 92 | |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 93 | static void |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame^] | 94 | 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] | 95 | { |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame^] | 96 | const struct xt_length_info *info = (void *)match->data; |
| 97 | |
| 98 | printf("length %s", info->invert ? "!" : ""); |
| 99 | if (info->min == info->max) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 100 | printf("%u ", info->min); |
| 101 | else |
| 102 | printf("%u:%u ", info->min, info->max); |
| 103 | } |
| 104 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 105 | 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] | 106 | { |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame^] | 107 | const struct xt_length_info *info = (void *)match->data; |
| 108 | |
| 109 | printf("%s--length ", info->invert ? "! " : ""); |
| 110 | if (info->min == info->max) |
| 111 | printf("%u ", info->min); |
| 112 | else |
| 113 | printf("%u:%u ", info->min, info->max); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 116 | static struct xtables_match length_match = { |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 117 | .family = AF_UNSPEC, |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 118 | .name = "length", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 119 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 36087d9 | 2007-07-24 07:15:03 +0000 | [diff] [blame] | 120 | .size = XT_ALIGN(sizeof(struct xt_length_info)), |
| 121 | .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 122 | .help = length_help, |
| 123 | .parse = length_parse, |
| 124 | .final_check = length_check, |
| 125 | .print = length_print, |
| 126 | .save = length_save, |
| 127 | .extra_opts = length_opts, |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | void _init(void) |
| 131 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 132 | xtables_register_match(&length_match); |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 133 | } |