summaryrefslogtreecommitdiff
path: root/tests/test-netlink-policy.c
blob: 55083935a8112e6698bb584791e009dec4b327a0 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
98
99
100
101
102
103
104
105
106
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
/*
 * Copyright (c) 2021 Canonical
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include <string.h>

#include "netlink.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/types.h"
#include "ovstest.h"
#include "util.h"

struct nlattr_fixture {
    struct nlattr nlattr;
    uint8_t data[32];
};

/* nla_len is an inline function in the kernel net/netlink header, which we
 * don't necessarilly have at build time, so provide our own with
 * non-conflicting name. */
static int
_nla_len(const struct nlattr *nla) {
    return nla->nla_len - NLA_HDRLEN;
}

#define TEST_POLICY_ATTR 42

static void
test_nl_policy_parse_ll_addr(struct ovs_cmdl_context *ctx OVS_UNUSED) {
    struct nl_policy policy[] = {
        [TEST_POLICY_ATTR] = { .type = NL_A_LL_ADDR,
                               .optional = false, },
    };
    struct nlattr *attrs[ARRAY_SIZE(policy)];
    struct nlattr_fixture fixture_nl_data_policy_short = {
        /* too short according to policy */
        .nlattr.nla_len = 5,
        .nlattr.nla_type = TEST_POLICY_ATTR,
        .data = { 0x00 },
    };
    struct nlattr_fixture fixture_nl_data_policy_long = {
        /* too long according to policy */
        .nlattr.nla_len = 25,
        .nlattr.nla_type = TEST_POLICY_ATTR,
        .data = { 0x00, 0x00, 0x67, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
                  0x00, 0xe4, 0x1d, 0x2d, 0x03, 0x00, 0xa5, 0xf0, 0x2f, 0x00,
                  0x00 },
    };
    struct nlattr_fixture fixture_nl_data_eth = {
        /* valid policy and eth_addr length */
        .nlattr.nla_len = 10,
        .nlattr.nla_type = TEST_POLICY_ATTR,
        .data = { 0x00, 0x53, 0x00, 0x00, 0x00, 0x2a },
    };
    struct nlattr_fixture fixture_nl_data_ib = {
        /* valid policy and ib_addr length */
        .nlattr.nla_len = 24,
        .nlattr.nla_type = TEST_POLICY_ATTR,
        .data = { 0x00, 0x00, 0x00, 0x67, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
                  0x00, 0x00, 0xe4, 0x1d, 0x2d, 0x03, 0x00, 0xa5, 0xf0, 0x2f },
    };
    struct nlattr_fixture fixture_nl_data_invalid = {
        /* valid policy but data neither eth_addr nor ib_addr */
        .nlattr.nla_len = 11,
        .nlattr.nla_type = TEST_POLICY_ATTR,
        .data = { 0x00, 0x53, 0x00, 0x00, 0x00, 0x2a, 0x00 },
    };
    struct ofpbuf *buf;

    /* confirm policy fails with too short data */
    buf = ofpbuf_clone_data(&fixture_nl_data_policy_short,
                            fixture_nl_data_policy_short.nlattr.nla_len);
    ovs_assert(!nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy)));
    ofpbuf_delete(buf);
    memset(&attrs, 0, sizeof *attrs);

    /* confirm policy fails with too long data */
    buf = ofpbuf_clone_data(&fixture_nl_data_policy_long,
                            fixture_nl_data_policy_long.nlattr.nla_len);
    ovs_assert(!nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy)));
    ofpbuf_delete(buf);
    memset(&attrs, 0, sizeof *attrs);

    /* confirm policy passes and interpret valid ethernet lladdr */
    buf = ofpbuf_clone_data(&fixture_nl_data_eth,
                            fixture_nl_data_eth.nlattr.nla_len);
    ovs_assert(nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy)));
    ovs_assert((_nla_len(attrs[42]) == sizeof(struct eth_addr)));
    struct eth_addr eth_expect = ETH_ADDR_C(00,53,00,00,00,2a);
    struct eth_addr eth_parsed = nl_attr_get_eth_addr(attrs[42]);
    ovs_assert((!memcmp(&eth_expect, &eth_parsed, sizeof(struct eth_addr))));
    ofpbuf_delete(buf);
    memset(&attrs, 0, sizeof *attrs);

    /* confirm policy passes and interpret valid infiniband lladdr */
    buf = ofpbuf_clone_data(&fixture_nl_data_ib,
                            fixture_nl_data_ib.nlattr.nla_len);
    ovs_assert(nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy)));
    ovs_assert((_nla_len(attrs[42]) == sizeof(struct ib_addr)));
    struct ib_addr ib_expect = {
            .ia = {
                0x00, 0x00, 0x00, 0x67, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0xe4, 0x1d, 0x2d, 0x03, 0x00, 0xa5, 0xf0, 0x2f,
            },
    };
    struct ib_addr ib_parsed = nl_attr_get_ib_addr(attrs[42]);
    ovs_assert((!memcmp(&ib_expect, &ib_parsed, sizeof(struct eth_addr))));
    ofpbuf_delete(buf);
    memset(&attrs, 0, sizeof *attrs);

    /* confirm we're able to detect invalid data that passes policy check, this
     * can happen because the policy defines the data to be between the
     * currently known lladdr sizes of 6 (ETH_ALEN) and 20 (INFINIBAND_ALEN) */
    buf = ofpbuf_clone_data(&fixture_nl_data_invalid,
                            fixture_nl_data_invalid.nlattr.nla_len);
    ovs_assert(nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy)));
    ovs_assert(_nla_len(attrs[42]) != sizeof(struct eth_addr)
               && _nla_len(attrs[42]) != sizeof(struct ib_addr));
    ofpbuf_delete(buf);
    memset(&attrs, 0, sizeof *attrs);
}

static const struct ovs_cmdl_command commands[] = {
    {"ll_addr", "", 0, 0, test_nl_policy_parse_ll_addr, OVS_RO},
    {NULL, NULL, 0, 0, NULL, OVS_RO},
};

static void
test_netlink_policy(int argc, char *argv[])
{
    struct ovs_cmdl_context ctx = {
        .argc = argc - 1,
        .argv = argv + 1,
    };
    ovs_set_program_name(argv[0], OVS_PACKAGE_VERSION);
    ovs_cmdl_run_command(&ctx, commands);
}

OVSTEST_REGISTER("test-netlink-policy", test_netlink_policy);