29 #include <netlink-private/netlink.h>
30 #include <netlink/netlink.h>
31 #include <netlink/utils.h>
32 #include <netlink/cache.h>
33 #include <netlink/attr.h>
34 #include <linux/socket.h>
36 static size_t default_msg_size;
38 static void __init init_msg_size(
void)
40 default_msg_size = getpagesize();
56 return NLMSG_HDRLEN + payload;
59 static int nlmsg_msg_size(
int payload)
74 return NLMSG_ALIGN(nlmsg_msg_size(payload));
107 return (
unsigned char *) nlh + NLMSG_HDRLEN;
110 void *nlmsg_tail(
const struct nlmsghdr *nlh)
112 return (
unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
123 return nlh->nlmsg_len - NLMSG_HDRLEN;
126 static int nlmsg_len(
const struct nlmsghdr *nlh)
146 return (
struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
156 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
166 int nlmsg_valid_hdr(
const struct nlmsghdr *nlh,
int hdrlen)
168 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
179 int nlmsg_ok(
const struct nlmsghdr *nlh,
int remaining)
181 return (remaining >= (
int)
sizeof(
struct nlmsghdr) &&
182 nlh->nlmsg_len >=
sizeof(
struct nlmsghdr) &&
183 nlh->nlmsg_len <= remaining);
194 struct nlmsghdr *
nlmsg_next(
struct nlmsghdr *nlh,
int *remaining)
196 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
198 *remaining -= totlen;
200 return (
struct nlmsghdr *) ((
unsigned char *) nlh + totlen);
213 int nlmsg_parse(
struct nlmsghdr *nlh,
int hdrlen,
struct nlattr *tb[],
216 if (!nlmsg_valid_hdr(nlh, hdrlen))
217 return -NLE_MSG_TOOSHORT;
247 if (!nlmsg_valid_hdr(nlh, hdrlen))
248 return -NLE_MSG_TOOSHORT;
261 static struct nl_msg *__nlmsg_alloc(
size_t len)
265 if (len <
sizeof(
struct nlmsghdr))
266 len =
sizeof(
struct nlmsghdr);
268 nm = calloc(1,
sizeof(*nm));
274 nm->nm_nlh = calloc(1, len);
278 memset(nm->nm_nlh, 0,
sizeof(
struct nlmsghdr));
280 nm->nm_protocol = -1;
284 NL_DBG(2,
"msg %p: Allocated new message, maxlen=%zu\n", nm, len);
303 return __nlmsg_alloc(default_msg_size);
311 return __nlmsg_alloc(max);
330 struct nlmsghdr *
new = nm->nm_nlh;
332 new->nlmsg_type = hdr->nlmsg_type;
333 new->nlmsg_flags = hdr->nlmsg_flags;
334 new->nlmsg_seq = hdr->nlmsg_seq;
335 new->nlmsg_pid = hdr->nlmsg_pid;
351 struct nlmsghdr nlh = {
352 .nlmsg_type = nlmsgtype,
353 .nlmsg_flags = flags,
358 NL_DBG(2,
"msg %p: Allocated new simple message\n", msg);
372 default_msg_size = max;
388 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
392 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
414 void *buf = n->nm_nlh;
415 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
418 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
420 if ((tlen + nlmsg_len) > n->nm_size)
424 n->nm_nlh->nlmsg_len += tlen;
427 memset(buf + len, 0, tlen - len);
429 NL_DBG(2,
"msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n",
430 n, tlen, len, pad, n->nm_nlh->nlmsg_len);
455 memcpy(tmp, data, len);
456 NL_DBG(2,
"msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
479 if (newlen <= n->nm_size)
482 tmp = realloc(n->nm_nlh, newlen);
508 struct nlmsghdr *
nlmsg_put(
struct nl_msg *n, uint32_t pid, uint32_t seq,
509 int type,
int payload,
int flags)
511 struct nlmsghdr *nlh;
513 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
516 nlh = (
struct nlmsghdr *) n->nm_nlh;
517 nlh->nlmsg_type = type;
518 nlh->nlmsg_flags = flags;
519 nlh->nlmsg_pid = pid;
520 nlh->nlmsg_seq = seq;
522 NL_DBG(2,
"msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
523 "seq=%d\n", n, type, flags, pid, seq);
553 NL_DBG(4,
"New reference to message %p, total %d\n",
554 msg, msg->nm_refcnt);
569 NL_DBG(4,
"Returned message reference %p, %d remaining\n",
570 msg, msg->nm_refcnt);
572 if (msg->nm_refcnt < 0)
575 if (msg->nm_refcnt <= 0) {
578 NL_DBG(2,
"msg %p: Freed\n", msg);
589 void nlmsg_set_proto(
struct nl_msg *msg,
int protocol)
591 msg->nm_protocol = protocol;
594 int nlmsg_get_proto(
struct nl_msg *msg)
596 return msg->nm_protocol;
599 size_t nlmsg_get_max_size(
struct nl_msg *msg)
604 void nlmsg_set_src(
struct nl_msg *msg,
struct sockaddr_nl *addr)
606 memcpy(&msg->nm_src, addr,
sizeof(*addr));
609 struct sockaddr_nl *nlmsg_get_src(
struct nl_msg *msg)
614 void nlmsg_set_dst(
struct nl_msg *msg,
struct sockaddr_nl *addr)
616 memcpy(&msg->nm_dst, addr,
sizeof(*addr));
619 struct sockaddr_nl *nlmsg_get_dst(
struct nl_msg *msg)
624 void nlmsg_set_creds(
struct nl_msg *msg,
struct ucred *creds)
626 memcpy(&msg->nm_creds, creds,
sizeof(*creds));
627 msg->nm_flags |= NL_MSG_CRED_PRESENT;
630 struct ucred *nlmsg_get_creds(
struct nl_msg *msg)
632 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
633 return &msg->nm_creds;
644 static const struct trans_tbl nl_msgtypes[] = {
645 __ADD(NLMSG_NOOP,NOOP)
646 __ADD(NLMSG_ERROR,ERROR)
647 __ADD(NLMSG_DONE,DONE)
648 __ADD(NLMSG_OVERRUN,OVERRUN)
651 char *nl_nlmsgtype2str(
int type,
char *buf,
size_t size)
653 return __type2str(type, buf, size, nl_msgtypes,
654 ARRAY_SIZE(nl_msgtypes));
657 int nl_str2nlmsgtype(
const char *name)
659 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
669 char *nl_nlmsg_flags2str(
int flags,
char *buf,
size_t len)
673 #define PRINT_FLAG(f) \
674 if (flags & NLM_F_##f) { \
675 flags &= ~NLM_F_##f; \
676 strncat(buf, #f, len - strlen(buf) - 1); \
678 strncat(buf, ",", len - strlen(buf) - 1); \
695 snprintf(s,
sizeof(s),
"0x%x", flags);
696 strncat(buf, s, len - strlen(buf) - 1);
712 void (*cb)(
struct nl_object *,
void *);
717 static int parse_cb(
struct nl_object *obj,
struct nl_parser_param *p)
719 struct dp_xdata *x = p->pp_arg;
725 int nl_msg_parse(
struct nl_msg *msg,
void (*cb)(
struct nl_object *,
void *),
728 struct nl_cache_ops *ops;
729 struct nl_parser_param p = {
732 struct dp_xdata x = {
741 return -NLE_MSGTYPE_NOSUPPORT;
744 err = nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
757 static void prefix_line(FILE *ofd,
int prefix)
761 for (i = 0; i < prefix; i++)
765 static inline void dump_hex(FILE *ofd,
char *start,
int len,
int prefix)
768 char ascii[21] = {0};
770 limit = 18 - (prefix * 2);
771 prefix_line(ofd, prefix);
774 for (i = 0, a = 0, c = 0; i < len; i++) {
775 int v = *(uint8_t *) (start + i);
777 fprintf(ofd,
"%02x ", v);
778 ascii[a++] = isprint(v) ? v :
'.';
781 fprintf(ofd,
"%s\n", ascii);
783 prefix_line(ofd, prefix);
787 memset(ascii, 0,
sizeof(ascii));
793 for (i = 0; i < (limit - c); i++)
795 fprintf(ofd,
"%s\n", ascii);
799 static void print_hdr(FILE *ofd,
struct nl_msg *msg)
802 struct nl_cache_ops *ops;
803 struct nl_msgtype *mt;
806 fprintf(ofd,
" .nlmsg_len = %d\n", nlh->nlmsg_len);
814 snprintf(buf,
sizeof(buf),
"%s::%s", ops->co_name, mt->mt_name);
817 nl_nlmsgtype2str(nlh->nlmsg_type, buf,
sizeof(buf));
819 fprintf(ofd,
" .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
820 fprintf(ofd,
" .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
821 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf,
sizeof(buf)));
822 fprintf(ofd,
" .nlmsg_seq = %d\n", nlh->nlmsg_seq);
823 fprintf(ofd,
" .nlmsg_pid = %d\n", nlh->nlmsg_pid);
827 static void dump_attrs(FILE *ofd,
struct nlattr *attrs,
int attrlen,
834 int padlen, alen =
nla_len(nla);
836 prefix_line(ofd, prefix);
838 if (nla->nla_type == 0)
839 fprintf(ofd,
" [ATTR PADDING] %d octets\n", alen);
841 fprintf(ofd,
" [ATTR %02d%s] %d octets\n",
nla_type(nla),
842 nla->nla_type & NLA_F_NESTED ?
" NESTED" :
"",
845 if (nla->nla_type & NLA_F_NESTED)
846 dump_attrs(ofd,
nla_data(nla), alen, prefix+1);
848 dump_hex(ofd,
nla_data(nla), alen, prefix);
852 prefix_line(ofd, prefix);
853 fprintf(ofd,
" [PADDING] %d octets\n",
861 prefix_line(ofd, prefix);
862 fprintf(ofd,
" [LEFTOVER] %d octets\n", rem);
866 static void dump_error_msg(
struct nl_msg *msg, FILE *ofd)
871 fprintf(ofd,
" [ERRORMSG] %zu octets\n",
sizeof(*err));
873 if (nlmsg_len(hdr) >=
sizeof(*err)) {
875 struct nl_msg *errmsg;
877 fprintf(ofd,
" .error = %d \"%s\"\n", err->error,
878 strerror_r(-err->error, buf,
sizeof(buf)));
879 fprintf(ofd,
" [ORIGINAL MESSAGE] %zu octets\n",
sizeof(*hdr));
882 print_hdr(ofd, errmsg);
897 "-------------------------- BEGIN NETLINK MESSAGE "
898 "---------------------------\n");
900 fprintf(ofd,
" [HEADER] %zu octets\n",
sizeof(
struct nlmsghdr));
903 if (hdr->nlmsg_type == NLMSG_ERROR)
904 dump_error_msg(msg, ofd);
905 else if (nlmsg_len(hdr) > 0) {
906 struct nl_cache_ops *ops;
907 int payloadlen = nlmsg_len(hdr);
914 payloadlen -= attrlen;
917 fprintf(ofd,
" [PAYLOAD] %d octets\n", payloadlen);
918 dump_hex(ofd,
nlmsg_data(hdr), payloadlen, 0);
921 struct nlattr *attrs;
926 dump_attrs(ofd, attrs, attrlen, 0);
934 "--------------------------- END NETLINK MESSAGE "
935 "---------------------------\n");