-
Notifications
You must be signed in to change notification settings - Fork 346
/
ofctrl_packetout.go
544 lines (499 loc) · 16.6 KB
/
ofctrl_packetout.go
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright 2020 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openflow
import (
"encoding/binary"
"math/rand"
"net"
"time"
"antrea.io/libOpenflow/openflow15"
"antrea.io/libOpenflow/protocol"
"antrea.io/libOpenflow/util"
"antrea.io/ofnet/ofctrl"
"k8s.io/klog/v2"
)
// #nosec G404: random number generator not used for security purposes
var pktRand = rand.New(rand.NewSource(time.Now().UnixNano()))
type ofPacketOutBuilder struct {
pktOut *ofctrl.PacketOut
icmpID *uint16
icmpSeq *uint16
}
// SetSrcMAC sets the packet's source MAC with the provided value.
func (b *ofPacketOutBuilder) SetSrcMAC(mac net.HardwareAddr) PacketOutBuilder {
b.pktOut.SrcMAC = mac
return b
}
// SetDstMAC sets the packet's destination MAC with the provided value.
func (b *ofPacketOutBuilder) SetDstMAC(mac net.HardwareAddr) PacketOutBuilder {
b.pktOut.DstMAC = mac
return b
}
// SetSrcIP sets the packet's source IP with the provided value.
func (b *ofPacketOutBuilder) SetSrcIP(ip net.IP) PacketOutBuilder {
if ip.To4() != nil {
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
b.pktOut.IPHeader.NWSrc = ip
} else {
if b.pktOut.IPv6Header == nil {
b.pktOut.IPv6Header = new(protocol.IPv6)
}
b.pktOut.IPv6Header.NWSrc = ip
}
return b
}
// SetDstIP sets the packet's destination IP with the provided value.
func (b *ofPacketOutBuilder) SetDstIP(ip net.IP) PacketOutBuilder {
if ip.To4() != nil {
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
b.pktOut.IPHeader.NWDst = ip
} else {
if b.pktOut.IPv6Header == nil {
b.pktOut.IPv6Header = new(protocol.IPv6)
}
b.pktOut.IPv6Header.NWDst = ip
}
return b
}
// SetIPProtocol sets IP protocol in the packet's IP header.
func (b *ofPacketOutBuilder) SetIPProtocol(proto Protocol) PacketOutBuilder {
switch proto {
case ProtocolTCPv6, ProtocolUDPv6, ProtocolSCTPv6, ProtocolICMPv6:
if b.pktOut.IPv6Header == nil {
b.pktOut.IPv6Header = new(protocol.IPv6)
}
default:
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
}
switch proto {
case ProtocolTCPv6:
b.pktOut.IPv6Header.NextHeader = protocol.Type_TCP
case ProtocolUDPv6:
b.pktOut.IPv6Header.NextHeader = protocol.Type_UDP
case ProtocolSCTPv6:
b.pktOut.IPv6Header.NextHeader = 0x84
case ProtocolICMPv6:
b.pktOut.IPv6Header.NextHeader = protocol.Type_IPv6ICMP
case ProtocolTCP:
b.pktOut.IPHeader.Protocol = protocol.Type_TCP
case ProtocolUDP:
b.pktOut.IPHeader.Protocol = protocol.Type_UDP
case ProtocolSCTP:
b.pktOut.IPHeader.Protocol = 0x84
case ProtocolICMP:
b.pktOut.IPHeader.Protocol = protocol.Type_ICMP
case ProtocolIGMP:
b.pktOut.IPHeader.Protocol = protocol.Type_IGMP
default:
b.pktOut.IPHeader.Protocol = 0xff
}
return b
}
// SetIPProtocolValue sets IP protocol in the packet's IP header with the
// intetger protocol value.
func (b *ofPacketOutBuilder) SetIPProtocolValue(isIPv6 bool, protoValue uint8) PacketOutBuilder {
if isIPv6 {
if b.pktOut.IPv6Header == nil {
b.pktOut.IPv6Header = new(protocol.IPv6)
}
b.pktOut.IPv6Header.NextHeader = protoValue
} else {
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
b.pktOut.IPHeader.Protocol = protoValue
}
return b
}
// SetTTL sets TTL in the packet's IP header.
func (b *ofPacketOutBuilder) SetTTL(ttl uint8) PacketOutBuilder {
if b.pktOut.IPv6Header == nil {
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
b.pktOut.IPHeader.TTL = ttl
} else {
b.pktOut.IPv6Header.HopLimit = ttl
}
return b
}
// SetIPFlags sets flags in the packet's IP header. IPv4 only.
func (b *ofPacketOutBuilder) SetIPFlags(flags uint16) PacketOutBuilder {
if b.pktOut.IPv6Header == nil {
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
b.pktOut.IPHeader.Flags = flags
}
return b
}
// SetIPHeaderID sets identifier field in the packet's IP header. IPv4 only.
func (b *ofPacketOutBuilder) SetIPHeaderID(id uint16) PacketOutBuilder {
if b.pktOut.IPv6Header == nil {
if b.pktOut.IPHeader == nil {
b.pktOut.IPHeader = new(protocol.IPv4)
}
b.pktOut.IPHeader.Id = id
}
return b
}
// SetTCPSrcPort sets the source port in the packet's TCP header.
func (b *ofPacketOutBuilder) SetTCPSrcPort(port uint16) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.PortSrc = port
return b
}
// SetTCPDstPort sets the destination port in the packet's TCP header.
func (b *ofPacketOutBuilder) SetTCPDstPort(port uint16) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.PortDst = port
return b
}
// SetTCPFlags sets the flags in the packet's TCP header.
func (b *ofPacketOutBuilder) SetTCPFlags(flags uint8) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.Code = flags
return b
}
func (b *ofPacketOutBuilder) SetTCPSeqNum(seqNum uint32) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.SeqNum = seqNum
return b
}
func (b *ofPacketOutBuilder) SetTCPAckNum(ackNum uint32) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.AckNum = ackNum
return b
}
func (b *ofPacketOutBuilder) SetTCPHdrLen(hdrLen uint8) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.HdrLen = hdrLen
return b
}
func (b *ofPacketOutBuilder) SetTCPWinSize(winSize uint16) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.WinSize = winSize
return b
}
func (b *ofPacketOutBuilder) SetTCPData(data []byte) PacketOutBuilder {
if b.pktOut.TCPHeader == nil {
b.pktOut.TCPHeader = new(protocol.TCP)
}
b.pktOut.TCPHeader.Data = data
return b
}
// SetUDPSrcPort sets the source port in the packet's UDP header.
func (b *ofPacketOutBuilder) SetUDPSrcPort(port uint16) PacketOutBuilder {
if b.pktOut.UDPHeader == nil {
b.pktOut.UDPHeader = new(protocol.UDP)
}
b.pktOut.UDPHeader.PortSrc = port
return b
}
// SetUDPDstPort sets the destination port in the packet's UDP header.
func (b *ofPacketOutBuilder) SetUDPDstPort(port uint16) PacketOutBuilder {
if b.pktOut.UDPHeader == nil {
b.pktOut.UDPHeader = new(protocol.UDP)
}
b.pktOut.UDPHeader.PortDst = port
return b
}
// SetICMPType sets the type in the packet's ICMP header.
func (b *ofPacketOutBuilder) SetICMPType(icmpType uint8) PacketOutBuilder {
if b.pktOut.ICMPHeader == nil {
b.pktOut.ICMPHeader = new(protocol.ICMP)
}
b.pktOut.ICMPHeader.Type = icmpType
return b
}
// SetICMPCode sets the code in the packet's ICMP header.
func (b *ofPacketOutBuilder) SetICMPCode(icmpCode uint8) PacketOutBuilder {
if b.pktOut.ICMPHeader == nil {
b.pktOut.ICMPHeader = new(protocol.ICMP)
}
b.pktOut.ICMPHeader.Code = icmpCode
return b
}
// SetICMPID sets the identifier in the packet's ICMP header.
func (b *ofPacketOutBuilder) SetICMPID(id uint16) PacketOutBuilder {
if b.pktOut.ICMPHeader == nil {
b.pktOut.ICMPHeader = new(protocol.ICMP)
}
b.icmpID = &id
return b
}
// SetICMPSequence sets the sequence number in the packet's ICMP header.
func (b *ofPacketOutBuilder) SetICMPSequence(seq uint16) PacketOutBuilder {
if b.pktOut.ICMPHeader == nil {
b.pktOut.ICMPHeader = new(protocol.ICMP)
}
b.icmpSeq = &seq
return b
}
func (b *ofPacketOutBuilder) SetICMPData(data []byte) PacketOutBuilder {
if b.pktOut.ICMPHeader == nil {
b.pktOut.ICMPHeader = new(protocol.ICMP)
}
b.pktOut.ICMPHeader.Data = data
return b
}
func (b *ofPacketOutBuilder) SetUDPData(data []byte) PacketOutBuilder {
if b.pktOut.UDPHeader == nil {
b.pktOut.UDPHeader = new(protocol.UDP)
}
b.pktOut.UDPHeader.Data = data
return b
}
// SetInport sets the in_port field of the packetOut message.
func (b *ofPacketOutBuilder) SetInport(inPort uint32) PacketOutBuilder {
b.pktOut.InPort = inPort
return b
}
// SetOutport sets the output port of the packetOut message. If the message is expected to go through OVS pipeline
// from table0, use openflow15.P_TABLE, which is also the default value.
func (b *ofPacketOutBuilder) SetOutport(outport uint32) PacketOutBuilder {
b.pktOut.OutPort = outport
return b
}
// SetL4Packet sets the L4 packet of the packetOut message. It provides a generic function to create a packet
// of protocol other than TCP/UDP/ICMP.
func (b *ofPacketOutBuilder) SetL4Packet(packet util.Message) PacketOutBuilder {
b.pktOut.IPHeader.Data = packet
return b
}
func (b *ofPacketOutBuilder) SetEthPacket(packet *protocol.Ethernet) PacketOutBuilder {
b.pktOut.EthernetPacket = packet
return b
}
// AddSetIPToSAction sets the IP_TOS field in the packet-out message. The action clears the two ECN bits as 0,
// and only 2-7 bits of the DSCP field in IP header is set.
func (b *ofPacketOutBuilder) AddSetIPTOSAction(data uint8) PacketOutBuilder {
field, _ := openflow15.FindFieldHeaderByName(NxmFieldIPToS, true)
field.Value = &openflow15.IpDscpField{Dscp: data << IPDSCPToSRange.Offset()}
field.Mask = &openflow15.IpDscpField{Dscp: uint8(0xff) >> (8 - IPDSCPToSRange.Length()) << IPDSCPToSRange.Offset()}
act := ofctrl.NewSetFieldAction(field)
b.pktOut.Actions = append(b.pktOut.Actions, act)
return b
}
func (b *ofPacketOutBuilder) AddLoadRegMark(mark *RegMark) PacketOutBuilder {
valueData := mark.value
mask := uint32(0)
if mark.field.rng != nil {
mask = ^mask >> (32 - mark.field.rng.Length()) << mark.field.rng.Offset()
valueData = valueData << mark.field.rng.Offset()
}
tgtField := openflow15.NewRegMatchFieldWithMask(mark.field.regID, valueData, mask)
act := ofctrl.NewSetFieldAction(tgtField)
b.pktOut.Actions = append(b.pktOut.Actions, act)
return b
}
func (b *ofPacketOutBuilder) AddResubmitAction(inPort *uint16, table *uint8) PacketOutBuilder {
act := ofctrl.NewResubmit(inPort, table)
b.pktOut.Actions = append(b.pktOut.Actions, act)
return b
}
func (b *ofPacketOutBuilder) Done() *ofctrl.PacketOut {
if b.pktOut.EthernetPacket != nil {
// Entire ethernet packet is provided. No need to fill L3/L4 header.
return b.pktOut
}
if b.pktOut.IPHeader != nil && b.pktOut.IPv6Header != nil {
klog.Errorf("Invalid PacketOutBuilder: IP header and IPv6 header are not allowed to exist at the same time")
return nil
}
if b.pktOut.IPv6Header == nil {
if b.pktOut.ICMPHeader != nil {
if len(b.pktOut.ICMPHeader.Data) == 0 {
b.setICMPData()
}
b.pktOut.ICMPHeader.Checksum = b.icmpHeaderChecksum()
b.pktOut.IPHeader.Length = 20 + b.pktOut.ICMPHeader.Len()
} else if b.pktOut.TCPHeader != nil {
if b.pktOut.TCPHeader.HdrLen == 0 {
b.pktOut.TCPHeader.HdrLen = 5
}
if b.pktOut.TCPHeader.SeqNum == 0 {
// #nosec G404: random number generator not used for security purposes
b.pktOut.TCPHeader.SeqNum = pktRand.Uint32()
}
if b.pktOut.TCPHeader.AckNum == 0 {
// #nosec G404: random number generator not used for security purposes
b.pktOut.TCPHeader.AckNum = pktRand.Uint32()
}
b.pktOut.TCPHeader.Checksum = b.tcpHeaderChecksum()
b.pktOut.IPHeader.Length = 20 + b.pktOut.TCPHeader.Len()
} else if b.pktOut.UDPHeader != nil {
b.pktOut.UDPHeader.Length = b.pktOut.UDPHeader.Len()
b.pktOut.UDPHeader.Checksum = b.udpHeaderChecksum()
b.pktOut.IPHeader.Length = 20 + b.pktOut.UDPHeader.Len()
} else if b.pktOut.IPHeader.Protocol == protocol.Type_IGMP {
if igmpv1or2, ok := b.pktOut.IPHeader.Data.(*protocol.IGMPv1or2); ok {
igmpv1or2.Checksum = 0
igmpv1or2.Checksum = b.igmpHeaderChecksum()
} else if igmpv3Query, ok := b.pktOut.IPHeader.Data.(*protocol.IGMPv3Query); ok {
igmpv3Query.Checksum = 0
igmpv3Query.Checksum = b.igmpHeaderChecksum()
} else if igmpv3Report, ok := b.pktOut.IPHeader.Data.(*protocol.IGMPv3MembershipReport); ok {
igmpv3Report.Checksum = 0
igmpv3Report.Checksum = b.igmpHeaderChecksum()
}
b.pktOut.IPHeader.Length = 20 + b.pktOut.IPHeader.Data.Len()
}
if b.pktOut.IPHeader.Id == 0 {
// #nosec G404: random number generator not used for security purposes
b.pktOut.IPHeader.Id = uint16(pktRand.Uint32())
}
// Set IP version in the IP Header.
b.pktOut.IPHeader.Version = 0x4
b.pktOut.IPHeader.Checksum = b.ipHeaderChecksum()
} else {
if b.pktOut.ICMPHeader != nil {
if len(b.pktOut.ICMPHeader.Data) == 0 {
b.setICMPData()
}
b.pktOut.ICMPHeader.Checksum = b.icmpHeaderChecksum()
b.pktOut.IPv6Header.Length = b.pktOut.ICMPHeader.Len()
} else if b.pktOut.TCPHeader != nil {
if b.pktOut.TCPHeader.HdrLen == 0 {
b.pktOut.TCPHeader.HdrLen = 5
}
if b.pktOut.TCPHeader.SeqNum == 0 {
// #nosec G404: random number generator not used for security purposes
b.pktOut.TCPHeader.SeqNum = pktRand.Uint32()
}
if b.pktOut.TCPHeader.AckNum == 0 {
// #nosec G404: random number generator not used for security purposes
b.pktOut.TCPHeader.AckNum = pktRand.Uint32()
}
b.pktOut.TCPHeader.Checksum = b.tcpHeaderChecksum()
b.pktOut.IPv6Header.Length = b.pktOut.TCPHeader.Len()
} else if b.pktOut.UDPHeader != nil {
b.pktOut.UDPHeader.Length = b.pktOut.UDPHeader.Len()
b.pktOut.UDPHeader.Checksum = b.udpHeaderChecksum()
b.pktOut.IPv6Header.Length = b.pktOut.UDPHeader.Len()
}
// Set IPv6 version in the IP Header.
b.pktOut.IPv6Header.Version = 0x6
}
return b.pktOut
}
func (b *ofPacketOutBuilder) setICMPData() {
data := make([]byte, 4)
if b.icmpID != nil {
binary.BigEndian.PutUint16(data, *b.icmpID)
}
if b.icmpSeq != nil {
binary.BigEndian.PutUint16(data[2:], *b.icmpSeq)
}
b.pktOut.ICMPHeader.Data = data
}
func (b *ofPacketOutBuilder) ipHeaderChecksum() uint16 {
ipHeader := *b.pktOut.IPHeader
ipHeader.Checksum = 0
ipHeader.Data = nil
data, _ := ipHeader.MarshalBinary()
return checksum(data)
}
func (b *ofPacketOutBuilder) icmpHeaderChecksum() uint16 {
icmpHeader := *b.pktOut.ICMPHeader
icmpHeader.Checksum = 0
data, _ := icmpHeader.MarshalBinary()
checksumData := data
if b.pktOut.IPv6Header != nil {
checksumData = append(b.generatePseudoHeader(uint16(len(data))), data...)
}
return checksum(checksumData)
}
func (b *ofPacketOutBuilder) tcpHeaderChecksum() uint16 {
tcpHeader := *b.pktOut.TCPHeader
tcpHeader.Checksum = 0
data, _ := tcpHeader.MarshalBinary()
checksumData := append(b.generatePseudoHeader(uint16(len(data))), data...)
return checksum(checksumData)
}
func (b *ofPacketOutBuilder) udpHeaderChecksum() uint16 {
udpHeader := *b.pktOut.UDPHeader
udpHeader.Checksum = 0
data, _ := udpHeader.MarshalBinary()
checksumData := append(b.generatePseudoHeader(uint16(len(data))), data...)
checksum := checksum(checksumData)
// From RFC 768:
// If the computed checksum is zero, it is transmitted as all ones (the
// equivalent in one's complement arithmetic). An all zero transmitted
// checksum value means that the transmitter generated no checksum (for
// debugging or for higher level protocols that don't care).
if checksum == 0 {
checksum = 0xffff
}
return checksum
}
func (b *ofPacketOutBuilder) igmpHeaderChecksum() uint16 {
data, _ := b.pktOut.IPHeader.Data.MarshalBinary()
checksum := checksum(data)
return checksum
}
func (b *ofPacketOutBuilder) generatePseudoHeader(length uint16) []byte {
var pseudoHeader []byte
if b.pktOut.IPv6Header == nil {
pseudoHeader = make([]byte, 12)
copy(pseudoHeader[0:4], b.pktOut.IPHeader.NWSrc.To4())
copy(pseudoHeader[4:8], b.pktOut.IPHeader.NWDst.To4())
pseudoHeader[8] = 0x0
pseudoHeader[9] = b.pktOut.IPHeader.Protocol
binary.BigEndian.PutUint16(pseudoHeader[10:12], length)
} else {
pseudoHeader = make([]byte, 40)
copy(pseudoHeader[0:16], b.pktOut.IPv6Header.NWSrc.To16())
copy(pseudoHeader[16:32], b.pktOut.IPv6Header.NWDst.To16())
binary.BigEndian.PutUint32(pseudoHeader[32:36], uint32(length))
pseudoHeader[36] = 0x0
pseudoHeader[37] = 0x0
pseudoHeader[38] = 0x0
pseudoHeader[39] = b.pktOut.IPv6Header.NextHeader
}
return pseudoHeader
}
func checksum(data []byte) uint16 {
sum := uint32(0)
for ; len(data) >= 2; data = data[2:] {
sum += uint32(data[0])<<8 | uint32(data[1])
}
if len(data) > 0 {
sum += uint32(data[0]) << 8
}
for sum > 0xffff {
sum = (sum >> 16) + (sum & 0xffff)
}
return ^uint16(sum)
}