pkt_filter_lpf_unittest.cc 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config.h>
#include <asiolink/io_address.h>
#include <dhcp/iface_mgr.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt_filter_lpf.h>
#include <dhcp/protocol_util.h>
#include <util/buffer.h>

#include <gtest/gtest.h>

#include <linux/if_packet.h>
#include <sys/socket.h>

using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::util;

namespace {

/// Port number used by tests.
const uint16_t PORT = 10067;
/// Size of the buffer holding received packets.
const size_t RECV_BUF_SIZE = 2048;

/// This class handles has simple algorithm checking
/// presence of loopback interface and initializing
/// its index.
class PktFilterLPFTest : public ::testing::Test {
public:
44
45
46

    /// @brief Constructor
    ///
47
    /// This constructor initializes socket_ member to a negative value.
48
49
50
    /// Explcit initialization is performed here because some of the
    /// tests do not initialize this value. In such cases, destructor
    /// could invoke close() on uninitialized socket descriptor which
51
    /// would result in errors being reported by Valgrind.
52
    PktFilterLPFTest()
53
        : socket_(-1) {
54
55
56
57
        // Initialize ifname_ and ifindex_.
        loInit();
    }

58
59
60
    /// @brief Destructor
    ///
    /// Closes open socket (if any).
61
62
63
    ~PktFilterLPFTest() {
        // Cleanup after each test. This guarantees
        // that the socket does not hang after a test.
64
65
66
        if (socket_ >= 0) {
            close(socket_);
        }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    }

    /// @brief Detect loopback interface.
    ///
    /// @todo this function will be removed once cross-OS interface
    /// detection is implemented
    void loInit() {
        if (if_nametoindex("lo") > 0) {
            ifname_ = "lo";
            ifindex_ = if_nametoindex("lo");

        } else if (if_nametoindex("lo0") > 0) {
            ifname_ = "lo0";
            ifindex_ = if_nametoindex("lo0");

        } else {
            std::cout << "Failed to detect loopback interface. Neither "
                      << "lo nor lo0 worked. Giving up." << std::endl;
            FAIL();



        }
    }

    std::string ifname_; ///< Loopback interface name
    uint16_t ifindex_;   ///< Loopback interface index.
    int socket_;         ///< Socket descriptor.

};

98
99
100
101
102
103
104
105
106
// This test verifies that the PktFilterLPF class reports its capability
// to send packets to the host having no IP address assigned.
TEST_F(PktFilterLPFTest, isDirectResponseSupported) {
    // Create object under test.
    PktFilterLPF pkt_filter;
    // Must support direct responses.
    EXPECT_TRUE(pkt_filter.isDirectResponseSupported());
}

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// All tests below require root privileges to execute successfully. If
// they are run as non-root user they will fail due to insufficient privileges
// to open raw network sockets. Therefore, they should remain disabled by default
// and "DISABLED_" tags should not be removed. If one is willing to run these
// tests please run "make check" as root and enable execution of disabled tests
// by setting GTEST_ALSO_RUN_DISABLED_TESTS to a value other than 0. In order
// to run tests from this particular file, set the GTEST_FILTER environmental
// variable to "PktFilterLPFTest.*" apart from GTEST_ALSO_RUN_DISABLED_TESTS
// setting.

// This test verifies that the raw AF_PACKET family socket can
// be opened and bound to the specific interface.
TEST_F(PktFilterLPFTest, DISABLED_openSocket) {
    // Create object representing loopback interface.
    Iface iface(ifname_, ifindex_);
    // Set loopback address.
    IOAddress addr("127.0.0.1");

    // Try to open socket.
    PktFilterLPF pkt_filter;
    socket_ = pkt_filter.openSocket(iface, addr, PORT, false, false);
    // Check that socket has been opened.
    ASSERT_GE(socket_, 0);

    // Verify that the socket belongs to AF_PACKET family.
    sockaddr_ll sock_address;
    socklen_t sock_address_len = sizeof(sock_address);
    ASSERT_EQ(0, getsockname(socket_, reinterpret_cast<sockaddr*>(&sock_address),
                             &sock_address_len));
    EXPECT_EQ(AF_PACKET, sock_address.sll_family);

    // Verify that the socket is bound to appropriate interface.
    EXPECT_EQ(ifindex_, sock_address.sll_ifindex);

    // Verify that the socket has SOCK_RAW type.
    int sock_type;
    socklen_t sock_type_len = sizeof(sock_type);
    ASSERT_EQ(0, getsockopt(socket_, SOL_SOCKET, SO_TYPE, &sock_type, &sock_type_len));
    EXPECT_EQ(SOCK_RAW, sock_type);
}

// This test verifies correctness of sending DHCP packet through the raw
// socket, whereby all IP stack headers are hand-crafted.
TEST_F(PktFilterLPFTest, DISABLED_send) {
        // Let's create a DHCPv4 packet.
    Pkt4Ptr pkt(new Pkt4(DHCPOFFER, 0));
    ASSERT_TRUE(pkt);

    // Set required fields.
156
157
158
159
160
161
    // By setting the local address to broadcast we simulate the
    // typical scenario when client's request was send to broadcast
    // address and server by default used it as a source address
    // in its response. The send() function should be able to detect
    // it and correct the source address.
    pkt->setLocalAddr(IOAddress("255.255.255.255"));
162
    pkt->setRemoteAddr(IOAddress("127.0.0.1"));
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    pkt->setRemotePort(PORT);
    pkt->setLocalPort(PORT + 1);
    pkt->setIndex(ifindex_);
    pkt->setIface(ifname_);
    pkt->setHops(6);
    pkt->setSecs(42);
    pkt->setCiaddr(IOAddress("192.0.2.1"));
    pkt->setSiaddr(IOAddress("192.0.2.2"));
    pkt->setYiaddr(IOAddress("192.0.2.3"));
    pkt->setGiaddr(IOAddress("192.0.2.4"));

    // Create the on-wire data.
    ASSERT_NO_THROW(pkt->pack());

    // Packet will be sent over loopback interface.
    Iface iface(ifname_, ifindex_);
    IOAddress addr("127.0.0.1");

    // Create an instance of the class which we are testing.
    PktFilterLPF pkt_filter;
    // Open socket. We don't check that the socket has appropriate
    // options and family set because we have checked that in the
    // openSocket test already.
    socket_ = pkt_filter.openSocket(iface, addr, PORT, false, false);
    ASSERT_GE(socket_, 0);

    // Send the packet over the socket.
    ASSERT_NO_THROW(pkt_filter.send(iface, socket_, pkt));

    // Read the data from socket.
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(socket_, &readfds);
    
    struct timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    int result = select(socket_ + 1, &readfds, NULL, NULL, &timeout);
    // We should receive some data from loopback interface.
    ASSERT_GT(result, 0);

    // Get the actual data.
    uint8_t rcv_buf[RECV_BUF_SIZE];
    result = recv(socket_, rcv_buf, RECV_BUF_SIZE, 0);
    ASSERT_GT(result, 0);

    Pkt4Ptr dummy_pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 0));

    InputBuffer buf(rcv_buf, result);

    // Decode ethernet, ip and udp headers.
    decodeEthernetHeader(buf, dummy_pkt);
    decodeIpUdpHeader(buf, dummy_pkt);

    // Create the DHCPv4 packet from the received data.
    std::vector<uint8_t> dhcp_buf;
    buf.readVector(dhcp_buf, buf.getLength() - buf.getPosition());
    Pkt4Ptr rcvd_pkt(new Pkt4(&dhcp_buf[0], dhcp_buf.size()));
    ASSERT_TRUE(rcvd_pkt);

    // Parse the packet.
    ASSERT_NO_THROW(rcvd_pkt->unpack());

    // Verify that the received packet matches sent packet.
    EXPECT_EQ(pkt->getHops(), rcvd_pkt->getHops());
    EXPECT_EQ(pkt->getOp(),   rcvd_pkt->getOp());
    EXPECT_EQ(pkt->getSecs(), rcvd_pkt->getSecs());
    EXPECT_EQ(pkt->getFlags(), rcvd_pkt->getFlags());
    EXPECT_EQ(pkt->getCiaddr(), rcvd_pkt->getCiaddr());
    EXPECT_EQ(pkt->getSiaddr(), rcvd_pkt->getSiaddr());
    EXPECT_EQ(pkt->getYiaddr(), rcvd_pkt->getYiaddr());
    EXPECT_EQ(pkt->getGiaddr(), rcvd_pkt->getGiaddr());
    EXPECT_EQ(pkt->getTransid(), rcvd_pkt->getTransid());
    EXPECT_TRUE(pkt->getSname() == rcvd_pkt->getSname());
    EXPECT_TRUE(pkt->getFile() == rcvd_pkt->getFile());
    EXPECT_EQ(pkt->getHtype(), rcvd_pkt->getHtype());
    EXPECT_EQ(pkt->getHlen(), rcvd_pkt->getHlen());
}

// This test verifies correctness of reception of the DHCP packet over
// raw socket, whereby all IP stack headers are hand-crafted.
TEST_F(PktFilterLPFTest, DISABLED_receive) {

    // Let's create a DHCPv4 packet.
    Pkt4Ptr pkt(new Pkt4(DHCPOFFER, 0));
    ASSERT_TRUE(pkt);

    // Set required fields.
    pkt->setLocalAddr(IOAddress("127.0.0.1"));
252
    pkt->setRemoteAddr(IOAddress("127.0.0.1"));
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    pkt->setRemotePort(PORT);
    pkt->setLocalPort(PORT + 1);
    pkt->setIndex(ifindex_);
    pkt->setIface(ifname_);
    pkt->setHops(6);
    pkt->setSecs(42);
    pkt->setCiaddr(IOAddress("192.0.2.1"));
    pkt->setSiaddr(IOAddress("192.0.2.2"));
    pkt->setYiaddr(IOAddress("192.0.2.3"));
    pkt->setGiaddr(IOAddress("192.0.2.4"));

    // Create the on-wire data.
    ASSERT_NO_THROW(pkt->pack());

    // Packet will be sent over loopback interface.
    Iface iface(ifname_, ifindex_);
    IOAddress addr("127.0.0.1");

    // Create an instance of the class which we are testing.
    PktFilterLPF pkt_filter;
    // Open socket. We don't check that the socket has appropriate
    // options and family set because we have checked that in the
    // openSocket test already.
    socket_ = pkt_filter.openSocket(iface, addr, PORT, false, false);
    ASSERT_GE(socket_, 0);

    // Send the packet over the socket.
    ASSERT_NO_THROW(pkt_filter.send(iface, socket_, pkt));

    // Receive the packet.
    SocketInfo socket_info(socket_, IOAddress("127.0.0.1"), PORT);
    Pkt4Ptr rcvd_pkt = pkt_filter.receive(iface, socket_info);
    // Check that the packet has been correctly received.
    ASSERT_TRUE(rcvd_pkt);

    // Parse the packet.
    ASSERT_NO_THROW(rcvd_pkt->unpack());

    // Verify that the received packet matches sent packet.
    EXPECT_EQ(pkt->getHops(), rcvd_pkt->getHops());
    EXPECT_EQ(pkt->getOp(),   rcvd_pkt->getOp());
    EXPECT_EQ(pkt->getSecs(), rcvd_pkt->getSecs());
    EXPECT_EQ(pkt->getFlags(), rcvd_pkt->getFlags());
    EXPECT_EQ(pkt->getCiaddr(), rcvd_pkt->getCiaddr());
    EXPECT_EQ(pkt->getSiaddr(), rcvd_pkt->getSiaddr());
    EXPECT_EQ(pkt->getYiaddr(), rcvd_pkt->getYiaddr());
    EXPECT_EQ(pkt->getGiaddr(), rcvd_pkt->getGiaddr());
    EXPECT_EQ(pkt->getTransid(), rcvd_pkt->getTransid());
    EXPECT_TRUE(pkt->getSname() == rcvd_pkt->getSname());
    EXPECT_TRUE(pkt->getFile() == rcvd_pkt->getFile());
    EXPECT_EQ(pkt->getHtype(), rcvd_pkt->getHtype());
    EXPECT_EQ(pkt->getHlen(), rcvd_pkt->getHlen());
}

} // anonymous namespace