2 * dhcp.c Functions to send/receive dhcp packets.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2008 The FreeRADIUS server project
21 * Copyright 2008 Alan DeKok <aland@deployingradius.com>
26 #include <freeradius-devel/libradius.h>
27 #include <freeradius-devel/udpfromto.h>
28 #include <freeradius-devel/dhcp.h>
31 #include <sys/ioctl.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
42 #include <net/if_arp.h>
45 #define DHCP_CHADDR_LEN (16)
46 #define DHCP_SNAME_LEN (64)
47 #define DHCP_FILE_LEN (128)
48 #define DHCP_VEND_LEN (308)
49 #define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
51 #ifndef INADDR_BROADCAST
52 #define INADDR_BROADCAST INADDR_NONE
55 /* @todo: this is a hack */
56 # define DEBUG if (fr_debug_flag && fr_log_fp) fr_printf_log
57 # define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
58 vp_print(fr_log_fp, vp); \
62 typedef struct dhcp_packet_t {
68 uint16_t secs; /* 8 */
70 uint32_t ciaddr; /* 12 */
71 uint32_t yiaddr; /* 16 */
72 uint32_t siaddr; /* 20 */
73 uint32_t giaddr; /* 24 */
74 uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
75 uint8_t sname[DHCP_SNAME_LEN]; /* 44 */
76 uint8_t file[DHCP_FILE_LEN]; /* 108 */
77 uint32_t option_format; /* 236 */
78 uint8_t options[DHCP_VEND_LEN];
81 typedef struct dhcp_option_t {
87 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 DISCOVER
88 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 OFFER
89 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 REQUEST
90 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 ACK
92 static char const *dhcp_header_names[] = {
95 "DHCP-Hardware-Address-Length",
97 "DHCP-Transaction-Id",
98 "DHCP-Number-of-Seconds",
100 "DHCP-Client-IP-Address",
101 "DHCP-Your-IP-Address",
102 "DHCP-Server-IP-Address",
103 "DHCP-Gateway-IP-Address",
104 "DHCP-Client-Hardware-Address",
105 "DHCP-Server-Host-Name",
106 "DHCP-Boot-Filename",
111 static char const *dhcp_message_types[] = {
124 static int dhcp_header_sizes[] = {
135 * Some clients silently ignore responses less than 300 bytes.
137 #define MIN_PACKET_SIZE (244)
138 #define DEFAULT_PACKET_SIZE (300)
139 #define MAX_PACKET_SIZE (1500 - 40)
141 #define DHCP_OPTION_FIELD (0)
142 #define DHCP_FILE_FIELD (1)
143 #define DHCP_SNAME_FIELD (2)
145 static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
149 int field = DHCP_OPTION_FIELD;
154 size = packet_size - offsetof(dhcp_packet_t, options);
155 data = &packet->options[where];
157 while (where < size) {
158 if (data[0] == 0) { /* padding */
163 if (data[0] == 255) { /* end of options */
164 if ((field == DHCP_OPTION_FIELD) &&
165 (overload & DHCP_FILE_FIELD)) {
168 size = sizeof(packet->file);
169 field = DHCP_FILE_FIELD;
172 } else if ((field == DHCP_FILE_FIELD) &&
173 (overload & DHCP_SNAME_FIELD)) {
174 data = packet->sname;
176 size = sizeof(packet->sname);
177 field = DHCP_SNAME_FIELD;
185 * We MUST have a real option here.
187 if ((where + 2) > size) {
188 fr_strerror_printf("Options overflow field at %u",
189 (unsigned int) (data - (uint8_t *) packet));
193 if ((where + 2 + data[1]) > size) {
194 fr_strerror_printf("Option length overflows field at %u",
195 (unsigned int) (data - (uint8_t *) packet));
199 if (data[0] == option) return data;
201 if (data[0] == 52) { /* overload sname and/or file */
205 where += data[1] + 2;
213 * DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
216 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
219 struct sockaddr_storage src;
220 struct sockaddr_storage dst;
221 socklen_t sizeof_src;
222 socklen_t sizeof_dst;
223 RADIUS_PACKET *packet;
228 packet = rad_alloc(NULL, false);
230 fr_strerror_printf("Failed allocating packet");
234 packet->data = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
236 fr_strerror_printf("Out of memory");
241 packet->sockfd = sockfd;
242 sizeof_src = sizeof(src);
243 #ifdef WITH_UDPFROMTO
244 sizeof_dst = sizeof(dst);
245 data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
246 (struct sockaddr *)&src, &sizeof_src,
247 (struct sockaddr *)&dst, &sizeof_dst);
249 data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
250 (struct sockaddr *)&src, &sizeof_src);
254 fr_strerror_printf("Failed reading DHCP socket: %s", fr_syserror(errno));
259 packet->data_len = data_len;
260 if (packet->data_len < MIN_PACKET_SIZE) {
261 fr_strerror_printf("DHCP packet is too small (%zu < %d)",
262 packet->data_len, MIN_PACKET_SIZE);
267 if (packet->data_len > MAX_PACKET_SIZE) {
268 fr_strerror_printf("DHCP packet is too large (%zx > %d)",
269 packet->data_len, MAX_PACKET_SIZE);
274 if (packet->data[1] != 1) {
275 fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
281 if (packet->data[2] != 6) {
282 fr_strerror_printf("Ethernet HW length is wrong length %d",
288 memcpy(&magic, packet->data + 236, 4);
289 magic = ntohl(magic);
290 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
291 fr_strerror_printf("Cannot do BOOTP");
297 * Create unique keys for the packet.
299 memcpy(&magic, packet->data + 4, 4);
300 packet->id = ntohl(magic);
302 code = dhcp_get_option((dhcp_packet_t *) packet->data,
303 packet->data_len, 53);
305 fr_strerror_printf("No message-type option was found in the packet");
310 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
311 fr_strerror_printf("Unknown value for message-type option");
316 packet->code = code[2] | PW_DHCP_OFFSET;
319 * Create a unique vector from the MAC address and the
320 * DHCP opcode. This is a hack for the RADIUS
321 * infrastructure in the rest of the server.
323 * Note: packet->data[2] == 6, which is smaller than
324 * sizeof(packet->vector)
326 * FIXME: Look for client-identifier in packet,
329 memset(packet->vector, 0, sizeof(packet->vector));
330 memcpy(packet->vector, packet->data + 28, packet->data[2]);
331 packet->vector[packet->data[2]] = packet->code & 0xff;
334 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
335 * FIXME: for OFFER / ACK : src_port = dst_port - 1
339 * Unique keys are xid, client mac, and client ID?
343 * FIXME: More checks, like DHCP packet type?
346 sizeof_dst = sizeof(dst);
348 #ifndef WITH_UDPFROMTO
350 * This should never fail...
352 if (getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst) < 0) {
353 fr_strerror_printf("getsockname failed: %s", fr_syserror(errno));
359 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
360 packet->dst_port = port;
362 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
363 packet->src_port = port;
365 if (fr_debug_flag > 1) {
367 char const *name = type_buf;
368 char src_ip_buf[256], dst_ip_buf[256];
370 if ((packet->code >= PW_DHCP_DISCOVER) &&
371 (packet->code <= PW_DHCP_INFORM)) {
372 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
374 snprintf(type_buf, sizeof(type_buf), "%d",
375 packet->code - PW_DHCP_OFFSET);
378 DEBUG("Received %s of Id %08x from %s:%d to %s:%d\n",
379 name, (unsigned int) packet->id,
380 inet_ntop(packet->src_ipaddr.af,
381 &packet->src_ipaddr.ipaddr,
382 src_ip_buf, sizeof(src_ip_buf)),
384 inet_ntop(packet->dst_ipaddr.af,
385 &packet->dst_ipaddr.ipaddr,
386 dst_ip_buf, sizeof(dst_ip_buf)),
395 * Send a DHCP packet.
397 int fr_dhcp_send(RADIUS_PACKET *packet)
399 struct sockaddr_storage dst;
400 socklen_t sizeof_dst;
401 #ifdef WITH_UDPFROMTO
402 struct sockaddr_storage src;
403 socklen_t sizeof_src;
405 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
409 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
412 if (packet->data_len == 0) {
413 fr_strerror_printf("No data to send");
417 if (fr_debug_flag > 1) {
419 char const *name = type_buf;
420 #ifdef WITH_UDPFROMTO
421 char src_ip_buf[INET6_ADDRSTRLEN];
423 char dst_ip_buf[INET6_ADDRSTRLEN];
425 if ((packet->code >= PW_DHCP_DISCOVER) &&
426 (packet->code <= PW_DHCP_INFORM)) {
427 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
429 snprintf(type_buf, sizeof(type_buf), "%d",
430 packet->code - PW_DHCP_OFFSET);
434 #ifdef WITH_UDPFROMTO
435 "Sending %s Id %08x from %s:%d to %s:%d\n",
437 "Sending %s Id %08x to %s:%d\n",
439 name, (unsigned int) packet->id,
440 #ifdef WITH_UDPFROMTO
441 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
444 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),
448 #ifndef WITH_UDPFROMTO
450 * Assume that the packet is encoded before sending it.
452 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
453 (struct sockaddr *)&dst, sizeof_dst);
456 return sendfromto(packet->sockfd, packet->data, packet->data_len, 0,
457 (struct sockaddr *)&src, sizeof_src,
458 (struct sockaddr *)&dst, sizeof_dst);
462 static int fr_dhcp_attr2vp(VALUE_PAIR **vp_p, TALLOC_CTX *ctx, uint8_t const *p, size_t alen);
464 /** Returns the number of array members for arrays with fixed element sizes
467 static int fr_dhcp_array_members(size_t *len, DICT_ATTR const *da)
472 * Could be an array of bytes, integers, etc.
474 if (da->flags.array) switch (da->type) {
480 case PW_TYPE_SHORT: /* ignore any trailing data */
481 num_entries = *len >> 1;
485 case PW_TYPE_IPV4_ADDR:
486 case PW_TYPE_INTEGER:
487 case PW_TYPE_DATE: /* ignore any trailing data */
488 num_entries = *len >> 2;
492 case PW_TYPE_IPV6_ADDR:
493 num_entries = *len >> 4;
504 /** RFC 4243 Vendor Specific Suboptions
506 * Vendor specific suboptions are in the format.
509 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
510 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
511 | Enterprise Number 0 |
512 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
514 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
516 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
517 | Enterprise Number n |
518 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
525 * So although the vendor is identified, the format of the data isn't specified
526 * so we can't actually resolve the suboption to an attribute.
528 * To get around that, we create an attribute with a vendor matching the
529 * enterprise number, and attr 0.
531 * How the suboption data is then processed, is dependent on what type
532 * \<iana\>.0 is defined as in the dictionary.
534 * @param[in,out] tlv to decode. *tlv will be set to the head of the list of suboptions and original will be freed.
535 * @param[in] ctx context to alloc new attributes in.
536 * @param[in] data to parse.
537 * @param[in] len length of data to parse.
539 static int fr_dhcp_decode_vsa(VALUE_PAIR **tlv, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
541 uint8_t const *p, *q;
546 if (len < 4) goto malformed;
551 if (p + 5 >= q) goto malformed;
552 p += sizeof(uint32_t);
556 * Check if length > the length of the buffer we have left
558 if (p >= q) goto malformed;
563 fr_cursor_init(&cursor, &head);
566 * Now we know its sane, start decoding!
574 vendor = ntohl(*((uint32_t const *) p));
576 * This is pretty much all we can do. RFC 4243 doesn't specify
577 * an attribute field, so it's up to vendors to figure out how
578 * they want to encode their attributes.
580 da = dict_attrbyvalue(0, vendor);
582 da = dict_attrunknown(0, vendor, true);
588 vp = pairalloc(ctx, da);
595 if (fr_dhcp_attr2vp(&vp, ctx, p + 5, p[4]) < 0) {
600 fr_cursor_merge(&cursor, vp);
602 p += 4 + 1 + p[4]; /* vendor id (4) + len (1) + vsa len (n) */
606 * The caller allocated TLV, if decoding it generated additional
607 * attributes, we now need to free it, and write the HEAD of our
608 * new list of attributes in its place.
611 vp_cursor_t tlv_cursor;
614 * Free the old TLV attribute
619 * Cursor not necessary but means we don't have to set
622 fr_cursor_init(&tlv_cursor, tlv);
623 fr_cursor_merge(&tlv_cursor, head);
629 (*tlv)->vp_tlv = talloc_array(*tlv, uint8_t, len);
630 if (!(*tlv)->vp_tlv) {
631 fr_strerror_printf("No memory");
634 memcpy((*tlv)->vp_tlv, data, len);
635 (*tlv)->length = len;
640 /** Decode DHCP suboptions
642 * @param[in,out] tlv to decode. *tlv will be set to the head of the list of suboptions and original will be freed.
643 * @param[in] ctx context to alloc new attributes in.
644 * @param[in] data to parse.
645 * @param[in] len length of data to parse.
647 static int fr_dhcp_decode_suboption(VALUE_PAIR **tlv, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
649 uint8_t const *p, *q;
650 VALUE_PAIR *head, *vp;
654 * TLV must already point to a VALUE_PAIR.
659 * Take a pass at parsing it.
665 * RFC 3046 is very specific about not allowing termination
666 * with a 255 sub-option. But it's required for decoding
667 * option 43, and vendors will probably screw it up
680 * Check if reading length would take us past the end of the buffer
682 if (++p >= q) goto malformed;
686 * Check if length > the length of the buffer we have left
688 if (p >= q) goto malformed;
693 * Got here... must be well formed.
696 fr_cursor_init(&cursor, &head);
708 * The initial OID string looks like:
711 * If <iana>.0 is type TLV then we attempt to decode its contents as more
712 * DHCP suboptions, which gives us:
715 * If <iana>.0 is not defined in the dictionary or is type octets, we leave
716 * the attribute as is.
718 attr = (*tlv)->da->attr ? ((*tlv)->da->attr | (p[0] << 8)) : p[0];
721 * Use the vendor of the parent TLV which is not necessarily
724 * Note: This does not deal with dictionary numbering clashes. If
725 * the vendor uses different numbers for DHCP suboptions and RADIUS
726 * attributes then it's time to break out %{hex:} and regular
729 da = dict_attrbyvalue(attr, (*tlv)->da->vendor);
731 da = dict_attrunknown(attr, (*tlv)->da->vendor, true);
740 num_entries = fr_dhcp_array_members(&a_len, da);
741 for (i = 0; i < num_entries; i++) {
742 vp = pairalloc(ctx, da);
749 if (fr_dhcp_attr2vp(&vp, ctx, a_p, a_len) < 0) {
753 fr_cursor_merge(&cursor, vp);
757 p += 2 + p[1]; /* code (1) + len (1) + suboption len (n)*/
761 * The caller allocated a TLV, if decoding it generated
762 * additional attributes, we now need to free it, and write
763 * the HEAD of our new list of attributes in its place.
766 vp_cursor_t tlv_cursor;
769 * Free the old TLV attribute
774 * Cursor not necessary but means we don't have to set
777 fr_cursor_init(&tlv_cursor, tlv);
778 fr_cursor_merge(&tlv_cursor, head);
784 (*tlv)->vp_tlv = talloc_array(*tlv, uint8_t, len);
785 if (!(*tlv)->vp_tlv) {
786 fr_strerror_printf("No memory");
789 memcpy((*tlv)->vp_tlv, data, len);
790 (*tlv)->length = len;
796 * Decode ONE value into a VP
798 static int fr_dhcp_attr2vp(VALUE_PAIR **vp_p, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
800 VALUE_PAIR *vp = *vp_p;
803 switch (vp->da->type) {
805 if (len != 1) goto raw;
806 vp->vp_byte = data[0];
810 if (len != 2) goto raw;
811 memcpy(&vp->vp_short, data, 2);
812 vp->vp_short = ntohs(vp->vp_short);
815 case PW_TYPE_INTEGER:
816 if (len != 4) goto raw;
817 memcpy(&vp->vp_integer, data, 4);
818 vp->vp_integer = ntohl(vp->vp_integer);
821 case PW_TYPE_IPV4_ADDR:
822 if (len != 4) goto raw;
824 * Keep value in Network Order!
826 memcpy(&vp->vp_ipaddr, data, 4);
831 * In DHCPv4, string options which can also be arrays,
832 * have their values '\0' delimited.
837 uint8_t const *q, *end;
841 q = end = data + len;
843 if (!vp->da->flags.array) {
844 pairstrncpy(vp, (char const *)p, q - p);
849 * Initialise the cursor as we may be inserting
850 * multiple additional VPs
852 fr_cursor_init(&cursor, vp_p);
854 q = memchr(p, '\0', q - p);
855 /* Malformed but recoverable */
858 pairstrncpy(vp, (char const *)p, q - p);
861 /* Need another VP for the next round */
863 vp = pairalloc(ctx, vp->da);
868 fr_cursor_insert(&cursor, vp);
876 case PW_TYPE_ETHERNET:
877 memcpy(vp->vp_ether, data, sizeof(vp->vp_ether));
878 vp->length = sizeof(vp->vp_ether);
882 * Value doesn't match up with attribute type, overwrite the
883 * vp's original DICT_ATTR with an unknown one.
886 if (pair2unknown(vp) < 0) return -1;
889 if (len > 255) return -1;
890 pairmemcpy(vp, data, len);
894 * For option 82 et al...
897 return fr_dhcp_decode_suboption(vp_p, ctx, data, len);
903 return fr_dhcp_decode_vsa(vp_p, ctx, data, len);
906 fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
908 } /* switch over type */
914 /** Decode DHCP options
916 * @param[in,out] out Where to write the decoded options.
917 * @param[in] ctx context to alloc new attributes in.
918 * @param[in] data to parse.
919 * @param[in] len of data to parse.
921 ssize_t fr_dhcp_decode_options(VALUE_PAIR **out, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
925 uint8_t const *p, *q;
928 fr_cursor_init(&cursor, out);
931 * FIXME: This should also check sname && file fields.
932 * See the dhcp_get_option() function above.
943 if (*p == 0) { /* 0x00 - Padding option */
948 if (*p == 255) { /* 0xff - End of options signifier */
952 if ((p + 2) > q) break;
958 * Unknown attribute, create an octets type
959 * attribute with the contents of the sub-option.
961 da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
963 da = dict_attrunknown(p[0], DHCP_MAGIC_VENDOR, true);
968 vp = pairalloc(ctx, da);
973 pairmemcpy(vp, a_p, a_len);
974 fr_cursor_insert(&cursor, vp);
980 * Array type sub-option create a new VALUE_PAIR
981 * for each array element.
983 num_entries = fr_dhcp_array_members(&a_len, da);
984 for (i = 0; i < num_entries; i++) {
985 vp = pairalloc(ctx, da);
992 if (fr_dhcp_attr2vp(&vp, ctx, a_p, a_len) < 0) {
997 fr_cursor_insert(&cursor, vp);
999 for (vp = fr_cursor_current(&cursor);
1001 vp = fr_cursor_next(&cursor)) {
1005 } /* loop over array entries */
1007 p += 2 + p[1]; /* code (1) + len (1) + option len (n)*/
1008 } /* loop over the entire packet */
1013 int fr_dhcp_decode(RADIUS_PACKET *packet)
1019 VALUE_PAIR *head = NULL, *vp;
1020 VALUE_PAIR *maxms, *mtu;
1022 fr_cursor_init(&cursor, &head);
1025 if ((fr_debug_flag > 2) && fr_log_fp) {
1026 for (i = 0; i < packet->data_len; i++) {
1027 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
1028 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1029 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1031 fprintf(fr_log_fp, "\n");
1034 if (packet->data[1] != 1) {
1035 fr_strerror_printf("Packet is not Ethernet: %u",
1041 * Decode the header.
1043 for (i = 0; i < 14; i++) {
1046 vp = pairmake(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
1049 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1050 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1056 * If chaddr does != 6 bytes it's probably not ethernet, and we should store
1057 * it as an opaque type (octets).
1059 if ((i == 11) && (packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
1060 DICT_ATTR const *da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
1067 switch (vp->da->type) {
1074 vp->vp_short = (p[0] << 8) | p[1];
1078 case PW_TYPE_INTEGER:
1079 memcpy(&vp->vp_integer, p, 4);
1080 vp->vp_integer = ntohl(vp->vp_integer);
1084 case PW_TYPE_IPV4_ADDR:
1085 memcpy(&vp->vp_ipaddr, p, 4);
1089 case PW_TYPE_STRING:
1090 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1092 memcpy(q, p, dhcp_header_sizes[i]);
1093 q[dhcp_header_sizes[i]] = '\0';
1094 vp->length = strlen(vp->vp_strvalue);
1095 if (vp->length == 0) {
1100 case PW_TYPE_OCTETS:
1101 pairmemcpy(vp, p, packet->data[2]);
1104 case PW_TYPE_ETHERNET:
1105 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1106 vp->length = sizeof(vp->vp_ether);
1110 fr_strerror_printf("BAD TYPE %d", vp->da->type);
1114 p += dhcp_header_sizes[i];
1119 fr_cursor_insert(&cursor, vp);
1123 * Loop over the options.
1127 * Nothing uses tail after this call, if it does in the future
1128 * it'll need to find the new tail...
1131 VALUE_PAIR *options = NULL;
1133 if (fr_dhcp_decode_options(&options, packet, packet->data + 240, packet->data_len - 240) < 0) {
1137 if (options) fr_cursor_merge(&cursor, options);
1141 * If DHCP request, set ciaddr to zero.
1145 * Set broadcast flag for broken vendors, but only if
1148 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
1149 if (giaddr == htonl(INADDR_ANY)) {
1151 * DHCP Opcode is request
1153 vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1154 if (vp && vp->vp_integer == 3) {
1156 * Vendor is "MSFT 98"
1158 vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
1159 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
1160 vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
1163 * Reply should be broadcast.
1165 if (vp) vp->vp_integer |= 0x8000;
1166 packet->data[10] |= 0x80;
1172 * FIXME: Nuke attributes that aren't used in the normal
1173 * header for discover/requests.
1178 * Client can request a LARGER size, but not a smaller
1179 * one. They also cannot request a size larger than MTU.
1181 maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1182 mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
1184 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
1185 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification");
1189 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
1190 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
1191 maxms->vp_integer = DEFAULT_PACKET_SIZE;
1194 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
1195 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
1196 maxms->vp_integer = mtu->vp_integer;
1199 if (fr_debug_flag) fflush(stdout);
1205 int8_t fr_dhcp_attr_cmp(void const *a, void const *b)
1207 VALUE_PAIR const *my_a = a;
1208 VALUE_PAIR const *my_b = b;
1214 * DHCP-Message-Type is first, for simplicity.
1216 if ((my_a->da->attr == 53) && (my_b->da->attr != 53)) return -1;
1219 * Relay-Agent is last
1221 if ((my_a->da->attr == 82) && (my_b->da->attr != 82)) return 1;
1223 if (my_a->da->attr < my_b->da->attr) return -1;
1224 if (my_a->da->attr > my_b->da->attr) return 1;
1229 /** Write DHCP option value into buffer
1231 * Does not include DHCP option length or number.
1233 * @param out where to write the DHCP option.
1234 * @param outlen length of output buffer.
1235 * @param vp option to encode.
1236 * @return the length of data writen, -1 if out of buffer, -2 if unsupported type.
1238 static ssize_t fr_dhcp_vp2attr(uint8_t *out, size_t outlen, VALUE_PAIR *vp)
1243 if (outlen < vp->length) {
1247 switch (vp->da->type) {
1253 p[0] = (vp->vp_short >> 8) & 0xff;
1254 p[1] = vp->vp_short & 0xff;
1257 case PW_TYPE_INTEGER:
1258 lvalue = htonl(vp->vp_integer);
1259 memcpy(p, &lvalue, 4);
1262 case PW_TYPE_IPV4_ADDR:
1263 memcpy(p, &vp->vp_ipaddr, 4);
1266 case PW_TYPE_ETHERNET:
1267 memcpy(p, &vp->vp_ether, 6);
1270 case PW_TYPE_STRING:
1271 memcpy(p, vp->vp_strvalue, vp->length);
1274 case PW_TYPE_TLV: /* FIXME: split it on 255? */
1275 memcpy(p, vp->vp_tlv, vp->length);
1278 case PW_TYPE_OCTETS:
1279 memcpy(p, vp->vp_octets, vp->length);
1283 fr_strerror_printf("Unsupported option type %d", vp->da->type);
1290 /** Create a new TLV attribute from multiple sub options
1292 * @param[in,out] ctx to allocate new attribute in.
1293 * @param[in,out] cursor should be set to the start of the list of TLV attributes.
1294 * Will be advanced to the first non-TLV attribute.
1295 * @return attribute holding the concatenation of the values of the sub options.
1297 static VALUE_PAIR *fr_dhcp_vp2suboption(TALLOC_CTX *ctx, vp_cursor_t *cursor)
1300 unsigned int parent; /* Parent attribute of suboption */
1302 uint8_t *p, *opt_len = NULL;
1303 vp_cursor_t to_pack;
1304 VALUE_PAIR *vp, *tlv;
1306 #define SUBOPTION_PARENT(_x) (_x & 0xffff00ff)
1307 #define SUBOPTION_ATTR(_x) ((_x & 0xff00) >> 8)
1309 vp = fr_cursor_current(cursor);
1310 if (!vp) return NULL;
1312 parent = SUBOPTION_PARENT(vp->da->attr);
1313 tlv = paircreate(ctx, parent, DHCP_MAGIC_VENDOR);
1314 if (!tlv) return NULL;
1316 fr_cursor_copy(&to_pack, cursor);
1319 * Loop over TLVs to determine how much memory we need to allocate
1321 * We advanced the cursor we were passed, so if we fail encoding,
1322 * the cursor is at the right position for the next potentially
1325 for (vp = fr_cursor_current(cursor);
1326 vp && vp->da->flags.is_tlv && !vp->da->flags.extended && (SUBOPTION_PARENT(vp->da->attr) == parent);
1327 vp = fr_cursor_next(cursor)) {
1329 * If it's not an array type or is an array type, but is not the same
1330 * as the previous attribute, we add 2 for the additional sub-option
1333 if (!vp->da->flags.array || (SUBOPTION_ATTR(vp->da->attr) != attr)) {
1334 attr = SUBOPTION_ATTR(vp->da->attr);
1337 tlv->length += vp->length;
1340 tlv->vp_tlv = talloc_zero_array(tlv, uint8_t, tlv->length);
1348 for (vp = fr_cursor_current(&to_pack);
1349 vp && vp->da->flags.is_tlv && !vp->da->flags.extended && (SUBOPTION_PARENT(vp->da->attr) == parent);
1350 vp = fr_cursor_next(&to_pack)) {
1351 if (SUBOPTION_ATTR(vp->da->attr) == 0) {
1352 fr_strerror_printf("Invalid attribute number 0");
1356 /* Don't write out the header, were packing array options */
1357 if (!vp->da->flags.array || (attr != SUBOPTION_ATTR(vp->da->attr))) {
1358 attr = SUBOPTION_ATTR(vp->da->attr);
1363 length = fr_dhcp_vp2attr(p, (tlv->vp_tlv + tlv->length) - p, vp);
1364 if ((length < 0) || (length > 255)) {
1377 /** Encode a DHCP option and any sub-options.
1379 * @param out Where to write encoded DHCP attributes.
1380 * @param outlen Length of out buffer.
1381 * @param ctx to use for any allocated memory.
1382 * @param cursor with current VP set to the option to be encoded. Will be advanced to the next option to encode.
1383 * @return > 0 length of data written, < 0 error, 0 not valid option (skipping).
1385 ssize_t fr_dhcp_encode_option(uint8_t *out, size_t outlen, TALLOC_CTX *ctx, vp_cursor_t *cursor)
1388 DICT_ATTR const *previous;
1389 uint8_t *opt_len, *p = out;
1390 size_t freespace = outlen;
1393 vp = fr_cursor_current(cursor);
1396 if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next; /* not a DHCP option */
1397 if (vp->da->attr == 53) goto next; /* already done */
1398 if ((vp->da->attr > 255) && (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
1400 if (vp->da->flags.extended) {
1402 fr_strerror_printf("Attribute \"%s\" is not a DHCP option", vp->da->name);
1403 fr_cursor_next(cursor);
1407 /* Write out the option number */
1408 *(p++) = vp->da->attr & 0xff;
1410 /* Pointer to the length field of the option */
1413 /* Zero out the option's length field */
1416 /* We just consumed two bytes for the header */
1419 /* DHCP options with the same number get coalesced into a single option */
1421 VALUE_PAIR *tlv = NULL;
1424 if (vp->da->flags.is_tlv) {
1426 * Coalesce TLVs into one sub-option.
1427 * Cursor will be advanced to next non-TLV attribute.
1429 tlv = vp = fr_dhcp_vp2suboption(ctx, cursor);
1432 * Skip if there's an issue coalescing the sub-options.
1433 * Cursor will still have been advanced to next non-TLV attribute.
1437 * If not calling fr_dhcp_vp2suboption() advance the cursor, so fr_cursor_current()
1438 * returns the next attribute.
1441 fr_cursor_next(cursor);
1444 if ((*opt_len + vp->length) > 255) {
1445 fr_strerror_printf("Skipping \"%s\": Option splitting not supported "
1446 "(option > 255 bytes)", vp->da->name);
1451 len = fr_dhcp_vp2attr(p, freespace, vp);
1454 /* Failed encoding option */
1463 } while ((vp = fr_cursor_current(cursor)) && (previous == vp->da) && vp->da->flags.array);
1468 int fr_dhcp_encode(RADIUS_PACKET *packet)
1479 # ifdef WITH_UDPFROMTO
1480 char src_ip_buf[256];
1482 char dst_ip_buf[256];
1485 if (packet->data) return 0;
1487 packet->data_len = MAX_PACKET_SIZE;
1488 packet->data = talloc_zero_array(packet, uint8_t, packet->data_len);
1490 /* XXX Ugly ... should be set by the caller */
1491 if (packet->code == 0) packet->code = PW_DHCP_NAK;
1494 if ((vp = pairfind(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1495 packet->id = vp->vp_integer;
1497 packet->id = fr_rand();
1501 if ((packet->code >= PW_DHCP_DISCOVER) &&
1502 (packet->code <= PW_DHCP_INFORM)) {
1503 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1509 # ifdef WITH_UDPFROMTO
1510 "Encoding %s of id %08x from %s:%d to %s:%d\n",
1512 "Encoding %s of id %08x to %s:%d\n",
1514 name, (unsigned int) packet->id,
1515 # ifdef WITH_UDPFROMTO
1516 inet_ntop(packet->src_ipaddr.af,
1517 &packet->src_ipaddr.ipaddr,
1518 src_ip_buf, sizeof(src_ip_buf)),
1521 inet_ntop(packet->dst_ipaddr.af,
1522 &packet->dst_ipaddr.ipaddr,
1523 dst_ip_buf, sizeof(dst_ip_buf)),
1530 * @todo: Make this work again.
1533 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1536 * Clients can request a LARGER size, but not a
1537 * smaller one. They also cannot request a size
1541 /* DHCP-DHCP-Maximum-Msg-Size */
1542 vp = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1543 if (vp && (vp->vp_integer > mms)) {
1544 mms = vp->vp_integer;
1546 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1550 vp = pairfind(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1552 *p++ = vp->vp_integer & 0xff;
1554 *p++ = 1; /* client message */
1557 /* DHCP-Hardware-Type */
1558 if ((vp = pairfind(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1559 *p++ = vp->vp_integer & 0xFF;
1561 *p++ = 1; /* hardware type = ethernet */
1564 /* DHCP-Hardware-Address-Length */
1565 if ((vp = pairfind(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1566 *p++ = vp->vp_integer & 0xFF;
1568 *p++ = 6; /* 6 bytes of ethernet */
1571 /* DHCP-Hop-Count */
1572 if ((vp = pairfind(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1573 *p = vp->vp_integer & 0xff;
1577 /* DHCP-Transaction-Id */
1578 lvalue = htonl(packet->id);
1579 memcpy(p, &lvalue, 4);
1582 /* DHCP-Number-of-Seconds */
1583 if ((vp = pairfind(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1584 lvalue = htonl(vp->vp_integer);
1585 memcpy(p, &lvalue, 2);
1590 if ((vp = pairfind(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1591 lvalue = htons(vp->vp_integer);
1592 memcpy(p, &lvalue, 2);
1596 /* DHCP-Client-IP-Address */
1597 if ((vp = pairfind(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1598 memcpy(p, &vp->vp_ipaddr, 4);
1602 /* DHCP-Your-IP-address */
1603 if ((vp = pairfind(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1604 lvalue = vp->vp_ipaddr;
1606 lvalue = htonl(INADDR_ANY);
1608 memcpy(p, &lvalue, 4);
1611 /* DHCP-Server-IP-Address */
1612 vp = pairfind(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
1614 /* DHCP-DHCP-Server-Identifier */
1615 if (!vp && (vp = pairfind(packet->vps, 54, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1616 lvalue = vp->vp_ipaddr;
1618 lvalue = htonl(INADDR_ANY);
1620 memcpy(p, &lvalue, 4);
1624 * DHCP-Gateway-IP-Address
1626 if ((vp = pairfind(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1627 lvalue = vp->vp_ipaddr;
1629 lvalue = htonl(INADDR_ANY);
1631 memcpy(p, &lvalue, 4);
1634 /* DHCP-Client-Hardware-Address */
1635 if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1636 if (vp->length == sizeof(vp->vp_ether)) {
1637 memcpy(p, vp->vp_ether, vp->length);
1638 } /* else ignore it */
1640 p += DHCP_CHADDR_LEN;
1642 /* DHCP-Server-Host-Name */
1643 if ((vp = pairfind(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1644 if (vp->length > DHCP_SNAME_LEN) {
1645 memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
1647 memcpy(p, vp->vp_strvalue, vp->length);
1650 p += DHCP_SNAME_LEN;
1653 * Copy over DHCP-Boot-Filename.
1655 * FIXME: This copy should be delayed until AFTER the options
1656 * have been processed. If there are too many options for
1657 * the packet, then they go into the sname && filename fields.
1658 * When that happens, the boot filename is passed as an option,
1659 * instead of being placed verbatim in the filename field.
1662 /* DHCP-Boot-Filename */
1663 vp = pairfind(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY);
1665 if (vp->length > DHCP_FILE_LEN) {
1666 memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
1668 memcpy(p, vp->vp_strvalue, vp->length);
1673 /* DHCP magic number */
1674 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER);
1675 memcpy(p, &lvalue, 4);
1681 if (fr_debug_flag > 1) {
1686 for (i = 0; i < 14; i++) {
1689 vp = pairmake(packet, NULL,
1690 dhcp_header_names[i], NULL, T_OP_EQ);
1693 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1694 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1698 switch (vp->da->type) {
1704 vp->vp_short = (p[0] << 8) | p[1];
1707 case PW_TYPE_INTEGER:
1708 memcpy(&vp->vp_integer, p, 4);
1709 vp->vp_integer = ntohl(vp->vp_integer);
1712 case PW_TYPE_IPV4_ADDR:
1713 memcpy(&vp->vp_ipaddr, p, 4);
1716 case PW_TYPE_STRING:
1717 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1719 memcpy(q, p, dhcp_header_sizes[i]);
1720 q[dhcp_header_sizes[i]] = '\0';
1721 vp->length = strlen(vp->vp_strvalue);
1724 case PW_TYPE_OCTETS: /* only for Client HW Address */
1725 pairmemcpy(vp, p, packet->data[2]);
1728 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1729 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1733 fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
1738 p += dhcp_header_sizes[i];
1745 * Jump over DHCP magic number, response, etc.
1750 p[0] = 0x35; /* DHCP-Message-Type */
1752 p[2] = packet->code - PW_DHCP_OFFSET;
1757 * Pre-sort attributes into contiguous blocks so that fr_dhcp_encode_option
1758 * operates correctly. This changes the order of the list, but never mind...
1760 pairsort(&packet->vps, fr_dhcp_attr_cmp);
1761 fr_cursor_init(&cursor, &packet->vps);
1764 * Each call to fr_dhcp_encode_option will encode one complete DHCP option,
1767 while ((vp = fr_cursor_current(&cursor))) {
1768 len = fr_dhcp_encode_option(p, packet->data_len - (p - packet->data), packet, &cursor);
1770 if (len > 0) debug_pair(vp);
1774 p[0] = 0xff; /* end of option option */
1777 dhcp_size = p - packet->data;
1780 * FIXME: if (dhcp_size > mms),
1781 * then we put the extra options into the "sname" and "file"
1782 * fields, AND set the "end option option" in the "options"
1783 * field. We also set the "overload option",
1784 * and put options into the "file" field, followed by
1785 * the "sname" field. Where each option is completely
1786 * enclosed in the "file" and/or "sname" field, AND
1787 * followed by the "end of option", and MUST be followed
1788 * by padding option.
1790 * Yuck. That sucks...
1792 packet->data_len = dhcp_size;
1794 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1795 memset(packet->data + packet->data_len, 0,
1796 DEFAULT_PACKET_SIZE - packet->data_len);
1797 packet->data_len = DEFAULT_PACKET_SIZE;
1800 if ((fr_debug_flag > 2) && fr_log_fp) {
1801 fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
1802 for (i = 0; i < packet->data_len; i++) {
1803 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1804 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1805 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1807 fprintf(fr_log_fp, "\n");
1814 int fr_dhcp_add_arp_entry(int fd, char const *interface,
1815 VALUE_PAIR *macaddr, VALUE_PAIR *ip)
1817 struct sockaddr_in *sin;
1821 fr_strerror_printf("No interface specified. Cannot update ARP table");
1825 if (!fr_assert(macaddr) ||
1826 !fr_assert((macaddr->da->type == PW_TYPE_ETHERNET) || (macaddr->da->type == PW_TYPE_OCTETS))) {
1827 fr_strerror_printf("Wrong VP type (%s) for chaddr",
1828 fr_int2str(dict_attr_types, macaddr->da->type, "<invalid>"));
1832 if (macaddr->length > sizeof(req.arp_ha.sa_data)) {
1833 fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
1834 sizeof(req.arp_ha.sa_data), macaddr->length);
1838 memset(&req, 0, sizeof(req));
1839 sin = (struct sockaddr_in *) &req.arp_pa;
1840 sin->sin_family = AF_INET;
1841 sin->sin_addr.s_addr = ip->vp_ipaddr;
1843 strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
1845 if (macaddr->da->type == PW_TYPE_ETHERNET) {
1846 memcpy(&req.arp_ha.sa_data, &macaddr->vp_ether, sizeof(macaddr->vp_ether));
1848 memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->length);
1851 req.arp_flags = ATF_COM;
1852 if (ioctl(fd, SIOCSARP, &req) < 0) {
1853 fr_strerror_printf("Failed to add entry in ARP cache: %s (%d)", fr_syserror(errno), errno);
1860 int fr_dhcp_add_arp_entry(UNUSED int fd, UNUSED char const *interface,
1861 UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
1863 fr_strerror_printf("Adding ARP entry is unsupported on this system");