-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvirtEgressLookup.c
370 lines (302 loc) · 9.86 KB
/
virtEgressLookup.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (C) 2014 Joshua Hare, Lance Hartung, and Suman Banerjee.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/time.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/ip.h> /* struct iphdr */
#include <linux/tcp.h> /* struct tcphdr */
#include <linux/udp.h> /* struct udphdr */
#include <linux/skbuff.h>
#include "virt.h"
#include "virtDebug.h"
#include "virtParse.h"
#include "virtPolicy.h"
#include "virtPolicyTypes.h"
#include "virtDevList.h"
#include "virtEgressLookup.h"
#include "virtFlowTable.h"
#include "virtSelectInterface.h"
#include "virtNAT.h"
// GLOBALS
#define STATE_INIT 0
#define STATE_SYN_SENT 1
#define STATE_SYN_RECV 2
#define STATE_ESTABLISHED 3
#define STATE_FIN_SENT_1 4
#define STATE_FIN_WAIT_1 5
#define STATE_FIN_SENT_2 6
#define STATE_CLOSED 7
#define STATE_DNS_REQUEST 11
#define STATE_DNS_RESPONSE 12
#define STATE_UNKNOWN 13
// FUNCTION PROTOTYPES
static struct flow_table_entry *alloc_init_table_entry(struct packet *pkt);
static int track_connection(struct flow_table_entry *entry, struct packet *pkt);
static unsigned long next_flow_id = 0;
/*
* This function is called from virt_tx after the packet has been parsed.
* The function will lookup up and fill in the routing info in the packet
* structure.
*/
int virt_egress_lookup_flow(struct virt_priv *virt, struct packet *pkt)
{
struct flow_table_entry *entry = NULL;
unsigned long now = jiffies;
if(!pkt)
return -1;
entry = flow_table_lookup(&virt->flow_table, pkt->key);
VIRT_DBG("egress %pI4:%hu -> %pI4:%hu %s\n",
&pkt->key->sAddr,
ntohs(pkt->key->sPort),
&pkt->key->dAddr,
ntohs(pkt->key->dPort),
entry ? "found" : "not found");
// if new flow build entry
if( entry == NULL ) {
// if table miss then we need to lookup the policy
entry = alloc_init_table_entry(pkt);
if( !entry )
return -ENOMEM;
goto setup_defaults;
} else {
pkt->tbl_hit = 1; // ok to free pkt->key
// set info in packet structure
//pkt->actions = entry->actions;
//pkt->policy = entry->policy;
pkt->flow_stats = entry->flow_stats;
pkt->ftable_entry = entry;
entry->last_touched = now;
}
// Copy the flow's policy mask to the packet
pkt->policy = entry->policy;
// track connection state
track_connection(entry, pkt);
return 0;
setup_defaults:
//entry->key = pkt->key; // don't free pkt->key
pkt->ftable_entry = entry;
/* Have connections that originate locally use the default tunnel port.
* This only affects ENCAP flows. */
entry->rx_port = htons(virt_tunnel_source_port());
// lookup policy
entry->policy = virt_policy_lookup(virt, pkt, EGRESS);
pkt->policy = entry->policy;
if(entry->policy) {
entry->action = entry->policy->action;
}
entry->last_touched = now;
//pkt->actions->flow_table_entry = (void *)entry;
pkt->tbl_hit = 1;
flow_table_add(&virt->flow_table, entry);
// track connection state
track_connection(entry, pkt);
return 0;
}
// TODO: Why is this in virtEgressLookup.c?
int virt_ingress_lookup_flow(struct virt_priv *virt, struct packet *pkt)
{
struct flow_table_entry *entry = NULL;
unsigned long now = jiffies;
// hash table lookup
entry = flow_table_lookup(&virt->flow_table, pkt->key);
// packet might have been NAT'ed
if( entry == NULL ) {
struct nat_entry *nat = nat_table_ingress_lookup(pkt);
if( nat != NULL ) {
VIRT_DBG("ingress flow was nated\n");
pkt->key->sAddr = nat->oldip;
pkt->key->sPort = nat->oldport;
// lookup the flow again
entry = flow_table_lookup(&virt->flow_table, pkt->key);
VIRT_DBG("ingress (2) %pI4:%hu -> %pI4:%hu %s\n",
&pkt->key->sAddr,
ntohs(pkt->key->sPort),
&pkt->key->dAddr,
ntohs(pkt->key->dPort),
entry ? "found" : "not found");
// update flow info
}
}
// if new flow build entry
if( entry == NULL ) {
entry = alloc_init_table_entry(pkt);
if( !entry )
return -ENOMEM;
// if table miss then we need to lookup the policy
goto setup_defaults;
} else {
pkt->tbl_hit = 1; // ok to free pkt->key
// set info in packet structure
//pkt->actions = entry->actions;
pkt->policy = entry->policy;
pkt->flow_stats = entry->flow_stats;
pkt->ftable_entry = entry;
entry->last_touched = now;
}
// track connection state
track_connection(entry, pkt);
return 0;
setup_defaults:
pkt->ftable_entry = entry;
entry->rx_port = pkt->key->sPort;
// lookup policy
entry->policy = virt_policy_lookup(virt, pkt, INGRESS);
pkt->policy = entry->policy;
if(entry->policy) {
entry->action = entry->policy->action;
}
entry->last_touched = now;
//pkt->actions->flow_table_entry = (void *)entry;
pkt->tbl_hit = 1;
flow_table_add(&virt->flow_table, entry);
// track connection state
track_connection(entry, pkt);
return 0;
}
static int track_udp_connection(struct flow_table_entry *entry, struct packet *pkt)
{
// handle dns
return 0;
}
/*
* This function tracks the state of a TCP connection. Each state name reflects
* the state to which the tcp connection will be at the next packet.
*/
static int track_tcp_connection(struct flow_table_entry *entry, struct packet *pkt)
{
struct tcphdr *tcp = pkt->hdr_ptrs->tcp_ptr;
// implement TCP state table
switch( entry->state ) {
case STATE_INIT:
if( tcp->syn ) {
entry->state = STATE_SYN_SENT;
} else {
entry->state = STATE_UNKNOWN;
}
break;
case STATE_SYN_SENT:
if( tcp->rst ) {
entry->state = STATE_CLOSED;
} else if ( (tcp->syn) && (tcp->ack) ) {
entry->state = STATE_SYN_RECV;
} else {
entry->state = STATE_UNKNOWN;
}
break;
case STATE_SYN_RECV:
if( tcp->rst ) {
entry->state = STATE_CLOSED;
} else if ( tcp->ack ) {
entry->state = STATE_ESTABLISHED;
} else {
entry->state = STATE_UNKNOWN;
}
break;
case STATE_ESTABLISHED:
if( tcp->rst ) {
entry->state = STATE_CLOSED;
} else if( tcp->fin ) {
entry->state = STATE_FIN_SENT_1;
} else {
entry->state = STATE_UNKNOWN;
}
break;
case STATE_FIN_SENT_1:
if( tcp->rst ) {
entry->state = STATE_CLOSED;
} else if( (tcp->fin) && (tcp->ack) ) {
entry->state = STATE_FIN_SENT_2;
} else if( tcp->ack ) {
entry->state = STATE_FIN_WAIT_1;
} else {
entry->state = STATE_UNKNOWN;
}
case STATE_FIN_WAIT_1:
if( tcp->rst ) {
entry->state = STATE_CLOSED;
} else if( tcp->fin ) {
entry->state = STATE_FIN_SENT_2;
} else {
entry->state = STATE_UNKNOWN;
}
break;
case STATE_FIN_SENT_2:
if( tcp->ack ) {
entry->state = STATE_CLOSED;
} else {
entry->state = STATE_UNKNOWN;
}
break;
case STATE_CLOSED:
if( tcp->ack ) {
entry->state = STATE_CLOSED;
} else {
entry->state = STATE_UNKNOWN;
}
break;
default:
break;
}
return 0;
}
static int track_other_connection(struct flow_table_entry *entry, struct packet *pkt)
{
// ping and arp probably don't need to be stored so have some
// default actions/operations for such protocols
return 0;
}
static int track_connection(struct flow_table_entry *entry, struct packet *pkt)
{
int rtn = 0;
struct iphdr *ip = (struct iphdr *)pkt->hdr_ptrs->ip_ptr;
if( !ip )
return rtn; //may not be an ip packet (i.e., arp)
if( ip->protocol == IPPROTO_TCP ) {
rtn = track_tcp_connection(entry, pkt);
} else if( ip->protocol == IPPROTO_UDP ) {
rtn = track_udp_connection(entry, pkt);
} else {
rtn = track_other_connection(entry, pkt);
}
return rtn;
}
/*
* This function will allocate and initialize a new hash entry for a given
* flow.
*/
static struct flow_table_entry *alloc_init_table_entry(struct packet *pkt)
{
struct flow_table_entry *entry;
struct virt_priv *virt = netdev_priv(pkt->master);
entry = alloc_flow_table_entry();
if(!entry)
return NULL;
entry->network = &virt->network;
memcpy(entry->key, pkt->key, sizeof(struct flow_tuple)); // not need with tbl_hit flag
entry->flow_stats->flow_id = next_flow_id++;
//pkt->actions = entry->actions;
pkt->flow_stats = entry->flow_stats;
return entry;
}