-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawClient.go
281 lines (232 loc) · 5.61 KB
/
rawClient.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
package main
import (
"bytes"
"encoding/binary"
"log"
"net"
"os"
"syscall"
"github.com/mdlayher/ethernet"
"github.com/mdlayher/raw"
)
const UDP_HEADER_LEN = 8
// A RawClient is a Wake-on-LAN client which operates directly on top of
// Ethernet frames using raw sockets. It can be used to send WoL magic packets
// to other machines on a local network, using their hardware addresses.
type RawClient struct {
ifi *net.Interface
p net.PacketConn
}
type udphdr struct {
src uint16
dst uint16
ulen uint16
csum uint16
}
type pseudohdr struct {
ipsrc [4]byte
ipdst [4]byte
zero uint8
ipproto uint8
plen uint16
}
type iphdr struct {
vhl uint8
tos uint8
iplen uint16
id uint16
off uint16
ttl uint8
proto uint8
csum uint16
src [4]byte
dst [4]byte
}
// NewRawClient creates a new RawClient using the specified network interface.
//
// Note that raw sockets typically require elevated user privileges, such as
// the 'root' user on Linux, or the 'SET_CAP_RAW' capability.
//
// For this reason, it is typically recommended to use the regular Client type
// instead, which operates over UDP.
func NewRawClient(ifi *net.Interface) (*RawClient, error) {
// Open raw socket to send Wake-on-LAN magic packets
var cfg raw.Config
p, err := raw.ListenPacket(ifi, 0x0806, &cfg)
if err != nil {
return nil, err
}
return &RawClient{
ifi: ifi,
p: p,
}, nil
}
// Close closes a RawClient's raw socket.
func (c *RawClient) Close() error {
return c.p.Close()
}
// sendDHCP create a udp packet and stores it in an
// Ethernet frame, and sends the frame over a raw socket to attempt to wake
// a machine.
func (c *RawClient) sendDHCP(target net.HardwareAddr, dhcp []byte, dstIP net.IP, srcIP net.IP) error {
proto := 17
udpsrc := uint(67)
udpdst := uint(68)
udp := udphdr{
src: uint16(udpsrc),
dst: uint16(udpdst),
}
udplen := 8 + len(dhcp)
ip := iphdr{
vhl: 0x45,
tos: 0,
id: 0x0000, // the kernel overwrites id if it is zero
off: 0,
ttl: 128,
proto: uint8(proto),
}
copy(ip.src[:], srcIP.To4())
copy(ip.dst[:], dstIP.To4())
udp.ulen = uint16(udplen)
udp.checksum(&ip, dhcp)
totalLen := 20 + udplen
ip.iplen = uint16(totalLen)
ip.checksum()
buf := bytes.NewBuffer([]byte{})
err := binary.Write(buf, binary.BigEndian, &udp)
if err != nil {
log.Fatal(err)
}
udpHeader := buf.Bytes()
dataWithHeader := append(udpHeader, dhcp...)
buff := bytes.NewBuffer([]byte{})
err = binary.Write(buff, binary.BigEndian, &ip)
if err != nil {
log.Fatal(err)
}
ipHeader := buff.Bytes()
packet := append(ipHeader, dataWithHeader...)
// Create Ethernet frame
f := ðernet.Frame{
Destination: target,
Source: c.ifi.HardwareAddr,
EtherType: ethernet.EtherTypeIPv4,
Payload: packet,
}
fb, err := f.MarshalBinary()
if err != nil {
return err
}
// Send packet to target
_, err = c.p.WriteTo(fb, &raw.Addr{
HardwareAddr: target,
})
return err
}
func (u *udphdr) checksum(ip *iphdr, payload []byte) {
u.csum = 0
phdr := pseudohdr{
ipsrc: ip.src,
ipdst: ip.dst,
zero: 0,
ipproto: ip.proto,
plen: u.ulen,
}
var b bytes.Buffer
binary.Write(&b, binary.BigEndian, &phdr)
binary.Write(&b, binary.BigEndian, u)
binary.Write(&b, binary.BigEndian, &payload)
u.csum = checksum(b.Bytes())
}
func checksum(buf []byte) uint16 {
sum := uint32(0)
for ; len(buf) >= 2; buf = buf[2:] {
sum += uint32(buf[0])<<8 | uint32(buf[1])
}
if len(buf) > 0 {
sum += uint32(buf[0]) << 8
}
for sum > 0xffff {
sum = (sum >> 16) + (sum & 0xffff)
}
csum := ^uint16(sum)
/*
* 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 csum == 0 {
csum = 0xffff
}
return csum
}
func (h *iphdr) checksum() {
h.csum = 0
var b bytes.Buffer
binary.Write(&b, binary.BigEndian, h)
h.csum = checksum(b.Bytes())
}
// sendUnicastDHCP create a udp packet and stores it in an
// Ethernet frame, and sends the frame over a raw socket to attempt to wake
// a machine.
func sendUnicastDHCP(dhcp []byte, dstIP net.IP, srcIP net.IP, giAddr net.IP, udpsrc int, udpdst int) error {
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
if err != nil {
log.Fatal(err)
}
proto := 17
var udp udphdr
// Keep as is for futur test
if !(dstIP.Equal(giAddr)) {
if !(giAddr.Equal(net.IPv4zero)) {
dstIP = giAddr
}
}
udp = udphdr{
src: uint16(udpsrc),
dst: uint16(udpdst),
}
udplen := 8 + len(dhcp)
ip := iphdr{
vhl: 0x45,
tos: 0,
id: 0x0000, // the kernel overwrites id if it is zero
off: 0,
ttl: 128,
proto: uint8(proto),
}
copy(ip.src[:], srcIP.To4())
copy(ip.dst[:], dstIP.To4())
udp.ulen = uint16(udplen)
udp.checksum(&ip, dhcp)
totalLen := 20 + udplen
ip.iplen = uint16(totalLen)
ip.checksum()
buf := bytes.NewBuffer([]byte{})
err = binary.Write(buf, binary.BigEndian, &udp)
if err != nil {
log.Fatal(err)
}
udpHeader := buf.Bytes()
dataWithHeader := append(udpHeader, dhcp...)
buff := bytes.NewBuffer([]byte{})
err = binary.Write(buff, binary.BigEndian, &ip)
if err != nil {
log.Fatal(err)
}
ipHeader := buff.Bytes()
packet := append(ipHeader, dataWithHeader...)
addr := syscall.SockaddrInet4{}
copy(addr.Addr[:], dstIP.To4())
addr.Port = int(udpdst)
err = syscall.Sendto(s, packet, 0, &addr)
// Send packet to target
err = syscall.Close(s)
if err != nil {
log.Fatal("error closing the socket: ", err)
os.Exit(1)
}
return err
}