-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
237 lines (189 loc) · 5.72 KB
/
server.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
package goping
import (
"errors"
"fmt"
"log"
"os"
"syscall"
"unsafe"
)
const (
TAP_DEV_NAME = "tap00"
PACKET_BUF_SIZE = 2048
)
var (
IP_ADDR = IpAddr(192) | IpAddr(168<<8) | IpAddr(200<<16) | IpAddr(10<<24)
MAC_ADDR = MacAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
)
func Run() error {
fd, err := createTapDev()
if err != nil {
return fmt.Errorf("failed to create TAP device: %w", err)
}
defer syscall.Close(fd)
for {
rx, rxLen, err := read(fd)
if err != nil {
return fmt.Errorf("failed to read: %w", err)
}
eth := DecodeEth(rx[:])
switch ntohs(eth.Type) {
case syscall.ETH_P_IP:
if err := handleIp(fd, rx, rxLen, eth); err != nil {
return fmt.Errorf("failed to handle ip: %w", err)
}
case syscall.ETH_P_ARP:
if err := handleArp(fd, rx, eth); err != nil {
return fmt.Errorf("failed to handle arp: %w", err)
}
}
}
}
func createTapDev() (int, error) {
fd, err := syscall.Open("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return 0, fmt.Errorf("open error: /dev/net/tun")
}
ifr := createIfReq()
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&ifr))); errno != 0 {
return 0, os.NewSyscallError("ioctl", errno)
}
return fd, nil
}
type ifReq struct {
name [16]byte
flags uint16
pad [22]byte
}
func createIfReq() ifReq {
ifr := ifReq{flags: syscall.IFF_TAP | syscall.IFF_NO_PI}
copy(ifr.name[:], TAP_DEV_NAME)
return ifr
}
func handleIp(fd int, rx [PACKET_BUF_SIZE]byte, rxLen int, eth EthHeader) error {
if eth.DestAddr != MAC_ADDR {
return nil
}
ip := DecodeIp(rx[ETH_HEADER_SIZE:])
if ip.DestAddr != IP_ADDR {
return nil
}
switch ip.Protocol {
case syscall.IPPROTO_ICMP:
if err := handleIcmp(fd, rx, rxLen, eth, ip); err != nil {
return fmt.Errorf("failed to handle icmp: %w", err)
}
}
return nil
}
func handleIcmp(fd int, rx [PACKET_BUF_SIZE]byte, rxLen int, eth EthHeader, ip IpHeader) error {
icmp := DecodeIcmp(rx[ETH_HEADER_SIZE+ip.HeaderLenBytes():])
switch icmp.Type {
case ICMP_TYPE_ECHO_REQ:
if err := replyIcmp(fd, rx, rxLen, eth, ip, icmp); err != nil {
return fmt.Errorf("failed to reply icmp: %w", err)
}
}
return nil
}
func replyIcmp(fd int, rx [PACKET_BUF_SIZE]byte, rxLen int, eth EthHeader, ip IpHeader, icmp IcmpHeader) error {
reply, err := createIcmpEchoReply(rx, rxLen, eth, ip, icmp)
if err != nil {
return fmt.Errorf("failed to create icmp echo reply: %w", err)
}
if err := write(fd, reply); err != nil {
return fmt.Errorf("failed to write icmp echo reply: %w", err)
}
log.Printf("Send icmp reply to %s", ip.SrcAddr.ToString())
return nil
}
func createIcmpEchoReply(rx [PACKET_BUF_SIZE]byte, rxLen int, eth EthHeader, ip IpHeader, icmp IcmpHeader) ([]byte, error) {
tx := [PACKET_BUF_SIZE]byte{}
ethTx := EthHeader{
SrcAddr: eth.DestAddr,
DestAddr: eth.SrcAddr,
Type: htons(syscall.ETH_P_IP),
}
*(*[ETH_HEADER_SIZE]byte)(unsafe.Pointer(&tx[0])) = ethTx.Encode()
ipTx := IpHeader{
VersionAndHeaderLen: 4<<4 | IP_HEADER_SIZE/4,
TTL: 64,
Protocol: syscall.IPPROTO_ICMP,
SrcAddr: ip.DestAddr,
DestAddr: ip.SrcAddr,
}
ipTx.SetTotalLen(rxLen, ip)
ipTx.SetChecksum()
*(*[IP_HEADER_SIZE]byte)(unsafe.Pointer(&tx[ETH_HEADER_SIZE])) = ipTx.Encode()
leftSize := PACKET_BUF_SIZE - ETH_HEADER_SIZE + ip.HeaderLenBytes()
if ip.DataSize() >= leftSize {
return nil, errors.New("packet buffer size is too small")
}
icmpTx := IcmpHeader{
Type: ICMP_TYPE_ECHO_REPLY,
Code: icmp.Code,
Id: icmp.Id,
Seq: icmp.Seq,
}
*(*[ICMP_HEADER_SIZE]byte)(unsafe.Pointer(&tx[ETH_HEADER_SIZE+ipTx.HeaderLenBytes()])) = icmpTx.Encode()
txOffset := ETH_HEADER_SIZE + ipTx.HeaderLenBytes() + ICMP_HEADER_SIZE
rxOffset := ETH_HEADER_SIZE + ip.HeaderLenBytes() + ICMP_HEADER_SIZE
if n := copy(tx[txOffset:], rx[rxOffset:rxLen]); n < 1 {
return nil, fmt.Errorf("failed to copy icmp data (n: %d)", n)
}
icmpTx.SetChecksum(tx, ipTx)
*(*[ICMP_HEADER_SIZE]byte)(unsafe.Pointer(&tx[ETH_HEADER_SIZE+ipTx.HeaderLenBytes()])) = icmpTx.Encode()
return tx[:ETH_HEADER_SIZE+ntohs(ipTx.TotalLen)], nil
}
func handleArp(fd int, rx [PACKET_BUF_SIZE]byte, eth EthHeader) error {
if eth.DestAddr != MAC_BROADCAST && eth.DestAddr != MAC_ADDR {
return nil
}
arp := DecodeArp(rx[ETH_HEADER_SIZE:])
switch ntohs(arp.Operation) {
case ARP_OP_REQUEST:
if err := handleArpReq(fd, eth, arp); err != nil {
return fmt.Errorf("failed to handle arp request: %w", err)
}
}
return nil
}
func handleArpReq(fd int, eth EthHeader, arp ArpHeader) error {
if arp.TargetIpAddr != IP_ADDR {
return nil
}
if err := replyArp(fd, eth, arp); err != nil {
return fmt.Errorf("failed to reply arp: %w", err)
}
return nil
}
func replyArp(fd int, eth EthHeader, arp ArpHeader) error {
reply := createArpReply(eth, arp)
if err := write(fd, reply); err != nil {
return fmt.Errorf("failed to write arp reply: %w", err)
}
log.Printf("Send arp reply to %s (asked who has %s)", arp.SrcIpAddr.ToString(), arp.TargetIpAddr.ToString())
return nil
}
func createArpReply(eth EthHeader, arp ArpHeader) []byte {
tx := [PACKET_BUF_SIZE]byte{}
ethTx := EthHeader{
SrcAddr: MAC_ADDR,
DestAddr: eth.SrcAddr,
Type: htons(syscall.ETH_P_ARP),
}
*(*[ETH_HEADER_SIZE]byte)(unsafe.Pointer(&tx[0])) = ethTx.Encode()
arpTx := ArpHeader{
HardwareType: htons(syscall.ARPHRD_ETHER),
ProtocolType: htons(syscall.ETH_P_IP),
HardwareLen: 6,
ProtocolLen: 4,
Operation: htons(ARP_OP_REPLY),
SrcMacAddr: MAC_ADDR,
SrcIpAddr: IP_ADDR,
TargetMacAddr: arp.SrcMacAddr,
TargetIpAddr: arp.SrcIpAddr,
}
*(*[ARP_HEADER_SIZE]byte)(unsafe.Pointer(&tx[ETH_HEADER_SIZE])) = arpTx.Encode()
return tx[:ETH_HEADER_SIZE+ARP_HEADER_SIZE]
}