2018-10-06 07:40:00 +08:00
|
|
|
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
|
2018-01-31 04:55:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NETLINK Netlink attributes
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include "nlattr.h"
|
2019-11-10 04:37:30 +08:00
|
|
|
#include "libbpf_internal.h"
|
2018-01-31 04:55:02 +08:00
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
libbpf: Poison kernel-only integer types
It's been a recurring issue with types like u32 slipping into libbpf source
code accidentally. This is not detected during builds inside kernel source
tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
libbpf_internal.h.
This patch also fixes a new u32 occurence in libbpf.c, added recently.
Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200110181916.271446-1-andriin@fb.com
2020-01-11 02:19:16 +08:00
|
|
|
/* make sure libbpf doesn't use kernel-only integer typedefs */
|
|
|
|
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
|
|
|
|
[LIBBPF_NLA_U8] = sizeof(uint8_t),
|
|
|
|
[LIBBPF_NLA_U16] = sizeof(uint16_t),
|
|
|
|
[LIBBPF_NLA_U32] = sizeof(uint32_t),
|
|
|
|
[LIBBPF_NLA_U64] = sizeof(uint64_t),
|
|
|
|
[LIBBPF_NLA_STRING] = 1,
|
|
|
|
[LIBBPF_NLA_FLAG] = 0,
|
2018-01-31 04:55:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
|
|
|
|
{
|
|
|
|
int totlen = NLA_ALIGN(nla->nla_len);
|
|
|
|
|
|
|
|
*remaining -= totlen;
|
|
|
|
return (struct nlattr *) ((char *) nla + totlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nla_ok(const struct nlattr *nla, int remaining)
|
|
|
|
{
|
|
|
|
return remaining >= sizeof(*nla) &&
|
|
|
|
nla->nla_len >= sizeof(*nla) &&
|
|
|
|
nla->nla_len <= remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nla_type(const struct nlattr *nla)
|
|
|
|
{
|
|
|
|
return nla->nla_type & NLA_TYPE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int validate_nla(struct nlattr *nla, int maxtype,
|
2018-10-04 06:26:40 +08:00
|
|
|
struct libbpf_nla_policy *policy)
|
2018-01-31 04:55:02 +08:00
|
|
|
{
|
2018-10-04 06:26:40 +08:00
|
|
|
struct libbpf_nla_policy *pt;
|
2018-01-31 04:55:02 +08:00
|
|
|
unsigned int minlen = 0;
|
|
|
|
int type = nla_type(nla);
|
|
|
|
|
|
|
|
if (type < 0 || type > maxtype)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pt = &policy[type];
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
if (pt->type > LIBBPF_NLA_TYPE_MAX)
|
2018-01-31 04:55:02 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pt->minlen)
|
|
|
|
minlen = pt->minlen;
|
2018-10-04 06:26:40 +08:00
|
|
|
else if (pt->type != LIBBPF_NLA_UNSPEC)
|
2018-01-31 04:55:02 +08:00
|
|
|
minlen = nla_attr_minlen[pt->type];
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
if (libbpf_nla_len(nla) < minlen)
|
2018-01-31 04:55:02 +08:00
|
|
|
return -1;
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
|
2018-01-31 04:55:02 +08:00
|
|
|
return -1;
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
if (pt->type == LIBBPF_NLA_STRING) {
|
|
|
|
char *data = libbpf_nla_data(nla);
|
|
|
|
|
|
|
|
if (data[libbpf_nla_len(nla) - 1] != '\0')
|
2018-01-31 04:55:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int nlmsg_len(const struct nlmsghdr *nlh)
|
|
|
|
{
|
|
|
|
return nlh->nlmsg_len - NLMSG_HDRLEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create attribute index based on a stream of attributes.
|
|
|
|
* @arg tb Index array to be filled (maxtype+1 elements).
|
|
|
|
* @arg maxtype Maximum attribute type expected and accepted.
|
|
|
|
* @arg head Head of attribute stream.
|
|
|
|
* @arg len Length of attribute stream.
|
|
|
|
* @arg policy Attribute validation policy.
|
|
|
|
*
|
|
|
|
* Iterates over the stream of attributes and stores a pointer to each
|
|
|
|
* attribute in the index array using the attribute type as index to
|
|
|
|
* the array. Attribute with a type greater than the maximum type
|
|
|
|
* specified will be silently ignored in order to maintain backwards
|
|
|
|
* compatibility. If \a policy is not NULL, the attribute will be
|
|
|
|
* validated using the specified policy.
|
|
|
|
*
|
|
|
|
* @see nla_validate
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
2018-10-04 06:26:40 +08:00
|
|
|
int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
|
|
|
|
int len, struct libbpf_nla_policy *policy)
|
2018-01-31 04:55:02 +08:00
|
|
|
{
|
|
|
|
struct nlattr *nla;
|
|
|
|
int rem, err;
|
|
|
|
|
|
|
|
memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
libbpf_nla_for_each_attr(nla, head, len, rem) {
|
2018-01-31 04:55:02 +08:00
|
|
|
int type = nla_type(nla);
|
|
|
|
|
|
|
|
if (type > maxtype)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (policy) {
|
|
|
|
err = validate_nla(nla, maxtype, policy);
|
|
|
|
if (err < 0)
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[type])
|
2019-11-10 04:37:30 +08:00
|
|
|
pr_warn("Attribute of type %#x found multiple times in message, "
|
|
|
|
"previous attribute is being ignored.\n", type);
|
2018-01-31 04:55:02 +08:00
|
|
|
|
|
|
|
tb[type] = nla;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
errout:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
tools/bpf: add more netlink functionalities in lib/bpf
This patch added a few netlink attribute parsing functions
and the netlink API functions to query networking links, tc classes,
tc qdiscs and tc filters. For example, the following API is
to get networking links:
int nl_get_link(int sock, unsigned int nl_pid,
dump_nlmsg_t dump_link_nlmsg,
void *cookie);
Note that when the API is called, the user also provided a
callback function with the following signature:
int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
The "cookie" is the parameter the user passed to the API and will
be available for the callback function.
The "msg" is the information about the result, e.g., ifinfomsg or
tcmsg. The "tb" is the parsed netlink attributes.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 07:58:05 +08:00
|
|
|
/**
|
|
|
|
* Create attribute index based on nested attribute
|
|
|
|
* @arg tb Index array to be filled (maxtype+1 elements).
|
|
|
|
* @arg maxtype Maximum attribute type expected and accepted.
|
|
|
|
* @arg nla Nested Attribute.
|
|
|
|
* @arg policy Attribute validation policy.
|
|
|
|
*
|
|
|
|
* Feeds the stream of attributes nested into the specified attribute
|
2018-10-04 06:26:40 +08:00
|
|
|
* to libbpf_nla_parse().
|
tools/bpf: add more netlink functionalities in lib/bpf
This patch added a few netlink attribute parsing functions
and the netlink API functions to query networking links, tc classes,
tc qdiscs and tc filters. For example, the following API is
to get networking links:
int nl_get_link(int sock, unsigned int nl_pid,
dump_nlmsg_t dump_link_nlmsg,
void *cookie);
Note that when the API is called, the user also provided a
callback function with the following signature:
int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
The "cookie" is the parameter the user passed to the API and will
be available for the callback function.
The "msg" is the information about the result, e.g., ifinfomsg or
tcmsg. The "tb" is the parsed netlink attributes.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 07:58:05 +08:00
|
|
|
*
|
2018-10-04 06:26:40 +08:00
|
|
|
* @see libbpf_nla_parse
|
tools/bpf: add more netlink functionalities in lib/bpf
This patch added a few netlink attribute parsing functions
and the netlink API functions to query networking links, tc classes,
tc qdiscs and tc filters. For example, the following API is
to get networking links:
int nl_get_link(int sock, unsigned int nl_pid,
dump_nlmsg_t dump_link_nlmsg,
void *cookie);
Note that when the API is called, the user also provided a
callback function with the following signature:
int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
The "cookie" is the parameter the user passed to the API and will
be available for the callback function.
The "msg" is the information about the result, e.g., ifinfomsg or
tcmsg. The "tb" is the parsed netlink attributes.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 07:58:05 +08:00
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
2018-10-04 06:26:40 +08:00
|
|
|
int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
|
|
|
|
struct nlattr *nla,
|
|
|
|
struct libbpf_nla_policy *policy)
|
tools/bpf: add more netlink functionalities in lib/bpf
This patch added a few netlink attribute parsing functions
and the netlink API functions to query networking links, tc classes,
tc qdiscs and tc filters. For example, the following API is
to get networking links:
int nl_get_link(int sock, unsigned int nl_pid,
dump_nlmsg_t dump_link_nlmsg,
void *cookie);
Note that when the API is called, the user also provided a
callback function with the following signature:
int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
The "cookie" is the parameter the user passed to the API and will
be available for the callback function.
The "msg" is the information about the result, e.g., ifinfomsg or
tcmsg. The "tb" is the parsed netlink attributes.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 07:58:05 +08:00
|
|
|
{
|
2018-10-04 06:26:40 +08:00
|
|
|
return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
|
|
|
|
libbpf_nla_len(nla), policy);
|
tools/bpf: add more netlink functionalities in lib/bpf
This patch added a few netlink attribute parsing functions
and the netlink API functions to query networking links, tc classes,
tc qdiscs and tc filters. For example, the following API is
to get networking links:
int nl_get_link(int sock, unsigned int nl_pid,
dump_nlmsg_t dump_link_nlmsg,
void *cookie);
Note that when the API is called, the user also provided a
callback function with the following signature:
int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
The "cookie" is the parameter the user passed to the API and will
be available for the callback function.
The "msg" is the information about the result, e.g., ifinfomsg or
tcmsg. The "tb" is the parsed netlink attributes.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 07:58:05 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 04:55:02 +08:00
|
|
|
/* dump netlink extended ack error message */
|
2018-10-04 06:26:40 +08:00
|
|
|
int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
|
2018-01-31 04:55:02 +08:00
|
|
|
{
|
2018-10-04 06:26:40 +08:00
|
|
|
struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
|
|
|
|
[NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING },
|
|
|
|
[NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 },
|
2018-01-31 04:55:02 +08:00
|
|
|
};
|
|
|
|
struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
|
|
|
|
struct nlmsgerr *err;
|
|
|
|
char *errmsg = NULL;
|
|
|
|
int hlen, alen;
|
|
|
|
|
|
|
|
/* no TLVs, nothing to do here */
|
|
|
|
if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = (struct nlmsgerr *)NLMSG_DATA(nlh);
|
|
|
|
hlen = sizeof(*err);
|
|
|
|
|
|
|
|
/* if NLM_F_CAPPED is set then the inner err msg was capped */
|
|
|
|
if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
|
|
|
|
hlen += nlmsg_len(&err->msg);
|
|
|
|
|
|
|
|
attr = (struct nlattr *) ((void *) err + hlen);
|
|
|
|
alen = nlh->nlmsg_len - hlen;
|
|
|
|
|
2018-10-04 06:26:40 +08:00
|
|
|
if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
|
|
|
|
extack_policy) != 0) {
|
2019-11-10 04:37:30 +08:00
|
|
|
pr_warn("Failed to parse extended error attributes\n");
|
2018-01-31 04:55:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[NLMSGERR_ATTR_MSG])
|
2018-10-04 06:26:40 +08:00
|
|
|
errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
|
2018-01-31 04:55:02 +08:00
|
|
|
|
2019-11-10 04:37:30 +08:00
|
|
|
pr_warn("Kernel error message: %s\n", errmsg);
|
2018-01-31 04:55:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|