-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.p4
483 lines (418 loc) · 13.3 KB
/
basic.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>
const bit<16> TYPE_IPV4 = 0x800;
const bit<16> TYPE_IPV6 = 0x86DD;
const bit<8> TYPE_TCP = 0x6;
const bit<4> VERSION_IPV4 = 0x4;
const bit<4> VERSION_IPV6 = 0x6;
const bit<64> const_1 = 0x736f6d6570736575;
const bit<64> const_2 = 0x646f72616e646f6d;
const bit<64> const_3 = 0x6c7967656e657261;
const bit<64> const_4 = 0x7465646279746573;
/*************************************************************************
*********************** H E A D E R S ***********************************
*************************************************************************/
typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
typedef bit<128> ip6Addr_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 ipv6_t {
bit<4> version;
bit<8> traffic_class;
bit<20> flow_label;
bit<16> length;
bit<8> next_header;
bit<8> hop_limit;
bit<128> srcAddr;
bit<128> dstAddr;
}
header tcp_t {
bit<16> src_port;
bit<16> dst_port;
bit<32> seq_no;
bit<32> ack_no;
}
struct metadata {
bit<64> key_src_1;
bit<64> key_dst_1;
bit<64> key_src_2;
bit<64> key_dst_2;
bit<32> one_time_pad_src;
bit<32> one_time_pad_dst;
bit<30> nonce;
bit<2> version;
ip6Addr_t new_addr;
bit<1> needs_enc;
bit<1> needs_dec;
bit<64> v0;
bit<64> v1;
bit<64> v2;
bit<64> v3;
}
struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
ipv6_t ipv6;
tcp_t tcp;
}
/*************************************************************************
*********************** P A R S E R ***********************************
*************************************************************************/
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;
TYPE_IPV6: parse_ipv6;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
TYPE_TCP: parse_tcp;
default: accept;
}
}
state parse_ipv6 {
packet.extract(hdr.ipv6);
transition select(hdr.ipv6.next_header) {
TYPE_TCP: parse_tcp;
default: accept;
}
}
state parse_tcp {
packet.extract(hdr.tcp);
transition accept;
}
}
/*************************************************************************
************ C H E C K S U M V E R I F I C A T I O N *************
*************************************************************************/
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
/*************************************************************************
************** I N G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
action drop() {
mark_to_drop();
}
// Helper function for SipHash
action sip_round() {
meta.v0 = meta.v0 + meta.v1;
meta.v2 = meta.v2 + meta.v3;
meta.v1 = (bit<64>) (meta.v1 << 13);
meta.v3 = (bit<64>) (meta.v3 << 16);
meta.v1 = meta.v1 ^ meta.v0;
meta.v3 = meta.v3 ^ meta.v2;
meta.v0 = (bit<64>) (meta.v0 << 32);
meta.v2 = meta.v2 + meta.v1;
meta.v0 = meta.v0 + meta.v3;
meta.v1 = (bit<64>) (meta.v1 << 17);
meta.v3 = (bit<64>) (meta.v3 << 21);
meta.v1 = meta.v1 ^ meta.v2;
meta.v3 = meta.v3 ^ meta.v0;
meta.v2 = (bit<64>) (meta.v2 << 32);
}
// Performs SipHash
action sip_hash(bit<64> message, bit<64> key_src, bit<64> key_dst) {
meta.v0 = key_src ^ const_1;
meta.v1 = key_dst ^ const_2;
meta.v2 = key_src ^ const_3;
meta.v3 = key_dst ^ const_4;
meta.v3 = meta.v3 ^ message;
sip_round();
meta.v0 = meta.v0 ^ message;
meta.v2 = meta.v2 ^ 0x00000000000000ff;
sip_round();
sip_round();
bit<64> result = meta.v0 ^ meta.v1 ^ meta.v2 ^ meta.v3;
meta.one_time_pad_src = (bit<32>) result;
meta.one_time_pad_dst = (bit<32>) (result >> 32);
}
// Perform decryption, remove IPv6 header, and restore IPv4 header
action decrypt(bit<64> key_src_0_1, bit<64> key_dst_0_1, bit<64> key_src_1_1, bit<64> key_dst_1_1,
bit<64> key_src_2_1, bit<64> key_dst_2_1, bit<64> key_src_0_2, bit<64> key_dst_0_2,
bit<64> key_src_1_2, bit<64> key_dst_1_2, bit<64> key_src_2_2, bit<64> key_dst_2_2) {
// Get version
bit<2> version = (bit<2>)(hdr.ipv6.dstAddr >> 62);
// Get appropriate keys
if (version == 0) {
meta.key_src_1 = key_src_0_1;
meta.key_dst_1 = key_dst_0_1;
meta.key_src_2 = key_src_0_2;
meta.key_dst_2 = key_dst_0_2;
}
else if (version == 1) {
meta.key_src_1 = key_src_1_1;
meta.key_dst_1 = key_dst_1_1;
meta.key_src_2 = key_src_1_2;
meta.key_dst_2 = key_dst_1_2;
}
else {
meta.key_src_1 = key_src_2_1;
meta.key_dst_1 = key_dst_2_1;
meta.key_src_2 = key_src_2_2;
meta.key_dst_2 = key_dst_2_2;
}
// Decrypt IPv4 addresses and restore IPv4 header
hdr.ipv4.setValid();
hdr.ethernet.etherType = TYPE_IPV4;
hdr.ipv4.srcAddr = (bit<32>) (hdr.ipv6.srcAddr & 0xFFFFFFFF);
hdr.ipv4.dstAddr = (bit<32>) (hdr.ipv6.dstAddr & 0xFFFFFFFF);
hdr.ipv4.version = VERSION_IPV4;
hdr.ipv4.ihl = 5;
hdr.ipv4.totalLen = hdr.ipv6.length + 20;
hdr.ipv4.protocol = hdr.ipv6.next_header;
hdr.ipv4.ttl = hdr.ipv6.hop_limit;
hdr.ipv4.diffserv = hdr.ipv6.traffic_class;
meta.nonce = (bit<30>)(hdr.ipv6.dstAddr >> 32);
sip_hash((bit<64>) meta.nonce, meta.key_src_1, meta.key_dst_1);
hdr.ipv4.srcAddr = hdr.ipv4.srcAddr ^ meta.one_time_pad_src;
hdr.ipv4.dstAddr = hdr.ipv4.dstAddr ^ meta.one_time_pad_dst;
// Decrypt TCP sequence and acknowledgment numbers if present
if (hdr.tcp.isValid()) {
sip_hash((bit<64>) meta.nonce, meta.key_src_2, meta.key_dst_2);
hdr.tcp.seq_no = hdr.tcp.seq_no ^ meta.one_time_pad_src;
hdr.tcp.ack_no = hdr.tcp.ack_no ^ meta.one_time_pad_dst;
}
hdr.ipv6.setInvalid();
}
// Perform encryption, remove IPv4 header, and add IPv6 header
action encrypt(bit<64> key_src_0_1, bit<64> key_dst_0_1, bit<64> key_src_1_1, bit<64> key_dst_1_1, bit<64> key_src_2_1, bit<64> key_dst_2_1,
bit<64> key_src_0_2, bit<64> key_dst_0_2, bit<64> key_src_1_2, bit<64> key_dst_1_2, bit<64> key_src_2_2, bit<64> key_dst_2_2,
bit<2> version) {
// Set version
meta.version = version;
// Choose appropriate keys
if (version == 0) {
meta.key_src_1 = key_src_0_1;
meta.key_dst_1 = key_dst_0_1;
meta.key_src_2 = key_src_0_2;
meta.key_dst_2 = key_dst_0_2;
}
else if (version == 1) {
meta.key_src_1 = key_src_1_1;
meta.key_dst_1 = key_dst_1_1;
meta.key_src_2 = key_src_1_2;
meta.key_dst_2 = key_dst_1_2;
}
else {
meta.key_src_1 = key_src_2_1;
meta.key_dst_1 = key_dst_2_1;
meta.key_src_2 = key_src_2_2;
meta.key_dst_2 = key_dst_2_2;
}
// Encrypt IPv4 addresses
random(meta.nonce , (bit<30>) 0, (bit<30>) ((1 << 30) - 1));
sip_hash((bit<64>) meta.nonce, meta.key_src_1, meta.key_dst_1);
hdr.ipv4.srcAddr = hdr.ipv4.srcAddr ^ meta.one_time_pad_src;
hdr.ipv4.dstAddr = hdr.ipv4.dstAddr ^ meta.one_time_pad_dst;
// Encrypt TCP sequence and acknowledgment numbers if present
if (hdr.tcp.isValid()) {
sip_hash((bit<64>) meta.nonce, meta.key_src_2, meta.key_dst_2);
hdr.tcp.seq_no = hdr.tcp.seq_no ^ meta.one_time_pad_src;
hdr.tcp.ack_no = hdr.tcp.ack_no ^ meta.one_time_pad_dst;
}
// Add IPv6 header
hdr.ipv6.setValid();
hdr.ethernet.etherType = TYPE_IPV6;
hdr.ipv6.dstAddr = meta.new_addr + ((bit<128>)(meta.version) << 62) + ((bit<128>)(meta.nonce) << 32) + (bit<128>)hdr.ipv4.dstAddr;
hdr.ipv6.srcAddr = meta.new_addr + ((bit<128>)(meta.nonce) << 32) + (bit<128>)hdr.ipv4.srcAddr;
hdr.ipv6.version = VERSION_IPV6;
hdr.ipv6.length = hdr.ipv4.totalLen - 20;
hdr.ipv6.next_header = hdr.ipv4.protocol;
hdr.ipv6.hop_limit = hdr.ipv4.ttl;
hdr.ipv6.traffic_class = hdr.ipv4.diffserv;
hdr.ipv4.setInvalid();
}
// Sets needs_enc to 1 and stores the appropriate d_r prefix
action set_needs_enc(bit<128> new_addr) {
meta.new_addr = new_addr;
meta.needs_enc = 1;
}
// Sets needs_dec to 1
action set_needs_dec() {
meta.needs_dec = 1;
}
// Forward IPv4 packets
action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
standard_metadata.egress_spec = port;
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
hdr.ethernet.dstAddr = dstAddr;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
}
// Forward IPv6 packets
action ipv6_forward(macAddr_t dstAddr, egressSpec_t port) {
standard_metadata.egress_spec = port;
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
hdr.ethernet.dstAddr = dstAddr;
hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
}
table ipv6_lpm {
key = {
hdr.ipv6.dstAddr: lpm;
}
actions = {
ipv6_forward;
drop;
NoAction;
}
size = 1024;
default_action = drop();
}
table check_for_decrypt {
key = {
hdr.ipv6.dstAddr: lpm;
}
actions = {
set_needs_dec;
ipv6_forward;
drop;
NoAction;
}
size = 1024;
default_action = drop();
}
table do_decrypt {
key = {
meta.needs_dec: exact;
}
actions = {
decrypt;
NoAction;
}
size = 1024;
default_action = NoAction();
}
table check_for_encrypt {
key = {
hdr.ipv4.dstAddr: lpm;
}
actions = {
set_needs_enc;
NoAction;
}
size = 1024;
default_action = NoAction();
}
table do_encrypt {
key = {
meta.needs_enc: exact;
}
actions = {
encrypt;
NoAction;
}
size = 1024;
default_action = NoAction();
}
table ipv4_lpm {
key = {
hdr.ipv4.dstAddr: lpm;
}
actions = {
ipv4_forward;
drop;
NoAction;
}
size = 1024;
default_action = drop();
}
apply {
if (hdr.ipv6.isValid()) {
check_for_decrypt.apply();
do_decrypt.apply();
}
if (hdr.ipv6.isValid()) {
ipv6_lpm.apply();
}
else if (hdr.ipv4.isValid()) {
ipv4_lpm.apply();
check_for_encrypt.apply();
do_encrypt.apply();
}
}
}
/*************************************************************************
**************** E G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
apply { }
}
/*************************************************************************
************* C H E C K S U M C O M P U T A T I O N **************
*************************************************************************/
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);
}
}
/*************************************************************************
*********************** D E P A R S E R *******************************
*************************************************************************/
control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv6);
packet.emit(hdr.ipv4);
packet.emit(hdr.tcp);
}
}
/*************************************************************************
*********************** S W I T C H *******************************
*************************************************************************/
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;