29 #include <netlink-local.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 >=
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);
719 struct dp_xdata *x = p->
pp_arg;
725 int nl_msg_parse(
struct nl_msg *msg,
void (*cb)(
struct nl_object *,
void *),
732 struct dp_xdata x = {
740 return -NLE_MSGTYPE_NOSUPPORT;
743 return nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
753 static void prefix_line(FILE *ofd,
int prefix)
757 for (i = 0; i < prefix; i++)
761 static inline void dump_hex(FILE *ofd,
char *start,
int len,
int prefix)
764 char ascii[21] = {0};
766 limit = 18 - (prefix * 2);
767 prefix_line(ofd, prefix);
770 for (i = 0, a = 0, c = 0; i < len; i++) {
771 int v = *(uint8_t *) (start + i);
773 fprintf(ofd,
"%02x ", v);
774 ascii[a++] = isprint(v) ? v :
'.';
777 fprintf(ofd,
"%s\n", ascii);
779 prefix_line(ofd, prefix);
783 memset(ascii, 0,
sizeof(ascii));
789 for (i = 0; i < (limit - c); i++)
791 fprintf(ofd,
"%s\n", ascii);
795 static void print_hdr(FILE *ofd,
struct nl_msg *msg)
802 fprintf(ofd,
" .nlmsg_len = %d\n", nlh->nlmsg_len);
812 nl_nlmsgtype2str(nlh->nlmsg_type, buf,
sizeof(buf));
814 fprintf(ofd,
" .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
815 fprintf(ofd,
" .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
816 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf,
sizeof(buf)));
817 fprintf(ofd,
" .nlmsg_seq = %d\n", nlh->nlmsg_seq);
818 fprintf(ofd,
" .nlmsg_pid = %d\n", nlh->nlmsg_pid);
822 static void dump_attrs(FILE *ofd,
struct nlattr *attrs,
int attrlen,
829 int padlen, alen =
nla_len(nla);
831 prefix_line(ofd, prefix);
832 fprintf(ofd,
" [ATTR %02d%s] %d octets\n",
nla_type(nla),
833 nla->nla_type & NLA_F_NESTED ?
" NESTED" :
"",
836 if (nla->nla_type & NLA_F_NESTED)
837 dump_attrs(ofd,
nla_data(nla), alen, prefix+1);
839 dump_hex(ofd,
nla_data(nla), alen, prefix);
843 prefix_line(ofd, prefix);
844 fprintf(ofd,
" [PADDING] %d octets\n",
852 prefix_line(ofd, prefix);
853 fprintf(ofd,
" [LEFTOVER] %d octets\n", rem);
867 "-------------------------- BEGIN NETLINK MESSAGE "
868 "---------------------------\n");
870 fprintf(ofd,
" [HEADER] %zu octets\n",
sizeof(
struct nlmsghdr));
873 if (hdr->nlmsg_type == NLMSG_ERROR &&
874 hdr->nlmsg_len >= nlmsg_msg_size(
sizeof(
struct nlmsgerr))) {
875 struct nl_msg *errmsg;
878 fprintf(ofd,
" [ERRORMSG] %zu octets\n",
sizeof(*err));
879 fprintf(ofd,
" .error = %d \"%s\"\n", err->error,
880 strerror(-err->error));
881 fprintf(ofd,
" [ORIGINAL MESSAGE] %zu octets\n",
sizeof(*hdr));
884 print_hdr(ofd, errmsg);
886 }
else if (nlmsg_len(hdr) > 0) {
888 int payloadlen = nlmsg_len(hdr);
895 payloadlen -= attrlen;
898 fprintf(ofd,
" [PAYLOAD] %d octets\n", payloadlen);
899 dump_hex(ofd,
nlmsg_data(hdr), payloadlen, 0);
902 struct nlattr *attrs;
907 dump_attrs(ofd, attrs, attrlen, 0);
912 "--------------------------- END NETLINK MESSAGE "
913 "---------------------------\n");