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 | |
| 8 | #include <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_length.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "length v%s options:\n" |
| 17 | "[!] --length length[:length] Match packet length against value or range\n" |
| 18 | " of values (inclusive)\n", |
| 19 | NETFILTER_VERSION); |
| 20 | |
| 21 | } |
| 22 | |
| 23 | static struct option opts[] = { |
| 24 | { "length", 1, 0, '1' }, |
| 25 | {0} |
| 26 | }; |
| 27 | |
| 28 | /* Initialize the match. */ |
| 29 | static void |
| 30 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 31 | { |
| 32 | *nfcache |= NFC_UNKNOWN; |
| 33 | } |
| 34 | |
| 35 | static u_int16_t |
| 36 | parse_length(const char *s) |
| 37 | { |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame^] | 38 | unsigned int len; |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 39 | |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame^] | 40 | if (string_to_number(s, 0, 0xFFFF, &len) == -1) |
Fabrice MARIE | 147a2be | 2001-05-02 15:45:57 +0000 | [diff] [blame] | 41 | exit_error(PARAMETER_PROBLEM, "length invalid: `%s'\n", s); |
| 42 | else |
| 43 | return (u_int16_t )len; |
| 44 | } |
| 45 | |
| 46 | /* If a single value is provided, min and max are both set to the value */ |
| 47 | static void |
| 48 | parse_lengths(const char *s, struct ipt_length_info *info) |
| 49 | { |
| 50 | char *buffer; |
| 51 | char *cp; |
| 52 | |
| 53 | buffer = strdup(s); |
| 54 | if ((cp = strchr(buffer, ':')) == NULL) |
| 55 | info->min = info->max = parse_length(buffer); |
| 56 | else { |
| 57 | *cp = '\0'; |
| 58 | cp++; |
| 59 | |
| 60 | info->min = buffer[0] ? parse_length(buffer) : 0; |
| 61 | info->max = cp[0] ? parse_length(cp) : 0xFFFF; |
| 62 | } |
| 63 | free(buffer); |
| 64 | |
| 65 | if (info->min > info->max) |
| 66 | exit_error(PARAMETER_PROBLEM, |
| 67 | "length min. range value `%u' greater than max. " |
| 68 | "range value `%u'", info->min, info->max); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /* Function which parses command options; returns true if it |
| 73 | ate an option */ |
| 74 | static int |
| 75 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 76 | const struct ipt_entry *entry, |
| 77 | unsigned int *nfcache, |
| 78 | struct ipt_entry_match **match) |
| 79 | { |
| 80 | struct ipt_length_info *info = (struct ipt_length_info *)(*match)->data; |
| 81 | |
| 82 | switch (c) { |
| 83 | case '1': |
| 84 | if (*flags) |
| 85 | exit_error(PARAMETER_PROBLEM, |
| 86 | "length: `--length' may only be " |
| 87 | "specified once"); |
| 88 | if (check_inverse(optarg, &invert)) |
| 89 | optind++; |
| 90 | parse_lengths(argv[optind-1], info); |
| 91 | if (invert) |
| 92 | info->invert = 1; |
| 93 | *flags = 1; |
| 94 | break; |
| 95 | |
| 96 | default: |
| 97 | return 0; |
| 98 | } |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | /* Final check; must have specified --length. */ |
| 103 | static void |
| 104 | final_check(unsigned int flags) |
| 105 | { |
| 106 | if (!flags) |
| 107 | exit_error(PARAMETER_PROBLEM, |
| 108 | "length: You must specify `--length'"); |
| 109 | } |
| 110 | |
| 111 | /* Common match printing code. */ |
| 112 | static void |
| 113 | print_length(struct ipt_length_info *info) |
| 114 | { |
| 115 | if (info->invert) |
| 116 | fputc('!', stdout); |
| 117 | |
| 118 | if (info->max == info->min) |
| 119 | printf("%u ", info->min); |
| 120 | else |
| 121 | printf("%u:%u ", info->min, info->max); |
| 122 | } |
| 123 | |
| 124 | /* Prints out the matchinfo. */ |
| 125 | static void |
| 126 | print(const struct ipt_ip *ip, |
| 127 | const struct ipt_entry_match *match, |
| 128 | int numeric) |
| 129 | { |
| 130 | printf("length "); |
| 131 | print_length((struct ipt_length_info *)match->data); |
| 132 | } |
| 133 | |
| 134 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 135 | static void |
| 136 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 137 | { |
| 138 | printf("--length "); |
| 139 | print_length((struct ipt_length_info *)match->data); |
| 140 | } |
| 141 | |
| 142 | struct iptables_match length |
| 143 | = { NULL, |
| 144 | "length", |
| 145 | NETFILTER_VERSION, |
| 146 | IPT_ALIGN(sizeof(struct ipt_length_info)), |
| 147 | IPT_ALIGN(sizeof(struct ipt_length_info)), |
| 148 | &help, |
| 149 | &init, |
| 150 | &parse, |
| 151 | &final_check, |
| 152 | &print, |
| 153 | &save, |
| 154 | opts |
| 155 | }; |
| 156 | |
| 157 | void _init(void) |
| 158 | { |
| 159 | register_match(&length); |
| 160 | } |