@@ -168,6 +168,14 @@ struct eth_addr {
};
};
+/* Similar to struct eth_addr, for Infiniband LL addresses. */
+struct ib_addr {
+ union {
+ uint8_t ia[20];
+ ovs_be16 be16[10];
+ };
+};
+
/* Ethernet address constant, e.g. ETH_ADDR_C(01,23,45,67,89,ab) is
* 01:23:45:67:89:ab. */
#define ETH_ADDR_C(A,B,C,D,E,F) \
@@ -741,6 +741,20 @@ nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla));
}
+/* Returns the Ethernet Address value in 'nla''s payload. */
+struct eth_addr
+nl_attr_get_eth_addr(const struct nlattr *nla)
+{
+ return NL_ATTR_GET_AS(nla, struct eth_addr);
+}
+
+/* Returns the Infiniband LL Address value in 'nla''s payload. */
+struct ib_addr
+nl_attr_get_ib_addr(const struct nlattr *nla)
+{
+ return NL_ATTR_GET_AS(nla, struct ib_addr);
+}
+
/* Default minimum payload size for each type of attribute. */
static size_t
min_attr_len(enum nl_attr_type type)
@@ -757,6 +771,7 @@ min_attr_len(enum nl_attr_type type)
case NL_A_FLAG: return 0;
case NL_A_IPV6: return 16;
case NL_A_NESTED: return 0;
+ case NL_A_LL_ADDR: return 6; /* ETH_ALEN */
case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
}
}
@@ -777,6 +792,7 @@ max_attr_len(enum nl_attr_type type)
case NL_A_FLAG: return SIZE_MAX;
case NL_A_IPV6: return 16;
case NL_A_NESTED: return SIZE_MAX;
+ case NL_A_LL_ADDR: return 20; /* INFINIBAND_ALEN */
case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
}
}
@@ -126,6 +126,8 @@ struct nlmsghdr *nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg);
#define NL_A_BE128_SIZE NL_ATTR_SIZE(sizeof(ovs_be128))
#define NL_A_FLAG_SIZE NL_ATTR_SIZE(0)
#define NL_A_IPV6_SIZE NL_ATTR_SIZE(sizeof(struct in6_addr))
+#define NL_A_LL_ADDR_ETH_SIZE NL_ATTR_SIZE(sizeof(struct eth_addr))
+#define NL_A_LL_ADDR_IB_SIZE NL_ATTR_SIZE(sizeof(struct ib_addr))
bool nl_attr_oversized(size_t payload_size);
@@ -147,6 +149,7 @@ enum nl_attr_type
NL_A_FLAG,
NL_A_IPV6,
NL_A_NESTED,
+ NL_A_LL_ADDR,
N_NL_ATTR_TYPES
};
@@ -214,6 +217,8 @@ struct in6_addr nl_attr_get_in6_addr(const struct nlattr *nla);
odp_port_t nl_attr_get_odp_port(const struct nlattr *);
const char *nl_attr_get_string(const struct nlattr *);
void nl_attr_get_nested(const struct nlattr *, struct ofpbuf *);
+struct eth_addr nl_attr_get_eth_addr(const struct nlattr *nla);
+struct ib_addr nl_attr_get_ib_addr(const struct nlattr *nla);
/* Netlink attribute policy.
*
@@ -498,7 +498,8 @@ endif
if LINUX
tests_ovstest_SOURCES += \
- tests/test-netlink-conntrack.c
+ tests/test-netlink-conntrack.c \
+ tests/test-netlink-policy.c
endif
tests_ovstest_LDADD = lib/libopenvswitch.la
@@ -254,3 +254,7 @@ AT_SETUP([stopwatch module])
AT_CHECK([ovstest test-stopwatch], [0], [......
], [ignore])
AT_CLEANUP
+
+AT_SETUP([netlink policy])
+AT_CHECK([ovstest test-netlink-policy ll_addr], [0])
+AT_CLEANUP
new file mode 100644
@@ -0,0 +1,136 @@
+/*
+ * 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"
+
+/* 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;
+}
+
+static void
+test_nl_policy_parse_ll_addr(struct ovs_cmdl_context *ctx OVS_UNUSED) {
+ struct nl_policy policy[] = {
+ [42] = { .type = NL_A_LL_ADDR,
+ .optional = false, },
+ };
+ struct nlattr *attrs[ARRAY_SIZE(policy)];
+ uint8_t fixture_nl_data_policy_short[] = {
+ /* too short according to policy */
+ 0x04, 0x00, 0x2a, 0x00,
+ };
+ uint8_t fixture_nl_data_policy_long[] = {
+ /* too long according to policy */
+ 0x19, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x67, 0xfe, 0x80, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0xe4, 0x1d, 0x2d, 0x03, 0x00, 0xa5, 0xf0, 0x2f, 0x00,
+ 0x00,
+ };
+ uint8_t fixture_nl_data_eth[] = {
+ /* valid policy and eth_addr length */
+ 0x0a, 0x00, 0x2a, 0x00, 0x00, 0x53, 0x00, 0x00, 0x00, 0x2a,
+ };
+ uint8_t fixture_nl_data_ib[] = {
+ /* valid policy and ib_addr length */
+ 0x18, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x67, 0xfe, 0x80, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0xe4, 0x1d, 0x2d, 0x03, 0x00, 0xa5, 0xf0, 0x2f,
+ };
+ uint8_t fixture_nl_data_invalid[] = {
+ /* valid policy but data neither eth_addr nor ib_addr */
+ 0x0b, 0x00, 0x2a, 0x00, 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,
+ sizeof(fixture_nl_data_policy_short));
+ 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,
+ sizeof(fixture_nl_data_policy_long));
+ 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,
+ sizeof(fixture_nl_data_eth));
+ 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(ð_expect, ð_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,
+ sizeof(fixture_nl_data_ib));
+ 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,
+ sizeof(fixture_nl_data_invalid));
+ 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);
Data retrieved from netlink and friends may include link layer address. Add type to nl_attr_type and min/max functions to allow use of nl_policy_parse with this type of data. While this will not be used by Open vSwitch itself at this time, sibling and derived projects want to use the great netlink library that OVS provides, and it is not possible to safely override the global nl_attr_type symbol at link time. Signed-off-by: Frode Nordahl <frode.nordahl@canonical.com> --- include/openvswitch/types.h | 8 +++ lib/netlink.c | 16 +++++ lib/netlink.h | 5 ++ tests/automake.mk | 3 +- tests/library.at | 4 ++ tests/test-netlink-policy.c | 136 ++++++++++++++++++++++++++++++++++++ 6 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 tests/test-netlink-policy.c