-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
475 lines (428 loc) · 9.61 KB
/
main.c
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/timerfd.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <net/route.h>
#include <poll.h>
#include <assert.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#define elem(x) ((int)(sizeof(x)/sizeof(x[0])))
#define IP(a,b,c,d) ((u8[4]){a,b,c,d})
#define MIN(a,b) (((a)<(b))?(a):(b))
enum{
IPv4addrlen = 4,
Maxoptlen = 312-4,
Maxfilelen = 128,
Maxhwlen = 16,
SrcPort = 68,
DstPort = 67,
Discover = 1,
Offer = 2,
Request = 3,
DHCPdecline = 4,
Ack = 5,
Nak = 6,
Release = 7,
Timeout0 = 200,
Timeout1,
Timeout2,
Bootrequest = 1,
// boot flags
Fbroadcast = 0x8000,
Broadcast = 0,
Unicast = 1,
// bootp option type
OBpad = 0,
OBmask = 1,
OBtimeoff = 2,
OBrouter = 3,
OBdnsserver = 6,
OBhostname = 12,
OBend = 255,
// dhcp v4 options
ODipaddr = 50,
ODlease = 51,
ODtype = 53,
ODserverid = 54,
ODrenewaltime = 58,
ODrebindingtime = 59,
ODclientid = 61,
};
typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;
typedef struct Bootp {
u8 op[1];
u8 htype[1]; // hardware type
u8 hlen[1]; // hardware addr len
u8 hops[1];
u8 xid[4]; //random number
u8 secs[2];
u8 flags[2];
u8 ciaddr[IPv4addrlen]; // client ip addr (client tells server)
u8 yiaddr[IPv4addrlen]; // client ip addr (server tells client)
u8 siaddr[IPv4addrlen]; // server ip addr
u8 giaddr[IPv4addrlen]; // gateway ip addr
u8 chaddr[Maxhwlen]; // client hw address
u8 sname[64]; // server host name
u8 file[Maxfilelen]; // boot file name
u8 optmagic[4];
u8 optdata [Maxoptlen];
} Bootp;
static Bootp bp;
static time_t begat;
static u8 optmagic[] = {99, 130, 83, 99};
static u8 client[IPv4addrlen];
static u8 server[IPv4addrlen];
static u8 mask[IPv4addrlen];
static u8 router[IPv4addrlen];
static u8 dns[IPv4addrlen];
static u64 xid;
static char hostname[HOST_NAME_MAX + 1];
static u8 hwaddr[sizeof(struct sockaddr) + 1];
static u8 cid[sizeof(struct sockaddr) + 1];
static char *ifname;
static int sock, timers[3];
static struct itimerspec timeout; // time to timeout - seconds
static void dhcpsend(int type, int how);
static void
cexit(int _)
{
dhcpsend(Release, Unicast);
close(sock);
printf("bye\n");
exit(0);
}
static void
put(u8 *dst, u32 src, int n)
{
for(int i = 0; n--; ++i){
dst[i] = (src >> (n * 8)) & 0xFF;
}
}
static struct sockaddr*
iptoaddr(struct sockaddr *ifaddr, u8 ip[4], int port)
{
struct sockaddr_in *in = (struct sockaddr_in*)ifaddr;
in->sin_family = AF_INET;
in->sin_port = htons(port);
memcpy(&in->sin_addr, ip, sizeof(in->sin_addr));
return ifaddr;
}
static void
optget(Bootp *bp, void *v, int opt, int n)
{
u8 *p = bp->optdata;
u8 *top = ((u8*)bp) + sizeof(*bp);
while(p < top){
int code = *p++;
if(code == OBpad)
continue;
if(code == OBend || p == top)
break;
int len = *p++;
if(len > top - p)
break;
if(code == opt){
memcpy(v, p, MIN(len, n));
break;
}
p += len;
}
}
static u8*
optpput(u8 *p, int opt, u8 *v, int len)
{
*p++ = opt;
*p++ = (u8)len;
memcpy(p, v, len);
return p + len;
}
static u8*
optput(u8 *p, int opt, u32 v, int len)
{
*p++ = opt;
*p++ = (u8)len;
put(p, v, len);
return p + len;
}
static u8*
termput(u8 *p)
{
*p++ = OBend;
return p;
}
// make udp paylad
static int
udpsend(u8 ip[4], int fd, void *v, int n)
{
struct sockaddr addr = {0};
socklen_t addrlen = sizeof(addr);
int wc = -1;
iptoaddr(&addr, ip, DstPort);
wc = sendto(fd, v, n, 0, &addr, addrlen);
assert(wc != -1);
return wc;
}
static int
udprecv(u8 ip[4], int fd, void *v, int n)
{
struct sockaddr addr = {0};
socklen_t addrlen = sizeof(addr);
int rc = -1;
iptoaddr(&addr, ip, SrcPort);
rc = recvfrom(fd, v, n, 0, &addr, &addrlen);
assert(rc != -1);
return rc;
}
// make dhcp payload
static void
dhcpsend(int type, int how)
{
u8 *ip, *p;
memset(&bp, 0, sizeof(bp));
put(bp.op, Bootrequest, 1);
put(bp.htype, 1, 1);
put(bp.hlen, 6, 1);
memcpy(bp.xid, &xid, sizeof(xid));
put(bp.secs, time(0) - begat, sizeof(bp.secs));
put(bp.flags, Fbroadcast, sizeof(bp.flags));
memcpy(bp.optmagic, optmagic, sizeof(bp.optmagic));
memcpy(bp.chaddr, hwaddr, sizeof(bp.chaddr));
p = bp.optdata;
p = optput(p, ODtype, type, 1);
p = optpput(p, ODclientid, cid, sizeof(cid));
p = optpput(p, OBhostname, (u8*)hostname, strlen(hostname));
switch(type){
case Discover:
break;
case Request:
case Release:
memcpy(bp.ciaddr, client, sizeof(client));
p = optpput(p, ODipaddr, client, sizeof(client));
p = optpput(p, ODserverid, server, sizeof(server));
break;
default:
assert(0);
}
p = termput(p);
if(how == Broadcast) ip = IP(255,255,255,255);
else ip = server;
udpsend(ip, sock, &bp, p - (u8*)&bp);
}
static int
dhcprecv(void)
{
struct pollfd pfd[] = {
{.fd=sock, .events=POLL_IN},
{.fd=timers[0], .events=POLL_IN},
{.fd=timers[1], .events=POLL_IN},
{.fd=timers[2], .events=POLL_IN}};
u8 type = 0;
assert(poll(pfd, elem(pfd), -1) != -1);
if(pfd[0].revents){
memset(&bp, 0, sizeof(bp));
udprecv(IP(255,255,255,255), sock, &bp, sizeof(bp));
optget(&bp, &type, ODtype, sizeof(type));
return type;
}
u64 n = 0;
if(pfd[1].revents){
type = Timeout0;
read(timers[0], &n, sizeof(n));
}
if(pfd[2].revents){
type = Timeout1;
read(timers[0], &n, sizeof(n));
}
if(pfd[3].revents){
type = Timeout2;
read(timers[2], &n, sizeof(n));
};
return type;
}
static void
settimeout(int i, u32 t)
{
timeout.it_value.tv_sec = t;
assert(timerfd_settime(timers[i], 0, &timeout, 0) >= 0);
}
static void
setdifftime(int i)
{
assert(timerfd_gettime(timers[i], &timeout) >= 0);
timeout.it_value.tv_nsec /= 2;
if(timeout.it_value.tv_sec % 2)
timeout.it_value.tv_nsec += 500000000;
timeout.it_value.tv_sec /= 2;
if(timeout.it_value.tv_sec < 60){
timeout.it_value.tv_sec = 60;
timeout.it_value.tv_nsec = 0;
}
}
static void
setdns(u8 dns[4])
{
char buf[1024];
int fd;
assert((fd = open("/etc/resolv.conf",O_CREAT|O_RDWR, 0644)) != -1);
int wc = snprintf(buf, sizeof(buf)-1, "\nnameserver %u.%u.%u.%u\n",
dns[0],dns[1],dns[2],dns[3]);
assert(wc != -1);
assert(wc == write(fd, buf, wc));
close(fd);
}
static void
setip(u8 ip[4], u8 mask[4], u8 gw[4])
{
struct ifreq i = {0};
struct rtentry r = {0}; // routing table
int fd = -1;
assert((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) != -1);
strlcpy(i.ifr_name, ifname, IF_NAMESIZE);
iptoaddr(&i.ifr_addr, ip, 0);
ioctl(fd, SIOCSIFADDR, &i);
iptoaddr(&i.ifr_netmask, mask, 0);
ioctl(fd, SIOCSIFNETMASK, &i);
i.ifr_flags = IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST;
ioctl(fd, SIOCSIFFLAGS, &i);
r.rt_flags = RTF_UP | RTF_GATEWAY;
iptoaddr(&r.rt_gateway, gw, 0);
iptoaddr(&r.rt_genmask, IP(0,0,0,0), 0);
iptoaddr(&r.rt_dst, IP(0,0,0,0), 0);
ioctl(fd, SIOCADDRT, &r);
close(fd);
}
static void
acceptlease(void)
{
setip(client, mask, router);
setdns(dns);
}
static void
run(void)
{
u32 renewaltime, rebindtime, lease;
Init:
dhcpsend(Discover, Broadcast);
settimeout(0, 1);
for(;;)
switch(dhcprecv()){
case Offer:
memcpy(client, bp.yiaddr, sizeof(client));
optget(&bp, server, ODserverid, sizeof(server));
goto Requesting;
case Timeout0: goto Init;
default: assert(0);
}
Requesting:
for(int t = 0; t < 5; ++t){
dhcpsend(Request, Broadcast);
settimeout(0, 4);
switch(dhcprecv()){
case Ack: goto Bound;
case Nak: goto Init;
case Timeout0: break;
default: assert(0);
}
}
fprintf(stderr, "timeout\n");
cexit(1);
Bound:
optget(&bp, mask, OBmask, sizeof(mask));
optget(&bp, router, OBrouter, sizeof(router));
optget(&bp, dns, OBdnsserver, sizeof(dns));
optget(&bp, &renewaltime, ODrenewaltime, sizeof(renewaltime));
optget(&bp, &lease, ODlease, sizeof(lease));
renewaltime = ntohl(renewaltime);
rebindtime = ntohl(rebindtime);
lease = ntohl(lease);
acceptlease();
printf("success lease\n");
settimeout(0, renewaltime);
settimeout(1, rebindtime);
settimeout(2, lease);
for(;;)
switch(dhcprecv()){
case Timeout0: goto Renewing;
case Timeout1: goto Rebinding;
case Timeout2: goto Init;
}
Renewing:
dhcpsend(Request, Unicast);
setdifftime(1);
settimeout(0, timeout.it_value.tv_sec);
for(;;)
switch(dhcprecv()){
case Ack: goto Bound;
case Timeout0: goto Renewing;
case Timeout1: goto Rebinding;
case Timeout2:
case Nak: goto Init;
default: assert(0);
}
Rebinding:
setdifftime(2);
settimeout(0,timeout.it_value.tv_sec);
dhcpsend(Request, Broadcast);
for(;;)
switch(dhcprecv()){
case Ack: goto Bound;
case Timeout0: goto Rebinding;
case Timeout2:
case Nak: goto Init;
case Timeout1: break;
default: assert(0);
}
}
static void
dhcpinit(void)
{
int fd = open("/dev/urandom", O_RDONLY);
if(fd >= 0){
read(fd, &xid, sizeof(xid));
close(fd);
}else
xid = time(0)*getpid();
srand(xid);
}
static void
openlisten(void)
{
int b = 1;
struct ifreq r = {0};
struct sockaddr a = {0};
assert((sock = socket(AF_INET, SOCK_DGRAM, 0)) != -1);
assert(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &b, sizeof(b)) != -1);
strlcpy(r.ifr_name, ifname, IF_NAMESIZE);
ioctl(sock, SIOCGIFINDEX, &r);
assert(setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &r, sizeof(r)) != -1);
iptoaddr(&a, IP(255,255,255,255), SrcPort);
assert(bind(sock, &a, sizeof(a)) == 0);
ioctl(sock, SIOCGIFHWADDR, &r);
memcpy(hwaddr, r.ifr_hwaddr.sa_data, sizeof(r.ifr_hwaddr.sa_data));
memcpy(cid, hwaddr, sizeof(cid));
}
int
main(int argc, char *argv[])
{
if(argc == 2) ifname = argv[1];
else ifname = "wlp3s0";
signal(SIGTERM, cexit);
gethostname(hostname, sizeof(hostname));
dhcpinit();
openlisten();
for(int i = 0; i < elem(timers); ++i)
assert((timers[i]=timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) != -1);
begat = time(0);
run();
return 0;
}