blob: 5353d2cd89daa57be39ac0775735ebc06e2e95ff [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
11/* Function which prints out usage message. */
12static void
13help(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",
Harald Welte80fe35d2002-05-29 13:08:15 +000019IPTABLES_VERSION);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000020
21}
22
Jan Engelhardt661f1122007-07-30 14:46:51 +000023static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000024 { "length", 1, NULL, '1' },
25 { }
Fabrice MARIE147a2be2001-05-02 15:45:57 +000026};
27
Fabrice MARIE147a2be2001-05-02 15:45:57 +000028static u_int16_t
29parse_length(const char *s)
30{
Harald Welteb4719762001-07-23 02:14:22 +000031 unsigned int len;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000032
Harald Welteb4719762001-07-23 02:14:22 +000033 if (string_to_number(s, 0, 0xFFFF, &len) == -1)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000034 exit_error(PARAMETER_PROBLEM, "length invalid: `%s'\n", s);
35 else
36 return (u_int16_t )len;
37}
38
39/* If a single value is provided, min and max are both set to the value */
40static void
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +000041parse_lengths(const char *s, struct xt_length_info *info)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000042{
43 char *buffer;
44 char *cp;
45
46 buffer = strdup(s);
47 if ((cp = strchr(buffer, ':')) == NULL)
48 info->min = info->max = parse_length(buffer);
49 else {
50 *cp = '\0';
51 cp++;
52
53 info->min = buffer[0] ? parse_length(buffer) : 0;
54 info->max = cp[0] ? parse_length(cp) : 0xFFFF;
55 }
56 free(buffer);
57
58 if (info->min > info->max)
59 exit_error(PARAMETER_PROBLEM,
60 "length min. range value `%u' greater than max. "
61 "range value `%u'", info->min, info->max);
62
63}
64
65/* Function which parses command options; returns true if it
66 ate an option */
67static int
68parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000069 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000070 struct xt_entry_match **match)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000071{
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +000072 struct xt_length_info *info = (struct xt_length_info *)(*match)->data;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000073
74 switch (c) {
75 case '1':
76 if (*flags)
77 exit_error(PARAMETER_PROBLEM,
78 "length: `--length' may only be "
79 "specified once");
Harald Welteb77f1da2002-03-14 11:35:58 +000080 check_inverse(optarg, &invert, &optind, 0);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000081 parse_lengths(argv[optind-1], info);
82 if (invert)
83 info->invert = 1;
84 *flags = 1;
85 break;
86
87 default:
88 return 0;
89 }
90 return 1;
91}
92
93/* Final check; must have specified --length. */
94static void
95final_check(unsigned int flags)
96{
97 if (!flags)
98 exit_error(PARAMETER_PROBLEM,
99 "length: You must specify `--length'");
100}
101
102/* Common match printing code. */
103static void
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000104print_length(struct xt_length_info *info)
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000105{
106 if (info->invert)
Gerry Skerbitzc8c0f402002-12-05 20:37:22 +0000107 printf("! ");
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000108
109 if (info->max == info->min)
110 printf("%u ", info->min);
111 else
112 printf("%u:%u ", info->min, info->max);
113}
114
115/* Prints out the matchinfo. */
116static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000117print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000118 const struct xt_entry_match *match,
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000119 int numeric)
120{
121 printf("length ");
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000122 print_length((struct xt_length_info *)match->data);
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000123}
124
125/* Saves the union ipt_matchinfo in parsable form to stdout. */
126static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000127save(const void *ip, const struct xt_entry_match *match)
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000128{
129 printf("--length ");
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000130 print_length((struct xt_length_info *)match->data);
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000131}
132
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000133static struct xtables_match length = {
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000134 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000135 .name = "length",
136 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000137 .size = XT_ALIGN(sizeof(struct xt_length_info)),
138 .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)),
139 .help = &help,
140 .parse = &parse,
141 .final_check = &final_check,
142 .print = &print,
143 .save = &save,
144 .extra_opts = opts
145};
146
147static struct xtables_match length6 = {
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000148 .family = AF_INET6,
149 .name = "length",
150 .version = IPTABLES_VERSION,
151 .size = XT_ALIGN(sizeof(struct xt_length_info)),
152 .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000153 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000154 .parse = &parse,
155 .final_check = &final_check,
156 .print = &print,
157 .save = &save,
158 .extra_opts = opts
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000159};
160
161void _init(void)
162{
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +0000163 xtables_register_match(&length);
164 xtables_register_match(&length6);
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000165}