aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGleb Smirnoff <glebius@FreeBSD.org>2025-01-29 16:31:04 +0000
committerGleb Smirnoff <glebius@FreeBSD.org>2025-01-29 23:48:55 +0000
commit8b094a9801edcd4010d3fb8dc85cf2cec598abfc (patch)
treeb6245310279b23e9fee84f77a10214c68361600c
parent0feaf865ce8e333f834177a5656e57b5e32ad5cf (diff)
netlink: style(9) pass over message parsing code
Mostly breaking long lines, few other changes.
-rw-r--r--sys/netlink/netlink_message_parser.c113
-rw-r--r--sys/netlink/netlink_message_parser.h77
2 files changed, 114 insertions, 76 deletions
diff --git a/sys/netlink/netlink_message_parser.c b/sys/netlink/netlink_message_parser.c
index eafc2d83cbfb..368cb43cf496 100644
--- a/sys/netlink/netlink_message_parser.c
+++ b/sys/netlink/netlink_message_parser.c
@@ -122,8 +122,9 @@ search_states(const struct nlattr_parser *ps, int pslen, int key)
}
int
-nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps, int pslen,
- struct nl_pstate *npt, void *target)
+nl_parse_attrs_raw(struct nlattr *nla_head, int len,
+ const struct nlattr_parser *ps, int pslen, struct nl_pstate *npt,
+ void *target)
{
struct nlattr *nla = NULL;
int error = 0;
@@ -131,9 +132,11 @@ nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser
NL_LOG(LOG_DEBUG3, "parse %p remaining_len %d", nla_head, len);
int orig_len = len;
NLA_FOREACH(nla, nla_head, len) {
- NL_LOG(LOG_DEBUG3, ">> parsing %p attr_type %d len %d (rem %d)", nla, nla->nla_type, nla->nla_len, len);
+ NL_LOG(LOG_DEBUG3, ">> parsing %p attr_type %d len %d (rem %d)",
+ nla, nla->nla_type, nla->nla_len, len);
if (nla->nla_len < sizeof(struct nlattr)) {
- NLMSG_REPORT_ERR_MSG(npt, "Invalid attr %p type %d len: %d",
+ NLMSG_REPORT_ERR_MSG(npt,
+ "Invalid attr %p type %d len: %d",
nla, nla->nla_type, nla->nla_len);
uint32_t off = (char *)nla - (char *)npt->hdr;
nlmsg_report_err_offset(npt, off);
@@ -148,7 +151,8 @@ nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser
if (error != 0) {
uint32_t off = (char *)nla - (char *)npt->hdr;
nlmsg_report_err_offset(npt, off);
- NL_LOG(LOG_DEBUG3, "parse failed at offset %u", off);
+ NL_LOG(LOG_DEBUG3,
+ "parse failed at offset %u", off);
return (error);
}
} else {
@@ -167,7 +171,8 @@ nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser
}
void
-nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm)
+nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len,
+ struct nlattr_bmask *bm)
{
struct nlattr *nla = NULL;
@@ -180,7 +185,8 @@ nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm
if (nla_type < NL_ATTR_BMASK_SIZE)
BIT_SET(NL_ATTR_BMASK_SIZE, nla_type, bm);
else
- NL_LOG(LOG_DEBUG2, "Skipping type %d in the mask: too short",
+ NL_LOG(LOG_DEBUG2,
+ "Skipping type %d in the mask: too short",
nla_type);
}
}
@@ -194,7 +200,8 @@ nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type)
}
int
-nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != 0)) {
NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not a flag",
@@ -211,7 +218,8 @@ parse_rta_ip4(void *rta_data, struct nl_pstate *npt, int *perror)
{
struct sockaddr_in *sin;
- sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in));
+ sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt,
+ sizeof(struct sockaddr_in));
if (__predict_false(sin == NULL)) {
*perror = ENOBUFS;
return (NULL);
@@ -227,7 +235,8 @@ parse_rta_ip6(void *rta_data, struct nl_pstate *npt, int *perror)
{
struct sockaddr_in6 *sin6;
- sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in6));
+ sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt,
+ sizeof(struct sockaddr_in6));
if (__predict_false(sin6 == NULL)) {
*perror = ENOBUFS;
return (NULL);
@@ -258,7 +267,8 @@ parse_rta_ip(struct rtattr *rta, struct nl_pstate *npt, int *perror)
}
int
-nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
int error = 0;
@@ -302,7 +312,8 @@ parse_rta_via(struct rtattr *rta, struct nl_pstate *npt, int *perror)
}
int
-nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
int error = 0;
@@ -313,7 +324,8 @@ nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, voi
}
int
-nlattr_get_bool(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_bool(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(bool))) {
NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not bool",
@@ -325,7 +337,8 @@ nlattr_get_bool(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void
}
int
-nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint8_t))) {
NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint8",
@@ -337,7 +350,8 @@ nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt, const void *arg, voi
}
int
-nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint16_t))) {
NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint16",
@@ -349,7 +363,8 @@ nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, vo
}
int
-nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
@@ -361,7 +376,8 @@ nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, vo
}
int
-nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint64_t))) {
NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint64",
@@ -373,10 +389,12 @@ nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, vo
}
int
-nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(in_addr_t))) {
- NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not in_addr_t",
+ NLMSG_REPORT_ERR_MSG(npt,
+ "nla type %d size(%u) is not in_addr_t",
nla->nla_type, NLA_DATA_LEN(nla));
return (EINVAL);
}
@@ -385,10 +403,12 @@ nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, v
}
int
-nlattr_get_in6_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_in6_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct in6_addr))) {
- NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not struct in6_addr",
+ NLMSG_REPORT_ERR_MSG(npt,
+ "nla type %d size(%u) is not struct in6_addr",
nla->nla_type, NLA_DATA_LEN(nla));
return (EINVAL);
}
@@ -428,26 +448,31 @@ nlattr_get_ifp_internal(struct nlattr *nla, struct nl_pstate *npt,
}
int
-nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
return (nlattr_get_ifp_internal(nla, npt, target, false));
}
int
-nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
return (nlattr_get_ifp_internal(nla, npt, target, true));
}
int
-nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
int maxlen = NLA_DATA_LEN(nla);
int target_size = (size_t)arg;
int len = strnlen((char *)NLA_DATA(nla), maxlen);
- if (__predict_false(len >= maxlen) || __predict_false(len >= target_size)) {
- NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated or longer than %u",
+ if (__predict_false(len >= maxlen) ||
+ __predict_false(len >= target_size)) {
+ NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not "
+ "NULL-terminated or longer than %u",
nla->nla_type, maxlen, target_size);
return (EINVAL);
}
@@ -457,12 +482,14 @@ nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt, const void *arg, voi
}
int
-nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
int maxlen = NLA_DATA_LEN(nla);
if (__predict_false(strnlen((char *)NLA_DATA(nla), maxlen) >= maxlen)) {
- NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated",
+ NLMSG_REPORT_ERR_MSG(npt,
+ "nla type %d size(%u) is not NULL-terminated",
nla->nla_type, maxlen);
return (EINVAL);
}
@@ -472,7 +499,8 @@ nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, vo
}
int
-nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
int maxlen = NLA_DATA_LEN(nla);
@@ -487,7 +515,8 @@ nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, v
}
int
-nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
size_t size = (size_t)arg;
@@ -500,7 +529,8 @@ nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt, const void *arg, voi
}
int
-nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
NL_LOG(LOG_DEBUG3, "STORING %p len %d", nla, nla->nla_len);
*((struct nlattr **)target) = nla;
@@ -508,25 +538,25 @@ nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void
}
int
-nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg,
+ void *target)
{
const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
- int error;
- /* Assumes target points to the beginning of the structure */
- error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, target);
- return (error);
+ /* Assumes target points to the beginning of the structure. */
+ return (nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt,
+ target));
}
int
-nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
+nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt,
+ const void *arg, void *target)
{
const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
- int error;
- /* Assumes target points to the beginning of the structure */
- error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, *(void **)target);
- return (error);
+ /* Assumes target points to the beginning of the structure. */
+ return (nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt,
+ *(void **)target));
}
int
@@ -593,4 +623,3 @@ nlf_get_u32(void *src, struct nl_pstate *npt, void *target)
*((uint32_t *)target) = *((const uint32_t *)src);
return (0);
}
-
diff --git a/sys/netlink/netlink_message_parser.h b/sys/netlink/netlink_message_parser.h
index 42a59f45b22a..13d52d80ecdc 100644
--- a/sys/netlink/netlink_message_parser.h
+++ b/sys/netlink/netlink_message_parser.h
@@ -64,15 +64,15 @@ lb_clear(struct linear_buffer *lb)
#define NL_MAX_ERROR_BUF 128
#define SCRATCH_BUFFER_SIZE (1024 + NL_MAX_ERROR_BUF)
struct nl_pstate {
- struct linear_buffer lb; /* Per-message scratch buffer */
- struct nlpcb *nlp; /* Originator socket */
- struct nl_writer *nw; /* Message writer to use */
- struct nlmsghdr *hdr; /* Current parsed message header */
- uint32_t err_off; /* error offset from hdr start */
- int error; /* last operation error */
- char *err_msg; /* Description of last error */
- struct nlattr *cookie; /* NLA to return to the userspace */
- bool strict; /* Strict parsing required */
+ struct linear_buffer lb; /* Per-message scratch buffer */
+ struct nlpcb *nlp; /* Originator socket */
+ struct nl_writer *nw; /* Message writer to use */
+ struct nlmsghdr *hdr; /* Current parsed message header */
+ uint32_t err_off; /* error offset from hdr start */
+ int error; /* last operation error */
+ char *err_msg; /* Description of last error */
+ struct nlattr *cookie; /* NLA to return to the userspace */
+ bool strict; /* Strict parsing required */
};
static inline void *
@@ -80,10 +80,10 @@ npt_alloc(struct nl_pstate *npt, int len)
{
return (lb_alloc(&npt->lb, len));
}
-#define npt_alloc_sockaddr(_npt, _len) ((struct sockaddr *)(npt_alloc(_npt, _len)))
+#define npt_alloc_sockaddr(_npt, _len) \
+ ((struct sockaddr *)(npt_alloc((_npt), (_len))))
-typedef int parse_field_f(void *hdr, struct nl_pstate *npt,
- void *target);
+typedef int parse_field_f(void *hdr, struct nl_pstate *npt, void *target);
struct nlfield_parser {
uint16_t off_in;
uint16_t off_out;
@@ -98,29 +98,28 @@ int nlf_get_u16(void *src, struct nl_pstate *npt, void *target);
int nlf_get_u32(void *src, struct nl_pstate *npt, void *target);
int nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target);
-
struct nlattr_parser;
typedef int parse_attr_f(struct nlattr *attr, struct nl_pstate *npt,
const void *arg, void *target);
struct nlattr_parser {
- uint16_t type; /* Attribute type */
- uint16_t off; /* field offset in the target structure */
- parse_attr_f *cb; /* parser function to call */
- const void *arg;
+ uint16_t type; /* Attribute type */
+ uint16_t off; /* field offset in the target structure */
+ parse_attr_f *cb; /* parser function to call */
+ const void *arg;
};
typedef bool strict_parser_f(void *hdr, struct nl_pstate *npt);
typedef bool post_parser_f(void *parsed_attrs, struct nl_pstate *npt);
struct nlhdr_parser {
- int nl_hdr_off; /* aligned netlink header size */
- int out_hdr_off; /* target header size */
- int fp_size;
- int np_size;
+ int nl_hdr_off; /* aligned netlink header size */
+ int out_hdr_off; /* target header size */
+ int fp_size;
+ int np_size;
const struct nlfield_parser *fp; /* array of header field parsers */
const struct nlattr_parser *np; /* array of attribute parsers */
- strict_parser_f *sp; /* Pre-parse strict validation function */
- post_parser_f *post_parse;
+ strict_parser_f *sp; /* Pre-parse strict validation function */
+ post_parser_f *post_parse;
};
#define NL_DECLARE_PARSER_EXT(_name, _t, _sp, _fp, _np, _pp) \
@@ -163,11 +162,13 @@ static const struct nlhdr_parser _name = { \
#define NL_ATTR_BMASK_SIZE 128
BITSET_DEFINE(nlattr_bmask, NL_ATTR_BMASK_SIZE);
-void nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm);
+void nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len,
+ struct nlattr_bmask *bm);
bool nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type);
-int nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps,
- int pslen, struct nl_pstate *npt, void *target);
+int nl_parse_attrs_raw(struct nlattr *nla_head, int len,
+ const struct nlattr_parser *ps, int pslen, struct nl_pstate *npt,
+ void *target);
int nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt,
const void *arg, void *target);
@@ -228,17 +229,24 @@ static inline int
nl_parse_header(void *hdr, int len, const struct nlhdr_parser *parser,
struct nl_pstate *npt, void *target)
{
+ struct nlattr *nla;
int error;
if (__predict_false(len < parser->nl_hdr_off)) {
+ void *tmp_hdr;
+
if (npt->strict) {
- nlmsg_report_err_msg(npt, "header too short: expected %d, got %d",
+ nlmsg_report_err_msg(npt,
+ "header too short: expected %d, got %d",
parser->nl_hdr_off, len);
return (EINVAL);
}
- /* Compat with older applications: pretend there's a full header */
- void *tmp_hdr = npt_alloc(npt, parser->nl_hdr_off);
+ /*
+ * Compatibility with older applications:
+ * pretend there's a full header.
+ */
+ tmp_hdr = npt_alloc(npt, parser->nl_hdr_off);
if (tmp_hdr == NULL)
return (EINVAL);
memcpy(tmp_hdr, hdr, len);
@@ -260,8 +268,8 @@ nl_parse_header(void *hdr, int len, const struct nlhdr_parser *parser,
return (error);
}
- struct nlattr *nla_head = (struct nlattr *)((char *)hdr + parser->nl_hdr_off);
- error = nl_parse_attrs_raw(nla_head, len - parser->nl_hdr_off, parser->np,
+ nla = (struct nlattr *)((char *)hdr + parser->nl_hdr_off);
+ error = nl_parse_attrs_raw(nla, len - parser->nl_hdr_off, parser->np,
parser->np_size, npt, target);
if (parser->post_parse != NULL && error == 0) {
@@ -314,12 +322,13 @@ static inline int
nl_parse_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
struct nl_pstate *npt, void *target)
{
- return (nl_parse_header(hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, npt, target));
+ return (nl_parse_header(hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser,
+ npt, target));
}
static inline void
-nl_get_attrs_bmask_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
- struct nlattr_bmask *bm)
+nl_get_attrs_bmask_nlmsg(struct nlmsghdr *hdr,
+ const struct nlhdr_parser *parser, struct nlattr_bmask *bm)
{
struct nlattr *nla_head;