-
Notifications
You must be signed in to change notification settings - Fork 101
/
dhcpclient.c
924 lines (734 loc) · 21 KB
/
dhcpclient.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/*
* Copyright (c) 2004 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* dhcpclient.c
*
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: dhcpclient.c,v 1.6 2005/07/20 21:13:11 provos Exp $
*/
#include <sys/param.h>
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/queue.h>
#include <sys/tree.h>
#include <pcap.h>
#include <stdlib.h>
#include <err.h>
#include <syslog.h>
#include <string.h>
#include <syslog.h>
#include <dnet.h>
#include <event.h>
#include "honeyd.h"
#include "interface.h"
#include "arp.h"
#include "template.h"
#include "dhcpclient.h"
extern char *templateDump;
extern rand_t *honeyd_rand;
int need_dhcp = 0; /* set to one if a configuration specifies dhcp */
static struct timeval _timeout_tv = {2, 0};
#define NTRIES 10
static int _pack_request(struct dhcpclient_req *, void *, size_t *);
static int _pack_release(struct dhcpclient_req *, void *, size_t *);
static int _pack_renew(struct dhcpclient_req *, void *, size_t *);
static int _bcast(struct template *,
int (*)(struct dhcpclient_req *, void *, size_t *));
static int _unicast(struct template *,
int (*)(struct dhcpclient_req *, void *, size_t *));
static void _dhcp_timeout_cb(int, short, void *);
static void _dhcp_renew_timeout_cb(int, short, void *);
static void _dhcp_reply(struct template *, u_char *, size_t);
static struct template * _dhcp_dequeue();
int _dhcp_getconf(struct template *);
//DHCP Queue type definitions
typedef struct node
{
struct template* m_template;
struct node* m_next;
} QueueNode;
typedef struct
{
QueueNode* m_front;
QueueNode* m_rear;
unsigned int m_count;
} Queue;
static Queue *dhcp_queue = NULL;
void
queue_dhcp_discover(struct template *tmpl)
{
//Initialize the queue if this is the first operation
if(dhcp_queue == NULL)
{
dhcp_queue = (Queue*) malloc(sizeof(Queue));
dhcp_queue->m_count = 0;
dhcp_queue->m_front = NULL;
dhcp_queue->m_rear = NULL;
}
QueueNode* nextQueueNode;
if(!(nextQueueNode = (QueueNode*)malloc(sizeof(QueueNode))))
{
//TODO: malloc returned an error, let's at least make a warning
return;
}
nextQueueNode->m_template = tmpl;
nextQueueNode->m_next = NULL;
if (dhcp_queue->m_count == 0)
{
dhcp_queue->m_front = nextQueueNode;
}
else
{
dhcp_queue->m_rear->m_next = nextQueueNode;
}
(dhcp_queue->m_count)++;
dhcp_queue->m_rear = nextQueueNode;
}
struct template *_dhcp_dequeue()
{
if(dhcp_queue == NULL)
{
return NULL;
}
if(dhcp_queue->m_count == 0)
{
return 0;
}
(dhcp_queue->m_count)--;
QueueNode* front = dhcp_queue->m_front;
dhcp_queue->m_front = front->m_next;
struct template *template = front->m_template;
free(front);
return template;
}
void
dhcp_send_discover()
{
//This will start the chain of discoveries. We only need to start it
// out with the front of the queue if this is the first run
struct template *template = _dhcp_dequeue();
if(template != NULL)
{
_dhcp_getconf(template);
}
}
int
_dhcp_getconf(struct template *tmpl)
{
struct dhcpclient_req *req = tmpl->dhcp_req;
struct interface *inter = tmpl->inter;
if (req == NULL) {
req = calloc(1, sizeof(struct dhcpclient_req));
if (req == NULL)
{
syslog(LOG_ERR, "%s: calloc",__func__);
exit(EXIT_FAILURE);
}
tmpl->dhcp_req = req;
}
syslog(LOG_NOTICE, "[%s] trying DHCP", inter->if_ent.intf_name);
if (req->state != 0) {
warnx("Aborting DHCP configuration in progress");
dhcp_abort(tmpl);
}
/* For now, do the pcap in here independently. */
gettimeofday(&req->timer, NULL);
assert(tmpl->ethernet_addr);
req->state |= DHREQ_STATE_WAITANS | DHREQ_STATE_BUSY;
req->xid = rand_uint32(honeyd_rand);
req->ea = tmpl->ethernet_addr->addr_eth;
if (_bcast(tmpl, _pack_request) < 0)
return (-1);
req->ntries = 0;
req->timeoutEv = evtimer_new(libevent_base, _dhcp_timeout_cb, tmpl);
evtimer_add(req->timeoutEv, &_timeout_tv);
return (0);
}
int
dhcp_release(struct template *tmpl)
{
struct dhcpclient_req *req = tmpl->dhcp_req;
if (!(req->nc.defined & NC_HOSTADDR))
return (-1);
return (_unicast(tmpl, _pack_release));
}
void
dhcp_abort(struct template *tmpl)
{
struct dhcpclient_req *req = tmpl->dhcp_req;
if (req == NULL) {
struct addr *eth_addr = tmpl->ethernet_addr;
syslog(LOG_WARNING,
"%s: called without request on template %s",
__func__,
eth_addr != NULL ? addr_ntoa(eth_addr) : tmpl->name);
return;
}
if (req->state == 0)
return;
event_del(req->timeoutEv);
req->state = 0;
}
static void
_dhcp_renew_cb(int fd, short ev, void *arg)
{
struct template *tmpl = arg;
tmpl->dhcp_req->state = DHREQ_STATE_RENEWAL;
event_del(tmpl->dhcp_req->renewEv);
// Start a timer to trigger if the renew fails
tmpl->dhcp_req->renewTimeoutEv = evtimer_new(libevent_base, _dhcp_renew_timeout_cb, tmpl);
evtimer_add(tmpl->dhcp_req->renewTimeoutEv, &_timeout_tv);
_unicast(tmpl, _pack_renew);
}
static void
_dhcp_renew_timeout_cb(int fd, short ev, void *arg)
{
struct template *tmpl = arg;
event_del(tmpl->dhcp_req->renewTimeoutEv);
dhcp_template_new(tmpl);
}
static void
_dhcp_timeout_cb(int fd, short ev, void *arg)
{
struct template *tmpl = arg;
struct interface* inter = tmpl->inter;
struct dhcpclient_req *req = tmpl->dhcp_req;
struct timeval timeout_tv;
assert(inter != NULL);
if (req->ntries++ > NTRIES) {
printf("aborting dhclient on interface %s after %d tries\n",
inter->if_ent.intf_name, req->ntries);
dhcp_abort(tmpl);
//Try the next template
dhcp_send_discover();
return;
}
_bcast(tmpl, _pack_request);
if (req->ntries < 5)
timeout_tv.tv_sec = 1;
else
/* backoff on sending dhcp requests */
timeout_tv.tv_sec = 0x01 << (req->ntries - 4);
/* set a limit on the backoff */
if (timeout_tv.tv_sec > 128)
timeout_tv.tv_sec = 128;
timeout_tv.tv_usec = 0;
evtimer_add(req->timeoutEv, &timeout_tv);
}
static void
netconf_mknetmask(struct addr *ipaddr, struct addr *ipmask)
{
uint32_t mask;
u_int bits = 32;
mask = ntohl(ipmask->addr_ip);
while (!(mask & 0x1) && bits > 0) {
mask >>= 1;
bits--;
}
ipaddr->addr_bits = bits;
}
static void
_dhcp_reply(struct template *tmpl, u_char *buf, size_t buflen)
{
struct dhcpclient_req *req = tmpl->dhcp_req;
struct dhcp_msg *msg = (struct dhcp_msg *)buf;
size_t optlen = buflen - sizeof(*msg);
uint8_t *p, *end, opt1, opt1len, *opt1p;
uint32_t leaseTime = 0;
short replyreq = 0, ack = 0, done = 0;
struct netconf nc;
struct addr *which = NULL, ipmask;
if (req->xid != msg->dh_xid)
return;
memset(&nc, 0, sizeof(nc));
memset(&ipmask, 0, sizeof(ipmask));
/* Parse the options on the reply message */
p = (u_char *)msg + sizeof(*msg);
end = p + optlen;
while (p < end) {
opt1 = *p++;
if (p == end)
break;
if (opt1 != 0x00 && done)
goto optdone;
switch (opt1) {
case 0x00:
continue;
case 0xff:
done = 1;
continue;
default:
opt1len = *p++;
if (p + opt1len >= end)
goto optdone;
opt1p = p;
p += opt1len;
break;
}
switch (opt1) {
case DH_SUBNETMASK:
nc.defined |= NC_MASK;
which = &ipmask;
break;
case DH_ROUTER:
nc.defined |= NC_GWADDR;
which = &nc.gwaddr;
break;
case DH_NS:
nc.defined |= NC_NSADDR;
which = &nc.nsaddr[0];
break;
case DH_SERVIDENT:
which = &req->servident;
break;
default:
break;
}
switch(opt1) {
case DH_MSGTYPE:
if ((req->state & DHREQ_STATE_WAITANS) &&
(*opt1p == DH_MSGTYPE_OFFER))
replyreq = 1;
if ((req->state & DHREQ_STATE_WAITACK || req->state & DHREQ_STATE_RENEWAL) &&
(*opt1p == DH_MSGTYPE_ACK))
ack = 1;
break;
case DH_DOMAINNAME: {
size_t len = MIN(sizeof(nc.domain) - 1, opt1len);
memcpy(nc.domain, opt1p, len);
nc.domain[len] = '\0';
nc.defined |= NC_DOMAIN;
break;
}
case DH_LEASETIME:{
memcpy(&leaseTime, opt1p, sizeof(leaseTime));
leaseTime = ntohl(leaseTime);
break;
}
case DH_SERVIDENT:
case DH_SUBNETMASK:
case DH_NS:
case DH_ROUTER: {
uint32_t addr;
if (opt1len < IP_ADDR_LEN)
goto optdone;
memcpy(&addr, opt1p, sizeof(addr));
addr_pack(which, ADDR_TYPE_IP, IP_ADDR_BITS,
&addr, sizeof(addr));
break;
}
default:
break;
}
}
optdone:
/*
* XXX - Does not warn if the error is on the last one.. make
* opterr instead.
*/
if (p < end)
warnx("Error processing options");
if (ack || replyreq) {
uint32_t ipaddr;
req->nc = nc;
ipaddr = /* ntohl( */msg->dh_yiaddr/* ) */;
addr_pack(&req->nc.hostaddr, ADDR_TYPE_IP, IP_ADDR_BITS,
&ipaddr, sizeof(ipaddr));
req->nc.defined |= NC_HOSTADDR;
}
if (replyreq) {
req->state &= ~DHREQ_STATE_WAITANS;
req->state |= DHREQ_STATE_WAITACK;
_bcast(tmpl, _pack_request);
}
if (ack) {
struct addr addr = req->nc.hostaddr;
struct interface *inter = tmpl->inter;
// Set up renewal
if (leaseTime != 0)
{
req->renewTimer.tv_usec = 0;
req->renewTimer.tv_sec = leaseTime / 2;
req->renewEv = evtimer_new(libevent_base, _dhcp_renew_cb, tmpl);
evtimer_add(req->renewEv, &req->renewTimer);
}
// If this is a successful renewal, drop out here
if (req->state == DHREQ_STATE_RENEWAL)
{
syslog(LOG_NOTICE, "DHCP lease renewed with new lease of %d seconds\n", leaseTime);
req->state = DHREQ_STATE_GOTACK;
event_del(req->renewTimeoutEv);
return;
}
syslog(LOG_NOTICE, "[%s] got DHCP offer: %s",
inter->if_ent.intf_name, addr_ntoa(&addr));
dhcp_abort(tmpl);
//Abort resets the state variable, so we have to set it again afterward
req->state = DHREQ_STATE_GOTACK;
if (template_find(addr_ntoa(&addr)) != NULL) {
syslog(LOG_WARNING,
"%s: Already got a template named %s",
__func__, addr_ntoa(&addr));
return;
}
/* We are done - tell the template about our luck */
template_remove(tmpl);
free(tmpl->name);
tmpl->name = strdup(addr_ntoa(&addr));
if (tmpl->name == NULL)
{
syslog(LOG_ERR, "%s: strdup", __func__);
exit(EXIT_FAILURE);
}
tmpl->honeypot_instance = 1;
template_insert(tmpl);
template_dump_ips(templateDump);
/* Update our ARP table */
syslog(LOG_DEBUG, "Updating ARP binding: %s -> %s",
addr_ntoa(tmpl->ethernet_addr), tmpl->name);
template_remove_arp(tmpl);
template_post_arp(tmpl, &addr);
/* Callback for central configuration here. */
if (ipmask.addr_type != 0)
netconf_mknetmask(&req->nc.hostaddr, &ipmask);
else
req->nc.hostaddr.addr_bits = 24;
//If we got an ack, then go ahead and send another discover
dhcp_send_discover();
}
}
/*
* Receives a UDP packet from port 68 to port 67 from the Honeyd packet
* dispatcher and attempts to find the correct template for it.
*/
void
dhcp_recv_cb(struct eth_hdr *eth, struct ip_hdr *ip, u_short iplen)
{
struct arp_req *arp;
struct template *tmpl;
struct dhcpclient_req *req = NULL;
struct udp_hdr *udp;
size_t msglen;
struct dhcp_msg *msg;
uint16_t ip_sum, uh_sum;
struct addr eth_dha;
/* IPv4 only */
/* Check if we manage a virtual machine with this ethernet address */
addr_pack(ð_dha, ADDR_TYPE_ETH, ETH_ADDR_BITS,
ð->eth_dst, ETH_ADDR_LEN);
udp = (struct udp_hdr *)((u_char *)ip + (ip->ip_hl << 2));
msg = (struct dhcp_msg *)((u_char *)udp + UDP_HDR_LEN);
msglen = ntohs(udp->uh_ulen) - UDP_HDR_LEN;
memcpy(ð_dha.__addr_u.__data8[0], (&msg->dh_chaddr[0]), ETH_ADDR_LEN);
arp = arp_find(ð_dha);
if ( (arp == NULL) || !(arp->flags & ARP_INTERNAL))
return;
tmpl = arp->owner;
req = tmpl->dhcp_req;
if (req == NULL) {
syslog(LOG_WARNING, "%s: received DHCP reply for template %s "
"without dhcp_req", __func__, addr_ntoa(ð_dha));
return;
}
if (!(req->state & (DHREQ_STATE_WAITANS | DHREQ_STATE_WAITACK | DHREQ_STATE_RENEWAL)))
return;
if (msglen != (iplen - (ip->ip_hl << 2) - UDP_HDR_LEN))
return;
ip_sum = ip->ip_sum;
uh_sum = udp->uh_sum;
ip_checksum(ip, iplen);
if (ip_sum != ip->ip_sum || uh_sum != udp->uh_sum) {
syslog(LOG_WARNING, "%s: bad checksum for template %s",
__func__, addr_ntoa(ð_dha));
return;
}
/* save the servers address */
memcpy(&req->server_ea, ð->eth_src, ETH_ADDR_LEN);
_dhcp_reply(tmpl, (u_char *)msg, msglen);
}
static int
_bcast(struct template *tmpl,
int (*_pack)(struct dhcpclient_req *, void *, size_t *))
{
struct eth_hdr *eth;
uint8_t buf[1024], *p;
size_t restlen = 1024, len, iplen;
struct udp_hdr *udph;
struct ip_hdr *iph;
struct dhcpclient_req *req = tmpl->dhcp_req;
struct interface *inter = tmpl->inter;
assert(req != NULL);
assert(inter != NULL);
memset(buf, 0, sizeof(buf));
p = &buf[0];
eth = (struct eth_hdr *)p;
eth_pack_hdr(eth, ETH_ADDR_BROADCAST, req->ea, ETH_TYPE_IP);
restlen -= ETH_HDR_LEN;
p += ETH_HDR_LEN;
iph = (struct ip_hdr *)p;
ip_pack_hdr(iph, 0, 0, 0, 0, 16,
IP_PROTO_UDP, IP_ADDR_ANY, IP_ADDR_BROADCAST);
p += IP_HDR_LEN;
restlen -= IP_HDR_LEN;
udph = (struct udp_hdr *)p;
udp_pack_hdr(udph, 68, 67, 0);
p += UDP_HDR_LEN;
restlen -= UDP_HDR_LEN;
(*_pack)(req, p, &restlen);
len = 1024 - restlen;
iplen = len - ETH_HDR_LEN;
iph->ip_len = htons(iplen);
udph->uh_ulen = htons(iplen - IP_HDR_LEN);
ip_checksum(buf + ETH_HDR_LEN, iplen);
if (eth_send(inter->if_eth, buf, len) < 0)
{
syslog(LOG_ERR, "eth_send function call failed");
exit(EXIT_FAILURE);
}
return (0);
}
static int
_unicast(struct template *tmpl,
int (*_pack)(struct dhcpclient_req *, void *, size_t *))
{
struct eth_hdr *eth;
uint8_t buf[1024], *p;
size_t restlen = 1024, len, iplen;
struct udp_hdr *udph;
struct ip_hdr *iph;
struct dhcpclient_req *req = tmpl->dhcp_req;
struct interface *inter = tmpl->inter;
assert(req != NULL);
assert(inter != NULL);
memset(buf, 0, sizeof(buf));
p = &buf[0];
eth = (struct eth_hdr *)p;
eth_pack_hdr(eth, req->server_ea, req->ea, ETH_TYPE_IP);
restlen -= ETH_HDR_LEN;
p += ETH_HDR_LEN;
iph = (struct ip_hdr *)p;
ip_pack_hdr(iph, 0, 0, 0, 0, 16,
IP_PROTO_UDP,
req->nc.hostaddr.addr_ip, req->nc.gwaddr.addr_ip);
p += IP_HDR_LEN;
restlen -= IP_HDR_LEN;
udph = (struct udp_hdr *)p;
udp_pack_hdr(udph, 68, 67, 0);
p += UDP_HDR_LEN;
restlen -= UDP_HDR_LEN;
(*_pack)(req, p, &restlen);
len = 1024 - restlen;
iplen = len - ETH_HDR_LEN;
iph->ip_len = htons(iplen);
udph->uh_ulen = htons(iplen - IP_HDR_LEN);
ip_checksum(buf + ETH_HDR_LEN, iplen);
if (eth_send(inter->if_eth, buf, len) < 0)
{
syslog(LOG_ERR, "eth_send function call failed");
exit(EXIT_FAILURE);
}
return (0);
}
/*
* We should cache dhcp packets.
*/
static int
_pack_request(struct dhcpclient_req *req, void *buf, size_t *restlen)
{
struct dhcp_msg *msg;
u_char *p;
size_t optlen, padlen = 0;
struct timeval tv, difftv;
struct netconf *nc = &req->nc;
gettimeofday(&tv, NULL);
timersub(&tv, &req->timer, &difftv);
//3 bytes for Message type
//7 bytes for Requested Parameters
//1 byte for End of Options
optlen = (3) + (7) + (1);
optlen += 6 * (nc->defined & NC_HOSTADDR) +
6 * (req->servident.addr_type != 0);
/* optlen += (nc->defined & NC_DOMAIN) * strlen(nc->domain); */
if (*restlen < sizeof(*msg) + optlen)
return (-1);
msg = (struct dhcp_msg *)buf;
msg->dh_op = DH_BOOTREQUEST;
msg->dh_htype = DH_HTYPE_ETHERNET;
msg->dh_hlen = ETH_ADDR_LEN;
msg->dh_xid = req->xid;
msg->dh_secs = htons((uint16_t)difftv.tv_sec);
memcpy(msg->dh_chaddr, &req->ea, ETH_ADDR_LEN);
msg->dh_magiccookie = htonl(DH_MAGICCOOKIE);
p = (u_char *)buf + sizeof(*msg);
/* Options */
/* Message type */
*p++ = DH_MSGTYPE;
*p++ = 1;
*p++ = req->state & DHREQ_STATE_WAITANS ?
DH_MSGTYPE_DISCOVER : DH_MSGTYPE_REQUEST;
/* Requested Parameters */
*p++ = DH_PARAMREQ;
*p++ = 4; /* Number of parameters */
*p++ = 1; /* Subnet mask */
padlen += 4;
*p++ = 28; /* Broadcast address */
padlen += 4;
*p++ = 3; /* Router */
padlen += 4;
*p++ = 6; /* Domain name server */
padlen += 4;
/* *p++ = 12; /\* Host name *\/ */
if (nc->defined & NC_HOSTADDR) {
uint32_t ipaddr;
*p++ = DH_REQIP;
*p++ = 4;
ipaddr = /* htonl( */nc->hostaddr.addr_ip/* ) */;
memcpy(p, &ipaddr, IP_ADDR_LEN);
p += IP_ADDR_LEN;
}
if (req->servident.addr_type != 0) {
uint32_t ipaddr;
*p++ = DH_SERVIDENT;
*p++ = 4;
ipaddr = /* htonl( */req->servident.addr_ip/* ) */;
memcpy(p, &ipaddr, IP_ADDR_LEN);
p += IP_ADDR_LEN;
}
*p = 0xff; /* End options */
*restlen -= sizeof(*msg) + optlen;
if (*restlen >= padlen)
*restlen -= padlen; /* Fix for retarted DHCP servers. */
return (0);
}
static int
_pack_release(struct dhcpclient_req *req, void *buf, size_t *restlen)
{
struct dhcp_msg *msg;
u_char *p;
size_t optlen, padlen = 0;
struct netconf *nc = &req->nc;
optlen = (3) + (1); /* just message type */
if (*restlen < sizeof(*msg) + optlen)
return (-1);
msg = (struct dhcp_msg *)buf;
memset(msg, 0, sizeof(struct dhcp_msg));
msg->dh_op = DH_BOOTREQUEST;
msg->dh_htype = DH_HTYPE_ETHERNET;
msg->dh_hlen = ETH_ADDR_LEN;
msg->dh_xid = req->xid;
memcpy(msg->dh_chaddr, &req->ea, ETH_ADDR_LEN);
msg->dh_ciaddr = nc->hostaddr.addr_ip;
msg->dh_magiccookie = htonl(DH_MAGICCOOKIE);
p = (u_char *)buf + sizeof(*msg);
/* Options */
/* Message type */
*p++ = DH_MSGTYPE;
*p++ = 1;
*p++ = DH_MSGTYPE_RELEASE;
*p = 0xff; /* End options */
*restlen -= sizeof(*msg) + optlen;
if (*restlen >= padlen)
*restlen -= padlen; /* Fix for retarted DHCP servers. */
return (0);
}
static int
_pack_renew(struct dhcpclient_req *req, void *buf, size_t *restlen)
{
struct dhcp_msg *msg;
u_char *p;
size_t optlen, padlen = 0;
struct netconf *nc = &req->nc;
optlen = (3) + (1); /* just message type */
if (*restlen < sizeof(*msg) + optlen)
return (-1);
msg = (struct dhcp_msg *)buf;
memset(msg, 0, sizeof(struct dhcp_msg));
msg->dh_op = DH_BOOTREQUEST;
msg->dh_htype = DH_HTYPE_ETHERNET;
msg->dh_hlen = ETH_ADDR_LEN;
msg->dh_xid = req->xid;
memcpy(msg->dh_chaddr, &req->ea, ETH_ADDR_LEN);
msg->dh_ciaddr = nc->hostaddr.addr_ip;
msg->dh_magiccookie = htonl(DH_MAGICCOOKIE);
p = (u_char *)buf + sizeof(*msg);
/* Options */
/* Message type */
*p++ = DH_MSGTYPE;
*p++ = 1;
*p++ = DH_MSGTYPE_REQUEST;
*p = 0xff; /* End options */
*restlen -= sizeof(*msg) + optlen;
if (*restlen >= padlen)
*restlen -= padlen;
return (0);
}
void
dhcp_template_new(struct template *tmpl)
{
struct addr addr;
/* Need to find a temporary IP address */
if (template_get_dhcp_address(&addr) == -1) {
syslog(LOG_ERR, "Failed to obtain temporary IP address.");
return;
}
template_remove_arp(tmpl);
template_remove(tmpl);
free(tmpl->name);
tmpl->name = strdup(addr_ntoa(&addr));
template_post_arp(tmpl, &addr);
queue_dhcp_discover(tmpl);
dhcp_send_discover();
}