-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathip.go
225 lines (208 loc) · 6.56 KB
/
ip.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
package xlat
import (
"encoding/binary"
"fmt"
"net"
"github.com/google/gopacket/layers"
)
func IsIPv4Layer(data []byte) bool {
if len(data) < HeaderIPv4Length {
return false
}
if data[0]>>4 == 0x4 && int(binary.BigEndian.Uint16(data[2:4])) == len(data) && IPv4NextLayer(data) != LayerTypePayload {
return true
}
return false
}
func IPv4NextLayer(data []byte) string {
switch data[9] {
// case layers.IPProtocolICMPv4:
case 1:
return LayerTypeICMPv4
// case layers.IPProtocolTCP:
case 6:
return LayerTypeTCP
// case layers.IPProtocolUDP:
case 17:
return LayerTypeUDP
}
return LayerTypePayload
}
func IsIPv6Layer(data []byte) bool {
if len(data) < HeaderIPv6Length {
return false
}
if data[0]>>4 == 0x6 && int(binary.BigEndian.Uint16(data[4:6])) == len(data)-40 && IPv6NextLayer(data) != LayerTypePayload {
return true
}
return false
}
func IPv6NextLayer(data []byte) string {
switch data[6] {
// case layers.IPProtocolICMPv6:
case 58:
return LayerTypeICMPv6
// case layers.IPProtocolTCP:
case 6:
return LayerTypeTCP
// case layers.IPProtocolUDP:
case 17:
return LayerTypeUDP
}
return LayerTypePayload
}
func IPv6HeaderToBytes(ipv6 *layers.IPv6) []byte {
// pLen := len(ipv6.Payload)
bytes := make([]byte, 40)
bytes[0] = (ipv6.Version << 4) | (ipv6.TrafficClass >> 4)
bytes[1] = (ipv6.TrafficClass << 4) | uint8(ipv6.FlowLabel>>16)
binary.BigEndian.PutUint16(bytes[2:], uint16(ipv6.FlowLabel))
// ipv6.Length = uint16(pLen)
binary.BigEndian.PutUint16(bytes[4:], ipv6.Length)
bytes[6] = byte(ipv6.NextHeader)
bytes[7] = byte(ipv6.HopLimit)
// if err := ipv6.AddressTo16(); err != nil {
// return nil
// }
copy(bytes[8:], ipv6.SrcIP)
copy(bytes[24:], ipv6.DstIP)
return bytes
}
func IPv4HeaderToBytes(ip *layers.IPv4) []byte {
bytes := make([]byte, 20)
ip.IHL = 5
bytes[0] = (ip.Version << 4) | ip.IHL
bytes[1] = ip.TOS
binary.BigEndian.PutUint16(bytes[2:], ip.Length)
binary.BigEndian.PutUint16(bytes[4:], ip.Id)
var ff uint16
ff |= uint16(ip.Flags) << 13
ff |= ip.FragOffset
binary.BigEndian.PutUint16(bytes[6:], ff)
bytes[8] = ip.TTL
bytes[9] = byte(ip.Protocol)
copy(bytes[12:16], ip.SrcIP)
copy(bytes[16:20], ip.DstIP)
ip.Checksum = IPChecksum(bytes)
binary.BigEndian.PutUint16(bytes[10:], ip.Checksum)
return bytes
}
func IP4ToIP6(p *Packet) (*Packet, error) {
layerIndex := p.LayerIndex(LayerTypeIPv4)
ipv4Layer := p.Layers[layerIndex].ToIPLayer()
// ipLayer := ipv4Layer.Parse(p).(*layers.IPv4)
ipv6Layer := &layers.IPv6{}
// ipv6Layer.SrcIP = ConfigVar.Clat.Src.IP
if p.Stateful {
ipv6Layer.SrcIP = CopyIP(ConfigVar.Plat.Dst.IP)
srcIP := ipv4Layer.GetSrc(p)
copy(ipv6Layer.SrcIP[12:], srcIP)
} else {
ipv6Layer.SrcIP = CopyIP(ConfigVar.Clat.Src.IP)
srcIP := ipv4Layer.GetSrc(p)
copy(ipv6Layer.SrcIP[12:], srcIP)
}
// ipv6Layer.SrcIP[15] = srcIP[3]
// ipv6Layer.SrcIP[14] = srcIP[2]
// ipv6Layer.SrcIP[13] = srcIP[1]
// ipv6Layer.SrcIP[12] = srcIP[0]
// ipv6Layer.DstIP = ConfigVar.Clat.Dst.IP
if p.Stateful {
ip4t := p.GetDstTuple()
// log.Printf("lookup ip4t %+v", ip4t)
ip6t := Ctrl.GetIP(ip4t)
// log.Printf("return ip6t %+v", ip6t)
if ip6t == nil {
// p.Print()
return nil, fmt.Errorf("Failed to find tuple by %s %d", ip4t.IP4, ip4t.Port4)
}
ipv6Layer.DstIP = CopyIP(ip6t.IP6)
if ip6t.Port4 != ip6t.Port6 {
p.SetDstPort(ip6t.Port6)
}
// return nil, fmt.Errorf("stateful unsupported")
} else {
ipv6Layer.DstIP = CopyIP(ConfigVar.Clat.Dst.IP)
dstIP := ipv4Layer.GetDst(p)
copy(ipv6Layer.DstIP[12:], dstIP)
}
// ipv6Layer.DstIP[15] = dstIP[3]
// ipv6Layer.DstIP[14] = dstIP[2]
// ipv6Layer.DstIP[13] = dstIP[1]
// ipv6Layer.DstIP[12] = dstIP[0]
// convert next protocol
// if ipv4Layer.Protocol(p) == layers.IPProtocolICMPv4 {
if ipv4Layer.Protocol(p) == 1 {
ipv6Layer.NextHeader = layers.IPProtocolICMPv6
} else {
ipv6Layer.NextHeader = layers.IPProtocol(ipv4Layer.Protocol(p))
}
ipv6Layer.HopLimit = ipv4Layer.TTL(p)
ipv6Layer.Version = 6
ipv6Layer.Length = uint16(len(p.Data[ipv4Layer.DataEnd:p.DataEnd]))
ipv6Layer.Contents = IPv6HeaderToBytes(ipv6Layer)
// newData := make([]byte, len(ipv6Layer.Contents)+len(p.Data[ipv4Layer.DataEnd:]))
// copy(newData, ipv6Layer.Contents)
// copy(newData[len(ipv6Layer.Contents):], p.Data[ipv4Layer.DataEnd:])
// p.Data = newData
copy(p.Data[ipv4Layer.DataEnd-len(ipv6Layer.Contents):], ipv6Layer.Contents)
p.Layers[layerIndex].DataStart = ipv4Layer.DataEnd - len(ipv6Layer.Contents)
p.DataStart = p.Layers[layerIndex].DataStart
// p.Data = p.Buffer[p.DataStart:p.DataEnd]
// p.ReplaceIPLayer()
p.Layers[layerIndex].Type = LayerTypeIPv6
return p, nil
}
func IP6ToIP4(p *Packet) (*Packet, error) {
layerIndex := p.LayerIndex(LayerTypeIPv4)
layer := p.Layers[layerIndex].ToIP6Layer()
// ip6Layer := layer.Parse(p).(*layers.IPv6)
ipLayer := &layers.IPv4{}
// ipLayer.SrcIP = net.ParseIP("1.1.1.1").To4()
// ipLayer.SrcIP = net.IP(make([]byte, 4))
if p.Stateful {
ipt := p.GetSrcTuple()
Ctrl.AllocIP(ipt)
ipLayer.SrcIP = net.IPv4(ipt.IP4[0], ipt.IP4[1], ipt.IP4[2], ipt.IP4[3]).To4()
if ipt.Port4 != ipt.Port6 {
p.SetSrcPort(ipt.Port4)
}
// return nil, fmt.Errorf("stateful unsupported")
} else {
srcIP := layer.GetSrc(p)
ipLayer.SrcIP = net.IPv4(srcIP[12], srcIP[13], srcIP[14], srcIP[15]).To4()
}
// copy(ipLayer.SrcIP, srcIP[12:16])
// ipLayer.SrcIP[3] = srcIP[15]
// ipLayer.SrcIP[2] = srcIP[14]
// ipLayer.SrcIP[1] = srcIP[13]
// ipLayer.SrcIP[0] = srcIP[12]
// ipLayer.DstIP = net.ParseIP("1.1.1.1").To4()
// ipLayer.SrcIP = net.IP(make([]byte, 4))
dstIP := layer.GetDst(p)
ipLayer.DstIP = net.IPv4(dstIP[12], dstIP[13], dstIP[14], dstIP[15]).To4()
// ipLayer.DstIP[3] = dstIP[15]
// ipLayer.DstIP[2] = dstIP[14]
// ipLayer.DstIP[1] = dstIP[13]
// ipLayer.DstIP[0] = dstIP[12]
// convert next protocol
if layers.IPProtocol(layer.NextHeader(p)) == layers.IPProtocolICMPv6 {
// if layer.NextHeader(p) == 58 {
ipLayer.Protocol = layers.IPProtocolICMPv4
} else {
ipLayer.Protocol = layers.IPProtocol(layer.NextHeader(p))
}
ipLayer.TTL = layer.HopLimit(p)
ipLayer.Version = 4
ipLayer.Length = uint16(HeaderIPv4Length + len(p.Data[layer.DataEnd:p.DataEnd]))
// ipv6Layer.Contents = IPv6HeaderToBytes(ipv6Layer)
ipLayer.Contents = IPv4HeaderToBytes(ipLayer)
copy(p.Data[layer.DataEnd-len(ipLayer.Contents):], ipLayer.Contents)
p.Layers[layerIndex].DataStart = layer.DataEnd - len(ipLayer.Contents)
p.DataStart = p.Layers[layerIndex].DataStart
p.Layers[layerIndex].Type = LayerTypeIPv4
// p.Data = p.Data[layer.DataEnd-len(ipLayer.Contents):]
// p.Data = p.Data[:len(p.Data)-20]
// p.ReplaceIPLayer()
return p, nil
}