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 void fr_strerror_printf(char const *fmt, ...);
58 # define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
59 vp_print(fr_log_fp, vp); \
63 typedef struct dhcp_packet_t {
69 uint16_t secs; /* 8 */
71 uint32_t ciaddr; /* 12 */
72 uint32_t yiaddr; /* 16 */
73 uint32_t siaddr; /* 20 */
74 uint32_t giaddr; /* 24 */
75 uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
76 uint8_t sname[DHCP_SNAME_LEN]; /* 44 */
77 uint8_t file[DHCP_FILE_LEN]; /* 108 */
78 uint32_t option_format; /* 236 */
79 uint8_t options[DHCP_VEND_LEN];
82 typedef struct dhcp_option_t {
88 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 DISCOVER
89 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 OFFER
90 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 REQUEST
91 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 ACK
93 static char const *dhcp_header_names[] = {
96 "DHCP-Hardware-Address-Length",
98 "DHCP-Transaction-Id",
99 "DHCP-Number-of-Seconds",
101 "DHCP-Client-IP-Address",
102 "DHCP-Your-IP-Address",
103 "DHCP-Server-IP-Address",
104 "DHCP-Gateway-IP-Address",
105 "DHCP-Client-Hardware-Address",
106 "DHCP-Server-Host-Name",
107 "DHCP-Boot-Filename",
112 static char const *dhcp_message_types[] = {
125 static int dhcp_header_sizes[] = {
136 * Some clients silently ignore responses less than 300 bytes.
138 #define MIN_PACKET_SIZE (244)
139 #define DEFAULT_PACKET_SIZE (300)
140 #define MAX_PACKET_SIZE (1500 - 40)
142 #define DHCP_OPTION_FIELD (0)
143 #define DHCP_FILE_FIELD (1)
144 #define DHCP_SNAME_FIELD (2)
146 static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
150 int field = DHCP_OPTION_FIELD;
155 size = packet_size - offsetof(dhcp_packet_t, options);
156 data = &packet->options[where];
158 while (where < size) {
159 if (data[0] == 0) { /* padding */
164 if (data[0] == 255) { /* end of options */
165 if ((field == DHCP_OPTION_FIELD) &&
166 (overload & DHCP_FILE_FIELD)) {
169 size = sizeof(packet->file);
170 field = DHCP_FILE_FIELD;
173 } else if ((field == DHCP_FILE_FIELD) &&
174 (overload & DHCP_SNAME_FIELD)) {
175 data = packet->sname;
177 size = sizeof(packet->sname);
178 field = DHCP_SNAME_FIELD;
186 * We MUST have a real option here.
188 if ((where + 2) > size) {
189 fr_strerror_printf("Options overflow field at %u",
190 (unsigned int) (data - (uint8_t *) packet));
194 if ((where + 2 + data[1]) > size) {
195 fr_strerror_printf("Option length overflows field at %u",
196 (unsigned int) (data - (uint8_t *) packet));
200 if (data[0] == option) return data;
202 if (data[0] == 52) { /* overload sname and/or file */
206 where += data[1] + 2;
214 * DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
217 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
220 struct sockaddr_storage src;
221 struct sockaddr_storage dst;
222 socklen_t sizeof_src;
223 socklen_t sizeof_dst;
224 RADIUS_PACKET *packet;
229 packet = rad_alloc(NULL, 0);
231 fr_strerror_printf("Failed allocating packet");
235 packet->data = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
237 fr_strerror_printf("Out of memory");
242 packet->sockfd = sockfd;
243 sizeof_src = sizeof(src);
244 #ifdef WITH_UDPFROMTO
245 sizeof_dst = sizeof(dst);
246 data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
247 (struct sockaddr *)&src, &sizeof_src,
248 (struct sockaddr *)&dst, &sizeof_dst);
250 data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
251 (struct sockaddr *)&src, &sizeof_src);
255 fr_strerror_printf("Failed reading DHCP socket: %s", fr_syserror(errno));
260 packet->data_len = data_len;
261 if (packet->data_len < MIN_PACKET_SIZE) {
262 fr_strerror_printf("DHCP packet is too small (%zu < %d)",
263 packet->data_len, MIN_PACKET_SIZE);
268 if (packet->data_len > MAX_PACKET_SIZE) {
269 fr_strerror_printf("DHCP packet is too large (%zx > %d)",
270 packet->data_len, MAX_PACKET_SIZE);
275 if (packet->data[1] != 1) {
276 fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
282 if (packet->data[2] != 6) {
283 fr_strerror_printf("Ethernet HW length is wrong length %d",
289 memcpy(&magic, packet->data + 236, 4);
290 magic = ntohl(magic);
291 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
292 fr_strerror_printf("Cannot do BOOTP");
298 * Create unique keys for the packet.
300 memcpy(&magic, packet->data + 4, 4);
301 packet->id = ntohl(magic);
303 code = dhcp_get_option((dhcp_packet_t *) packet->data,
304 packet->data_len, 53);
306 fr_strerror_printf("No message-type option was found in the packet");
311 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
312 fr_strerror_printf("Unknown value for message-type option");
317 packet->code = code[2] | PW_DHCP_OFFSET;
320 * Create a unique vector from the MAC address and the
321 * DHCP opcode. This is a hack for the RADIUS
322 * infrastructure in the rest of the server.
324 * Note: packet->data[2] == 6, which is smaller than
325 * sizeof(packet->vector)
327 * FIXME: Look for client-identifier in packet,
330 memset(packet->vector, 0, sizeof(packet->vector));
331 memcpy(packet->vector, packet->data + 28, packet->data[2]);
332 packet->vector[packet->data[2]] = packet->code & 0xff;
335 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
336 * FIXME: for OFFER / ACK : src_port = dst_port - 1
340 * Unique keys are xid, client mac, and client ID?
344 * FIXME: More checks, like DHCP packet type?
347 sizeof_dst = sizeof(dst);
349 #ifndef WITH_UDPFROMTO
351 * This should never fail...
353 if (getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst) < 0) {
354 fr_strerror_printf("getsockname failed: %s", fr_syserror(errno));
360 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
361 packet->dst_port = port;
363 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
364 packet->src_port = port;
366 if (fr_debug_flag > 1) {
368 char const *name = type_buf;
369 char src_ip_buf[256], dst_ip_buf[256];
371 if ((packet->code >= PW_DHCP_DISCOVER) &&
372 (packet->code <= PW_DHCP_INFORM)) {
373 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
375 snprintf(type_buf, sizeof(type_buf), "%d",
376 packet->code - PW_DHCP_OFFSET);
379 DEBUG("Received %s of id %08x from %s:%d to %s:%d\n",
380 name, (unsigned int) packet->id,
381 inet_ntop(packet->src_ipaddr.af,
382 &packet->src_ipaddr.ipaddr,
383 src_ip_buf, sizeof(src_ip_buf)),
385 inet_ntop(packet->dst_ipaddr.af,
386 &packet->dst_ipaddr.ipaddr,
387 dst_ip_buf, sizeof(dst_ip_buf)),
396 * Send a DHCP packet.
398 int fr_dhcp_send(RADIUS_PACKET *packet)
400 struct sockaddr_storage dst;
401 socklen_t sizeof_dst;
402 #ifdef WITH_UDPFROMTO
403 struct sockaddr_storage src;
404 socklen_t sizeof_src;
406 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
410 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
413 if (packet->data_len == 0) {
414 fr_strerror_printf("No data to send");
418 if (fr_debug_flag > 1) {
420 char const *name = type_buf;
421 #ifdef WITH_UDPFROMTO
422 char src_ip_buf[INET6_ADDRSTRLEN];
424 char dst_ip_buf[INET6_ADDRSTRLEN];
426 if ((packet->code >= PW_DHCP_DISCOVER) &&
427 (packet->code <= PW_DHCP_INFORM)) {
428 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
430 snprintf(type_buf, sizeof(type_buf), "%d",
431 packet->code - PW_DHCP_OFFSET);
435 #ifdef WITH_UDPFROMTO
436 "Sending %s of id %08x from %s:%d to %s:%d\n",
438 "Sending %s of id %08x to %s:%d\n",
440 name, (unsigned int) packet->id,
441 #ifdef WITH_UDPFROMTO
442 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
445 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),
449 #ifndef WITH_UDPFROMTO
451 * Assume that the packet is encoded before sending it.
453 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
454 (struct sockaddr *)&dst, sizeof_dst);
457 return sendfromto(packet->sockfd, packet->data, packet->data_len, 0,
458 (struct sockaddr *)&src, sizeof_src,
459 (struct sockaddr *)&dst, sizeof_dst);
463 static int fr_dhcp_attr2vp(RADIUS_PACKET *packet, VALUE_PAIR *vp, uint8_t const *p, size_t alen);
465 static int decode_tlv(RADIUS_PACKET *packet, VALUE_PAIR *tlv, uint8_t const *data, size_t data_len)
468 VALUE_PAIR *head, *vp;
472 * Take a pass at parsing it.
475 while (p < (data + data_len)) {
476 if ((p + 2) > (data + data_len)) goto make_tlv;
478 if ((p + p[1] + 2) > (data + data_len)) goto make_tlv;
483 * Got here... must be well formed.
486 paircursor(&cursor, &head);
489 while (p < (data + data_len)) {
490 vp = paircreate(packet, tlv->da->attr | (p[0] << 8), DHCP_MAGIC_VENDOR);
496 if (fr_dhcp_attr2vp(packet, vp, p + 2, p[1]) < 0) {
501 pairinsert(&cursor, vp);
506 * The caller allocated TLV, so we need to copy the FIRST
507 * attribute over top of that.
510 memcpy(tlv, head, sizeof(*tlv));
518 tlv->vp_tlv = talloc_array(tlv, uint8_t, data_len);
520 fr_strerror_printf("No memory");
523 memcpy(tlv->vp_tlv, data, data_len);
524 tlv->length = data_len;
531 * Decode ONE value into a VP
533 static int fr_dhcp_attr2vp(RADIUS_PACKET *packet, VALUE_PAIR *vp, uint8_t const *p, size_t alen)
537 switch (vp->da->type) {
539 if (alen != 1) goto raw;
540 vp->vp_integer = p[0];
544 if (alen != 2) goto raw;
545 memcpy(&vp->vp_integer, p, 2);
546 vp->vp_integer = ntohs(vp->vp_integer);
549 case PW_TYPE_INTEGER:
550 if (alen != 4) goto raw;
551 memcpy(&vp->vp_integer, p, 4);
552 vp->vp_integer = ntohl(vp->vp_integer);
556 if (alen != 4) goto raw;
558 * Keep value in Network Order!
560 memcpy(&vp->vp_ipaddr, p , 4);
565 vp->vp_strvalue = q = talloc_array(vp, char, alen + 1);
571 case PW_TYPE_ETHERNET:
572 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
573 vp->length = sizeof(vp->vp_ether);
577 * Value doesn't match up with attribute type, overwrite the
578 * vp's original DICT_ATTR with an unknown one.
581 if (pair2unknown(vp) < 0) return -1;
584 if (alen > 253) return -1;
585 pairmemcpy(vp, p, alen);
589 return decode_tlv(packet, vp, p, alen);
592 fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
594 } /* switch over type */
600 ssize_t fr_dhcp_decode_options(RADIUS_PACKET *packet,
601 uint8_t const *data, size_t len, VALUE_PAIR **head)
606 uint8_t const *p, *next;
610 paircursor(&cursor, head);
613 * FIXME: This should also check sname && file fields.
614 * See the dhcp_get_option() function above.
616 while (next < (data + len)) {
617 int num_entries, alen;
623 if (*p == 255) break; /* end of options signifier */
624 if ((p + 2) > (data + len)) break;
629 fr_strerror_printf("Attribute too long %u %u",
634 da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
636 fr_strerror_printf("Attribute not in our dictionary: %u",
647 * Could be an array of bytes, integers, etc.
649 if (da->flags.array) {
656 case PW_TYPE_SHORT: /* ignore any trailing data */
657 num_entries = alen >> 1;
662 case PW_TYPE_INTEGER:
663 case PW_TYPE_DATE: /* ignore any trailing data */
664 num_entries = alen >> 2;
670 break; /* really an internal sanity failure */
675 * Loop over all of the entries, building VPs
677 for (i = 0; i < num_entries; i++) {
678 vp = pairmake(packet, NULL, da->name, NULL, T_OP_ADD);
680 fr_strerror_printf("Cannot build attribute %s",
687 * Hack for ease of use.
689 if ((da->vendor == DHCP_MAGIC_VENDOR) &&
690 (da->attr == 61) && !da->flags.array &&
691 (alen == 7) && (*p == 1) && (num_entries == 1)) {
692 pairmemcpy(vp, p + 1, 6);
694 } else if (fr_dhcp_attr2vp(packet, vp, p, alen) < 0) {
700 pairinsert(&cursor, vp);
702 for (vp = paircurrent(&cursor);
704 vp = pairnext(&cursor)) {
708 } /* loop over array entries */
709 } /* loop over the entire packet */
714 int fr_dhcp_decode(RADIUS_PACKET *packet)
720 VALUE_PAIR *head = NULL, *vp;
721 VALUE_PAIR *maxms, *mtu;
723 paircursor(&cursor, &head);
726 if ((fr_debug_flag > 2) && fr_log_fp) {
727 for (i = 0; i < packet->data_len; i++) {
728 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
729 fprintf(fr_log_fp, "%02x ", packet->data[i]);
730 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
732 fprintf(fr_log_fp, "\n");
735 if (packet->data[1] != 1) {
736 fr_strerror_printf("Packet is not Ethernet: %u",
744 for (i = 0; i < 14; i++) {
747 vp = pairmake(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
750 strlcpy(buffer, fr_strerror(), sizeof(buffer));
751 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
757 * If chaddr does != 6 bytes it's probably not ethernet, and we should store
758 * it as an opaque type (octets).
760 if ((i == 11) && (packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
761 DICT_ATTR const *da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
768 switch (vp->da->type) {
770 vp->vp_integer = p[0];
775 vp->vp_integer = (p[0] << 8) | p[1];
779 case PW_TYPE_INTEGER:
780 memcpy(&vp->vp_integer, p, 4);
781 vp->vp_integer = ntohl(vp->vp_integer);
786 memcpy(&vp->vp_ipaddr, p, 4);
791 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
793 memcpy(q, p, dhcp_header_sizes[i]);
794 q[dhcp_header_sizes[i]] = '\0';
795 vp->length = strlen(vp->vp_strvalue);
796 if (vp->length == 0) {
802 pairmemcpy(vp, p, packet->data[2]);
805 case PW_TYPE_ETHERNET:
806 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
807 vp->length = sizeof(vp->vp_ether);
811 fr_strerror_printf("BAD TYPE %d", vp->da->type);
815 p += dhcp_header_sizes[i];
820 pairinsert(&cursor, vp);
824 * Loop over the options.
828 * Nothing uses tail after this call, if it does in the future
829 * it'll need to find the new tail...
832 VALUE_PAIR *options = NULL;
834 if (fr_dhcp_decode_options(packet,
835 packet->data + 240, packet->data_len - 240,
841 pairinsert(&cursor, options);
846 * If DHCP request, set ciaddr to zero.
850 * Set broadcast flag for broken vendors, but only if
853 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
854 if (giaddr == htonl(INADDR_ANY)) {
856 * DHCP Opcode is request
858 vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
859 if (vp && vp->vp_integer == 3) {
861 * Vendor is "MSFT 98"
863 vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
864 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
865 vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
868 * Reply should be broadcast.
870 if (vp) vp->vp_integer |= 0x8000;
871 packet->data[10] |= 0x80;
877 * FIXME: Nuke attributes that aren't used in the normal
878 * header for discover/requests.
883 * Client can request a LARGER size, but not a smaller
884 * one. They also cannot request a size larger than MTU.
886 maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
887 mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
889 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
890 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
894 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
895 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
896 maxms->vp_integer = DEFAULT_PACKET_SIZE;
899 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
900 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
901 maxms->vp_integer = mtu->vp_integer;
904 if (fr_debug_flag) fflush(stdout);
910 static int attr_cmp(void const *one, void const *two)
912 VALUE_PAIR const * const *a = one;
913 VALUE_PAIR const * const *b = two;
916 * DHCP-Message-Type is first, for simplicity.
918 if (((*a)->da->attr == 53) &&
919 (*b)->da->attr != 53) return -1;
922 * Relay-Agent is last
924 if (((*a)->da->attr == 82) &&
925 (*b)->da->attr != 82) return 1;
927 return ((*a)->da->attr - (*b)->da->attr);
933 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, UNUSED size_t room)
939 * Search for all attributes of the same
940 * type, and pack them into the same
943 switch (vp->da->type) {
946 *p = vp->vp_integer & 0xff;
951 p[0] = (vp->vp_integer >> 8) & 0xff;
952 p[1] = vp->vp_integer & 0xff;
955 case PW_TYPE_INTEGER:
957 lvalue = htonl(vp->vp_integer);
958 memcpy(p, &lvalue, 4);
963 memcpy(p, &vp->vp_ipaddr, 4);
966 case PW_TYPE_ETHERNET:
968 memcpy(p, &vp->vp_ether, 6);
972 memcpy(p, vp->vp_strvalue, vp->length);
976 case PW_TYPE_TLV: /* FIXME: split it on 255? */
977 memcpy(p, vp->vp_tlv, vp->length);
982 memcpy(p, vp->vp_octets, vp->length);
987 fr_strerror_printf("BAD TYPE2 %d", vp->da->type);
995 static VALUE_PAIR *fr_dhcp_vp2suboption(RADIUS_PACKET *packet, VALUE_PAIR *vps)
998 unsigned int attribute;
1001 VALUE_PAIR *vp, *tlv;
1003 attribute = vps->da->attr & 0xffff00ff;
1005 tlv = paircreate(packet, attribute, DHCP_MAGIC_VENDOR);
1006 if (!tlv) return NULL;
1009 for (vp = paircursor(&cursor, &vps);
1011 vp = pairnext(&cursor)) {
1013 * Group the attributes ONLY until we see a
1014 * non-TLV attribute.
1016 if (!vp->da->flags.is_tlv ||
1017 vp->da->flags.extended ||
1018 ((vp->da->attr & 0xffff00ff) != attribute)) {
1022 tlv->length += vp->length + 2;
1030 tlv->vp_tlv = talloc_array(tlv, uint8_t, tlv->length);
1037 for (vp = paircursor(&cursor, &vps);
1039 vp = pairnext(&cursor)) {
1040 if (!vp->da->flags.is_tlv ||
1041 vp->da->flags.extended ||
1042 ((vp->da->attr & 0xffff00ff) != attribute)) {
1046 length = fr_dhcp_vp2attr(vp, ptr + 2,
1047 tlv->vp_tlv + tlv->length - ptr);
1054 * Pack the attribute.
1056 ptr[0] = (vp->da->attr & 0xff00) >> 8;
1066 int fr_dhcp_encode(RADIUS_PACKET *packet)
1068 unsigned int i, num_vps;
1073 size_t dhcp_size, length;
1076 # ifdef WITH_UDPFROMTO
1077 char src_ip_buf[256];
1079 char dst_ip_buf[256];
1082 if (packet->data) return 0;
1084 packet->data_len = MAX_PACKET_SIZE;
1085 packet->data = talloc_zero_array(packet, uint8_t, packet->data_len);
1087 /* XXX Ugly ... should be set by the caller */
1088 if (packet->code == 0) packet->code = PW_DHCP_NAK;
1091 if ((packet->code >= PW_DHCP_DISCOVER) &&
1092 (packet->code <= PW_DHCP_INFORM)) {
1093 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1099 #ifdef WITH_UDPFROMTO
1100 "Encoding %s of id %08x from %s:%d to %s:%d\n",
1102 "Encoding %s of id %08x to %s:%d\n",
1104 name, (unsigned int) packet->id,
1105 #ifdef WITH_UDPFROMTO
1106 inet_ntop(packet->src_ipaddr.af,
1107 &packet->src_ipaddr.ipaddr,
1108 src_ip_buf, sizeof(src_ip_buf)),
1111 inet_ntop(packet->dst_ipaddr.af,
1112 &packet->dst_ipaddr.ipaddr,
1113 dst_ip_buf, sizeof(dst_ip_buf)),
1120 * @todo: Make this work again.
1123 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1126 * Clients can request a LARGER size, but not a
1127 * smaller one. They also cannot request a size
1131 /* DHCP-DHCP-Maximum-Msg-Size */
1132 vp = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1133 if (vp && (vp->vp_integer > mms)) {
1134 mms = vp->vp_integer;
1136 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1140 vp = pairfind(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1142 *p++ = vp->vp_integer & 0xff;
1144 *p++ = 1; /* client message */
1147 /* DHCP-Hardware-Type */
1148 if ((vp = pairfind(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1149 *p++ = vp->vp_integer & 0xFF;
1151 *p++ = 1; /* hardware type = ethernet */
1154 /* DHCP-Hardware-Address-Length */
1155 if ((vp = pairfind(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1156 *p++ = vp->vp_integer & 0xFF;
1158 *p++ = 6; /* 6 bytes of ethernet */
1161 /* DHCP-Hop-Count */
1162 if ((vp = pairfind(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1163 *p = vp->vp_integer & 0xff;
1167 /* DHCP-Transaction-Id */
1168 if ((vp = pairfind(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1169 lvalue = htonl(vp->vp_integer);
1173 memcpy(p, &lvalue, 4);
1176 /* DHCP-Number-of-Seconds */
1177 if ((vp = pairfind(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1178 lvalue = htonl(vp->vp_integer);
1179 memcpy(p, &lvalue, 2);
1184 if ((vp = pairfind(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1185 lvalue = htons(vp->vp_integer);
1186 memcpy(p, &lvalue, 2);
1190 /* DHCP-Client-IP-Address */
1191 if ((vp = pairfind(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1192 memcpy(p, &vp->vp_ipaddr, 4);
1196 /* DHCP-Your-IP-address */
1197 if ((vp = pairfind(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1198 lvalue = vp->vp_ipaddr;
1200 lvalue = htonl(INADDR_ANY);
1202 memcpy(p, &lvalue, 4);
1205 /* DHCP-Server-IP-Address */
1206 vp = pairfind(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
1208 /* DHCP-DHCP-Server-Identifier */
1209 if (!vp && (vp = pairfind(packet->vps, 54, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1210 lvalue = vp->vp_ipaddr;
1212 lvalue = htonl(INADDR_ANY);
1214 memcpy(p, &lvalue, 4);
1218 * DHCP-Gateway-IP-Address
1220 if ((vp = pairfind(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1221 lvalue = vp->vp_ipaddr;
1223 lvalue = htonl(INADDR_ANY);
1225 memcpy(p, &lvalue, 4);
1228 /* DHCP-Client-Hardware-Address */
1229 if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1230 if (vp->length == sizeof(vp->vp_ether)) {
1231 memcpy(p, vp->vp_ether, vp->length);
1232 } /* else ignore it */
1234 p += DHCP_CHADDR_LEN;
1236 /* DHCP-Server-Host-Name */
1237 if ((vp = pairfind(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1238 if (vp->length > DHCP_SNAME_LEN) {
1239 memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
1241 memcpy(p, vp->vp_strvalue, vp->length);
1244 p += DHCP_SNAME_LEN;
1247 * Copy over DHCP-Boot-Filename.
1249 * FIXME: This copy should be delayed until AFTER the options
1250 * have been processed. If there are too many options for
1251 * the packet, then they go into the sname && filename fields.
1252 * When that happens, the boot filename is passed as an option,
1253 * instead of being placed verbatim in the filename field.
1256 /* DHCP-Boot-Filename */
1257 if ((vp = pairfind(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1258 if (vp->length > DHCP_FILE_LEN) {
1259 memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
1261 memcpy(p, vp->vp_strvalue, vp->length);
1266 /* DHCP magic number */
1267 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER);
1268 memcpy(p, &lvalue, 4);
1274 if (fr_debug_flag > 1) {
1279 for (i = 0; i < 14; i++) {
1282 vp = pairmake(packet, NULL,
1283 dhcp_header_names[i], NULL, T_OP_EQ);
1286 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1287 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1291 switch (vp->da->type) {
1293 vp->vp_integer = p[0];
1298 vp->vp_integer = (p[0] << 8) | p[1];
1302 case PW_TYPE_INTEGER:
1303 memcpy(&vp->vp_integer, p, 4);
1304 vp->vp_integer = ntohl(vp->vp_integer);
1308 case PW_TYPE_IPADDR:
1309 memcpy(&vp->vp_ipaddr, p, 4);
1313 case PW_TYPE_STRING:
1314 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1316 memcpy(q, p, dhcp_header_sizes[i]);
1317 q[dhcp_header_sizes[i]] = '\0';
1318 vp->length = strlen(vp->vp_strvalue);
1321 case PW_TYPE_OCTETS: /* only for Client HW Address */
1322 pairmemcpy(vp, p, packet->data[2]);
1325 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1326 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1327 vp->length = sizeof(vp->vp_ether);
1331 fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
1336 p += dhcp_header_sizes[i];
1343 * Jump over DHCP magic number, response, etc.
1349 * Before packing the attributes, re-order them so that
1350 * the array ones are all contiguous. This simplifies
1354 for (vp = paircursor(&cursor, &packet->vps);
1356 vp = pairnext(&cursor)) {
1362 array = talloc_array(packet, VALUE_PAIR*, num_vps);
1365 for (vp = paircursor(&cursor, &packet->vps);
1367 vp = pairnext(&cursor)) {
1372 * Sort the attributes.
1374 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *), attr_cmp);
1377 paircursor(&cursor, &packet->vps);
1378 for (i = 0; i < num_vps; i++) {
1379 array[i]->next = NULL;
1380 pairinsert(&cursor, array[i]);
1385 p[0] = 0x35; /* DHCP-Message-Type */
1387 p[2] = packet->code - PW_DHCP_OFFSET;
1391 * Pack in the attributes.
1395 unsigned int num_entries = 1;
1399 if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next;
1400 if (vp->da->attr == 53) goto next; /* already done */
1401 if ((vp->da->attr > 255) &&
1402 (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
1405 if (vp->da->flags.extended) goto next;
1407 for (same = paircursor(&cursor, &vp->next);
1409 same = pairnext(&cursor)) {
1410 if (same->da->attr != vp->da->attr) break;
1415 * For client-identifier
1416 * @todo What's this meant to be doing?!
1419 if ((vp->da->type == PW_TYPE_ETHERNET) &&
1420 (vp->length == 6) &&
1421 (num_entries == 1)) {
1422 vp->da->type = PW_TYPE_OCTETS;
1423 memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1424 vp->vp_octets[0] = 1;
1427 *(p++) = vp->da->attr & 0xff;
1429 *(p++) = 0; /* header isn't included in attr length */
1431 for (i = 0; i < num_entries; i++) {
1432 if (i != 0) debug_pair(vp);
1434 if (vp->da->flags.is_tlv) {
1438 * Should NOT have been encoded yet!
1440 tlv = fr_dhcp_vp2suboption(packet, vp);
1443 * Ignore it if there's an issue
1446 if (!tlv) goto next;
1448 tlv->next = vp->next;
1453 length = fr_dhcp_vp2attr(vp, p, 0);
1456 * This will never happen due to FreeRADIUS
1457 * limitations: sizeof(vp->vp_octets) < 255
1460 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->da->name);
1465 * More than one attribute of the same type
1466 * in a row: they are packed together
1467 * into the same TLV. If we overflow,
1470 if ((*plength + length) > 255) {
1471 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->da->name);
1479 (vp->next->da->attr == vp->da->attr))
1481 } /* loop over num_entries */
1487 p[0] = 0xff; /* end of option option */
1490 dhcp_size = p - packet->data;
1493 * FIXME: if (dhcp_size > mms),
1494 * then we put the extra options into the "sname" and "file"
1495 * fields, AND set the "end option option" in the "options"
1496 * field. We also set the "overload option",
1497 * and put options into the "file" field, followed by
1498 * the "sname" field. Where each option is completely
1499 * enclosed in the "file" and/or "sname" field, AND
1500 * followed by the "end of option", and MUST be followed
1501 * by padding option.
1503 * Yuck. That sucks...
1505 packet->data_len = dhcp_size;
1507 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1508 memset(packet->data + packet->data_len, 0,
1509 DEFAULT_PACKET_SIZE - packet->data_len);
1510 packet->data_len = DEFAULT_PACKET_SIZE;
1513 if ((fr_debug_flag > 2) && fr_log_fp) {
1514 fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
1515 for (i = 0; i < packet->data_len; i++) {
1516 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1517 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1518 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1520 fprintf(fr_log_fp, "\n");
1527 int fr_dhcp_add_arp_entry(int fd, char const *interface,
1528 VALUE_PAIR *macaddr, VALUE_PAIR *ip)
1530 struct sockaddr_in *sin;
1534 fr_strerror_printf("No interface specified. Cannot update ARP table");
1538 if (!fr_assert(macaddr) ||
1539 !fr_assert((macaddr->da->type == PW_TYPE_ETHERNET) || (macaddr->da->type == PW_TYPE_OCTETS))) {
1540 fr_strerror_printf("Wrong VP type (%s) for chaddr",
1541 fr_int2str(dict_attr_types, macaddr->da->type, "<invalid>"));
1545 if (macaddr->length > sizeof(req.arp_ha.sa_data)) {
1546 fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
1547 sizeof(req.arp_ha.sa_data), macaddr->length);
1551 memset(&req, 0, sizeof(req));
1552 sin = (struct sockaddr_in *) &req.arp_pa;
1553 sin->sin_family = AF_INET;
1554 sin->sin_addr.s_addr = ip->vp_ipaddr;
1556 strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
1558 if (macaddr->da->type == PW_TYPE_ETHERNET) {
1559 memcpy(&req.arp_ha.sa_data, &macaddr->vp_ether, sizeof(macaddr->vp_ether));
1561 memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->length);
1564 req.arp_flags = ATF_COM;
1565 if (ioctl(fd, SIOCSARP, &req) < 0) {
1566 fr_strerror_printf("Failed to add entry in ARP cache: %s (%d)", fr_syserror(errno), errno);
1573 int fr_dhcp_add_arp_entry(UNUSED int fd, UNUSED char const *interface,
1574 UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
1576 fr_strerror_printf("Adding ARP entry is unsupported on this system");