2 * dhcp.c Functions to send/receive dhcp packets.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2008 The FreeRADIUS server project
21 * Copyright 2008 Alan DeKok <aland@deployingradius.com>
26 #include <freeradius-devel/libradius.h>
27 #include <freeradius-devel/udpfromto.h>
28 #include <freeradius-devel/dhcp.h>
29 #include <freeradius-devel/net.h>
32 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
38 #ifdef HAVE_SYS_TYPES_H
39 #include <sys/types.h>
43 #include <net/if_arp.h>
46 #define DHCP_CHADDR_LEN (16)
47 #define DHCP_SNAME_LEN (64)
48 #define DHCP_FILE_LEN (128)
49 #define DHCP_VEND_LEN (308)
50 #define DHCP_OPTION_MAGIC_NUMBER (0x63825363)
52 #ifndef INADDR_BROADCAST
53 #define INADDR_BROADCAST INADDR_NONE
56 /* @todo: this is a hack */
57 # define DEBUG if (fr_debug_lvl && fr_log_fp) fr_printf_log
58 # define debug_pair(vp) do { if (fr_debug_lvl && fr_log_fp) { \
59 vp_print(fr_log_fp, vp); \
63 #ifdef HAVE_LINUX_IF_PACKET_H
64 #define ETH_HDR_SIZE 14
65 #define IP_HDR_SIZE 20
66 #define UDP_HDR_SIZE 8
67 #define ETH_ADDR_LEN 6
68 #define ETH_TYPE_IP 0x0800
69 #define ETH_P_ALL 0x0003
71 static uint8_t eth_bcast[ETH_ADDR_LEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
73 /* Discard raw packets which we are not interested in. Allow to trace why we discard. */
74 #define DISCARD_RP(...) { \
75 if (fr_debug_lvl > 2) { \
76 fprintf(stdout, "dhcpclient: discarding received packet: "); \
77 fprintf(stdout, ## __VA_ARGS__); \
78 fprintf(stdout, "\n"); \
85 typedef struct dhcp_packet_t {
91 uint16_t secs; /* 8 */
93 uint32_t ciaddr; /* 12 */
94 uint32_t yiaddr; /* 16 */
95 uint32_t siaddr; /* 20 */
96 uint32_t giaddr; /* 24 */
97 uint8_t chaddr[DHCP_CHADDR_LEN]; /* 28 */
98 uint8_t sname[DHCP_SNAME_LEN]; /* 44 */
99 uint8_t file[DHCP_FILE_LEN]; /* 108 */
100 uint32_t option_format; /* 236 */
101 uint8_t options[DHCP_VEND_LEN];
104 typedef struct dhcp_option_t {
110 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 DISCOVER
111 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 OFFER
112 * INADDR_ANY : 68 -> INADDR_BROADCAST : 67 REQUEST
113 * INADDR_BROADCAST : 68 <- SERVER_IP : 67 ACK
115 static char const *dhcp_header_names[] = {
117 "DHCP-Hardware-Type",
118 "DHCP-Hardware-Address-Length",
120 "DHCP-Transaction-Id",
121 "DHCP-Number-of-Seconds",
123 "DHCP-Client-IP-Address",
124 "DHCP-Your-IP-Address",
125 "DHCP-Server-IP-Address",
126 "DHCP-Gateway-IP-Address",
127 "DHCP-Client-Hardware-Address",
128 "DHCP-Server-Host-Name",
129 "DHCP-Boot-Filename",
134 static char const *dhcp_message_types[] = {
147 static int dhcp_header_sizes[] = {
158 * Some clients silently ignore responses less than 300 bytes.
160 #define MIN_PACKET_SIZE (244)
161 #define DEFAULT_PACKET_SIZE (300)
162 #define MAX_PACKET_SIZE (1500 - 40)
164 #define DHCP_OPTION_FIELD (0)
165 #define DHCP_FILE_FIELD (1)
166 #define DHCP_SNAME_FIELD (2)
168 static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
172 int field = DHCP_OPTION_FIELD;
177 size = packet_size - offsetof(dhcp_packet_t, options);
178 data = &packet->options[where];
180 while (where < size) {
181 if (data[0] == 0) { /* padding */
186 if (data[0] == 255) { /* end of options */
187 if ((field == DHCP_OPTION_FIELD) &&
188 (overload & DHCP_FILE_FIELD)) {
191 size = sizeof(packet->file);
192 field = DHCP_FILE_FIELD;
195 } else if ((field == DHCP_FILE_FIELD) &&
196 (overload & DHCP_SNAME_FIELD)) {
197 data = packet->sname;
199 size = sizeof(packet->sname);
200 field = DHCP_SNAME_FIELD;
208 * We MUST have a real option here.
210 if ((where + 2) > size) {
211 fr_strerror_printf("Options overflow field at %u",
212 (unsigned int) (data - (uint8_t *) packet));
216 if ((where + 2 + data[1]) > size) {
217 fr_strerror_printf("Option length overflows field at %u",
218 (unsigned int) (data - (uint8_t *) packet));
222 if (data[0] == option) return data;
224 if (data[0] == 52) { /* overload sname and/or file */
228 where += data[1] + 2;
236 * DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
239 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
242 struct sockaddr_storage src;
243 struct sockaddr_storage dst;
244 socklen_t sizeof_src;
245 socklen_t sizeof_dst;
246 RADIUS_PACKET *packet;
251 packet = rad_alloc(NULL, false);
253 fr_strerror_printf("Failed allocating packet");
257 packet->data = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
259 fr_strerror_printf("Out of memory");
264 packet->sockfd = sockfd;
265 sizeof_src = sizeof(src);
266 #ifdef WITH_UDPFROMTO
267 sizeof_dst = sizeof(dst);
268 data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
269 (struct sockaddr *)&src, &sizeof_src,
270 (struct sockaddr *)&dst, &sizeof_dst);
272 data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
273 (struct sockaddr *)&src, &sizeof_src);
277 fr_strerror_printf("Failed reading DHCP socket: %s", fr_syserror(errno));
282 packet->data_len = data_len;
283 if (packet->data_len < MIN_PACKET_SIZE) {
284 fr_strerror_printf("DHCP packet is too small (%zu < %d)",
285 packet->data_len, MIN_PACKET_SIZE);
290 if (packet->data_len > MAX_PACKET_SIZE) {
291 fr_strerror_printf("DHCP packet is too large (%zx > %d)",
292 packet->data_len, MAX_PACKET_SIZE);
297 if (packet->data[1] != 1) {
298 fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
304 if (packet->data[2] != 6) {
305 fr_strerror_printf("Ethernet HW length is wrong length %d",
311 memcpy(&magic, packet->data + 236, 4);
312 magic = ntohl(magic);
313 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
314 fr_strerror_printf("Cannot do BOOTP");
320 * Create unique keys for the packet.
322 memcpy(&magic, packet->data + 4, 4);
323 packet->id = ntohl(magic);
325 code = dhcp_get_option((dhcp_packet_t *) packet->data,
326 packet->data_len, 53);
328 fr_strerror_printf("No message-type option was found in the packet");
333 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
334 fr_strerror_printf("Unknown value for message-type option");
339 packet->code = code[2] | PW_DHCP_OFFSET;
342 * Create a unique vector from the MAC address and the
343 * DHCP opcode. This is a hack for the RADIUS
344 * infrastructure in the rest of the server.
346 * Note: packet->data[2] == 6, which is smaller than
347 * sizeof(packet->vector)
349 * FIXME: Look for client-identifier in packet,
352 memset(packet->vector, 0, sizeof(packet->vector));
353 memcpy(packet->vector, packet->data + 28, packet->data[2]);
354 packet->vector[packet->data[2]] = packet->code & 0xff;
357 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
358 * FIXME: for OFFER / ACK : src_port = dst_port - 1
362 * Unique keys are xid, client mac, and client ID?
366 * FIXME: More checks, like DHCP packet type?
369 sizeof_dst = sizeof(dst);
371 #ifndef WITH_UDPFROMTO
373 * This should never fail...
375 if (getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst) < 0) {
376 fr_strerror_printf("getsockname failed: %s", fr_syserror(errno));
382 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
383 packet->dst_port = port;
385 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
386 packet->src_port = port;
388 if (fr_debug_lvl > 1) {
390 char const *name = type_buf;
391 char src_ip_buf[256], dst_ip_buf[256];
393 if ((packet->code >= PW_DHCP_DISCOVER) &&
394 (packet->code <= PW_DHCP_INFORM)) {
395 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
397 snprintf(type_buf, sizeof(type_buf), "%d",
398 packet->code - PW_DHCP_OFFSET);
401 DEBUG("Received %s of Id %08x from %s:%d to %s:%d\n",
402 name, (unsigned int) packet->id,
403 inet_ntop(packet->src_ipaddr.af,
404 &packet->src_ipaddr.ipaddr,
405 src_ip_buf, sizeof(src_ip_buf)),
407 inet_ntop(packet->dst_ipaddr.af,
408 &packet->dst_ipaddr.ipaddr,
409 dst_ip_buf, sizeof(dst_ip_buf)),
418 * Send a DHCP packet.
420 int fr_dhcp_send(RADIUS_PACKET *packet)
422 struct sockaddr_storage dst;
423 socklen_t sizeof_dst;
424 #ifdef WITH_UDPFROMTO
425 struct sockaddr_storage src;
426 socklen_t sizeof_src;
428 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
432 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
435 if (packet->data_len == 0) {
436 fr_strerror_printf("No data to send");
440 if (fr_debug_lvl > 1) {
442 char const *name = type_buf;
443 #ifdef WITH_UDPFROMTO
444 char src_ip_buf[INET6_ADDRSTRLEN];
446 char dst_ip_buf[INET6_ADDRSTRLEN];
448 if ((packet->code >= PW_DHCP_DISCOVER) &&
449 (packet->code <= PW_DHCP_INFORM)) {
450 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
452 snprintf(type_buf, sizeof(type_buf), "%d",
453 packet->code - PW_DHCP_OFFSET);
457 #ifdef WITH_UDPFROMTO
458 "Sending %s Id %08x from %s:%d to %s:%d\n",
460 "Sending %s Id %08x to %s:%d\n",
462 name, (unsigned int) packet->id,
463 #ifdef WITH_UDPFROMTO
464 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
467 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),
471 #ifndef WITH_UDPFROMTO
473 * Assume that the packet is encoded before sending it.
475 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
476 (struct sockaddr *)&dst, sizeof_dst);
479 return sendfromto(packet->sockfd, packet->data, packet->data_len, 0,
480 (struct sockaddr *)&src, sizeof_src,
481 (struct sockaddr *)&dst, sizeof_dst);
485 static int fr_dhcp_attr2vp(TALLOC_CTX *ctx, VALUE_PAIR **vp_p, uint8_t const *p, size_t alen);
487 /** Returns the number of array members for arrays with fixed element sizes
490 static int fr_dhcp_array_members(size_t *len, DICT_ATTR const *da)
495 * Could be an array of bytes, integers, etc.
497 if (da->flags.array) switch (da->type) {
503 case PW_TYPE_SHORT: /* ignore any trailing data */
504 num_entries = *len >> 1;
508 case PW_TYPE_IPV4_ADDR:
509 case PW_TYPE_INTEGER:
510 case PW_TYPE_DATE: /* ignore any trailing data */
511 num_entries = *len >> 2;
515 case PW_TYPE_IPV6_ADDR:
516 num_entries = *len >> 4;
527 /** RFC 4243 Vendor Specific Suboptions
529 * Vendor specific suboptions are in the format.
532 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
533 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 | Enterprise Number 0 |
535 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
539 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540 | Enterprise Number n |
541 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
543 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
545 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
548 * So although the vendor is identified, the format of the data isn't
549 * specified so we can't actually resolve the suboption to an
550 * attribute. For now, we just convert it to an attribute of
551 * DHCP-Vendor-Specific-Information with raw octets contents.
555 /** Decode DHCP suboptions
557 * @param[in,out] tlv to decode. *tlv will be set to the head of the list of suboptions and original will be freed.
558 * @param[in] ctx context to alloc new attributes in.
559 * @param[in] data to parse.
560 * @param[in] len length of data to parse.
562 static int fr_dhcp_decode_suboption(TALLOC_CTX *ctx, VALUE_PAIR **tlv, uint8_t const *data, size_t len)
564 uint8_t const *p, *q;
565 VALUE_PAIR *head, *vp;
569 * TLV must already point to a VALUE_PAIR.
574 * Take a pass at parsing it.
580 * RFC 3046 is very specific about not allowing termination
581 * with a 255 sub-option. But it's required for decoding
582 * option 43, and vendors will probably screw it up
595 * Check if reading length would take us past the end of the buffer
597 if (++p >= q) goto malformed;
601 * Check if length > the length of the buffer we have left
603 if (p >= q) goto malformed;
608 * Got here... must be well formed.
611 fr_cursor_init(&cursor, &head);
623 * The initial OID string looks like:
626 * If <iana>.0 is type TLV then we attempt to decode its contents as more
627 * DHCP suboptions, which gives us:
630 * If <iana>.0 is not defined in the dictionary or is type octets, we leave
631 * the attribute as is.
633 attr = (*tlv)->da->attr ? ((*tlv)->da->attr | (p[0] << 8)) : p[0];
636 * Use the vendor of the parent TLV which is not necessarily
639 * Note: This does not deal with dictionary numbering clashes. If
640 * the vendor uses different numbers for DHCP suboptions and RADIUS
641 * attributes then it's time to break out %{hex:} and regular
644 da = dict_attrbyvalue(attr, (*tlv)->da->vendor);
646 da = dict_unknown_afrom_fields(ctx, attr, (*tlv)->da->vendor);
655 num_entries = fr_dhcp_array_members(&a_len, da);
656 for (i = 0; i < num_entries; i++) {
657 vp = pairalloc(ctx, da);
663 pairsteal(ctx, vp); /* for unknown attributes hack */
665 if (fr_dhcp_attr2vp(ctx, &vp, a_p, a_len) < 0) {
670 fr_cursor_merge(&cursor, vp);
675 dict_attr_free(&da); /* for unknown attributes hack */
677 p += 2 + p[1]; /* code (1) + len (1) + suboption len (n)*/
681 * The caller allocated a TLV, if decoding it generated
682 * additional attributes, we now need to free it, and write
683 * the HEAD of our new list of attributes in its place.
686 vp_cursor_t tlv_cursor;
689 * Free the old TLV attribute
694 * Cursor not necessary but means we don't have to set
697 fr_cursor_init(&tlv_cursor, tlv);
698 fr_cursor_merge(&tlv_cursor, head);
705 pairmemcpy(*tlv, data, len);
711 * Decode ONE value into a VP
713 static int fr_dhcp_attr2vp(TALLOC_CTX *ctx, VALUE_PAIR **vp_p, uint8_t const *data, size_t len)
715 VALUE_PAIR *vp = *vp_p;
718 switch (vp->da->type) {
720 if (len != 1) goto raw;
721 vp->vp_byte = data[0];
725 if (len != 2) goto raw;
726 memcpy(&vp->vp_short, data, 2);
727 vp->vp_short = ntohs(vp->vp_short);
730 case PW_TYPE_INTEGER:
731 if (len != 4) goto raw;
732 memcpy(&vp->vp_integer, data, 4);
733 vp->vp_integer = ntohl(vp->vp_integer);
736 case PW_TYPE_IPV4_ADDR:
737 if (len != 4) goto raw;
739 * Keep value in Network Order!
741 memcpy(&vp->vp_ipaddr, data, 4);
746 * In DHCPv4, string options which can also be arrays,
747 * have their values '\0' delimited.
752 uint8_t const *q, *end;
756 q = end = data + len;
758 if (!vp->da->flags.array) {
759 pairbstrncpy(vp, (char const *)p, q - p);
764 * Initialise the cursor as we may be inserting
765 * multiple additional VPs
767 fr_cursor_init(&cursor, vp_p);
769 q = memchr(p, '\0', q - p);
770 /* Malformed but recoverable */
773 pairbstrncpy(vp, (char const *)p, q - p);
776 /* Need another VP for the next round */
778 vp = pairalloc(ctx, vp->da);
783 fr_cursor_insert(&cursor, vp);
791 case PW_TYPE_ETHERNET:
792 memcpy(vp->vp_ether, data, sizeof(vp->vp_ether));
793 vp->vp_length = sizeof(vp->vp_ether);
797 * Value doesn't match up with attribute type, overwrite the
798 * vp's original DICT_ATTR with an unknown one.
801 if (pair2unknown(vp) < 0) return -1;
804 if (len > 255) return -1;
805 pairmemcpy(vp, data, len);
809 * For option 82 et al...
812 return fr_dhcp_decode_suboption(ctx, vp_p, data, len);
815 fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
817 } /* switch over type */
823 /** Decode DHCP options
825 * @param[in,out] out Where to write the decoded options.
826 * @param[in] ctx context to alloc new attributes in.
827 * @param[in] data to parse.
828 * @param[in] len of data to parse.
830 ssize_t fr_dhcp_decode_options(TALLOC_CTX *ctx, VALUE_PAIR **out, uint8_t const *data, size_t len)
834 uint8_t const *p, *q;
837 fr_cursor_init(&cursor, out);
840 * FIXME: This should also check sname && file fields.
841 * See the dhcp_get_option() function above.
852 if (*p == 0) { /* 0x00 - Padding option */
857 if (*p == 255) { /* 0xff - End of options signifier */
861 if ((p + 2) > q) break;
867 * Unknown attribute, create an octets type
868 * attribute with the contents of the sub-option.
870 da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
872 da = dict_unknown_afrom_fields(ctx, p[0], DHCP_MAGIC_VENDOR);
877 vp = pairalloc(ctx, da);
882 pairmemcpy(vp, a_p, a_len);
883 fr_cursor_insert(&cursor, vp);
889 * Array type sub-option create a new VALUE_PAIR
890 * for each array element.
892 num_entries = fr_dhcp_array_members(&a_len, da);
893 for (i = 0; i < num_entries; i++) {
894 vp = pairalloc(ctx, da);
901 if (fr_dhcp_attr2vp(ctx, &vp, a_p, a_len) < 0) {
906 fr_cursor_merge(&cursor, vp);
908 } /* loop over array entries */
910 p += 2 + p[1]; /* code (1) + len (1) + option len (n)*/
911 } /* loop over the entire packet */
916 int fr_dhcp_decode(RADIUS_PACKET *packet)
922 VALUE_PAIR *head = NULL, *vp;
923 VALUE_PAIR *maxms, *mtu;
925 fr_cursor_init(&cursor, &head);
928 if ((fr_debug_lvl > 2) && fr_log_fp) {
929 for (i = 0; i < packet->data_len; i++) {
930 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
931 fprintf(fr_log_fp, "%02x ", packet->data[i]);
932 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
934 fprintf(fr_log_fp, "\n");
937 if (packet->data[1] != 1) {
938 fr_strerror_printf("Packet is not Ethernet: %u",
946 for (i = 0; i < 14; i++) {
949 vp = pairmake(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
952 strlcpy(buffer, fr_strerror(), sizeof(buffer));
953 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
959 * If chaddr does != 6 bytes it's probably not ethernet, and we should store
960 * it as an opaque type (octets).
962 if ((i == 11) && (packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
963 DICT_ATTR const *da = dict_unknown_afrom_fields(packet, vp->da->attr, vp->da->vendor);
970 switch (vp->da->type) {
977 vp->vp_short = (p[0] << 8) | p[1];
981 case PW_TYPE_INTEGER:
982 memcpy(&vp->vp_integer, p, 4);
983 vp->vp_integer = ntohl(vp->vp_integer);
987 case PW_TYPE_IPV4_ADDR:
988 memcpy(&vp->vp_ipaddr, p, 4);
993 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
995 memcpy(q, p, dhcp_header_sizes[i]);
996 q[dhcp_header_sizes[i]] = '\0';
997 vp->vp_length = strlen(vp->vp_strvalue);
998 if (vp->vp_length == 0) {
1003 case PW_TYPE_OCTETS:
1004 pairmemcpy(vp, p, packet->data[2]);
1007 case PW_TYPE_ETHERNET:
1008 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1009 vp->vp_length = sizeof(vp->vp_ether);
1013 fr_strerror_printf("BAD TYPE %d", vp->da->type);
1017 p += dhcp_header_sizes[i];
1022 fr_cursor_insert(&cursor, vp);
1026 * Loop over the options.
1030 * Nothing uses tail after this call, if it does in the future
1031 * it'll need to find the new tail...
1034 VALUE_PAIR *options = NULL;
1035 vp_cursor_t options_cursor;
1037 if (fr_dhcp_decode_options(packet, &options, packet->data + 240, packet->data_len - 240) < 0) {
1042 for (vp = fr_cursor_init(&options_cursor, options);
1044 vp = fr_cursor_next(&options_cursor)) {
1047 fr_cursor_merge(&cursor, options);
1052 * If DHCP request, set ciaddr to zero.
1056 * Set broadcast flag for broken vendors, but only if
1059 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
1060 if (giaddr == htonl(INADDR_ANY)) {
1062 * DHCP Opcode is request
1064 vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1065 if (vp && vp->vp_integer == 3) {
1067 * Vendor is "MSFT 98"
1069 vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
1070 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
1071 vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
1074 * Reply should be broadcast.
1076 if (vp) vp->vp_integer |= 0x8000;
1077 packet->data[10] |= 0x80;
1083 * FIXME: Nuke attributes that aren't used in the normal
1084 * header for discover/requests.
1089 * Client can request a LARGER size, but not a smaller
1090 * one. They also cannot request a size larger than MTU.
1092 maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1093 mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
1095 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
1096 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification");
1100 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
1101 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
1102 maxms->vp_integer = DEFAULT_PACKET_SIZE;
1105 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
1106 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
1107 maxms->vp_integer = mtu->vp_integer;
1110 if (fr_debug_lvl) fflush(stdout);
1116 int8_t fr_dhcp_attr_cmp(void const *a, void const *b)
1118 VALUE_PAIR const *my_a = a;
1119 VALUE_PAIR const *my_b = b;
1125 * DHCP-Message-Type is first, for simplicity.
1127 if ((my_a->da->attr == 53) && (my_b->da->attr != 53)) return -1;
1130 * Relay-Agent is last
1132 if ((my_a->da->attr == 82) && (my_b->da->attr != 82)) return 1;
1134 if (my_a->da->attr < my_b->da->attr) return -1;
1135 if (my_a->da->attr > my_b->da->attr) return 1;
1140 /** Write DHCP option value into buffer
1142 * Does not include DHCP option length or number.
1144 * @param out where to write the DHCP option.
1145 * @param outlen length of output buffer.
1146 * @param vp option to encode.
1147 * @return the length of data writen, -1 if out of buffer, -2 if unsupported type.
1149 static ssize_t fr_dhcp_vp2attr(uint8_t *out, size_t outlen, VALUE_PAIR *vp)
1154 if (outlen < vp->vp_length) {
1158 switch (vp->da->type) {
1164 p[0] = (vp->vp_short >> 8) & 0xff;
1165 p[1] = vp->vp_short & 0xff;
1168 case PW_TYPE_INTEGER:
1169 lvalue = htonl(vp->vp_integer);
1170 memcpy(p, &lvalue, 4);
1173 case PW_TYPE_IPV4_ADDR:
1174 memcpy(p, &vp->vp_ipaddr, 4);
1177 case PW_TYPE_ETHERNET:
1178 memcpy(p, vp->vp_ether, 6);
1181 case PW_TYPE_STRING:
1182 memcpy(p, vp->vp_strvalue, vp->vp_length);
1185 case PW_TYPE_TLV: /* FIXME: split it on 255? */
1186 memcpy(p, vp->vp_tlv, vp->vp_length);
1189 case PW_TYPE_OCTETS:
1190 memcpy(p, vp->vp_octets, vp->vp_length);
1194 fr_strerror_printf("Unsupported option type %d", vp->da->type);
1198 return vp->vp_length;
1201 /** Create a new TLV attribute from multiple sub options
1203 * @param[in,out] ctx to allocate new attribute in.
1204 * @param[in,out] cursor should be set to the start of the list of TLV attributes.
1205 * Will be advanced to the first non-TLV attribute.
1206 * @return attribute holding the concatenation of the values of the sub options.
1208 static VALUE_PAIR *fr_dhcp_vp2suboption(TALLOC_CTX *ctx, vp_cursor_t *cursor)
1211 unsigned int parent; /* Parent attribute of suboption */
1213 uint8_t *p, *opt_len = NULL;
1214 vp_cursor_t to_pack;
1215 VALUE_PAIR *vp, *tlv;
1217 #define SUBOPTION_PARENT(_x) (_x & 0xffff00ff)
1218 #define SUBOPTION_ATTR(_x) ((_x & 0xff00) >> 8)
1220 vp = fr_cursor_current(cursor);
1221 if (!vp) return NULL;
1223 parent = SUBOPTION_PARENT(vp->da->attr);
1224 tlv = paircreate(ctx, parent, DHCP_MAGIC_VENDOR);
1225 if (!tlv) return NULL;
1227 fr_cursor_copy(&to_pack, cursor);
1230 * Loop over TLVs to determine how much memory we need to allocate
1232 * We advanced the cursor we were passed, so if we fail encoding,
1233 * the cursor is at the right position for the next potentially
1236 for (vp = fr_cursor_current(cursor);
1237 vp && vp->da->flags.is_tlv && !vp->da->flags.extended && (SUBOPTION_PARENT(vp->da->attr) == parent);
1238 vp = fr_cursor_next(cursor)) {
1240 * If it's not an array type or is an array type, but is not the same
1241 * as the previous attribute, we add 2 for the additional sub-option
1244 if (!vp->da->flags.array || (SUBOPTION_ATTR(vp->da->attr) != attr)) {
1245 attr = SUBOPTION_ATTR(vp->da->attr);
1246 tlv->vp_length += 2;
1248 tlv->vp_length += vp->vp_length;
1251 tlv->vp_tlv = talloc_zero_array(tlv, uint8_t, tlv->vp_length);
1259 for (vp = fr_cursor_current(&to_pack);
1260 vp && vp->da->flags.is_tlv && !vp->da->flags.extended && (SUBOPTION_PARENT(vp->da->attr) == parent);
1261 vp = fr_cursor_next(&to_pack)) {
1262 if (SUBOPTION_ATTR(vp->da->attr) == 0) {
1263 fr_strerror_printf("Invalid attribute number 0");
1267 /* Don't write out the header, were packing array options */
1268 if (!vp->da->flags.array || (attr != SUBOPTION_ATTR(vp->da->attr))) {
1269 attr = SUBOPTION_ATTR(vp->da->attr);
1274 length = fr_dhcp_vp2attr(p, (tlv->vp_tlv + tlv->vp_length) - p, vp);
1275 if ((length < 0) || (length > 255)) {
1288 /** Encode a DHCP option and any sub-options.
1290 * @param out Where to write encoded DHCP attributes.
1291 * @param outlen Length of out buffer.
1292 * @param ctx to use for any allocated memory.
1293 * @param cursor with current VP set to the option to be encoded. Will be advanced to the next option to encode.
1294 * @return > 0 length of data written, < 0 error, 0 not valid option (skipping).
1296 ssize_t fr_dhcp_encode_option(TALLOC_CTX *ctx, uint8_t *out, size_t outlen, vp_cursor_t *cursor)
1299 DICT_ATTR const *previous;
1300 uint8_t *opt_len, *p = out;
1301 size_t freespace = outlen;
1304 vp = fr_cursor_current(cursor);
1307 if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next; /* not a DHCP option */
1308 if (vp->da->attr == 53) goto next; /* already done */
1309 if ((vp->da->attr > 255) && (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
1311 if (vp->da->flags.extended) {
1313 fr_strerror_printf("Attribute \"%s\" is not a DHCP option", vp->da->name);
1314 fr_cursor_next(cursor);
1318 /* Write out the option number */
1319 *(p++) = vp->da->attr & 0xff;
1321 /* Pointer to the length field of the option */
1324 /* Zero out the option's length field */
1327 /* We just consumed two bytes for the header */
1330 /* DHCP options with the same number get coalesced into a single option */
1332 VALUE_PAIR *tlv = NULL;
1335 if (vp->da->flags.is_tlv) {
1337 * Coalesce TLVs into one sub-option.
1338 * Cursor will be advanced to next non-TLV attribute.
1340 tlv = vp = fr_dhcp_vp2suboption(ctx, cursor);
1343 * Skip if there's an issue coalescing the sub-options.
1344 * Cursor will still have been advanced to next non-TLV attribute.
1348 * If not calling fr_dhcp_vp2suboption() advance the cursor, so fr_cursor_current()
1349 * returns the next attribute.
1352 fr_cursor_next(cursor);
1355 if ((*opt_len + vp->vp_length) > 255) {
1356 fr_strerror_printf("Skipping \"%s\": Option splitting not supported "
1357 "(option > 255 bytes)", vp->da->name);
1362 len = fr_dhcp_vp2attr(p, freespace, vp);
1365 /* Failed encoding option */
1374 } while ((vp = fr_cursor_current(cursor)) && (previous == vp->da) && vp->da->flags.array);
1379 int fr_dhcp_encode(RADIUS_PACKET *packet)
1390 # ifdef WITH_UDPFROMTO
1391 char src_ip_buf[256];
1393 char dst_ip_buf[256];
1396 if (packet->data) return 0;
1398 packet->data_len = MAX_PACKET_SIZE;
1399 packet->data = talloc_zero_array(packet, uint8_t, packet->data_len);
1401 /* XXX Ugly ... should be set by the caller */
1402 if (packet->code == 0) packet->code = PW_DHCP_NAK;
1405 if ((vp = pairfind(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1406 packet->id = vp->vp_integer;
1408 packet->id = fr_rand();
1412 if ((packet->code >= PW_DHCP_DISCOVER) &&
1413 (packet->code <= PW_DHCP_INFORM)) {
1414 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1420 # ifdef WITH_UDPFROMTO
1421 "Encoding %s of id %08x from %s:%d to %s:%d\n",
1423 "Encoding %s of id %08x to %s:%d\n",
1425 name, (unsigned int) packet->id,
1426 # ifdef WITH_UDPFROMTO
1427 inet_ntop(packet->src_ipaddr.af,
1428 &packet->src_ipaddr.ipaddr,
1429 src_ip_buf, sizeof(src_ip_buf)),
1432 inet_ntop(packet->dst_ipaddr.af,
1433 &packet->dst_ipaddr.ipaddr,
1434 dst_ip_buf, sizeof(dst_ip_buf)),
1441 * @todo: Make this work again.
1444 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1447 * Clients can request a LARGER size, but not a
1448 * smaller one. They also cannot request a size
1452 /* DHCP-DHCP-Maximum-Msg-Size */
1453 vp = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1454 if (vp && (vp->vp_integer > mms)) {
1455 mms = vp->vp_integer;
1457 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1461 vp = pairfind(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1463 *p++ = vp->vp_integer & 0xff;
1465 *p++ = 1; /* client message */
1468 /* DHCP-Hardware-Type */
1469 if ((vp = pairfind(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1470 *p++ = vp->vp_integer & 0xFF;
1472 *p++ = 1; /* hardware type = ethernet */
1475 /* DHCP-Hardware-Address-Length */
1476 if ((vp = pairfind(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1477 *p++ = vp->vp_integer & 0xFF;
1479 *p++ = 6; /* 6 bytes of ethernet */
1482 /* DHCP-Hop-Count */
1483 if ((vp = pairfind(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1484 *p = vp->vp_integer & 0xff;
1488 /* DHCP-Transaction-Id */
1489 lvalue = htonl(packet->id);
1490 memcpy(p, &lvalue, 4);
1493 /* DHCP-Number-of-Seconds */
1494 if ((vp = pairfind(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1495 lvalue = htonl(vp->vp_integer);
1496 memcpy(p, &lvalue, 2);
1501 if ((vp = pairfind(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1502 lvalue = htons(vp->vp_integer);
1503 memcpy(p, &lvalue, 2);
1507 /* DHCP-Client-IP-Address */
1508 if ((vp = pairfind(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1509 memcpy(p, &vp->vp_ipaddr, 4);
1513 /* DHCP-Your-IP-address */
1514 if ((vp = pairfind(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1515 lvalue = vp->vp_ipaddr;
1517 lvalue = htonl(INADDR_ANY);
1519 memcpy(p, &lvalue, 4);
1522 /* DHCP-Server-IP-Address */
1523 vp = pairfind(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
1525 lvalue = vp->vp_ipaddr;
1527 lvalue = htonl(INADDR_ANY);
1529 memcpy(p, &lvalue, 4);
1533 * DHCP-Gateway-IP-Address
1535 if ((vp = pairfind(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1536 lvalue = vp->vp_ipaddr;
1538 lvalue = htonl(INADDR_ANY);
1540 memcpy(p, &lvalue, 4);
1543 /* DHCP-Client-Hardware-Address */
1544 if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1545 if (vp->vp_length == sizeof(vp->vp_ether)) {
1546 memcpy(p, vp->vp_ether, vp->vp_length);
1547 } /* else ignore it */
1549 p += DHCP_CHADDR_LEN;
1551 /* DHCP-Server-Host-Name */
1552 if ((vp = pairfind(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1553 if (vp->vp_length > DHCP_SNAME_LEN) {
1554 memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
1556 memcpy(p, vp->vp_strvalue, vp->vp_length);
1559 p += DHCP_SNAME_LEN;
1562 * Copy over DHCP-Boot-Filename.
1564 * FIXME: This copy should be delayed until AFTER the options
1565 * have been processed. If there are too many options for
1566 * the packet, then they go into the sname && filename fields.
1567 * When that happens, the boot filename is passed as an option,
1568 * instead of being placed verbatim in the filename field.
1571 /* DHCP-Boot-Filename */
1572 vp = pairfind(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY);
1574 if (vp->vp_length > DHCP_FILE_LEN) {
1575 memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
1577 memcpy(p, vp->vp_strvalue, vp->vp_length);
1582 /* DHCP magic number */
1583 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER);
1584 memcpy(p, &lvalue, 4);
1590 if (fr_debug_lvl > 1) {
1595 for (i = 0; i < 14; i++) {
1598 vp = pairmake(packet, NULL,
1599 dhcp_header_names[i], NULL, T_OP_EQ);
1602 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1603 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1607 switch (vp->da->type) {
1613 vp->vp_short = (p[0] << 8) | p[1];
1616 case PW_TYPE_INTEGER:
1617 memcpy(&vp->vp_integer, p, 4);
1618 vp->vp_integer = ntohl(vp->vp_integer);
1621 case PW_TYPE_IPV4_ADDR:
1622 memcpy(&vp->vp_ipaddr, p, 4);
1625 case PW_TYPE_STRING:
1626 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1628 memcpy(q, p, dhcp_header_sizes[i]);
1629 q[dhcp_header_sizes[i]] = '\0';
1630 vp->vp_length = strlen(vp->vp_strvalue);
1633 case PW_TYPE_OCTETS: /* only for Client HW Address */
1634 pairmemcpy(vp, p, packet->data[2]);
1637 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1638 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1642 fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
1647 p += dhcp_header_sizes[i];
1654 * Jump over DHCP magic number, response, etc.
1659 p[0] = 0x35; /* DHCP-Message-Type */
1661 p[2] = packet->code - PW_DHCP_OFFSET;
1666 * Pre-sort attributes into contiguous blocks so that fr_dhcp_encode_option
1667 * operates correctly. This changes the order of the list, but never mind...
1669 pairsort(&packet->vps, fr_dhcp_attr_cmp);
1670 fr_cursor_init(&cursor, &packet->vps);
1673 * Each call to fr_dhcp_encode_option will encode one complete DHCP option,
1676 while ((vp = fr_cursor_current(&cursor))) {
1677 len = fr_dhcp_encode_option(packet, p, packet->data_len - (p - packet->data), &cursor);
1679 if (len > 0) debug_pair(vp);
1683 p[0] = 0xff; /* end of option option */
1686 dhcp_size = p - packet->data;
1689 * FIXME: if (dhcp_size > mms),
1690 * then we put the extra options into the "sname" and "file"
1691 * fields, AND set the "end option option" in the "options"
1692 * field. We also set the "overload option",
1693 * and put options into the "file" field, followed by
1694 * the "sname" field. Where each option is completely
1695 * enclosed in the "file" and/or "sname" field, AND
1696 * followed by the "end of option", and MUST be followed
1697 * by padding option.
1699 * Yuck. That sucks...
1701 packet->data_len = dhcp_size;
1703 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1704 memset(packet->data + packet->data_len, 0,
1705 DEFAULT_PACKET_SIZE - packet->data_len);
1706 packet->data_len = DEFAULT_PACKET_SIZE;
1709 if ((fr_debug_lvl > 2) && fr_log_fp) {
1710 fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
1711 for (i = 0; i < packet->data_len; i++) {
1712 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
1713 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1714 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1716 fprintf(fr_log_fp, "\n");
1723 int fr_dhcp_add_arp_entry(int fd, char const *interface,
1724 VALUE_PAIR *macaddr, VALUE_PAIR *ip)
1726 struct sockaddr_in *sin;
1730 fr_strerror_printf("No interface specified. Cannot update ARP table");
1734 if (!fr_assert(macaddr) ||
1735 !fr_assert((macaddr->da->type == PW_TYPE_ETHERNET) || (macaddr->da->type == PW_TYPE_OCTETS))) {
1736 fr_strerror_printf("Wrong VP type (%s) for chaddr",
1737 fr_int2str(dict_attr_types, macaddr->da->type, "<invalid>"));
1741 if (macaddr->vp_length > sizeof(req.arp_ha.sa_data)) {
1742 fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
1743 sizeof(req.arp_ha.sa_data), macaddr->vp_length);
1747 memset(&req, 0, sizeof(req));
1748 sin = (struct sockaddr_in *) &req.arp_pa;
1749 sin->sin_family = AF_INET;
1750 sin->sin_addr.s_addr = ip->vp_ipaddr;
1752 strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
1754 if (macaddr->da->type == PW_TYPE_ETHERNET) {
1755 memcpy(&req.arp_ha.sa_data, macaddr->vp_ether, sizeof(macaddr->vp_ether));
1757 memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->vp_length);
1760 req.arp_flags = ATF_COM;
1761 if (ioctl(fd, SIOCSARP, &req) < 0) {
1762 fr_strerror_printf("Failed to add entry in ARP cache: %s (%d)", fr_syserror(errno), errno);
1769 int fr_dhcp_add_arp_entry(UNUSED int fd, UNUSED char const *interface,
1770 UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
1772 fr_strerror_printf("Adding ARP entry is unsupported on this system");
1778 #ifdef HAVE_LINUX_IF_PACKET_H
1780 * Open a packet interface raw socket.
1781 * Bind it to the specified interface using a device independent physical layer address.
1783 int fr_socket_packet(int iface_index, struct sockaddr_ll *p_ll)
1787 /* PF_PACKET - packet interface on device level.
1788 using a raw socket allows packet data to be unchanged by the device driver.
1790 lsockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1792 fr_strerror_printf("cannot open socket: %s", fr_syserror(errno));
1796 /* Set link layer parameters */
1797 memset(p_ll, 0, sizeof(struct sockaddr_ll));
1799 p_ll->sll_family = AF_PACKET;
1800 p_ll->sll_protocol = htons(ETH_P_ALL);
1801 p_ll->sll_ifindex = iface_index;
1802 p_ll->sll_hatype = ARPHRD_ETHER;
1803 p_ll->sll_pkttype = PACKET_OTHERHOST;
1804 p_ll->sll_halen = 6;
1806 if (bind(lsockfd, (struct sockaddr *)p_ll, sizeof(struct sockaddr_ll)) < 0) {
1808 fr_strerror_printf("cannot bind raw socket: %s", fr_syserror(errno));
1816 * Encode and send a DHCP packet on a raw packet socket.
1818 int fr_dhcp_send_raw_packet(int sockfd, struct sockaddr_ll *p_ll, RADIUS_PACKET *packet)
1821 u_char dhcp_packet[1518] = { 0 };
1823 /* set ethernet source address to our MAC address (DHCP-Client-Hardware-Address). */
1824 u_char dhmac[ETH_ADDR_LEN] = { 0 };
1825 if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1826 if (vp->length == sizeof(vp->vp_ether)) {
1827 memcpy(dhmac, vp->vp_ether, vp->length);
1831 /* fill in Ethernet layer (L2) */
1832 struct ethernet_header *ethhdr = (struct ethernet_header *)dhcp_packet;
1833 memcpy(ethhdr->ether_dst, eth_bcast, ETH_ADDR_LEN);
1834 memcpy(ethhdr->ether_src, dhmac, ETH_ADDR_LEN);
1835 ethhdr->ether_type = htons(ETH_TYPE_IP);
1837 /* fill in IP layer (L3) */
1838 struct ip_header *iph = (struct ip_header *)(dhcp_packet + ETH_HDR_SIZE);
1839 iph->ip_vhl = IP_VHL(4, 5);
1841 iph->ip_len = htons(IP_HDR_SIZE + UDP_HDR_SIZE + packet->data_len);
1846 iph->ip_sum = 0; /* Filled later */
1848 /* saddr: Packet-Src-IP-Address (default: 0.0.0.0). */
1849 iph->ip_src.s_addr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
1851 /* daddr: packet destination IP addr (should be 255.255.255.255 for broadcast). */
1852 iph->ip_dst.s_addr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
1854 /* IP header checksum */
1855 iph->ip_sum = fr_iph_checksum((uint8_t const *)iph, 5);
1857 /* fill in UDP layer (L4) */
1858 udp_header_t *uh = (udp_header_t *) (dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE);
1860 uh->src = htons(68);
1861 uh->dst = htons(67);
1862 u_int16_t l4_len = (UDP_HDR_SIZE + packet->data_len);
1863 uh->len = htons(l4_len);
1864 uh->checksum = 0; /* UDP checksum will be done after dhcp header */
1866 /* DHCP layer (L7) */
1867 dhcp_packet_t *dhpointer = (dhcp_packet_t *)(dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE);
1868 /* just copy what FreeRADIUS has encoded for us. */
1869 memcpy(dhpointer, packet->data, packet->data_len);
1871 /* UDP checksum is done here */
1872 uh->checksum = fr_udp_checksum((uint8_t const *)(dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE), ntohs(uh->len), uh->checksum,
1873 packet->src_ipaddr.ipaddr.ip4addr, packet->dst_ipaddr.ipaddr.ip4addr);
1875 if (fr_debug_lvl > 1) {
1877 char const *name = type_buf;
1878 char src_ip_buf[INET6_ADDRSTRLEN];
1879 char dst_ip_buf[INET6_ADDRSTRLEN];
1881 if ((packet->code >= PW_DHCP_DISCOVER) &&
1882 (packet->code <= PW_DHCP_INFORM)) {
1883 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1885 snprintf(type_buf, sizeof(type_buf), "%d",
1886 packet->code - PW_DHCP_OFFSET);
1890 "Sending %s Id %08x from %s:%d to %s:%d\n",
1891 name, (unsigned int) packet->id,
1892 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)), packet->src_port,
1893 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)), packet->dst_port);
1896 return sendto(sockfd, dhcp_packet,
1897 (ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE + packet->data_len),
1898 0, (struct sockaddr *) p_ll, sizeof(struct sockaddr_ll));
1902 * print an ethernet address in a buffer
1904 char * ether_addr_print(const uint8_t *addr, char *buf)
1906 sprintf (buf, "%02x:%02x:%02x:%02x:%02x:%02x",
1907 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
1912 * For a client, receive a DHCP packet from a raw packet
1913 * socket. Make sure it matches the ongoing request.
1915 * FIXME: split this into two, recv_raw_packet, and verify(packet, original)
1917 RADIUS_PACKET *fr_dhcp_recv_raw_packet(int sockfd, struct sockaddr_ll *p_ll, RADIUS_PACKET *request)
1920 RADIUS_PACKET *packet;
1922 uint32_t magic, xid;
1925 uint8_t *raw_packet;
1926 ethernet_header_t *eth_hdr;
1927 struct ip_header *ip_hdr;
1928 udp_header_t *udp_hdr;
1929 dhcp_packet_t *dhcp_hdr;
1930 uint16_t udp_src_port;
1931 uint16_t udp_dst_port;
1932 size_t dhcp_data_len;
1937 packet = rad_alloc(NULL, false);
1939 fr_strerror_printf("Failed allocating packet");
1943 raw_packet = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
1945 fr_strerror_printf("Out of memory");
1950 packet->sockfd = sockfd;
1952 /* a packet was received (but maybe it is not for us) */
1953 sock_len = sizeof(struct sockaddr_ll);
1954 data_len = recvfrom(sockfd, raw_packet, MAX_PACKET_SIZE, 0,
1955 (struct sockaddr *)p_ll, &sock_len);
1957 uint8_t data_offset = ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE; // DHCP data starts after Ethernet, IP, UDP.
1959 if (data_len <= data_offset) DISCARD_RP("Payload (%d) smaller than required for layers 2+3+4", (int)data_len);
1961 /* map raw packet to packet header of the different layers (Ethernet, IP, UDP) */
1962 eth_hdr = (ethernet_header_t *)raw_packet;
1964 /* a. Check Ethernet layer data (L2) */
1965 if (ntohs(eth_hdr->ether_type) != ETH_TYPE_IP) DISCARD_RP("Ethernet type (%d) != IP", ntohs(eth_hdr->ether_type));
1967 /* If Ethernet destination is not broadcast (ff:ff:ff:ff:ff:ff)
1968 * Check if it matches the source HW address used (DHCP-Client-Hardware-Address = 267)
1970 if ( (memcmp(ð_bcast, ð_hdr->ether_dst, ETH_ADDR_LEN) != 0) &&
1971 (vp = pairfind(request->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY)) &&
1972 (vp->length == sizeof(vp->vp_ether)) &&
1973 (memcmp(vp->vp_ether, ð_hdr->ether_dst, ETH_ADDR_LEN) != 0) ) {
1975 char eth_dest[17+1];
1976 char eth_req_src[17+1];
1977 DISCARD_RP("Ethernet destination (%s) is not broadcast and doesn't match request source (%s)",
1978 ether_addr_print(eth_hdr->ether_dst, eth_dest),
1979 ether_addr_print(vp->vp_ether, eth_req_src));
1983 * Ethernet is OK. Now look at IP.
1985 ip_hdr = (struct ip_header *)(raw_packet + ETH_HDR_SIZE);
1987 /* b. Check IPv4 layer data (L3) */
1988 if (ip_hdr->ip_p != IPPROTO_UDP) DISCARD_RP("IP protocol (%d) != UDP", ip_hdr->ip_p);
1991 * note: checking the destination IP address is not
1992 * useful (it would be the offered IP address - which we
1993 * don't know beforehand, or the broadcast address).
1999 udp_hdr = (udp_header_t *)(raw_packet + ETH_HDR_SIZE + IP_HDR_SIZE);
2001 /* c. Check UDP layer data (L4) */
2002 udp_src_port = ntohs(udp_hdr->src);
2003 udp_dst_port = ntohs(udp_hdr->dst);
2006 * A DHCP server will always respond to port 68 (to a
2007 * client) or 67 (to a relay). Just check that both
2008 * ports are 67 or 68.
2010 if (udp_src_port != 67 && udp_src_port != 68) DISCARD_RP("UDP src port (%d) != DHCP (67 or 68)", udp_src_port);
2011 if (udp_dst_port != 67 && udp_dst_port != 68) DISCARD_RP("UDP dst port (%d) != DHCP (67 or 68)", udp_dst_port);
2013 /* d. Check DHCP layer data */
2014 dhcp_data_len = data_len - data_offset;
2016 if (dhcp_data_len < MIN_PACKET_SIZE) DISCARD_RP("DHCP packet is too small (%d < %d)", dhcp_data_len, MIN_PACKET_SIZE);
2017 if (dhcp_data_len > MAX_PACKET_SIZE) DISCARD_RP("DHCP packet is too large (%d > %d)", dhcp_data_len, MAX_PACKET_SIZE);
2019 dhcp_hdr = (dhcp_packet_t *)(raw_packet + ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE);
2021 if (dhcp_hdr->htype != 1) DISCARD_RP("DHCP hardware type (%d) != Ethernet (1)", dhcp_hdr->htype);
2022 if (dhcp_hdr->hlen != 6) DISCARD_RP("DHCP hardware address length (%d) != 6", dhcp_hdr->hlen);
2024 magic = ntohl(dhcp_hdr->option_format);
2026 if (magic != DHCP_OPTION_MAGIC_NUMBER) DISCARD_RP("DHCP magic cookie (0x%04x) != DHCP (0x%04x)", magic, DHCP_OPTION_MAGIC_NUMBER);
2029 * Reply transaction id must match value from request.
2031 xid = ntohl(dhcp_hdr->xid);
2032 if (xid != (uint32_t)request->id) DISCARD_RP("DHCP transaction ID (0x%04x) != xid from request (0x%04x)", xid, request->id)
2034 /* all checks ok! this is a DHCP reply we're interested in. */
2035 packet->data_len = dhcp_data_len;
2036 packet->data = talloc_memdup(packet, raw_packet + data_offset, dhcp_data_len);
2037 TALLOC_FREE(raw_packet);
2040 code = dhcp_get_option((dhcp_packet_t *) packet->data,
2041 packet->data_len, 53);
2043 fr_strerror_printf("No message-type option was found in the packet");
2048 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
2049 fr_strerror_printf("Unknown value for message-type option");
2054 packet->code = code[2] | PW_DHCP_OFFSET;
2057 * Create a unique vector from the MAC address and the
2058 * DHCP opcode. This is a hack for the RADIUS
2059 * infrastructure in the rest of the server.
2061 * Note: packet->data[2] == 6, which is smaller than
2062 * sizeof(packet->vector)
2064 * FIXME: Look for client-identifier in packet,
2065 * and use that, too?
2067 memset(packet->vector, 0, sizeof(packet->vector));
2068 memcpy(packet->vector, packet->data + 28, packet->data[2]);
2069 packet->vector[packet->data[2]] = packet->code & 0xff;
2071 packet->src_port = udp_src_port;
2072 packet->dst_port = udp_dst_port;
2074 packet->src_ipaddr.af = AF_INET;
2075 packet->src_ipaddr.ipaddr.ip4addr.s_addr = ip_hdr->ip_src.s_addr;
2076 packet->dst_ipaddr.af = AF_INET;
2077 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = ip_hdr->ip_dst.s_addr;
2079 if (fr_debug_lvl > 1) {
2081 char const *name = type_buf;
2082 char src_ip_buf[256], dst_ip_buf[256];
2084 if ((packet->code >= PW_DHCP_DISCOVER) &&
2085 (packet->code <= PW_DHCP_INFORM)) {
2086 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
2088 snprintf(type_buf, sizeof(type_buf), "%d", packet->code - PW_DHCP_OFFSET);
2091 DEBUG("Received %s of Id %08x from %s:%d to %s:%d\n",
2092 name, (unsigned int) packet->id,
2093 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
2095 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),