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[] = {
146 "DHCP-Lease-Unassigned",
147 "DHCP-Lease-Unknown",
149 "DHCP-Bulk-Lease-Query",
150 "DHCP-Lease-Query-Done"
153 #define DHCP_MAX_MESSAGE_TYPE (sizeof(dhcp_message_types) / sizeof(dhcp_message_types[0]))
155 static int dhcp_header_sizes[] = {
166 * Some clients silently ignore responses less than 300 bytes.
168 #define MIN_PACKET_SIZE (244)
169 #define DEFAULT_PACKET_SIZE (300)
170 #define MAX_PACKET_SIZE (1500 - 40)
172 #define DHCP_OPTION_FIELD (0)
173 #define DHCP_FILE_FIELD (1)
174 #define DHCP_SNAME_FIELD (2)
176 static uint8_t *dhcp_get_option(dhcp_packet_t *packet, size_t packet_size,
180 int field = DHCP_OPTION_FIELD;
185 size = packet_size - offsetof(dhcp_packet_t, options);
186 data = &packet->options[where];
188 while (where < size) {
189 if (data[0] == 0) { /* padding */
194 if (data[0] == 255) { /* end of options */
195 if ((field == DHCP_OPTION_FIELD) &&
196 (overload & DHCP_FILE_FIELD)) {
199 size = sizeof(packet->file);
200 field = DHCP_FILE_FIELD;
203 } else if ((field == DHCP_FILE_FIELD) &&
204 (overload & DHCP_SNAME_FIELD)) {
205 data = packet->sname;
207 size = sizeof(packet->sname);
208 field = DHCP_SNAME_FIELD;
216 * We MUST have a real option here.
218 if ((where + 2) > size) {
219 fr_strerror_printf("Options overflow field at %u",
220 (unsigned int) (data - (uint8_t *) packet));
224 if ((where + 2 + data[1]) > size) {
225 fr_strerror_printf("Option length overflows field at %u",
226 (unsigned int) (data - (uint8_t *) packet));
230 if (data[0] == option) return data;
232 if (data[0] == 52) { /* overload sname and/or file */
236 where += data[1] + 2;
244 * DHCPv4 is only for IPv4. Broadcast only works if udpfromto is
247 RADIUS_PACKET *fr_dhcp_recv(int sockfd)
250 struct sockaddr_storage src;
251 struct sockaddr_storage dst;
252 socklen_t sizeof_src;
253 socklen_t sizeof_dst;
254 RADIUS_PACKET *packet;
259 packet = rad_alloc(NULL, false);
261 fr_strerror_printf("Failed allocating packet");
265 packet->data = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
267 fr_strerror_printf("Out of memory");
272 packet->sockfd = sockfd;
273 sizeof_src = sizeof(src);
274 #ifdef WITH_UDPFROMTO
275 sizeof_dst = sizeof(dst);
276 data_len = recvfromto(sockfd, packet->data, MAX_PACKET_SIZE, 0,
277 (struct sockaddr *)&src, &sizeof_src,
278 (struct sockaddr *)&dst, &sizeof_dst);
280 data_len = recvfrom(sockfd, packet->data, MAX_PACKET_SIZE, 0,
281 (struct sockaddr *)&src, &sizeof_src);
285 fr_strerror_printf("Failed reading DHCP socket: %s", fr_syserror(errno));
290 packet->data_len = data_len;
291 if (packet->data_len < MIN_PACKET_SIZE) {
292 fr_strerror_printf("DHCP packet is too small (%zu < %d)",
293 packet->data_len, MIN_PACKET_SIZE);
298 if (packet->data_len > MAX_PACKET_SIZE) {
299 fr_strerror_printf("DHCP packet is too large (%zx > %d)",
300 packet->data_len, MAX_PACKET_SIZE);
305 if (packet->data[1] > 1) {
306 fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
312 if ((packet->data[2] != 0) && (packet->data[2] != 6)) {
313 fr_strerror_printf("Ethernet HW length is wrong length %d",
319 memcpy(&magic, packet->data + 236, 4);
320 magic = ntohl(magic);
321 if (magic != DHCP_OPTION_MAGIC_NUMBER) {
322 fr_strerror_printf("Cannot do BOOTP");
328 * Create unique keys for the packet.
330 memcpy(&magic, packet->data + 4, 4);
331 packet->id = ntohl(magic);
333 code = dhcp_get_option((dhcp_packet_t *) packet->data,
334 packet->data_len, 53);
336 fr_strerror_printf("No message-type option was found in the packet");
341 if ((code[1] < 1) || (code[2] == 0) || (code[2] >= DHCP_MAX_MESSAGE_TYPE)) {
342 fr_strerror_printf("Unknown value %d for message-type option", code[2]);
347 packet->code = code[2] | PW_DHCP_OFFSET;
350 * Create a unique vector from the MAC address and the
351 * DHCP opcode. This is a hack for the RADIUS
352 * infrastructure in the rest of the server.
354 * Note: packet->data[2] == 6, which is smaller than
355 * sizeof(packet->vector)
357 * FIXME: Look for client-identifier in packet,
360 memset(packet->vector, 0, sizeof(packet->vector));
361 memcpy(packet->vector, packet->data + 28, packet->data[2]);
362 packet->vector[packet->data[2]] = packet->code & 0xff;
365 * FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
366 * FIXME: for OFFER / ACK : src_port = dst_port - 1
370 * Unique keys are xid, client mac, and client ID?
374 * FIXME: More checks, like DHCP packet type?
377 sizeof_dst = sizeof(dst);
379 #ifndef WITH_UDPFROMTO
381 * This should never fail...
383 if (getsockname(sockfd, (struct sockaddr *) &dst, &sizeof_dst) < 0) {
384 fr_strerror_printf("getsockname failed: %s", fr_syserror(errno));
390 fr_sockaddr2ipaddr(&dst, sizeof_dst, &packet->dst_ipaddr, &port);
391 packet->dst_port = port;
393 fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
394 packet->src_port = port;
396 if (fr_debug_lvl > 1) {
398 char const *name = type_buf;
399 char src_ip_buf[256], dst_ip_buf[256];
401 if ((packet->code >= PW_DHCP_DISCOVER) &&
402 (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
403 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
405 snprintf(type_buf, sizeof(type_buf), "%d",
406 packet->code - PW_DHCP_OFFSET);
409 DEBUG("Received %s of Id %08x from %s:%d to %s:%d\n",
410 name, (unsigned int) packet->id,
411 inet_ntop(packet->src_ipaddr.af,
412 &packet->src_ipaddr.ipaddr,
413 src_ip_buf, sizeof(src_ip_buf)),
415 inet_ntop(packet->dst_ipaddr.af,
416 &packet->dst_ipaddr.ipaddr,
417 dst_ip_buf, sizeof(dst_ip_buf)),
426 * Send a DHCP packet.
428 int fr_dhcp_send(RADIUS_PACKET *packet)
430 struct sockaddr_storage dst;
431 socklen_t sizeof_dst;
432 #ifdef WITH_UDPFROMTO
433 struct sockaddr_storage src;
434 socklen_t sizeof_src;
436 fr_ipaddr2sockaddr(&packet->src_ipaddr, packet->src_port,
440 fr_ipaddr2sockaddr(&packet->dst_ipaddr, packet->dst_port,
443 if (packet->data_len == 0) {
444 fr_strerror_printf("No data to send");
448 if (fr_debug_lvl > 1) {
450 char const *name = type_buf;
451 #ifdef WITH_UDPFROMTO
452 char src_ip_buf[INET6_ADDRSTRLEN];
454 char dst_ip_buf[INET6_ADDRSTRLEN];
456 if ((packet->code >= PW_DHCP_DISCOVER) &&
457 (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
458 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
460 snprintf(type_buf, sizeof(type_buf), "%d",
461 packet->code - PW_DHCP_OFFSET);
465 #ifdef WITH_UDPFROMTO
466 "Sending %s Id %08x from %s:%d to %s:%d\n",
468 "Sending %s Id %08x to %s:%d\n",
470 name, (unsigned int) packet->id,
471 #ifdef WITH_UDPFROMTO
472 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
475 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),
479 #ifndef WITH_UDPFROMTO
481 * Assume that the packet is encoded before sending it.
483 return sendto(packet->sockfd, packet->data, packet->data_len, 0,
484 (struct sockaddr *)&dst, sizeof_dst);
487 return sendfromto(packet->sockfd, packet->data, packet->data_len, 0,
488 (struct sockaddr *)&src, sizeof_src,
489 (struct sockaddr *)&dst, sizeof_dst);
493 static int fr_dhcp_attr2vp(TALLOC_CTX *ctx, VALUE_PAIR **vp_p, uint8_t const *p, size_t alen);
495 /** Returns the number of array members for arrays with fixed element sizes
498 static int fr_dhcp_array_members(size_t *len, DICT_ATTR const *da)
503 * Could be an array of bytes, integers, etc.
505 if (da->flags.array) switch (da->type) {
511 case PW_TYPE_SHORT: /* ignore any trailing data */
512 num_entries = *len >> 1;
516 case PW_TYPE_IPV4_ADDR:
517 case PW_TYPE_INTEGER:
518 case PW_TYPE_DATE: /* ignore any trailing data */
519 num_entries = *len >> 2;
523 case PW_TYPE_IPV6_ADDR:
524 num_entries = *len >> 4;
535 /** RFC 4243 Vendor Specific Suboptions
537 * Vendor specific suboptions are in the format.
540 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
541 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542 | Enterprise Number 0 |
543 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
545 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
547 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
548 | Enterprise Number n |
549 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556 * So although the vendor is identified, the format of the data isn't
557 * specified so we can't actually resolve the suboption to an
558 * attribute. For now, we just convert it to an attribute of
559 * DHCP-Vendor-Specific-Information with raw octets contents.
563 /** Decode DHCP suboptions
565 * @param[in,out] tlv to decode. *tlv will be set to the head of the list of suboptions and original will be freed.
566 * @param[in] ctx context to alloc new attributes in.
567 * @param[in] data to parse.
568 * @param[in] len length of data to parse.
570 static int fr_dhcp_decode_suboption(TALLOC_CTX *ctx, VALUE_PAIR **tlv, uint8_t const *data, size_t len)
572 uint8_t const *p, *q;
573 VALUE_PAIR *head, *vp;
577 * TLV must already point to a VALUE_PAIR.
582 * Take a pass at parsing it.
588 * RFC 3046 is very specific about not allowing termination
589 * with a 255 sub-option. But it's required for decoding
590 * option 43, and vendors will probably screw it up
603 * Check if reading length would take us past the end of the buffer
605 if (++p >= q) goto malformed;
609 * Check if length > the length of the buffer we have left
611 if (p >= q) goto malformed;
616 * Got here... must be well formed.
619 fr_cursor_init(&cursor, &head);
631 * The initial OID string looks like:
634 * If <iana>.0 is type TLV then we attempt to decode its contents as more
635 * DHCP suboptions, which gives us:
638 * If <iana>.0 is not defined in the dictionary or is type octets, we leave
639 * the attribute as is.
641 attr = (*tlv)->da->attr ? ((*tlv)->da->attr | (p[0] << 8)) : p[0];
644 * Use the vendor of the parent TLV which is not necessarily
647 * Note: This does not deal with dictionary numbering clashes. If
648 * the vendor uses different numbers for DHCP suboptions and RADIUS
649 * attributes then it's time to break out %{hex:} and regular
652 da = dict_attrbyvalue(attr, (*tlv)->da->vendor);
654 da = dict_unknown_afrom_fields(ctx, attr, (*tlv)->da->vendor);
656 fr_pair_list_free(&head);
663 num_entries = fr_dhcp_array_members(&a_len, da);
664 for (i = 0; i < num_entries; i++) {
665 vp = fr_pair_afrom_da(ctx, da);
667 fr_pair_list_free(&head);
671 fr_pair_steal(ctx, vp); /* for unknown attributes hack */
673 if (fr_dhcp_attr2vp(ctx, &vp, a_p, a_len) < 0) {
675 fr_pair_list_free(&head);
678 fr_cursor_merge(&cursor, vp);
683 dict_attr_free(&da); /* for unknown attributes hack */
685 p += 2 + p[1]; /* code (1) + len (1) + suboption len (n)*/
689 * The caller allocated a TLV, if decoding it generated
690 * additional attributes, we now need to free it, and write
691 * the HEAD of our new list of attributes in its place.
694 vp_cursor_t tlv_cursor;
697 * Free the old TLV attribute
702 * Cursor not necessary but means we don't have to set
705 fr_cursor_init(&tlv_cursor, tlv);
706 fr_cursor_merge(&tlv_cursor, head);
712 fr_pair_to_unknown(*tlv);
713 fr_pair_value_memcpy(*tlv, data, len);
719 * Decode ONE value into a VP
721 static int fr_dhcp_attr2vp(TALLOC_CTX *ctx, VALUE_PAIR **vp_p, uint8_t const *data, size_t len)
723 VALUE_PAIR *vp = *vp_p;
726 switch (vp->da->type) {
728 if (len != 1) goto raw;
729 vp->vp_byte = data[0];
733 if (len != 2) goto raw;
734 memcpy(&vp->vp_short, data, 2);
735 vp->vp_short = ntohs(vp->vp_short);
738 case PW_TYPE_INTEGER:
739 if (len != 4) goto raw;
740 memcpy(&vp->vp_integer, data, 4);
741 vp->vp_integer = ntohl(vp->vp_integer);
744 case PW_TYPE_IPV4_ADDR:
745 if (len != 4) goto raw;
747 * Keep value in Network Order!
749 memcpy(&vp->vp_ipaddr, data, 4);
754 * In DHCPv4, string options which can also be arrays,
755 * have their values '\0' delimited.
760 uint8_t const *q, *end;
764 q = end = data + len;
766 if (!vp->da->flags.array) {
767 fr_pair_value_bstrncpy(vp, (char const *)p, q - p);
772 * Initialise the cursor as we may be inserting
773 * multiple additional VPs
775 fr_cursor_init(&cursor, vp_p);
777 q = memchr(p, '\0', q - p);
778 /* Malformed but recoverable */
781 fr_pair_value_bstrncpy(vp, (char const *)p, q - p);
784 /* Need another VP for the next round */
786 vp = fr_pair_afrom_da(ctx, vp->da);
788 fr_pair_list_free(vp_p);
791 fr_cursor_insert(&cursor, vp);
799 case PW_TYPE_ETHERNET:
800 memcpy(vp->vp_ether, data, sizeof(vp->vp_ether));
801 vp->vp_length = sizeof(vp->vp_ether);
805 * Value doesn't match up with attribute type, overwrite the
806 * vp's original DICT_ATTR with an unknown one.
809 if (fr_pair_to_unknown(vp) < 0) return -1;
812 if (len > 255) return -1;
813 fr_pair_value_memcpy(vp, data, len);
817 * For option 82 et al...
820 return fr_dhcp_decode_suboption(ctx, vp_p, data, len);
823 fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
825 } /* switch over type */
831 /** Decode DHCP options
833 * @param[in,out] out Where to write the decoded options.
834 * @param[in] ctx context to alloc new attributes in.
835 * @param[in] data to parse.
836 * @param[in] len of data to parse.
838 ssize_t fr_dhcp_decode_options(TALLOC_CTX *ctx, VALUE_PAIR **out, uint8_t const *data, size_t len)
842 uint8_t const *p, *q;
845 fr_cursor_init(&cursor, out);
848 * FIXME: This should also check sname && file fields.
849 * See the dhcp_get_option() function above.
860 if (*p == 0) { /* 0x00 - Padding option */
865 if (*p == 255) { /* 0xff - End of options signifier */
869 if ((p + 2) > q) break;
875 * Ensure we've not been given a bad length value
877 if ((a_p + a_len) > q) {
878 fr_strerror_printf("Length field value of option %u is incorrect. "
879 "Got %u bytes, expected <= %zu bytes", p[0], p[1], q - a_p);
880 fr_pair_list_free(out);
885 * Unknown attribute, create an octets type
886 * attribute with the contents of the sub-option.
888 da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
890 da = dict_unknown_afrom_fields(ctx, p[0], DHCP_MAGIC_VENDOR);
892 fr_pair_list_free(out);
895 vp = fr_pair_afrom_da(ctx, da);
897 fr_pair_list_free(out);
900 fr_pair_value_memcpy(vp, a_p, a_len);
901 fr_cursor_insert(&cursor, vp);
907 * Array type sub-option create a new VALUE_PAIR
908 * for each array element.
910 num_entries = fr_dhcp_array_members(&a_len, da);
911 for (i = 0; i < num_entries; i++) {
912 vp = fr_pair_afrom_da(ctx, da);
914 fr_pair_list_free(out);
919 if (fr_dhcp_attr2vp(ctx, &vp, a_p, a_len) < 0) {
920 fr_pair_list_free(&vp);
921 fr_pair_list_free(out);
924 fr_cursor_merge(&cursor, vp);
926 } /* loop over array entries */
928 p += 2 + p[1]; /* code (1) + len (1) + option len (n)*/
929 } /* loop over the entire packet */
934 int fr_dhcp_decode(RADIUS_PACKET *packet)
940 VALUE_PAIR *head = NULL, *vp;
941 VALUE_PAIR *maxms, *mtu;
943 fr_cursor_init(&cursor, &head);
946 if ((fr_debug_lvl > 2) && fr_log_fp) {
947 for (i = 0; i < packet->data_len; i++) {
948 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
949 fprintf(fr_log_fp, "%02x ", packet->data[i]);
950 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
952 fprintf(fr_log_fp, "\n");
955 if (packet->data[1] > 1) {
956 fr_strerror_printf("Packet is not Ethernet: %u",
964 for (i = 0; i < 14; i++) {
967 vp = fr_pair_make(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
970 strlcpy(buffer, fr_strerror(), sizeof(buffer));
971 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
972 fr_pair_list_free(&head);
977 * If chaddr != 6 bytes it's probably not ethernet, and we should store
978 * it as an opaque type (octets).
982 * Skip chaddr if it doesn't exist.
984 if ((packet->data[1] == 0) || (packet->data[2] == 0)) continue;
986 if ((packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
987 DICT_ATTR const *da = dict_unknown_afrom_fields(packet, vp->da->attr, vp->da->vendor);
995 switch (vp->da->type) {
1002 vp->vp_short = (p[0] << 8) | p[1];
1006 case PW_TYPE_INTEGER:
1007 memcpy(&vp->vp_integer, p, 4);
1008 vp->vp_integer = ntohl(vp->vp_integer);
1012 case PW_TYPE_IPV4_ADDR:
1013 memcpy(&vp->vp_ipaddr, p, 4);
1017 case PW_TYPE_STRING:
1018 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1020 memcpy(q, p, dhcp_header_sizes[i]);
1021 q[dhcp_header_sizes[i]] = '\0';
1022 vp->vp_length = strlen(vp->vp_strvalue);
1023 if (vp->vp_length == 0) {
1024 fr_pair_list_free(&vp);
1028 case PW_TYPE_OCTETS:
1029 if (packet->data[2] == 0) break;
1031 fr_pair_value_memcpy(vp, p, packet->data[2]);
1034 case PW_TYPE_ETHERNET:
1035 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1036 vp->vp_length = sizeof(vp->vp_ether);
1040 fr_strerror_printf("BAD TYPE %d", vp->da->type);
1041 fr_pair_list_free(&vp);
1044 p += dhcp_header_sizes[i];
1049 fr_cursor_insert(&cursor, vp);
1053 * Loop over the options.
1057 * Nothing uses tail after this call, if it does in the future
1058 * it'll need to find the new tail...
1061 VALUE_PAIR *options = NULL;
1062 vp_cursor_t options_cursor;
1064 if (fr_dhcp_decode_options(packet, &options, packet->data + 240, packet->data_len - 240) < 0) {
1069 for (vp = fr_cursor_init(&options_cursor, &options);
1071 vp = fr_cursor_next(&options_cursor)) {
1074 fr_cursor_merge(&cursor, options);
1079 * If DHCP request, set ciaddr to zero.
1083 * Set broadcast flag for broken vendors, but only if
1086 memcpy(&giaddr, packet->data + 24, sizeof(giaddr));
1087 if (giaddr == htonl(INADDR_ANY)) {
1089 * DHCP Opcode is request
1091 vp = fr_pair_find_by_num(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1092 if (vp && vp->vp_integer == 3) {
1094 * Vendor is "MSFT 98"
1096 vp = fr_pair_find_by_num(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
1097 if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
1098 vp = fr_pair_find_by_num(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
1101 * Reply should be broadcast.
1103 if (vp) vp->vp_short |= 0x8000;
1104 packet->data[10] |= 0x80;
1110 * FIXME: Nuke attributes that aren't used in the normal
1111 * header for discover/requests.
1116 * Client can request a LARGER size, but not a smaller
1117 * one. They also cannot request a size larger than MTU.
1119 maxms = fr_pair_find_by_num(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1120 mtu = fr_pair_find_by_num(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
1122 if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
1123 fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification");
1127 if (maxms && (maxms->vp_integer < DEFAULT_PACKET_SIZE)) {
1128 fr_strerror_printf("DHCP WARNING: Client says maximum message size is smaller than minimum permitted by the specification: fixing it");
1129 maxms->vp_integer = DEFAULT_PACKET_SIZE;
1132 if (maxms && mtu && (maxms->vp_integer > mtu->vp_integer)) {
1133 fr_strerror_printf("DHCP WARNING: Client says MTU is smaller than maximum message size: fixing it");
1134 maxms->vp_integer = mtu->vp_integer;
1137 if (fr_debug_lvl) fflush(stdout);
1143 int8_t fr_dhcp_attr_cmp(void const *a, void const *b)
1145 VALUE_PAIR const *my_a = a;
1146 VALUE_PAIR const *my_b = b;
1152 * DHCP-Message-Type is first, for simplicity.
1154 if ((my_a->da->attr == 53) && (my_b->da->attr != 53)) return -1;
1157 * Relay-Agent is last
1159 if ((my_a->da->attr == 82) && (my_b->da->attr != 82)) return 1;
1161 if (my_a->da->attr < my_b->da->attr) return -1;
1162 if (my_a->da->attr > my_b->da->attr) return 1;
1167 /** Write DHCP option value into buffer
1169 * Does not include DHCP option length or number.
1171 * @param out where to write the DHCP option.
1172 * @param outlen length of output buffer.
1173 * @param vp option to encode.
1174 * @return the length of data writen, -1 if out of buffer, -2 if unsupported type.
1176 static ssize_t fr_dhcp_vp2data(uint8_t *out, size_t outlen, VALUE_PAIR *vp)
1181 if (outlen < vp->vp_length) {
1185 switch (vp->da->type) {
1191 p[0] = (vp->vp_short >> 8) & 0xff;
1192 p[1] = vp->vp_short & 0xff;
1195 case PW_TYPE_INTEGER:
1196 lvalue = htonl(vp->vp_integer);
1197 memcpy(p, &lvalue, 4);
1200 case PW_TYPE_IPV4_ADDR:
1201 memcpy(p, &vp->vp_ipaddr, 4);
1204 case PW_TYPE_ETHERNET:
1205 memcpy(p, vp->vp_ether, 6);
1208 case PW_TYPE_STRING:
1209 memcpy(p, vp->vp_strvalue, vp->vp_length);
1212 case PW_TYPE_OCTETS:
1213 memcpy(p, vp->vp_octets, vp->vp_length);
1217 fr_strerror_printf("Unsupported option type %d", vp->da->type);
1221 return vp->vp_length;
1224 /** Create a new TLV attribute from multiple sub options
1226 * @param[in,out] out buffer to write the data
1227 * @param[out] outlen length of the output buffer
1228 * @param[in,out] cursor should be set to the start of the list of TLV attributes.
1229 * Will be advanced to the first non-TLV attribute.
1230 * @return length of data encoded, or -1 on error
1232 static ssize_t fr_dhcp_vp2data_tlv(uint8_t *out, ssize_t outlen, vp_cursor_t *cursor)
1235 unsigned int parent; /* Parent attribute of suboption */
1237 uint8_t *p, *opt_len;
1238 vp_cursor_t tlv_cursor;
1241 #define SUBOPTION_PARENT(_x) (_x & 0xffff00ff)
1242 #define SUBOPTION_ATTR(_x) ((_x & 0xff00) >> 8)
1244 vp = fr_cursor_current(cursor);
1247 parent = SUBOPTION_PARENT(vp->da->attr);
1250 * Remember where we started off.
1252 fr_cursor_copy(&tlv_cursor, cursor);
1255 * Loop over TLVs to determine how much memory we need to allocate
1257 * We advanced the tlv_cursor we were passed, so if we
1258 * fail encoding, the tlv_cursor is at the right position
1259 * for the next potentially encodable attr.
1262 for (vp = fr_cursor_current(&tlv_cursor);
1263 vp && vp->da->flags.is_tlv && (SUBOPTION_PARENT(vp->da->attr) == parent);
1264 vp = fr_cursor_next(&tlv_cursor)) {
1265 if (SUBOPTION_ATTR(vp->da->attr) == 0) {
1266 fr_strerror_printf("Invalid attribute number 0");
1271 * If it's not an array type or is an array type,
1272 * but is not the same as the previous attribute,
1273 * we add 2 for the additional sub-option header
1276 if (!vp->da->flags.array || (SUBOPTION_ATTR(vp->da->attr) != attr)) {
1277 attr = SUBOPTION_ATTR(vp->da->attr);
1280 len += vp->vp_length;
1284 fr_strerror_printf("Insufficient room for suboption");
1292 for (vp = fr_cursor_current(cursor);
1293 vp && vp->da->flags.is_tlv && (SUBOPTION_PARENT(vp->da->attr) == parent);
1294 vp = fr_cursor_next(cursor)) {
1295 /* Don't write out the header, were packing array options */
1296 if (!opt_len || !vp->da->flags.array || (attr != SUBOPTION_ATTR(vp->da->attr))) {
1297 attr = SUBOPTION_ATTR(vp->da->attr);
1303 len = fr_dhcp_vp2data(p, out + outlen - p, vp);
1304 if ((len < 0) || (len > 255)) {
1316 /** Encode a DHCP option and any sub-options.
1318 * @param out Where to write encoded DHCP attributes.
1319 * @param outlen Length of out buffer.
1320 * @param ctx to use for any allocated memory.
1321 * @param cursor with current VP set to the option to be encoded. Will be advanced to the next option to encode.
1322 * @return > 0 length of data written, < 0 error, 0 not valid option (skipping).
1324 ssize_t fr_dhcp_encode_option(UNUSED TALLOC_CTX *ctx, uint8_t *out, size_t outlen, vp_cursor_t *cursor)
1327 DICT_ATTR const *previous;
1328 uint8_t *opt_len, *p = out;
1329 size_t freespace = outlen;
1332 vp = fr_cursor_current(cursor);
1335 if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next; /* not a DHCP option */
1336 if (vp->da->attr == 53) goto next; /* already done */
1337 if ((vp->da->attr > 255) && (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
1339 if (vp->da->flags.extended) {
1341 fr_strerror_printf("Attribute \"%s\" is not a DHCP option", vp->da->name);
1342 fr_cursor_next(cursor);
1346 /* Write out the option number */
1347 *(p++) = vp->da->attr & 0xff;
1349 /* Pointer to the length field of the option */
1352 /* Zero out the option's length field */
1355 /* We just consumed two bytes for the header */
1358 /* DHCP options with the same number get coalesced into a single option */
1361 * Sub-option encoder will encode the data and
1362 * advance the cursor.
1364 if (vp->da->flags.is_tlv) {
1365 len = fr_dhcp_vp2data_tlv(p, freespace, cursor);
1369 len = fr_dhcp_vp2data(p, freespace, vp);
1370 if (len >= 0) debug_pair(vp);
1371 fr_cursor_next(cursor);
1375 if (len < 0) return len;
1377 if ((*opt_len + len) > 255) {
1378 fr_strerror_printf("Skipping \"%s\": Option splitting not supported "
1379 "(option > 255 bytes)", vp->da->name);
1387 } while ((vp = fr_cursor_current(cursor)) && previous && (previous == vp->da) && vp->da->flags.array);
1392 int fr_dhcp_encode(RADIUS_PACKET *packet)
1404 # ifdef WITH_UDPFROMTO
1405 char src_ip_buf[256];
1407 char dst_ip_buf[256];
1410 if (packet->data) return 0;
1412 packet->data_len = MAX_PACKET_SIZE;
1413 packet->data = talloc_zero_array(packet, uint8_t, packet->data_len);
1415 /* XXX Ugly ... should be set by the caller */
1416 if (packet->code == 0) packet->code = PW_DHCP_NAK;
1419 if ((vp = fr_pair_find_by_num(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1420 packet->id = vp->vp_integer;
1422 packet->id = fr_rand();
1426 if ((packet->code >= PW_DHCP_DISCOVER) &&
1427 (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
1428 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1434 # ifdef WITH_UDPFROMTO
1435 "Encoding %s of id %08x from %s:%d to %s:%d\n",
1437 "Encoding %s of id %08x to %s:%d\n",
1439 name, (unsigned int) packet->id,
1440 # ifdef WITH_UDPFROMTO
1441 inet_ntop(packet->src_ipaddr.af,
1442 &packet->src_ipaddr.ipaddr,
1443 src_ip_buf, sizeof(src_ip_buf)),
1446 inet_ntop(packet->dst_ipaddr.af,
1447 &packet->dst_ipaddr.ipaddr,
1448 dst_ip_buf, sizeof(dst_ip_buf)),
1455 * @todo: Make this work again.
1458 mms = DEFAULT_PACKET_SIZE; /* maximum message size */
1461 * Clients can request a LARGER size, but not a
1462 * smaller one. They also cannot request a size
1466 /* DHCP-DHCP-Maximum-Msg-Size */
1467 vp = fr_pair_find_by_num(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
1468 if (vp && (vp->vp_integer > mms)) {
1469 mms = vp->vp_integer;
1471 if (mms > MAX_PACKET_SIZE) mms = MAX_PACKET_SIZE;
1475 vp = fr_pair_find_by_num(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
1477 *p++ = vp->vp_integer & 0xff;
1479 *p++ = 1; /* client message */
1482 /* DHCP-Hardware-Type */
1483 if ((vp = fr_pair_find_by_num(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1486 *p++ = 1; /* hardware type = ethernet */
1489 /* DHCP-Hardware-Address-Length */
1490 if ((vp = fr_pair_find_by_num(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1493 *p++ = 6; /* 6 bytes of ethernet */
1496 /* DHCP-Hop-Count */
1497 if ((vp = fr_pair_find_by_num(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1502 /* DHCP-Transaction-Id */
1503 lvalue = htonl(packet->id);
1504 memcpy(p, &lvalue, 4);
1507 /* DHCP-Number-of-Seconds */
1508 if ((vp = fr_pair_find_by_num(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1509 svalue = htons(vp->vp_short);
1510 memcpy(p, &svalue, 2);
1515 if ((vp = fr_pair_find_by_num(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1516 svalue = htons(vp->vp_short);
1517 memcpy(p, &svalue, 2);
1521 /* DHCP-Client-IP-Address */
1522 if ((vp = fr_pair_find_by_num(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1523 memcpy(p, &vp->vp_ipaddr, 4);
1527 /* DHCP-Your-IP-address */
1528 if ((vp = fr_pair_find_by_num(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1529 lvalue = vp->vp_ipaddr;
1531 lvalue = htonl(INADDR_ANY);
1533 memcpy(p, &lvalue, 4);
1536 /* DHCP-Server-IP-Address */
1537 vp = fr_pair_find_by_num(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
1539 lvalue = vp->vp_ipaddr;
1541 lvalue = htonl(INADDR_ANY);
1543 memcpy(p, &lvalue, 4);
1547 * DHCP-Gateway-IP-Address
1549 if ((vp = fr_pair_find_by_num(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1550 lvalue = vp->vp_ipaddr;
1552 lvalue = htonl(INADDR_ANY);
1554 memcpy(p, &lvalue, 4);
1557 /* DHCP-Client-Hardware-Address */
1558 if ((vp = fr_pair_find_by_num(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1559 if (vp->vp_length == sizeof(vp->vp_ether)) {
1561 * Ensure that we mark the packet as being Ethernet.
1562 * This is mainly for DHCP-Lease-Query responses.
1564 packet->data[1] = 1;
1565 packet->data[2] = 6;
1567 memcpy(p, vp->vp_ether, vp->vp_length);
1568 } /* else ignore it */
1570 p += DHCP_CHADDR_LEN;
1572 /* DHCP-Server-Host-Name */
1573 if ((vp = fr_pair_find_by_num(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1574 if (vp->vp_length > DHCP_SNAME_LEN) {
1575 memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
1577 memcpy(p, vp->vp_strvalue, vp->vp_length);
1580 p += DHCP_SNAME_LEN;
1583 * Copy over DHCP-Boot-Filename.
1585 * FIXME: This copy should be delayed until AFTER the options
1586 * have been processed. If there are too many options for
1587 * the packet, then they go into the sname && filename fields.
1588 * When that happens, the boot filename is passed as an option,
1589 * instead of being placed verbatim in the filename field.
1592 /* DHCP-Boot-Filename */
1593 vp = fr_pair_find_by_num(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY);
1595 if (vp->vp_length > DHCP_FILE_LEN) {
1596 memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
1598 memcpy(p, vp->vp_strvalue, vp->vp_length);
1603 /* DHCP magic number */
1604 lvalue = htonl(DHCP_OPTION_MAGIC_NUMBER);
1605 memcpy(p, &lvalue, 4);
1611 if (fr_debug_lvl > 1) {
1616 for (i = 0; i < 14; i++) {
1619 vp = fr_pair_make(packet, NULL,
1620 dhcp_header_names[i], NULL, T_OP_EQ);
1623 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1624 fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
1628 switch (vp->da->type) {
1634 vp->vp_short = (p[0] << 8) | p[1];
1637 case PW_TYPE_INTEGER:
1638 memcpy(&vp->vp_integer, p, 4);
1639 vp->vp_integer = ntohl(vp->vp_integer);
1642 case PW_TYPE_IPV4_ADDR:
1643 memcpy(&vp->vp_ipaddr, p, 4);
1646 case PW_TYPE_STRING:
1647 vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
1649 memcpy(q, p, dhcp_header_sizes[i]);
1650 q[dhcp_header_sizes[i]] = '\0';
1651 vp->vp_length = strlen(vp->vp_strvalue);
1654 case PW_TYPE_OCTETS: /* only for Client HW Address */
1655 fr_pair_value_memcpy(vp, p, packet->data[2]);
1658 case PW_TYPE_ETHERNET: /* only for Client HW Address */
1659 memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
1663 fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
1664 fr_pair_list_free(&vp);
1668 p += dhcp_header_sizes[i];
1671 fr_pair_list_free(&vp);
1675 * Jump over DHCP magic number, response, etc.
1680 p[0] = 0x35; /* DHCP-Message-Type */
1682 p[2] = packet->code - PW_DHCP_OFFSET;
1686 * Pre-sort attributes into contiguous blocks so that fr_dhcp_encode_option
1687 * operates correctly. This changes the order of the list, but never mind...
1689 fr_pair_list_sort(&packet->vps, fr_dhcp_attr_cmp);
1690 fr_cursor_init(&cursor, &packet->vps);
1693 * Each call to fr_dhcp_encode_option will encode one complete DHCP option,
1696 while ((vp = fr_cursor_current(&cursor))) {
1697 len = fr_dhcp_encode_option(packet, p, packet->data_len - (p - packet->data), &cursor);
1702 p[0] = 0xff; /* end of option option */
1705 dhcp_size = p - packet->data;
1708 * FIXME: if (dhcp_size > mms),
1709 * then we put the extra options into the "sname" and "file"
1710 * fields, AND set the "end option option" in the "options"
1711 * field. We also set the "overload option",
1712 * and put options into the "file" field, followed by
1713 * the "sname" field. Where each option is completely
1714 * enclosed in the "file" and/or "sname" field, AND
1715 * followed by the "end of option", and MUST be followed
1716 * by padding option.
1718 * Yuck. That sucks...
1720 packet->data_len = dhcp_size;
1722 if (packet->data_len < DEFAULT_PACKET_SIZE) {
1723 memset(packet->data + packet->data_len, 0,
1724 DEFAULT_PACKET_SIZE - packet->data_len);
1725 packet->data_len = DEFAULT_PACKET_SIZE;
1728 if ((fr_debug_lvl > 2) && fr_log_fp) {
1729 fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
1730 for (i = 0; i < packet->data_len; i++) {
1731 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
1732 fprintf(fr_log_fp, "%02x ", packet->data[i]);
1733 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1735 fprintf(fr_log_fp, "\n");
1742 int fr_dhcp_add_arp_entry(int fd, char const *interface,
1743 VALUE_PAIR *macaddr, VALUE_PAIR *ip)
1745 struct sockaddr_in *sin;
1749 fr_strerror_printf("No interface specified. Cannot update ARP table");
1753 if (!fr_assert(macaddr) ||
1754 !fr_assert((macaddr->da->type == PW_TYPE_ETHERNET) || (macaddr->da->type == PW_TYPE_OCTETS))) {
1755 fr_strerror_printf("Wrong VP type (%s) for chaddr",
1756 fr_int2str(dict_attr_types, macaddr->da->type, "<invalid>"));
1760 if (macaddr->vp_length > sizeof(req.arp_ha.sa_data)) {
1761 fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
1762 sizeof(req.arp_ha.sa_data), macaddr->vp_length);
1766 memset(&req, 0, sizeof(req));
1767 sin = (struct sockaddr_in *) &req.arp_pa;
1768 sin->sin_family = AF_INET;
1769 sin->sin_addr.s_addr = ip->vp_ipaddr;
1771 strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
1773 if (macaddr->da->type == PW_TYPE_ETHERNET) {
1774 memcpy(&req.arp_ha.sa_data, macaddr->vp_ether, sizeof(macaddr->vp_ether));
1776 memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->vp_length);
1779 req.arp_flags = ATF_COM;
1780 if (ioctl(fd, SIOCSARP, &req) < 0) {
1781 fr_strerror_printf("Failed to add entry in ARP cache: %s (%d)", fr_syserror(errno), errno);
1788 int fr_dhcp_add_arp_entry(UNUSED int fd, UNUSED char const *interface,
1789 UNUSED VALUE_PAIR *macaddr, UNUSED VALUE_PAIR *ip)
1791 fr_strerror_printf("Adding ARP entry is unsupported on this system");
1797 #ifdef HAVE_LINUX_IF_PACKET_H
1799 * Open a packet interface raw socket.
1800 * Bind it to the specified interface using a device independent physical layer address.
1802 int fr_socket_packet(int iface_index, struct sockaddr_ll *p_ll)
1806 /* PF_PACKET - packet interface on device level.
1807 using a raw socket allows packet data to be unchanged by the device driver.
1809 lsockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1811 fr_strerror_printf("cannot open socket: %s", fr_syserror(errno));
1815 /* Set link layer parameters */
1816 memset(p_ll, 0, sizeof(struct sockaddr_ll));
1818 p_ll->sll_family = AF_PACKET;
1819 p_ll->sll_protocol = htons(ETH_P_ALL);
1820 p_ll->sll_ifindex = iface_index;
1821 p_ll->sll_hatype = ARPHRD_ETHER;
1822 p_ll->sll_pkttype = PACKET_OTHERHOST;
1823 p_ll->sll_halen = 6;
1825 if (bind(lsockfd, (struct sockaddr *)p_ll, sizeof(struct sockaddr_ll)) < 0) {
1827 fr_strerror_printf("cannot bind raw socket: %s", fr_syserror(errno));
1835 * Encode and send a DHCP packet on a raw packet socket.
1837 int fr_dhcp_send_raw_packet(int sockfd, struct sockaddr_ll *p_ll, RADIUS_PACKET *packet)
1840 u_char dhcp_packet[1518] = { 0 };
1842 /* set ethernet source address to our MAC address (DHCP-Client-Hardware-Address). */
1843 u_char dhmac[ETH_ADDR_LEN] = { 0 };
1844 if ((vp = fr_pair_find_by_num(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
1845 if (vp->length == sizeof(vp->vp_ether)) {
1846 memcpy(dhmac, vp->vp_ether, vp->length);
1850 /* fill in Ethernet layer (L2) */
1851 struct ethernet_header *ethhdr = (struct ethernet_header *)dhcp_packet;
1852 memcpy(ethhdr->ether_dst, eth_bcast, ETH_ADDR_LEN);
1853 memcpy(ethhdr->ether_src, dhmac, ETH_ADDR_LEN);
1854 ethhdr->ether_type = htons(ETH_TYPE_IP);
1856 /* fill in IP layer (L3) */
1857 struct ip_header *iph = (struct ip_header *)(dhcp_packet + ETH_HDR_SIZE);
1858 iph->ip_vhl = IP_VHL(4, 5);
1860 iph->ip_len = htons(IP_HDR_SIZE + UDP_HDR_SIZE + packet->data_len);
1865 iph->ip_sum = 0; /* Filled later */
1867 /* saddr: Packet-Src-IP-Address (default: 0.0.0.0). */
1868 iph->ip_src.s_addr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
1870 /* daddr: packet destination IP addr (should be 255.255.255.255 for broadcast). */
1871 iph->ip_dst.s_addr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
1873 /* IP header checksum */
1874 iph->ip_sum = fr_iph_checksum((uint8_t const *)iph, 5);
1876 /* fill in UDP layer (L4) */
1877 udp_header_t *uh = (udp_header_t *) (dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE);
1879 uh->src = htons(68);
1880 uh->dst = htons(67);
1881 u_int16_t l4_len = (UDP_HDR_SIZE + packet->data_len);
1882 uh->len = htons(l4_len);
1883 uh->checksum = 0; /* UDP checksum will be done after dhcp header */
1885 /* DHCP layer (L7) */
1886 dhcp_packet_t *dhpointer = (dhcp_packet_t *)(dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE);
1887 /* just copy what FreeRADIUS has encoded for us. */
1888 memcpy(dhpointer, packet->data, packet->data_len);
1890 /* UDP checksum is done here */
1891 uh->checksum = fr_udp_checksum((uint8_t const *)(dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE), ntohs(uh->len), uh->checksum,
1892 packet->src_ipaddr.ipaddr.ip4addr, packet->dst_ipaddr.ipaddr.ip4addr);
1894 if (fr_debug_lvl > 1) {
1896 char const *name = type_buf;
1897 char src_ip_buf[INET6_ADDRSTRLEN];
1898 char dst_ip_buf[INET6_ADDRSTRLEN];
1900 if ((packet->code >= PW_DHCP_DISCOVER) &&
1901 (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
1902 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
1904 snprintf(type_buf, sizeof(type_buf), "%d",
1905 packet->code - PW_DHCP_OFFSET);
1909 "Sending %s Id %08x from %s:%d to %s:%d\n",
1910 name, (unsigned int) packet->id,
1911 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)), packet->src_port,
1912 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)), packet->dst_port);
1915 return sendto(sockfd, dhcp_packet,
1916 (ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE + packet->data_len),
1917 0, (struct sockaddr *) p_ll, sizeof(struct sockaddr_ll));
1921 * print an ethernet address in a buffer
1923 char * ether_addr_print(const uint8_t *addr, char *buf)
1925 sprintf (buf, "%02x:%02x:%02x:%02x:%02x:%02x",
1926 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
1931 * For a client, receive a DHCP packet from a raw packet
1932 * socket. Make sure it matches the ongoing request.
1934 * FIXME: split this into two, recv_raw_packet, and verify(packet, original)
1936 RADIUS_PACKET *fr_dhcp_recv_raw_packet(int sockfd, struct sockaddr_ll *p_ll, RADIUS_PACKET *request)
1939 RADIUS_PACKET *packet;
1941 uint32_t magic, xid;
1944 uint8_t *raw_packet;
1945 ethernet_header_t *eth_hdr;
1946 struct ip_header *ip_hdr;
1947 udp_header_t *udp_hdr;
1948 dhcp_packet_t *dhcp_hdr;
1949 uint16_t udp_src_port;
1950 uint16_t udp_dst_port;
1951 size_t dhcp_data_len;
1956 packet = rad_alloc(NULL, false);
1958 fr_strerror_printf("Failed allocating packet");
1962 raw_packet = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
1964 fr_strerror_printf("Out of memory");
1969 packet->sockfd = sockfd;
1971 /* a packet was received (but maybe it is not for us) */
1972 sock_len = sizeof(struct sockaddr_ll);
1973 data_len = recvfrom(sockfd, raw_packet, MAX_PACKET_SIZE, 0,
1974 (struct sockaddr *)p_ll, &sock_len);
1976 uint8_t data_offset = ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE; // DHCP data starts after Ethernet, IP, UDP.
1978 if (data_len <= data_offset) DISCARD_RP("Payload (%d) smaller than required for layers 2+3+4", (int)data_len);
1980 /* map raw packet to packet header of the different layers (Ethernet, IP, UDP) */
1981 eth_hdr = (ethernet_header_t *)raw_packet;
1983 /* a. Check Ethernet layer data (L2) */
1984 if (ntohs(eth_hdr->ether_type) != ETH_TYPE_IP) DISCARD_RP("Ethernet type (%d) != IP", ntohs(eth_hdr->ether_type));
1986 /* If Ethernet destination is not broadcast (ff:ff:ff:ff:ff:ff)
1987 * Check if it matches the source HW address used (DHCP-Client-Hardware-Address = 267)
1989 if ( (memcmp(ð_bcast, ð_hdr->ether_dst, ETH_ADDR_LEN) != 0) &&
1990 (vp = fr_pair_find_by_num(request->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY)) &&
1991 (vp->length == sizeof(vp->vp_ether)) &&
1992 (memcmp(vp->vp_ether, ð_hdr->ether_dst, ETH_ADDR_LEN) != 0) ) {
1994 char eth_dest[17+1];
1995 char eth_req_src[17+1];
1996 DISCARD_RP("Ethernet destination (%s) is not broadcast and doesn't match request source (%s)",
1997 ether_addr_print(eth_hdr->ether_dst, eth_dest),
1998 ether_addr_print(vp->vp_ether, eth_req_src));
2002 * Ethernet is OK. Now look at IP.
2004 ip_hdr = (struct ip_header *)(raw_packet + ETH_HDR_SIZE);
2006 /* b. Check IPv4 layer data (L3) */
2007 if (ip_hdr->ip_p != IPPROTO_UDP) DISCARD_RP("IP protocol (%d) != UDP", ip_hdr->ip_p);
2010 * note: checking the destination IP address is not
2011 * useful (it would be the offered IP address - which we
2012 * don't know beforehand, or the broadcast address).
2018 udp_hdr = (udp_header_t *)(raw_packet + ETH_HDR_SIZE + IP_HDR_SIZE);
2020 /* c. Check UDP layer data (L4) */
2021 udp_src_port = ntohs(udp_hdr->src);
2022 udp_dst_port = ntohs(udp_hdr->dst);
2025 * A DHCP server will always respond to port 68 (to a
2026 * client) or 67 (to a relay). Just check that both
2027 * ports are 67 or 68.
2029 if (udp_src_port != 67 && udp_src_port != 68) DISCARD_RP("UDP src port (%d) != DHCP (67 or 68)", udp_src_port);
2030 if (udp_dst_port != 67 && udp_dst_port != 68) DISCARD_RP("UDP dst port (%d) != DHCP (67 or 68)", udp_dst_port);
2032 /* d. Check DHCP layer data */
2033 dhcp_data_len = data_len - data_offset;
2035 if (dhcp_data_len < MIN_PACKET_SIZE) DISCARD_RP("DHCP packet is too small (%d < %d)", dhcp_data_len, MIN_PACKET_SIZE);
2036 if (dhcp_data_len > MAX_PACKET_SIZE) DISCARD_RP("DHCP packet is too large (%d > %d)", dhcp_data_len, MAX_PACKET_SIZE);
2038 dhcp_hdr = (dhcp_packet_t *)(raw_packet + ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE);
2040 if (dhcp_hdr->htype != 1) DISCARD_RP("DHCP hardware type (%d) != Ethernet (1)", dhcp_hdr->htype);
2041 if (dhcp_hdr->hlen != 6) DISCARD_RP("DHCP hardware address length (%d) != 6", dhcp_hdr->hlen);
2043 magic = ntohl(dhcp_hdr->option_format);
2045 if (magic != DHCP_OPTION_MAGIC_NUMBER) DISCARD_RP("DHCP magic cookie (0x%04x) != DHCP (0x%04x)", magic, DHCP_OPTION_MAGIC_NUMBER);
2048 * Reply transaction id must match value from request.
2050 xid = ntohl(dhcp_hdr->xid);
2051 if (xid != (uint32_t)request->id) DISCARD_RP("DHCP transaction ID (0x%04x) != xid from request (0x%04x)", xid, request->id)
2053 /* all checks ok! this is a DHCP reply we're interested in. */
2054 packet->data_len = dhcp_data_len;
2055 packet->data = talloc_memdup(packet, raw_packet + data_offset, dhcp_data_len);
2056 TALLOC_FREE(raw_packet);
2059 code = dhcp_get_option((dhcp_packet_t *) packet->data,
2060 packet->data_len, 53);
2062 fr_strerror_printf("No message-type option was found in the packet");
2067 if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
2068 fr_strerror_printf("Unknown value for message-type option");
2073 packet->code = code[2] | PW_DHCP_OFFSET;
2076 * Create a unique vector from the MAC address and the
2077 * DHCP opcode. This is a hack for the RADIUS
2078 * infrastructure in the rest of the server.
2080 * Note: packet->data[2] == 6, which is smaller than
2081 * sizeof(packet->vector)
2083 * FIXME: Look for client-identifier in packet,
2084 * and use that, too?
2086 memset(packet->vector, 0, sizeof(packet->vector));
2087 memcpy(packet->vector, packet->data + 28, packet->data[2]);
2088 packet->vector[packet->data[2]] = packet->code & 0xff;
2090 packet->src_port = udp_src_port;
2091 packet->dst_port = udp_dst_port;
2093 packet->src_ipaddr.af = AF_INET;
2094 packet->src_ipaddr.ipaddr.ip4addr.s_addr = ip_hdr->ip_src.s_addr;
2095 packet->dst_ipaddr.af = AF_INET;
2096 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = ip_hdr->ip_dst.s_addr;
2098 if (fr_debug_lvl > 1) {
2100 char const *name = type_buf;
2101 char src_ip_buf[256], dst_ip_buf[256];
2103 if ((packet->code >= PW_DHCP_DISCOVER) &&
2104 (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
2105 name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
2107 snprintf(type_buf, sizeof(type_buf), "%d", packet->code - PW_DHCP_OFFSET);
2110 DEBUG("Received %s of Id %08x from %s:%d to %s:%d\n",
2111 name, (unsigned int) packet->id,
2112 inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
2114 inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),