Skip to content

Commit da08e07

Browse files
committed
Fix DHCPv6 options serialization to prevent extra bytes in replies
In the `generate_reply_options` function, fixed an issue where DHCPv6 reply packets contained extra unintended bytes at the end. The problem was due to the `options` pointer not being incremented after copying the `opt_rapid` and `boot_file_url` options into the packet buffer. Changes made: - After copying `opt_rapid`, the `options` pointer is now incremented by `reply_options.opt_rapid_len`. - After copying `boot_file_url`, the `options` pointer is now incremented by `reply_options.opt_boot_file_len`. These changes ensure that all DHCPv6 options are correctly serialized in the packet buffer without overlaps or gaps. By properly advancing the `options` pointer, we prevent unintended data from being included at the end of the packet, eliminating the extra bytes that were being sent. This fix addresses the issue where clients were receiving malformed DHCPv6 packets due to the extra bytes, which could lead to communication errors or unexpected behavior.
1 parent a6d0032 commit da08e07

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/nodes/dhcpv6_node.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,54 +213,77 @@ static int generate_reply_options(struct rte_mbuf *m, uint8_t *options, int opti
213213
int reply_options_len;
214214
struct dhcpv6_opt_server_id_ll opt_sid;
215215
struct dhcpv6_opt_dns_servers dns_opt;
216-
struct dp_dhcpv6_reply_options reply_options = {0}; // this makes *_len fields 0, needed later
216+
struct dp_dhcpv6_reply_options reply_options = {0}; // This initializes *_len fields to 0
217217
const struct dp_conf_dhcp_dns *dhcpv6_dns = dp_conf_get_dhcpv6_dns();
218218

219+
// Parse the options from the incoming packet
219220
if (DP_FAILED(parse_options(m, options, options_len, &reply_options))) {
220221
DPS_LOG_WARNING("Invalid DHCPv6 options received");
221222
return DP_ERROR;
222223
}
223224

225+
// Prepare the DNS server option
224226
dns_opt.opt_code = htons(DHCPV6_OPT_DNS);
225227
dns_opt.opt_len = htons(dhcpv6_dns->len);
226228

229+
// Prepare the Server Identifier option
227230
opt_sid = opt_sid_template;
228231
rte_ether_addr_copy(&rte_pktmbuf_mtod(m, struct rte_ether_hdr *)->dst_addr, &opt_sid.id.mac);
229232

230-
reply_options_len = (int)sizeof(opt_sid) + reply_options.opt_cid_len + reply_options.opt_iana_len + reply_options.opt_rapid_len;
231-
reply_options_len += (int)(sizeof(dns_opt.opt_code) + sizeof(dns_opt.opt_len) + ntohs(dns_opt.opt_len));
233+
// Calculate the total length of reply options
234+
reply_options_len = (int)sizeof(opt_sid)
235+
+ reply_options.opt_cid_len
236+
+ reply_options.opt_iana_len
237+
+ reply_options.opt_rapid_len
238+
+ (int)(sizeof(dns_opt.opt_code) + sizeof(dns_opt.opt_len) + ntohs(dns_opt.opt_len));
232239

233-
if (reply_options.pxe_mode != DP_PXE_MODE_NONE)
240+
if (reply_options.pxe_mode != DP_PXE_MODE_NONE) {
234241
reply_options_len += reply_options.opt_boot_file_len;
242+
}
235243

236-
if (DP_FAILED(resize_packet(m, reply_options_len - options_len)))
244+
// Resize the packet to accommodate the reply options
245+
if (DP_FAILED(resize_packet(m, reply_options_len - options_len))) {
237246
return DP_ERROR;
247+
}
238248

239-
// had to use memcpy() here, because GCC's array-bounds check fails for rte_memcpy (using XMM optimization)
249+
// Start copying the reply options into the packet buffer
250+
// Copy Server Identifier option
240251
memcpy(options, &opt_sid, sizeof(opt_sid));
241252
options += sizeof(opt_sid);
253+
254+
// Copy Client Identifier option if present
242255
if (reply_options.opt_cid_len) {
243256
memcpy(options, (void *)&reply_options.opt_cid, reply_options.opt_cid_len);
244257
options += reply_options.opt_cid_len;
245258
}
259+
260+
// Copy IA_NA option if present
246261
if (reply_options.opt_iana_len) {
247262
memcpy(options, (void *)&reply_options.opt_iana, reply_options.opt_iana_len);
248263
options += reply_options.opt_iana_len;
249264
}
250-
if (reply_options.opt_rapid_len)
265+
266+
// Copy Rapid Commit option if present
267+
if (reply_options.opt_rapid_len) {
251268
memcpy(options, &reply_options.opt_rapid, reply_options.opt_rapid_len);
269+
options += reply_options.opt_rapid_len; // Increment options pointer
270+
}
252271

253-
// Add DNS server option
272+
// Copy DNS Servers option
254273
memcpy(options, &dns_opt.opt_code, sizeof(dns_opt.opt_code));
255274
options += sizeof(dns_opt.opt_code);
256275
memcpy(options, &dns_opt.opt_len, sizeof(dns_opt.opt_len));
257276
options += sizeof(dns_opt.opt_len);
258277
memcpy(options, dhcpv6_dns->array, dhcpv6_dns->len);
259278
options += dhcpv6_dns->len;
260279

261-
if (reply_options.pxe_mode != DP_PXE_MODE_NONE)
280+
// Copy Boot File URL option if PXE mode is enabled
281+
if (reply_options.pxe_mode != DP_PXE_MODE_NONE) {
262282
memcpy(options, &reply_options.boot_file_url, reply_options.opt_boot_file_len);
283+
options += reply_options.opt_boot_file_len; // Increment options pointer
284+
}
263285

286+
// Return the total length of the reply options
264287
return reply_options_len;
265288
}
266289

0 commit comments

Comments
 (0)