23#include "nl-default.h"
27#include <linux/socket.h>
29#include <netlink/netlink.h>
30#include <netlink/utils.h>
31#include <netlink/cache.h>
32#include <netlink/attr.h>
35#include "nl-priv-dynamic-core/nl-core.h"
36#include "nl-priv-dynamic-core/cache-api.h"
37#include "nl-aux-core/nl-core.h"
39static size_t default_msg_size;
41static void _nl_init init_msg_size(
void)
43 default_msg_size = getpagesize();
59 return NLMSG_HDRLEN + payload;
62static int nlmsg_msg_size(
int payload)
77 return NLMSG_ALIGN(nlmsg_msg_size(payload));
110 return (
unsigned char *) nlh + NLMSG_HDRLEN;
113void *nlmsg_tail(
const struct nlmsghdr *nlh)
115 return (
unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
126 return nlh->nlmsg_len - NLMSG_HDRLEN;
129static int nlmsg_len(
const struct nlmsghdr *nlh)
149 return (
struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
159 return _NL_MAX(nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen), 0u);
169int nlmsg_valid_hdr(
const struct nlmsghdr *nlh,
int hdrlen)
171 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
182int nlmsg_ok(
const struct nlmsghdr *nlh,
int remaining)
184 return (remaining >= (
int)
sizeof(
struct nlmsghdr) &&
185 nlh->nlmsg_len >=
sizeof(
struct nlmsghdr) &&
186 nlh->nlmsg_len <= remaining);
197struct nlmsghdr *
nlmsg_next(
struct nlmsghdr *nlh,
int *remaining)
199 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
201 *remaining -= totlen;
203 return (
struct nlmsghdr *) ((
unsigned char *) nlh + totlen);
216int nlmsg_parse(
struct nlmsghdr *nlh,
int hdrlen,
struct nlattr *tb[],
219 if (!nlmsg_valid_hdr(nlh, hdrlen))
220 return -NLE_MSG_TOOSHORT;
250 if (!nlmsg_valid_hdr(nlh, hdrlen))
251 return -NLE_MSG_TOOSHORT;
264static struct nl_msg *__nlmsg_alloc(
size_t len)
268 if (len <
sizeof(
struct nlmsghdr))
269 len =
sizeof(
struct nlmsghdr);
271 nm = calloc(1,
sizeof(*nm));
277 nm->nm_nlh = calloc(1, len);
281 nm->nm_protocol = -1;
285 NL_DBG(2,
"msg %p: Allocated new message, maxlen=%zu\n", nm, len);
304 return __nlmsg_alloc(default_msg_size);
312 return __nlmsg_alloc(max);
331 struct nlmsghdr *
new = nm->nm_nlh;
333 new->nlmsg_type = hdr->nlmsg_type;
334 new->nlmsg_flags = hdr->nlmsg_flags;
335 new->nlmsg_seq = hdr->nlmsg_seq;
336 new->nlmsg_pid = hdr->nlmsg_pid;
352 struct nlmsghdr nlh = {
353 .nlmsg_type = nlmsgtype,
354 .nlmsg_flags = flags,
356 .nlmsg_pid = NL_AUTO_PID,
361 NL_DBG(2,
"msg %p: Allocated new simple message\n", msg);
375 default_msg_size = max;
391 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
395 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
414 char *buf = (
char *) n->nm_nlh;
415 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
418 if (len > n->nm_size)
421 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
423 if ((tlen + nlmsg_len) > n->nm_size)
427 n->nm_nlh->nlmsg_len += tlen;
430 memset(buf + len, 0, tlen - len);
432 NL_DBG(2,
"msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n",
433 n, tlen, len, pad, n->nm_nlh->nlmsg_len);
458 memcpy(tmp, data, len);
459 NL_DBG(2,
"msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
482 if (newlen <= n->nm_size)
485 tmp = realloc(n->nm_nlh, newlen);
511struct nlmsghdr *
nlmsg_put(
struct nl_msg *n, uint32_t pid, uint32_t seq,
512 int type,
int payload,
int flags)
514 struct nlmsghdr *nlh;
516 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
519 nlh = (
struct nlmsghdr *) n->nm_nlh;
520 nlh->nlmsg_type = type;
521 nlh->nlmsg_flags = flags;
522 nlh->nlmsg_pid = pid;
523 nlh->nlmsg_seq = seq;
525 NL_DBG(2,
"msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
526 "seq=%d\n", n, type, flags, pid, seq);
556 NL_DBG(4,
"New reference to message %p, total %d\n",
557 msg, msg->nm_refcnt);
572 NL_DBG(4,
"Returned message reference %p, %d remaining\n",
573 msg, msg->nm_refcnt);
575 if (msg->nm_refcnt < 0)
578 if (msg->nm_refcnt <= 0) {
580 NL_DBG(2,
"msg %p: Freed\n", msg);
592void nlmsg_set_proto(
struct nl_msg *msg,
int protocol)
594 msg->nm_protocol = protocol;
597int nlmsg_get_proto(
struct nl_msg *msg)
599 return msg->nm_protocol;
602size_t nlmsg_get_max_size(
struct nl_msg *msg)
607void nlmsg_set_src(
struct nl_msg *msg,
struct sockaddr_nl *addr)
609 memcpy(&msg->nm_src, addr,
sizeof(*addr));
612struct sockaddr_nl *nlmsg_get_src(
struct nl_msg *msg)
617void nlmsg_set_dst(
struct nl_msg *msg,
struct sockaddr_nl *addr)
619 memcpy(&msg->nm_dst, addr,
sizeof(*addr));
622struct sockaddr_nl *nlmsg_get_dst(
struct nl_msg *msg)
627void nlmsg_set_creds(
struct nl_msg *msg,
struct ucred *creds)
629 memcpy(&msg->nm_creds, creds,
sizeof(*creds));
630 msg->nm_flags |= NL_MSG_CRED_PRESENT;
633struct ucred *nlmsg_get_creds(
struct nl_msg *msg)
635 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
636 return &msg->nm_creds;
647static const struct trans_tbl nl_msgtypes[] = {
648 __ADD(NLMSG_NOOP,NOOP),
649 __ADD(NLMSG_ERROR,ERROR),
650 __ADD(NLMSG_DONE,DONE),
651 __ADD(NLMSG_OVERRUN,OVERRUN),
654char *nl_nlmsgtype2str(
int type,
char *buf,
size_t size)
656 return __type2str(type, buf, size, nl_msgtypes,
657 ARRAY_SIZE(nl_msgtypes));
660int nl_str2nlmsgtype(
const char *name)
662 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
672char *nl_nlmsg_flags2str(
int flags,
char *buf,
size_t len)
676#define PRINT_FLAG(f) \
677 if (flags & NLM_F_##f) { \
678 flags &= ~NLM_F_##f; \
679 strncat(buf, #f, len - strlen(buf) - 1); \
681 strncat(buf, ",", len - strlen(buf) - 1); \
698 snprintf(s,
sizeof(s),
"0x%x", flags);
699 strncat(buf, s, len - strlen(buf) - 1);
715 void (*cb)(
struct nl_object *,
void *);
720static int parse_cb(
struct nl_object *obj,
struct nl_parser_param *p)
722 struct dp_xdata *x = p->pp_arg;
728int nl_msg_parse(
struct nl_msg *msg,
void (*cb)(
struct nl_object *,
void *),
731 struct nl_cache_ops *ops;
732 struct nl_parser_param p = {
735 struct dp_xdata x = {
744 return -NLE_MSGTYPE_NOSUPPORT;
747 err = nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
760static void prefix_line(FILE *ofd,
int prefix)
764 for (i = 0; i < prefix; i++)
768static inline void dump_hex(FILE *ofd,
char *start,
int len,
int prefix)
771 char ascii[21] = {0};
773 limit = 16 - (prefix * 2);
774 prefix_line(ofd, prefix);
777 for (i = 0, a = 0, c = 0; i < len; i++) {
778 int v = *(uint8_t *) (start + i);
780 fprintf(ofd,
"%02x ", v);
781 ascii[a++] = isprint(v) ? v :
'.';
784 fprintf(ofd,
"%s\n", ascii);
786 prefix_line(ofd, prefix);
790 memset(ascii, 0,
sizeof(ascii));
795 for (i = 0; i < (limit - c); i++)
797 fprintf(ofd,
"%s\n", ascii);
801static void print_hdr(FILE *ofd,
struct nl_msg *msg)
804 struct nl_cache_ops *ops;
805 struct nl_msgtype *mt;
808 fprintf(ofd,
" .nlmsg_len = %d\n", nlh->nlmsg_len);
816 snprintf(buf,
sizeof(buf),
"%s::%s", ops->co_name, mt->mt_name);
819 nl_nlmsgtype2str(nlh->nlmsg_type, buf,
sizeof(buf));
821 fprintf(ofd,
" .type = %d <%s>\n", nlh->nlmsg_type, buf);
822 fprintf(ofd,
" .flags = %d <%s>\n", nlh->nlmsg_flags,
823 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf,
sizeof(buf)));
824 fprintf(ofd,
" .seq = %d\n", nlh->nlmsg_seq);
825 fprintf(ofd,
" .port = %d\n", nlh->nlmsg_pid);
829static void print_genl_hdr(FILE *ofd,
void *start)
831 struct genlmsghdr *ghdr = start;
833 fprintf(ofd,
" [GENERIC NETLINK HEADER] %zu octets\n", GENL_HDRLEN);
834 fprintf(ofd,
" .cmd = %u\n", ghdr->cmd);
835 fprintf(ofd,
" .version = %u\n", ghdr->version);
836 fprintf(ofd,
" .unused = %#x\n", ghdr->reserved);
839static void *print_genl_msg(
struct nl_msg *msg, FILE *ofd,
struct nlmsghdr *hdr,
840 struct nl_cache_ops *ops,
int *payloadlen)
844 if (*payloadlen < GENL_HDRLEN)
847 print_genl_hdr(ofd, data);
849 *payloadlen -= GENL_HDRLEN;
853 int hdrsize = ops->co_hdrsize - GENL_HDRLEN;
856 if (*payloadlen < hdrsize)
859 fprintf(ofd,
" [HEADER] %d octets\n", hdrsize);
860 dump_hex(ofd, data, hdrsize, 0);
862 *payloadlen -= hdrsize;
870static void dump_attr(FILE *ofd,
struct nlattr *attr,
int prefix)
874 dump_hex(ofd,
nla_data(attr), len, prefix);
877static void dump_attrs(FILE *ofd,
struct nlattr *attrs,
int attrlen,
884 int padlen, alen =
nla_len(nla);
886 prefix_line(ofd, prefix);
888 if (nla->nla_type == 0)
889 fprintf(ofd,
" [ATTR PADDING] %d octets\n", alen);
891 fprintf(ofd,
" [ATTR %02d%s] %d octets\n",
nla_type(nla),
896 dump_attrs(ofd,
nla_data(nla), alen, prefix+1);
898 dump_attr(ofd, nla, prefix);
902 prefix_line(ofd, prefix);
903 fprintf(ofd,
" [PADDING] %d octets\n",
905 dump_hex(ofd, (
char *)
nla_data(nla) + alen,
911 prefix_line(ofd, prefix);
912 fprintf(ofd,
" [LEFTOVER] %d octets\n", rem);
916static void dump_error_msg(
struct nl_msg *msg, FILE *ofd)
921 fprintf(ofd,
" [ERRORMSG] %zu octets\n",
sizeof(*err));
923 if (nlmsg_len(hdr) >=
sizeof(*err)) {
924 struct nl_msg *errmsg;
926 fprintf(ofd,
" .error = %d \"%s\"\n", err->error,
927 nl_strerror_l(-err->error));
928 fprintf(ofd,
" [ORIGINAL MESSAGE] %zu octets\n",
sizeof(*hdr));
931 print_hdr(ofd, errmsg);
936static void print_msg(
struct nl_msg *msg, FILE *ofd,
struct nlmsghdr *hdr)
938 struct nl_cache_ops *ops;
939 int payloadlen = nlmsg_len(hdr);
948 payloadlen -= attrlen;
951 if (msg->nm_protocol == NETLINK_GENERIC)
952 data = print_genl_msg(msg, ofd, hdr, ops, &payloadlen);
955 fprintf(ofd,
" [PAYLOAD] %d octets\n", payloadlen);
956 dump_hex(ofd, data, payloadlen, 0);
960 struct nlattr *attrs;
965 dump_attrs(ofd, attrs, attrlen, 0);
982 "-------------------------- BEGIN NETLINK MESSAGE ---------------------------\n");
984 fprintf(ofd,
" [NETLINK HEADER] %zu octets\n",
sizeof(
struct nlmsghdr));
987 if (hdr->nlmsg_type == NLMSG_ERROR)
988 dump_error_msg(msg, ofd);
989 else if (nlmsg_len(hdr) > 0)
990 print_msg(msg, ofd, hdr);
993 "--------------------------- END NETLINK MESSAGE ---------------------------\n");
int nla_validate(const struct nlattr *head, int len, int maxtype, const struct nla_policy *policy)
Validate a stream of attributes.
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
int nla_type(const struct nlattr *nla)
Return type of the attribute.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int nla_is_nested(const struct nlattr *attr)
Return true if attribute has NLA_F_NESTED flag set.
#define nla_for_each_attr(pos, head, len, rem)
Iterate over a stream of attributes.
int nla_len(const struct nlattr *nla)
Return length of the payload .
struct nlattr * nla_find(const struct nlattr *head, int len, int attrtype)
Find a single attribute in a stream of attributes.
int nla_padlen(int payload)
Return length of padding at the tail of the attribute.
struct nl_msgtype * nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
Lookup message type cache association.
struct nl_cache_ops * nl_cache_ops_associate_safe(int protocol, int msgtype)
Associate protocol and message type to cache operations.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
check if the netlink message fits into the remaining bytes
void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
Dump message in human readable format to file descriptor.
int nlmsg_total_size(int payload)
Calculates size of netlink message including padding based on payload length.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
void nlmsg_get(struct nl_msg *msg)
Acquire a reference on a netlink message.
struct nlmsghdr * nlmsg_next(struct nlmsghdr *nlh, int *remaining)
next netlink message in message stream
int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, const struct nla_policy *policy)
nlmsg_validate - validate a netlink message including attributes
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
struct nl_msg * nlmsg_convert(struct nlmsghdr *hdr)
Convert a netlink message received from a netlink socket to a nl_msg.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
struct nl_msg * nlmsg_alloc_size(size_t max)
Allocate a new netlink message with maximum payload size specified.
int nlmsg_expand(struct nl_msg *n, size_t newlen)
Expand maximum payload size of a netlink message.
struct nlattr * nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
nlmsg_find_attr - find a specific attribute in a netlink message
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
int nlmsg_datalen(const struct nlmsghdr *nlh)
Return length of message payload.
struct nlmsghdr * nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq, int type, int payload, int flags)
Add a netlink message header to a netlink message.
int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
length of attributes data
void nlmsg_set_default_size(size_t max)
Set the default maximum message payload size for allocated messages.
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
int nlmsg_padlen(int payload)
Size of padding that needs to be added at end of message.
struct nlattr * nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
head of attributes data
int nlmsg_size(int payload)
Calculates size of netlink message based on payload length.
struct nl_msg * nlmsg_inherit(struct nlmsghdr *hdr)
Allocate a new netlink message and inherit netlink message header.
Attribute validation policy.