-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip4.go
246 lines (223 loc) · 6.11 KB
/
ip4.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
package packet
import (
"encoding/binary"
"fmt"
"net"
"sync"
"sync/atomic"
)
type IPv4Option struct {
OptionType uint8
OptionLength uint8
OptionData []byte
}
// IPProtocol is an enumeration of IP protocol values, and acts as a decoder
// for any type it supports.
type IPProtocol uint8
const (
IPProtocolIPv6HopByHop IPProtocol = 0
IPProtocolICMPv4 IPProtocol = 1
IPProtocolIGMP IPProtocol = 2
IPProtocolIPv4 IPProtocol = 4
IPProtocolTCP IPProtocol = 6
IPProtocolUDP IPProtocol = 17
IPProtocolRUDP IPProtocol = 27
IPProtocolIPv6 IPProtocol = 41
IPProtocolIPv6Routing IPProtocol = 43
IPProtocolIPv6Fragment IPProtocol = 44
IPProtocolGRE IPProtocol = 47
IPProtocolESP IPProtocol = 50
IPProtocolAH IPProtocol = 51
IPProtocolICMPv6 IPProtocol = 58
IPProtocolNoNextHeader IPProtocol = 59
IPProtocolIPv6Destination IPProtocol = 60
IPProtocolIPIP IPProtocol = 94
IPProtocolEtherIP IPProtocol = 97
IPProtocolSCTP IPProtocol = 132
IPProtocolUDPLite IPProtocol = 136
IPProtocolMPLSInIP IPProtocol = 137
IPv4_PSEUDO_LENGTH int = 12
)
type IPv4 struct {
Version uint8
IHL uint8
TOS uint8
Length uint16
Id uint16
Flags uint8
FragOffset uint16
TTL uint8
Protocol IPProtocol
Checksum uint16
SrcIP net.IP
DstIP net.IP
Options []IPv4Option
Padding []byte
Payload []byte
headerLength int
}
var (
ipv4Pool *sync.Pool = &sync.Pool{
New: func() interface{} {
return &IPv4{}
},
}
globalIPID uint32
)
func PutIPv4(ip4 *IPv4) {
// clear internal slice references
ip4.SrcIP = nil
ip4.DstIP = nil
ip4.Options = nil
ip4.Padding = nil
ip4.Payload = nil
ipv4Pool.Put(ip4)
}
func GetIPv4() *IPv4 {
var zero IPv4
ip4 := ipv4Pool.Get().(*IPv4)
*ip4 = zero
return ip4
}
func IPID() uint16 {
return uint16(atomic.AddUint32(&globalIPID, 1) & 0x0000ffff)
}
func ParseIPv4(pkt []byte, ip4 *IPv4) error {
flagsfrags := binary.BigEndian.Uint16(pkt[6:8])
ip4.Version = uint8(pkt[0]) >> 4
ip4.IHL = uint8(pkt[0]) & 0x0F
ip4.TOS = pkt[1]
ip4.Length = binary.BigEndian.Uint16(pkt[2:4])
ip4.Id = binary.BigEndian.Uint16(pkt[4:6])
ip4.Flags = uint8(flagsfrags >> 13)
ip4.FragOffset = flagsfrags & 0x1FFF
ip4.TTL = pkt[8]
ip4.Protocol = IPProtocol(pkt[9])
ip4.Checksum = binary.BigEndian.Uint16(pkt[10:12])
ip4.SrcIP = pkt[12:16]
ip4.DstIP = pkt[16:20]
if ip4.Length < 20 {
return fmt.Errorf("Invalid (too small) IP length (%d < 20)", ip4.Length)
}
if ip4.IHL < 5 {
return fmt.Errorf("Invalid (too small) IP header length (%d < 5)", ip4.IHL)
}
if int(ip4.IHL*4) > int(ip4.Length) {
return fmt.Errorf("Invalid IP header length > IP length (%d > %d)", ip4.IHL, ip4.Length)
}
if int(ip4.IHL)*4 > len(pkt) {
return fmt.Errorf("Not all IP header bytes available")
}
ip4.Payload = pkt[ip4.IHL*4:]
rest := pkt[20 : ip4.IHL*4]
// Pull out IP options
for len(rest) > 0 {
if ip4.Options == nil {
// Pre-allocate to avoid growing the slice too much.
ip4.Options = make([]IPv4Option, 0, 4)
}
opt := IPv4Option{OptionType: rest[0]}
switch opt.OptionType {
case 0: // End of options
opt.OptionLength = 1
ip4.Options = append(ip4.Options, opt)
ip4.Padding = rest[1:]
break
case 1: // 1 byte padding
opt.OptionLength = 1
default:
opt.OptionLength = rest[1]
opt.OptionData = rest[2:opt.OptionLength]
}
if len(rest) >= int(opt.OptionLength) {
rest = rest[opt.OptionLength:]
} else {
return fmt.Errorf("IP option length exceeds remaining IP header size, option type %v length %v", opt.OptionType, opt.OptionLength)
}
ip4.Options = append(ip4.Options, opt)
}
return nil
}
func (ip *IPv4) PseudoHeader(buf []byte, proto IPProtocol, dataLen int) error {
if len(buf) != IPv4_PSEUDO_LENGTH {
return fmt.Errorf("incorrect buffer size: %d buffer given, %d needed", len(buf), IPv4_PSEUDO_LENGTH)
}
copy(buf[0:4], ip.SrcIP)
copy(buf[4:8], ip.DstIP)
buf[8] = 0
buf[9] = byte(proto)
binary.BigEndian.PutUint16(buf[10:], uint16(dataLen))
return nil
}
func (ip *IPv4) HeaderLength() int {
if ip.headerLength == 0 {
optionLength := uint8(0)
for _, opt := range ip.Options {
switch opt.OptionType {
case 0:
// this is the end of option lists
optionLength++
case 1:
// this is the padding
optionLength++
default:
optionLength += opt.OptionLength
}
}
// make sure the options are aligned to 32 bit boundary
if (optionLength % 4) != 0 {
optionLength += 4 - (optionLength % 4)
}
ip.IHL = 5 + (optionLength / 4)
ip.headerLength = int(optionLength) + 20
}
return ip.headerLength
}
func (ip *IPv4) flagsfrags() (ff uint16) {
ff |= uint16(ip.Flags) << 13
ff |= ip.FragOffset
return
}
func (ip *IPv4) Serialize(hdr []byte, dataLen int) error {
if len(hdr) != ip.HeaderLength() {
return fmt.Errorf("incorrect buffer size: %d buffer given, %d needed", len(hdr), ip.HeaderLength())
}
hdr[0] = (ip.Version << 4) | ip.IHL
hdr[1] = ip.TOS
ip.Length = uint16(ip.headerLength + dataLen)
binary.BigEndian.PutUint16(hdr[2:], ip.Length)
binary.BigEndian.PutUint16(hdr[4:], ip.Id)
binary.BigEndian.PutUint16(hdr[6:], ip.flagsfrags())
hdr[8] = ip.TTL
hdr[9] = byte(ip.Protocol)
copy(hdr[12:16], ip.SrcIP)
copy(hdr[16:20], ip.DstIP)
curLocation := 20
// Now, we will encode the options
for _, opt := range ip.Options {
switch opt.OptionType {
case 0:
// this is the end of option lists
hdr[curLocation] = 0
curLocation++
case 1:
// this is the padding
hdr[curLocation] = 1
curLocation++
default:
hdr[curLocation] = opt.OptionType
hdr[curLocation+1] = opt.OptionLength
// sanity checking to protect us from buffer overrun
if len(opt.OptionData) > int(opt.OptionLength-2) {
return fmt.Errorf("option length is smaller than length of option data")
}
copy(hdr[curLocation+2:curLocation+int(opt.OptionLength)], opt.OptionData)
curLocation += int(opt.OptionLength)
}
}
hdr[10] = 0
hdr[11] = 0
ip.Checksum = checksum(hdr)
binary.BigEndian.PutUint16(hdr[10:], ip.Checksum)
return nil
}