Changeset View
Changeset View
Standalone View
Standalone View
sys/netlink/netlink_message_parser.h
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (c) 2022 Alexander V. Chernikov | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
#ifndef _NETLINK_NETLINK_MESSAGE_PARSER_H_ | |||||
#define _NETLINK_NETLINK_MESSAGE_PARSER_H_ | |||||
/* | |||||
* It is not meant to be included directly | |||||
*/ | |||||
struct netlink_parse_tracker; | |||||
typedef int parse_field_f(void *hdr, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
struct nlfield_parser { | |||||
uint16_t off_in; | |||||
uint16_t off_out; | |||||
parse_field_f *cb; | |||||
}; | |||||
int nlf_get_ifp(void *src, struct netlink_parse_tracker *npt, void *target); | |||||
int nlf_get_ifpz(void *src, struct netlink_parse_tracker *npt, void *target); | |||||
int nlf_get_u8(void *src, struct netlink_parse_tracker *npt, void *target); | |||||
typedef int parse_attr_f(struct nlattr *attr, struct netlink_parse_tracker *npt, | |||||
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 */ | |||||
}; | |||||
struct nlhdr_parser { | |||||
int hdr_off; /* aligned header size */ | |||||
int fp_size; | |||||
int np_size; | |||||
struct nlfield_parser *fp; /* array of header field parsers */ | |||||
struct nlattr_parser *np; /* array of attribute parsers */ | |||||
}; | |||||
int nl_parse_attrs_raw(struct nlattr *nla_head, int len, struct nlattr_parser *ps, | |||||
int pslen, struct netlink_parse_tracker *npt, void *target); | |||||
int nl_parse_attrs(struct nlmsghdr *hdr, int hdrlen, struct nlattr_parser *ps, | |||||
int pslen, struct netlink_parse_tracker *npt, void *target); | |||||
int nl_parse_header(void *hdr, int len, struct nlhdr_parser *parser, | |||||
struct netlink_parse_tracker *npt, void *target); | |||||
int nl_parse_nlmsg(struct nlmsghdr *hdr, struct nlhdr_parser *parser, | |||||
struct netlink_parse_tracker *npt, void *target); | |||||
int nlattr_get_flag(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_ip(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_uint32(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_ifp(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_ifpz(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_ipvia(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_string(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
int nlattr_get_nla(struct nlattr *nla, struct netlink_parse_tracker *npt, | |||||
void *target); | |||||
/* Parsing state */ | |||||
struct linear_buffer { | |||||
char *base; /* Base allocated memory pointer */ | |||||
uint32_t offset; /* Currently used offset */ | |||||
uint32_t size; /* Total buffer size */ | |||||
}; | |||||
static inline void * | |||||
lb_alloc(struct linear_buffer *lb, int len) | |||||
{ | |||||
len = roundup2(len, sizeof(uint64_t)); | |||||
if (lb->offset + len > lb->size) | |||||
return (NULL); | |||||
void *data = (void *)(lb->base + lb->offset); | |||||
lb->offset += len; | |||||
return (data); | |||||
} | |||||
static inline void | |||||
lb_clear(struct linear_buffer *lb) | |||||
{ | |||||
memset(lb->base, 0, lb->size); | |||||
lb->offset = 0; | |||||
} | |||||
#define NL_MAX_ERROR_BUF 128 | |||||
#define SCRATCH_BUFFER_SIZE (1024 + NL_MAX_ERROR_BUF) | |||||
struct netlink_parse_tracker { | |||||
struct linear_buffer lb; /* Per-message scratch buffer */ | |||||
struct nlpcb *nlp; /* Originator socket */ | |||||
struct nlmsg_state *ns; /* 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 */ | |||||
}; | |||||
static inline void * | |||||
npt_alloc(struct netlink_parse_tracker *npt, int len) | |||||
{ | |||||
return (lb_alloc(&npt->lb, len)); | |||||
} | |||||
#define npt_alloc_sockaddr(_npt, _len) ((struct sockaddr *)(npt_alloc(_npt, _len))) | |||||
bool nlmsg_report_err_msg(struct netlink_parse_tracker *npt, const char *fmt, ...); | |||||
#define NLMSG_REPORT_ERR_MSG(_npt, _fmt, ...) { \ | |||||
nlmsg_report_err_msg(_npt, _fmt, ## __VA_ARGS__); \ | |||||
NLP_LOG(LOG_DEBUG, (_npt)->nlp, _fmt, ## __VA_ARGS__); \ | |||||
} | |||||
bool nlmsg_report_err_offset(struct netlink_parse_tracker *npt, uint32_t off); | |||||
#endif |