blob: 9f7ba16559afd838efdf840c77e7b0aee27e4c83 [file] [log] [blame]
Harald Welte6185c4b2001-09-02 15:20:11 +00001/* Shared library add-on to ip6tables to add packet length matching support. */
2
3#include <stdio.h>
4#include <netdb.h>
5#include <string.h>
6#include <stdlib.h>
7#include <getopt.h>
8
9#include <ip6tables.h>
10#include <linux/netfilter_ipv6/ip6t_length.h>
11
Harald Welte6185c4b2001-09-02 15:20:11 +000012/* Function which prints out usage message. */
13static void
14help(void)
15{
16 printf(
17"length v%s options:\n"
18"[!] --length length[:length] Match packet length against value or range\n"
19" of values (inclusive)\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000020IPTABLES_VERSION);
Harald Welte6185c4b2001-09-02 15:20:11 +000021
22}
23
24static struct option opts[] = {
25 { "length", 1, 0, '1' },
26 {0}
27};
28
Harald Welte6185c4b2001-09-02 15:20:11 +000029static u_int16_t
30parse_length(const char *s)
Harald Welte52b68572001-09-13 14:06:00 +000031{
Harald Weltee143b912001-09-13 13:46:46 +000032
Harald Welteefa8fc22005-07-19 22:03:49 +000033 unsigned int len;
Harald Welte6185c4b2001-09-02 15:20:11 +000034
Harald Weltee143b912001-09-13 13:46:46 +000035 if (string_to_number(s, 0, 0xFFFF, &len) == -1)
Harald Welte6185c4b2001-09-02 15:20:11 +000036 exit_error(PARAMETER_PROBLEM, "length invalid: `%s'\n", s);
37 else
38 return (u_int16_t )len;
39}
40
41/* If a single value is provided, min and max are both set to the value */
42static void
43parse_lengths(const char *s, struct ip6t_length_info *info)
44{
45 char *buffer;
46 char *cp;
47
48 buffer = strdup(s);
49 if ((cp = strchr(buffer, ':')) == NULL)
50 info->min = info->max = parse_length(buffer);
51 else {
52 *cp = '\0';
53 cp++;
54
55 info->min = buffer[0] ? parse_length(buffer) : 0;
56 info->max = cp[0] ? parse_length(cp) : 0xFFFF;
57 }
58 free(buffer);
59
60 if (info->min > info->max)
61 exit_error(PARAMETER_PROBLEM,
62 "length min. range value `%u' greater than max. "
63 "range value `%u'", info->min, info->max);
64
65}
66
67/* Function which parses command options; returns true if it
68 ate an option */
69static int
70parse(int c, char **argv, int invert, unsigned int *flags,
71 const struct ip6t_entry *entry,
72 unsigned int *nfcache,
73 struct ip6t_entry_match **match)
74{
75 struct ip6t_length_info *info = (struct ip6t_length_info *)(*match)->data;
76
77 switch (c) {
78 case '1':
79 if (*flags)
80 exit_error(PARAMETER_PROBLEM,
81 "length: `--length' may only be "
82 "specified once");
Harald Welteb77f1da2002-03-14 11:35:58 +000083 check_inverse(optarg, &invert, &optind, 0);
Harald Welte6185c4b2001-09-02 15:20:11 +000084 parse_lengths(argv[optind-1], info);
85 if (invert)
86 info->invert = 1;
87 *flags = 1;
88 break;
89
90 default:
91 return 0;
92 }
93 return 1;
94}
95
96/* Final check; must have specified --length. */
97static void
98final_check(unsigned int flags)
99{
100 if (!flags)
101 exit_error(PARAMETER_PROBLEM,
102 "length: You must specify `--length'");
103}
104
105/* Common match printing code. */
106static void
107print_length(struct ip6t_length_info *info)
108{
109 if (info->invert)
Gerry Skerbitzc8c0f402002-12-05 20:37:22 +0000110 printf("! ");
Harald Welte6185c4b2001-09-02 15:20:11 +0000111
112 if (info->max == info->min)
113 printf("%u ", info->min);
114 else
115 printf("%u:%u ", info->min, info->max);
116}
117
118/* Prints out the matchinfo. */
119static void
120print(const struct ip6t_ip6 *ip,
121 const struct ip6t_entry_match *match,
122 int numeric)
123{
124 printf("length ");
125 print_length((struct ip6t_length_info *)match->data);
126}
127
128/* Saves the union ip6t_matchinfo in parsable form to stdout. */
129static void
130save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
131{
132 printf("--length ");
133 print_length((struct ip6t_length_info *)match->data);
134}
135
Harald Welte02aa7332005-02-01 15:38:20 +0000136struct ip6tables_match length = {
137 .name = "length",
138 .version = IPTABLES_VERSION,
139 .size = IP6T_ALIGN(sizeof(struct ip6t_length_info)),
140 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_length_info)),
141 .help = &help,
Harald Welte02aa7332005-02-01 15:38:20 +0000142 .parse = &parse,
143 .final_check = &final_check,
144 .print = &print,
145 .save = &save,
146 .extra_opts = opts,
Harald Welte6185c4b2001-09-02 15:20:11 +0000147};
148
149void _init(void)
150{
151 register_match6(&length);
152}