-
Notifications
You must be signed in to change notification settings - Fork 4
/
ping.go
136 lines (122 loc) · 2.78 KB
/
ping.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
package vmnet
import (
"fmt"
"io"
"net"
xicmp "golang.org/x/net/icmp"
xipv4 "golang.org/x/net/ipv4"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/checksum"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
)
type pingPacket struct {
srcIP, dstIP tcpip.Address
srcMAC, dstMAC tcpip.LinkAddress
payload []byte
ident, sequence uint16
}
func pingv4(conn io.Writer, p pingPacket) {
ttl, err := pingv4Echo(
p.dstIP,
p.payload,
p.ident,
p.sequence,
)
if err != nil {
return
}
packet := makeICMPv4EchoPacket(
p.dstIP,
p.srcIP,
p.dstMAC,
p.srcMAC,
uint8(ttl),
header.ICMPv4EchoReply,
p.payload,
p.ident,
p.sequence,
)
conn.Write(packet)
}
func pingv4Echo(
dst tcpip.Address,
payload []byte,
ident, sequence uint16,
) (uint8, error) {
c, err := xicmp.ListenPacket("udp4", "0.0.0.0")
if err != nil {
return 0, err
}
wm := xicmp.Message{
Type: xipv4.ICMPTypeEcho,
Code: 0,
Body: &xicmp.Echo{
ID: int(ident),
Seq: int(sequence),
Data: payload,
},
}
wb, err := wm.Marshal(nil)
if err != nil {
return 0, err
}
if _, err := c.WriteTo(wb, &net.UDPAddr{IP: net.ParseIP(dst.String())}); err != nil {
return 0, err
}
cc := c.IPv4PacketConn()
if err := cc.SetControlMessage(xipv4.FlagTTL, true); err != nil {
return 0, err
}
// Use nil because there is no required payload.
_, cm, _, err := cc.ReadFrom(nil)
if err != nil {
return 0, err
}
if cm == nil {
return 0, fmt.Errorf("no ttl")
}
return uint8(cm.TTL), nil
}
// makeICMPv4EchoPacket returns an ICMPv4 echo packet.
func makeICMPv4EchoPacket(
srcIP, dstIP tcpip.Address,
srcMAC, dstMAC tcpip.LinkAddress,
ttl uint8,
ty header.ICMPv4Type,
payload []byte,
ident, sequence uint16,
) []byte {
const ethernetICMPv4MinimumSize = header.EthernetMinimumSize + header.IPv4MinimumSize + header.ICMPv4MinimumSize
buf := make([]byte, ethernetICMPv4MinimumSize+len(payload))
// Ethernet header
eth := header.Ethernet(buf)
eth.Encode(&header.EthernetFields{
SrcAddr: srcMAC,
DstAddr: dstMAC,
Type: ipv4.ProtocolNumber,
})
// IP header
ipbuf := buf[header.EthernetMinimumSize:]
ip := header.IPv4(ipbuf)
ip.Encode(&header.IPv4Fields{
TotalLength: uint16(len(ipbuf)),
TTL: ttl,
Protocol: uint8(icmp.ProtocolNumber4),
SrcAddr: srcIP,
DstAddr: dstIP,
})
ip.SetChecksum(^ip.CalculateChecksum())
// ICMPv4 header
icmpv4buf := buf[header.EthernetMinimumSize+header.IPv4MinimumSize:]
icmpv4 := header.ICMPv4(icmpv4buf)
icmpv4.SetType(ty)
icmpv4.SetCode(header.ICMPv4UnusedCode)
icmpv4.SetIdent(ident)
icmpv4.SetSequence(sequence)
copy(icmpv4.Payload(), payload)
icmpv4.SetChecksum(0)
icmpv4.SetChecksum(^checksum.Checksum(icmpv4, 0))
return buf
}