23#include <netlink-private/netlink.h>
24#include <netlink-private/utils.h>
25#include <netlink/netlink.h>
26#include <netlink/utils.h>
27#include <netlink/cache.h>
28#include <netlink/attr.h>
29#include <linux/socket.h>
31static size_t default_msg_size;
33static void __init init_msg_size(
void)
35 default_msg_size = getpagesize();
51 return NLMSG_HDRLEN + payload;
54static int nlmsg_msg_size(
int payload)
69 return NLMSG_ALIGN(nlmsg_msg_size(payload));
102 return (
unsigned char *) nlh + NLMSG_HDRLEN;
105void *nlmsg_tail(
const struct nlmsghdr *nlh)
107 return (
unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
118 return nlh->nlmsg_len - NLMSG_HDRLEN;
121static int nlmsg_len(
const struct nlmsghdr *nlh)
141 return (
struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
151 return max_t(
int, nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen), 0);
161int nlmsg_valid_hdr(
const struct nlmsghdr *nlh,
int hdrlen)
163 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
174int nlmsg_ok(
const struct nlmsghdr *nlh,
int remaining)
176 return (remaining >= (
int)
sizeof(
struct nlmsghdr) &&
177 nlh->nlmsg_len >=
sizeof(
struct nlmsghdr) &&
178 nlh->nlmsg_len <= remaining);
189struct nlmsghdr *
nlmsg_next(
struct nlmsghdr *nlh,
int *remaining)
191 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
193 *remaining -= totlen;
195 return (
struct nlmsghdr *) ((
unsigned char *) nlh + totlen);
208int nlmsg_parse(
struct nlmsghdr *nlh,
int hdrlen,
struct nlattr *tb[],
211 if (!nlmsg_valid_hdr(nlh, hdrlen))
212 return -NLE_MSG_TOOSHORT;
242 if (!nlmsg_valid_hdr(nlh, hdrlen))
243 return -NLE_MSG_TOOSHORT;
256static struct nl_msg *__nlmsg_alloc(
size_t len)
260 if (len <
sizeof(
struct nlmsghdr))
261 len =
sizeof(
struct nlmsghdr);
263 nm = calloc(1,
sizeof(*nm));
269 nm->nm_nlh = calloc(1, len);
273 nm->nm_protocol = -1;
277 NL_DBG(2,
"msg %p: Allocated new message, maxlen=%zu\n", nm, len);
296 return __nlmsg_alloc(default_msg_size);
304 return __nlmsg_alloc(max);
323 struct nlmsghdr *
new = nm->nm_nlh;
325 new->nlmsg_type = hdr->nlmsg_type;
326 new->nlmsg_flags = hdr->nlmsg_flags;
327 new->nlmsg_seq = hdr->nlmsg_seq;
328 new->nlmsg_pid = hdr->nlmsg_pid;
344 struct nlmsghdr nlh = {
345 .nlmsg_type = nlmsgtype,
346 .nlmsg_flags = flags,
348 .nlmsg_pid = NL_AUTO_PID,
353 NL_DBG(2,
"msg %p: Allocated new simple message\n", msg);
367 default_msg_size = max;
383 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
387 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
406 char *buf = (
char *) n->nm_nlh;
407 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
410 if (len > n->nm_size)
413 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
415 if ((tlen + nlmsg_len) > n->nm_size)
419 n->nm_nlh->nlmsg_len += tlen;
422 memset(buf + len, 0, tlen - len);
424 NL_DBG(2,
"msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n",
425 n, tlen, len, pad, n->nm_nlh->nlmsg_len);
450 memcpy(tmp, data, len);
451 NL_DBG(2,
"msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
474 if (newlen <= n->nm_size)
477 tmp = realloc(n->nm_nlh, newlen);
503struct nlmsghdr *
nlmsg_put(
struct nl_msg *n, uint32_t pid, uint32_t seq,
504 int type,
int payload,
int flags)
506 struct nlmsghdr *nlh;
508 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
511 nlh = (
struct nlmsghdr *) n->nm_nlh;
512 nlh->nlmsg_type = type;
513 nlh->nlmsg_flags = flags;
514 nlh->nlmsg_pid = pid;
515 nlh->nlmsg_seq = seq;
517 NL_DBG(2,
"msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
518 "seq=%d\n", n, type, flags, pid, seq);
548 NL_DBG(4,
"New reference to message %p, total %d\n",
549 msg, msg->nm_refcnt);
564 NL_DBG(4,
"Returned message reference %p, %d remaining\n",
565 msg, msg->nm_refcnt);
567 if (msg->nm_refcnt < 0)
570 if (msg->nm_refcnt <= 0) {
572 NL_DBG(2,
"msg %p: Freed\n", msg);
584void nlmsg_set_proto(
struct nl_msg *msg,
int protocol)
586 msg->nm_protocol = protocol;
589int nlmsg_get_proto(
struct nl_msg *msg)
591 return msg->nm_protocol;
594size_t nlmsg_get_max_size(
struct nl_msg *msg)
599void nlmsg_set_src(
struct nl_msg *msg,
struct sockaddr_nl *addr)
601 memcpy(&msg->nm_src, addr,
sizeof(*addr));
604struct sockaddr_nl *nlmsg_get_src(
struct nl_msg *msg)
609void nlmsg_set_dst(
struct nl_msg *msg,
struct sockaddr_nl *addr)
611 memcpy(&msg->nm_dst, addr,
sizeof(*addr));
614struct sockaddr_nl *nlmsg_get_dst(
struct nl_msg *msg)
619void nlmsg_set_creds(
struct nl_msg *msg,
struct ucred *creds)
621 memcpy(&msg->nm_creds, creds,
sizeof(*creds));
622 msg->nm_flags |= NL_MSG_CRED_PRESENT;
625struct ucred *nlmsg_get_creds(
struct nl_msg *msg)
627 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
628 return &msg->nm_creds;
639static const struct trans_tbl nl_msgtypes[] = {
640 __ADD(NLMSG_NOOP,NOOP),
641 __ADD(NLMSG_ERROR,ERROR),
642 __ADD(NLMSG_DONE,DONE),
643 __ADD(NLMSG_OVERRUN,OVERRUN),
646char *nl_nlmsgtype2str(
int type,
char *buf,
size_t size)
648 return __type2str(type, buf, size, nl_msgtypes,
649 ARRAY_SIZE(nl_msgtypes));
652int nl_str2nlmsgtype(
const char *name)
654 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
664char *nl_nlmsg_flags2str(
int flags,
char *buf,
size_t len)
668#define PRINT_FLAG(f) \
669 if (flags & NLM_F_##f) { \
670 flags &= ~NLM_F_##f; \
671 strncat(buf, #f, len - strlen(buf) - 1); \
673 strncat(buf, ",", len - strlen(buf) - 1); \
690 snprintf(s,
sizeof(s),
"0x%x", flags);
691 strncat(buf, s, len - strlen(buf) - 1);
707 void (*cb)(
struct nl_object *,
void *);
712static int parse_cb(
struct nl_object *obj,
struct nl_parser_param *p)
714 struct dp_xdata *x = p->pp_arg;
720int nl_msg_parse(
struct nl_msg *msg,
void (*cb)(
struct nl_object *,
void *),
723 struct nl_cache_ops *ops;
724 struct nl_parser_param p = {
727 struct dp_xdata x = {
736 return -NLE_MSGTYPE_NOSUPPORT;
739 err = nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
752static void prefix_line(FILE *ofd,
int prefix)
756 for (i = 0; i < prefix; i++)
760static inline void dump_hex(FILE *ofd,
char *start,
int len,
int prefix)
763 char ascii[21] = {0};
765 limit = 16 - (prefix * 2);
766 prefix_line(ofd, prefix);
769 for (i = 0, a = 0, c = 0; i < len; i++) {
770 int v = *(uint8_t *) (start + i);
772 fprintf(ofd,
"%02x ", v);
773 ascii[a++] = isprint(v) ? v :
'.';
776 fprintf(ofd,
"%s\n", ascii);
778 prefix_line(ofd, prefix);
782 memset(ascii, 0,
sizeof(ascii));
787 for (i = 0; i < (limit - c); i++)
789 fprintf(ofd,
"%s\n", ascii);
793static void print_hdr(FILE *ofd,
struct nl_msg *msg)
796 struct nl_cache_ops *ops;
797 struct nl_msgtype *mt;
800 fprintf(ofd,
" .nlmsg_len = %d\n", nlh->nlmsg_len);
808 snprintf(buf,
sizeof(buf),
"%s::%s", ops->co_name, mt->mt_name);
811 nl_nlmsgtype2str(nlh->nlmsg_type, buf,
sizeof(buf));
813 fprintf(ofd,
" .type = %d <%s>\n", nlh->nlmsg_type, buf);
814 fprintf(ofd,
" .flags = %d <%s>\n", nlh->nlmsg_flags,
815 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf,
sizeof(buf)));
816 fprintf(ofd,
" .seq = %d\n", nlh->nlmsg_seq);
817 fprintf(ofd,
" .port = %d\n", nlh->nlmsg_pid);
821static void print_genl_hdr(FILE *ofd,
void *start)
823 struct genlmsghdr *ghdr = start;
825 fprintf(ofd,
" [GENERIC NETLINK HEADER] %zu octets\n", GENL_HDRLEN);
826 fprintf(ofd,
" .cmd = %u\n", ghdr->cmd);
827 fprintf(ofd,
" .version = %u\n", ghdr->version);
828 fprintf(ofd,
" .unused = %#x\n", ghdr->reserved);
831static void *print_genl_msg(
struct nl_msg *msg, FILE *ofd,
struct nlmsghdr *hdr,
832 struct nl_cache_ops *ops,
int *payloadlen)
836 if (*payloadlen < GENL_HDRLEN)
839 print_genl_hdr(ofd, data);
841 *payloadlen -= GENL_HDRLEN;
845 int hdrsize = ops->co_hdrsize - GENL_HDRLEN;
848 if (*payloadlen < hdrsize)
851 fprintf(ofd,
" [HEADER] %d octets\n", hdrsize);
852 dump_hex(ofd, data, hdrsize, 0);
854 *payloadlen -= hdrsize;
862static void dump_attr(FILE *ofd,
struct nlattr *attr,
int prefix)
866 dump_hex(ofd,
nla_data(attr), len, prefix);
869static void dump_attrs(FILE *ofd,
struct nlattr *attrs,
int attrlen,
876 int padlen, alen =
nla_len(nla);
878 prefix_line(ofd, prefix);
880 if (nla->nla_type == 0)
881 fprintf(ofd,
" [ATTR PADDING] %d octets\n", alen);
883 fprintf(ofd,
" [ATTR %02d%s] %d octets\n",
nla_type(nla),
888 dump_attrs(ofd,
nla_data(nla), alen, prefix+1);
890 dump_attr(ofd, nla, prefix);
894 prefix_line(ofd, prefix);
895 fprintf(ofd,
" [PADDING] %d octets\n",
897 dump_hex(ofd, (
char *)
nla_data(nla) + alen,
903 prefix_line(ofd, prefix);
904 fprintf(ofd,
" [LEFTOVER] %d octets\n", rem);
908static void dump_error_msg(
struct nl_msg *msg, FILE *ofd)
913 fprintf(ofd,
" [ERRORMSG] %zu octets\n",
sizeof(*err));
915 if (nlmsg_len(hdr) >=
sizeof(*err)) {
916 struct nl_msg *errmsg;
918 fprintf(ofd,
" .error = %d \"%s\"\n", err->error,
919 nl_strerror_l(-err->error));
920 fprintf(ofd,
" [ORIGINAL MESSAGE] %zu octets\n",
sizeof(*hdr));
923 print_hdr(ofd, errmsg);
928static void print_msg(
struct nl_msg *msg, FILE *ofd,
struct nlmsghdr *hdr)
930 struct nl_cache_ops *ops;
931 int payloadlen = nlmsg_len(hdr);
940 payloadlen -= attrlen;
943 if (msg->nm_protocol == NETLINK_GENERIC)
944 data = print_genl_msg(msg, ofd, hdr, ops, &payloadlen);
947 fprintf(ofd,
" [PAYLOAD] %d octets\n", payloadlen);
948 dump_hex(ofd, data, payloadlen, 0);
952 struct nlattr *attrs;
957 dump_attrs(ofd, attrs, attrlen, 0);
974 "-------------------------- BEGIN NETLINK MESSAGE ---------------------------\n");
976 fprintf(ofd,
" [NETLINK HEADER] %zu octets\n",
sizeof(
struct nlmsghdr));
979 if (hdr->nlmsg_type == NLMSG_ERROR)
980 dump_error_msg(msg, ofd);
981 else if (nlmsg_len(hdr) > 0)
982 print_msg(msg, ofd, hdr);
985 "--------------------------- 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.