Path: blob/main/sys/netlink/netlink_message_parser.h
39475 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2022 Alexander V. Chernikov <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef _NETLINK_NETLINK_MESSAGE_PARSER_H_28#define _NETLINK_NETLINK_MESSAGE_PARSER_H_2930#ifdef _KERNEL3132#include <sys/bitset.h>3334/*35* It is not meant to be included directly36*/3738/* Parsing state */39struct linear_buffer {40char *base; /* Base allocated memory pointer */41uint32_t offset; /* Currently used offset */42uint32_t size; /* Total buffer size */43} __aligned(_Alignof(__max_align_t));4445static inline void *46lb_alloc(struct linear_buffer *lb, int len)47{48len = roundup2(len, _Alignof(__max_align_t));49if (lb->offset + len > lb->size)50return (NULL);51void *data = (void *)(lb->base + lb->offset);52lb->offset += len;53return (data);54}5556static inline void57lb_clear(struct linear_buffer *lb)58{59memset(lb->base, 0, lb->size);60lb->offset = 0;61}6263#define NL_MAX_ERROR_BUF 12864#define SCRATCH_BUFFER_SIZE (1024 + NL_MAX_ERROR_BUF)65struct nl_pstate {66struct linear_buffer lb; /* Per-message scratch buffer */67struct nlpcb *nlp; /* Originator socket */68struct nl_writer *nw; /* Message writer to use */69struct nlmsghdr *hdr; /* Current parsed message header */70uint32_t err_off; /* error offset from hdr start */71int error; /* last operation error */72char *err_msg; /* Description of last error */73struct nlattr *cookie; /* NLA to return to the userspace */74bool strict; /* Strict parsing required */75};7677static inline void *78npt_alloc(struct nl_pstate *npt, int len)79{80return (lb_alloc(&npt->lb, len));81}82#define npt_alloc_sockaddr(_npt, _len) \83((struct sockaddr *)(npt_alloc((_npt), (_len))))8485typedef int parse_field_f(void *hdr, struct nl_pstate *npt, void *target);86struct nlfield_parser {87uint16_t off_in;88uint16_t off_out;89parse_field_f *cb;90};91static const struct nlfield_parser nlf_p_empty[] = {};9293int nlf_get_ifp(void *src, struct nl_pstate *npt, void *target);94int nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target);95int nlf_get_u8(void *src, struct nl_pstate *npt, void *target);96int nlf_get_u16(void *src, struct nl_pstate *npt, void *target);97int nlf_get_u32(void *src, struct nl_pstate *npt, void *target);98int nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target);99100struct nlattr_parser;101typedef int parse_attr_f(struct nlattr *attr, struct nl_pstate *npt,102const void *arg, void *target);103struct nlattr_parser {104uint16_t type; /* Attribute type */105uint16_t off; /* field offset in the target structure */106parse_attr_f *cb; /* parser function to call */107const void *arg;108};109110typedef bool strict_parser_f(void *hdr, struct nl_pstate *npt);111typedef bool post_parser_f(void *parsed_attrs, struct nl_pstate *npt);112113struct nlhdr_parser {114u_int nl_hdr_off; /* aligned netlink header size */115u_int out_hdr_off; /* target header size */116u_int fp_size;117u_int np_size;118const struct nlfield_parser *fp; /* array of header field parsers */119const struct nlattr_parser *np; /* array of attribute parsers */120strict_parser_f *sp; /* Pre-parse strict validation function */121post_parser_f *post_parse;122};123124#define NL_DECLARE_PARSER_EXT(_name, _t, _sp, _fp, _np, _pp) \125static const struct nlhdr_parser _name = { \126.nl_hdr_off = sizeof(_t), \127.fp = &((_fp)[0]), \128.np = &((_np)[0]), \129.fp_size = nitems(_fp), \130.np_size = nitems(_np), \131.sp = _sp, \132.post_parse = _pp, \133}134135#define NL_DECLARE_PARSER(_name, _t, _fp, _np) \136NL_DECLARE_PARSER_EXT(_name, _t, NULL, _fp, _np, NULL)137138#define NL_DECLARE_STRICT_PARSER(_name, _t, _sp, _fp, _np) \139NL_DECLARE_PARSER_EXT(_name, _t, _sp, _fp, _np, NULL)140141#define NL_DECLARE_ARR_PARSER(_name, _t, _o, _fp, _np) \142static const struct nlhdr_parser _name = { \143.nl_hdr_off = sizeof(_t), \144.out_hdr_off = sizeof(_o), \145.fp = &((_fp)[0]), \146.np = &((_np)[0]), \147.fp_size = nitems(_fp), \148.np_size = nitems(_np), \149}150151#define NL_DECLARE_ATTR_PARSER_EXT(_name, _np, _pp) \152static const struct nlhdr_parser _name = { \153.np = &((_np)[0]), \154.np_size = nitems(_np), \155.post_parse = (_pp) \156}157158#define NL_DECLARE_ATTR_PARSER(_name, _np) \159NL_DECLARE_ATTR_PARSER_EXT(_name, _np, NULL)160161#define NL_ATTR_BMASK_SIZE 128162BITSET_DEFINE(nlattr_bmask, NL_ATTR_BMASK_SIZE);163164void nl_get_attrs_bmask_raw(struct nlattr *nla_head, uint32_t len,165struct nlattr_bmask *bm);166bool nl_has_attr(const struct nlattr_bmask *bm, uint16_t nla_type);167168int nl_parse_attrs_raw(struct nlattr *nla_head, uint16_t len,169const struct nlattr_parser *ps, u_int pslen, struct nl_pstate *npt,170void *target);171172int nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt,173const void *arg, void *target);174int nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt,175const void *arg, void *target);176int nlattr_get_bool(struct nlattr *nla, struct nl_pstate *npt,177const void *arg, void *target);178int nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt,179const void *arg, void *target);180int nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt,181const void *arg, void *target);182int nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt,183const void *arg, void *target);184int nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt,185const void *arg, void *target);186int nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt,187const void *arg, void *target);188int nlattr_get_in6_addr(struct nlattr *nla, struct nl_pstate *npt,189const void *arg, void *target);190int nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt,191const void *arg, void *target);192int nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt,193const void *arg, void *target);194int nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt,195const void *arg, void *target);196int nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt,197const void *arg, void *target);198int nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt,199const void *arg, void *target);200int nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt,201const void *arg, void *target);202int nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt,203const void *arg, void *target);204int nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt,205const void *arg, void *target);206int nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt,207const void *arg, void *target);208int nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt,209const void *arg, void *target);210211bool nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...)212__printflike(2, 3);213214#define NLMSG_REPORT_ERR_MSG(_npt, _fmt, ...) { \215nlmsg_report_err_msg(_npt, _fmt, ## __VA_ARGS__); \216NLP_LOG(LOG_DEBUG, (_npt)->nlp, _fmt, ## __VA_ARGS__); \217}218219bool nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off);220221void nlmsg_report_cookie(struct nl_pstate *npt, struct nlattr *nla);222void nlmsg_report_cookie_u32(struct nl_pstate *npt, uint32_t val);223224/*225* Have it inline so compiler can optimize field accesses into226* the list of direct function calls without iteration.227*/228static inline int229nl_parse_header(void *hdr, uint32_t len, const struct nlhdr_parser *parser,230struct nl_pstate *npt, void *target)231{232int error;233234if (__predict_false(len < parser->nl_hdr_off)) {235void *tmp_hdr;236237if (npt->strict) {238nlmsg_report_err_msg(npt,239"header too short: expected %d, got %d",240parser->nl_hdr_off, len);241return (EINVAL);242}243244/*245* Compatibility with older applications:246* pretend there's a full header.247*/248tmp_hdr = npt_alloc(npt, parser->nl_hdr_off);249if (tmp_hdr == NULL)250return (EINVAL);251memcpy(tmp_hdr, hdr, len);252hdr = tmp_hdr;253len = parser->nl_hdr_off;254}255256if (npt->strict && parser->sp != NULL && !parser->sp(hdr, npt))257return (EINVAL);258259/* Extract fields first */260for (u_int i = 0; i < parser->fp_size; i++) {261const struct nlfield_parser *fp = &parser->fp[i];262void *src = (char *)hdr + fp->off_in;263void *dst = (char *)target + fp->off_out;264265error = fp->cb(src, npt, dst);266if (error != 0)267return (error);268}269270error = nl_parse_attrs_raw(271(struct nlattr *)((char *)hdr + parser->nl_hdr_off),272len - parser->nl_hdr_off, parser->np, parser->np_size, npt, target);273274if (parser->post_parse != NULL && error == 0) {275if (!parser->post_parse(target, npt))276return (EINVAL);277}278279return (error);280}281282static inline int283nl_parse_nested(struct nlattr *nla, const struct nlhdr_parser *parser,284struct nl_pstate *npt, void *target)285{286return (nl_parse_attrs_raw((struct nlattr *)NLA_DATA(nla),287NLA_DATA_LEN(nla), parser->np, parser->np_size, npt, target));288}289290/*291* Checks that attributes are sorted by attribute type.292*/293static inline void294nl_verify_parsers(const struct nlhdr_parser **parser, int count)295{296#ifdef INVARIANTS297for (int i = 0; i < count; i++) {298const struct nlhdr_parser *p = parser[i];299int attr_type = 0;300for (int j = 0; j < p->np_size; j++) {301MPASS(p->np[j].type > attr_type);302attr_type = p->np[j].type;303304/* Recurse into nested objects. */305if (p->np[j].cb == nlattr_get_nested ||306p->np[j].cb == nlattr_get_nested_ptr) {307const struct nlhdr_parser *np =308(const struct nlhdr_parser *)p->np[j].arg;309nl_verify_parsers(&np, 1);310}311}312}313#endif314}315void nl_verify_parsers(const struct nlhdr_parser **parser, int count);316#define NL_VERIFY_PARSERS(_p) nl_verify_parsers((_p), nitems(_p))317318static inline int319nl_parse_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,320struct nl_pstate *npt, void *target)321{322return (nl_parse_header(hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser,323npt, target));324}325326static inline void327nl_get_attrs_bmask_nlmsg(struct nlmsghdr *hdr,328const struct nlhdr_parser *parser, struct nlattr_bmask *bm)329{330nl_get_attrs_bmask_raw(331(struct nlattr *)((char *)(hdr + 1) + parser->nl_hdr_off),332hdr->nlmsg_len - sizeof(*hdr) - parser->nl_hdr_off, bm);333}334335#endif336#endif337338339