blob: d039904bffb69fce4069515e0e6cbd07abf948d7 [file] [log] [blame]
Fabrice MARIE147a2be2001-05-02 15:45:57 +00001/* 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 KOZAKAI36087d92007-07-24 07:15:03 +00008#include <xtables.h>
9#include <linux/netfilter/xt_length.h>
Fabrice MARIE147a2be2001-05-02 15:45:57 +000010
Jan Engelhardt181dead2007-10-04 16:27:07 +000011static void length_help(void)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000012{
13 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020014"length match options:\n"
Fabrice MARIE147a2be2001-05-02 15:45:57 +000015"[!] --length length[:length] Match packet length against value or range\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020016" of values (inclusive)\n");
Fabrice MARIE147a2be2001-05-02 15:45:57 +000017}
18
Jan Engelhardt181dead2007-10-04 16:27:07 +000019static const struct option length_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000020 { "length", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000021 { .name = NULL }
Fabrice MARIE147a2be2001-05-02 15:45:57 +000022};
23
Fabrice MARIE147a2be2001-05-02 15:45:57 +000024static u_int16_t
25parse_length(const char *s)
26{
Harald Welteb4719762001-07-23 02:14:22 +000027 unsigned int len;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000028
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010029 if (!xtables_strtoui(s, NULL, &len, 0, UINT32_MAX))
Fabrice MARIE147a2be2001-05-02 15:45:57 +000030 exit_error(PARAMETER_PROBLEM, "length invalid: `%s'\n", s);
31 else
Jan Engelhardt213e1852009-01-27 17:24:34 +010032 return len;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000033}
34
35/* If a single value is provided, min and max are both set to the value */
36static void
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +000037parse_lengths(const char *s, struct xt_length_info *info)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000038{
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 MARIE147a2be2001-05-02 15:45:57 +000061static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000062length_parse(int c, char **argv, int invert, unsigned int *flags,
63 const void *entry, struct xt_entry_match **match)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000064{
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +000065 struct xt_length_info *info = (struct xt_length_info *)(*match)->data;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000066
67 switch (c) {
68 case '1':
69 if (*flags)
70 exit_error(PARAMETER_PROBLEM,
71 "length: `--length' may only be "
72 "specified once");
Harald Welteb77f1da2002-03-14 11:35:58 +000073 check_inverse(optarg, &invert, &optind, 0);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000074 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 Engelhardt181dead2007-10-04 16:27:07 +000086static void length_check(unsigned int flags)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000087{
88 if (!flags)
89 exit_error(PARAMETER_PROBLEM,
90 "length: You must specify `--length'");
91}
92
Fabrice MARIE147a2be2001-05-02 15:45:57 +000093static void
Jan Engelhardtcea9f712008-12-09 15:06:20 +010094length_print(const void *ip, const struct xt_entry_match *match, int numeric)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000095{
Jan Engelhardtcea9f712008-12-09 15:06:20 +010096 const struct xt_length_info *info = (void *)match->data;
97
98 printf("length %s", info->invert ? "!" : "");
99 if (info->min == info->max)
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000100 printf("%u ", info->min);
101 else
102 printf("%u:%u ", info->min, info->max);
103}
104
Jan Engelhardt181dead2007-10-04 16:27:07 +0000105static void length_save(const void *ip, const struct xt_entry_match *match)
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000106{
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100107 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 MARIE147a2be2001-05-02 15:45:57 +0000114}
115
Jan Engelhardt181dead2007-10-04 16:27:07 +0000116static struct xtables_match length_match = {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100117 .family = AF_UNSPEC,
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000118 .name = "length",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200119 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000120 .size = XT_ALIGN(sizeof(struct xt_length_info)),
121 .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000122 .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 MARIE147a2be2001-05-02 15:45:57 +0000128};
129
130void _init(void)
131{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000132 xtables_register_match(&length_match);
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000133}