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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/*
* (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation (or any later at your option).
*
* This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
*/
#include "helper.h"
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dlfcn.h>
int
cthelper_expect_init(struct nf_expect *exp, struct nf_conntrack *master,
uint32_t class,
union nfct_attr_grp_addr *saddr,
union nfct_attr_grp_addr *daddr,
uint8_t l4proto, uint16_t *sport, uint16_t *dport,
uint32_t flags)
{
struct nf_conntrack *expected, *mask;
expected = nfct_new();
if (!expected)
return -1;
mask = nfct_new();
if (!mask)
return -1;
if (saddr) {
switch(nfct_get_attr_u8(master, ATTR_L3PROTO)) {
int i;
uint32_t addr[4] = {};
case AF_INET:
nfct_set_attr_u8(expected, ATTR_L3PROTO, AF_INET);
nfct_set_attr_u32(expected, ATTR_IPV4_SRC, saddr->ip);
nfct_set_attr_u8(mask, ATTR_L3PROTO, AF_INET);
nfct_set_attr_u32(mask, ATTR_IPV4_SRC, 0xffffffff);
break;
case AF_INET6:
nfct_set_attr_u8(expected, ATTR_L3PROTO, AF_INET6);
nfct_set_attr(expected, ATTR_IPV6_SRC, saddr->ip6);
for (i=0; i<4; i++)
memset(&addr[i], 0xffffffff, sizeof(uint32_t));
nfct_set_attr_u8(mask, ATTR_L3PROTO, AF_INET6);
nfct_set_attr(mask, ATTR_IPV6_SRC, addr);
break;
default:
break;
}
} else {
switch(nfct_get_attr_u8(master, ATTR_L3PROTO)) {
int i;
uint32_t addr[4] = {};
case AF_INET:
nfct_set_attr_u8(expected, ATTR_L3PROTO, AF_INET);
nfct_set_attr_u32(expected, ATTR_IPV4_SRC, 0x00000000);
nfct_set_attr_u8(mask, ATTR_L3PROTO, AF_INET);
nfct_set_attr_u32(mask, ATTR_IPV4_SRC, 0x00000000);
break;
case AF_INET6:
for (i=0; i<4; i++)
memset(&addr[i], 0x00000000, sizeof(uint32_t));
nfct_set_attr_u8(expected, ATTR_L3PROTO, AF_INET6);
nfct_set_attr(expected, ATTR_IPV6_SRC, addr);
nfct_set_attr_u8(mask, ATTR_L3PROTO, AF_INET6);
nfct_set_attr(mask, ATTR_IPV6_SRC, addr);
break;
default:
break;
}
}
if (sport) {
switch(l4proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
nfct_set_attr_u8(expected, ATTR_L4PROTO, l4proto);
nfct_set_attr_u16(expected, ATTR_PORT_SRC, *sport);
nfct_set_attr_u8(mask, ATTR_L4PROTO, l4proto);
nfct_set_attr_u16(mask, ATTR_PORT_SRC, 0xffff);
break;
default:
break;
}
} else {
switch(l4proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
nfct_set_attr_u8(expected, ATTR_L4PROTO, l4proto);
nfct_set_attr_u16(expected, ATTR_PORT_SRC, 0x0000);
nfct_set_attr_u8(mask, ATTR_L4PROTO, l4proto);
nfct_set_attr_u16(mask, ATTR_PORT_SRC, 0x0000);
break;
default:
break;
}
}
switch(nfct_get_attr_u8(master, ATTR_L3PROTO)) {
uint32_t addr[4] = {};
int i;
case AF_INET:
nfct_set_attr_u8(expected, ATTR_L3PROTO, AF_INET);
nfct_set_attr_u32(expected, ATTR_IPV4_DST, daddr->ip);
nfct_set_attr_u32(mask, ATTR_IPV4_DST, 0xffffffff);
break;
case AF_INET6:
nfct_set_attr_u8(expected, ATTR_L3PROTO, AF_INET6);
nfct_set_attr(expected, ATTR_IPV6_DST, daddr->ip6);
for (i=0; i<4; i++)
memset(addr, 0xffffffff, sizeof(uint32_t));
nfct_set_attr(mask, ATTR_IPV6_DST, addr);
break;
default:
break;
}
switch(l4proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
nfct_set_attr_u8(expected, ATTR_L4PROTO, l4proto);
nfct_set_attr_u16(expected, ATTR_PORT_DST, *dport);
nfct_set_attr_u8(mask, ATTR_L4PROTO, l4proto);
nfct_set_attr_u16(mask, ATTR_PORT_DST, 0xffff);
break;
default:
break;
}
nfexp_set_attr(exp, ATTR_EXP_MASTER, master);
nfexp_set_attr(exp, ATTR_EXP_EXPECTED, expected);
nfexp_set_attr(exp, ATTR_EXP_MASK, mask);
nfexp_set_attr_u32(exp, ATTR_EXP_FLAGS, flags);
nfct_destroy(expected);
nfct_destroy(mask);
return 0;
}
static int cthelper_expect_cmd(struct nf_expect *exp, int cmd)
{
int ret;
struct nfct_handle *h;
h = nfct_open(EXPECT, 0);
if (!h)
return -1;
ret = nfexp_query(h, cmd, exp);
nfct_close(h);
return ret;
}
int cthelper_add_expect(struct nf_expect *exp)
{
return cthelper_expect_cmd(exp, NFCT_Q_CREATE_UPDATE);
}
int cthelper_del_expect(struct nf_expect *exp)
{
return cthelper_expect_cmd(exp, NFCT_Q_DESTROY);
}
void
cthelper_get_addr_src(struct nf_conntrack *ct, int dir,
union nfct_attr_grp_addr *addr)
{
switch (dir) {
case MYCT_DIR_ORIG:
nfct_get_attr_grp(ct, ATTR_GRP_ORIG_ADDR_SRC, addr);
break;
case MYCT_DIR_REPL:
nfct_get_attr_grp(ct, ATTR_GRP_REPL_ADDR_SRC, addr);
break;
}
}
void
cthelper_get_addr_dst(struct nf_conntrack *ct, int dir,
union nfct_attr_grp_addr *addr)
{
switch (dir) {
case MYCT_DIR_ORIG:
nfct_get_attr_grp(ct, ATTR_GRP_ORIG_ADDR_DST, addr);
break;
case MYCT_DIR_REPL:
nfct_get_attr_grp(ct, ATTR_GRP_REPL_ADDR_DST, addr);
break;
}
}
void cthelper_get_port_src(struct nf_conntrack *ct, int dir, uint16_t *port)
{
switch (dir) {
case MYCT_DIR_ORIG:
*port = nfct_get_attr_u16(ct, ATTR_PORT_SRC);
break;
case MYCT_DIR_REPL:
*port = nfct_get_attr_u16(ct, ATTR_REPL_PORT_SRC);
break;
}
}
void cthelper_get_port_dst(struct nf_conntrack *ct, int dir, uint16_t *port)
{
switch (dir) {
case MYCT_DIR_ORIG:
*port = nfct_get_attr_u16(ct, ATTR_PORT_DST);
break;
case MYCT_DIR_REPL:
*port = nfct_get_attr_u16(ct, ATTR_REPL_PORT_DST);
break;
}
}
|