Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add string matching support. |
| 2 | * |
| 3 | * Copyright (C) 2000 Emmanuel Roger <winfield@freegates.be> |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 4 | * |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 5 | * 2005-08-05 Pablo Neira Ayuso <pablo@eurodev.net> |
| 6 | * - reimplemented to use new string matching iptables match |
| 7 | * - add functionality to match packets by using window offsets |
| 8 | * - add functionality to select the string matching algorithm |
| 9 | * |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 10 | * ChangeLog |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 11 | * 29.12.2003: Michael Rash <mbr@cipherdyne.org> |
| 12 | * Fixed iptables save/restore for ascii strings |
| 13 | * that contain space chars, and hex strings that |
| 14 | * contain embedded NULL chars. Updated to print |
| 15 | * strings in hex mode if any non-printable char |
| 16 | * is contained within the string. |
| 17 | * |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 18 | * 27.01.2001: Gianni Tedesco <gianni@ecsc.co.uk> |
| 19 | * Changed --tos to --string in save(). Also |
| 20 | * updated to work with slightly modified |
| 21 | * ipt_string_info. |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 22 | */ |
Jan Engelhardt | 67156c0 | 2011-08-01 20:08:42 +0200 | [diff] [blame] | 23 | #define _GNU_SOURCE 1 /* strnlen for older glibcs */ |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 28 | #include <xtables.h> |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 29 | #include <linux/netfilter/xt_string.h> |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 30 | |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 31 | enum { |
| 32 | O_FROM = 0, |
| 33 | O_TO, |
| 34 | O_ALGO, |
| 35 | O_ICASE, |
| 36 | O_STRING, |
| 37 | O_HEX_STRING, |
| 38 | F_STRING = 1 << O_STRING, |
| 39 | F_HEX_STRING = 1 << O_HEX_STRING, |
| 40 | F_OP_ANY = F_STRING | F_HEX_STRING, |
| 41 | }; |
| 42 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 43 | static void string_help(void) |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 44 | { |
| 45 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 46 | "string match options:\n" |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 47 | "--from Offset to start searching from\n" |
| 48 | "--to Offset to stop searching\n" |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 49 | "--algo Algorithm\n" |
| 50 | "--icase Ignore case (default: 0)\n" |
Jan Engelhardt | 9b488b9 | 2008-06-08 19:11:51 +0200 | [diff] [blame] | 51 | "[!] --string string Match a string in a packet\n" |
| 52 | "[!] --hex-string string Match a hex string in a packet\n"); |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 55 | #define s struct xt_string_info |
| 56 | static const struct xt_option_entry string_opts[] = { |
| 57 | {.name = "from", .id = O_FROM, .type = XTTYPE_UINT16, |
| 58 | .flags = XTOPT_PUT, XTOPT_POINTER(s, from_offset)}, |
| 59 | {.name = "to", .id = O_TO, .type = XTTYPE_UINT16, |
| 60 | .flags = XTOPT_PUT, XTOPT_POINTER(s, to_offset)}, |
| 61 | {.name = "algo", .id = O_ALGO, .type = XTTYPE_STRING, |
| 62 | .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, algo)}, |
| 63 | {.name = "string", .id = O_STRING, .type = XTTYPE_STRING, |
| 64 | .flags = XTOPT_INVERT, .excl = F_HEX_STRING}, |
| 65 | {.name = "hex-string", .id = O_HEX_STRING, .type = XTTYPE_STRING, |
| 66 | .flags = XTOPT_INVERT, .excl = F_STRING}, |
| 67 | {.name = "icase", .id = O_ICASE, .type = XTTYPE_NONE}, |
| 68 | XTOPT_TABLEEND, |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 69 | }; |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 70 | #undef s |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 71 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 72 | static void string_init(struct xt_entry_match *m) |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 73 | { |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 74 | struct xt_string_info *i = (struct xt_string_info *) m->data; |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 75 | |
Jan Engelhardt | e88a7c2 | 2011-02-18 02:00:33 +0100 | [diff] [blame] | 76 | i->to_offset = UINT16_MAX; |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 80 | parse_string(const char *s, struct xt_string_info *info) |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 81 | { |
Pablo Neira Ayuso | 409f2a8 | 2009-03-02 11:46:55 +0100 | [diff] [blame] | 82 | /* xt_string does not need \0 at the end of the pattern */ |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 83 | if (strlen(s) <= XT_STRING_MAX_PATTERN_SIZE) { |
| 84 | strncpy(info->pattern, s, XT_STRING_MAX_PATTERN_SIZE); |
Pablo Neira Ayuso | 409f2a8 | 2009-03-02 11:46:55 +0100 | [diff] [blame] | 85 | info->patlen = strnlen(s, XT_STRING_MAX_PATTERN_SIZE); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 86 | return; |
| 87 | } |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 88 | xtables_error(PARAMETER_PROBLEM, "STRING too long \"%s\"", s); |
Michael Rash | 96d8593 | 2003-04-21 07:27:03 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 91 | static void |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 92 | parse_hex_string(const char *s, struct xt_string_info *info) |
Michael Rash | 96d8593 | 2003-04-21 07:27:03 +0000 | [diff] [blame] | 93 | { |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 94 | int i=0, slen, sindex=0, schar; |
| 95 | short hex_f = 0, literal_f = 0; |
| 96 | char hextmp[3]; |
| 97 | |
| 98 | slen = strlen(s); |
| 99 | |
| 100 | if (slen == 0) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 101 | xtables_error(PARAMETER_PROBLEM, |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 102 | "STRING must contain at least one char"); |
| 103 | } |
| 104 | |
| 105 | while (i < slen) { |
Phil Sutter | 6fc7762 | 2018-09-17 13:38:33 +0200 | [diff] [blame] | 106 | if (sindex >= XT_STRING_MAX_PATTERN_SIZE) |
| 107 | xtables_error(PARAMETER_PROBLEM, |
| 108 | "STRING too long \"%s\"", s); |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 109 | if (s[i] == '\\' && !hex_f) { |
| 110 | literal_f = 1; |
| 111 | } else if (s[i] == '\\') { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 112 | xtables_error(PARAMETER_PROBLEM, |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 113 | "Cannot include literals in hex data"); |
| 114 | } else if (s[i] == '|') { |
| 115 | if (hex_f) |
| 116 | hex_f = 0; |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 117 | else { |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 118 | hex_f = 1; |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 119 | /* get past any initial whitespace just after the '|' */ |
| 120 | while (s[i+1] == ' ') |
| 121 | i++; |
| 122 | } |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 123 | if (i+1 >= slen) |
| 124 | break; |
| 125 | else |
| 126 | i++; /* advance to the next character */ |
| 127 | } |
| 128 | |
| 129 | if (literal_f) { |
| 130 | if (i+1 >= slen) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 131 | xtables_error(PARAMETER_PROBLEM, |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 132 | "Bad literal placement at end of string"); |
| 133 | } |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 134 | info->pattern[sindex] = s[i+1]; |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 135 | i += 2; /* skip over literal char */ |
| 136 | literal_f = 0; |
| 137 | } else if (hex_f) { |
| 138 | if (i+1 >= slen) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 139 | xtables_error(PARAMETER_PROBLEM, |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 140 | "Odd number of hex digits"); |
| 141 | } |
| 142 | if (i+2 >= slen) { |
| 143 | /* must end with a "|" */ |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 144 | xtables_error(PARAMETER_PROBLEM, "Invalid hex block"); |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 145 | } |
Michael Rash | 96d8593 | 2003-04-21 07:27:03 +0000 | [diff] [blame] | 146 | if (! isxdigit(s[i])) /* check for valid hex char */ |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 147 | xtables_error(PARAMETER_PROBLEM, "Invalid hex char '%c'", s[i]); |
Michael Rash | 96d8593 | 2003-04-21 07:27:03 +0000 | [diff] [blame] | 148 | if (! isxdigit(s[i+1])) /* check for valid hex char */ |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 149 | xtables_error(PARAMETER_PROBLEM, "Invalid hex char '%c'", s[i+1]); |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 150 | hextmp[0] = s[i]; |
| 151 | hextmp[1] = s[i+1]; |
| 152 | hextmp[2] = '\0'; |
| 153 | if (! sscanf(hextmp, "%x", &schar)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 154 | xtables_error(PARAMETER_PROBLEM, |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 155 | "Invalid hex char `%c'", s[i]); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 156 | info->pattern[sindex] = (char) schar; |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 157 | if (s[i+2] == ' ') |
| 158 | i += 3; /* spaces included in the hex block */ |
| 159 | else |
| 160 | i += 2; |
| 161 | } else { /* the char is not part of hex data, so just copy */ |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 162 | info->pattern[sindex] = s[i]; |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 163 | i++; |
| 164 | } |
Phil Sutter | 6fc7762 | 2018-09-17 13:38:33 +0200 | [diff] [blame] | 165 | sindex++; |
Michael Rash | f8ac329 | 2003-02-26 17:34:13 +0000 | [diff] [blame] | 166 | } |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 167 | info->patlen = sindex; |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 170 | static void string_parse(struct xt_option_call *cb) |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 171 | { |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 172 | struct xt_string_info *stringinfo = cb->data; |
| 173 | const unsigned int revision = (*cb->match)->u.user.revision; |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 174 | |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 175 | xtables_option_parse(cb); |
| 176 | switch (cb->entry->id) { |
| 177 | case O_STRING: |
| 178 | parse_string(cb->arg, stringinfo); |
| 179 | if (cb->invert) { |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 180 | if (revision == 0) |
| 181 | stringinfo->u.v0.invert = 1; |
| 182 | else |
| 183 | stringinfo->u.v1.flags |= XT_STRING_FLAG_INVERT; |
| 184 | } |
Michael Rash | 96d8593 | 2003-04-21 07:27:03 +0000 | [diff] [blame] | 185 | break; |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 186 | case O_HEX_STRING: |
| 187 | parse_hex_string(cb->arg, stringinfo); /* sets length */ |
| 188 | if (cb->invert) { |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 189 | if (revision == 0) |
| 190 | stringinfo->u.v0.invert = 1; |
| 191 | else |
| 192 | stringinfo->u.v1.flags |= XT_STRING_FLAG_INVERT; |
| 193 | } |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 194 | break; |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 195 | case O_ICASE: |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 196 | if (revision == 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 197 | xtables_error(VERSION_PROBLEM, |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 198 | "Kernel doesn't support --icase"); |
| 199 | |
| 200 | stringinfo->u.v1.flags |= XT_STRING_FLAG_IGNORECASE; |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 201 | break; |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 202 | } |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 205 | static void string_check(struct xt_fcheck_call *cb) |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 206 | { |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 207 | if (!(cb->xflags & F_OP_ANY)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 208 | xtables_error(PARAMETER_PROBLEM, |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 209 | "STRING match: You must specify `--string' or " |
| 210 | "`--hex-string'"); |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 213 | /* Test to see if the string contains non-printable chars or quotes */ |
| 214 | static unsigned short int |
| 215 | is_hex_string(const char *str, const unsigned short int len) |
| 216 | { |
| 217 | unsigned int i; |
| 218 | for (i=0; i < len; i++) |
| 219 | if (! isprint(str[i])) |
| 220 | return 1; /* string contains at least one non-printable char */ |
| 221 | /* use hex output if the last char is a "\" */ |
Jan Engelhardt | f4daf54 | 2011-08-25 12:11:20 +0200 | [diff] [blame] | 222 | if (str[len-1] == '\\') |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 223 | return 1; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* Print string with "|" chars included as one would pass to --hex-string */ |
| 228 | static void |
| 229 | print_hex_string(const char *str, const unsigned short int len) |
| 230 | { |
| 231 | unsigned int i; |
| 232 | /* start hex block */ |
Dwight Davis | 3716dfd | 2011-08-12 17:02:09 -0400 | [diff] [blame] | 233 | printf(" \"|"); |
Jan Engelhardt | 131d4fb | 2011-08-21 13:16:16 +0200 | [diff] [blame] | 234 | for (i=0; i < len; i++) |
| 235 | printf("%02x", (unsigned char)str[i]); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 236 | /* close hex block */ |
Dwight Davis | 3716dfd | 2011-08-12 17:02:09 -0400 | [diff] [blame] | 237 | printf("|\""); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void |
| 241 | print_string(const char *str, const unsigned short int len) |
| 242 | { |
| 243 | unsigned int i; |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 244 | printf(" \""); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 245 | for (i=0; i < len; i++) { |
Jan Engelhardt | d51a97b | 2011-08-26 14:46:40 +0200 | [diff] [blame] | 246 | if (str[i] == '\"' || str[i] == '\\') |
Jan Engelhardt | f4daf54 | 2011-08-25 12:11:20 +0200 | [diff] [blame] | 247 | putchar('\\'); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 248 | printf("%c", (unsigned char) str[i]); |
| 249 | } |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 250 | printf("\""); /* closing quote */ |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 251 | } |
Stephane Ouellette | 451f3ea | 2003-04-27 12:59:00 +0000 | [diff] [blame] | 252 | |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 253 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 254 | string_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 255 | { |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 256 | const struct xt_string_info *info = |
| 257 | (const struct xt_string_info*) match->data; |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 258 | const int revision = match->u.user.revision; |
| 259 | int invert = (revision == 0 ? info->u.v0.invert : |
| 260 | info->u.v1.flags & XT_STRING_FLAG_INVERT); |
Stephane Ouellette | 451f3ea | 2003-04-27 12:59:00 +0000 | [diff] [blame] | 261 | |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 262 | if (is_hex_string(info->pattern, info->patlen)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 263 | printf(" STRING match %s", invert ? "!" : ""); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 264 | print_hex_string(info->pattern, info->patlen); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 265 | } else { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 266 | printf(" STRING match %s", invert ? "!" : ""); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 267 | print_string(info->pattern, info->patlen); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 268 | } |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 269 | printf(" ALGO name %s", info->algo); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 270 | if (info->from_offset != 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 271 | printf(" FROM %u", info->from_offset); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 272 | if (info->to_offset != 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 273 | printf(" TO %u", info->to_offset); |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 274 | if (revision > 0 && info->u.v1.flags & XT_STRING_FLAG_IGNORECASE) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 275 | printf(" ICASE"); |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 278 | static void string_save(const void *ip, const struct xt_entry_match *match) |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 279 | { |
Yasuyuki KOZAKAI | 6ac58e3 | 2007-07-24 06:50:03 +0000 | [diff] [blame] | 280 | const struct xt_string_info *info = |
| 281 | (const struct xt_string_info*) match->data; |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 282 | const int revision = match->u.user.revision; |
| 283 | int invert = (revision == 0 ? info->u.v0.invert : |
| 284 | info->u.v1.flags & XT_STRING_FLAG_INVERT); |
Stephane Ouellette | 451f3ea | 2003-04-27 12:59:00 +0000 | [diff] [blame] | 285 | |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 286 | if (is_hex_string(info->pattern, info->patlen)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 287 | printf("%s --hex-string", (invert) ? " !" : ""); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 288 | print_hex_string(info->pattern, info->patlen); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 289 | } else { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 290 | printf("%s --string", (invert) ? " !": ""); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 291 | print_string(info->pattern, info->patlen); |
Michael Rash | b807fb3 | 2004-01-05 09:50:12 +0000 | [diff] [blame] | 292 | } |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 293 | printf(" --algo %s", info->algo); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 294 | if (info->from_offset != 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 295 | printf(" --from %u", info->from_offset); |
Pablo Neira | c6fbf41 | 2005-08-28 08:09:44 +0000 | [diff] [blame] | 296 | if (info->to_offset != 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 297 | printf(" --to %u", info->to_offset); |
Joonwoo Park | 78d2d14 | 2008-07-07 13:32:25 +0200 | [diff] [blame] | 298 | if (revision > 0 && info->u.v1.flags & XT_STRING_FLAG_IGNORECASE) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 299 | printf(" --icase"); |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Stephane Ouellette | 451f3ea | 2003-04-27 12:59:00 +0000 | [diff] [blame] | 302 | |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 303 | static struct xtables_match string_mt_reg[] = { |
| 304 | { |
| 305 | .name = "string", |
| 306 | .revision = 0, |
| 307 | .family = NFPROTO_UNSPEC, |
| 308 | .version = XTABLES_VERSION, |
| 309 | .size = XT_ALIGN(sizeof(struct xt_string_info)), |
| 310 | .userspacesize = offsetof(struct xt_string_info, config), |
| 311 | .help = string_help, |
| 312 | .init = string_init, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 313 | .print = string_print, |
| 314 | .save = string_save, |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 315 | .x6_parse = string_parse, |
| 316 | .x6_fcheck = string_check, |
| 317 | .x6_options = string_opts, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 318 | }, |
| 319 | { |
| 320 | .name = "string", |
| 321 | .revision = 1, |
| 322 | .family = NFPROTO_UNSPEC, |
| 323 | .version = XTABLES_VERSION, |
| 324 | .size = XT_ALIGN(sizeof(struct xt_string_info)), |
| 325 | .userspacesize = offsetof(struct xt_string_info, config), |
| 326 | .help = string_help, |
| 327 | .init = string_init, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 328 | .print = string_print, |
| 329 | .save = string_save, |
Jan Engelhardt | c618a0b | 2011-03-06 18:12:04 +0100 | [diff] [blame] | 330 | .x6_parse = string_parse, |
| 331 | .x6_fcheck = string_check, |
| 332 | .x6_options = string_opts, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 333 | }, |
Yasuyuki KOZAKAI | 4ccb6f5 | 2007-07-24 06:52:16 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 336 | void _init(void) |
| 337 | { |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 338 | xtables_register_matches(string_mt_reg, ARRAY_SIZE(string_mt_reg)); |
Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 339 | } |