1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* IPv6 Hop Limit matching module
* Maciej Soltysiak <solt@dns.toxicfilms.tv>
* Based on HW's ttl match
* This program is released under the terms of GNU GPL
* Cleanups by Stephane Ouellette <ouellettes@videotron.ca>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <ip6tables.h>
#include <linux/netfilter_ipv6/ip6_tables.h>
#include <linux/netfilter_ipv6/ip6t_hl.h>
static void help(void)
{
printf(
"HL match v%s options:\n"
" --hl-eq [!] value Match hop limit value\n"
" --hl-lt value Match HL < value\n"
" --hl-gt value Match HL > value\n"
, IPTABLES_VERSION);
}
static int parse(int c, char **argv, int invert, unsigned int *flags,
const void *entry,
struct xt_entry_match **match)
{
struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data;
u_int8_t value;
check_inverse(optarg, &invert, &optind, 0);
value = atoi(argv[optind-1]);
if (*flags)
exit_error(PARAMETER_PROBLEM,
"Can't specify HL option twice");
if (!optarg)
exit_error(PARAMETER_PROBLEM,
"hl: You must specify a value");
switch (c) {
case '2':
if (invert)
info->mode = IP6T_HL_NE;
else
info->mode = IP6T_HL_EQ;
/* is 0 allowed? */
info->hop_limit = value;
*flags = 1;
break;
case '3':
if (invert)
exit_error(PARAMETER_PROBLEM,
"hl: unexpected `!'");
info->mode = IP6T_HL_LT;
info->hop_limit = value;
*flags = 1;
break;
case '4':
if (invert)
exit_error(PARAMETER_PROBLEM,
"hl: unexpected `!'");
info->mode = IP6T_HL_GT;
info->hop_limit = value;
*flags = 1;
break;
default:
return 0;
}
return 1;
}
static void final_check(unsigned int flags)
{
if (!flags)
exit_error(PARAMETER_PROBLEM,
"HL match: You must specify one of "
"`--hl-eq', `--hl-lt', `--hl-gt'");
}
static void print(const void *ip,
const struct xt_entry_match *match,
int numeric)
{
static const char *op[] = {
[IP6T_HL_EQ] = "==",
[IP6T_HL_NE] = "!=",
[IP6T_HL_LT] = "<",
[IP6T_HL_GT] = ">" };
const struct ip6t_hl_info *info =
(struct ip6t_hl_info *) match->data;
printf("HL match HL %s %u ", op[info->mode], info->hop_limit);
}
static void save(const void *ip,
const struct xt_entry_match *match)
{
static const char *op[] = {
[IP6T_HL_EQ] = "eq",
[IP6T_HL_NE] = "eq !",
[IP6T_HL_LT] = "lt",
[IP6T_HL_GT] = "gt" };
const struct ip6t_hl_info *info =
(struct ip6t_hl_info *) match->data;
printf("--hl-%s %u ", op[info->mode], info->hop_limit);
}
static const struct option opts[] = {
{ .name = "hl", .has_arg = 1, .flag = 0, .val = '2' },
{ .name = "hl-eq", .has_arg = 1, .flag = 0, .val = '2' },
{ .name = "hl-lt", .has_arg = 1, .flag = 0, .val = '3' },
{ .name = "hl-gt", .has_arg = 1, .flag = 0, .val = '4' },
{ 0 }
};
static
struct ip6tables_match hl = {
.name = "hl",
.version = IPTABLES_VERSION,
.size = IP6T_ALIGN(sizeof(struct ip6t_hl_info)),
.userspacesize = IP6T_ALIGN(sizeof(struct ip6t_hl_info)),
.help = &help,
.parse = &parse,
.final_check = &final_check,
.print = &print,
.save = &save,
.extra_opts = opts
};
void _init(void)
{
register_match6(&hl);
}
|