This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcp.c
3791 lines (3404 loc) · 91.5 KB
/
dhcp.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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dhcpcd - DHCP client daemon
* Copyright (c) 2006-2016 Roy Marples <[email protected]>
* 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
#include <netinet/udp.h>
#undef __FAVOR_BSD
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ELOOP_QUEUE 2
#include "config.h"
#include "arp.h"
#include "common.h"
#include "dhcp.h"
#include "dhcpcd.h"
#include "dhcp-common.h"
#include "duid.h"
#include "eloop.h"
#include "if.h"
#include "ipv4.h"
#include "ipv4ll.h"
#include "sa.h"
#include "script.h"
#define DAD "Duplicate address detected"
#define DHCP_MIN_LEASE 20
#define IPV4A ADDRIPV4 | ARRAY
#define IPV4R ADDRIPV4 | REQUEST
/* We should define a maximum for the NAK exponential backoff */
#define NAKOFF_MAX 60
/* Wait N nanoseconds between sending a RELEASE and dropping the address.
* This gives the kernel enough time to actually send it. */
#define RELEASE_DELAY_S 0
#define RELEASE_DELAY_NS 10000000
#ifndef IPDEFTTL
#define IPDEFTTL 64 /* RFC1340 */
#endif
/* Assert the correct structure size for on wire */
__CTASSERT(sizeof(struct ip) == 20);
__CTASSERT(sizeof(struct udphdr) == 8);
__CTASSERT(sizeof(struct bootp) == 300);
struct dhcp_op {
uint8_t value;
const char *name;
};
static const struct dhcp_op dhcp_ops[] = {
{ DHCP_DISCOVER, "DISCOVER" },
{ DHCP_OFFER, "OFFER" },
{ DHCP_REQUEST, "REQUEST" },
{ DHCP_DECLINE, "DECLINE" },
{ DHCP_ACK, "ACK" },
{ DHCP_NAK, "NAK" },
{ DHCP_RELEASE, "RELEASE" },
{ DHCP_INFORM, "INFORM" },
{ DHCP_FORCERENEW, "FORCERENEW"},
{ 0, NULL }
};
static const char * const dhcp_params[] = {
"ip_address",
"subnet_cidr",
"network_number",
"filename",
"server_name",
NULL
};
struct udp_bootp_packet
{
struct ip ip;
struct udphdr udp;
uint8_t bootp[];
};
static int dhcp_open(struct interface *);
#ifdef ARP
static void dhcp_arp_conflicted(struct arp_state *, const struct arp_msg *);
#endif
void
dhcp_printoptions(const struct dhcpcd_ctx *ctx,
const struct dhcp_opt *opts, size_t opts_len)
{
const char * const *p;
size_t i, j;
const struct dhcp_opt *opt, *opt2;
int cols;
for (p = dhcp_params; *p; p++)
printf(" %s\n", *p);
for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++) {
for (j = 0, opt2 = opts; j < opts_len; j++, opt2++)
if (opt->option == opt2->option)
break;
if (j == opts_len) {
cols = printf("%03d %s", opt->option, opt->var);
dhcp_print_option_encoding(opt, cols);
}
}
for (i = 0, opt = opts; i < opts_len; i++, opt++) {
cols = printf("%03d %s", opt->option, opt->var);
dhcp_print_option_encoding(opt, cols);
}
}
#define get_option_raw(ctx, bootp, bootp_len, opt) \
get_option((ctx), (bootp), (bootp_len), NULL)
static const uint8_t *
get_option(struct dhcpcd_ctx *ctx,
const struct bootp *bootp, size_t bootp_len,
unsigned int opt, size_t *opt_len)
{
const uint8_t *p, *e;
uint8_t l, o, ol, overl, *bp;
const uint8_t *op;
size_t bl;
/* Check we have the magic cookie */
if (!IS_DHCP(bootp)) {
errno = ENOTSUP;
return NULL;
}
p = bootp->vend + 4; /* options after the 4 byte cookie */
e = (const uint8_t *)bootp + bootp_len;
ol = o = overl = 0;
bp = NULL;
op = NULL;
bl = 0;
while (p < e) {
o = *p++;
switch (o) {
case DHO_PAD:
/* No length to read */
continue;
case DHO_END:
if (overl & 1) {
/* bit 1 set means parse boot file */
overl = (uint8_t)(overl & ~1);
p = bootp->file;
e = p + sizeof(bootp->file);
} else if (overl & 2) {
/* bit 2 set means parse server name */
overl = (uint8_t)(overl & ~2);
p = bootp->sname;
e = p + sizeof(bootp->sname);
} else
goto exit;
/* No length to read */
continue;
}
/* Check we can read the length */
if (p == e) {
errno = EINVAL;
return NULL;
}
l = *p++;
if (o == DHO_OPTSOVERLOADED) {
/* Ensure we only get this option once by setting
* the last bit as well as the value.
* This is valid because only the first two bits
* actually mean anything in RFC2132 Section 9.3 */
if (l == 1 && !overl)
overl = 0x80 | p[0];
}
if (o == opt) {
if (op) {
/* We must concatonate the options. */
if (bl + l > ctx->opt_buffer_len) {
size_t pos;
uint8_t *nb;
if (bp)
pos = (size_t)
(bp - ctx->opt_buffer);
else
pos = 0;
nb = realloc(ctx->opt_buffer, bl + l);
if (nb == NULL)
return NULL;
ctx->opt_buffer = nb;
ctx->opt_buffer_len = bl + l;
bp = ctx->opt_buffer + pos;
}
if (bp == NULL)
bp = ctx->opt_buffer;
memcpy(bp, op, ol);
bp += ol;
}
ol = l;
if (p + ol >= e) {
errno = EINVAL;
return NULL;
}
op = p;
bl += ol;
}
p += l;
}
exit:
if (opt_len)
*opt_len = bl;
if (bp) {
memcpy(bp, op, ol);
return (const uint8_t *)ctx->opt_buffer;
}
if (op)
return op;
errno = ENOENT;
return NULL;
}
static int
get_option_addr(struct dhcpcd_ctx *ctx,
struct in_addr *a, const struct bootp *bootp, size_t bootp_len,
uint8_t option)
{
const uint8_t *p;
size_t len;
p = get_option(ctx, bootp, bootp_len, option, &len);
if (!p || len < (ssize_t)sizeof(a->s_addr))
return -1;
memcpy(&a->s_addr, p, sizeof(a->s_addr));
return 0;
}
static int
get_option_uint32(struct dhcpcd_ctx *ctx,
uint32_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option)
{
const uint8_t *p;
size_t len;
uint32_t d;
p = get_option(ctx, bootp, bootp_len, option, &len);
if (!p || len < (ssize_t)sizeof(d))
return -1;
memcpy(&d, p, sizeof(d));
if (i)
*i = ntohl(d);
return 0;
}
static int
get_option_uint16(struct dhcpcd_ctx *ctx,
uint16_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option)
{
const uint8_t *p;
size_t len;
uint16_t d;
p = get_option(ctx, bootp, bootp_len, option, &len);
if (!p || len < (ssize_t)sizeof(d))
return -1;
memcpy(&d, p, sizeof(d));
if (i)
*i = ntohs(d);
return 0;
}
static int
get_option_uint8(struct dhcpcd_ctx *ctx,
uint8_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option)
{
const uint8_t *p;
size_t len;
p = get_option(ctx, bootp, bootp_len, option, &len);
if (!p || len < (ssize_t)sizeof(*p))
return -1;
if (i)
*i = *(p);
return 0;
}
ssize_t
decode_rfc3442(char *out, size_t len, const uint8_t *p, size_t pl)
{
const uint8_t *e;
size_t bytes = 0, ocets;
int b;
uint8_t cidr;
struct in_addr addr;
char *o = out;
/* Minimum is 5 -first is CIDR and a router length of 4 */
if (pl < 5) {
errno = EINVAL;
return -1;
}
e = p + pl;
while (p < e) {
cidr = *p++;
if (cidr > 32) {
errno = EINVAL;
return -1;
}
ocets = (size_t)(cidr + 7) / NBBY;
if (p + 4 + ocets > e) {
errno = ERANGE;
return -1;
}
if (!out) {
p += 4 + ocets;
bytes += ((4 * 4) * 2) + 4;
continue;
}
if ((((4 * 4) * 2) + 4) > len) {
errno = ENOBUFS;
return -1;
}
if (o != out) {
*o++ = ' ';
len--;
}
/* If we have ocets then we have a destination and netmask */
if (ocets > 0) {
addr.s_addr = 0;
memcpy(&addr.s_addr, p, ocets);
b = snprintf(o, len, "%s/%d", inet_ntoa(addr), cidr);
p += ocets;
} else
b = snprintf(o, len, "0.0.0.0/0");
o += b;
len -= (size_t)b;
/* Finally, snag the router */
memcpy(&addr.s_addr, p, 4);
p += 4;
b = snprintf(o, len, " %s", inet_ntoa(addr));
o += b;
len -= (size_t)b;
}
if (out)
return o - out;
return (ssize_t)bytes;
}
static int
decode_rfc3442_rt(struct rt_head *routes, struct interface *ifp,
const uint8_t *data, size_t dl, const struct bootp *bootp)
{
const uint8_t *p = data;
const uint8_t *e;
uint8_t cidr;
size_t ocets;
struct rt *rt = NULL;
struct in_addr dest, netmask, gateway;
int n;
/* Minimum is 5 -first is CIDR and a router length of 4 */
if (dl < 5) {
errno = EINVAL;
return -1;
}
n = 0;
e = p + dl;
while (p < e) {
cidr = *p++;
if (cidr > 32) {
errno = EINVAL;
return -1;
}
ocets = (size_t)(cidr + 7) / NBBY;
if (p + 4 + ocets > e) {
errno = ERANGE;
return -1;
}
if ((rt = rt_new(ifp)) == NULL)
return -1;
TAILQ_INSERT_TAIL(routes, rt, rt_next);
/* If we have ocets then we have a destination and netmask */
dest.s_addr = 0;
if (ocets > 0) {
memcpy(&dest.s_addr, p, ocets);
p += ocets;
netmask.s_addr = htonl(~0U << (32 - cidr));
} else
netmask.s_addr = 0;
/* Finally, snag the router */
memcpy(&gateway.s_addr, p, 4);
p += 4;
/* A host route is normally set by having the
* gateway match the destination or assigned address */
if (gateway.s_addr == dest.s_addr ||
(gateway.s_addr == bootp->yiaddr ||
gateway.s_addr == bootp->ciaddr))
{
gateway.s_addr = INADDR_ANY;
netmask.s_addr = INADDR_BROADCAST;
rt->rt_flags = RTF_HOST;
}
sa_in_init(&rt->rt_dest, &dest);
sa_in_init(&rt->rt_dest, &netmask);
sa_in_init(&rt->rt_gateway, &gateway);
/* If CIDR is 32 then it's a host route. */
if (cidr == 32)
rt->rt_flags = RTF_HOST;
n++;
}
return n;
}
char *
decode_rfc3361(const uint8_t *data, size_t dl)
{
uint8_t enc;
size_t l;
ssize_t r;
char *sip = NULL;
struct in_addr addr;
char *p;
if (dl < 2) {
errno = EINVAL;
return 0;
}
enc = *data++;
dl--;
switch (enc) {
case 0:
if ((r = decode_rfc1035(NULL, 0, data, dl)) > 0) {
l = (size_t)r;
sip = malloc(l);
if (sip == NULL)
return 0;
decode_rfc1035(sip, l, data, dl);
}
break;
case 1:
if (dl == 0 || dl % 4 != 0) {
errno = EINVAL;
break;
}
addr.s_addr = INADDR_BROADCAST;
l = ((dl / sizeof(addr.s_addr)) * ((4 * 4) + 1)) + 1;
sip = p = malloc(l);
if (sip == NULL)
return 0;
while (dl != 0) {
memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
data += sizeof(addr.s_addr);
p += snprintf(p, l - (size_t)(p - sip),
"%s ", inet_ntoa(addr));
dl -= sizeof(addr.s_addr);
}
*--p = '\0';
break;
default:
errno = EINVAL;
return 0;
}
return sip;
}
static char *
get_option_string(struct dhcpcd_ctx *ctx,
const struct bootp *bootp, size_t bootp_len, uint8_t option)
{
size_t len;
const uint8_t *p;
char *s;
p = get_option(ctx, bootp, bootp_len, option, &len);
if (!p || len == 0 || *p == '\0')
return NULL;
s = malloc(sizeof(char) * (len + 1));
if (s) {
memcpy(s, p, len);
s[len] = '\0';
}
return s;
}
/* This calculates the netmask that we should use for static routes.
* This IS different from the calculation used to calculate the netmask
* for an interface address. */
static uint32_t
route_netmask(uint32_t ip_in)
{
/* used to be unsigned long - check if error */
uint32_t p = ntohl(ip_in);
uint32_t t;
if (IN_CLASSA(p))
t = ~IN_CLASSA_NET;
else {
if (IN_CLASSB(p))
t = ~IN_CLASSB_NET;
else {
if (IN_CLASSC(p))
t = ~IN_CLASSC_NET;
else
t = 0;
}
}
while (t & p)
t >>= 1;
return (htonl(~t));
}
/* We need to obey routing options.
* If we have a CSR then we only use that.
* Otherwise we add static routes and then routers. */
static int
get_option_routes(struct rt_head *routes, struct interface *ifp,
const struct bootp *bootp, size_t bootp_len)
{
struct if_options *ifo = ifp->options;
const uint8_t *p;
const uint8_t *e;
struct rt *rt = NULL;
struct in_addr dest, netmask, gateway;
size_t len;
const char *csr = "";
int n;
/* If we have CSR's then we MUST use these only */
if (!has_option_mask(ifo->nomask, DHO_CSR))
p = get_option(ifp->ctx, bootp, bootp_len, DHO_CSR, &len);
else
p = NULL;
/* Check for crappy MS option */
if (!p && !has_option_mask(ifo->nomask, DHO_MSCSR)) {
p = get_option(ifp->ctx, bootp, bootp_len, DHO_MSCSR, &len);
if (p)
csr = "MS ";
}
if (p && (n = decode_rfc3442_rt(routes, ifp, p, len, bootp)) != -1) {
const struct dhcp_state *state;
state = D_CSTATE(ifp);
if (!(ifo->options & DHCPCD_CSR_WARNED) &&
!(state->added & STATE_FAKE))
{
logger(ifp->ctx, LOG_DEBUG,
"%s: using %sClassless Static Routes",
ifp->name, csr);
ifo->options |= DHCPCD_CSR_WARNED;
}
return n;
}
n = 0;
/* OK, get our static routes first. */
if (!has_option_mask(ifo->nomask, DHO_STATICROUTE))
p = get_option(ifp->ctx, bootp, bootp_len,
DHO_STATICROUTE, &len);
else
p = NULL;
/* RFC 2131 Section 5.8 states length MUST be in multiples of 8 */
if (p && len % 8 == 0) {
e = p + len;
while (p < e) {
memcpy(&dest.s_addr, p, sizeof(dest.s_addr));
p += 4;
memcpy(&gateway.s_addr, p, sizeof(gateway.s_addr));
p += 4;
/* RFC 2131 Section 5.8 states default route is
* illegal */
if (sa_is_unspecified(&rt->rt_dest))
continue;
if ((rt = rt_new(ifp)) == NULL)
return -1;
/* A host route is normally set by having the
* gateway match the destination or assigned address */
if (gateway.s_addr == dest.s_addr ||
(gateway.s_addr == bootp->yiaddr ||
gateway.s_addr == bootp->ciaddr))
{
gateway.s_addr = INADDR_ANY;
netmask.s_addr = INADDR_BROADCAST;
rt->rt_flags = RTF_HOST;
} else
netmask.s_addr = route_netmask(dest.s_addr);
sa_in_init(&rt->rt_dest, &dest);
sa_in_init(&rt->rt_netmask, &netmask);
sa_in_init(&rt->rt_gateway, &gateway);
TAILQ_INSERT_TAIL(routes, rt, rt_next);
n++;
}
}
/* Now grab our routers */
if (!has_option_mask(ifo->nomask, DHO_ROUTER))
p = get_option(ifp->ctx, bootp, bootp_len, DHO_ROUTER, &len);
else
p = NULL;
if (p) {
e = p + len;
dest.s_addr = INADDR_ANY;
netmask.s_addr = INADDR_ANY;
while (p < e) {
if ((rt = rt_new(ifp)) == NULL)
return -1;
memcpy(&gateway.s_addr, p, sizeof(gateway.s_addr));
p += 4;
sa_in_init(&rt->rt_dest, &dest);
sa_in_init(&rt->rt_netmask, &netmask);
sa_in_init(&rt->rt_gateway, &gateway);
TAILQ_INSERT_TAIL(routes, rt, rt_next);
n++;
}
}
return n;
}
uint16_t
dhcp_get_mtu(const struct interface *ifp)
{
const struct dhcp_state *state;
uint16_t mtu;
if (ifp->options->mtu)
return (uint16_t)ifp->options->mtu;
mtu = 0; /* bogus gcc warning */
if ((state = D_CSTATE(ifp)) == NULL ||
has_option_mask(ifp->options->nomask, DHO_MTU) ||
get_option_uint16(ifp->ctx, &mtu,
state->new, state->new_len, DHO_MTU) == -1)
return 0;
return mtu;
}
/* Grab our routers from the DHCP message and apply any MTU value
* the message contains */
int
dhcp_get_routes(struct rt_head *routes, struct interface *ifp)
{
const struct dhcp_state *state;
if ((state = D_CSTATE(ifp)) == NULL || state->state != DHS_BOUND)
return 0;
return get_option_routes(routes, ifp, state->new, state->new_len);
}
/* Assumes DHCP options */
static int
dhcp_message_add_addr(struct bootp *bootp,
uint8_t type, struct in_addr addr)
{
uint8_t *p;
size_t len;
p = bootp->vend;
while (*p != DHO_END) {
p++;
p += *p + 1;
}
len = (size_t)(p - bootp->vend);
if (len + 6 > sizeof(bootp->vend)) {
errno = ENOMEM;
return -1;
}
*p++ = type;
*p++ = 4;
memcpy(p, &addr.s_addr, 4);
p += 4;
*p = DHO_END;
return 0;
}
static ssize_t
make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type)
{
struct bootp *bootp;
uint8_t *lp, *p, *e;
uint8_t *n_params = NULL;
uint32_t ul;
uint16_t sz;
size_t len, i;
const struct dhcp_opt *opt;
struct if_options *ifo = ifp->options;
const struct dhcp_state *state = D_CSTATE(ifp);
const struct dhcp_lease *lease = &state->lease;
char hbuf[HOSTNAME_MAX_LEN + 1];
const char *hostname;
const struct vivco *vivco;
int mtu;
#ifdef AUTH
uint8_t *auth, auth_len;
#endif
if ((mtu = if_getmtu(ifp)) == -1)
logger(ifp->ctx, LOG_ERR,
"%s: if_getmtu: %m", ifp->name);
else if (mtu < MTU_MIN) {
if (if_setmtu(ifp, MTU_MIN) == -1)
logger(ifp->ctx, LOG_ERR,
"%s: if_setmtu: %m", ifp->name);
mtu = MTU_MIN;
}
if (ifo->options & DHCPCD_BOOTP)
bootp = calloc(1, sizeof (*bootp));
else
/* Make the maximal message we could send */
bootp = calloc(1, (size_t)(mtu - IP_UDP_SIZE));
if (bootp == NULL)
return -1;
*bootpm = bootp;
if (state->addr != NULL &&
(type == DHCP_INFORM || type == DHCP_RELEASE ||
(type == DHCP_REQUEST &&
state->addr->mask.s_addr == lease->mask.s_addr &&
(state->new == NULL || IS_DHCP(state->new)))))
bootp->ciaddr = state->addr->addr.s_addr;
bootp->op = BOOTREQUEST;
bootp->htype = (uint8_t)ifp->family;
switch (ifp->family) {
case ARPHRD_ETHER:
case ARPHRD_IEEE802:
bootp->hlen = (uint8_t)ifp->hwlen;
memcpy(&bootp->chaddr, &ifp->hwaddr, ifp->hwlen);
break;
}
if (ifo->options & DHCPCD_BROADCAST &&
bootp->ciaddr == 0 &&
type != DHCP_DECLINE &&
type != DHCP_RELEASE)
bootp->flags = htons(BROADCAST_FLAG);
if (type != DHCP_DECLINE && type != DHCP_RELEASE) {
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
timespecsub(&tv, &state->started, &tv);
if (tv.tv_sec < 0 || tv.tv_sec > (time_t)UINT16_MAX)
bootp->secs = htons((uint16_t)UINT16_MAX);
else
bootp->secs = htons((uint16_t)tv.tv_sec);
}
bootp->xid = htonl(state->xid);
if (ifo->options & DHCPCD_BOOTP)
return sizeof(*bootp);
p = bootp->vend;
e = (uint8_t *)bootp + (mtu - IP_UDP_SIZE) - 1; /* -1 for DHO_END */
ul = htonl(MAGIC_COOKIE);
memcpy(p, &ul, sizeof(ul));
p += sizeof(ul);
*p++ = DHO_MESSAGETYPE;
*p++ = 1;
*p++ = type;
#define AREA_LEFT (size_t)(e - p)
#define AREA_FIT(s) if ((s) > AREA_LEFT) goto toobig
#define AREA_CHECK(s) if ((s) + 2UL > AREA_LEFT) goto toobig
#define PUT_ADDR(o, a) do { \
AREA_CHECK(4); \
*p++ = (o); \
*p++ = 4; \
memcpy(p, &(a)->s_addr, 4); \
p += 4; \
} while (0 /* CONSTCOND */)
if (state->clientid) {
AREA_CHECK(state->clientid[0]);
*p++ = DHO_CLIENTID;
memcpy(p, state->clientid, (size_t)state->clientid[0] + 1);
p += state->clientid[0] + 1;
}
if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) {
if (type == DHCP_DECLINE ||
(type == DHCP_REQUEST &&
(state->addr == NULL ||
lease->addr.s_addr != state->addr->addr.s_addr)))
{
PUT_ADDR(DHO_IPADDRESS, &lease->addr);
if (lease->server.s_addr)
PUT_ADDR(DHO_SERVERID, &lease->server);
}
if (type == DHCP_RELEASE) {
if (lease->server.s_addr)
PUT_ADDR(DHO_SERVERID, &lease->server);
}
}
if (type == DHCP_DECLINE) {
len = strlen(DAD);
if (len > AREA_LEFT) {
*p++ = DHO_MESSAGE;
*p++ = (uint8_t)len;
memcpy(p, DAD, len);
p += len;
}
}
if (type == DHCP_DISCOVER &&
!(ifp->ctx->options & DHCPCD_TEST) &&
has_option_mask(ifo->requestmask, DHO_RAPIDCOMMIT))
{
/* RFC 4039 Section 3 */
AREA_CHECK(0);
*p++ = DHO_RAPIDCOMMIT;
*p++ = 0;
}
if (type == DHCP_DISCOVER && ifo->options & DHCPCD_REQUEST)
PUT_ADDR(DHO_IPADDRESS, &ifo->req_addr);
/* RFC 2563 Auto Configure */
if (type == DHCP_DISCOVER && ifo->options & DHCPCD_IPV4LL) {
AREA_CHECK(1);
*p++ = DHO_AUTOCONFIGURE;
*p++ = 1;
*p++ = 1;
}
if (type == DHCP_DISCOVER ||
type == DHCP_INFORM ||
type == DHCP_REQUEST)
{
if (mtu != -1) {
AREA_CHECK(2);
*p++ = DHO_MAXMESSAGESIZE;
*p++ = 2;
sz = htons((uint16_t)(mtu - IP_UDP_SIZE));
memcpy(p, &sz, 2);
p += 2;
}
if (ifo->userclass[0]) {
AREA_CHECK(ifo->userclass[0]);
*p++ = DHO_USERCLASS;
memcpy(p, ifo->userclass,
(size_t)ifo->userclass[0] + 1);
p += ifo->userclass[0] + 1;
}
if (ifo->vendorclassid[0]) {
AREA_CHECK(ifo->vendorclassid[0]);
*p++ = DHO_VENDORCLASSID;
memcpy(p, ifo->vendorclassid,
(size_t)ifo->vendorclassid[0] + 1);
p += ifo->vendorclassid[0] + 1;
}
if (type != DHCP_INFORM) {
if (ifo->leasetime != 0) {
AREA_CHECK(4);
*p++ = DHO_LEASETIME;
*p++ = 4;
ul = htonl(ifo->leasetime);
memcpy(p, &ul, 4);
p += 4;
}
}
hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo);
/*
* RFC4702 3.1 States that if we send the Client FQDN option
* then we MUST NOT also send the Host Name option.
* Technically we could, but that is not RFC conformant and
* also seems to break some DHCP server implemetations such as
* Windows. On the other hand, ISC dhcpd is just as non RFC
* conformant by not accepting a partially qualified FQDN.
*/
if (ifo->fqdn != FQDN_DISABLE) {
/* IETF DHC-FQDN option (81), RFC4702 */
i = 3;
if (hostname)
i += encode_rfc1035(hostname, NULL);
AREA_CHECK(i);
*p++ = DHO_FQDN;
*p++ = (uint8_t)i;
/*
* Flags: 0000NEOS
* S: 1 => Client requests Server to update
* a RR in DNS as well as PTR
* O: 1 => Server indicates to client that
* DNS has been updated
* E: 1 => Name data is DNS format
* N: 1 => Client requests Server to not
* update DNS
*/
if (hostname)
*p++ = (uint8_t)((ifo->fqdn & 0x09) | 0x04);
else
*p++ = (FQDN_NONE & 0x09) | 0x04;
*p++ = 0; /* from server for PTR RR */
*p++ = 0; /* from server for A RR if S=1 */
if (hostname) {
i = encode_rfc1035(hostname, p);
p += i;
}
} else if (ifo->options & DHCPCD_HOSTNAME && hostname) {
len = strlen(hostname);
AREA_CHECK(len);
*p++ = DHO_HOSTNAME;
*p++ = (uint8_t)len;
memcpy(p, hostname, len);
p += len;
}
/* vendor is already encoded correctly, so just add it */
if (ifo->vendor[0]) {
AREA_CHECK(ifo->vendor[0]);
*p++ = DHO_VENDOR;
memcpy(p, ifo->vendor, (size_t)ifo->vendor[0] + 1);
p += ifo->vendor[0] + 1;
}
#ifdef AUTH
if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) !=
DHCPCD_AUTH_SENDREQUIRE)
{
/* We support HMAC-MD5 */
AREA_CHECK(1);
*p++ = DHO_FORCERENEW_NONCE;
*p++ = 1;
*p++ = AUTH_ALG_HMAC_MD5;
}