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 typedef struct dhcp_packet_t {
44 uint16_t secs; /* 8 */
46 uint32_t ciaddr; /* 12 */
47 uint32_t yiaddr; /* 16 */
48 uint32_t siaddr; /* 20 */
49 uint32_t giaddr; /* 24 */
50 uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
51 char sname[DHCP_SNAME_LEN]; /* 44 */
52 char file[DHCP_FILE_LEN]; /* 108 */
53 uint32_t option_format; /* 236 */
54 uint8_t options[DHCP_VEND_LEN];
58 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 DISCOVER
59 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 OFFER
60 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 REQUEST
61 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 ACK
63 static const char *dhcp_header_names[] = {
66 "DHCP-Hardware-Address-Length",
68 "DHCP-Transaction-Id",
69 "DHCP-Number-of-Seconds",
71 "DHCP-Client-IP-Address",
72 "DHCP-Your-IP-Address",
73 "DHCP-Server-IP-Address",
74 "DHCP-Gateway-IP-Address",
75 "DHCP-Client-Hardware-Address",
76 "DHCP-Server-Host-Name",
82 static const char *dhcp_message_types[] = {
95 static int dhcp_header_sizes[] = {
106 * Some clients silently ignore responses less than 300 bytes.
108 #define MIN_PACKET_SIZE (244)
109 #define DEFAULT_PACKET_SIZE (300)
110 #define MAX_PACKET_SIZE (1500 - 40)
112 static int getcode(const uint8_t *data, size_t data_len, int *code)
116 end = data + data_len;
120 * End of packet or end of options
122 if ((p[0] == 0) || (p[0] == 255)) return 0;
125 * Not enough room for 0x3501cc
127 if ((end - p) < 3) return 0; /* t l v */
130 * Option is larger than the packet.
132 if ((p + p[1] + 2) > end) return 0;
135 * Found it. Ensure it's well formed.
138 if ((p[1] != 1) || (p[2] == 0) || (p[2] > 8)) {
152 * DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
155 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
158 struct sockaddr_storage src;
159 struct sockaddr_storage dst;
160 socklen_t sizeof_src;
161 socklen_t sizeof_dst;
162 RADIUS_PACKET *packet;
165 packet = rad_alloc(0);
166 if (!packet) return NULL;
167 memset(packet, 0, sizeof(packet));
169 packet->data = malloc(MAX_PACKET_SIZE);
175 packet->sockfd = sockfd;
176 sizeof_src = sizeof(src);
177 packet->data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
178 (struct sockaddr *)&src, &sizeof_src);
179 if (packet->data_len <= 0) {
180 fprintf(stderr, "Failed reading DHCP socket: %s", strerror(errno));
185 if (packet->data_len < MIN_PACKET_SIZE) {
186 fprintf(stderr, "DHCP packet is too small (%d < %d)",
187 packet->data_len, MIN_PACKET_SIZE);
192 if (packet->data[0] != 1) {
193 fprintf(stderr, "Cannot receive DHCP server messages");
198 if (packet->data[1] != 1) {
199 fprintf(stderr, "DHCP can only receive ethernet requests, not type %02x",
205 if (packet->data[2] != 6) {
206 fprintf(stderr, "Ethernet HW length is wrong length %d\n",
212 memcpy(&magic, packet->data + 236, 4);
213 magic = ntohl(magic);
214 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
215 fprintf(stderr, "Cannot do BOOTP\n");
221 * Create unique keys for the packet.
223 memcpy(&magic, packet->data + 4, 4);
224 packet->id = ntohl(magic);
227 * Check that it's a known packet type.
229 if ((packet->data[240] != 53) ||
230 (packet->data[241] != 1) ||
231 (packet->data[242] == 0) ||
232 (packet->data[242] > 8)) {
234 * Some clients send the packet type buried
235 * inside of the packet...
237 if (!getcode(packet->data + 240, packet->data_len - 240,
239 fprintf(stderr, "Unknown, or badly formatted DHCP packet\n");
244 packet->code = packet->data[242];
246 packet->code |= PW_DHCP_OFFSET;
249 * Create a unique vector from the MAC address and the
250 * DHCP opcode. This is a hack for the RADIUS
251 * infrastructure in the rest of the server.
253 * Note: packet->data[2] == 6, which is smaller than
254 * sizeof(packet->vector)
256 * FIXME: Look for client-identifier in packet,
259 memset(packet->vector, 0, sizeof(packet->vector));
260 memcpy(packet->vector, packet->data + 28, packet->data[2]);
261 packet->vector[packet->data[2]] = packet->code & 0xff;
264 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
265 * FIXME: for OFFER / ACK : src_port = dst_port - 1
269 * Unique keys are xid, client mac, and client ID?
273 * FIXME: More checks, like DHCP packet type?
276 sizeof_dst = sizeof(dst);
278 * This should never fail...
280 getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst);
282 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
283 packet->src_port = port;
285 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
286 packet->dst_port = port;
288 if (fr_debug_flag > 1) {
290 const char *name = type_buf;
291 char src_ip_buf[256], dst_ip_buf[256];
293 if ((packet->code >= PW_DHCP_DISCOVER) &&
294 (packet->code <= PW_DHCP_INFORM)) {
295 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
297 snprintf(type_buf, sizeof(type_buf), "%d",
298 packet->code - PW_DHCP_OFFSET);
301 printf("Received %s of id %u from %s:%d to %s:%d\n",
302 name, (unsigned int) packet->id,
303 inet_ntop(packet->src_ipaddr.af,
304 &packet->src_ipaddr.ipaddr,
305 src_ip_buf, sizeof(src_ip_buf)),
307 inet_ntop(packet->dst_ipaddr.af,
308 &packet->dst_ipaddr.ipaddr,
309 dst_ip_buf, sizeof(dst_ip_buf)),
319 * Send a DHCP packet.
321 int fr_dhcp_send(RADIUS_PACKET *packet)
323 struct sockaddr_storage dst;
324 struct sockaddr_storage src;
325 socklen_t sizeof_dst;
326 socklen_t sizeof_src;
328 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
332 * Currently unused...
334 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
338 * Assume that the packet is encoded before sending it.
340 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
341 (struct sockaddr *)&dst, sizeof_dst);
345 int fr_dhcp_decode(RADIUS_PACKET *packet)
351 VALUE_PAIR *head, *vp, **tail;
352 VALUE_PAIR *maxms, *mtu;
359 if ((fr_debug_flag > 2) && fr_log_fp) {
360 for (i = 0; i < packet->data_len; i++) {
361 if ((i & 0x0f) == 0x00) fprintf(stderr, "%d: ", i);
362 fprintf(fr_log_fp, "%02x ", packet->data[i]);
363 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
365 fprintf(fr_log_fp, "\n");
368 if (packet->data[1] != 1) {
369 fprintf(stderr, "Packet is not Ethernet: %u\n",
377 for (i = 0; i < 14; i++) {
378 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
380 fprintf(stderr, "Parse error %s\n", fr_strerror());
387 (packet->data[1] == 1) &&
388 (packet->data[2] == 6)) {
389 vp->type = PW_TYPE_ETHERNET;
394 vp->vp_integer = p[0];
399 vp->vp_integer = (p[0] << 8) | p[1];
403 case PW_TYPE_INTEGER:
404 memcpy(&vp->vp_integer, p, 4);
405 vp->vp_integer = ntohl(vp->vp_integer);
410 memcpy(&vp->vp_ipaddr, p, 4);
415 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
416 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
417 vp->length = strlen(vp->vp_strvalue);
418 if (vp->length == 0) {
424 memcpy(vp->vp_octets, p, packet->data[2]);
425 vp->length = packet->data[2];
428 case PW_TYPE_ETHERNET:
429 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
430 vp->length = sizeof(vp->vp_ether);
434 fprintf(stderr, "BAD TYPE %d\n", vp->type);
438 p += dhcp_header_sizes[i];
442 if (fr_debug_flag > 1) {
443 vp_prints(buffer, sizeof(buffer), vp);
444 fprintf(stderr, "\t%s\n", buffer);
451 * Loop over the options.
453 p = packet->data + 240;
454 total = packet->data_len - 240;
457 int num_entries, alen;
461 if (*p == 255) break; /* end of options signifier */
464 fprintf(stderr, "Attribute too long %u %u\n",
469 da = dict_attrbyvalue(DHCP2ATTR(p[0]));
471 fprintf(stderr, "Attribute not in our dictionary: %u\n",
486 if (da->flags.array) {
494 if ((alen & 0x01) != 0) goto raw;
495 num_entries = alen / 2;
500 case PW_TYPE_INTEGER:
502 if ((alen & 0x03) != 0) goto raw;
503 num_entries = alen / 4;
508 break; /* really an internal sanity failure */
515 if (alen != 1) goto raw;
519 if (alen != 2) goto raw;
523 case PW_TYPE_INTEGER:
525 if (alen != 4) goto raw;
533 for (i = 0; i < num_entries; i++) {
534 vp = pairmake(da->name, NULL, T_OP_EQ);
536 fprintf(stderr, "Cannot build attribute %s\n",
543 * Hacks for ease of use.
545 if ((da->attr == DHCP2ATTR(0x3d)) &&
547 (alen == 7) && (*p == 1) && (num_entries == 1)) {
548 vp->type = PW_TYPE_ETHERNET;
549 memcpy(vp->vp_octets, p + 1, 6);
554 vp->vp_integer = p[0];
558 vp->vp_integer = (p[0] << 8) | p[1];
561 case PW_TYPE_INTEGER:
562 memcpy(&vp->vp_integer, p, 4);
563 vp->vp_integer = ntohl(vp->vp_integer);
567 memcpy(&vp->vp_ipaddr, p , 4);
572 memcpy(vp->vp_strvalue, p , alen);
573 vp->vp_strvalue[alen] = '\0';
577 vp = pairmake(da->name, NULL, T_OP_EQ);
579 fprintf(stderr, "Cannot build attribute %s\n", fr_strerror());
584 vp->type = PW_TYPE_OCTETS;
587 memcpy(vp->vp_octets, p, alen);
591 fprintf(stderr, "Internal sanity check %d %d\n", vp->type, __LINE__);
594 } /* switch over type */
598 if (fr_debug_flag > 1) {
599 vp_prints(buffer, sizeof(buffer), vp);
600 fprintf(stderr, "\t%s\n", buffer);
606 } /* loop over array entries */
609 total -= (alen * num_entries);
613 * If DHCP request, set ciaddr to zero.
617 * Set broadcast flag for broken vendors, but only if
620 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
621 if (giaddr == htonl(INADDR_ANY)) {
623 * DHCP Opcode is request
625 vp = pairfind(head, DHCP2ATTR(256));
626 if (vp && vp->lvalue == 3) {
628 * Vendor is "MSFT 98"
630 vp = pairfind(head, DHCP2ATTR(63));
631 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
632 vp = pairfind(head, DHCP2ATTR(262));
635 * Reply should be broadcast.
637 if (vp) vp->lvalue |= 0x8000;
638 packet->data[10] |= 0x80;
644 * FIXME: Nuke attributes that aren't used in the normal
645 * header for discover/requests.
650 * Client can request a LARGER size, but not a smaller
651 * one. They also cannot request a size larger than MTU.
653 maxms = pairfind(packet->vps, DHCP2ATTR(57));
654 mtu = pairfind(packet->vps, DHCP2ATTR(26));
656 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
657 fprintf(stderr, "DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification.");
661 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
662 fprintf(stderr, "DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
663 maxms->vp_integer = DEFAULT_PACKET_SIZE;
666 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
667 fprintf(stderr, "DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
668 maxms->vp_integer = mtu->vp_integer;
671 if (fr_debug_flag) fflush(stdout);
677 static int attr_cmp(const void *one, const void *two)
679 const VALUE_PAIR * const *a = one;
680 const VALUE_PAIR * const *b = two;
683 * DHCP-Message-Type is first, for simplicity.
685 if (((*a)->attribute == DHCP2ATTR(53)) &&
686 (*b)->attribute != DHCP2ATTR(53)) return -1;
689 * Relay-Agent is last
691 if (((*a)->attribute == DHCP2ATTR(82)) &&
692 (*b)->attribute != DHCP2ATTR(82)) return +1;
694 return ((*a)->attribute - (*b)->attribute);
698 static size_t fr_dhcp_vp2attr(VALUE_PAIR *vp, uint8_t *p, size_t room)
706 room = room; /* -Wunused */
709 * Search for all attributes of the same
710 * type, and pack them into the same
716 *p = vp->vp_integer & 0xff;
721 p[0] = (vp->vp_integer >> 8) & 0xff;
722 p[1] = vp->vp_integer & 0xff;
725 case PW_TYPE_INTEGER:
727 lvalue = htonl(vp->vp_integer);
728 memcpy(p, &lvalue, 4);
733 memcpy(p, &vp->vp_ipaddr, 4);
736 case PW_TYPE_ETHERNET:
738 memcpy(p, &vp->vp_ether, 6);
742 memcpy(p, vp->vp_strvalue, vp->length);
747 memcpy(p, vp->vp_octets, vp->length);
752 fprintf(stderr, "BAD TYPE2 %d\n", vp->type);
760 int fr_dhcp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original)
765 uint32_t lvalue, mms;
766 size_t dhcp_size, length;
770 if (packet->data) return 0;
772 packet->data = malloc(MAX_PACKET_SIZE);
773 if (!packet->data) return -1;
775 packet->data_len = MAX_PACKET_SIZE;
777 if (packet->code == 0) packet->code = PW_DHCP_NAK;
780 * FIXME: allow it to send client packets.
783 fr_strerror_printf("Need original to send response!");
787 if (fr_debug_flag > 1) {
789 const char *name = type_buf;
790 char src_ip_buf[256], dst_ip_buf[256];
792 if ((packet->code >= PW_DHCP_DISCOVER) &&
793 (packet->code <= PW_DHCP_INFORM)) {
794 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
796 snprintf(type_buf, sizeof(type_buf), "%d",
797 packet->code - PW_DHCP_OFFSET);
800 printf("Sending %s of id %u from %s:%d to %s:%d\n",
801 name, (unsigned int) packet->id,
802 inet_ntop(packet->src_ipaddr.af,
803 &packet->src_ipaddr.ipaddr,
804 src_ip_buf, sizeof(src_ip_buf)),
806 inet_ntop(packet->dst_ipaddr.af,
807 &packet->dst_ipaddr.ipaddr,
808 dst_ip_buf, sizeof(dst_ip_buf)),
815 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
818 * Client can request a LARGER size, but not a smaller
819 * one. They also cannot request a size larger than MTU.
821 vp = pairfind(original->vps, DHCP2ATTR(57));
822 if (vp && (vp->vp_integer > mms)) {
823 mms = vp->vp_integer;
825 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
829 * RFC 3118: Authentication option.
831 vp = pairfind(packet->vps, DHCP2ATTR(90));
833 if (vp->length < 2) {
834 memset(vp->vp_octets + vp->length, 0,
839 if (vp->length < 3) {
842 gettimeofday(&tv, NULL);
843 vp->vp_octets[2] = 0;
844 timeval2ntp(&tv, vp->vp_octets + 3);
849 * Configuration token (clear-text token)
851 if (vp->vp_octets[0] == 0) {
853 vp->vp_octets[1] = 0;
855 pass = pairfind(packet->vps, PW_CLEARTEXT_PASSWORD);
857 length = pass->length;
858 if ((length + 11) > sizeof(vp->vp_octets)) {
859 length -= ((length + 11) - sizeof(vp->vp_octets));
861 memcpy(vp->vp_octets + 11, pass->vp_strvalue,
863 vp->length = length + 11;
866 memset(vp->vp_octets + 11, 8, 0);
869 } else { /* we don't support this type! */
870 fprintf(stderr, "DHCP-Authentication %d unsupported\n",
876 *p++ = 1; /* client message */
878 *p++ = 2; /* server message */
880 *p++ = 1; /* hardware type = ethernet */
881 *p++ = original->data[2];
884 if (!original) { /* Xid */
886 memcpy(p, &lvalue, 4);
888 memcpy(p, original->data + 4, 4);
892 memset(p, 0, 2); /* secs are zero */
895 memcpy(p, original->data + 10, 6); /* copy flags && ciaddr */
898 * Allow the admin to set the broadcast flag.
900 vp = pairfind(packet->vps, DHCP2ATTR(262));
902 p[0] |= (vp->vp_integer & 0xff00) >> 8;
903 p[1] |= (vp->vp_integer & 0xff);
909 * Set client IP address.
911 vp = pairfind(packet->vps, DHCP2ATTR(264)); /* Your IP address */
913 lvalue = vp->vp_ipaddr;
915 lvalue = htonl(INADDR_ANY);
917 memcpy(p, &lvalue, 4); /* your IP address */
920 memset(p, 0, 4); /* siaddr is zero */
923 memcpy(p, original->data + 24, 4); /* copy gateway IP address */
926 memcpy(p, original->data + 28, DHCP_CHADDR_LEN);
927 p += DHCP_CHADDR_LEN;
929 memset(p, 0, 192); /* bootp legacy */
932 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER); /* DHCP magic number */
933 memcpy(p, &lvalue, 4);
939 if (fr_debug_flag > 1) {
944 for (i = 0; i < 14; i++) {
945 vp = pairmake(dhcp_header_names[i], NULL, T_OP_EQ);
947 fprintf(stderr, "Parse error %s\n", fr_strerror());
953 vp->vp_integer = p[0];
958 vp->vp_integer = (p[0] << 8) | p[1];
962 case PW_TYPE_INTEGER:
963 memcpy(&vp->vp_integer, p, 4);
964 vp->vp_integer = ntohl(vp->vp_integer);
969 memcpy(&vp->vp_ipaddr, p, 4);
974 memcpy(vp->vp_strvalue, p, dhcp_header_sizes[i]);
975 vp->vp_strvalue[dhcp_header_sizes[i]] = '\0';
976 vp->length = strlen(vp->vp_strvalue);
979 case PW_TYPE_OCTETS: /* only for Client HW Address */
980 memcpy(vp->vp_octets, p, packet->data[2]);
981 vp->length = packet->data[2];
984 case PW_TYPE_ETHERNET: /* only for Client HW Address */
985 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
986 vp->length = sizeof(vp->vp_ether);
990 fprintf(stderr, "Internal sanity check failed %d %d\n", vp->type, __LINE__);
995 p += dhcp_header_sizes[i];
997 vp_prints(buffer, sizeof(buffer), vp);
998 fprintf(stderr, "\t%s\n", buffer);
1003 * Jump over DHCP magic number, response, etc.
1009 * Before packing the attributes, re-order them so that
1010 * the array ones are all contiguous. This simplifies
1014 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1018 VALUE_PAIR **array, **last;
1020 array = malloc(num_vps * sizeof(VALUE_PAIR *));
1023 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1028 * Sort the attributes.
1030 qsort(array, (size_t) num_vps, sizeof(VALUE_PAIR *),
1033 last = &packet->vps;
1034 for (i = 0; i < num_vps; i++) {
1036 array[i]->next = NULL;
1037 last = &(array[i]->next);
1042 p[0] = 0x35; /* DHCP-Message-Type */
1044 p[2] = packet->code - PW_DHCP_OFFSET;
1048 * Pack in the attributes.
1052 int num_entries = 1;
1055 uint8_t *plength, *pattr;
1057 if (!IS_DHCP_ATTR(vp)) goto next;
1058 if (vp->attribute == DHCP2ATTR(53)) goto next; /* already done */
1059 if (((vp->attribute & 0xffff) > 255) &&
1060 (DHCP_BASE_ATTR(vp->attribute) != PW_DHCP_OPTION_82)) goto next;
1062 length = vp->length;
1064 for (same = vp->next; same != NULL; same = same->next) {
1065 if (same->attribute != vp->attribute) break;
1070 * For client-identifier
1072 if ((vp->type == PW_TYPE_ETHERNET) &&
1073 (vp->length == 6) &&
1074 (num_entries == 1)) {
1075 vp->type = PW_TYPE_OCTETS;
1076 memmove(vp->vp_octets + 1, vp->vp_octets, 6);
1077 vp->vp_octets[0] = 1;
1081 *(p++) = vp->attribute & 0xff;
1083 *(p++) = 0; /* header isn't included in attr length */
1085 if (DHCP_BASE_ATTR(vp->attribute) == PW_DHCP_OPTION_82) {
1086 *(p++) = DHCP_UNPACK_OPTION1(vp->attribute);
1091 for (i = 0; i < num_entries; i++) {
1092 if (fr_debug_flag > 1) {
1093 vp_prints(buffer, sizeof(buffer), vp);
1094 fprintf(stderr, "\t%s\n", buffer);
1097 length = fr_dhcp_vp2attr(vp, p, 0);
1100 * This will never happen due to FreeRADIUS
1101 * limitations: sizeof(vp->vp_octets) < 255
1104 fprintf(stderr, "WARNING Ignoring too long attribute %s!\n", vp->name);
1109 * More than one attribute of the same type
1110 * in a row: they are packed together
1111 * into the same TLV. If we overflow,
1114 if ((*plength + length) > 255) {
1115 fprintf(stderr, "WARNING Ignoring too long attribute %s!\n", vp->name);
1123 (vp->next->attribute == vp->attribute))
1125 } /* loop over num_entries */
1127 if (DHCP_BASE_ATTR(vp->attribute) == PW_DHCP_OPTION_82) {
1128 plength[2] = plength[0] - 2;
1135 p[0] = 0xff; /* end of option option */
1138 dhcp_size = p - packet->data;
1141 * FIXME: if (dhcp_size > mms),
1142 * then we put the extra options into the "sname" and "file"
1143 * fields, AND set the "end option option" in the "options"
1144 * field. We also set the "overload option",
1145 * and put options into the "file" field, followed by
1146 * the "sname" field. Where each option is completely
1147 * enclosed in the "file" and/or "sname" field, AND
1148 * followed by the "end of option", and MUST be followed
1149 * by padding option.
1151 * Yuck. That sucks...
1153 packet->data_len = dhcp_size;
1155 packet->dst_ipaddr.af = AF_INET;
1156 packet->src_ipaddr.af = AF_INET;
1158 packet->dst_port = original->src_port;
1159 packet->src_port = original->dst_port;
1162 * Note that for DHCP, we NEVER send the response to the
1163 * source IP address of the request. It may have
1164 * traversed multiple relays, and we need to send the request
1165 * to the relay closest to the client.
1167 * if giaddr, send to giaddr.
1168 * if NAK, send broadcast packet
1169 * if ciaddr, unicast to ciaddr
1170 * if flags & 0x8000, broadcast (client request)
1171 * if sent from 0.0.0.0, broadcast response
1172 * unicast to client yiaddr
1176 * FIXME: alignment issues. We likely don't want to
1177 * de-reference the packet structure directly..
1179 dhcp = (dhcp_packet_t *) original->data;
1181 if (dhcp->giaddr != htonl(INADDR_ANY)) {
1182 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->giaddr;
1184 if (dhcp->giaddr != htonl(INADDR_LOOPBACK)) {
1185 packet->dst_port = original->dst_port;
1187 packet->dst_port = original->src_port; /* debugging */
1190 } else if (packet->code == PW_DHCP_NAK) {
1191 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1193 } else if (dhcp->ciaddr != htonl(INADDR_ANY)) {
1194 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->ciaddr;
1196 } else if ((dhcp->flags & 0x8000) != 0) {
1197 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1199 } else if (packet->dst_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY)) {
1200 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_BROADCAST);
1203 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = dhcp->yiaddr;
1207 * FIXME: This may set it to broadcast, which we don't
1208 * want. Instead, set it to the real address of the
1211 packet->src_ipaddr = original->dst_ipaddr;
1213 packet->sockfd = original->sockfd;
1215 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1216 memset(packet->data + packet->data_len, 0,
1217 DEFAULT_PACKET_SIZE - packet->data_len);
1218 packet->data_len = DEFAULT_PACKET_SIZE;
1221 if ((fr_debug_flag > 2) && fr_log_fp) {
1222 for (i = 0; i < packet->data_len; i++) {
1223 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", i);
1224 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1225 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1227 fprintf(fr_log_fp, "\n");
1232 #endif /* WITH_DHCP */