2 * dhcp.c Functions to send/receive dhcp packets.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; 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>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/libradius.h>
28 #include <freeradius-devel/udpfromto.h>
29 #include <freeradius-devel/dhcp.h>
32 #define DHCP_CHADDR_LEN (16)
33 #define DHCP_SNAME_LEN (64)
34 #define DHCP_FILE_LEN (128)
35 #define DHCP_VEND_LEN (308)
36 #define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
38 #ifndef INADDR_BROADCAST
39 #define INADDR_BROADCAST INADDR_NONE
42 typedef struct dhcp_packet_t {
48 uint16_t secs; /* 8 */
50 uint32_t ciaddr; /* 12 */
51 uint32_t yiaddr; /* 16 */
52 uint32_t siaddr; /* 20 */
53 uint32_t giaddr; /* 24 */
54 uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
55 uint8_t sname[DHCP_SNAME_LEN]; /* 44 */
56 uint8_t file[DHCP_FILE_LEN]; /* 108 */
57 uint32_t option_format; /* 236 */
58 uint8_t options[DHCP_VEND_LEN];
61 typedef struct dhcp_option_t {
67 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 DISCOVER
68 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 OFFER
69 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 REQUEST
70 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 ACK
72 static const char *dhcp_header_names[] = {
75 "DHCP-Hardware-Address-Length",
77 "DHCP-Transaction-Id",
78 "DHCP-Number-of-Seconds",
80 "DHCP-Client-IP-Address",
81 "DHCP-Your-IP-Address",
82 "DHCP-Server-IP-Address",
83 "DHCP-Gateway-IP-Address",
84 "DHCP-Client-Hardware-Address",
85 "DHCP-Server-Host-Name",
91 static const char *dhcp_message_types[] = {
104 static int dhcp_header_sizes[] = {
115 * Some clients silently ignore responses less than 300 bytes.
117 #define MIN_PACKET_SIZE (244)
118 #define DEFAULT_PACKET_SIZE (300)
119 #define MAX_PACKET_SIZE (1500 - 40)
121 #define DHCP_OPTION_FIELD (0)
122 #define DHCP_FILE_FIELD (1)
123 #define DHCP_SNAME_FIELD (2)
125 static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
130 int field = DHCP_OPTION_FIELD;
132 uint8_t *data = packet->options;
135 size = packet_size - offsetof(dhcp_packet_t, options);
136 data = &packet->options[where];
138 while (where < size) {
139 if (data[0] == 0) { /* padding */
144 if (data[0] == 255) { /* end of options */
145 if ((field == DHCP_OPTION_FIELD) &&
146 (overload & DHCP_FILE_FIELD)) {
149 size = sizeof(packet->file);
150 field = DHCP_FILE_FIELD;
153 } else if ((field == DHCP_FILE_FIELD) &&
154 (overload && DHCP_SNAME_FIELD)) {
155 data = packet->sname;
157 size = sizeof(packet->sname);
158 field = DHCP_SNAME_FIELD;
166 * We MUST have a real option here.
168 if ((where + 2) > size) {
169 fr_strerror_printf("Options overflow field at %u",
170 (unsigned int) (data - (uint8_t *) packet));
174 if ((where + 2 + data[1]) > size) {
175 fr_strerror_printf("Option length overflows field at %u",
176 (unsigned int) (data - (uint8_t *) packet));
180 if (data[0] == option) return data;
182 if (data[0] == 52) { /* overload sname and/or file */
186 where += data[1] + 2;
194 * DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
197 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
200 struct sockaddr_storage src;
201 struct sockaddr_storage dst;
202 socklen_t sizeof_src;
203 socklen_t sizeof_dst;
204 RADIUS_PACKET *packet;
208 packet = rad_alloc(0);
210 fr_strerror_printf("Failed allocating packet");
213 memset(packet, 0, sizeof(packet));
215 packet->data = malloc(MAX_PACKET_SIZE);
217 fr_strerror_printf("Failed in malloc");
222 packet->sockfd = sockfd;
223 sizeof_src = sizeof(src);
224 #ifdef WITH_UDPFROMTO
225 sizeof_dst = sizeof(dst);
226 packet->data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
227 (struct sockaddr *)&src, &sizeof_src,
228 (struct sockaddr *)&dst, &sizeof_dst);
230 packet->data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
231 (struct sockaddr *)&src, &sizeof_src);
234 if (packet->data_len <= 0) {
235 fr_strerror_printf("Failed reading DHCP socket: %s", strerror(errno));
240 if (packet->data_len < MIN_PACKET_SIZE) {
241 fr_strerror_printf("DHCP packet is too small (%d < %d)",
242 packet->data_len, MIN_PACKET_SIZE);
247 if (packet->data[1] != 1) {
248 fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
254 if (packet->data[2] != 6) {
255 fr_strerror_printf("Ethernet HW length is wrong length %d",
261 memcpy(&magic, packet->data + 236, 4);
262 magic = ntohl(magic);
263 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
264 fr_strerror_printf("Cannot do BOOTP");
270 * Create unique keys for the packet.
272 memcpy(&magic, packet->data + 4, 4);
273 packet->id = ntohl(magic);
275 code = dhcp_get_option((dhcp_packet_t *) packet->data,
276 packet->data_len, 53);
278 fr_strerror_printf("No message-type option was found in the packet");
283 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
284 fr_strerror_printf("Unknown value for message-type option");
289 packet->code = code[2] | PW_DHCP_OFFSET;
292 * Create a unique vector from the MAC address and the
293 * DHCP opcode. This is a hack for the RADIUS
294 * infrastructure in the rest of the server.
296 * Note: packet->data[2] == 6, which is smaller than
297 * sizeof(packet->vector)
299 * FIXME: Look for client-identifier in packet,
302 memset(packet->vector, 0, sizeof(packet->vector));
303 memcpy(packet->vector, packet->data + 28, packet->data[2]);
304 packet->vector[packet->data[2]] = packet->code & 0xff;
307 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
308 * FIXME: for OFFER / ACK : src_port = dst_port - 1
312 * Unique keys are xid, client mac, and client ID?
316 * FIXME: More checks, like DHCP packet type?
319 sizeof_dst = sizeof(dst);
321 #ifndef WITH_UDPFROMTO
323 * This should never fail...
325 getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst);
328 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
329 packet->dst_port = port;
331 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
332 packet->src_port = port;
334 if (fr_debug_flag > 1) {
336 const char *name = type_buf;
337 char src_ip_buf[256], dst_ip_buf[256];
339 if ((packet->code >= PW_DHCP_DISCOVER) &&
340 (packet->code <= PW_DHCP_INFORM)) {
341 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
343 snprintf(type_buf, sizeof(type_buf), "%d",
344 packet->code - PW_DHCP_OFFSET);
347 DEBUG("Received %s of id %08x from %s:%d to %s:%d\n",
348 name, (unsigned int) packet->id,
349 inet_ntop(packet->src_ipaddr.af,
350 &packet->src_ipaddr.ipaddr,
351 src_ip_buf, sizeof(src_ip_buf)),
353 inet_ntop(packet->dst_ipaddr.af,
354 &packet->dst_ipaddr.ipaddr,
355 dst_ip_buf, sizeof(dst_ip_buf)),
364 * Send a DHCP packet.
366 int fr_dhcp_send(RADIUS_PACKET *packet)
368 struct sockaddr_storage dst;
369 socklen_t sizeof_dst;
370 #ifdef WITH_UDPFROMTO
371 struct sockaddr_storage src;
372 socklen_t sizeof_src;
375 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
378 #ifndef WITH_UDPFROMTO
380 * Assume that the packet is encoded before sending it.
382 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
383 (struct sockaddr *)&dst, sizeof_dst);
385 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
388 return sendfromto(packet->sockfd,
389 packet->data, packet->data_len, 0,
390 (struct sockaddr *)&src, sizeof_src,
391 (struct sockaddr *)&dst, sizeof_dst);
395 static int fr_dhcp_attr2vp(VALUE_PAIR *vp, const uint8_t *p, size_t alen);
397 static int decode_tlv(VALUE_PAIR *tlv, const uint8_t *data, size_t data_len)
400 VALUE_PAIR *head, **tail, *vp;
403 * Take a pass at parsing it.
406 while (p < (data + data_len)) {
407 if ((p + 2) > (data + data_len)) goto make_tlv;
409 if ((p + p[1] + 2) > (data + data_len)) goto make_tlv;
414 * Got here... must be well formed.
420 while (p < (data + data_len)) {
421 vp = paircreate(tlv->attribute | (p[0] << 8), PW_TYPE_OCTETS);
427 if (fr_dhcp_attr2vp(vp, p + 2, p[1]) < 0) {
438 * The caller allocated TLV, so we need to copy the FIRST
439 * attribute over top of that.
441 memcpy(tlv, head, sizeof(*tlv));
447 tlv->vp_tlv = malloc(data_len);
449 fr_strerror_printf("No memory");
452 memcpy(tlv->vp_tlv, data, data_len);
453 tlv->length = data_len;
460 * Decode ONE value into a VP
462 static int fr_dhcp_attr2vp(VALUE_PAIR *vp, const uint8_t *p, size_t alen)
466 if (alen != 1) goto raw;
467 vp->vp_integer = p[0];
471 if (alen != 2) goto raw;
472 vp->vp_integer = (p[0] << 8) | p[1];
475 case PW_TYPE_INTEGER:
476 if (alen != 4) goto raw;
477 memcpy(&vp->vp_integer, p, 4);
478 vp->vp_integer = ntohl(vp->vp_integer);
482 if (alen != 4) goto raw;
483 memcpy(&vp->vp_ipaddr, p , 4);
488 if (alen > 253) return -1;
489 memcpy(vp->vp_strvalue, p , alen);
490 vp->vp_strvalue[alen] = '\0';
494 vp->type = PW_TYPE_OCTETS;
497 if (alen > 253) return -1;
498 memcpy(vp->vp_octets, p, alen);
502 return decode_tlv(vp, p, alen);
505 fr_strerror_printf("Internal sanity check %d %d", vp->type, __LINE__);
507 } /* switch over type */
513 int fr_dhcp_decode(RADIUS_PACKET *packet)
518 VALUE_PAIR *head, *vp, **tail;
519 VALUE_PAIR *maxms, *mtu;
525 if ((fr_debug_flag > 2) && fr_log_fp) {
526 for (i = 0; i < packet->data_len; i++) {
527 if ((i & 0x0f) == 0x00) fr_strerror_printf("%d: ", i);
528 fprintf(fr_log_fp, "%02x ", packet->data[i]);
529 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
531 fprintf(fr_log_fp, "\n");
534 if (packet->data[1] != 1) {
535 fr_strerror_printf("Packet is not Ethernet: %u",
543 for (i = 0; i < 14; i++) {
544 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
546 fr_strerror_printf("Parse error %s", fr_strerror());
552 (packet->data[1] == 1) &&
553 (packet->data[2] == 6)) {
554 vp->type = PW_TYPE_ETHERNET;
559 vp->vp_integer = p[0];
564 vp->vp_integer = (p[0] << 8) | p[1];
568 case PW_TYPE_INTEGER:
569 memcpy(&vp->vp_integer, p, 4);
570 vp->vp_integer = ntohl(vp->vp_integer);
575 memcpy(&vp->vp_ipaddr, p, 4);
580 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
581 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
582 vp->length = strlen(vp->vp_strvalue);
583 if (vp->length == 0) {
589 memcpy(vp->vp_octets, p, packet->data[2]);
590 vp->length = packet->data[2];
593 case PW_TYPE_ETHERNET:
594 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
595 vp->length = sizeof(vp->vp_ether);
599 fr_strerror_printf("BAD TYPE %d", vp->type);
603 p += dhcp_header_sizes[i];
613 * Loop over the options.
615 next = packet->data + 240;
618 * FIXME: This should also check sname && file fields.
619 * See the dhcp_get_option() function above.
621 while (next < (packet->data + packet->data_len)) {
622 int num_entries, alen;
628 if (*p == 255) break; /* end of options signifier */
629 if ((p + 2) > (packet->data + packet->data_len)) break;
634 fr_strerror_printf("Attribute too long %u %u",
639 da = dict_attrbyvalue(DHCP2ATTR(p[0]));
641 fr_strerror_printf("Attribute not in our dictionary: %u",
652 * Could be an array of bytes, integers, etc.
654 if (da->flags.array) {
661 case PW_TYPE_SHORT: /* ignore any trailing data */
662 num_entries = alen >> 1;
667 case PW_TYPE_INTEGER:
668 case PW_TYPE_DATE: /* ignore any trailing data */
669 num_entries = alen >> 2;
675 break; /* really an internal sanity failure */
680 * Loop over all of the entries, building VPs
682 for (i = 0; i < num_entries; i++) {
683 vp = pairmake(da->name, NULL, T_OP_EQ);
685 fr_strerror_printf("Cannot build attribute %s",
692 * Hack for ease of use.
694 if ((da->attr == DHCP2ATTR(0x3d)) &&
696 (alen == 7) && (*p == 1) && (num_entries == 1)) {
697 vp->type = PW_TYPE_ETHERNET;
698 memcpy(vp->vp_octets, p + 1, 6);
701 } else if (fr_dhcp_attr2vp(vp, p, alen) < 0) {
709 while (*tail) tail = &(*tail)->next;
711 } /* loop over array entries */
712 } /* loop over the entire packet */
715 * If DHCP request, set ciaddr to zero.
719 * Set broadcast flag for broken vendors, but only if
722 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
723 if (giaddr == htonl(INADDR_ANY)) {
725 * DHCP Opcode is request
727 vp = pairfind(head, DHCP2ATTR(256));
728 if (vp && vp->lvalue == 3) {
730 * Vendor is "MSFT 98"
732 vp = pairfind(head, DHCP2ATTR(63));
733 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
734 vp = pairfind(head, DHCP2ATTR(262));
737 * Reply should be broadcast.
739 if (vp) vp->lvalue |= 0x8000;
740 packet->data[10] |= 0x80;
746 * FIXME: Nuke attributes that aren't used in the normal
747 * header for discover/requests.
752 * Client can request a LARGER size, but not a smaller
753 * one. They also cannot request a size larger than MTU.
755 maxms = pairfind(packet->vps, DHCP2ATTR(57));
756 mtu = pairfind(packet->vps, DHCP2ATTR(26));
758 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
759 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
763 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
764 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
765 maxms->vp_integer = DEFAULT_PACKET_SIZE;
768 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
769 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
770 maxms->vp_integer = mtu->vp_integer;
773 if (fr_debug_flag > 0) {
774 for (vp = packet->vps; vp != NULL; vp = vp->next) {
779 if (fr_debug_flag) fflush(stdout);
785 static int attr_cmp(const void *one, const void *two)
787 const VALUE_PAIR * const *a = one;
788 const VALUE_PAIR * const *b = two;
791 * DHCP-Message-Type is first, for simplicity.
793 if (((*a)->attribute == DHCP2ATTR(53)) &&
794 (*b)->attribute != DHCP2ATTR(53)) return -1;
797 * Relay-Agent is last
799 if (((*a)->attribute == DHCP2ATTR(82)) &&
800 (*b)->attribute != DHCP2ATTR(82)) return +1;
802 return ((*a)->attribute - (*b)->attribute);
806 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, size_t room)
814 room = room; /* -Wunused */
817 * Search for all attributes of the same
818 * type, and pack them into the same
824 *p = vp->vp_integer & 0xff;
829 p[0] = (vp->vp_integer >> 8) & 0xff;
830 p[1] = vp->vp_integer & 0xff;
833 case PW_TYPE_INTEGER:
835 lvalue = htonl(vp->vp_integer);
836 memcpy(p, &lvalue, 4);
841 memcpy(p, &vp->vp_ipaddr, 4);
844 case PW_TYPE_ETHERNET:
846 memcpy(p, &vp->vp_ether, 6);
850 memcpy(p, vp->vp_strvalue, vp->length);
854 case PW_TYPE_TLV: /* FIXME: split it on 255? */
855 memcpy(p, vp->vp_tlv, vp->length);
860 memcpy(p, vp->vp_octets, vp->length);
865 fr_strerror_printf("BAD TYPE2 %d", vp->type);
873 static VALUE_PAIR *fr_dhcp_vp2suboption(VALUE_PAIR *vps)
876 unsigned int attribute;
878 VALUE_PAIR *vp, *tlv;
880 attribute = vps->attribute & 0xffff00ff;
882 tlv = paircreate(attribute, PW_TYPE_TLV);
883 if (!tlv) return NULL;
886 for (vp = vps; vp != NULL; vp = vp->next) {
888 * Group the attributes ONLY until we see a
891 if (!vp->flags.is_tlv ||
893 ((vp->attribute & 0xffff00ff) != attribute)) {
897 tlv->length += vp->length + 2;
905 tlv->vp_tlv = malloc(tlv->length);
912 for (vp = vps; vp != NULL; vp = vp->next) {
913 if (!vp->flags.is_tlv ||
915 ((vp->attribute & 0xffff00ff) != attribute)) {
919 length = fr_dhcp_vp2attr(vp, ptr + 2,
920 tlv->vp_tlv + tlv->length - ptr);
921 if (length > 255) return NULL;
924 * Pack the attribute.
926 ptr[0] = (vp->attribute & 0xff00) >> 8;
930 vp->flags.encoded = 1;
937 int fr_dhcp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
942 uint32_t lvalue, mms;
943 size_t dhcp_size, length;
947 if (packet->data) return 0;
949 packet->data = malloc(MAX_PACKET_SIZE);
950 if (!packet->data) return -1;
952 packet->data_len = MAX_PACKET_SIZE;
954 if (packet->code == 0) packet->code = PW_DHCP_NAK;
957 * If there's a request, use it as a template.
958 * Otherwise, assume that the caller has set up
959 * everything appropriately.
962 packet->dst_ipaddr.af = AF_INET;
963 packet->src_ipaddr.af = AF_INET;
965 packet->dst_port = original->src_port;
966 packet->src_port = original->dst_port;
969 * Note that for DHCP, we NEVER send the response
970 * to the source IP address of the request. It
971 * may have traversed multiple relays, and we
972 * need to send the request to the relay closest
975 * if giaddr, send to giaddr.
976 * if NAK, send broadcast packet
977 * if ciaddr, unicast to ciaddr
978 * if flags & 0x8000, broadcast (client request)
979 * if sent from 0.0.0.0, broadcast response
980 * unicast to client yiaddr
984 * FIXME: alignment issues. We likely don't want to
985 * de-reference the packet structure directly..
987 dhcp = (dhcp_packet_t *) original->data;
989 if (dhcp->giaddr != htonl(INADDR_ANY)) {
990 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->giaddr;
992 if (dhcp->giaddr != htonl(INADDR_LOOPBACK)) {
993 packet->dst_port = original->dst_port;
995 packet->dst_port = original->src_port; /* debugging */
998 } else if (packet->code == PW_DHCP_NAK) {
999 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1001 } else if (dhcp->ciaddr != htonl(INADDR_ANY)) {
1002 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->ciaddr;
1004 } else if ((dhcp->flags & 0x8000) != 0) {
1005 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1007 } else if (packet->dst_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY)) {
1008 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1010 } else if (dhcp->yiaddr != htonl(INADDR_ANY)) {
1011 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->yiaddr;
1014 /* leave destination IP alone. */
1018 * Rewrite the source IP to be our own, if we know it.
1020 if (packet->src_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_BROADCAST)) {
1021 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1024 memset(packet->data, 0, packet->data_len);
1027 if (fr_debug_flag > 1) {
1029 const char *name = type_buf;
1030 char src_ip_buf[256], dst_ip_buf[256];
1032 if ((packet->code >= PW_DHCP_DISCOVER) &&
1033 (packet->code <= PW_DHCP_INFORM)) {
1034 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1036 snprintf(type_buf, sizeof(type_buf), "%d",
1037 packet->code - PW_DHCP_OFFSET);
1040 DEBUG("Sending %s of id %08x from %s:%d to %s:%d\n",
1041 name, (unsigned int) packet->id,
1042 inet_ntop(packet->src_ipaddr.af,
1043 &packet->src_ipaddr.ipaddr,
1044 src_ip_buf, sizeof(src_ip_buf)),
1046 inet_ntop(packet->dst_ipaddr.af,
1047 &packet->dst_ipaddr.ipaddr,
1048 dst_ip_buf, sizeof(dst_ip_buf)),
1054 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1058 * Clients can request a LARGER size, but not a
1059 * smaller one. They also cannot request a size
1062 vp = pairfind(original->vps, DHCP2ATTR(57));
1063 if (vp && (vp->vp_integer > mms)) {
1064 mms = vp->vp_integer;
1066 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1071 * RFC 3118: Authentication option.
1073 vp = pairfind(packet->vps, DHCP2ATTR(90));
1075 if (vp->length < 2) {
1076 memset(vp->vp_octets + vp->length, 0,
1081 if (vp->length < 3) {
1084 gettimeofday(&tv, NULL);
1085 vp->vp_octets[2] = 0;
1086 timeval2ntp(&tv, vp->vp_octets + 3);
1091 * Configuration token (clear-text token)
1093 if (vp->vp_octets[0] == 0) {
1095 vp->vp_octets[1] = 0;
1097 pass = pairfind(packet->vps, PW_CLEARTEXT_PASSWORD);
1099 length = pass->length;
1100 if ((length + 11) > sizeof(vp->vp_octets)) {
1101 length -= ((length + 11) - sizeof(vp->vp_octets));
1103 memcpy(vp->vp_octets + 11, pass->vp_strvalue,
1105 vp->length = length + 11;
1107 vp->length = 11 + 8;
1108 memset(vp->vp_octets + 11, 0, 8);
1109 vp->length = 11 + 8;
1111 } else { /* we don't support this type! */
1112 fr_strerror_printf("DHCP-Authentication %d unsupported",
1118 *p++ = 1; /* client message */
1120 *p++ = 2; /* server message */
1122 *p++ = 1; /* hardware type = ethernet */
1123 *p++ = 6; /* 6 bytes of ethernet */
1124 *p++ = 0; /* hops */
1126 if (original) { /* Xid */
1127 memcpy(p, original->data + 4, 4);
1130 memcpy(p, &lvalue, 4);
1134 memset(p, 0, 2); /* secs are zero */
1138 memcpy(p, original->data + 10, 6); /* copy flags && ciaddr */
1142 * Allow the admin to set the broadcast flag.
1144 vp = pairfind(packet->vps, DHCP2ATTR(262));
1146 p[0] |= (vp->vp_integer & 0xff00) >> 8;
1147 p[1] |= (vp->vp_integer & 0xff);
1153 * Set client IP address.
1155 vp = pairfind(packet->vps, DHCP2ATTR(264)); /* Your IP address */
1157 lvalue = vp->vp_ipaddr;
1159 lvalue = htonl(INADDR_ANY);
1161 memcpy(p, &lvalue, 4); /* your IP address */
1164 vp = pairfind(packet->vps, DHCP2ATTR(265)); /* server IP address */
1165 if (!vp) vp = pairfind(packet->vps, DHCP2ATTR(54)); /* identifier */
1167 lvalue = vp->vp_ipaddr;
1169 lvalue = htonl(INADDR_ANY);
1171 memcpy(p, &lvalue, 4); /* Server IP address */
1175 memcpy(p, original->data + 24, 4); /* copy gateway IP address */
1177 vp = pairfind(packet->vps, DHCP2ATTR(266));
1179 lvalue = vp->vp_ipaddr;
1181 lvalue = htonl(INADDR_NONE);
1183 memcpy(p, &lvalue, 4);
1188 memcpy(p, original->data + 28, DHCP_CHADDR_LEN);
1190 vp = pairfind(packet->vps, DHCP2ATTR(267));
1192 if (vp->length > DHCP_CHADDR_LEN) {
1193 memcpy(p, vp->vp_octets, DHCP_CHADDR_LEN);
1195 memcpy(p, vp->vp_octets, vp->length);
1199 p += DHCP_CHADDR_LEN;
1201 memset(p, 0, 192); /* bootp legacy */
1204 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER); /* DHCP magic number */
1205 memcpy(p, &lvalue, 4);
1211 if (fr_debug_flag > 1) {
1216 for (i = 0; i < 14; i++) {
1217 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
1219 fr_strerror_printf("Parse error %s", fr_strerror());
1225 vp->vp_integer = p[0];
1230 vp->vp_integer = (p[0] << 8) | p[1];
1234 case PW_TYPE_INTEGER:
1235 memcpy(&vp->vp_integer, p, 4);
1236 vp->vp_integer = ntohl(vp->vp_integer);
1240 case PW_TYPE_IPADDR:
1241 memcpy(&vp->vp_ipaddr, p, 4);
1245 case PW_TYPE_STRING:
1246 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
1247 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
1248 vp->length = strlen(vp->vp_strvalue);
1251 case PW_TYPE_OCTETS: /* only for Client HW Address */
1252 memcpy(vp->vp_octets, p, packet->data[2]);
1253 vp->length = packet->data[2];
1256 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1257 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1258 vp->length = sizeof(vp->vp_ether);
1262 fr_strerror_printf("Internal sanity check failed %d %d", vp->type, __LINE__);
1267 p += dhcp_header_sizes[i];
1269 vp_prints(buffer, sizeof(buffer), vp);
1270 fr_strerror_printf("\t%s", buffer);
1275 * Jump over DHCP magic number, response, etc.
1281 * Before packing the attributes, re-order them so that
1282 * the array ones are all contiguous. This simplifies
1286 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1290 VALUE_PAIR **array, **last;
1292 array = malloc(num_vps * sizeof(VALUE_PAIR *));
1295 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1300 * Sort the attributes.
1302 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *),
1305 last = &packet->vps;
1306 for (i = 0; i < num_vps; i++) {
1308 array[i]->next = NULL;
1309 last = &(array[i]->next);
1314 p[0] = 0x35; /* DHCP-Message-Type */
1316 p[2] = packet->code - PW_DHCP_OFFSET;
1320 * Pack in the attributes.
1324 int num_entries = 1;
1327 uint8_t *plength, *pattr;
1329 if (!IS_DHCP_ATTR(vp)) goto next;
1330 if (vp->attribute == DHCP2ATTR(53)) goto next; /* already done */
1331 if ((vp->attribute > 255) &&
1332 (DHCP_BASE_ATTR(vp->attribute) != PW_DHCP_OPTION_82)) goto next;
1335 length = vp->length;
1337 for (same = vp->next; same != NULL; same = same->next) {
1338 if (same->attribute != vp->attribute) break;
1343 * For client-identifier
1345 if ((vp->type == PW_TYPE_ETHERNET) &&
1346 (vp->length == 6) &&
1347 (num_entries == 1)) {
1348 vp->type = PW_TYPE_OCTETS;
1349 memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1350 vp->vp_octets[0] = 1;
1354 *(p++) = vp->attribute & 0xff;
1356 *(p++) = 0; /* header isn't included in attr length */
1358 for (i = 0; i < num_entries; i++) {
1359 if (fr_debug_flag > 1) {
1360 vp_prints(buffer, sizeof(buffer), vp);
1361 fr_strerror_printf("\t%s", buffer);
1364 if (vp->flags.is_tlv) {
1365 VALUE_PAIR *tlv = fr_dhcp_vp2suboption(vp);
1367 tlv->next = vp->next;
1372 * The encoded flag MUST be set in the vp!
1377 length = fr_dhcp_vp2attr(vp, p, 0);
1380 * This will never happen due to FreeRADIUS
1381 * limitations: sizeof(vp->vp_octets) < 255
1384 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->name);
1389 * More than one attribute of the same type
1390 * in a row: they are packed together
1391 * into the same TLV. If we overflow,
1394 if ((*plength + length) > 255) {
1395 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->name);
1403 (vp->next->attribute == vp->attribute))
1405 } /* loop over num_entries */
1411 p[0] = 0xff; /* end of option option */
1414 dhcp_size = p - packet->data;
1417 * FIXME: if (dhcp_size > mms),
1418 * then we put the extra options into the "sname" and "file"
1419 * fields, AND set the "end option option" in the "options"
1420 * field. We also set the "overload option",
1421 * and put options into the "file" field, followed by
1422 * the "sname" field. Where each option is completely
1423 * enclosed in the "file" and/or "sname" field, AND
1424 * followed by the "end of option", and MUST be followed
1425 * by padding option.
1427 * Yuck. That sucks...
1429 packet->data_len = dhcp_size;
1433 * FIXME: This may set it to broadcast, which we don't
1434 * want. Instead, set it to the real address of the
1437 packet->src_ipaddr = original->dst_ipaddr;
1439 packet->sockfd = original->sockfd;
1442 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1443 memset(packet->data + packet->data_len, 0,
1444 DEFAULT_PACKET_SIZE - packet->data_len);
1445 packet->data_len = DEFAULT_PACKET_SIZE;
1448 if ((fr_debug_flag > 2) && fr_log_fp) {
1449 for (i = 0; i < packet->data_len; i++) {
1450 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1451 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1452 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1454 fprintf(fr_log_fp, "\n");
1459 #endif /* WITH_DHCP */