blob: 8ac98b624c578eae89ebc608fccb5d95fa00eaa6 [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
8#include <iptables.h>
9#include <linux/netfilter_ipv4/ipt_length.h>
10
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
23static struct option opts[] = {
24 { "length", 1, 0, '1' },
25 {0}
26};
27
28/* Initialize the match. */
29static void
30init(struct ipt_entry_match *m, unsigned int *nfcache)
31{
32 *nfcache |= NFC_UNKNOWN;
33}
34
35static u_int16_t
36parse_length(const char *s)
37{
Harald Welteb4719762001-07-23 02:14:22 +000038 unsigned int len;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000039
Harald Welteb4719762001-07-23 02:14:22 +000040 if (string_to_number(s, 0, 0xFFFF, &len) == -1)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000041 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 */
47static void
48parse_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 */
74static int
75parse(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");
Harald Welteb77f1da2002-03-14 11:35:58 +000088 check_inverse(optarg, &invert, &optind, 0);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000089 parse_lengths(argv[optind-1], info);
90 if (invert)
91 info->invert = 1;
92 *flags = 1;
93 break;
94
95 default:
96 return 0;
97 }
98 return 1;
99}
100
101/* Final check; must have specified --length. */
102static void
103final_check(unsigned int flags)
104{
105 if (!flags)
106 exit_error(PARAMETER_PROBLEM,
107 "length: You must specify `--length'");
108}
109
110/* Common match printing code. */
111static void
112print_length(struct ipt_length_info *info)
113{
114 if (info->invert)
Gerry Skerbitzc8c0f402002-12-05 20:37:22 +0000115 printf("! ");
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000116
117 if (info->max == info->min)
118 printf("%u ", info->min);
119 else
120 printf("%u:%u ", info->min, info->max);
121}
122
123/* Prints out the matchinfo. */
124static void
125print(const struct ipt_ip *ip,
126 const struct ipt_entry_match *match,
127 int numeric)
128{
129 printf("length ");
130 print_length((struct ipt_length_info *)match->data);
131}
132
133/* Saves the union ipt_matchinfo in parsable form to stdout. */
134static void
135save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
136{
137 printf("--length ");
138 print_length((struct ipt_length_info *)match->data);
139}
140
Harald Welte3efb6ea2001-08-06 18:50:21 +0000141static
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000142struct iptables_match length
143= { NULL,
144 "length",
Harald Welte80fe35d2002-05-29 13:08:15 +0000145 IPTABLES_VERSION,
Fabrice MARIE147a2be2001-05-02 15:45:57 +0000146 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
157void _init(void)
158{
159 register_match(&length);
160}