+
/*
* dhcp.c Functions to send/receive dhcp packets.
*
#include <freeradius-devel/libradius.h>
#include <freeradius-devel/udpfromto.h>
#include <freeradius-devel/dhcp.h>
+#include <freeradius-devel/net.h>
#ifndef __MINGW32__
#include <sys/ioctl.h>
#endif
/* @todo: this is a hack */
-# define DEBUG if (fr_debug_flag && fr_log_fp) fr_printf_log
-# define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
+# define DEBUG if (fr_debug_lvl && fr_log_fp) fr_printf_log
+# define debug_pair(vp) do { if (fr_debug_lvl && fr_log_fp) { \
vp_print(fr_log_fp, vp); \
} \
} while(0)
+#ifdef HAVE_LINUX_IF_PACKET_H
+#define ETH_HDR_SIZE 14
+#define IP_HDR_SIZE 20
+#define UDP_HDR_SIZE 8
+#define ETH_ADDR_LEN 6
+#define ETH_TYPE_IP 0x0800
+#define ETH_P_ALL 0x0003
+
+static uint8_t eth_bcast[ETH_ADDR_LEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+
+/* Discard raw packets which we are not interested in. Allow to trace why we discard. */
+#define DISCARD_RP(...) { \
+ if (fr_debug_lvl > 2) { \
+ fprintf(stdout, "dhcpclient: discarding received packet: "); \
+ fprintf(stdout, ## __VA_ARGS__); \
+ fprintf(stdout, "\n"); \
+ } \
+ rad_free(&packet); \
+ return NULL; \
+}
+#endif
+
typedef struct dhcp_packet_t {
uint8_t opcode;
uint8_t htype;
"DHCP-Release",
"DHCP-Inform",
"DHCP-Force-Renew",
+ "DHCP-Lease-Query",
+ "DHCP-Lease-Unassigned",
+ "DHCP-Lease-Unknown",
+ "DHCP-Lease-Active",
+ "DHCP-Bulk-Lease-Query",
+ "DHCP-Lease-Query-Done"
};
+#define DHCP_MAX_MESSAGE_TYPE (sizeof(dhcp_message_types) / sizeof(dhcp_message_types[0]))
+
static int dhcp_header_sizes[] = {
1, 1, 1, 1,
4, 2, 2, 4,
return NULL;
}
- if (packet->data[1] != 1) {
+ if (packet->data[1] > 1) {
fr_strerror_printf("DHCP can only receive ethernet requests, not type %02x",
packet->data[1]);
rad_free(&packet);
return NULL;
}
- if (packet->data[2] != 6) {
+ if ((packet->data[2] != 0) && (packet->data[2] != 6)) {
fr_strerror_printf("Ethernet HW length is wrong length %d",
packet->data[2]);
rad_free(&packet);
packet->id = ntohl(magic);
code = dhcp_get_option((dhcp_packet_t *) packet->data,
- packet->data_len, 53);
+ packet->data_len, PW_DHCP_MESSAGE_TYPE);
if (!code) {
fr_strerror_printf("No message-type option was found in the packet");
rad_free(&packet);
return NULL;
}
- if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
- fr_strerror_printf("Unknown value for message-type option");
+ if ((code[1] < 1) || (code[2] == 0) || (code[2] >= DHCP_MAX_MESSAGE_TYPE)) {
+ fr_strerror_printf("Unknown value %d for message-type option", code[2]);
rad_free(&packet);
return NULL;
}
fr_sockaddr2ipaddr(&src, sizeof_src, &packet->src_ipaddr, &port);
packet->src_port = port;
- if (fr_debug_flag > 1) {
+ if (fr_debug_lvl > 1) {
char type_buf[64];
char const *name = type_buf;
char src_ip_buf[256], dst_ip_buf[256];
if ((packet->code >= PW_DHCP_DISCOVER) &&
- (packet->code <= PW_DHCP_INFORM)) {
+ (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
} else {
snprintf(type_buf, sizeof(type_buf), "%d",
return -1;
}
- if (fr_debug_flag > 1) {
+ if (fr_debug_lvl > 1) {
char type_buf[64];
char const *name = type_buf;
#ifdef WITH_UDPFROMTO
char dst_ip_buf[INET6_ADDRSTRLEN];
if ((packet->code >= PW_DHCP_DISCOVER) &&
- (packet->code <= PW_DHCP_INFORM)) {
+ (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
} else {
snprintf(type_buf, sizeof(type_buf), "%d",
#endif
}
-static int fr_dhcp_attr2vp(VALUE_PAIR **vp_p, TALLOC_CTX *ctx, uint8_t const *p, size_t alen);
+static int fr_dhcp_attr2vp(TALLOC_CTX *ctx, VALUE_PAIR **vp_p, uint8_t const *p, size_t alen);
/** Returns the number of array members for arrays with fixed element sizes
*
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@endverbatim
*
- * So although the vendor is identified, the format of the data isn't specified
- * so we can't actually resolve the suboption to an attribute.
- *
- * To get around that, we create an attribute with a vendor matching the
- * enterprise number, and attr 0.
- *
- * How the suboption data is then processed, is dependent on what type
- * \<iana\>.0 is defined as in the dictionary.
- *
- * @param[in,out] tlv to decode. *tlv will be set to the head of the list of suboptions and original will be freed.
- * @param[in] ctx context to alloc new attributes in.
- * @param[in] data to parse.
- * @param[in] len length of data to parse.
+ * So although the vendor is identified, the format of the data isn't
+ * specified so we can't actually resolve the suboption to an
+ * attribute. For now, we just convert it to an attribute of
+ * DHCP-Vendor-Specific-Information with raw octets contents.
*/
-static int fr_dhcp_decode_vsa(VALUE_PAIR **tlv, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
-{
- uint8_t const *p, *q;
- vp_cursor_t cursor;
-
- VALUE_PAIR *head;
-
- if (len < 4) goto malformed;
-
- p = data;
- q = p + len;
- while (p < q) {
- if (p + 5 >= q) goto malformed;
- p += sizeof(uint32_t);
- p += p[0];
- /*
- * Check if length > the length of the buffer we have left
- */
- if (p >= q) goto malformed;
- p++;
- }
-
- head = NULL;
- fr_cursor_init(&cursor, &head);
-
- /*
- * Now we know its sane, start decoding!
- */
- p = data;
- while (p < q) {
- uint32_t vendor;
- DICT_ATTR const *da;
- VALUE_PAIR *vp;
-
- vendor = ntohl(*((uint32_t const *) p));
- /*
- * This is pretty much all we can do. RFC 4243 doesn't specify
- * an attribute field, so it's up to vendors to figure out how
- * they want to encode their attributes.
- */
- da = dict_attrbyvalue(0, vendor);
- if (!da) {
- da = dict_attrunknown(ctx, 0, vendor);
- if (!da) {
- pairfree(&head);
- goto malformed;
- }
- }
- vp = pairalloc(ctx, da);
- if (!vp) {
- pairfree(&head);
- return -1;
- }
- vp->op = T_OP_ADD;
- pairsteal(ctx, vp); /* for unknown attributes hack */
-
- if (fr_dhcp_attr2vp(&vp, ctx, p + 5, p[4]) < 0) {
- dict_attr_free(&da);
- pairfree(&head);
- return -1;
- }
-
- fr_cursor_merge(&cursor, vp);
- dict_attr_free(&da); /* for unknown attributes hack */
-
- p += 4 + 1 + p[4]; /* vendor id (4) + len (1) + vsa len (n) */
- }
-
- /*
- * The caller allocated TLV, if decoding it generated additional
- * attributes, we now need to free it, and write the HEAD of our
- * new list of attributes in its place.
- */
- if (head) {
- vp_cursor_t tlv_cursor;
-
- /*
- * Free the old TLV attribute
- */
- TALLOC_FREE(*tlv);
-
- /*
- * Cursor not necessary but means we don't have to set
- * ->next directly.
- */
- fr_cursor_init(&tlv_cursor, tlv);
- fr_cursor_merge(&tlv_cursor, head);
- }
-
- return 0;
-
-malformed:
- (*tlv)->vp_tlv = talloc_array(*tlv, uint8_t, len);
- if (!(*tlv)->vp_tlv) {
- fr_strerror_printf("No memory");
- return -1;
- }
- memcpy((*tlv)->vp_tlv, data, len);
- (*tlv)->length = len;
-
- return 0;
-}
/** Decode DHCP suboptions
*
* @param[in] data to parse.
* @param[in] len length of data to parse.
*/
-static int fr_dhcp_decode_suboption(VALUE_PAIR **tlv, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
+static int fr_dhcp_decode_suboption(TALLOC_CTX *ctx, VALUE_PAIR **tlv, uint8_t const *data, size_t len)
{
uint8_t const *p, *q;
VALUE_PAIR *head, *vp;
uint32_t attr;
/*
+ * Not enough room for the option header, it's a
+ * bad packet.
+ */
+ if ((p + 2) > (data + len)) {
+ fr_pair_list_free(&head);
+ return -1;
+ }
+
+ /*
+ * Not enough room for the option header + data,
+ * it's a bad packet.
+ */
+ if ((p + 2 + p[1]) > (data + len)) {
+ fr_pair_list_free(&head);
+ return -1;
+ }
+
+ /*
* The initial OID string looks like:
* <iana>.0
*
*/
da = dict_attrbyvalue(attr, (*tlv)->da->vendor);
if (!da) {
- da = dict_attrunknown(ctx, attr, (*tlv)->da->vendor);
+ da = dict_unknown_afrom_fields(ctx, attr, (*tlv)->da->vendor);
if (!da) {
- pairfree(&head);
+ fr_pair_list_free(&head);
return -1;
}
}
a_p = p + 2;
num_entries = fr_dhcp_array_members(&a_len, da);
for (i = 0; i < num_entries; i++) {
- vp = pairalloc(ctx, da);
+ vp = fr_pair_afrom_da(ctx, da);
if (!vp) {
- pairfree(&head);
+ fr_pair_list_free(&head);
return -1;
}
- vp->op = T_OP_ADD;
- pairsteal(ctx, vp); /* for unknown attributes hack */
+ vp->op = T_OP_EQ;
+ fr_pair_steal(ctx, vp); /* for unknown attributes hack */
- if (fr_dhcp_attr2vp(&vp, ctx, a_p, a_len) < 0) {
+ if (fr_dhcp_attr2vp(ctx, &vp, a_p, a_len) < 0) {
dict_attr_free(&da);
- pairfree(&head);
+ fr_pair_list_free(&head);
goto malformed;
}
fr_cursor_merge(&cursor, vp);
return 0;
malformed:
- (*tlv)->vp_tlv = talloc_array(*tlv, uint8_t, len);
- if (!(*tlv)->vp_tlv) {
- fr_strerror_printf("No memory");
- return -1;
- }
- memcpy((*tlv)->vp_tlv, data, len);
- (*tlv)->length = len;
+ fr_pair_to_unknown(*tlv);
+ fr_pair_value_memcpy(*tlv, data, len);
return 0;
}
/*
* Decode ONE value into a VP
*/
-static int fr_dhcp_attr2vp(VALUE_PAIR **vp_p, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
+static int fr_dhcp_attr2vp(TALLOC_CTX *ctx, VALUE_PAIR **vp_p, uint8_t const *data, size_t len)
{
VALUE_PAIR *vp = *vp_p;
VERIFY_VP(vp);
* Keep value in Network Order!
*/
memcpy(&vp->vp_ipaddr, data, 4);
- vp->length = 4;
+ vp->vp_length = 4;
break;
/*
q = end = data + len;
if (!vp->da->flags.array) {
- pairstrncpy(vp, (char const *)p, q - p);
+ fr_pair_value_bstrncpy(vp, (char const *)p, q - p);
break;
}
* multiple additional VPs
*/
fr_cursor_init(&cursor, vp_p);
- for (;;) {
- q = memchr(p, '\0', q - p);
+ while (p < end) {
+ q = memchr(p, '\0', end - p);
/* Malformed but recoverable */
if (!q) q = end;
- pairstrncpy(vp, (char const *)p, q - p);
+ fr_pair_value_bstrncpy(vp, (char const *)p, q - p);
p = q + 1;
+ if (p >= end) break;
+
/* Need another VP for the next round */
- if (p < end) {
- vp = pairalloc(ctx, vp->da);
- if (!vp) {
- pairfree(vp_p);
- return -1;
- }
- fr_cursor_insert(&cursor, vp);
- continue;
+ vp = fr_pair_afrom_da(ctx, vp->da);
+ if (!vp) {
+ fr_pair_list_free(vp_p);
+ return -1;
}
- break;
+ fr_cursor_insert(&cursor, vp);
}
}
break;
case PW_TYPE_ETHERNET:
memcpy(vp->vp_ether, data, sizeof(vp->vp_ether));
- vp->length = sizeof(vp->vp_ether);
+ vp->vp_length = sizeof(vp->vp_ether);
break;
/*
* vp's original DICT_ATTR with an unknown one.
*/
raw:
- if (pair2unknown(vp) < 0) return -1;
+ if (fr_pair_to_unknown(vp) < 0) return -1;
case PW_TYPE_OCTETS:
if (len > 255) return -1;
- pairmemcpy(vp, data, len);
+ fr_pair_value_memcpy(vp, data, len);
break;
/*
* For option 82 et al...
*/
case PW_TYPE_TLV:
- return fr_dhcp_decode_suboption(vp_p, ctx, data, len);
-
- /*
- * For option 82.9
- */
- case PW_TYPE_VSA:
- return fr_dhcp_decode_vsa(vp_p, ctx, data, len);
+ return fr_dhcp_decode_suboption(ctx, vp_p, data, len);
default:
fr_strerror_printf("Internal sanity check %d %d", vp->da->type, __LINE__);
return -1;
} /* switch over type */
- vp->length = len;
+ vp->vp_length = len;
return 0;
}
* @param[in] data to parse.
* @param[in] len of data to parse.
*/
-ssize_t fr_dhcp_decode_options(VALUE_PAIR **out, TALLOC_CTX *ctx, uint8_t const *data, size_t len)
+ssize_t fr_dhcp_decode_options(TALLOC_CTX *ctx, VALUE_PAIR **out, uint8_t const *data, size_t len)
{
VALUE_PAIR *vp;
vp_cursor_t cursor;
a_p = p + 2;
/*
+ * Ensure we've not been given a bad length value
+ */
+ if ((a_p + a_len) > q) {
+ fr_strerror_printf("Length field value of option %u is incorrect. "
+ "Got %u bytes, expected <= %zu bytes", p[0], p[1], q - a_p);
+ fr_pair_list_free(out);
+ return -1;
+ }
+
+ /*
* Unknown attribute, create an octets type
* attribute with the contents of the sub-option.
*/
da = dict_attrbyvalue(p[0], DHCP_MAGIC_VENDOR);
if (!da) {
- da = dict_attrunknown(ctx, p[0], DHCP_MAGIC_VENDOR);
+ da = dict_unknown_afrom_fields(ctx, p[0], DHCP_MAGIC_VENDOR);
if (!da) {
- pairfree(out);
+ fr_pair_list_free(out);
return -1;
}
- vp = pairalloc(ctx, da);
+ vp = fr_pair_afrom_da(ctx, da);
if (!vp) {
- pairfree(out);
+ fr_pair_list_free(out);
return -1;
}
- pairmemcpy(vp, a_p, a_len);
+ fr_pair_value_memcpy(vp, a_p, a_len);
fr_cursor_insert(&cursor, vp);
goto next;
*/
num_entries = fr_dhcp_array_members(&a_len, da);
for (i = 0; i < num_entries; i++) {
- vp = pairalloc(ctx, da);
+ vp = fr_pair_afrom_da(ctx, da);
if (!vp) {
- pairfree(out);
+ fr_pair_list_free(out);
return -1;
}
- vp->op = T_OP_ADD;
+ vp->op = T_OP_EQ;
- if (fr_dhcp_attr2vp(&vp, ctx, a_p, a_len) < 0) {
- pairfree(&vp);
- pairfree(out);
+ if (fr_dhcp_attr2vp(ctx, &vp, a_p, a_len) < 0) {
+ fr_pair_list_free(&vp);
+ fr_pair_list_free(out);
return -1;
}
- fr_cursor_insert(&cursor, vp);
-
- for (vp = fr_cursor_current(&cursor);
- vp;
- vp = fr_cursor_next(&cursor)) {
- debug_pair(vp);
- }
+ fr_cursor_merge(&cursor, vp);
a_p += a_len;
} /* loop over array entries */
next:
fr_cursor_init(&cursor, &head);
p = packet->data;
- if ((fr_debug_flag > 2) && fr_log_fp) {
+ if ((fr_debug_lvl > 2) && fr_log_fp) {
for (i = 0; i < packet->data_len; i++) {
if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
fprintf(fr_log_fp, "%02x ", packet->data[i]);
fprintf(fr_log_fp, "\n");
}
- if (packet->data[1] != 1) {
+ if (packet->data[1] > 1) {
fr_strerror_printf("Packet is not Ethernet: %u",
packet->data[1]);
return -1;
* Decode the header.
*/
for (i = 0; i < 14; i++) {
- char *q;
- vp = pairmake(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
+ vp = fr_pair_make(packet, NULL, dhcp_header_names[i], NULL, T_OP_EQ);
if (!vp) {
char buffer[256];
strlcpy(buffer, fr_strerror(), sizeof(buffer));
fr_strerror_printf("Cannot decode packet due to internal error: %s", buffer);
- pairfree(&head);
+ fr_pair_list_free(&head);
return -1;
}
/*
- * If chaddr does != 6 bytes it's probably not ethernet, and we should store
+ * If chaddr != 6 bytes it's probably not ethernet, and we should store
* it as an opaque type (octets).
*/
- if ((i == 11) && (packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
- DICT_ATTR const *da = dict_attrunknown(packet, vp->da->attr, vp->da->vendor);
- if (!da) {
- return -1;
+ if (i == 11) {
+ /*
+ * Skip chaddr if it doesn't exist.
+ */
+ if ((packet->data[1] == 0) || (packet->data[2] == 0)) continue;
+
+ if ((packet->data[1] == 1) && (packet->data[2] != sizeof(vp->vp_ether))) {
+ DICT_ATTR const *da = dict_unknown_afrom_fields(packet, vp->da->attr, vp->da->vendor);
+ if (!da) {
+ return -1;
+ }
+ vp->da = da;
}
- vp->da = da;
}
switch (vp->da->type) {
case PW_TYPE_BYTE:
vp->vp_byte = p[0];
- vp->length = 1;
+ vp->vp_length = 1;
break;
case PW_TYPE_SHORT:
vp->vp_short = (p[0] << 8) | p[1];
- vp->length = 2;
+ vp->vp_length = 2;
break;
case PW_TYPE_INTEGER:
memcpy(&vp->vp_integer, p, 4);
vp->vp_integer = ntohl(vp->vp_integer);
- vp->length = 4;
+ vp->vp_length = 4;
break;
case PW_TYPE_IPV4_ADDR:
memcpy(&vp->vp_ipaddr, p, 4);
- vp->length = 4;
+ vp->vp_length = 4;
break;
case PW_TYPE_STRING:
- vp->vp_strvalue = q = talloc_array(vp, char, dhcp_header_sizes[i] + 1);
- vp->type = VT_DATA;
- memcpy(q, p, dhcp_header_sizes[i]);
- q[dhcp_header_sizes[i]] = '\0';
- vp->length = strlen(vp->vp_strvalue);
- if (vp->length == 0) {
- pairfree(&vp);
+ /*
+ * According to RFC 2131, these are null terminated strings.
+ * We don't trust everyone to abide by the RFC, though.
+ */
+ if (*p != '\0') {
+ uint8_t *end;
+ int len;
+ end = memchr(p, '\0', dhcp_header_sizes[i]);
+ len = end ? end - p : dhcp_header_sizes[i];
+ fr_pair_value_bstrncpy(vp, p, len);
}
+ if (vp->vp_length == 0) fr_pair_list_free(&vp);
break;
case PW_TYPE_OCTETS:
- pairmemcpy(vp, p, packet->data[2]);
+ if (packet->data[2] == 0) break;
+
+ fr_pair_value_memcpy(vp, p, packet->data[2]);
break;
case PW_TYPE_ETHERNET:
memcpy(vp->vp_ether, p, sizeof(vp->vp_ether));
- vp->length = sizeof(vp->vp_ether);
+ vp->vp_length = sizeof(vp->vp_ether);
break;
default:
fr_strerror_printf("BAD TYPE %d", vp->da->type);
- pairfree(&vp);
+ fr_pair_list_free(&vp);
break;
}
p += dhcp_header_sizes[i];
*/
{
VALUE_PAIR *options = NULL;
+ vp_cursor_t options_cursor;
- if (fr_dhcp_decode_options(&options, packet, packet->data + 240, packet->data_len - 240) < 0) {
+ if (fr_dhcp_decode_options(packet, &options, packet->data + 240, packet->data_len - 240) < 0) {
return -1;
}
- if (options) fr_cursor_merge(&cursor, options);
+ if (options) {
+ for (vp = fr_cursor_init(&options_cursor, &options);
+ vp;
+ vp = fr_cursor_next(&options_cursor)) {
+ debug_pair(vp);
+ }
+ fr_cursor_merge(&cursor, options);
+ }
}
/*
/*
* DHCP Opcode is request
*/
- vp = pairfind(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
+ vp = fr_pair_find_by_num(head, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp && vp->vp_integer == 3) {
/*
* Vendor is "MSFT 98"
*/
- vp = pairfind(head, 63, DHCP_MAGIC_VENDOR, TAG_ANY);
- if (vp && (strcmp(vp->vp_strvalue, "MSFT 98") == 0)) {
- vp = pairfind(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
+ vp = fr_pair_find_by_num(head, 60, DHCP_MAGIC_VENDOR, TAG_ANY);
+ if (vp && (vp->vp_length >= 7) && (memcmp(vp->vp_octets, "MSFT 98", 7) == 0)) {
+ vp = fr_pair_find_by_num(head, 262, DHCP_MAGIC_VENDOR, TAG_ANY);
/*
* Reply should be broadcast.
*/
- if (vp) vp->vp_integer |= 0x8000;
+ if (vp) vp->vp_short |= 0x8000;
packet->data[10] |= 0x80;
}
}
* Client can request a LARGER size, but not a smaller
* one. They also cannot request a size larger than MTU.
*/
- maxms = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
- mtu = pairfind(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
+ maxms = fr_pair_find_by_num(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
+ mtu = fr_pair_find_by_num(packet->vps, 26, DHCP_MAGIC_VENDOR, TAG_ANY);
if (mtu && (mtu->vp_integer < DEFAULT_PACKET_SIZE)) {
fr_strerror_printf("DHCP Fatal: Client says MTU is smaller than minimum permitted by the specification");
maxms->vp_integer = mtu->vp_integer;
}
- if (fr_debug_flag) fflush(stdout);
+ if (fr_debug_lvl) fflush(stdout);
return 0;
}
/*
* DHCP-Message-Type is first, for simplicity.
*/
- if ((my_a->da->attr == 53) && (my_b->da->attr != 53)) return -1;
+ if ((my_a->da->attr == PW_DHCP_MESSAGE_TYPE) && (my_b->da->attr != PW_DHCP_MESSAGE_TYPE)) return -1;
+ if ((my_a->da->attr != PW_DHCP_MESSAGE_TYPE) && (my_b->da->attr == PW_DHCP_MESSAGE_TYPE)) return +1;
/*
* Relay-Agent is last
*/
- if ((my_a->da->attr == 82) && (my_b->da->attr != 82)) return 1;
+ if ((my_a->da->attr == PW_DHCP_OPTION_82) && (my_b->da->attr != PW_DHCP_OPTION_82)) return +1;
+ if ((my_a->da->attr != PW_DHCP_OPTION_82) && (my_b->da->attr == PW_DHCP_OPTION_82)) return -1;
if (my_a->da->attr < my_b->da->attr) return -1;
if (my_a->da->attr > my_b->da->attr) return 1;
* @param vp option to encode.
* @return the length of data writen, -1 if out of buffer, -2 if unsupported type.
*/
-static ssize_t fr_dhcp_vp2attr(uint8_t *out, size_t outlen, VALUE_PAIR *vp)
+static ssize_t fr_dhcp_vp2data(uint8_t *out, size_t outlen, VALUE_PAIR *vp)
{
uint32_t lvalue;
uint8_t *p = out;
- if (outlen < vp->length) {
+ if (outlen < vp->vp_length) {
return -1;
}
break;
case PW_TYPE_ETHERNET:
- memcpy(p, &vp->vp_ether, 6);
+ memcpy(p, vp->vp_ether, 6);
break;
case PW_TYPE_STRING:
- memcpy(p, vp->vp_strvalue, vp->length);
- break;
-
- case PW_TYPE_TLV: /* FIXME: split it on 255? */
- memcpy(p, vp->vp_tlv, vp->length);
+ memcpy(p, vp->vp_strvalue, vp->vp_length);
break;
case PW_TYPE_OCTETS:
- memcpy(p, vp->vp_octets, vp->length);
+ memcpy(p, vp->vp_octets, vp->vp_length);
break;
default:
return -2;
}
- return vp->length;
+ return vp->vp_length;
}
/** Create a new TLV attribute from multiple sub options
*
- * @param[in,out] ctx to allocate new attribute in.
+ * @param[in,out] out buffer to write the data
+ * @param[out] outlen length of the output buffer
* @param[in,out] cursor should be set to the start of the list of TLV attributes.
* Will be advanced to the first non-TLV attribute.
- * @return attribute holding the concatenation of the values of the sub options.
+ * @return length of data encoded, or -1 on error
*/
-static VALUE_PAIR *fr_dhcp_vp2suboption(TALLOC_CTX *ctx, vp_cursor_t *cursor)
+static ssize_t fr_dhcp_vp2data_tlv(uint8_t *out, ssize_t outlen, vp_cursor_t *cursor)
{
- ssize_t length;
+ ssize_t len;
unsigned int parent; /* Parent attribute of suboption */
uint8_t attr = 0;
- uint8_t *p, *opt_len = NULL;
- vp_cursor_t to_pack;
- VALUE_PAIR *vp, *tlv;
+ uint8_t *p, *opt_len;
+ vp_cursor_t tlv_cursor;
+ VALUE_PAIR *vp;
#define SUBOPTION_PARENT(_x) (_x & 0xffff00ff)
#define SUBOPTION_ATTR(_x) ((_x & 0xff00) >> 8)
vp = fr_cursor_current(cursor);
- if (!vp) return NULL;
+ if (!vp) return -1;
parent = SUBOPTION_PARENT(vp->da->attr);
- tlv = paircreate(ctx, parent, DHCP_MAGIC_VENDOR);
- if (!tlv) return NULL;
- fr_cursor_copy(&to_pack, cursor);
+ /*
+ * Remember where we started off.
+ */
+ fr_cursor_copy(&tlv_cursor, cursor);
/*
- * Loop over TLVs to determine how much memory we need to allocate
+ * Loop over TLVs to determine how much memory we need to allocate
*
- * We advanced the cursor we were passed, so if we fail encoding,
- * the cursor is at the right position for the next potentially
- * encodable attr.
+ * We advanced the tlv_cursor we were passed, so if we
+ * fail encoding, the tlv_cursor is at the right position
+ * for the next potentially encodable attr.
*/
- for (vp = fr_cursor_current(cursor);
- vp && vp->da->flags.is_tlv && !vp->da->flags.extended && (SUBOPTION_PARENT(vp->da->attr) == parent);
- vp = fr_cursor_next(cursor)) {
+ len = 0;
+ for (vp = fr_cursor_current(&tlv_cursor);
+ vp && vp->da->flags.is_tlv && (SUBOPTION_PARENT(vp->da->attr) == parent);
+ vp = fr_cursor_next(&tlv_cursor)) {
+ if (SUBOPTION_ATTR(vp->da->attr) == 0) {
+ fr_strerror_printf("Invalid attribute number 0");
+ return -1;
+ }
+
/*
- * If it's not an array type or is an array type, but is not the same
- * as the previous attribute, we add 2 for the additional sub-option
- * header bytes.
+ * If it's not an array type or is an array type,
+ * but is not the same as the previous attribute,
+ * we add 2 for the additional sub-option header
+ * bytes.
*/
if (!vp->da->flags.array || (SUBOPTION_ATTR(vp->da->attr) != attr)) {
attr = SUBOPTION_ATTR(vp->da->attr);
- tlv->length += 2;
+ len += 2;
}
- tlv->length += vp->length;
+ len += vp->vp_length;
}
- tlv->vp_tlv = talloc_zero_array(tlv, uint8_t, tlv->length);
- if (!tlv->vp_tlv) {
- talloc_free(tlv);
- return NULL;
+ if (len > outlen) {
+ fr_strerror_printf("Insufficient room for suboption");
+ return -1;
}
- p = tlv->vp_tlv;
attr = 0;
- for (vp = fr_cursor_current(&to_pack);
- vp && vp->da->flags.is_tlv && !vp->da->flags.extended && (SUBOPTION_PARENT(vp->da->attr) == parent);
- vp = fr_cursor_next(&to_pack)) {
- if (SUBOPTION_ATTR(vp->da->attr) == 0) {
- fr_strerror_printf("Invalid attribute number 0");
- return NULL;
- }
-
+ opt_len = NULL;
+ p = out;
+
+ for (vp = fr_cursor_current(cursor);
+ vp && vp->da->flags.is_tlv && (SUBOPTION_PARENT(vp->da->attr) == parent);
+ vp = fr_cursor_next(cursor)) {
/* Don't write out the header, were packing array options */
- if (!vp->da->flags.array || (attr != SUBOPTION_ATTR(vp->da->attr))) {
+ if (!opt_len || !vp->da->flags.array || (attr != SUBOPTION_ATTR(vp->da->attr))) {
attr = SUBOPTION_ATTR(vp->da->attr);
*p++ = attr;
opt_len = p++;
+ *opt_len = 0;
}
- length = fr_dhcp_vp2attr(p, (tlv->vp_tlv + tlv->length) - p, vp);
- if ((length < 0) || (length > 255)) {
- talloc_free(tlv);
- return NULL;
+ len = fr_dhcp_vp2data(p, out + outlen - p, vp);
+ if ((len < 0) || (len > 255)) {
+ return -1;
}
- fr_assert(opt_len);
- *opt_len += length;
- p += length;
+ debug_pair(vp);
+ *opt_len += len;
+ p += len;
};
- return tlv;
+ return p - out;
}
/** Encode a DHCP option and any sub-options.
* @param cursor with current VP set to the option to be encoded. Will be advanced to the next option to encode.
* @return > 0 length of data written, < 0 error, 0 not valid option (skipping).
*/
-ssize_t fr_dhcp_encode_option(uint8_t *out, size_t outlen, TALLOC_CTX *ctx, vp_cursor_t *cursor)
+ssize_t fr_dhcp_encode_option(UNUSED TALLOC_CTX *ctx, uint8_t *out, size_t outlen, vp_cursor_t *cursor)
{
VALUE_PAIR *vp;
DICT_ATTR const *previous;
if (!vp) return -1;
if (vp->da->vendor != DHCP_MAGIC_VENDOR) goto next; /* not a DHCP option */
- if (vp->da->attr == 53) goto next; /* already done */
+ if (vp->da->attr == PW_DHCP_MESSAGE_TYPE) goto next; /* already done */
if ((vp->da->attr > 255) && (DHCP_BASE_ATTR(vp->da->attr) != PW_DHCP_OPTION_82)) goto next;
if (vp->da->flags.extended) {
/* DHCP options with the same number get coalesced into a single option */
do {
- VALUE_PAIR *tlv = NULL;
-
- /* Sub option */
- if (vp->da->flags.is_tlv) {
- /*
- * Coalesce TLVs into one sub-option.
- * Cursor will be advanced to next non-TLV attribute.
- */
- tlv = vp = fr_dhcp_vp2suboption(ctx, cursor);
-
- /*
- * Skip if there's an issue coalescing the sub-options.
- * Cursor will still have been advanced to next non-TLV attribute.
- */
- if (!tlv) return 0;
/*
- * If not calling fr_dhcp_vp2suboption() advance the cursor, so fr_cursor_current()
- * returns the next attribute.
+ * Sub-option encoder will encode the data and
+ * advance the cursor.
*/
+ if (vp->da->flags.is_tlv) {
+ len = fr_dhcp_vp2data_tlv(p, freespace, cursor);
+ previous = NULL;
+
} else {
+ len = fr_dhcp_vp2data(p, freespace, vp);
+ if (len >= 0) debug_pair(vp);
fr_cursor_next(cursor);
+ previous = vp->da;
}
- if ((*opt_len + vp->length) > 255) {
+ if (len < 0) return len;
+
+ if ((*opt_len + len) > 255) {
fr_strerror_printf("Skipping \"%s\": Option splitting not supported "
"(option > 255 bytes)", vp->da->name);
- talloc_free(tlv);
return 0;
}
- len = fr_dhcp_vp2attr(p, freespace, vp);
- talloc_free(tlv);
- if (len < 0) {
- /* Failed encoding option */
- return len;
- }
-
p += len;
*opt_len += len;
freespace -= len;
- previous = vp->da;
- } while ((vp = fr_cursor_current(cursor)) && (previous == vp->da) && vp->da->flags.array);
+ } while ((vp = fr_cursor_current(cursor)) && previous && (previous == vp->da) && vp->da->flags.array);
return p - out;
}
vp_cursor_t cursor;
VALUE_PAIR *vp;
uint32_t lvalue;
+ uint16_t svalue;
size_t dhcp_size;
ssize_t len;
#ifndef NDEBUG
if (packet->code == 0) packet->code = PW_DHCP_NAK;
/* store xid */
- if ((vp = pairfind(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if ((vp = fr_pair_find_by_num(packet->vps, 260, DHCP_MAGIC_VENDOR, TAG_ANY))) {
packet->id = vp->vp_integer;
} else {
packet->id = fr_rand();
#ifndef NDEBUG
if ((packet->code >= PW_DHCP_DISCOVER) &&
- (packet->code <= PW_DHCP_INFORM)) {
+ (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
} else {
name = "?Unknown?";
*/
/* DHCP-DHCP-Maximum-Msg-Size */
- vp = pairfind(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, 57, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp && (vp->vp_integer > mms)) {
mms = vp->vp_integer;
}
#endif
- vp = pairfind(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, 256, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp) {
*p++ = vp->vp_integer & 0xff;
} else {
}
/* DHCP-Hardware-Type */
- if ((vp = pairfind(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- *p++ = vp->vp_integer & 0xFF;
+ if ((vp = fr_pair_find_by_num(packet->vps, 257, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ *p++ = vp->vp_byte;
} else {
*p++ = 1; /* hardware type = ethernet */
}
/* DHCP-Hardware-Address-Length */
- if ((vp = pairfind(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- *p++ = vp->vp_integer & 0xFF;
+ if ((vp = fr_pair_find_by_num(packet->vps, 258, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ *p++ = vp->vp_byte;
} else {
*p++ = 6; /* 6 bytes of ethernet */
}
/* DHCP-Hop-Count */
- if ((vp = pairfind(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- *p = vp->vp_integer & 0xff;
+ if ((vp = fr_pair_find_by_num(packet->vps, 259, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ *p = vp->vp_byte;
}
p++;
p += 4;
/* DHCP-Number-of-Seconds */
- if ((vp = pairfind(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- lvalue = htonl(vp->vp_integer);
- memcpy(p, &lvalue, 2);
+ if ((vp = fr_pair_find_by_num(packet->vps, 261, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ svalue = htons(vp->vp_short);
+ memcpy(p, &svalue, 2);
}
p += 2;
/* DHCP-Flags */
- if ((vp = pairfind(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- lvalue = htons(vp->vp_integer);
- memcpy(p, &lvalue, 2);
+ if ((vp = fr_pair_find_by_num(packet->vps, 262, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ svalue = htons(vp->vp_short);
+ memcpy(p, &svalue, 2);
}
p += 2;
/* DHCP-Client-IP-Address */
- if ((vp = pairfind(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if ((vp = fr_pair_find_by_num(packet->vps, 263, DHCP_MAGIC_VENDOR, TAG_ANY))) {
memcpy(p, &vp->vp_ipaddr, 4);
}
p += 4;
/* DHCP-Your-IP-address */
- if ((vp = pairfind(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if ((vp = fr_pair_find_by_num(packet->vps, 264, DHCP_MAGIC_VENDOR, TAG_ANY))) {
lvalue = vp->vp_ipaddr;
} else {
lvalue = htonl(INADDR_ANY);
p += 4;
/* DHCP-Server-IP-Address */
- vp = pairfind(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, 265, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp) {
lvalue = vp->vp_ipaddr;
} else {
/*
* DHCP-Gateway-IP-Address
*/
- if ((vp = pairfind(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if ((vp = fr_pair_find_by_num(packet->vps, 266, DHCP_MAGIC_VENDOR, TAG_ANY))) {
lvalue = vp->vp_ipaddr;
} else {
lvalue = htonl(INADDR_ANY);
p += 4;
/* DHCP-Client-Hardware-Address */
- if ((vp = pairfind(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- if (vp->length == sizeof(vp->vp_ether)) {
- memcpy(p, vp->vp_ether, vp->length);
+ if ((vp = fr_pair_find_by_num(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if (vp->vp_length == sizeof(vp->vp_ether)) {
+ /*
+ * Ensure that we mark the packet as being Ethernet.
+ * This is mainly for DHCP-Lease-Query responses.
+ */
+ packet->data[1] = 1;
+ packet->data[2] = 6;
+
+ memcpy(p, vp->vp_ether, vp->vp_length);
} /* else ignore it */
}
p += DHCP_CHADDR_LEN;
/* DHCP-Server-Host-Name */
- if ((vp = pairfind(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
- if (vp->length > DHCP_SNAME_LEN) {
+ if ((vp = fr_pair_find_by_num(packet->vps, 268, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if (vp->vp_length > DHCP_SNAME_LEN) {
memcpy(p, vp->vp_strvalue, DHCP_SNAME_LEN);
} else {
- memcpy(p, vp->vp_strvalue, vp->length);
+ memcpy(p, vp->vp_strvalue, vp->vp_length);
}
}
p += DHCP_SNAME_LEN;
*/
/* DHCP-Boot-Filename */
- vp = pairfind(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, 269, DHCP_MAGIC_VENDOR, TAG_ANY);
if (vp) {
- if (vp->length > DHCP_FILE_LEN) {
+ if (vp->vp_length > DHCP_FILE_LEN) {
memcpy(p, vp->vp_strvalue, DHCP_FILE_LEN);
} else {
- memcpy(p, vp->vp_strvalue, vp->length);
+ memcpy(p, vp->vp_strvalue, vp->vp_length);
}
}
p += DHCP_FILE_LEN;
/*
* Print the header.
*/
- if (fr_debug_flag > 1) {
+ if (fr_debug_lvl > 1) {
uint8_t *pp = p;
p = packet->data;
for (i = 0; i < 14; i++) {
char *q;
- vp = pairmake(packet, NULL,
+ vp = fr_pair_make(packet, NULL,
dhcp_header_names[i], NULL, T_OP_EQ);
if (!vp) {
char buffer[256];
vp->type = VT_DATA;
memcpy(q, p, dhcp_header_sizes[i]);
q[dhcp_header_sizes[i]] = '\0';
- vp->length = strlen(vp->vp_strvalue);
+ vp->vp_length = strlen(vp->vp_strvalue);
break;
case PW_TYPE_OCTETS: /* only for Client HW Address */
- pairmemcpy(vp, p, packet->data[2]);
+ fr_pair_value_memcpy(vp, p, packet->data[2]);
break;
case PW_TYPE_ETHERNET: /* only for Client HW Address */
default:
fr_strerror_printf("Internal sanity check failed %d %d", vp->da->type, __LINE__);
- pairfree(&vp);
+ fr_pair_list_free(&vp);
break;
}
p += dhcp_header_sizes[i];
debug_pair(vp);
- pairfree(&vp);
+ fr_pair_list_free(&vp);
}
/*
p[2] = packet->code - PW_DHCP_OFFSET;
p += 3;
-
/*
* Pre-sort attributes into contiguous blocks so that fr_dhcp_encode_option
* operates correctly. This changes the order of the list, but never mind...
*/
- pairsort(&packet->vps, fr_dhcp_attr_cmp);
+ fr_pair_list_sort(&packet->vps, fr_dhcp_attr_cmp);
fr_cursor_init(&cursor, &packet->vps);
/*
* and sub options.
*/
while ((vp = fr_cursor_current(&cursor))) {
- len = fr_dhcp_encode_option(p, packet->data_len - (p - packet->data), packet, &cursor);
+ len = fr_dhcp_encode_option(packet, p, packet->data_len - (p - packet->data), &cursor);
if (len < 0) break;
- if (len > 0) debug_pair(vp);
p += len;
};
packet->data_len = DEFAULT_PACKET_SIZE;
}
- if ((fr_debug_flag > 2) && fr_log_fp) {
+ if ((fr_debug_lvl > 2) && fr_log_fp) {
fprintf(fr_log_fp, "DHCP Sending %zu bytes\n", packet->data_len);
for (i = 0; i < packet->data_len; i++) {
if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "%d: ", (int) i);
return -1;
}
- if (macaddr->length > sizeof(req.arp_ha.sa_data)) {
+ if (macaddr->vp_length > sizeof(req.arp_ha.sa_data)) {
fr_strerror_printf("arp sa_data field too small (%zu octets) to contain chaddr (%zu octets)",
- sizeof(req.arp_ha.sa_data), macaddr->length);
+ sizeof(req.arp_ha.sa_data), macaddr->vp_length);
return -1;
}
strlcpy(req.arp_dev, interface, sizeof(req.arp_dev));
if (macaddr->da->type == PW_TYPE_ETHERNET) {
- memcpy(&req.arp_ha.sa_data, &macaddr->vp_ether, sizeof(macaddr->vp_ether));
+ memcpy(&req.arp_ha.sa_data, macaddr->vp_ether, sizeof(macaddr->vp_ether));
} else {
- memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->length);
+ memcpy(&req.arp_ha.sa_data, macaddr->vp_octets, macaddr->vp_length);
}
req.arp_flags = ATF_COM;
return -1;
}
#endif
+
+
+#ifdef HAVE_LINUX_IF_PACKET_H
+/*
+ * Open a packet interface raw socket.
+ * Bind it to the specified interface using a device independent physical layer address.
+ */
+int fr_socket_packet(int iface_index, struct sockaddr_ll *p_ll)
+{
+ int lsockfd;
+
+ /* PF_PACKET - packet interface on device level.
+ using a raw socket allows packet data to be unchanged by the device driver.
+ */
+ lsockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+ if (lsockfd < 0) {
+ fr_strerror_printf("cannot open socket: %s", fr_syserror(errno));
+ return lsockfd;
+ }
+
+ /* Set link layer parameters */
+ memset(p_ll, 0, sizeof(struct sockaddr_ll));
+
+ p_ll->sll_family = AF_PACKET;
+ p_ll->sll_protocol = htons(ETH_P_ALL);
+ p_ll->sll_ifindex = iface_index;
+ p_ll->sll_hatype = ARPHRD_ETHER;
+ p_ll->sll_pkttype = PACKET_OTHERHOST;
+ p_ll->sll_halen = 6;
+
+ if (bind(lsockfd, (struct sockaddr *)p_ll, sizeof(struct sockaddr_ll)) < 0) {
+ close(lsockfd);
+ fr_strerror_printf("cannot bind raw socket: %s", fr_syserror(errno));
+ return -1;
+ }
+
+ return lsockfd;
+}
+
+/*
+ * Encode and send a DHCP packet on a raw packet socket.
+ */
+int fr_dhcp_send_raw_packet(int sockfd, struct sockaddr_ll *p_ll, RADIUS_PACKET *packet)
+{
+ VALUE_PAIR *vp;
+ u_char dhcp_packet[1518] = { 0 };
+
+ /* set ethernet source address to our MAC address (DHCP-Client-Hardware-Address). */
+ u_char dhmac[ETH_ADDR_LEN] = { 0 };
+ if ((vp = fr_pair_find_by_num(packet->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY))) {
+ if (vp->length == sizeof(vp->vp_ether)) {
+ memcpy(dhmac, vp->vp_ether, vp->length);
+ }
+ }
+
+ /* fill in Ethernet layer (L2) */
+ struct ethernet_header *ethhdr = (struct ethernet_header *)dhcp_packet;
+ memcpy(ethhdr->ether_dst, eth_bcast, ETH_ADDR_LEN);
+ memcpy(ethhdr->ether_src, dhmac, ETH_ADDR_LEN);
+ ethhdr->ether_type = htons(ETH_TYPE_IP);
+
+ /* fill in IP layer (L3) */
+ struct ip_header *iph = (struct ip_header *)(dhcp_packet + ETH_HDR_SIZE);
+ iph->ip_vhl = IP_VHL(4, 5);
+ iph->ip_tos = 0;
+ iph->ip_len = htons(IP_HDR_SIZE + UDP_HDR_SIZE + packet->data_len);
+ iph->ip_id = 0;
+ iph->ip_off = 0;
+ iph->ip_ttl = 64;
+ iph->ip_p = 17;
+ iph->ip_sum = 0; /* Filled later */
+
+ /* saddr: Packet-Src-IP-Address (default: 0.0.0.0). */
+ iph->ip_src.s_addr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
+
+ /* daddr: packet destination IP addr (should be 255.255.255.255 for broadcast). */
+ iph->ip_dst.s_addr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
+
+ /* IP header checksum */
+ iph->ip_sum = fr_iph_checksum((uint8_t const *)iph, 5);
+
+ /* fill in UDP layer (L4) */
+ udp_header_t *uh = (udp_header_t *) (dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE);
+
+ uh->src = htons(68);
+ uh->dst = htons(67);
+ u_int16_t l4_len = (UDP_HDR_SIZE + packet->data_len);
+ uh->len = htons(l4_len);
+ uh->checksum = 0; /* UDP checksum will be done after dhcp header */
+
+ /* DHCP layer (L7) */
+ dhcp_packet_t *dhpointer = (dhcp_packet_t *)(dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE);
+ /* just copy what FreeRADIUS has encoded for us. */
+ memcpy(dhpointer, packet->data, packet->data_len);
+
+ /* UDP checksum is done here */
+ uh->checksum = fr_udp_checksum((uint8_t const *)(dhcp_packet + ETH_HDR_SIZE + IP_HDR_SIZE), ntohs(uh->len), uh->checksum,
+ packet->src_ipaddr.ipaddr.ip4addr, packet->dst_ipaddr.ipaddr.ip4addr);
+
+ if (fr_debug_lvl > 1) {
+ char type_buf[64];
+ char const *name = type_buf;
+ char src_ip_buf[INET6_ADDRSTRLEN];
+ char dst_ip_buf[INET6_ADDRSTRLEN];
+
+ if ((packet->code >= PW_DHCP_DISCOVER) &&
+ (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
+ name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
+ } else {
+ snprintf(type_buf, sizeof(type_buf), "%d",
+ packet->code - PW_DHCP_OFFSET);
+ }
+
+ DEBUG(
+ "Sending %s Id %08x from %s:%d to %s:%d\n",
+ name, (unsigned int) packet->id,
+ inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)), packet->src_port,
+ inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)), packet->dst_port);
+ }
+
+ return sendto(sockfd, dhcp_packet,
+ (ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE + packet->data_len),
+ 0, (struct sockaddr *) p_ll, sizeof(struct sockaddr_ll));
+}
+
+/*
+ * print an ethernet address in a buffer
+ */
+char * ether_addr_print(const uint8_t *addr, char *buf)
+{
+ sprintf (buf, "%02x:%02x:%02x:%02x:%02x:%02x",
+ addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
+ return buf;
+}
+
+/*
+ * For a client, receive a DHCP packet from a raw packet
+ * socket. Make sure it matches the ongoing request.
+ *
+ * FIXME: split this into two, recv_raw_packet, and verify(packet, original)
+ */
+RADIUS_PACKET *fr_dhcp_recv_raw_packet(int sockfd, struct sockaddr_ll *p_ll, RADIUS_PACKET *request)
+{
+ VALUE_PAIR *vp;
+ RADIUS_PACKET *packet;
+ uint8_t *code;
+ uint32_t magic, xid;
+ ssize_t data_len;
+
+ uint8_t *raw_packet;
+ ethernet_header_t *eth_hdr;
+ struct ip_header *ip_hdr;
+ udp_header_t *udp_hdr;
+ dhcp_packet_t *dhcp_hdr;
+ uint16_t udp_src_port;
+ uint16_t udp_dst_port;
+ size_t dhcp_data_len;
+ int retval;
+ socklen_t sock_len;
+ fd_set read_fd;
+
+ packet = rad_alloc(NULL, false);
+ if (!packet) {
+ fr_strerror_printf("Failed allocating packet");
+ return NULL;
+ }
+
+ raw_packet = talloc_zero_array(packet, uint8_t, MAX_PACKET_SIZE);
+ if (!raw_packet) {
+ fr_strerror_printf("Out of memory");
+ rad_free(&packet);
+ return NULL;
+ }
+
+ packet->sockfd = sockfd;
+
+ /* a packet was received (but maybe it is not for us) */
+ sock_len = sizeof(struct sockaddr_ll);
+ data_len = recvfrom(sockfd, raw_packet, MAX_PACKET_SIZE, 0,
+ (struct sockaddr *)p_ll, &sock_len);
+
+ uint8_t data_offset = ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE; // DHCP data starts after Ethernet, IP, UDP.
+
+ if (data_len <= data_offset) DISCARD_RP("Payload (%d) smaller than required for layers 2+3+4", (int)data_len);
+
+ /* map raw packet to packet header of the different layers (Ethernet, IP, UDP) */
+ eth_hdr = (ethernet_header_t *)raw_packet;
+
+ /* a. Check Ethernet layer data (L2) */
+ if (ntohs(eth_hdr->ether_type) != ETH_TYPE_IP) DISCARD_RP("Ethernet type (%d) != IP", ntohs(eth_hdr->ether_type));
+
+ /* If Ethernet destination is not broadcast (ff:ff:ff:ff:ff:ff)
+ * Check if it matches the source HW address used (DHCP-Client-Hardware-Address = 267)
+ */
+ if ( (memcmp(ð_bcast, ð_hdr->ether_dst, ETH_ADDR_LEN) != 0) &&
+ (vp = fr_pair_find_by_num(request->vps, 267, DHCP_MAGIC_VENDOR, TAG_ANY)) &&
+ (vp->length == sizeof(vp->vp_ether)) &&
+ (memcmp(vp->vp_ether, ð_hdr->ether_dst, ETH_ADDR_LEN) != 0) ) {
+ /* No match. */
+ char eth_dest[17+1];
+ char eth_req_src[17+1];
+ DISCARD_RP("Ethernet destination (%s) is not broadcast and doesn't match request source (%s)",
+ ether_addr_print(eth_hdr->ether_dst, eth_dest),
+ ether_addr_print(vp->vp_ether, eth_req_src));
+ }
+
+ /*
+ * Ethernet is OK. Now look at IP.
+ */
+ ip_hdr = (struct ip_header *)(raw_packet + ETH_HDR_SIZE);
+
+ /* b. Check IPv4 layer data (L3) */
+ if (ip_hdr->ip_p != IPPROTO_UDP) DISCARD_RP("IP protocol (%d) != UDP", ip_hdr->ip_p);
+
+ /*
+ * note: checking the destination IP address is not
+ * useful (it would be the offered IP address - which we
+ * don't know beforehand, or the broadcast address).
+ */
+
+ /*
+ * Now check UDP.
+ */
+ udp_hdr = (udp_header_t *)(raw_packet + ETH_HDR_SIZE + IP_HDR_SIZE);
+
+ /* c. Check UDP layer data (L4) */
+ udp_src_port = ntohs(udp_hdr->src);
+ udp_dst_port = ntohs(udp_hdr->dst);
+
+ /*
+ * A DHCP server will always respond to port 68 (to a
+ * client) or 67 (to a relay). Just check that both
+ * ports are 67 or 68.
+ */
+ if (udp_src_port != 67 && udp_src_port != 68) DISCARD_RP("UDP src port (%d) != DHCP (67 or 68)", udp_src_port);
+ if (udp_dst_port != 67 && udp_dst_port != 68) DISCARD_RP("UDP dst port (%d) != DHCP (67 or 68)", udp_dst_port);
+
+ /* d. Check DHCP layer data */
+ dhcp_data_len = data_len - data_offset;
+
+ if (dhcp_data_len < MIN_PACKET_SIZE) DISCARD_RP("DHCP packet is too small (%d < %d)", dhcp_data_len, MIN_PACKET_SIZE);
+ if (dhcp_data_len > MAX_PACKET_SIZE) DISCARD_RP("DHCP packet is too large (%d > %d)", dhcp_data_len, MAX_PACKET_SIZE);
+
+ dhcp_hdr = (dhcp_packet_t *)(raw_packet + ETH_HDR_SIZE + IP_HDR_SIZE + UDP_HDR_SIZE);
+
+ if (dhcp_hdr->htype != 1) DISCARD_RP("DHCP hardware type (%d) != Ethernet (1)", dhcp_hdr->htype);
+ if (dhcp_hdr->hlen != 6) DISCARD_RP("DHCP hardware address length (%d) != 6", dhcp_hdr->hlen);
+
+ magic = ntohl(dhcp_hdr->option_format);
+
+ if (magic != DHCP_OPTION_MAGIC_NUMBER) DISCARD_RP("DHCP magic cookie (0x%04x) != DHCP (0x%04x)", magic, DHCP_OPTION_MAGIC_NUMBER);
+
+ /*
+ * Reply transaction id must match value from request.
+ */
+ xid = ntohl(dhcp_hdr->xid);
+ if (xid != (uint32_t)request->id) DISCARD_RP("DHCP transaction ID (0x%04x) != xid from request (0x%04x)", xid, request->id)
+
+ /* all checks ok! this is a DHCP reply we're interested in. */
+ packet->data_len = dhcp_data_len;
+ packet->data = talloc_memdup(packet, raw_packet + data_offset, dhcp_data_len);
+ TALLOC_FREE(raw_packet);
+ packet->id = xid;
+
+ code = dhcp_get_option((dhcp_packet_t *) packet->data,
+ packet->data_len, PW_DHCP_MESSAGE_TYPE);
+ if (!code) {
+ fr_strerror_printf("No message-type option was found in the packet");
+ rad_free(&packet);
+ return NULL;
+ }
+
+ if ((code[1] < 1) || (code[2] == 0) || (code[2] > 8)) {
+ fr_strerror_printf("Unknown value for message-type option");
+ rad_free(&packet);
+ return NULL;
+ }
+
+ packet->code = code[2] | PW_DHCP_OFFSET;
+
+ /*
+ * Create a unique vector from the MAC address and the
+ * DHCP opcode. This is a hack for the RADIUS
+ * infrastructure in the rest of the server.
+ *
+ * Note: packet->data[2] == 6, which is smaller than
+ * sizeof(packet->vector)
+ *
+ * FIXME: Look for client-identifier in packet,
+ * and use that, too?
+ */
+ memset(packet->vector, 0, sizeof(packet->vector));
+ memcpy(packet->vector, packet->data + 28, packet->data[2]);
+ packet->vector[packet->data[2]] = packet->code & 0xff;
+
+ packet->src_port = udp_src_port;
+ packet->dst_port = udp_dst_port;
+
+ packet->src_ipaddr.af = AF_INET;
+ packet->src_ipaddr.ipaddr.ip4addr.s_addr = ip_hdr->ip_src.s_addr;
+ packet->dst_ipaddr.af = AF_INET;
+ packet->dst_ipaddr.ipaddr.ip4addr.s_addr = ip_hdr->ip_dst.s_addr;
+
+ if (fr_debug_lvl > 1) {
+ char type_buf[64];
+ char const *name = type_buf;
+ char src_ip_buf[256], dst_ip_buf[256];
+
+ if ((packet->code >= PW_DHCP_DISCOVER) &&
+ (packet->code < (1024 + DHCP_MAX_MESSAGE_TYPE))) {
+ name = dhcp_message_types[packet->code - PW_DHCP_OFFSET];
+ } else {
+ snprintf(type_buf, sizeof(type_buf), "%d", packet->code - PW_DHCP_OFFSET);
+ }
+
+ DEBUG("Received %s of Id %08x from %s:%d to %s:%d\n",
+ name, (unsigned int) packet->id,
+ inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src_ip_buf, sizeof(src_ip_buf)),
+ packet->src_port,
+ inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst_ip_buf, sizeof(dst_ip_buf)),
+ packet->dst_port);
+ }
+
+ return packet;
+}
+#endif