/
dhcp.c
1618 lines (1359 loc) · 35.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
/*
* dhcp.c Functions to send/receive dhcp packets.
*
* Version: $Id$
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2008 The FreeRADIUS server project
* Copyright 2008 Alan DeKok <aland@deployingradius.com>
*/
#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
#include <freeradius-devel/udpfromto.h>
#include <freeradius-devel/dhcp.h>
/*
* This doesn't appear to work right now.
*/
#undef WITH_UDPFROMTO
#ifdef WITH_DHCP
#include <sys/ioctl.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <net/if_arp.h>
#define DHCP_CHADDR_LEN (16)
#define DHCP_SNAME_LEN (64)
#define DHCP_FILE_LEN (128)
#define DHCP_VEND_LEN (308)
#define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
#ifndef INADDR_BROADCAST
#define INADDR_BROADCAST INADDR_NONE
#endif
typedef struct dhcp_packet_t {
uint8_t opcode;
uint8_t htype;
uint8_t hlen;
uint8_t hops;
uint32_t xid; /* 4 */
uint16_t secs; /* 8 */
uint16_t flags;
uint32_t ciaddr; /* 12 */
uint32_t yiaddr; /* 16 */
uint32_t siaddr; /* 20 */
uint32_t giaddr; /* 24 */
uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
uint8_t sname[DHCP_SNAME_LEN]; /* 44 */
uint8_t file[DHCP_FILE_LEN]; /* 108 */
uint32_t option_format; /* 236 */
uint8_t options[DHCP_VEND_LEN];
} dhcp_packet_t;
typedef struct dhcp_option_t {
uint8_t code;
uint8_t length;
} dhcp_option_t;
/*
* INADDR_ANY : 68 -> INADDR_BROADCAST : 67 DISCOVER
* INADDR_BROADCAST : 68 <- SERVER_IP : 67 OFFER
* INADDR_ANY : 68 -> INADDR_BROADCAST : 67 REQUEST
* INADDR_BROADCAST : 68 <- SERVER_IP : 67 ACK
*/
static const char *dhcp_header_names[] = {
"DHCP-Opcode",
"DHCP-Hardware-Type",
"DHCP-Hardware-Address-Length",
"DHCP-Hop-Count",
"DHCP-Transaction-Id",
"DHCP-Number-of-Seconds",
"DHCP-Flags",
"DHCP-Client-IP-Address",
"DHCP-Your-IP-Address",
"DHCP-Server-IP-Address",
"DHCP-Gateway-IP-Address",
"DHCP-Client-Hardware-Address",
"DHCP-Server-Host-Name",
"DHCP-Boot-Filename",
NULL
};
static const char *dhcp_message_types[] = {
"invalid",
"DHCP-Discover",
"DHCP-Offer",
"DHCP-Request",
"DHCP-Decline",
"DHCP-Ack",
"DHCP-NAK",
"DHCP-Release",
"DHCP-Inform",
"DHCP-Force-Renew",
};
static int dhcp_header_sizes[] = {
1, 1, 1, 1,
4, 2, 2, 4,
4, 4, 4,
DHCP_CHADDR_LEN,
DHCP_SNAME_LEN,
DHCP_FILE_LEN
};
/*
* Some clients silently ignore responses less than 300 bytes.
*/
#define MIN_PACKET_SIZE (244)
#define DEFAULT_PACKET_SIZE (300)
#define MAX_PACKET_SIZE (1500 - 40)
#define DHCP_OPTION_FIELD (0)
#define DHCP_FILE_FIELD (1)
#define DHCP_SNAME_FIELD (2)
static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
unsigned int option)
{
int overload = 0;
int field = DHCP_OPTION_FIELD;
size_t where, size;
uint8_t *data = packet->options;
where = 0;
size = packet_size - offsetof(dhcp_packet_t, options);
data = &packet->options[where];
while (where < size) {
if (data[0] == 0) { /* padding */
where++;
continue;
}
if (data[0] == 255) { /* end of options */
if ((field == DHCP_OPTION_FIELD) &&
(overload & DHCP_FILE_FIELD)) {
data = packet->file;
where = 0;
size = sizeof(packet->file);
field = DHCP_FILE_FIELD;
continue;
} else if ((field == DHCP_FILE_FIELD) &&
(overload && DHCP_SNAME_FIELD)) {
data = packet->sname;
where = 0;
size = sizeof(packet->sname);
field = DHCP_SNAME_FIELD;
continue;
}
return NULL;
}
/*
* We MUST have a real option here.
*/
if ((where + 2) > size) {
fr_strerror_printf("Options overflow field at %u",
(unsigned int) (data - (uint8_t *) packet));
return NULL;
}
if ((where + 2 + data[1]) > size) {
fr_strerror_printf("Option length overflows field at %u",
(unsigned int) (data - (uint8_t *) packet));
return NULL;
}
if (data[0] == option) return data;
if (data[0] == 52) { /* overload sname and/or file */
overload = data[3];
}
where += data[1] + 2;
data += data[1] + 2;
}
return NULL;
}
/*
* DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
* defined.
*/
RADIUS_PACKET *fr_dhcp_recv(int sockfd)
{
ssize_t len;
uint32_t magic;
struct sockaddr_storage src;
struct sockaddr_storage dst;
socklen_t sizeof_src;
socklen_t sizeof_dst;
RADIUS_PACKET *packet;
int port;
uint8_t *code;
packet = rad_alloc(0);
if (!packet) {
fr_strerror_printf("Failed allocating packet");
return NULL;
}
memset(packet, 0, sizeof(*packet));
packet->data = malloc(MAX_PACKET_SIZE);
if (!packet->data) {
fr_strerror_printf("Failed in malloc");
rad_free(&packet);
return NULL;
}
packet->sockfd = sockfd;
sizeof_src = sizeof(src);
#ifdef WITH_UDPFROMTO
sizeof_dst = sizeof(dst);
len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
(struct sockaddr *)&src, &sizeof_src,
(struct sockaddr *)&dst, &sizeof_dst);
#else
len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
(struct sockaddr *)&src, &sizeof_src);
#endif
if (len <= 0) {
fr_strerror_printf("Failed reading DHCP socket: %s", strerror(errno));
rad_free(&packet);
return NULL;
}
if (len < MIN_PACKET_SIZE) {
fr_strerror_printf("DHCP packet is too small (%d < %d)",
(int) len, MIN_PACKET_SIZE);
rad_free(&packet);
return NULL;
}
if (packet->data[1] != 1) {
fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
packet->data[1]);
rad_free(&packet);
return NULL;
}
if (packet->data[2] != 6) {
fr_strerror_printf("Ethernet HW length is wrong length %d",
packet->data[2]);
rad_free(&packet);
return NULL;
}
packet->data_len = len;
memcpy(&magic, packet->data + 236, 4);
magic = ntohl(magic);
if (magic != DHCP_OPTION_MAGIC_NUMBER) {
fr_strerror_printf("Cannot do BOOTP");
rad_free(&packet);
return NULL;
}
/*
* Create unique keys for the packet.
*/
memcpy(&magic, packet->data + 4, 4);
packet->id = ntohl(magic);
code = dhcp_get_option((dhcp_packet_t *) packet->data,
packet->data_len, 53);
if (!code) {
fr_strerror_printf("No message-type option was found in the packet");
rad_free(&packet);
return NULL;
}
if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
fr_strerror_printf("Unknown value for message-type option");
rad_free(&packet);
return NULL;
}
packet->code = code[2] | PW_DHCP_OFFSET;
/*
* Create a unique vector from the MAC address and the
* DHCP opcode. This is a hack for the RADIUS
* infrastructure in the rest of the server.
*
* Note: packet->data[2] == 6, which is smaller than
* sizeof(packet->vector)
*
* FIXME: Look for client-identifier in packet,
* and use that, too?
*/
memset(packet->vector, 0, sizeof(packet->vector));
memcpy(packet->vector, packet->data + 28, packet->data[2]);
packet->vector[packet->data[2]] = packet->code & 0xff;
/*
* FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
* FIXME: for OFFER / ACK : src_port = dst_port - 1
*/
/*
* Unique keys are xid, client mac, and client ID?
*/
/*
* FIXME: More checks, like DHCP packet type?
*/
sizeof_dst = sizeof(dst);
#ifndef WITH_UDPFROMTO
/*
* This should never fail...
*/
if (getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst) < 0) {
fr_strerror_printf("getsockname failed: %s", strerror(errno));
rad_free(&packet);
return NULL;
}
#endif
fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
packet->dst_port = port;
fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
packet->src_port = port;
if (fr_debug_flag > 1) {
char type_buf[64];
const char *name = type_buf;
char src_ip_buf[256], dst_ip_buf[256];
if ((packet->code >= PW_DHCP_DISCOVER) &&
(packet->code <= PW_DHCP_INFORM)) {
name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
} else {
snprintf(type_buf, sizeof(type_buf), "%d",
packet->code - PW_DHCP_OFFSET);
}
DEBUG("Received %s of id %08x from %s:%d to %s:%d\n",
name, (unsigned int) packet->id,
inet_ntop(packet->src_ipaddr.af,
&packet->src_ipaddr.ipaddr,
src_ip_buf, sizeof(src_ip_buf)),
packet->src_port,
inet_ntop(packet->dst_ipaddr.af,
&packet->dst_ipaddr.ipaddr,
dst_ip_buf, sizeof(dst_ip_buf)),
packet->dst_port);
}
return packet;
}
/*
* Send a DHCP packet.
*/
int fr_dhcp_send(RADIUS_PACKET *packet)
{
struct sockaddr_storage dst;
socklen_t sizeof_dst;
#ifdef WITH_UDPFROMTO
struct sockaddr_storage src;
socklen_t sizeof_src;
#endif
if (!fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
&dst, &sizeof_dst)) {
return -1;
}
/*
* The client doesn't yet have an IP address, but is
* expecting an ethernet packet unicast to it's MAC
* address. We need to build a raw frame.
*/
if (packet->offset == 0) {
/*
* FIXME: Insert code here!
*/
}
#ifndef WITH_UDPFROMTO
/*
* Assume that the packet is encoded before sending it.
*/
return sendto(packet->sockfd, packet->data, packet->data_len, 0,
(struct sockaddr *)&dst, sizeof_dst);
#else
if (!fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
&src, &sizeof_src)) {
return -1;
}
return sendfromto(packet->sockfd,
packet->data, packet->data_len, 0,
(struct sockaddr *)&src, sizeof_src,
(struct sockaddr *)&dst, sizeof_dst);
#endif
}
static int fr_dhcp_attr2vp(VALUE_PAIR *vp, const uint8_t *p, size_t alen);
static int decode_tlv(VALUE_PAIR *tlv, const uint8_t *data, size_t data_len)
{
const uint8_t *p;
VALUE_PAIR *head, **tail, *vp;
/*
* Take a pass at parsing it.
*/
p = data;
while (p < (data + data_len)) {
if ((p + 2) > (data + data_len)) goto make_tlv;
if ((p + p[1] + 2) > (data + data_len)) goto make_tlv;
p += 2 + p[1];
}
/*
* Got here... must be well formed.
*/
head = NULL;
tail = &head;
p = data;
while (p < (data + data_len)) {
vp = paircreate(tlv->da->attr | (p[0] << 8), DHCP_MAGIC_VENDOR);
if (!vp) {
pairfree(&head);
goto make_tlv;
}
if (fr_dhcp_attr2vp(vp, p + 2, p[1]) < 0) {
pairfree(&head);
goto make_tlv;
}
*tail = vp;
tail = &(vp->next);
p += 2 + p[1];
}
/*
* The caller allocated TLV, so we need to copy the FIRST
* attribute over top of that.
*/
if (head) {
memcpy(tlv, head, sizeof(*tlv));
head->next = NULL;
pairfree(&head);
}
return 0;
make_tlv:
tlv->vp_tlv = malloc(data_len);
if (!tlv->vp_tlv) {
fr_strerror_printf("No memory");
return -1;
}
memcpy(tlv->vp_tlv, data, data_len);
tlv->length = data_len;
return 0;
}
/*
* Decode ONE value into a VP
*/
static int fr_dhcp_attr2vp(VALUE_PAIR *vp, const uint8_t *p, size_t alen)
{
switch (vp->da->type) {
case PW_TYPE_BYTE:
if (alen != 1) goto raw;
vp->vp_integer = p[0];
break;
case PW_TYPE_SHORT:
if (alen != 2) goto raw;
vp->vp_integer = (p[0] << 8) | p[1];
break;
case PW_TYPE_INTEGER:
if (alen != 4) goto raw;
memcpy(&vp->vp_integer, p, 4);
vp->vp_integer = ntohl(vp->vp_integer);
break;
case PW_TYPE_IPADDR:
if (alen != 4) goto raw;
memcpy(&vp->vp_ipaddr, p , 4);
vp->length = 4;
break;
case PW_TYPE_STRING:
if (alen > 253) return -1;
memcpy(vp->vp_strvalue, p , alen);
vp->vp_strvalue[alen] = '\0';
break;
/*
* Value doesn't match up with attribute type, overwrite the
* vp's original DICT_ATTR with an unknown one.
*/
raw:
if (pair2unknown(vp) < 0) return -1;
case PW_TYPE_OCTETS:
if (alen > 253) return -1;
memcpy(vp->vp_octets, p, alen);
break;
case PW_TYPE_TLV:
return decode_tlv(vp, p, alen);
default:
fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
return -1;
} /* switch over type */
vp->length = alen;
return 0;
}
ssize_t fr_dhcp_decode_options(uint8_t *data, size_t len, VALUE_PAIR **head)
{
int i;
VALUE_PAIR *vp, **tail;
uint8_t *p, *next;
next = data;
*head = NULL;
tail = head;
/*
* FIXME: This should also check sname && file fields.
* See the dhcp_get_option() function above.
*/
while (next < (data + len)) {
int num_entries, alen;
const DICT_ATTR *da;
p = next;
if (*p == 0) break;
if (*p == 255) break; /* end of options signifier */
if ((p + 2) > (data + len)) break;
next = p + 2 + p[1];
if (p[1] >= 253) {
fr_strerror_printf("Attribute too long %u %u",
p[0], p[1]);
continue;
}
da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
if (!da) {
fr_strerror_printf("Attribute not in our dictionary: %u",
p[0]);
continue;
}
vp = NULL;
num_entries = 1;
alen = p[1];
p += 2;
/*
* Could be an array of bytes, integers, etc.
*/
if (da->flags.array) {
switch (da->type) {
case PW_TYPE_BYTE:
num_entries = alen;
alen = 1;
break;
case PW_TYPE_SHORT: /* ignore any trailing data */
num_entries = alen >> 1;
alen = 2;
break;
case PW_TYPE_IPADDR:
case PW_TYPE_INTEGER:
case PW_TYPE_DATE: /* ignore any trailing data */
num_entries = alen >> 2;
alen = 4;
break;
default:
break; /* really an internal sanity failure */
}
}
/*
* Loop over all of the entries, building VPs
*/
for (i = 0; i < num_entries; i++) {
vp = pairmake(da->name, NULL, T_OP_ADD);
if (!vp) {
fr_strerror_printf("Cannot build attribute %s",
fr_strerror());
pairfree(head);
return -1;
}
/*
* Hack for ease of use.
*/
if (fr_dhcp_attr2vp(vp, p, alen) < 0) {
pairfree(&vp);
pairfree(head);
return -1;
}
*tail = vp;
while (*tail) {
debug_pair(*tail);
tail = &(*tail)->next;
}
p += alen;
} /* loop over array entries */
} /* loop over the entire packet */
return next - data;
}
int fr_dhcp_decode(RADIUS_PACKET *packet)
{
size_t i;
uint8_t *p;
uint32_t giaddr;
VALUE_PAIR *head, *vp, **tail;
VALUE_PAIR *maxms, *mtu;
head = NULL;
tail = &head;
p = packet->data;
if ((fr_debug_flag > 2) && fr_log_fp) {
for (i = 0; i < packet->data_len; i++) {
if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
fprintf(fr_log_fp, "%02x ", packet->data[i]);
if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
}
fprintf(fr_log_fp, "\n");
}
if (packet->data[1] != 1) {
fr_strerror_printf("Packet is not Ethernet: %u",
packet->data[1]);
return -1;
}
/*
* Decode the header.
*/
for (i = 0; i < 14; i++) {
vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
if (!vp) {
fr_strerror_printf("Parse error %s", fr_strerror());
pairfree(&head);
return -1;
}
if ((i == 11) &&
(packet->data[1] == 1) &&
(packet->data[2] != 6)) {
fr_strerror_printf("chaddr of incorrect length for ethernet");
}
switch (vp->da->type) {
case PW_TYPE_BYTE:
vp->vp_integer = p[0];
vp->length = 1;
break;
case PW_TYPE_SHORT:
vp->vp_integer = (p[0] << 8) | p[1];
vp->length = 2;
break;
case PW_TYPE_INTEGER:
memcpy(&vp->vp_integer, p, 4);
vp->vp_integer = ntohl(vp->vp_integer);
vp->length = 4;
break;
case PW_TYPE_IPADDR:
memcpy(&vp->vp_ipaddr, p, 4);
vp->length = 4;
break;
case PW_TYPE_STRING:
memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
vp->length = strlen(vp->vp_strvalue);
if (vp->length == 0) {
pairfree(&vp);
}
break;
case PW_TYPE_OCTETS:
memcpy(vp->vp_octets, p, packet->data[2]);
vp->length = packet->data[2];
break;
case PW_TYPE_ETHERNET:
memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
vp->length = sizeof(vp->vp_ether);
break;
default:
fr_strerror_printf("BAD TYPE %d", vp->da->type);
pairfree(&vp);
break;
}
p += dhcp_header_sizes[i];
if (!vp) continue;
debug_pair(vp);
*tail = vp;
tail = &vp->next;
}
/*
* Loop over the options.
*/
/*
* Nothing uses tail after this call, if it does in the future
* it'll need to find the new tail...
*/
if (fr_dhcp_decode_options(packet->data + 240, packet->data_len - 240,
tail) < 0) {
return -1;
}
/*
* If DHCP request, set ciaddr to zero.
*/
/*
* Set broadcast flag for broken vendors, but only if
* giaddr isn't set.
*/
memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
if (giaddr == htonl(INADDR_ANY)) {
/*
* DHCP Opcode is request
*/
vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp && vp->vp_integer == 3) {
/*
* Vendor is "MSFT 98"
*/
vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
/*
* Reply should be broadcast.
*/
if (vp) vp->vp_integer |= 0x8000;
packet->data[10] |= 0x80;
}
}
}
/*
* FIXME: Nuke attributes that aren't used in the normal
* header for discover/requests.
*/
packet->vps = head;
/*
* Client can request a LARGER size, but not a smaller
* one. They also cannot request a size larger than MTU.
*/
maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
return -1;
}
if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
maxms->vp_integer = DEFAULT_PACKET_SIZE;
}
if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
maxms->vp_integer = mtu->vp_integer;
}
if (fr_debug_flag > 0) {
for (vp = packet->vps; vp != NULL; vp = vp->next) {
}
}
if (fr_debug_flag) fflush(stdout);
return 0;
}
static int attr_cmp(const void *one, const void *two)
{
const VALUE_PAIR * const *a = one;
const VALUE_PAIR * const *b = two;
/*
* DHCP-Message-Type is first, for simplicity.
*/
if (((*a)->da->attr == 53) &&
(*b)->da->attr != 53) return -1;
/*
* Relay-Agent is last
*/
if (((*a)->da->attr == 82) &&
(*b)->da->attr != 82) return +1;
return ((*a)->da->attr - (*b)->da->attr);
}
static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, size_t room)
{
size_t length;
uint32_t lvalue;
/*
* FIXME: Check room!
*/
room = room; /* -Wunused */
/*
* Search for all attributes of the same
* type, and pack them into the same
* attribute.
*/
switch (vp->da->type) {
case PW_TYPE_BYTE:
length = 1;
*p = vp->vp_integer & 0xff;
break;
case PW_TYPE_SHORT:
length = 2;
p[0] = (vp->vp_integer >> 8) & 0xff;
p[1] = vp->vp_integer & 0xff;
break;
case PW_TYPE_INTEGER:
length = 4;
lvalue = htonl(vp->vp_integer);
memcpy(p, &lvalue, 4);
break;
case PW_TYPE_IPADDR:
length = 4;
memcpy(p, &vp->vp_ipaddr, 4);
break;
case PW_TYPE_ETHERNET:
length = 6;
memcpy(p, &vp->vp_ether, 6);
break;
case PW_TYPE_STRING:
memcpy(p, vp->vp_strvalue, vp->length);
length = vp->length;
break;
case PW_TYPE_TLV: /* FIXME: split it on 255? */
memcpy(p, vp->vp_tlv, vp->length);
length = vp->length;
break;
case PW_TYPE_OCTETS:
memcpy(p, vp->vp_octets, vp->length);
length = vp->length;
break;
default:
fr_strerror_printf("BAD TYPE2 %d", vp->da->type);
length = 0;
break;
}
return length;
}
static VALUE_PAIR *fr_dhcp_vp2suboption(VALUE_PAIR *vps)
{
int length;
unsigned int attribute;
uint8_t *ptr;
VALUE_PAIR *vp, *tlv;
attribute = vps->da->attr & 0xffff00ff;
tlv = paircreate(attribute, DHCP_MAGIC_VENDOR);
if (!tlv) return NULL;
tlv->length = 0;
for (vp = vps; vp != NULL; vp = vp->next) {
/*
* Group the attributes ONLY until we see a
* non-TLV attribute.
*/
if (!vp->da->flags.is_tlv ||
vp->da->flags.extended ||
((vp->da->attr & 0xffff00ff) != attribute)) {
break;
}
tlv->length += vp->length + 2;
}
if (!tlv->length) {
pairfree(&tlv);
return NULL;
}
tlv->vp_tlv = malloc(tlv->length);
if (!tlv->vp_tlv) {
pairfree(&tlv);
return NULL;
}
ptr = tlv->vp_tlv;
for (vp = vps; vp != NULL; vp = vp->next) {
if (!vp->da->flags.is_tlv ||
vp->da->flags.extended ||
((vp->da->attr & 0xffff00ff) != attribute)) {
break;
}
length = fr_dhcp_vp2attr(vp, ptr + 2,
tlv->vp_tlv + tlv->length - ptr);
if (length > 255) {
pairfree(&tlv);
return NULL;
}
/*
* Pack the attribute.
*/
ptr[0] = (vp->da->attr & 0xff00) >> 8;
ptr[1] = length;
ptr += length + 2;
}
return tlv;
}