blob: 9b0633ec514a03cc366f3e2005cdd6f3bbc84292 [file] [log] [blame]
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "patchpanel/ndproxy.h"
#include <stdlib.h>
#include <net/ethernet.h>
#include <gtest/gtest.h>
namespace patchpanel {
const MacAddress physical_if_mac({0xa0, 0xce, 0xc8, 0xc6, 0x91, 0x0a});
const MacAddress guest_if_mac({0xd2, 0x47, 0xf7, 0xc5, 0x9e, 0x53});
const uint8_t ping_frame[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x86, 0xdd, 0x60, 0x0b, 0x8d, 0xb4, 0x00, 0x40, 0x3a, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0xb9, 0x3c, 0x13, 0x8f,
0x00, 0x09, 0xde, 0x6a, 0x78, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x8e, 0x13,
0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37};
const uint8_t rs_frame[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x02, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0,
0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x10, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75, 0xb2, 0x80, 0x97, 0x83,
0x76, 0xbf, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x85, 0x00, 0x2f, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x01, 0x01, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0};
// The byte at index 19 should be 0x10 instead of 0x11
const uint8_t rs_frame_too_large_plen[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x02, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0,
0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x11, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75, 0xb2, 0x80, 0x97, 0x83,
0x76, 0xbf, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x85, 0x00, 0x2f, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x01, 0x01, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0};
// The byte at index 19 should be 0x10 instead of 0x0f
const uint8_t rs_frame_too_small_plen[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x02, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0,
0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75, 0xb2, 0x80, 0x97, 0x83,
0x76, 0xbf, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x85, 0x00, 0x2f, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x01, 0x01, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0};
const uint8_t rs_frame_translated[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x02, 0xa0, 0xce, 0xc8, 0xc6, 0x91, 0x0a,
0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x10, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75, 0xb2, 0x80, 0x97, 0x83,
0x76, 0xbf, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x85, 0x00, 0x93, 0x55, 0x00, 0x00,
0x00, 0x00, 0x01, 0x01, 0xa0, 0xce, 0xc8, 0xc6, 0x91, 0x0a};
const uint8_t ra_frame[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x01, 0xc4, 0x71, 0xfe, 0xf1, 0xf6, 0x7f,
0x86, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1,
0xf6, 0x7f, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x86, 0x00, 0x8a, 0xd5, 0x40, 0x00,
0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
0xc4, 0x71, 0xfe, 0xf1, 0xf6, 0x7f, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00,
0x05, 0xdc, 0x03, 0x04, 0x40, 0xc0, 0x00, 0x27, 0x8d, 0x00, 0x00, 0x09,
0x3a, 0x80, 0x00, 0x00, 0x00, 0x00, 0x24, 0x01, 0xfa, 0x00, 0x00, 0x04,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t ra_frame_translated[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x01, 0xd2, 0x47, 0xf7, 0xc5, 0x9e, 0x53,
0x86, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1,
0xf6, 0x7f, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x86, 0x00, 0xdc, 0x53, 0x40, 0x04,
0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
0xd2, 0x47, 0xf7, 0xc5, 0x9e, 0x53, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00,
0x05, 0xdc, 0x03, 0x04, 0x40, 0xc0, 0x00, 0x27, 0x8d, 0x00, 0x00, 0x09,
0x3a, 0x80, 0x00, 0x00, 0x00, 0x00, 0x24, 0x01, 0xfa, 0x00, 0x00, 0x04,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t ra_frame_option_reordered[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x01, 0xc4, 0x71, 0xfe, 0xf1, 0xf6, 0x7f,
0x86, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1,
0xf6, 0x7f, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x86, 0x00, 0x8a, 0xd5, 0x40, 0x00,
0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01,
0x00, 0x00, 0x00, 0x00, 0x05, 0xdc, 0x01, 0x01, 0xc4, 0x71, 0xfe, 0xf1,
0xf6, 0x7f, 0x03, 0x04, 0x40, 0xc0, 0x00, 0x27, 0x8d, 0x00, 0x00, 0x09,
0x3a, 0x80, 0x00, 0x00, 0x00, 0x00, 0x24, 0x01, 0xfa, 0x00, 0x00, 0x04,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t ra_frame_option_reordered_translated[] = {
0x33, 0x33, 0x00, 0x00, 0x00, 0x01, 0xd2, 0x47, 0xf7, 0xc5, 0x9e, 0x53,
0x86, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1,
0xf6, 0x7f, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x86, 0x00, 0xdc, 0x53, 0x40, 0x04,
0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01,
0x00, 0x00, 0x00, 0x00, 0x05, 0xdc, 0x01, 0x01, 0xd2, 0x47, 0xf7, 0xc5,
0x9e, 0x53, 0x03, 0x04, 0x40, 0xc0, 0x00, 0x27, 0x8d, 0x00, 0x00, 0x09,
0x3a, 0x80, 0x00, 0x00, 0x00, 0x00, 0x24, 0x01, 0xfa, 0x00, 0x00, 0x04,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t ns_frame[] = {
0xd2, 0x47, 0xf7, 0xc5, 0x9e, 0x53, 0x1a, 0x9b, 0x82, 0xbd, 0xc0,
0xa0, 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff,
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75, 0xb2,
0x80, 0x97, 0x83, 0x76, 0xbf, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1, 0xf6, 0x7f, 0x87,
0x00, 0xba, 0x27, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1, 0xf6,
0x7f, 0x01, 0x01, 0x1a, 0x9b, 0x82, 0xbd, 0xc0, 0xa0};
const uint8_t ns_frame_translated[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xa0, 0xce, 0xc8, 0xc6, 0x91,
0x0a, 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff,
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75, 0xb2,
0x80, 0x97, 0x83, 0x76, 0xbf, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1, 0xf6, 0x7f, 0x87,
0x00, 0x1d, 0x81, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1, 0xf6,
0x7f, 0x01, 0x01, 0xa0, 0xce, 0xc8, 0xc6, 0x91, 0x0a};
const uint8_t na_frame[] = {
0xa0, 0xce, 0xc8, 0xc6, 0x91, 0x0a, 0xc4, 0x71, 0xfe, 0xf1, 0xf6, 0x7f,
0x86, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1,
0xf6, 0x7f, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75,
0xb2, 0x80, 0x97, 0x83, 0x76, 0xbf, 0x88, 0x00, 0x58, 0x29, 0xc0, 0x00,
0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71,
0xfe, 0xff, 0xfe, 0xf1, 0xf6, 0x7f};
const uint8_t na_frame_translated[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd2, 0x47, 0xf7, 0xc5, 0x9e, 0x53,
0x86, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3a, 0xff, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71, 0xfe, 0xff, 0xfe, 0xf1,
0xf6, 0x7f, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x75,
0xb2, 0x80, 0x97, 0x83, 0x76, 0xbf, 0x88, 0x00, 0x58, 0x29, 0xc0, 0x00,
0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x71,
0xfe, 0xff, 0xfe, 0xf1, 0xf6, 0x7f};
const uint8_t tcp_frame[] = {
0xc4, 0x71, 0xfe, 0xf1, 0xf6, 0x7f, 0xa0, 0xce, 0xc8, 0xc6, 0x91,
0x0a, 0x86, 0xdd, 0x60, 0x03, 0xa3, 0x57, 0x00, 0x20, 0x06, 0x40,
0x24, 0x01, 0xfa, 0x00, 0x00, 0x04, 0x00, 0x02, 0xf0, 0x94, 0x0d,
0xa1, 0x12, 0x6f, 0xfd, 0x6b, 0x24, 0x04, 0x68, 0x00, 0x40, 0x08,
0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x85,
0xc0, 0x01, 0xbb, 0xb2, 0x7e, 0xd0, 0xa6, 0x0c, 0x57, 0xa5, 0x6c,
0x80, 0x10, 0x01, 0x54, 0x04, 0xb9, 0x00, 0x00, 0x01, 0x01, 0x08,
0x0a, 0x00, 0x5a, 0x59, 0xc0, 0x32, 0x53, 0x14, 0x3a};
TEST(NDProxyTest, GetPrefixInfoOption) {
uint8_t in_buffer_extended[IP_MAXPACKET + ETHER_HDR_LEN + 4];
uint8_t* in_buffer = NDProxy::AlignFrameBuffer(in_buffer_extended);
const nd_opt_prefix_info* prefix_info;
memcpy(in_buffer, ra_frame, sizeof(ra_frame));
prefix_info = NDProxy::GetPrefixInfoOption(in_buffer, sizeof(ra_frame));
EXPECT_NE(nullptr, prefix_info);
EXPECT_EQ(64, prefix_info->nd_opt_pi_prefix_len);
memcpy(in_buffer, rs_frame, sizeof(rs_frame));
prefix_info = NDProxy::GetPrefixInfoOption(in_buffer, sizeof(rs_frame));
EXPECT_EQ(nullptr, prefix_info);
}
TEST(NDProxyTest, TranslateFrame) {
uint8_t in_buffer_extended[IP_MAXPACKET + ETHER_HDR_LEN + 4];
uint8_t out_buffer_extended[IP_MAXPACKET + ETHER_HDR_LEN + 4];
uint8_t* in_buffer = NDProxy::AlignFrameBuffer(in_buffer_extended);
uint8_t* out_buffer = NDProxy::AlignFrameBuffer(out_buffer_extended);
int result;
NDProxy ndproxy;
ndproxy.Init();
memcpy(in_buffer, tcp_frame, sizeof(tcp_frame));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(tcp_frame),
physical_if_mac, out_buffer);
EXPECT_EQ(NDProxy::kTranslateErrorNotICMPv6Frame, result);
memcpy(in_buffer, ping_frame, sizeof(ping_frame));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(ping_frame),
physical_if_mac, out_buffer);
EXPECT_EQ(NDProxy::kTranslateErrorNotNDFrame, result);
memcpy(in_buffer, rs_frame_too_large_plen, sizeof(rs_frame_too_large_plen));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(rs_frame_too_large_plen),
physical_if_mac, out_buffer);
EXPECT_EQ(NDProxy::kTranslateErrorMismatchedIp6Length, result);
memcpy(in_buffer, rs_frame_too_small_plen, sizeof(rs_frame_too_small_plen));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(rs_frame_too_small_plen),
physical_if_mac, out_buffer);
EXPECT_EQ(NDProxy::kTranslateErrorMismatchedIp6Length, result);
memcpy(in_buffer, rs_frame, sizeof(rs_frame));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(rs_frame),
physical_if_mac, out_buffer);
EXPECT_EQ(sizeof(rs_frame_translated), result);
EXPECT_EQ(0, memcmp(rs_frame_translated, out_buffer, sizeof(rs_frame)));
memcpy(in_buffer, ra_frame, sizeof(ra_frame));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(ra_frame), guest_if_mac,
out_buffer);
EXPECT_EQ(sizeof(ra_frame_translated), result);
EXPECT_EQ(0, memcmp(ra_frame_translated, out_buffer, sizeof(ra_frame)));
memcpy(in_buffer, ra_frame_option_reordered,
sizeof(ra_frame_option_reordered));
result = ndproxy.TranslateNDFrame(
in_buffer, sizeof(ra_frame_option_reordered), guest_if_mac, out_buffer);
EXPECT_EQ(sizeof(ra_frame_option_reordered_translated), result);
EXPECT_EQ(0, memcmp(ra_frame_option_reordered_translated, out_buffer,
sizeof(ra_frame_option_reordered)));
memcpy(in_buffer, ns_frame, sizeof(ns_frame));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(ns_frame),
physical_if_mac, out_buffer);
EXPECT_EQ(sizeof(ns_frame_translated), result);
EXPECT_EQ(0, memcmp(ns_frame_translated, out_buffer, sizeof(ns_frame)));
memcpy(in_buffer, na_frame, sizeof(na_frame));
result = ndproxy.TranslateNDFrame(in_buffer, sizeof(na_frame), guest_if_mac,
out_buffer);
EXPECT_EQ(sizeof(na_frame_translated), result);
EXPECT_EQ(0, memcmp(na_frame_translated, out_buffer, sizeof(na_frame)));
}
} // namespace patchpanel