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, 0);
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 of id %08x from %s:%d to %s:%d\n",
437 "Sending %s of 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(RADIUS_PACKET *packet, VALUE_PAIR *vp, uint8_t const *p, size_t alen);
464 static int decode_tlv(RADIUS_PACKET *packet, VALUE_PAIR *tlv, uint8_t const *data, size_t data_len)
467 VALUE_PAIR *head, *vp;
471 * Take a pass at parsing it.
474 while (p < (data + data_len)) {
475 if ((p + 2) > (data + data_len)) goto make_tlv;
477 if ((p + p[1] + 2) > (data + data_len)) goto make_tlv;
482 * Got here... must be well formed.
485 fr_cursor_init(&cursor, &head);
488 while (p < (data + data_len)) {
489 vp = paircreate(packet, tlv->da->attr | (p[0] << 8), DHCP_MAGIC_VENDOR);
495 if (fr_dhcp_attr2vp(packet, vp, p + 2, p[1]) < 0) {
500 fr_cursor_insert(&cursor, vp);
505 * The caller allocated TLV, so we need to copy the FIRST
506 * attribute over top of that.
508 * This is a pretty awful hack, but we should be able to
509 * clean it up when we get nested VPs so lets leave it for
513 /* Cleanup any old TLV data */
514 talloc_free(tlv->vp_tlv);
517 memcpy(tlv, head, sizeof(*tlv));
519 /* If the VP has a talloced value we need to reparent it to the original TLV attribute */
520 switch (head->da->type) {
524 talloc_steal(tlv, head->data.ptr);
528 tlv->next = head->next;
535 tlv->vp_tlv = talloc_array(tlv, uint8_t, data_len);
537 fr_strerror_printf("No memory");
540 memcpy(tlv->vp_tlv, data, data_len);
541 tlv->length = data_len;
548 * Decode ONE value into a VP
550 static int fr_dhcp_attr2vp(RADIUS_PACKET *packet, VALUE_PAIR *vp, uint8_t const *p, size_t alen)
554 switch (vp->da->type) {
556 if (alen != 1) goto raw;
557 vp->vp_integer = p[0];
561 if (alen != 2) goto raw;
562 memcpy(&vp->vp_integer, p, 2);
563 vp->vp_integer = ntohs(vp->vp_integer);
566 case PW_TYPE_INTEGER:
567 if (alen != 4) goto raw;
568 memcpy(&vp->vp_integer, p, 4);
569 vp->vp_integer = ntohl(vp->vp_integer);
573 if (alen != 4) goto raw;
575 * Keep value in Network Order!
577 memcpy(&vp->vp_ipaddr, p , 4);
582 vp->vp_strvalue = q = talloc_array(vp, char, alen + 1);
588 case PW_TYPE_ETHERNET:
589 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
590 vp->length = sizeof(vp->vp_ether);
594 * Value doesn't match up with attribute type, overwrite the
595 * vp's original DICT_ATTR with an unknown one.
598 if (pair2unknown(vp) < 0) return -1;
601 if (alen > 253) return -1;
602 pairmemcpy(vp, p, alen);
606 return decode_tlv(packet, vp, p, alen);
609 fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
611 } /* switch over type */
617 ssize_t fr_dhcp_decode_options(RADIUS_PACKET *packet,
618 uint8_t const *data, size_t len, VALUE_PAIR **head)
623 uint8_t const *p, *next;
627 fr_cursor_init(&cursor, head);
630 * FIXME: This should also check sname && file fields.
631 * See the dhcp_get_option() function above.
633 while (next < (data + len)) {
634 int num_entries, alen;
640 if (*p == 255) break; /* end of options signifier */
641 if ((p + 2) > (data + len)) break;
646 fr_strerror_printf("Attribute too long %u %u",
651 da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
653 fr_strerror_printf("Attribute not in our dictionary: %u",
664 * Could be an array of bytes, integers, etc.
666 if (da->flags.array) {
673 case PW_TYPE_SHORT: /* ignore any trailing data */
674 num_entries = alen >> 1;
679 case PW_TYPE_INTEGER:
680 case PW_TYPE_DATE: /* ignore any trailing data */
681 num_entries = alen >> 2;
687 break; /* really an internal sanity failure */
692 * Loop over all of the entries, building VPs
694 for (i = 0; i < num_entries; i++) {
695 vp = pairmake(packet, NULL, da->name, NULL, T_OP_ADD);
697 fr_strerror_printf("Cannot build attribute %s",
704 * Hack for ease of use.
706 if ((da->vendor == DHCP_MAGIC_VENDOR) &&
707 (da->attr == 61) && !da->flags.array &&
708 (alen == 7) && (*p == 1) && (num_entries == 1)) {
709 pairmemcpy(vp, p + 1, 6);
711 } else if (fr_dhcp_attr2vp(packet, vp, p, alen) < 0) {
717 fr_cursor_insert(&cursor, vp);
719 for (vp = fr_cursor_current(&cursor);
721 vp = fr_cursor_next(&cursor)) {
725 } /* loop over array entries */
726 } /* loop over the entire packet */
731 int fr_dhcp_decode(RADIUS_PACKET *packet)
737 VALUE_PAIR *head = NULL, *vp;
738 VALUE_PAIR *maxms, *mtu;
740 fr_cursor_init(&cursor, &head);
743 if ((fr_debug_flag > 2) && fr_log_fp) {
744 for (i = 0; i < packet->data_len; i++) {
745 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
746 fprintf(fr_log_fp, "%02x ", packet->data[i]);
747 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
749 fprintf(fr_log_fp, "\n");
752 if (packet->data[1] != 1) {
753 fr_strerror_printf("Packet is not Ethernet: %u",
761 for (i = 0; i < 14; i++) {
764 vp = pairmake(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
767 strlcpy(buffer, fr_strerror(), sizeof(buffer));
768 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
774 * If chaddr does != 6 bytes it's probably not ethernet, and we should store
775 * it as an opaque type (octets).
777 if ((i == 11) && (packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
778 DICT_ATTR const *da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
785 switch (vp->da->type) {
787 vp->vp_integer = p[0];
792 vp->vp_integer = (p[0] << 8) | p[1];
796 case PW_TYPE_INTEGER:
797 memcpy(&vp->vp_integer, p, 4);
798 vp->vp_integer = ntohl(vp->vp_integer);
803 memcpy(&vp->vp_ipaddr, p, 4);
808 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
810 memcpy(q, p, dhcp_header_sizes[i]);
811 q[dhcp_header_sizes[i]] = '\0';
812 vp->length = strlen(vp->vp_strvalue);
813 if (vp->length == 0) {
819 pairmemcpy(vp, p, packet->data[2]);
822 case PW_TYPE_ETHERNET:
823 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
824 vp->length = sizeof(vp->vp_ether);
828 fr_strerror_printf("BAD TYPE %d", vp->da->type);
832 p += dhcp_header_sizes[i];
837 fr_cursor_insert(&cursor, vp);
841 * Loop over the options.
845 * Nothing uses tail after this call, if it does in the future
846 * it'll need to find the new tail...
849 VALUE_PAIR *options = NULL;
851 if (fr_dhcp_decode_options(packet,
852 packet->data + 240, packet->data_len - 240,
858 fr_cursor_insert(&cursor, options);
863 * If DHCP request, set ciaddr to zero.
867 * Set broadcast flag for broken vendors, but only if
870 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
871 if (giaddr == htonl(INADDR_ANY)) {
873 * DHCP Opcode is request
875 vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
876 if (vp && vp->vp_integer == 3) {
878 * Vendor is "MSFT 98"
880 vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
881 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
882 vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
885 * Reply should be broadcast.
887 if (vp) vp->vp_integer |= 0x8000;
888 packet->data[10] |= 0x80;
894 * FIXME: Nuke attributes that aren't used in the normal
895 * header for discover/requests.
900 * Client can request a LARGER size, but not a smaller
901 * one. They also cannot request a size larger than MTU.
903 maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
904 mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
906 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
907 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
911 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
912 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
913 maxms->vp_integer = DEFAULT_PACKET_SIZE;
916 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
917 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
918 maxms->vp_integer = mtu->vp_integer;
921 if (fr_debug_flag) fflush(stdout);
927 static int attr_cmp(void const *one, void const *two)
929 VALUE_PAIR const * const *a = one;
930 VALUE_PAIR const * const *b = two;
933 * DHCP-Message-Type is first, for simplicity.
935 if (((*a)->da->attr == 53) &&
936 (*b)->da->attr != 53) return -1;
939 * Relay-Agent is last
941 if (((*a)->da->attr == 82) &&
942 (*b)->da->attr != 82) return 1;
944 return ((*a)->da->attr - (*b)->da->attr);
950 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, UNUSED size_t room)
956 * Search for all attributes of the same
957 * type, and pack them into the same
960 switch (vp->da->type) {
963 *p = vp->vp_integer & 0xff;
968 p[0] = (vp->vp_integer >> 8) & 0xff;
969 p[1] = vp->vp_integer & 0xff;
972 case PW_TYPE_INTEGER:
974 lvalue = htonl(vp->vp_integer);
975 memcpy(p, &lvalue, 4);
980 memcpy(p, &vp->vp_ipaddr, 4);
983 case PW_TYPE_ETHERNET:
985 memcpy(p, &vp->vp_ether, 6);
989 memcpy(p, vp->vp_strvalue, vp->length);
993 case PW_TYPE_TLV: /* FIXME: split it on 255? */
994 memcpy(p, vp->vp_tlv, vp->length);
999 memcpy(p, vp->vp_octets, vp->length);
1000 length = vp->length;
1004 fr_strerror_printf("BAD TYPE2 %d", vp->da->type);
1012 static VALUE_PAIR *fr_dhcp_vp2suboption(RADIUS_PACKET *packet, VALUE_PAIR *vps)
1015 unsigned int attribute;
1018 VALUE_PAIR *vp, *tlv;
1020 attribute = vps->da->attr & 0xffff00ff;
1022 tlv = paircreate(packet, attribute, DHCP_MAGIC_VENDOR);
1023 if (!tlv) return NULL;
1026 for (vp = fr_cursor_init(&cursor, &vps);
1028 vp = fr_cursor_next(&cursor)) {
1030 * Group the attributes ONLY until we see a
1031 * non-TLV attribute.
1033 if (!vp->da->flags.is_tlv ||
1034 vp->da->flags.extended ||
1035 ((vp->da->attr & 0xffff00ff) != attribute)) {
1039 tlv->length += vp->length + 2;
1047 tlv->vp_tlv = talloc_array(tlv, uint8_t, tlv->length);
1054 for (vp = fr_cursor_init(&cursor, &vps);
1056 vp = fr_cursor_next(&cursor)) {
1057 if (!vp->da->flags.is_tlv ||
1058 vp->da->flags.extended ||
1059 ((vp->da->attr & 0xffff00ff) != attribute)) {
1063 length = fr_dhcp_vp2attr(vp, ptr + 2,
1064 tlv->vp_tlv + tlv->length - ptr);
1071 * Pack the attribute.
1073 ptr[0] = (vp->da->attr & 0xff00) >> 8;
1083 int fr_dhcp_encode(RADIUS_PACKET *packet)
1085 unsigned int i, num_vps;
1090 size_t dhcp_size, length;
1093 # ifdef WITH_UDPFROMTO
1094 char src_ip_buf[256];
1096 char dst_ip_buf[256];
1099 if (packet->data) return 0;
1101 packet->data_len = MAX_PACKET_SIZE;
1102 packet->data = talloc_zero_array(packet, uint8_t, packet->data_len);
1104 /* XXX Ugly ... should be set by the caller */
1105 if (packet->code == 0) packet->code = PW_DHCP_NAK;
1108 if ((packet->code >= PW_DHCP_DISCOVER) &&
1109 (packet->code <= PW_DHCP_INFORM)) {
1110 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1116 # ifdef WITH_UDPFROMTO
1117 "Encoding %s of id %08x from %s:%d to %s:%d\n",
1119 "Encoding %s of id %08x to %s:%d\n",
1121 name, (unsigned int) packet->id,
1122 # ifdef WITH_UDPFROMTO
1123 inet_ntop(packet->src_ipaddr.af,
1124 &packet->src_ipaddr.ipaddr,
1125 src_ip_buf, sizeof(src_ip_buf)),
1128 inet_ntop(packet->dst_ipaddr.af,
1129 &packet->dst_ipaddr.ipaddr,
1130 dst_ip_buf, sizeof(dst_ip_buf)),
1137 * @todo: Make this work again.
1140 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1143 * Clients can request a LARGER size, but not a
1144 * smaller one. They also cannot request a size
1148 /* DHCP-DHCP-Maximum-Msg-Size */
1149 vp = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1150 if (vp && (vp->vp_integer > mms)) {
1151 mms = vp->vp_integer;
1153 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1157 vp = pairfind(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1159 *p++ = vp->vp_integer & 0xff;
1161 *p++ = 1; /* client message */
1164 /* DHCP-Hardware-Type */
1165 if ((vp = pairfind(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1166 *p++ = vp->vp_integer & 0xFF;
1168 *p++ = 1; /* hardware type = ethernet */
1171 /* DHCP-Hardware-Address-Length */
1172 if ((vp = pairfind(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1173 *p++ = vp->vp_integer & 0xFF;
1175 *p++ = 6; /* 6 bytes of ethernet */
1178 /* DHCP-Hop-Count */
1179 if ((vp = pairfind(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1180 *p = vp->vp_integer & 0xff;
1184 /* DHCP-Transaction-Id */
1185 if ((vp = pairfind(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1186 lvalue = htonl(vp->vp_integer);
1190 memcpy(p, &lvalue, 4);
1193 /* DHCP-Number-of-Seconds */
1194 if ((vp = pairfind(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1195 lvalue = htonl(vp->vp_integer);
1196 memcpy(p, &lvalue, 2);
1201 if ((vp = pairfind(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1202 lvalue = htons(vp->vp_integer);
1203 memcpy(p, &lvalue, 2);
1207 /* DHCP-Client-IP-Address */
1208 if ((vp = pairfind(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1209 memcpy(p, &vp->vp_ipaddr, 4);
1213 /* DHCP-Your-IP-address */
1214 if ((vp = pairfind(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1215 lvalue = vp->vp_ipaddr;
1217 lvalue = htonl(INADDR_ANY);
1219 memcpy(p, &lvalue, 4);
1222 /* DHCP-Server-IP-Address */
1223 vp = pairfind(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
1225 /* DHCP-DHCP-Server-Identifier */
1226 if (!vp && (vp = pairfind(packet->vps, 54, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1227 lvalue = vp->vp_ipaddr;
1229 lvalue = htonl(INADDR_ANY);
1231 memcpy(p, &lvalue, 4);
1235 * DHCP-Gateway-IP-Address
1237 if ((vp = pairfind(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1238 lvalue = vp->vp_ipaddr;
1240 lvalue = htonl(INADDR_ANY);
1242 memcpy(p, &lvalue, 4);
1245 /* DHCP-Client-Hardware-Address */
1246 if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1247 if (vp->length == sizeof(vp->vp_ether)) {
1248 memcpy(p, vp->vp_ether, vp->length);
1249 } /* else ignore it */
1251 p += DHCP_CHADDR_LEN;
1253 /* DHCP-Server-Host-Name */
1254 if ((vp = pairfind(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1255 if (vp->length > DHCP_SNAME_LEN) {
1256 memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
1258 memcpy(p, vp->vp_strvalue, vp->length);
1261 p += DHCP_SNAME_LEN;
1264 * Copy over DHCP-Boot-Filename.
1266 * FIXME: This copy should be delayed until AFTER the options
1267 * have been processed. If there are too many options for
1268 * the packet, then they go into the sname && filename fields.
1269 * When that happens, the boot filename is passed as an option,
1270 * instead of being placed verbatim in the filename field.
1273 /* DHCP-Boot-Filename */
1274 if ((vp = pairfind(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1275 if (vp->length > DHCP_FILE_LEN) {
1276 memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
1278 memcpy(p, vp->vp_strvalue, vp->length);
1283 /* DHCP magic number */
1284 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER);
1285 memcpy(p, &lvalue, 4);
1291 if (fr_debug_flag > 1) {
1296 for (i = 0; i < 14; i++) {
1299 vp = pairmake(packet, NULL,
1300 dhcp_header_names[i], NULL, T_OP_EQ);
1303 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1304 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1308 switch (vp->da->type) {
1310 vp->vp_integer = p[0];
1315 vp->vp_integer = (p[0] << 8) | p[1];
1319 case PW_TYPE_INTEGER:
1320 memcpy(&vp->vp_integer, p, 4);
1321 vp->vp_integer = ntohl(vp->vp_integer);
1325 case PW_TYPE_IPADDR:
1326 memcpy(&vp->vp_ipaddr, p, 4);
1330 case PW_TYPE_STRING:
1331 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1333 memcpy(q, p, dhcp_header_sizes[i]);
1334 q[dhcp_header_sizes[i]] = '\0';
1335 vp->length = strlen(vp->vp_strvalue);
1338 case PW_TYPE_OCTETS: /* only for Client HW Address */
1339 pairmemcpy(vp, p, packet->data[2]);
1342 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1343 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1344 vp->length = sizeof(vp->vp_ether);
1348 fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
1353 p += dhcp_header_sizes[i];
1360 * Jump over DHCP magic number, response, etc.
1366 * Before packing the attributes, re-order them so that
1367 * the array ones are all contiguous. This simplifies
1371 for (vp = fr_cursor_init(&cursor, &packet->vps);
1373 vp = fr_cursor_next(&cursor)) {
1379 array = talloc_array(packet, VALUE_PAIR*, num_vps);
1382 for (vp = fr_cursor_init(&cursor, &packet->vps);
1384 vp = fr_cursor_next(&cursor)) {
1389 * Sort the attributes.
1391 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *), attr_cmp);
1394 fr_cursor_init(&cursor, &packet->vps);
1395 for (i = 0; i < num_vps; i++) {
1396 array[i]->next = NULL;
1397 fr_cursor_insert(&cursor, array[i]);
1402 p[0] = 0x35; /* DHCP-Message-Type */
1404 p[2] = packet->code - PW_DHCP_OFFSET;
1408 * Pack in the attributes.
1412 unsigned int num_entries = 1;
1416 if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next;
1417 if (vp->da->attr == 53) goto next; /* already done */
1418 if ((vp->da->attr > 255) &&
1419 (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
1422 if (vp->da->flags.extended) goto next;
1424 for (same = fr_cursor_init(&cursor, &vp->next);
1426 same = fr_cursor_next(&cursor)) {
1427 if (same->da->attr != vp->da->attr) break;
1432 * For client-identifier
1433 * @todo What's this meant to be doing?!
1436 if ((vp->da->type == PW_TYPE_ETHERNET) &&
1437 (vp->length == 6) &&
1438 (num_entries == 1)) {
1439 vp->da->type = PW_TYPE_OCTETS;
1440 memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1441 vp->vp_octets[0] = 1;
1444 *(p++) = vp->da->attr & 0xff;
1446 *(p++) = 0; /* header isn't included in attr length */
1448 for (i = 0; i < num_entries; i++) {
1449 if (i != 0) debug_pair(vp);
1451 if (vp->da->flags.is_tlv) {
1455 * Should NOT have been encoded yet!
1457 tlv = fr_dhcp_vp2suboption(packet, vp);
1460 * Ignore it if there's an issue
1463 if (!tlv) goto next;
1465 tlv->next = vp->next;
1470 length = fr_dhcp_vp2attr(vp, p, 0);
1473 * This will never happen due to FreeRADIUS
1474 * limitations: sizeof(vp->vp_octets) < 255
1477 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->da->name);
1482 * More than one attribute of the same type
1483 * in a row: they are packed together
1484 * into the same TLV. If we overflow,
1487 if ((*plength + length) > 255) {
1488 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->da->name);
1495 if (vp->next && (vp->next->da->attr == vp->da->attr)) {
1498 } /* loop over num_entries */
1504 p[0] = 0xff; /* end of option option */
1507 dhcp_size = p - packet->data;
1510 * FIXME: if (dhcp_size > mms),
1511 * then we put the extra options into the "sname" and "file"
1512 * fields, AND set the "end option option" in the "options"
1513 * field. We also set the "overload option",
1514 * and put options into the "file" field, followed by
1515 * the "sname" field. Where each option is completely
1516 * enclosed in the "file" and/or "sname" field, AND
1517 * followed by the "end of option", and MUST be followed
1518 * by padding option.
1520 * Yuck. That sucks...
1522 packet->data_len = dhcp_size;
1524 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1525 memset(packet->data + packet->data_len, 0,
1526 DEFAULT_PACKET_SIZE - packet->data_len);
1527 packet->data_len = DEFAULT_PACKET_SIZE;
1530 if ((fr_debug_flag > 2) && fr_log_fp) {
1531 fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
1532 for (i = 0; i < packet->data_len; i++) {
1533 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1534 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1535 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1537 fprintf(fr_log_fp, "\n");
1544 int fr_dhcp_add_arp_entry(int fd, char const *interface,
1545 VALUE_PAIR *macaddr, VALUE_PAIR *ip)
1547 struct sockaddr_in *sin;
1551 fr_strerror_printf("No interface specified. Cannot update ARP table");
1555 if (!fr_assert(macaddr) ||
1556 !fr_assert((macaddr->da->type == PW_TYPE_ETHERNET) || (macaddr->da->type == PW_TYPE_OCTETS))) {
1557 fr_strerror_printf("Wrong VP type (%s) for chaddr",
1558 fr_int2str(dict_attr_types, macaddr->da->type, "<invalid>"));
1562 if (macaddr->length > sizeof(req.arp_ha.sa_data)) {
1563 fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
1564 sizeof(req.arp_ha.sa_data), macaddr->length);
1568 memset(&req, 0, sizeof(req));
1569 sin = (struct sockaddr_in *) &req.arp_pa;
1570 sin->sin_family = AF_INET;
1571 sin->sin_addr.s_addr = ip->vp_ipaddr;
1573 strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
1575 if (macaddr->da->type == PW_TYPE_ETHERNET) {
1576 memcpy(&req.arp_ha.sa_data, &macaddr->vp_ether, sizeof(macaddr->vp_ether));
1578 memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->length);
1581 req.arp_flags = ATF_COM;
1582 if (ioctl(fd, SIOCSARP, &req) < 0) {
1583 fr_strerror_printf("Failed to add entry in ARP cache: %s (%d)", fr_syserror(errno), errno);
1590 int fr_dhcp_add_arp_entry(UNUSED int fd, UNUSED char const *interface,
1591 UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
1593 fr_strerror_printf("Adding ARP entry is unsupported on this system");