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",
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;
526 if ((fr_debug_flag > 2) && fr_log_fp) {
527 for (i = 0; i < packet->data_len; i++) {
528 if ((i & 0x0f) == 0x00) fr_strerror_printf("%d: ", i);
529 fprintf(fr_log_fp, "%02x ", packet->data[i]);
530 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
532 fprintf(fr_log_fp, "\n");
535 if (packet->data[1] != 1) {
536 fr_strerror_printf("Packet is not Ethernet: %u",
544 for (i = 0; i < 14; i++) {
545 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
547 fr_strerror_printf("Parse error %s", fr_strerror());
553 (packet->data[1] == 1) &&
554 (packet->data[2] == 6)) {
555 vp->type = PW_TYPE_ETHERNET;
560 vp->vp_integer = p[0];
565 vp->vp_integer = (p[0] << 8) | p[1];
569 case PW_TYPE_INTEGER:
570 memcpy(&vp->vp_integer, p, 4);
571 vp->vp_integer = ntohl(vp->vp_integer);
576 memcpy(&vp->vp_ipaddr, p, 4);
581 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
582 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
583 vp->length = strlen(vp->vp_strvalue);
584 if (vp->length == 0) {
590 memcpy(vp->vp_octets, p, packet->data[2]);
591 vp->length = packet->data[2];
594 case PW_TYPE_ETHERNET:
595 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
596 vp->length = sizeof(vp->vp_ether);
600 fr_strerror_printf("BAD TYPE %d", vp->type);
604 p += dhcp_header_sizes[i];
608 if (fr_debug_flag > 1) {
609 vp_prints(buffer, sizeof(buffer), vp);
610 fr_strerror_printf("\t%s", buffer);
617 * Loop over the options.
619 next = packet->data + 240;
622 * FIXME: This should also check sname && file fields.
623 * See the dhcp_get_option() function above.
625 while (next < (packet->data + packet->data_len)) {
626 int num_entries, alen;
632 if (*p == 255) break; /* end of options signifier */
633 if ((p + 2) > (packet->data + packet->data_len)) break;
638 fr_strerror_printf("Attribute too long %u %u",
643 da = dict_attrbyvalue(DHCP2ATTR(p[0]));
645 fr_strerror_printf("Attribute not in our dictionary: %u",
656 * Could be an array of bytes, integers, etc.
658 if (da->flags.array) {
665 case PW_TYPE_SHORT: /* ignore any trailing data */
666 num_entries = alen >> 1;
671 case PW_TYPE_INTEGER:
672 case PW_TYPE_DATE: /* ignore any trailing data */
673 num_entries = alen >> 2;
679 break; /* really an internal sanity failure */
684 * Loop over all of the entries, building VPs
686 for (i = 0; i < num_entries; i++) {
687 vp = pairmake(da->name, NULL, T_OP_EQ);
689 fr_strerror_printf("Cannot build attribute %s",
696 * Hack for ease of use.
698 if ((da->attr == DHCP2ATTR(0x3d)) &&
700 (alen == 7) && (*p == 1) && (num_entries == 1)) {
701 vp->type = PW_TYPE_ETHERNET;
702 memcpy(vp->vp_octets, p + 1, 6);
705 } else if (fr_dhcp_attr2vp(vp, p, alen) < 0) {
711 if (fr_debug_flag > 1) {
712 vp_prints(buffer, sizeof(buffer), vp);
713 fr_strerror_printf("\t%s", buffer);
717 while (*tail) tail = &vp->next;
719 } /* loop over array entries */
720 } /* loop over the entire packet */
723 * If DHCP request, set ciaddr to zero.
727 * Set broadcast flag for broken vendors, but only if
730 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
731 if (giaddr == htonl(INADDR_ANY)) {
733 * DHCP Opcode is request
735 vp = pairfind(head, DHCP2ATTR(256));
736 if (vp && vp->lvalue == 3) {
738 * Vendor is "MSFT 98"
740 vp = pairfind(head, DHCP2ATTR(63));
741 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
742 vp = pairfind(head, DHCP2ATTR(262));
745 * Reply should be broadcast.
747 if (vp) vp->lvalue |= 0x8000;
748 packet->data[10] |= 0x80;
754 * FIXME: Nuke attributes that aren't used in the normal
755 * header for discover/requests.
760 * Client can request a LARGER size, but not a smaller
761 * one. They also cannot request a size larger than MTU.
763 maxms = pairfind(packet->vps, DHCP2ATTR(57));
764 mtu = pairfind(packet->vps, DHCP2ATTR(26));
766 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
767 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
771 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
772 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
773 maxms->vp_integer = DEFAULT_PACKET_SIZE;
776 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
777 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
778 maxms->vp_integer = mtu->vp_integer;
781 if (fr_debug_flag) fflush(stdout);
787 static int attr_cmp(const void *one, const void *two)
789 const VALUE_PAIR * const *a = one;
790 const VALUE_PAIR * const *b = two;
793 * DHCP-Message-Type is first, for simplicity.
795 if (((*a)->attribute == DHCP2ATTR(53)) &&
796 (*b)->attribute != DHCP2ATTR(53)) return -1;
799 * Relay-Agent is last
801 if (((*a)->attribute == DHCP2ATTR(82)) &&
802 (*b)->attribute != DHCP2ATTR(82)) return +1;
804 return ((*a)->attribute - (*b)->attribute);
808 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, size_t room)
816 room = room; /* -Wunused */
819 * Search for all attributes of the same
820 * type, and pack them into the same
826 *p = vp->vp_integer & 0xff;
831 p[0] = (vp->vp_integer >> 8) & 0xff;
832 p[1] = vp->vp_integer & 0xff;
835 case PW_TYPE_INTEGER:
837 lvalue = htonl(vp->vp_integer);
838 memcpy(p, &lvalue, 4);
843 memcpy(p, &vp->vp_ipaddr, 4);
846 case PW_TYPE_ETHERNET:
848 memcpy(p, &vp->vp_ether, 6);
852 memcpy(p, vp->vp_strvalue, vp->length);
856 case PW_TYPE_TLV: /* FIXME: split it on 255? */
857 memcpy(p, vp->vp_tlv, vp->length);
862 memcpy(p, vp->vp_octets, vp->length);
867 fr_strerror_printf("BAD TYPE2 %d", vp->type);
875 static VALUE_PAIR *fr_dhcp_vp2suboption(VALUE_PAIR *vps)
878 unsigned int attribute;
880 VALUE_PAIR *vp, *tlv;
882 attribute = vps->attribute & 0xffff00ff;
884 tlv = paircreate(attribute, PW_TYPE_TLV);
885 if (!tlv) return NULL;
888 for (vp = vps; vp != NULL; vp = vp->next) {
890 * Group the attributes ONLY until we see a
893 if (!vp->flags.is_tlv ||
895 ((vp->attribute & 0xffff00ff) != attribute)) {
899 tlv->length += vp->length + 2;
907 tlv->vp_tlv = malloc(tlv->length);
914 for (vp = vps; vp != NULL; vp = vp->next) {
915 if (!vp->flags.is_tlv ||
917 ((vp->attribute & 0xffff00ff) != attribute)) {
921 length = fr_dhcp_vp2attr(vp, ptr + 2,
922 tlv->vp_tlv + tlv->length - ptr);
923 if (length > 255) return NULL;
926 * Pack the attribute.
928 ptr[0] = (vp->attribute & 0xff00) >> 8;
932 vp->flags.encoded = 1;
939 int fr_dhcp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
944 uint32_t lvalue, mms;
945 size_t dhcp_size, length;
949 if (packet->data) return 0;
951 packet->data = malloc(MAX_PACKET_SIZE);
952 if (!packet->data) return -1;
954 packet->data_len = MAX_PACKET_SIZE;
956 if (packet->code == 0) packet->code = PW_DHCP_NAK;
959 * If there's a request, use it as a template.
960 * Otherwise, assume that the caller has set up
961 * everything appropriately.
964 packet->dst_ipaddr.af = AF_INET;
965 packet->src_ipaddr.af = AF_INET;
967 packet->dst_port = original->src_port;
968 packet->src_port = original->dst_port;
971 * Note that for DHCP, we NEVER send the response
972 * to the source IP address of the request. It
973 * may have traversed multiple relays, and we
974 * need to send the request to the relay closest
977 * if giaddr, send to giaddr.
978 * if NAK, send broadcast packet
979 * if ciaddr, unicast to ciaddr
980 * if flags & 0x8000, broadcast (client request)
981 * if sent from 0.0.0.0, broadcast response
982 * unicast to client yiaddr
986 * FIXME: alignment issues. We likely don't want to
987 * de-reference the packet structure directly..
989 dhcp = (dhcp_packet_t *) original->data;
991 if (dhcp->giaddr != htonl(INADDR_ANY)) {
992 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->giaddr;
994 if (dhcp->giaddr != htonl(INADDR_LOOPBACK)) {
995 packet->dst_port = original->dst_port;
997 packet->dst_port = original->src_port; /* debugging */
1000 } else if (packet->code == PW_DHCP_NAK) {
1001 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1003 } else if (dhcp->ciaddr != htonl(INADDR_ANY)) {
1004 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->ciaddr;
1006 } else if ((dhcp->flags & 0x8000) != 0) {
1007 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1009 } else if (packet->dst_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY)) {
1010 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1012 } else if (dhcp->yiaddr != htonl(INADDR_ANY)) {
1013 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->yiaddr;
1016 /* leave destination IP alone. */
1020 * Rewrite the source IP to be our own, if we know it.
1022 if (packet->src_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_BROADCAST)) {
1023 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1026 memset(packet->data, 0, packet->data_len);
1029 if (fr_debug_flag > 1) {
1031 const char *name = type_buf;
1032 char src_ip_buf[256], dst_ip_buf[256];
1034 if ((packet->code >= PW_DHCP_DISCOVER) &&
1035 (packet->code <= PW_DHCP_INFORM)) {
1036 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1038 snprintf(type_buf, sizeof(type_buf), "%d",
1039 packet->code - PW_DHCP_OFFSET);
1042 DEBUG("Sending %s of id %08x from %s:%d to %s:%d",
1043 name, (unsigned int) packet->id,
1044 inet_ntop(packet->src_ipaddr.af,
1045 &packet->src_ipaddr.ipaddr,
1046 src_ip_buf, sizeof(src_ip_buf)),
1048 inet_ntop(packet->dst_ipaddr.af,
1049 &packet->dst_ipaddr.ipaddr,
1050 dst_ip_buf, sizeof(dst_ip_buf)),
1057 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1061 * Clients can request a LARGER size, but not a
1062 * smaller one. They also cannot request a size
1065 vp = pairfind(original->vps, DHCP2ATTR(57));
1066 if (vp && (vp->vp_integer > mms)) {
1067 mms = vp->vp_integer;
1069 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1074 * RFC 3118: Authentication option.
1076 vp = pairfind(packet->vps, DHCP2ATTR(90));
1078 if (vp->length < 2) {
1079 memset(vp->vp_octets + vp->length, 0,
1084 if (vp->length < 3) {
1087 gettimeofday(&tv, NULL);
1088 vp->vp_octets[2] = 0;
1089 timeval2ntp(&tv, vp->vp_octets + 3);
1094 * Configuration token (clear-text token)
1096 if (vp->vp_octets[0] == 0) {
1098 vp->vp_octets[1] = 0;
1100 pass = pairfind(packet->vps, PW_CLEARTEXT_PASSWORD);
1102 length = pass->length;
1103 if ((length + 11) > sizeof(vp->vp_octets)) {
1104 length -= ((length + 11) - sizeof(vp->vp_octets));
1106 memcpy(vp->vp_octets + 11, pass->vp_strvalue,
1108 vp->length = length + 11;
1110 vp->length = 11 + 8;
1111 memset(vp->vp_octets + 11, 0, 8);
1112 vp->length = 11 + 8;
1114 } else { /* we don't support this type! */
1115 fr_strerror_printf("DHCP-Authentication %d unsupported",
1121 *p++ = 1; /* client message */
1123 *p++ = 2; /* server message */
1125 *p++ = 1; /* hardware type = ethernet */
1126 *p++ = 6; /* 6 bytes of ethernet */
1127 *p++ = 0; /* hops */
1129 if (original) { /* Xid */
1130 memcpy(p, original->data + 4, 4);
1133 memcpy(p, &lvalue, 4);
1137 memset(p, 0, 2); /* secs are zero */
1141 memcpy(p, original->data + 10, 6); /* copy flags && ciaddr */
1145 * Allow the admin to set the broadcast flag.
1147 vp = pairfind(packet->vps, DHCP2ATTR(262));
1149 p[0] |= (vp->vp_integer & 0xff00) >> 8;
1150 p[1] |= (vp->vp_integer & 0xff);
1156 * Set client IP address.
1158 vp = pairfind(packet->vps, DHCP2ATTR(264)); /* Your IP address */
1160 lvalue = vp->vp_ipaddr;
1162 lvalue = htonl(INADDR_ANY);
1164 memcpy(p, &lvalue, 4); /* your IP address */
1167 vp = pairfind(packet->vps, DHCP2ATTR(265)); /* server IP address */
1168 if (!vp) vp = pairfind(packet->vps, DHCP2ATTR(54)); /* identifier */
1170 lvalue = vp->vp_ipaddr;
1172 lvalue = htonl(INADDR_ANY);
1174 memcpy(p, &lvalue, 4); /* Server IP address */
1178 memcpy(p, original->data + 24, 4); /* copy gateway IP address */
1180 vp = pairfind(packet->vps, DHCP2ATTR(266));
1182 lvalue = vp->vp_ipaddr;
1184 lvalue = htonl(INADDR_NONE);
1186 memcpy(p, &lvalue, 4);
1191 memcpy(p, original->data + 28, DHCP_CHADDR_LEN);
1193 vp = pairfind(packet->vps, DHCP2ATTR(267));
1195 if (vp->length > DHCP_CHADDR_LEN) {
1196 memcpy(p, vp->vp_octets, DHCP_CHADDR_LEN);
1198 memcpy(p, vp->vp_octets, vp->length);
1202 p += DHCP_CHADDR_LEN;
1204 memset(p, 0, 192); /* bootp legacy */
1207 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER); /* DHCP magic number */
1208 memcpy(p, &lvalue, 4);
1214 if (fr_debug_flag > 1) {
1219 for (i = 0; i < 14; i++) {
1220 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
1222 fr_strerror_printf("Parse error %s", fr_strerror());
1228 vp->vp_integer = p[0];
1233 vp->vp_integer = (p[0] << 8) | p[1];
1237 case PW_TYPE_INTEGER:
1238 memcpy(&vp->vp_integer, p, 4);
1239 vp->vp_integer = ntohl(vp->vp_integer);
1243 case PW_TYPE_IPADDR:
1244 memcpy(&vp->vp_ipaddr, p, 4);
1248 case PW_TYPE_STRING:
1249 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
1250 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
1251 vp->length = strlen(vp->vp_strvalue);
1254 case PW_TYPE_OCTETS: /* only for Client HW Address */
1255 memcpy(vp->vp_octets, p, packet->data[2]);
1256 vp->length = packet->data[2];
1259 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1260 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1261 vp->length = sizeof(vp->vp_ether);
1265 fr_strerror_printf("Internal sanity check failed %d %d", vp->type, __LINE__);
1270 p += dhcp_header_sizes[i];
1272 vp_prints(buffer, sizeof(buffer), vp);
1273 fr_strerror_printf("\t%s", buffer);
1278 * Jump over DHCP magic number, response, etc.
1284 * Before packing the attributes, re-order them so that
1285 * the array ones are all contiguous. This simplifies
1289 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1293 VALUE_PAIR **array, **last;
1295 array = malloc(num_vps * sizeof(VALUE_PAIR *));
1298 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1303 * Sort the attributes.
1305 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *),
1308 last = &packet->vps;
1309 for (i = 0; i < num_vps; i++) {
1311 array[i]->next = NULL;
1312 last = &(array[i]->next);
1317 p[0] = 0x35; /* DHCP-Message-Type */
1319 p[2] = packet->code - PW_DHCP_OFFSET;
1323 * Pack in the attributes.
1327 int num_entries = 1;
1330 uint8_t *plength, *pattr;
1332 if (!IS_DHCP_ATTR(vp)) goto next;
1333 if (vp->attribute == DHCP2ATTR(53)) goto next; /* already done */
1334 if (((vp->attribute & 0xffff) > 255) &&
1335 (DHCP_BASE_ATTR(vp->attribute) != PW_DHCP_OPTION_82)) goto next;
1337 length = vp->length;
1339 for (same = vp->next; same != NULL; same = same->next) {
1340 if (same->attribute != vp->attribute) break;
1345 * For client-identifier
1347 if ((vp->type == PW_TYPE_ETHERNET) &&
1348 (vp->length == 6) &&
1349 (num_entries == 1)) {
1350 vp->type = PW_TYPE_OCTETS;
1351 memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1352 vp->vp_octets[0] = 1;
1356 *(p++) = vp->attribute & 0xff;
1358 *(p++) = 0; /* header isn't included in attr length */
1360 for (i = 0; i < num_entries; i++) {
1361 if (fr_debug_flag > 1) {
1362 vp_prints(buffer, sizeof(buffer), vp);
1363 fr_strerror_printf("\t%s", buffer);
1366 if (vp->flags.is_tlv) {
1367 VALUE_PAIR *tlv = fr_dhcp_vp2suboption(vp);
1369 tlv->next = vp->next;
1374 * The encoded flag MUST be set in the vp!
1379 length = fr_dhcp_vp2attr(vp, p, 0);
1382 * This will never happen due to FreeRADIUS
1383 * limitations: sizeof(vp->vp_octets) < 255
1386 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->name);
1391 * More than one attribute of the same type
1392 * in a row: they are packed together
1393 * into the same TLV. If we overflow,
1396 if ((*plength + length) > 255) {
1397 fr_strerror_printf("WARNING Ignoring too long attribute %s!", vp->name);
1405 (vp->next->attribute == vp->attribute))
1407 } /* loop over num_entries */
1413 p[0] = 0xff; /* end of option option */
1416 dhcp_size = p - packet->data;
1419 * FIXME: if (dhcp_size > mms),
1420 * then we put the extra options into the "sname" and "file"
1421 * fields, AND set the "end option option" in the "options"
1422 * field. We also set the "overload option",
1423 * and put options into the "file" field, followed by
1424 * the "sname" field. Where each option is completely
1425 * enclosed in the "file" and/or "sname" field, AND
1426 * followed by the "end of option", and MUST be followed
1427 * by padding option.
1429 * Yuck. That sucks...
1431 packet->data_len = dhcp_size;
1435 * FIXME: This may set it to broadcast, which we don't
1436 * want. Instead, set it to the real address of the
1439 packet->src_ipaddr = original->dst_ipaddr;
1441 packet->sockfd = original->sockfd;
1444 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1445 memset(packet->data + packet->data_len, 0,
1446 DEFAULT_PACKET_SIZE - packet->data_len);
1447 packet->data_len = DEFAULT_PACKET_SIZE;
1450 if ((fr_debug_flag > 2) && fr_log_fp) {
1451 for (i = 0; i < packet->data_len; i++) {
1452 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1453 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1454 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1456 fprintf(fr_log_fp, "\n");
1461 #endif /* WITH_DHCP */