-
Notifications
You must be signed in to change notification settings - Fork 0
/
nat64_session.c
304 lines (249 loc) · 7.09 KB
/
nat64_session.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "nat64_session.h"
int tcp_timeout_fsm(struct session_entry *session)
{
if(session->state == ESTABLISHED) {
session_renew(session, TCP_TRANS);
session->state = FOUR_MIN;
return 1;
}
return 0;
}
void tcp4_fsm(struct session_entry *session, const struct tcphdr *tcph)
{
// printk("nat64: [fsm4] Got packet state %d.\n", session->state);
switch(session->state) {
case CLOSED:
break;
case V6_SYN_RCV:
if(tcph->syn) {
session_renew(session, TCP_EST);
session->state = ESTABLISHED;
}
break;
case V4_SYN_RCV:
//if(tcph->syn)
// session_renew(session, TCP_TRANS);
break;
case FOUR_MIN:
if(!tcph->rst) {
session_renew(session, TCP_EST);
session->state = ESTABLISHED;
}
break;
case ESTABLISHED:
if(tcph->fin) {
//session_renew(session, TCP_EST);
session->state = V4_FIN_RCV;
} else if(tcph->rst) {
session_renew(session, TCP_TRANS);
session->state = FOUR_MIN;
} else {
session_renew(session, TCP_EST);
}
break;
case V6_FIN_RCV:
if(tcph->fin) {
session_renew(session, TCP_TRANS);
session->state = V6_FIN_V4_FIN;
} else {
session_renew(session, TCP_EST);
}
break;
case V4_FIN_RCV:
session_renew(session, TCP_EST);
break;
case V6_FIN_V4_FIN:
break;
}
}
void tcp6_fsm(struct session_entry *session, const struct tcphdr *tcph)
{
// printk("nat64: [fsm6] Got packet state %d.\n", session->state);
switch(session->state) {
case CLOSED:
if(tcph->syn) {
session_renew(session, TCP_TRANS);
session->state = V6_SYN_RCV;
}
break;
case V6_SYN_RCV:
if(tcph->syn)
session_renew(session, TCP_TRANS);
break;
case V4_SYN_RCV:
if(tcph->syn) {
session_renew(session, TCP_EST);
session->state = ESTABLISHED;
}
break;
case FOUR_MIN:
if(!tcph->rst) {
session_renew(session, TCP_EST);
session->state = ESTABLISHED;
}
break;
case ESTABLISHED:
if(tcph->fin) {
//session_renew(session, TCP_EST);
session->state = V6_FIN_RCV;
} else if(tcph->rst) {
session_renew(session, TCP_TRANS);
session->state = FOUR_MIN;
} else {
session_renew(session, TCP_EST);
}
break;
case V6_FIN_RCV:
session_renew(session, TCP_EST);
break;
case V4_FIN_RCV:
if(tcph->fin) {
session_renew(session, TCP_TRANS);
session->state = V6_FIN_V4_FIN;
} else {
session_renew(session, TCP_EST);
}
break;
case V6_FIN_V4_FIN:
break;
}
}
static inline int bib_allocate_local4_port(__be32 addr, __be16 port, int type)
{
struct hlist_node *node;
struct bib_entry *entry;
int min, max, i;
int flag = 0;
port = ntohs(port);
min = port < 1024 ? 0 : 1024;
max = port < 1024 ? 1023 : 65535;
for (i = port; i <= max; i += 2, flag = 0) {
hlist_for_each(node, &state.hash4[htons(i)]) {
entry = hlist_entry(node, struct bib_entry, bylocal);
if(entry->type == type && entry->local4_addr == addr) {
flag = 1;
break;
}
}
if(!flag)
return htons(i);
}
flag = 0;
for (i = port - 2; i >= min; i -=2, flag = 0) {
hlist_for_each(node, &state.hash4[htons(i)]) {
entry = hlist_entry(node, struct bib_entry, bylocal);
if(entry->type == type && entry->local4_addr == addr) {
flag = 1;
break;
}
}
if(!flag)
return htons(i);
}
return -1;
}
struct bib_entry *bib_ipv6_lookup(struct in6_addr *remote_addr, __be16 remote_port, int type)
{
struct hlist_node *pos;
struct bib_entry *bib;
__be16 h = nat64_hash6(*remote_addr, remote_port);
struct hlist_head *hlist = &state.hash6[h];
hlist_for_each(pos, hlist) {
bib = hlist_entry(pos, struct bib_entry, byremote);
if(bib->type == type && bib->remote6_port == remote_port && memcmp(&bib->remote6_addr, remote_addr, sizeof(*remote_addr)) == 0)
return bib;
}
//return (pos ? bib : NULL);
return NULL;
}
struct bib_entry *bib_ipv4_lookup(__be32 local_addr, __be16 local_port, int type)
{
struct hlist_node *pos;
struct bib_entry *bib;
__be16 h = nat64_hash4(local_addr, local_port);
struct hlist_head *hlist = &state.hash4[h];
hlist_for_each(pos, hlist) {
bib = hlist_entry(pos, struct bib_entry, bylocal);
if(bib->type == type && bib->local4_addr == local_addr && bib->local4_port == local_port)
return bib;
}
//return (pos ? bib : NULL);
return NULL;
}
struct bib_entry *bib_create(struct in6_addr *remote6_addr, __be16 remote6_port,
__be32 local4_addr, __be16 local4_port, int type)
{
struct bib_entry *bib;
bib = kmem_cache_zalloc(state.bib_cache, GFP_ATOMIC);
if (!bib) {
printk("nat64: [bib] Unable to allocate memory for new bib entry X(.\n");
return NULL;
}
bib->type = type;
memcpy(&bib->remote6_addr, remote6_addr, sizeof(struct in6_addr));
bib->local4_addr = local4_addr;
bib->remote6_port = remote6_port;
bib->local4_port = local4_port;
INIT_LIST_HEAD(&bib->sessions);
//printk("nat64: [bib] New bib %pI6c,%hu <--> %pI4:%hu.\n", remote6_addr, ntohs(remote6_port), &local4_addr, ntohs(local4_port));
return bib;
}
struct bib_entry *bib_session_create(struct in6_addr *saddr, __be32 daddr, __be16 sport, __be16 dport, int protocol, enum expiry_type type)
{
struct bib_entry *bib;
struct session_entry *session;
int local4_port;
__be32 local4_ip = map_6to4(saddr);
//map_6to4(saddr);
local4_port = bib_allocate_local4_port(local4_ip, sport, protocol);
if (local4_port < 0) {
printk("nat64: [bib] Unable to allocate new local IPv4 port. Dropping connection.\n");
return NULL;
}
bib = bib_create(saddr, sport, local4_ip, local4_port, protocol);
if (!bib)
return NULL;
hlist_add_head(&bib->byremote, &state.hash6[nat64_hash6(*saddr, sport)]);
hlist_add_head(&bib->bylocal, &state.hash4[local4_port]);
session = session_create(bib, daddr, dport, type);
if(!session) {
kmem_cache_free(state.bib_cache, bib);
return NULL;
}
return bib;
}
struct session_entry *session_ipv4_lookup(struct bib_entry *bib, __be32 remote4_addr, __be16 remote4_port)
{
struct session_entry *session;
struct list_head *pos;
list_for_each(pos, &bib->sessions) {
session = list_entry(pos, struct session_entry, list);
if(session->remote4_addr == remote4_addr && session->remote4_port == remote4_port)
return session;
}
return NULL;
}
void session_renew(struct session_entry *session, enum expiry_type type)
{
list_del(&session->byexpiry);
session->expires = jiffies + state.expiry_base[type].timeout*HZ;
list_add_tail(&session->byexpiry, &state.expiry_base[type].queue);
//printk("nat64: [session] Renewing session %pI4:%hu (timeout %u sec).\n", &session->remote4_addr, ntohs(session->remote4_port), state.expiry_base[type].timeout);
}
struct session_entry *session_create(struct bib_entry *bib, __be32 addr, __be16 port, enum expiry_type type)
{
struct session_entry *s;
s = kmem_cache_zalloc(state.session_cache, GFP_ATOMIC);
if(!s) {
printk("nat64: [session] Unable to allocate memory for new session entry X(.\n");
return NULL;
}
s->state = CLOSED;
s->remote4_addr = addr;
s->remote4_port = port;
list_add(&s->list, &bib->sessions);
s->expires = jiffies + state.expiry_base[type].timeout*HZ;
list_add_tail(&s->byexpiry, &state.expiry_base[type].queue);
// printk("nat64: [session] New session %pI4:%hu (timeout %u sec).\n", &addr, ntohs(port), state.expiry_base[type].timeout);
return s;
}