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 data - (uint8_t *) packet);
174 if ((where + 2 + data[1]) > size) {
175 fr_strerror_printf("Option length overflows field at %u",
176 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);
209 if (!packet) return NULL;
210 memset(packet, 0, sizeof(packet));
212 packet->data = malloc(MAX_PACKET_SIZE);
218 packet->sockfd = sockfd;
219 sizeof_src = sizeof(src);
220 #ifdef WITH_UDPFROMTO
221 sizeof_dst = sizeof(dst);
222 packet->data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
223 (struct sockaddr *)&src, &sizeof_src,
224 (struct sockaddr *)&dst, &sizeof_dst);
226 packet->data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
227 (struct sockaddr *)&src, &sizeof_src);
230 if (packet->data_len <= 0) {
231 fprintf(stderr, "Failed reading DHCP socket: %s", strerror(errno));
236 if (packet->data_len < MIN_PACKET_SIZE) {
237 fprintf(stderr, "DHCP packet is too small (%d < %d)",
238 packet->data_len, MIN_PACKET_SIZE);
243 if (packet->data[0] != 1) {
244 fprintf(stderr, "Cannot receive DHCP server messages");
249 if (packet->data[1] != 1) {
250 fprintf(stderr, "DHCP can only receive ethernet requests, not type %02x",
256 if (packet->data[2] != 6) {
257 fprintf(stderr, "Ethernet HW length is wrong length %d\n",
263 memcpy(&magic, packet->data + 236, 4);
264 magic = ntohl(magic);
265 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
266 fprintf(stderr, "Cannot do BOOTP\n");
272 * Create unique keys for the packet.
274 memcpy(&magic, packet->data + 4, 4);
275 packet->id = ntohl(magic);
277 code = dhcp_get_option((dhcp_packet_t *) packet->data,
278 packet->data_len, 53);
280 fr_strerror_printf("No message-type option was found in the packet");
285 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
286 fr_strerror_printf("Unknown value for message-type option");
291 packet->code = code[2] | PW_DHCP_OFFSET;
294 * Create a unique vector from the MAC address and the
295 * DHCP opcode. This is a hack for the RADIUS
296 * infrastructure in the rest of the server.
298 * Note: packet->data[2] == 6, which is smaller than
299 * sizeof(packet->vector)
301 * FIXME: Look for client-identifier in packet,
304 memset(packet->vector, 0, sizeof(packet->vector));
305 memcpy(packet->vector, packet->data + 28, packet->data[2]);
306 packet->vector[packet->data[2]] = packet->code & 0xff;
309 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
310 * FIXME: for OFFER / ACK : src_port = dst_port - 1
314 * Unique keys are xid, client mac, and client ID?
318 * FIXME: More checks, like DHCP packet type?
321 sizeof_dst = sizeof(dst);
323 #ifndef WITH_UDPFROMTO
325 * This should never fail...
327 getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst);
330 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
331 packet->dst_port = port;
333 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
334 packet->src_port = port;
336 if (fr_debug_flag > 1) {
338 const char *name = type_buf;
339 char src_ip_buf[256], dst_ip_buf[256];
341 if ((packet->code >= PW_DHCP_DISCOVER) &&
342 (packet->code <= PW_DHCP_INFORM)) {
343 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
345 snprintf(type_buf, sizeof(type_buf), "%d",
346 packet->code - PW_DHCP_OFFSET);
349 printf("Received %s of id %08x from %s:%d to %s:%d\n",
350 name, (unsigned int) packet->id,
351 inet_ntop(packet->src_ipaddr.af,
352 &packet->src_ipaddr.ipaddr,
353 src_ip_buf, sizeof(src_ip_buf)),
355 inet_ntop(packet->dst_ipaddr.af,
356 &packet->dst_ipaddr.ipaddr,
357 dst_ip_buf, sizeof(dst_ip_buf)),
367 * Send a DHCP packet.
369 int fr_dhcp_send(RADIUS_PACKET *packet)
371 struct sockaddr_storage dst;
372 socklen_t sizeof_dst;
373 #ifdef WITH_UDPFROMTO
374 struct sockaddr_storage src;
375 socklen_t sizeof_src;
378 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
381 #ifndef WITH_UDPFROMTO
383 * Assume that the packet is encoded before sending it.
385 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
386 (struct sockaddr *)&dst, sizeof_dst);
388 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
391 return sendfromto(packet->sockfd,
392 packet->data, packet->data_len, 0,
393 (struct sockaddr *)&src, sizeof_src,
394 (struct sockaddr *)&dst, sizeof_dst);
399 int fr_dhcp_decode(RADIUS_PACKET *packet)
405 VALUE_PAIR *head, *vp, **tail;
406 VALUE_PAIR *maxms, *mtu;
413 if ((fr_debug_flag > 2) && fr_log_fp) {
414 for (i = 0; i < packet->data_len; i++) {
415 if ((i & 0x0f) == 0x00) fprintf(stderr, "%d: ", i);
416 fprintf(fr_log_fp, "%02x ", packet->data[i]);
417 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
419 fprintf(fr_log_fp, "\n");
422 if (packet->data[1] != 1) {
423 fprintf(stderr, "Packet is not Ethernet: %u\n",
431 for (i = 0; i < 14; i++) {
432 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
434 fprintf(stderr, "Parse error %s\n", fr_strerror());
441 (packet->data[1] == 1) &&
442 (packet->data[2] == 6)) {
443 vp->type = PW_TYPE_ETHERNET;
448 vp->vp_integer = p[0];
453 vp->vp_integer = (p[0] << 8) | p[1];
457 case PW_TYPE_INTEGER:
458 memcpy(&vp->vp_integer, p, 4);
459 vp->vp_integer = ntohl(vp->vp_integer);
464 memcpy(&vp->vp_ipaddr, p, 4);
469 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
470 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
471 vp->length = strlen(vp->vp_strvalue);
472 if (vp->length == 0) {
478 memcpy(vp->vp_octets, p, packet->data[2]);
479 vp->length = packet->data[2];
482 case PW_TYPE_ETHERNET:
483 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
484 vp->length = sizeof(vp->vp_ether);
488 fprintf(stderr, "BAD TYPE %d\n", vp->type);
492 p += dhcp_header_sizes[i];
496 if (fr_debug_flag > 1) {
497 vp_prints(buffer, sizeof(buffer), vp);
498 fprintf(stderr, "\t%s\n", buffer);
505 * Loop over the options.
507 p = packet->data + 240;
508 total = packet->data_len - 240;
511 * FIXME: This should also check sname && file fields.
512 * See the dhcp_get_option() function above.
515 int num_entries, alen;
519 if (*p == 255) break; /* end of options signifier */
522 fprintf(stderr, "Attribute too long %u %u\n",
527 da = dict_attrbyvalue(DHCP2ATTR(p[0]));
529 fprintf(stderr, "Attribute not in our dictionary: %u\n",
544 if (da->flags.array) {
552 if ((alen & 0x01) != 0) goto raw;
553 num_entries = alen / 2;
558 case PW_TYPE_INTEGER:
560 if ((alen & 0x03) != 0) goto raw;
561 num_entries = alen / 4;
566 break; /* really an internal sanity failure */
573 if (alen != 1) goto raw;
577 if (alen != 2) goto raw;
581 case PW_TYPE_INTEGER:
583 if (alen != 4) goto raw;
591 for (i = 0; i < num_entries; i++) {
592 vp = pairmake(da->name, NULL, T_OP_EQ);
594 fprintf(stderr, "Cannot build attribute %s\n",
601 * Hacks for ease of use.
603 if ((da->attr == DHCP2ATTR(0x3d)) &&
605 (alen == 7) && (*p == 1) && (num_entries == 1)) {
606 vp->type = PW_TYPE_ETHERNET;
607 memcpy(vp->vp_octets, p + 1, 6);
612 vp->vp_integer = p[0];
616 vp->vp_integer = (p[0] << 8) | p[1];
619 case PW_TYPE_INTEGER:
620 memcpy(&vp->vp_integer, p, 4);
621 vp->vp_integer = ntohl(vp->vp_integer);
625 memcpy(&vp->vp_ipaddr, p , 4);
630 memcpy(vp->vp_strvalue, p , alen);
631 vp->vp_strvalue[alen] = '\0';
635 vp = pairmake(da->name, NULL, T_OP_EQ);
637 fprintf(stderr, "Cannot build attribute %s\n", fr_strerror());
642 vp->type = PW_TYPE_OCTETS;
645 memcpy(vp->vp_octets, p, alen);
649 fprintf(stderr, "Internal sanity check %d %d\n", vp->type, __LINE__);
652 } /* switch over type */
656 if (fr_debug_flag > 1) {
657 vp_prints(buffer, sizeof(buffer), vp);
658 fprintf(stderr, "\t%s\n", buffer);
664 } /* loop over array entries */
667 total -= (alen * num_entries);
671 * If DHCP request, set ciaddr to zero.
675 * Set broadcast flag for broken vendors, but only if
678 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
679 if (giaddr == htonl(INADDR_ANY)) {
681 * DHCP Opcode is request
683 vp = pairfind(head, DHCP2ATTR(256));
684 if (vp && vp->lvalue == 3) {
686 * Vendor is "MSFT 98"
688 vp = pairfind(head, DHCP2ATTR(63));
689 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
690 vp = pairfind(head, DHCP2ATTR(262));
693 * Reply should be broadcast.
695 if (vp) vp->lvalue |= 0x8000;
696 packet->data[10] |= 0x80;
702 * FIXME: Nuke attributes that aren't used in the normal
703 * header for discover/requests.
708 * Client can request a LARGER size, but not a smaller
709 * one. They also cannot request a size larger than MTU.
711 maxms = pairfind(packet->vps, DHCP2ATTR(57));
712 mtu = pairfind(packet->vps, DHCP2ATTR(26));
714 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
715 fprintf(stderr, "DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
719 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
720 fprintf(stderr, "DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
721 maxms->vp_integer = DEFAULT_PACKET_SIZE;
724 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
725 fprintf(stderr, "DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
726 maxms->vp_integer = mtu->vp_integer;
729 if (fr_debug_flag) fflush(stdout);
735 static int attr_cmp(const void *one, const void *two)
737 const VALUE_PAIR * const *a = one;
738 const VALUE_PAIR * const *b = two;
741 * DHCP-Message-Type is first, for simplicity.
743 if (((*a)->attribute == DHCP2ATTR(53)) &&
744 (*b)->attribute != DHCP2ATTR(53)) return -1;
747 * Relay-Agent is last
749 if (((*a)->attribute == DHCP2ATTR(82)) &&
750 (*b)->attribute != DHCP2ATTR(82)) return +1;
752 return ((*a)->attribute - (*b)->attribute);
756 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, size_t room)
764 room = room; /* -Wunused */
767 * Search for all attributes of the same
768 * type, and pack them into the same
774 *p = vp->vp_integer & 0xff;
779 p[0] = (vp->vp_integer >> 8) & 0xff;
780 p[1] = vp->vp_integer & 0xff;
783 case PW_TYPE_INTEGER:
785 lvalue = htonl(vp->vp_integer);
786 memcpy(p, &lvalue, 4);
791 memcpy(p, &vp->vp_ipaddr, 4);
794 case PW_TYPE_ETHERNET:
796 memcpy(p, &vp->vp_ether, 6);
800 memcpy(p, vp->vp_strvalue, vp->length);
805 memcpy(p, vp->vp_octets, vp->length);
810 fprintf(stderr, "BAD TYPE2 %d\n", vp->type);
818 int fr_dhcp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
823 uint32_t lvalue, mms;
824 size_t dhcp_size, length;
828 if (packet->data) return 0;
830 packet->data = malloc(MAX_PACKET_SIZE);
831 if (!packet->data) return -1;
833 packet->data_len = MAX_PACKET_SIZE;
835 if (packet->code == 0) packet->code = PW_DHCP_NAK;
838 * FIXME: allow it to send client packets.
841 fr_strerror_printf("Need original to send response!");
845 packet->dst_ipaddr.af = AF_INET;
846 packet->src_ipaddr.af = AF_INET;
848 packet->dst_port = original->src_port;
849 packet->src_port = original->dst_port;
852 * Note that for DHCP, we NEVER send the response to the
853 * source IP address of the request. It may have
854 * traversed multiple relays, and we need to send the request
855 * to the relay closest to the client.
857 * if giaddr, send to giaddr.
858 * if NAK, send broadcast packet
859 * if ciaddr, unicast to ciaddr
860 * if flags & 0x8000, broadcast (client request)
861 * if sent from 0.0.0.0, broadcast response
862 * unicast to client yiaddr
866 * FIXME: alignment issues. We likely don't want to
867 * de-reference the packet structure directly..
869 dhcp = (dhcp_packet_t *) original->data;
871 if (dhcp->giaddr != htonl(INADDR_ANY)) {
872 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->giaddr;
874 if (dhcp->giaddr != htonl(INADDR_LOOPBACK)) {
875 packet->dst_port = original->dst_port;
877 packet->dst_port = original->src_port; /* debugging */
880 } else if (packet->code == PW_DHCP_NAK) {
881 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
883 } else if (dhcp->ciaddr != htonl(INADDR_ANY)) {
884 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->ciaddr;
886 } else if ((dhcp->flags & 0x8000) != 0) {
887 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
889 } else if (packet->dst_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY)) {
890 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
892 } else if (dhcp->yiaddr != htonl(INADDR_ANY)) {
893 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->yiaddr;
896 /* leave destination IP alone. */
900 * Rewrite the source IP to be our own, if we know it.
902 if (packet->src_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_BROADCAST)) {
903 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
906 if (fr_debug_flag > 1) {
908 const char *name = type_buf;
909 char src_ip_buf[256], dst_ip_buf[256];
911 if ((packet->code >= PW_DHCP_DISCOVER) &&
912 (packet->code <= PW_DHCP_INFORM)) {
913 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
915 snprintf(type_buf, sizeof(type_buf), "%d",
916 packet->code - PW_DHCP_OFFSET);
919 printf("Sending %s of id %08x from %s:%d to %s:%d\n",
920 name, (unsigned int) packet->id,
921 inet_ntop(packet->src_ipaddr.af,
922 &packet->src_ipaddr.ipaddr,
923 src_ip_buf, sizeof(src_ip_buf)),
925 inet_ntop(packet->dst_ipaddr.af,
926 &packet->dst_ipaddr.ipaddr,
927 dst_ip_buf, sizeof(dst_ip_buf)),
934 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
937 * Client can request a LARGER size, but not a smaller
938 * one. They also cannot request a size larger than MTU.
940 vp = pairfind(original->vps, DHCP2ATTR(57));
941 if (vp && (vp->vp_integer > mms)) {
942 mms = vp->vp_integer;
944 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
948 * RFC 3118: Authentication option.
950 vp = pairfind(packet->vps, DHCP2ATTR(90));
952 if (vp->length < 2) {
953 memset(vp->vp_octets + vp->length, 0,
958 if (vp->length < 3) {
961 gettimeofday(&tv, NULL);
962 vp->vp_octets[2] = 0;
963 timeval2ntp(&tv, vp->vp_octets + 3);
968 * Configuration token (clear-text token)
970 if (vp->vp_octets[0] == 0) {
972 vp->vp_octets[1] = 0;
974 pass = pairfind(packet->vps, PW_CLEARTEXT_PASSWORD);
976 length = pass->length;
977 if ((length + 11) > sizeof(vp->vp_octets)) {
978 length -= ((length + 11) - sizeof(vp->vp_octets));
980 memcpy(vp->vp_octets + 11, pass->vp_strvalue,
982 vp->length = length + 11;
985 memset(vp->vp_octets + 11, 0, 8);
988 } else { /* we don't support this type! */
989 fprintf(stderr, "DHCP-Authentication %d unsupported\n",
995 *p++ = 1; /* client message */
997 *p++ = 2; /* server message */
999 *p++ = 1; /* hardware type = ethernet */
1000 *p++ = original->data[2];
1001 *p++ = 0; /* hops */
1003 if (!original) { /* Xid */
1005 memcpy(p, &lvalue, 4);
1007 memcpy(p, original->data + 4, 4);
1011 memset(p, 0, 2); /* secs are zero */
1014 memcpy(p, original->data + 10, 6); /* copy flags && ciaddr */
1017 * Allow the admin to set the broadcast flag.
1019 vp = pairfind(packet->vps, DHCP2ATTR(262));
1021 p[0] |= (vp->vp_integer & 0xff00) >> 8;
1022 p[1] |= (vp->vp_integer & 0xff);
1028 * Set client IP address.
1030 vp = pairfind(packet->vps, DHCP2ATTR(264)); /* Your IP address */
1032 lvalue = vp->vp_ipaddr;
1034 lvalue = htonl(INADDR_ANY);
1036 memcpy(p, &lvalue, 4); /* your IP address */
1039 vp = pairfind(packet->vps, DHCP2ATTR(265)); /* server IP address */
1040 if (!vp) vp = pairfind(packet->vps, DHCP2ATTR(54)); /* identifier */
1042 lvalue = vp->vp_ipaddr;
1044 lvalue = htonl(INADDR_ANY);
1046 memcpy(p, &lvalue, 4); /* Server IP address */
1049 memcpy(p, original->data + 24, 4); /* copy gateway IP address */
1052 memcpy(p, original->data + 28, DHCP_CHADDR_LEN);
1053 p += DHCP_CHADDR_LEN;
1055 memset(p, 0, 192); /* bootp legacy */
1058 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER); /* DHCP magic number */
1059 memcpy(p, &lvalue, 4);
1065 if (fr_debug_flag > 1) {
1070 for (i = 0; i < 14; i++) {
1071 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
1073 fprintf(stderr, "Parse error %s\n", fr_strerror());
1079 vp->vp_integer = p[0];
1084 vp->vp_integer = (p[0] << 8) | p[1];
1088 case PW_TYPE_INTEGER:
1089 memcpy(&vp->vp_integer, p, 4);
1090 vp->vp_integer = ntohl(vp->vp_integer);
1094 case PW_TYPE_IPADDR:
1095 memcpy(&vp->vp_ipaddr, p, 4);
1099 case PW_TYPE_STRING:
1100 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
1101 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
1102 vp->length = strlen(vp->vp_strvalue);
1105 case PW_TYPE_OCTETS: /* only for Client HW Address */
1106 memcpy(vp->vp_octets, p, packet->data[2]);
1107 vp->length = packet->data[2];
1110 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1111 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1112 vp->length = sizeof(vp->vp_ether);
1116 fprintf(stderr, "Internal sanity check failed %d %d\n", vp->type, __LINE__);
1121 p += dhcp_header_sizes[i];
1123 vp_prints(buffer, sizeof(buffer), vp);
1124 fprintf(stderr, "\t%s\n", buffer);
1129 * Jump over DHCP magic number, response, etc.
1135 * Before packing the attributes, re-order them so that
1136 * the array ones are all contiguous. This simplifies
1140 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1144 VALUE_PAIR **array, **last;
1146 array = malloc(num_vps * sizeof(VALUE_PAIR *));
1149 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1154 * Sort the attributes.
1156 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *),
1159 last = &packet->vps;
1160 for (i = 0; i < num_vps; i++) {
1162 array[i]->next = NULL;
1163 last = &(array[i]->next);
1168 p[0] = 0x35; /* DHCP-Message-Type */
1170 p[2] = packet->code - PW_DHCP_OFFSET;
1174 * Pack in the attributes.
1178 int num_entries = 1;
1181 uint8_t *plength, *pattr;
1183 if (!IS_DHCP_ATTR(vp)) goto next;
1184 if (vp->attribute == DHCP2ATTR(53)) goto next; /* already done */
1185 if (((vp->attribute & 0xffff) > 255) &&
1186 (DHCP_BASE_ATTR(vp->attribute) != PW_DHCP_OPTION_82)) goto next;
1188 length = vp->length;
1190 for (same = vp->next; same != NULL; same = same->next) {
1191 if (same->attribute != vp->attribute) break;
1196 * For client-identifier
1198 if ((vp->type == PW_TYPE_ETHERNET) &&
1199 (vp->length == 6) &&
1200 (num_entries == 1)) {
1201 vp->type = PW_TYPE_OCTETS;
1202 memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1203 vp->vp_octets[0] = 1;
1207 *(p++) = vp->attribute & 0xff;
1209 *(p++) = 0; /* header isn't included in attr length */
1211 for (i = 0; i < num_entries; i++) {
1212 if (fr_debug_flag > 1) {
1213 vp_prints(buffer, sizeof(buffer), vp);
1214 fprintf(stderr, "\t%s\n", buffer);
1217 length = fr_dhcp_vp2attr(vp, p, 0);
1220 * This will never happen due to FreeRADIUS
1221 * limitations: sizeof(vp->vp_octets) < 255
1224 fprintf(stderr, "WARNING Ignoring too long attribute %s!\n", vp->name);
1229 * More than one attribute of the same type
1230 * in a row: they are packed together
1231 * into the same TLV. If we overflow,
1234 if ((*plength + length) > 255) {
1235 fprintf(stderr, "WARNING Ignoring too long attribute %s!\n", vp->name);
1243 (vp->next->attribute == vp->attribute))
1245 } /* loop over num_entries */
1247 if (DHCP_BASE_ATTR(vp->attribute) == PW_DHCP_OPTION_82) {
1248 plength[2] = plength[0] - 2;
1255 p[0] = 0xff; /* end of option option */
1258 dhcp_size = p - packet->data;
1261 * FIXME: if (dhcp_size > mms),
1262 * then we put the extra options into the "sname" and "file"
1263 * fields, AND set the "end option option" in the "options"
1264 * field. We also set the "overload option",
1265 * and put options into the "file" field, followed by
1266 * the "sname" field. Where each option is completely
1267 * enclosed in the "file" and/or "sname" field, AND
1268 * followed by the "end of option", and MUST be followed
1269 * by padding option.
1271 * Yuck. That sucks...
1273 packet->data_len = dhcp_size;
1276 * FIXME: This may set it to broadcast, which we don't
1277 * want. Instead, set it to the real address of the
1280 packet->src_ipaddr = original->dst_ipaddr;
1282 packet->sockfd = original->sockfd;
1284 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1285 memset(packet->data + packet->data_len, 0,
1286 DEFAULT_PACKET_SIZE - packet->data_len);
1287 packet->data_len = DEFAULT_PACKET_SIZE;
1290 if ((fr_debug_flag > 2) && fr_log_fp) {
1291 for (i = 0; i < packet->data_len; i++) {
1292 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1293 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1294 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1296 fprintf(fr_log_fp, "\n");
1301 #endif /* WITH_DHCP */