forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ofPacket.go
208 lines (194 loc) · 4.96 KB
/
ofPacket.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
// #nosec G404: random number generator not used for security purposes
package ofctrl
import (
"encoding/binary"
"math/rand"
"net"
"antrea.io/libOpenflow/openflow13"
"antrea.io/libOpenflow/protocol"
"antrea.io/libOpenflow/util"
)
type PacketOut struct {
InPort uint32
OutPort uint32
Actions []OFAction
SrcMAC net.HardwareAddr
DstMAC net.HardwareAddr
IPHeader *protocol.IPv4
IPv6Header *protocol.IPv6
TCPHeader *protocol.TCP
UDPHeader *protocol.UDP
ICMPHeader *protocol.ICMP
ARPHeader *protocol.ARP
}
func (p *PacketOut) GetMessage() util.Message {
packetOut := openflow13.NewPacketOut()
packetOut.InPort = p.InPort
for _, act := range p.Actions {
packetOut.AddAction(act.GetActionMessage())
}
packetOut.Data = p.getEthernetHeader()
if p.OutPort > 0 {
packetOut.AddAction(openflow13.NewActionOutput(p.OutPort))
} else {
packetOut.AddAction(openflow13.NewActionOutput(openflow13.P_TABLE))
}
return packetOut
}
func (p *PacketOut) getEthernetHeader() *protocol.Ethernet {
ethPkt := &protocol.Ethernet{
HWDst: p.DstMAC,
HWSrc: p.SrcMAC,
}
var data util.Message
var ethType uint16
if p.ARPHeader != nil {
data = p.ARPHeader
ethType = 0x0806
} else if p.IPv6Header != nil {
switch {
case p.TCPHeader != nil:
p.IPv6Header.NextHeader = protocol.Type_TCP
p.IPv6Header.Data = p.TCPHeader
case p.UDPHeader != nil:
p.IPv6Header.NextHeader = protocol.Type_UDP
p.IPv6Header.Data = p.UDPHeader
case p.ICMPHeader != nil:
p.IPv6Header.NextHeader = protocol.Type_IPv6ICMP
p.IPv6Header.Data = p.ICMPHeader
}
// Let application set IPv6Header.Data and IPv6Header.NextHeader for other IPv6 packets.
data = p.IPv6Header
ethType = protocol.IPv6_MSG
} else {
switch {
case p.TCPHeader != nil:
p.IPHeader.Protocol = protocol.Type_TCP
p.IPHeader.Data = p.TCPHeader
case p.UDPHeader != nil:
p.IPHeader.Protocol = protocol.Type_UDP
p.IPHeader.Data = p.UDPHeader
case p.ICMPHeader != nil:
p.IPHeader.Protocol = protocol.Type_ICMP
p.IPHeader.Data = p.ICMPHeader
}
// Let application set IPHeader.Data and IPHeader.Protocol for other IPv4 packets.
data = p.IPHeader
ethType = 0x0800
}
ethPkt.Ethertype = ethType
ethPkt.Data = data
return ethPkt
}
func (p *PacketIn) GetMatches() *Matchers {
matches := make([]*MatchField, 0, len(p.Match.Fields))
for i := range p.Match.Fields {
matches = append(matches, NewMatchField(&p.Match.Fields[i]))
}
return &Matchers{matches: matches}
}
func GenerateTCPPacket(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, dstPort, srcPort uint16, tcpFlags *uint8) *PacketOut {
tcpHeader := GenerateTCPHeader(dstPort, srcPort, tcpFlags)
var pktOut *PacketOut
if srcIP.To4() == nil {
ipv6Header := &protocol.IPv6{
Version: 6,
Length: tcpHeader.Len(),
HopLimit: 64,
NextHeader: protocol.Type_TCP,
NWSrc: srcIP,
NWDst: dstIP,
}
pktOut = &PacketOut{
SrcMAC: srcMAC,
DstMAC: dstMAC,
IPv6Header: ipv6Header,
TCPHeader: tcpHeader,
}
} else {
ipHeader := &protocol.IPv4{
Version: 4,
IHL: 5,
Length: 20 + tcpHeader.Len(),
Id: uint16(rand.Int()),
Flags: 0,
FragmentOffset: 0,
TTL: 64,
Protocol: protocol.Type_TCP,
Checksum: 0,
NWSrc: srcIP,
NWDst: dstIP,
}
pktOut = &PacketOut{
SrcMAC: srcMAC,
DstMAC: dstMAC,
IPHeader: ipHeader,
TCPHeader: tcpHeader,
}
}
return pktOut
}
func GenerateSimpleIPPacket(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP) *PacketOut {
icmpHeader := GenerateICMPHeader(nil, nil)
ipHeader := &protocol.IPv4{
Version: 4,
IHL: 5,
Length: 20 + icmpHeader.Len(),
Id: uint16(rand.Int()),
Flags: 0,
FragmentOffset: 0,
TTL: 64,
Protocol: protocol.Type_ICMP,
Checksum: 0,
NWSrc: srcIP,
NWDst: dstIP,
}
pktOut := &PacketOut{
SrcMAC: srcMAC,
DstMAC: dstMAC,
IPHeader: ipHeader,
ICMPHeader: icmpHeader,
}
return pktOut
}
func GenerateTCPHeader(dstPort, srcPort uint16, flags *uint8) *protocol.TCP {
header := protocol.NewTCP()
if dstPort != 0 {
header.PortDst = dstPort
} else {
header.PortDst = uint16(rand.Uint32())
}
if srcPort != 0 {
header.PortSrc = srcPort
} else {
header.PortSrc = uint16(rand.Uint32())
}
header.AckNum = rand.Uint32()
header.AckNum = header.AckNum + 1
header.HdrLen = 20
if flags != nil {
header.Code = *flags
} else {
header.Code = uint8(1 << 1)
}
return header
}
func GenerateICMPHeader(icmpType, icmpCode *uint8) *protocol.ICMP {
header := protocol.NewICMP()
if icmpType != nil {
header.Type = *icmpType
} else {
header.Type = 8
}
if icmpCode != nil {
header.Code = *icmpCode
} else {
header.Code = 0
}
identifier := uint16(rand.Uint32())
seq := uint16(1)
data := make([]byte, 4)
binary.BigEndian.PutUint16(data, identifier)
binary.BigEndian.PutUint16(data[2:], seq)
return header
}