-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.p4
252 lines (214 loc) · 6.87 KB
/
program.p4
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
/*
* Copyright 2018 Jan-Jaap Korpershoek
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <core.p4>
#include <v1model.p4>
#define OUT_PORT 2
#define MAX_ADDRESS 32w4194304
#define PKT_THRESHOLD 32w5
#define DAY_THRESHOLD 32w1
// Two weeks
#define TIMEOUT 48w1209600000000
#define DAY 48w86400000000
const bit<16> TYPE_IPV4 = 0x800;
register<bit<32>>(MAX_ADDRESS) pkt;
register<bit<48>>(MAX_ADDRESS) timestamp;
register<bit<32>>(MAX_ADDRESS) days;
// First timestamp of the day
register<bit<48>>(32w1) day_start;
register<bit<1>>(32w1) attack; // Indicates whether an attack is happening or not
// Headers
typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
header ethernet_t {
macAddr_t dstAddr;
macAddr_t srcAddr;
bit<16> etherType;
}
header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
ip4Addr_t srcAddr;
ip4Addr_t dstAddr;
}
header tcp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seqNo;
bit<32> ackNo;
bit<4> dataOffset;
bit<4> res;
bit<8> flags;
bit<16> window;
bit<16> checksum;
bit<16> urgentPtr;
}
struct metadata {
/* empty */
}
struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
tcp_t tcp;
}
// Parser
parser MyParser(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
state start {
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_IPV4: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
8w0x6: parse_tcp;
default: accept;
}
}
state parse_tcp {
packet.extract(hdr.tcp);
transition accept;
}
}
// Checksum verification
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
// Ingress pipeline
control MyIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
action drop() {
mark_to_drop();
}
action get_data(inout bit<32> address ,inout bit<32> num_packets, inout bit<48> ts, inout bit<32> num_days) {
hash(address,
HashAlgorithm.crc32,
32w0,
{ hdr.ipv4.srcAddr },
MAX_ADDRESS);
// Read the files in the registers
pkt.read(num_packets, address);
timestamp.read(ts, address);
days.read(num_days, address);
}
action sliding_window(inout bit<32> num_packets, in bit<48> ts, inout bit<32> num_days) {
// Sliding window
if (standard_metadata.ingress_global_timestamp - ts > TIMEOUT) {
num_packets = 0;
num_days = 0;
}
}
action update_hashtable(inout bit<32> address, inout bit<32> num_packets, inout bit<48> ts, inout bit<32> num_days) {
bit<48> cur_time = standard_metadata.ingress_global_timestamp;
bit<48> ds; // Start of the day
day_start.read(ds, 32w0);
if (ts < ds || ts == 0) { // Last seen was before today or not seen before
num_days = num_days + 1;
}
num_packets = num_packets + 1;
// Write new values to the registers
pkt.write(address, num_packets);
timestamp.write(address, cur_time);
days.write(address, num_days);
}
apply {
standard_metadata.egress_spec = OUT_PORT; // Forward all packets to h2
// Set initial day
bit<48> ds;
day_start.read(ds,32w0);
if (ds == 0) {
ds = standard_metadata.ingress_global_timestamp;
day_start.write(32w0,standard_metadata.ingress_global_timestamp);
}
bit<1> flag;
bit<32> num_packets = 0;
bit<48> ts = 0; // Time stamp
bit<32> num_days = 0;
bit<32> address = 0;
get_data(address, num_packets,ts,num_days);
attack.read(flag, 0);
// Check if under attack
if (flag == 1w1) {
// Sliding_window(num_packets, ts, num_days);
if (num_packets < PKT_THRESHOLD || num_days < DAY_THRESHOLD) {
drop();
}
} else {
update_hashtable(address, num_packets,ts,num_days);
}
// Update start of the day
if (standard_metadata.ingress_global_timestamp - ds > DAY) { // One day
day_start.write(32w0,standard_metadata.ingress_global_timestamp); // A new day has started
}
}
}
// Egress pipeline
control MyEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
apply {
hdr.ethernet.etherType = TYPE_IPV4;
}
}
// Checksum computation
control MyComputeChecksum(inout headers hdr, inout metadata meta) {
apply {
update_checksum(
hdr.ipv4.isValid(),
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr },
hdr.ipv4.hdrChecksum,
HashAlgorithm.csum16);
}
}
// Deparser
control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
packet.emit(hdr.tcp);
}
}
// Switch
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;