Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send, test/send: isolate cease_adv of add_ra_header #201

Merged
merged 4 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions send.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,7 @@ static void add_ra_header(struct safe_buffer *sb, struct ra_header_info const *r
/* Mobile IPv6 ext */
radvert.nd_ra_flags_reserved |= (ra_header_info->AdvHomeAgentFlag) ? ND_RA_FLAG_HOME_AGENT : 0;

if (cease_adv) {
radvert.nd_ra_router_lifetime = 0;
} else {
/* if forwarding is disabled, send zero router lifetime */
radvert.nd_ra_router_lifetime = !check_ip6_forwarding() ? htons(ra_header_info->AdvDefaultLifetime) : 0;
}
radvert.nd_ra_router_lifetime = cease_adv ? 0 : htons(ra_header_info->AdvDefaultLifetime);
radvert.nd_ra_flags_reserved |= (ra_header_info->AdvDefaultPreference << ND_OPT_RI_PRF_SHIFT) & ND_OPT_RI_PRF_MASK;

radvert.nd_ra_reachable = htonl(ra_header_info->AdvReachableTime);
Expand Down Expand Up @@ -888,7 +883,10 @@ static int send_ra(int sock, struct Interface *iface, struct in6_addr const *des

// Build RA header
struct safe_buffer *ra_hdr = new_safe_buffer();
add_ra_header(ra_hdr, &iface->ra_header_info, iface->state_info.cease_adv);
// if forwarding is disabled, send zero router lifetime
// the check_ip6 function is hoisted here to enable testing of add_ra_header
int cease_adv = iface->state_info.cease_adv || !check_ip6_forwarding();
add_ra_header(ra_hdr, &iface->ra_header_info, cease_adv);
// Build RA option list
struct safe_buffer_list *ra_opts = build_ra_options(iface, dest);

Expand Down
3 changes: 1 addition & 2 deletions test/print_safe_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ size_t snprint_safe_buffer(char *s, size_t size, struct safe_buffer const *sb)
{
size_t count = 0;

count += snprintf((s + count), (size - count), "unsigned char expected[] = {");
count--;
robbat2 marked this conversation as resolved.
Show resolved Hide resolved
count += snprintf((s + count), (size - count), "unsigned char expected[] = { /* sb.allocated = %ld, sb.used = %ld */", sb->allocated, sb->used);

for (size_t i = 0; i < sb->used; ++i) {
if (i % 8 == 0) {
Expand Down
62 changes: 58 additions & 4 deletions test/send.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,72 @@ static void iface_teardown(void)
iface = 0;
}

START_TEST(test_add_ra_header)
START_TEST(test_add_ra_header_cease_adv0)
{

struct safe_buffer sb = SAFE_BUFFER_INIT;
add_ra_header(&sb, &iface->ra_header_info, iface->state_info.cease_adv);
add_ra_header(&sb, &iface->ra_header_info, 0);

#ifdef PRINT_SAFE_BUFFER
char buf[4096];
snprint_safe_buffer(buf, 4096, &sb);
ck_assert_msg(0, "\n// ra_header_info->AdvDefaultLifetime = %x\n%s", iface->ra_header_info.AdvDefaultLifetime, &buf);
#else
// Lifetime should be -1/ffff in this case, because we have not set it to anything else.
// interface.c:40:iface->ra_header_info.AdvDefaultLifetime = -1;
unsigned char expected[] = {
// nd_ra_type
// nd_ra_code
0x86, 0x00,
stappersg marked this conversation as resolved.
Show resolved Hide resolved
// nd_ra_cksum
0x00, 0x00,
// nd_ra_curhoplimit
0x40,
// nd_ra_flags_reserved
0x00,
// nd_ra_router_lifetime
0xff, 0xff,
// nd_ra_reachable
0x00, 0x00, 0x00, 0x00,
// nd_ra_retransmit
0x00, 0x00, 0x00, 0x00,
};

ck_assert_int_eq(sizeof(expected), sb.used);
ck_assert_int_eq(0, memcmp(expected, sb.buffer, sb.used));
#endif

safe_buffer_free(&sb);
}
END_TEST

START_TEST(test_add_ra_header_cease_adv1)
{

struct safe_buffer sb = SAFE_BUFFER_INIT;
add_ra_header(&sb, &iface->ra_header_info, 1);

#ifdef PRINT_SAFE_BUFFER
char buf[4096];
snprint_safe_buffer(buf, 4096, &sb);
ck_assert_msg(0, "\n%s", &buf);
#else
unsigned char expected[] = {
0x86, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// nd_ra_type
stappersg marked this conversation as resolved.
Show resolved Hide resolved
// nd_ra_code
0x86, 0x00,
// nd_ra_cksum
0x00, 0x00,
// nd_ra_curhoplimit
0x40,
// nd_ra_flags_reserved
0x00,
// nd_ra_router_lifetime
0x00, 0x00,
// nd_ra_reachable
0x00, 0x00, 0x00, 0x00,
// nd_ra_retransmit
0x00, 0x00, 0x00, 0x00,
};

ck_assert_int_eq(sizeof(expected), sb.used);
Expand Down Expand Up @@ -352,7 +405,8 @@ Suite *send_suite(void)

TCase *tc_build = tcase_create("build");
tcase_add_unchecked_fixture(tc_build, iface_setup, iface_teardown);
tcase_add_test(tc_build, test_add_ra_header);
tcase_add_test(tc_build, test_add_ra_header_cease_adv0);
tcase_add_test(tc_build, test_add_ra_header_cease_adv1);
tcase_add_test(tc_build, test_add_ra_options_prefix);
tcase_add_test(tc_build, test_add_ra_options_route);
tcase_add_test(tc_build, test_add_ra_options_rdnss);
Expand Down