-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStateful_Firewall.v
413 lines (383 loc) · 11.2 KB
/
Stateful_Firewall.v
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//===============================================================//
// Module name: Stateful Firewall for Unified Security Gateway (USG);
// Communication with lijunnan(lijunnan@nudt.edu.cn)
// Last edited time: 2018/11/16
// Function outline: USG_v0.1
//===============================================================//
`timescale 1ns/1ps
module Stateful_Firewall(
clk,
reset,
pktin_data_wr,
pktin_data,
pktin_ready,
pktout_data_wr,
pktout_data,
pktout_ready,
/** cin/cou used as control signals */
cin_data_wr,
cin_data,
cin_ready,
cout_data_wr,
cout_data,
cout_ready
);
/* width or depth or words info. of signals
*/
parameter LMID = 8, // lmid of stateful firewall;
w_pkt = 134, // the width of packet if FAST2.0;
// 16b content length, 8b tcpFlag, 32b sendSeq,
// 32b ackSeq, 16b window;
w_tcpInfo = 208 , // width of tcpInfo;
w_meta = 209, // width of metadata_in, includes 1b hitness(allowTb), 104b five-tuple information,
// 16b content length, 8b tcp flag, 32b send seq, 32b ack seq, 16b window;
b_window_pfv = 40; // last bit of window in pfv;
input clk;
input reset;
input pktin_data_wr;
input [w_pkt-1:0] pktin_data;
output reg pktin_ready;
output wire pktout_data_wr;
output wire [w_pkt-1:0] pktout_data;
input pktout_ready;
input cin_data_wr;
input [w_pkt-1:0] cin_data;
output wire cin_ready;
output reg cout_data_wr;
output reg [w_pkt-1:0] cout_data;
input cout_ready;
/*************************************************************************************/
/*** varialbe declaration */
/** fifo used to buffer action */
reg rdreq_action;
wire [1:0] q_action;
wire empty_action;
/** fifo used to buffer pkt */
reg rdreq_pkt;
wire [w_pkt-1:0] q_pkt;
wire empty_pkt;
wire [7:0] usedw_pkt;
/** temps: */
/** interact between alllowTb and sfw */
reg ruleID_valid;
reg [15:0] ruleID;
wire hit_valid;
wire hit;
/** interactive with uniman*/
reg metadata_in_valid;
reg [w_meta-1:0] metadata_in;
wire action_valid;
wire action;
wire uniman_ready;
/** interactive with NMIDcmp*/
wire pktin_data_wr_p;
wire [w_pkt-1:0] pktin_data_p;
reg pktout_data_wr_p;
reg [w_pkt-1:0] pktout_data_p;
/** counter used to record the number of established connnection */
reg [32:0] counter_established, counter_established_set;
reg [32:0] counter_closed, counter_closed_set;
wire cout_data_wr_m;
wire [w_pkt-1:0] cout_data_m;
wire eventInfo_valid;
wire [15:0] eventInfo;
/** states */
reg [3:0] state_proc;
parameter IDLE_S = 4'd0,
READ_HIT_FIFO_S = 4'd1,
READ_FIFO_S = 4'd2,
WAIT_PKT_TAIL_S = 4'd3,
WRITE_META_1_S = 4'd4,
WRITE_META_2_S = 4'd5,
CHANGE_ETH_S = 4'd6,
CHANGE_IP_S = 4'd7,
CHANGE_TCP_S = 4'd8;
reg [1:0] pkt_tag;
reg [31:0] srcIP_temp, dstIP_temp;
reg [15:0] srcPort_temp, dstPort_temp;
reg action_temp, action_valid_temp;
/*** subModules */
NMID_cmp NMIDcmp(
.clk(clk),
.reset(reset),
.pktin_data_wr(pktin_data_wr),
.pktin_data(pktin_data),
.pktin_data_wr_p(pktout_data_wr_p),
.pktin_data_p(pktout_data_p),
.pktout_data_wr_p(pktin_data_wr_p),
.pktout_data_p(pktin_data_p),
.pktout_data_wr(pktout_data_wr),
.pktout_data(pktout_data)
);
defparam
NMIDcmp.LMID = LMID;
uniman_top UniMan(
.clk(clk),
.reset(reset),
.metadata_in_valid(metadata_in_valid),
.metadata_in(metadata_in),
.action_valid(action_valid),
.action(action),
.eventInfo_valid(eventInfo_valid),
.eventInfo(eventInfo),
.ready(uniman_ready),
.cin_data_wr(),
.cin_data(),
.cin_ready(),
.cout_data_wr(),
.cout_data(),
.cout_ready()
);
Firewall_action sfwAction(
.clk(clk),
.reset(reset),
.ruleID_valid(ruleID_valid),
.ruleID(ruleID),
.action_valid(hit_valid),
.action(hit),
.cin_data_wr(cin_data_wr),
.cin_data(cin_data),
.cin_ready(cin_ready),
.cout_data_wr(cout_data_wr_m),
.cout_data(cout_data_m),
.cout_ready(cout_ready)
);
defparam
sfwAction.LMID = LMID;
fifo_134_256 pkt_buffer (
.clk(clk), // input wire clk
.srst(!reset), // input wire srst
.din(pktin_data_p), // input wire [133 : 0] din
.wr_en(pktin_data_wr_p), // input wire wr_en
.rd_en(rdreq_pkt), // input wire rd_en
.dout(q_pkt), // output wire [133 : 0] dout
.full(), // output wire full
.empty(empty_pkt), // output wire empty
.data_count(usedw_pkt) // output wire [7 : 0] data_count
);
fifo_2_32 action_buffer (
.clk(clk), // input wire clk
.srst(!reset), // input wire srst
.din({eventInfo[14],action_temp}), // input wire [133 : 0] din
.wr_en(action_valid_temp), // input wire wr_en
.rd_en(rdreq_action), // input wire rd_en
.dout(q_action), // output wire [133 : 0] dout
.full(), // output wire full
.empty(empty_action) // output wire empty
//.data_count() // output wire [7 : 0] data_count
);
/** maintain action and action_valid */
always @(posedge clk or negedge reset) begin
if (!reset) begin
// output
action_valid_temp <= 1'b0;
action_temp <= 1'b0;
end
else begin
action_valid_temp <= action_valid;
action_temp <= action;
end
end
/** extract ruleID */
always @(posedge clk or negedge reset) begin
if (!reset) begin
// output
ruleID_valid <= 1'b0;
ruleID <= 16'b0;
end
else begin
if((pktin_data_wr_p == 1'b1) && (pktin_data_p[133:132] == 2'b01)) begin
ruleID_valid <= 1'b1;
ruleID <= {2'b0,pktin_data_p[63:50]};
end
else begin
ruleID_valid <= 1'b0;
end
end
end
/*************************************************************************************/
/** state for sending metadata to uniman */
always @(posedge clk or negedge reset) begin
if (!reset) begin
// output signals inilization;
metadata_in_valid <= 1'b0;
metadata_in <= {w_meta{1'b0}};
// intermediate register inilization;
pkt_tag <= 2'b0;
// state inilizaition;
end
else begin
if(pktin_data_wr_p == 1'b1) begin
if(pktin_data_p[133:132] == 2'b01) pkt_tag <= 2'd1;
else if(pkt_tag == 2'd1) begin
metadata_in[207:104] <= pktin_data_p[103:0];
pkt_tag <= 2'd2;
end
else if(pkt_tag == 2'd2) begin
metadata_in[103:0] <= pktin_data_p[127:24];
pkt_tag <= 2'd0;
end
else begin
metadata_in[207:0] <= metadata_in[207:0];
pkt_tag <= 2'd0;
end
if(hit_valid == 1'b1) begin
metadata_in_valid <= 1'b1;
metadata_in[208] <= hit;
end
else begin
metadata_in_valid <= 1'b0;
end
end
else begin
pkt_tag <= 2'b0;
end
end
end
/*************************************************************************************/
/** state for procing packet according to the action returned from uniman;
* update counter, TO DO...;
*/
always @(posedge clk or negedge reset) begin
if (!reset) begin
// output signals inilization;
pktout_data_wr_p <= 1'b0;
pktout_data_p <= {w_pkt{1'b0}};
pktin_ready <= 1'b1;
// intermediate register inilization;
rdreq_action <= 1'b0;
rdreq_pkt <= 1'b0;
srcIP_temp <= 32'b0;
dstIP_temp <= 32'b0;
srcPort_temp <= 16'b0;
dstPort_temp <= 16'b0;
// state inilizaition;
state_proc <= IDLE_S;
end
else begin
if((pktout_ready == 1'b1)&&(usedw_pkt < 8'd200)) pktin_ready <= 1'b1;
else pktin_ready <= 1'b0;
case(state_proc)
IDLE_S: begin
// initialization;
pktout_data_wr_p <= 1'b0;
/** fifo is not empty */
if((empty_action == 1'b0)&&(pktout_ready == 1'b1)) begin
/** read pkt_fifo*/
rdreq_pkt <= 1'b1;
rdreq_action <= 1'b1;
state_proc <= READ_FIFO_S;
end
else begin
state_proc <= IDLE_S;
end
end
READ_FIFO_S: begin
/** action == 1 */
if(q_action[1] == 1'b1) begin
pktout_data_wr_p <= 1'b1;
if(q_pkt[120] == 1'b0)
pktout_data_p <= {q_pkt[133:120], 8'd0, q_pkt[111:0]};
else
pktout_data_p <= {q_pkt[133:120], 8'd1, q_pkt[111:0]};
//pktout_data_p <= q_pkt;
state_proc <= WRITE_META_1_S;
end
/** action == 1 */
else if(q_action[0] == 1'b1) begin
pktout_data_wr_p <= 1'b1;
pktout_data_p <= q_pkt;
state_proc <= WAIT_PKT_TAIL_S;
end
else begin
pktout_data_wr_p <= 1'b0;
state_proc <= WAIT_PKT_TAIL_S;
end
rdreq_action <= 1'b0;
end
WAIT_PKT_TAIL_S: begin
pktout_data_p <= q_pkt;
if(q_pkt[133:132] == 2'b10) begin
state_proc <= IDLE_S;
rdreq_pkt <= 1'b0;
end
else begin
state_proc <= WAIT_PKT_TAIL_S;
end
end
WRITE_META_1_S: begin
pktout_data_p <= q_pkt;
state_proc <= WRITE_META_2_S;
{srcPort_temp,dstPort_temp,srcIP_temp,dstIP_temp} <= q_pkt[95:0];
end
WRITE_META_2_S: begin
pktout_data_p <= q_pkt;
state_proc <= CHANGE_ETH_S;
end
CHANGE_ETH_S: begin
pktout_data_p <= {q_pkt[133:128], q_pkt[79:32], q_pkt[127:80], q_pkt[31:0]};
state_proc <= CHANGE_IP_S;
end
CHANGE_IP_S: begin
pktout_data_p <= {q_pkt[133:48], dstIP_temp, srcIP_temp[31:16]};
state_proc <= CHANGE_TCP_S;
end
CHANGE_TCP_S: begin
pktout_data_p <= {q_pkt[133:128], srcIP_temp[15:0], dstPort_temp, srcPort_temp,
q_pkt[79:8],8'd4};
state_proc <= WAIT_PKT_TAIL_S;
end
default: begin
state_proc <= IDLE_S;
end
endcase
end
end
/*************************************************************************************/
/** state for counting establisehd and closed connections */
always @(posedge clk or negedge reset) begin
if (!reset) begin
// output signals inilization;
cout_data_wr <= 1'b0;
cout_data <= 134'b0;
counter_established <= 33'b0;
counter_closed <= 33'b0;
counter_established_set <= 33'b0;
counter_closed_set <= 33'b0;
// intermediate register inilization;
// state inilizaition;
end
else begin
cout_data_wr <= cout_data_wr_m;
if(cout_data_m[133:132] == 2'b01) begin
/** response packet && SMID == LMID && 1th 32bit */
if((cout_data[111:104] == LMID) && (cout_data_m[126:124] == 3'b011)&&(cout_data_m[66:64] == 3'd1))
cout_data <= {cout_data_m[133:32], counter_established[31:0]};
else
cout_data <= cout_data_m;
end
else begin
cout_data <= cout_data_m;
end
if(cin_data[133:132] == 2'b01) begin
/** write packet and DMID == LMID */
if((cin_data[103:96] == LMID) && (cin_data[126:124] == 3'b010)&&(cin_data[66:64] == 3'd1))
counter_established_set <= {~counter_established_set[32], cin_data[31:0]};
else
counter_established_set <= counter_established_set;
end
else begin
counter_established_set <= counter_established_set;
end
if(counter_established_set[32] == counter_established[32]) begin
if((eventInfo_valid == 1'b1)&&(eventInfo[10] == 1'b1))
counter_established <= counter_established + 33'd1;
else
counter_established <= counter_established;
end
else begin
counter_established <= counter_established_set;
end
end
end
endmodule