161 #include <netlink-local.h>
162 #include <netlink/netlink.h>
163 #include <netlink/utils.h>
164 #include <netlink/cache.h>
165 #include <netlink/attr.h>
166 #include <linux/socket.h>
168 static size_t default_msg_size;
170 static void __init init_msg_size(
void)
172 default_msg_size = getpagesize();
188 return NLMSG_HDRLEN + payload;
191 static int nlmsg_msg_size(
int payload)
206 return NLMSG_ALIGN(nlmsg_msg_size(payload));
239 return (
unsigned char *) nlh + NLMSG_HDRLEN;
242 void *nlmsg_tail(
const struct nlmsghdr *nlh)
244 return (
unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
255 return nlh->nlmsg_len - NLMSG_HDRLEN;
258 static int nlmsg_len(
const struct nlmsghdr *nlh)
278 return (
struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
288 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
298 int nlmsg_valid_hdr(
const struct nlmsghdr *nlh,
int hdrlen)
300 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
311 int nlmsg_ok(
const struct nlmsghdr *nlh,
int remaining)
313 return (remaining >= (
int)
sizeof(
struct nlmsghdr) &&
314 nlh->nlmsg_len >=
sizeof(
struct nlmsghdr) &&
315 nlh->nlmsg_len <= remaining);
326 struct nlmsghdr *
nlmsg_next(
struct nlmsghdr *nlh,
int *remaining)
328 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
330 *remaining -= totlen;
332 return (
struct nlmsghdr *) ((
unsigned char *) nlh + totlen);
345 int nlmsg_parse(
struct nlmsghdr *nlh,
int hdrlen,
struct nlattr *tb[],
348 if (!nlmsg_valid_hdr(nlh, hdrlen))
349 return -NLE_MSG_TOOSHORT;
379 if (!nlmsg_valid_hdr(nlh, hdrlen))
380 return -NLE_MSG_TOOSHORT;
393 static struct nl_msg *__nlmsg_alloc(
size_t len)
397 if (len <
sizeof(
struct nlmsghdr))
398 len =
sizeof(
struct nlmsghdr);
400 nm = calloc(1,
sizeof(*nm));
406 nm->nm_nlh = calloc(1, len);
410 memset(nm->nm_nlh, 0,
sizeof(
struct nlmsghdr));
412 nm->nm_protocol = -1;
416 NL_DBG(2,
"msg %p: Allocated new message, maxlen=%zu\n", nm, len);
435 return __nlmsg_alloc(default_msg_size);
443 return __nlmsg_alloc(max);
462 struct nlmsghdr *
new = nm->nm_nlh;
464 new->nlmsg_type = hdr->nlmsg_type;
465 new->nlmsg_flags = hdr->nlmsg_flags;
466 new->nlmsg_seq = hdr->nlmsg_seq;
467 new->nlmsg_pid = hdr->nlmsg_pid;
483 struct nlmsghdr nlh = {
484 .nlmsg_type = nlmsgtype,
485 .nlmsg_flags = flags,
490 NL_DBG(2,
"msg %p: Allocated new simple message\n", msg);
504 default_msg_size = max;
520 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
524 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
546 void *buf = n->nm_nlh;
547 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
550 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
552 if ((tlen + nlmsg_len) > n->nm_size)
556 n->nm_nlh->nlmsg_len += tlen;
559 memset(buf + len, 0, tlen - len);
561 NL_DBG(2,
"msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n",
562 n, tlen, len, pad, n->nm_nlh->nlmsg_len);
587 memcpy(tmp, data, len);
588 NL_DBG(2,
"msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
611 if (newlen <= n->nm_size)
614 tmp = realloc(n->nm_nlh, newlen);
640 struct nlmsghdr *
nlmsg_put(
struct nl_msg *n, uint32_t pid, uint32_t seq,
641 int type,
int payload,
int flags)
643 struct nlmsghdr *nlh;
645 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
648 nlh = (
struct nlmsghdr *) n->nm_nlh;
649 nlh->nlmsg_type = type;
650 nlh->nlmsg_flags = flags;
651 nlh->nlmsg_pid = pid;
652 nlh->nlmsg_seq = seq;
654 NL_DBG(2,
"msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
655 "seq=%d\n", n, type, flags, pid, seq);
685 NL_DBG(4,
"New reference to message %p, total %d\n",
686 msg, msg->nm_refcnt);
701 NL_DBG(4,
"Returned message reference %p, %d remaining\n",
702 msg, msg->nm_refcnt);
704 if (msg->nm_refcnt < 0)
707 if (msg->nm_refcnt <= 0) {
710 NL_DBG(2,
"msg %p: Freed\n", msg);
721 void nlmsg_set_proto(
struct nl_msg *msg,
int protocol)
723 msg->nm_protocol = protocol;
726 int nlmsg_get_proto(
struct nl_msg *msg)
728 return msg->nm_protocol;
731 size_t nlmsg_get_max_size(
struct nl_msg *msg)
736 void nlmsg_set_src(
struct nl_msg *msg,
struct sockaddr_nl *addr)
738 memcpy(&msg->nm_src, addr,
sizeof(*addr));
741 struct sockaddr_nl *nlmsg_get_src(
struct nl_msg *msg)
746 void nlmsg_set_dst(
struct nl_msg *msg,
struct sockaddr_nl *addr)
748 memcpy(&msg->nm_dst, addr,
sizeof(*addr));
751 struct sockaddr_nl *nlmsg_get_dst(
struct nl_msg *msg)
756 void nlmsg_set_creds(
struct nl_msg *msg,
struct ucred *creds)
758 memcpy(&msg->nm_creds, creds,
sizeof(*creds));
759 msg->nm_flags |= NL_MSG_CRED_PRESENT;
762 struct ucred *nlmsg_get_creds(
struct nl_msg *msg)
764 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
765 return &msg->nm_creds;
776 static const struct trans_tbl nl_msgtypes[] = {
777 __ADD(NLMSG_NOOP,NOOP)
778 __ADD(NLMSG_ERROR,ERROR)
779 __ADD(NLMSG_DONE,DONE)
780 __ADD(NLMSG_OVERRUN,OVERRUN)
783 char *nl_nlmsgtype2str(
int type,
char *buf,
size_t size)
785 return __type2str(type, buf, size, nl_msgtypes,
786 ARRAY_SIZE(nl_msgtypes));
789 int nl_str2nlmsgtype(
const char *name)
791 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
801 char *nl_nlmsg_flags2str(
int flags,
char *buf,
size_t len)
805 #define PRINT_FLAG(f) \
806 if (flags & NLM_F_##f) { \
807 flags &= ~NLM_F_##f; \
808 strncat(buf, #f, len - strlen(buf) - 1); \
810 strncat(buf, ",", len - strlen(buf) - 1); \
827 snprintf(s,
sizeof(s),
"0x%x", flags);
828 strncat(buf, s, len - strlen(buf) - 1);
851 struct dp_xdata *x = p->
pp_arg;
857 int nl_msg_parse(
struct nl_msg *msg,
void (*cb)(
struct nl_object *,
void *),
864 struct dp_xdata x = {
872 return -NLE_MSGTYPE_NOSUPPORT;
875 return nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
885 static void prefix_line(FILE *ofd,
int prefix)
889 for (i = 0; i < prefix; i++)
893 static inline void dump_hex(FILE *ofd,
char *start,
int len,
int prefix)
896 char ascii[21] = {0};
898 limit = 18 - (prefix * 2);
899 prefix_line(ofd, prefix);
902 for (i = 0, a = 0, c = 0; i < len; i++) {
903 int v = *(uint8_t *) (start + i);
905 fprintf(ofd,
"%02x ", v);
906 ascii[a++] = isprint(v) ? v :
'.';
909 fprintf(ofd,
"%s\n", ascii);
911 prefix_line(ofd, prefix);
915 memset(ascii, 0,
sizeof(ascii));
921 for (i = 0; i < (limit - c); i++)
923 fprintf(ofd,
"%s\n", ascii);
927 static void print_hdr(FILE *ofd,
struct nl_msg *msg)
934 fprintf(ofd,
" .nlmsg_len = %d\n", nlh->nlmsg_len);
944 nl_nlmsgtype2str(nlh->nlmsg_type, buf,
sizeof(buf));
946 fprintf(ofd,
" .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
947 fprintf(ofd,
" .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
948 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf,
sizeof(buf)));
949 fprintf(ofd,
" .nlmsg_seq = %d\n", nlh->nlmsg_seq);
950 fprintf(ofd,
" .nlmsg_pid = %d\n", nlh->nlmsg_pid);
954 static void dump_attrs(FILE *ofd,
struct nlattr *attrs,
int attrlen,
961 int padlen, alen =
nla_len(nla);
963 prefix_line(ofd, prefix);
964 fprintf(ofd,
" [ATTR %02d%s] %d octets\n",
nla_type(nla),
965 nla->nla_type & NLA_F_NESTED ?
" NESTED" :
"",
968 if (nla->nla_type & NLA_F_NESTED)
969 dump_attrs(ofd,
nla_data(nla), alen, prefix+1);
971 dump_hex(ofd,
nla_data(nla), alen, prefix);
975 prefix_line(ofd, prefix);
976 fprintf(ofd,
" [PADDING] %d octets\n",
984 prefix_line(ofd, prefix);
985 fprintf(ofd,
" [LEFTOVER] %d octets\n", rem);
999 "-------------------------- BEGIN NETLINK MESSAGE "
1000 "---------------------------\n");
1002 fprintf(ofd,
" [HEADER] %Zu octets\n",
sizeof(
struct nlmsghdr));
1003 print_hdr(ofd, msg);
1005 if (hdr->nlmsg_type == NLMSG_ERROR &&
1006 hdr->nlmsg_len >= nlmsg_msg_size(
sizeof(
struct nlmsgerr))) {
1007 struct nl_msg *errmsg;
1010 fprintf(ofd,
" [ERRORMSG] %Zu octets\n",
sizeof(*err));
1011 fprintf(ofd,
" .error = %d \"%s\"\n", err->error,
1012 strerror(-err->error));
1013 fprintf(ofd,
" [ORIGINAL MESSAGE] %Zu octets\n",
sizeof(*hdr));
1016 print_hdr(ofd, errmsg);
1018 }
else if (nlmsg_len(hdr) > 0) {
1020 int payloadlen = nlmsg_len(hdr);
1027 payloadlen -= attrlen;
1030 fprintf(ofd,
" [PAYLOAD] %d octets\n", payloadlen);
1031 dump_hex(ofd,
nlmsg_data(hdr), payloadlen, 0);
1034 struct nlattr *attrs;
1039 dump_attrs(ofd, attrs, attrlen, 0);
1044 "--------------------------- END NETLINK MESSAGE "
1045 "---------------------------\n");