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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dhcp
import (
"context"
"fmt"
"io"
"log"
"sync"
"time"
"gvisor.googlesource.com/gvisor/pkg/tcpip"
"gvisor.googlesource.com/gvisor/pkg/tcpip/buffer"
"gvisor.googlesource.com/gvisor/pkg/tcpip/network/ipv4"
"gvisor.googlesource.com/gvisor/pkg/tcpip/stack"
"gvisor.googlesource.com/gvisor/pkg/tcpip/transport/udp"
"gvisor.googlesource.com/gvisor/pkg/waiter"
)
// Server is a DHCP server.
type Server struct {
conn conn
broadcast tcpip.FullAddress
addrs []tcpip.Address // TODO: use a tcpip.AddressMask or range structure
cfg Config
cfgopts []option // cfg to send to client
handlers []chan header
mu sync.Mutex
leases map[tcpip.LinkAddress]serverLease
}
// conn is a blocking read/write network endpoint.
type conn interface {
Read() (buffer.View, tcpip.FullAddress, error)
Write([]byte, *tcpip.FullAddress) error
}
type epConn struct {
ctx context.Context
wq *waiter.Queue
ep tcpip.Endpoint
we waiter.Entry
inCh chan struct{}
}
func newEPConn(ctx context.Context, wq *waiter.Queue, ep tcpip.Endpoint) *epConn {
c := &epConn{
ctx: ctx,
wq: wq,
ep: ep,
}
c.we, c.inCh = waiter.NewChannelEntry(nil)
wq.EventRegister(&c.we, waiter.EventIn)
go func() {
<-ctx.Done()
wq.EventUnregister(&c.we)
}()
return c
}
func (c *epConn) Read() (buffer.View, tcpip.FullAddress, error) {
for {
var addr tcpip.FullAddress
v, _, err := c.ep.Read(&addr)
if err == tcpip.ErrWouldBlock {
select {
case <-c.inCh:
continue
case <-c.ctx.Done():
return nil, tcpip.FullAddress{}, io.EOF
}
}
if err != nil {
return v, addr, fmt.Errorf("read: %v", err)
}
return v, addr, nil
}
}
func (c *epConn) Write(b []byte, addr *tcpip.FullAddress) error {
_, resCh, err := c.ep.Write(tcpip.SlicePayload(b), tcpip.WriteOptions{To: addr})
if err != nil && resCh == nil {
return fmt.Errorf("write: %v", err)
}
if resCh != nil {
select {
case <-resCh:
case <-c.ctx.Done():
return fmt.Errorf("dhcp server address resolution: %v", tcpip.ErrAborted)
}
if _, _, err := c.ep.Write(tcpip.SlicePayload(b), tcpip.WriteOptions{To: addr}); err != nil {
return fmt.Errorf("write: %v", err)
}
}
return nil
}
func newEPConnServer(ctx context.Context, stack *stack.Stack, addrs []tcpip.Address, cfg Config) (*Server, error) {
wq := new(waiter.Queue)
ep, err := stack.NewEndpoint(udp.ProtocolNumber, ipv4.ProtocolNumber, wq)
if err != nil {
return nil, fmt.Errorf("dhcp: server endpoint: %v", err)
}
if err := ep.Bind(tcpip.FullAddress{Port: ServerPort}, nil); err != nil {
return nil, fmt.Errorf("dhcp: server bind: %v", err)
}
if err := ep.SetSockOpt(tcpip.BroadcastOption(1)); err != nil {
return nil, fmt.Errorf("dhcp: server setsockopt: %v", err)
}
c := newEPConn(ctx, wq, ep)
return NewServer(ctx, c, addrs, cfg)
}
// NewServer creates a new DHCP server and begins serving.
// The server continues serving until ctx is done.
func NewServer(ctx context.Context, c conn, addrs []tcpip.Address, cfg Config) (*Server, error) {
if cfg.ServerAddress == "" {
return nil, fmt.Errorf("dhcp: server requires explicit server address")
}
s := &Server{
conn: c,
addrs: addrs,
cfg: cfg,
cfgopts: cfg.encode(),
broadcast: tcpip.FullAddress{
Addr: "\xff\xff\xff\xff",
Port: ClientPort,
},
handlers: make([]chan header, 8),
leases: make(map[tcpip.LinkAddress]serverLease),
}
for i := 0; i < len(s.handlers); i++ {
ch := make(chan header, 8)
s.handlers[i] = ch
go s.handler(ctx, ch)
}
go s.expirer(ctx)
go s.reader(ctx)
return s, nil
}
func (s *Server) expirer(ctx context.Context) {
t := time.NewTicker(1 * time.Minute)
defer t.Stop()
for {
select {
case <-t.C:
s.mu.Lock()
for linkAddr, lease := range s.leases {
if time.Since(lease.start) > s.cfg.LeaseLength {
lease.state = leaseExpired
s.leases[linkAddr] = lease
}
}
s.mu.Unlock()
case <-ctx.Done():
return
}
}
}
// reader listens for all incoming DHCP packets and fans them out to
// handling goroutines based on XID as session identifiers.
func (s *Server) reader(ctx context.Context) {
for {
v, _, err := s.conn.Read()
if err != nil {
return
}
h := header(v)
if !h.isValid() || h.op() != opRequest {
continue
}
xid := h.xid()
// Fan out the packet to a handler goroutine.
//
// Use a consistent handler for a given xid, so that
// packets from a particular client are processed
// in order.
ch := s.handlers[int(xid)%len(s.handlers)]
select {
case <-ctx.Done():
return
case ch <- h:
default:
// drop the packet
}
}
}
func (s *Server) handler(ctx context.Context, ch chan header) {
for {
select {
case h := <-ch:
if h == nil {
return
}
opts, err := h.options()
if err != nil {
continue
}
// TODO: Handle DHCPRELEASE and DHCPDECLINE.
msgtype, err := opts.dhcpMsgType()
if err != nil {
continue
}
switch msgtype {
case dhcpDISCOVER:
s.handleDiscover(h, opts)
case dhcpREQUEST:
s.handleRequest(h, opts)
}
case <-ctx.Done():
return
}
}
}
func (s *Server) handleDiscover(hreq header, opts options) {
linkAddr := tcpip.LinkAddress(hreq.chaddr()[:6])
xid := hreq.xid()
s.mu.Lock()
lease := s.leases[linkAddr]
switch lease.state {
case leaseNew:
if len(s.leases) < len(s.addrs) {
// Find an unused address.
// TODO: avoid building this state on each request.
alloced := make(map[tcpip.Address]bool)
for _, lease := range s.leases {
alloced[lease.addr] = true
}
for _, addr := range s.addrs {
if !alloced[addr] {
lease = serverLease{
start: time.Now(),
addr: addr,
xid: xid,
state: leaseOffer,
}
s.leases[linkAddr] = lease
break
}
}
} else {
// No more addresses, take an expired address.
for k, oldLease := range s.leases {
if oldLease.state == leaseExpired {
delete(s.leases, k)
lease = serverLease{
start: time.Now(),
addr: lease.addr,
xid: xid,
state: leaseOffer,
}
s.leases[linkAddr] = lease
break
}
}
log.Printf("server has no more addresses")
s.mu.Unlock()
return
}
case leaseOffer, leaseAck, leaseExpired:
lease = serverLease{
start: time.Now(),
addr: s.leases[linkAddr].addr,
xid: xid,
state: leaseOffer,
}
s.leases[linkAddr] = lease
}
s.mu.Unlock()
// DHCPOFFER
opts = options{{optDHCPMsgType, []byte{byte(dhcpOFFER)}}}
opts = append(opts, s.cfgopts...)
h := make(header, headerBaseSize+opts.len()+1)
h.init()
h.setOp(opReply)
copy(h.xidbytes(), hreq.xidbytes())
copy(h.yiaddr(), lease.addr)
copy(h.chaddr(), hreq.chaddr())
h.setOptions(opts)
s.conn.Write([]byte(h), &s.broadcast)
}
func (s *Server) nack(hreq header) {
// DHCPNACK
opts := options([]option{
{optDHCPMsgType, []byte{byte(dhcpNAK)}},
{optDHCPServer, []byte(s.cfg.ServerAddress)},
})
h := make(header, headerBaseSize+opts.len()+1)
h.init()
h.setOp(opReply)
copy(h.xidbytes(), hreq.xidbytes())
copy(h.chaddr(), hreq.chaddr())
h.setOptions(opts)
s.conn.Write([]byte(h), &s.broadcast)
}
func (s *Server) handleRequest(hreq header, opts options) {
linkAddr := tcpip.LinkAddress(hreq.chaddr()[:6])
xid := hreq.xid()
reqopts, err := hreq.options()
if err != nil {
s.nack(hreq)
return
}
var reqcfg Config
if err := reqcfg.decode(reqopts); err != nil {
s.nack(hreq)
return
}
if reqcfg.ServerAddress != s.cfg.ServerAddress {
// This request is for a different DHCP server. Ignore it.
return
}
s.mu.Lock()
lease := s.leases[linkAddr]
switch lease.state {
case leaseOffer, leaseAck, leaseExpired:
lease = serverLease{
start: time.Now(),
addr: s.leases[linkAddr].addr,
xid: xid,
state: leaseAck,
}
s.leases[linkAddr] = lease
}
s.mu.Unlock()
if lease.state == leaseNew {
// TODO: NACK or accept request
return
}
// DHCPACK
opts = []option{{optDHCPMsgType, []byte{byte(dhcpACK)}}}
opts = append(opts, s.cfgopts...)
h := make(header, headerBaseSize+opts.len()+1)
h.init()
h.setOp(opReply)
copy(h.xidbytes(), hreq.xidbytes())
copy(h.yiaddr(), lease.addr)
copy(h.chaddr(), hreq.chaddr())
h.setOptions(opts)
s.conn.Write([]byte(h), &s.broadcast)
}
type leaseState int
const (
leaseNew leaseState = iota
leaseOffer
leaseAck
leaseExpired
)
type serverLease struct {
start time.Time
addr tcpip.Address
xid xid
state leaseState
}
|