summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/netfilter/Makefile.am4
-rw-r--r--examples/netfilter/nf-log.c229
-rw-r--r--examples/rtnl/rtnl-link-set.c3
-rw-r--r--include/libmnl/libmnl.h4
-rw-r--r--src/nlmsg.c156
5 files changed, 369 insertions, 27 deletions
diff --git a/examples/netfilter/Makefile.am b/examples/netfilter/Makefile.am
index 927eb38..96466e0 100644
--- a/examples/netfilter/Makefile.am
+++ b/examples/netfilter/Makefile.am
@@ -1,10 +1,14 @@
include $(top_srcdir)/Make_global.am
check_PROGRAMS = nf-queue \
+ nf-log \
nfct-event
nf_queue_SOURCES = nf-queue.c
nf_queue_LDADD = ../../src/libmnl.la
+nf_log_SOURCES = nf-log.c
+nf_log_LDADD = ../../src/libmnl.la
+
nfct_event_SOURCES = nfct-event.c
nfct_event_LDADD = ../../src/libmnl.la
diff --git a/examples/netfilter/nf-log.c b/examples/netfilter/nf-log.c
new file mode 100644
index 0000000..8f0a877
--- /dev/null
+++ b/examples/netfilter/nf-log.c
@@ -0,0 +1,229 @@
+/*
+ * (C) 2010 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This software may be used and distributed according to the terms
+ * of the GNU General Public License, incorporated herein by reference.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <arpa/inet.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/netfilter.h>
+#include <linux/netfilter/nfnetlink.h>
+
+#ifndef aligned_be64
+#define aligned_be64 u_int64_t __attribute__((aligned(8)))
+#endif
+
+#include <linux/netfilter/nfnetlink_log.h>
+
+static int parse_attr_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ /* skip unsupported attribute in user-space */
+ if (mnl_attr_type_valid(attr, NFULA_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFULA_MARK:
+ case NFULA_IFINDEX_INDEV:
+ case NFULA_IFINDEX_OUTDEV:
+ case NFULA_IFINDEX_PHYSINDEV:
+ case NFULA_IFINDEX_PHYSOUTDEV:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case NFULA_TIMESTAMP:
+ if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
+ sizeof(struct nfulnl_msg_packet_timestamp)) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case NFULA_HWADDR:
+ if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
+ sizeof(struct nfulnl_msg_packet_hw)) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case NFULA_PREFIX:
+ if (mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case NFULA_PAYLOAD:
+ break;
+ }
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int log_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nlattr *tb[NFULA_MAX+1] = {};
+ struct nfulnl_msg_packet_hdr *ph = NULL;
+ const char *prefix = NULL;
+ uint32_t mark = 0;
+
+ mnl_attr_parse(nlh, sizeof(struct nfgenmsg), parse_attr_cb, tb);
+ if (tb[NFULA_PACKET_HDR])
+ ph = mnl_attr_get_payload(tb[NFULA_PACKET_HDR]);
+ if (tb[NFULA_PREFIX])
+ prefix = mnl_attr_get_str(tb[NFULA_PREFIX]);
+ if (tb[NFULA_MARK])
+ mark = ntohl(mnl_attr_get_u32(tb[NFULA_MARK]));
+
+ printf("log received (prefix=\"%s\" hw=0x%04x hook=%u mark=%u)\n",
+ prefix ? prefix : "", ntohs(ph->hw_protocol), ph->hook,
+ mark);
+
+ return MNL_CB_OK;
+}
+
+static struct nlmsghdr *
+nflog_build_cfg_pf_request(char *buf, uint8_t command)
+{
+ struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
+ nlh->nlmsg_flags = NLM_F_REQUEST;
+
+ struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
+ nfg->nfgen_family = AF_INET;
+ nfg->version = NFNETLINK_V0;
+
+ struct nfulnl_msg_config_cmd cmd = {
+ .command = command,
+ };
+ mnl_attr_put(nlh, NFULA_CFG_CMD, sizeof(cmd), &cmd);
+
+ return nlh;
+}
+
+static struct nlmsghdr *
+nflog_build_cfg_request(char *buf, uint8_t command, int qnum)
+{
+ struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
+ nlh->nlmsg_flags = NLM_F_REQUEST;
+
+ struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
+ nfg->nfgen_family = AF_INET;
+ nfg->version = NFNETLINK_V0;
+ nfg->res_id = htons(qnum);
+
+ struct nfulnl_msg_config_cmd cmd = {
+ .command = command,
+ };
+ mnl_attr_put(nlh, NFULA_CFG_CMD, sizeof(cmd), &cmd);
+
+ return nlh;
+}
+
+static struct nlmsghdr *
+nflog_build_cfg_params(char *buf, uint8_t mode, int range, int qnum)
+{
+ struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
+ nlh->nlmsg_flags = NLM_F_REQUEST;
+
+ struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
+ nfg->nfgen_family = AF_UNSPEC;
+ nfg->version = NFNETLINK_V0;
+ nfg->res_id = htons(qnum);
+
+ struct nfulnl_msg_config_mode params = {
+ .copy_range = htonl(range),
+ .copy_mode = mode,
+ };
+ mnl_attr_put(nlh, NFULA_CFG_MODE, sizeof(params), &params);
+
+ return nlh;
+}
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[MNL_SOCKET_BUFFER_SIZE];
+ struct nlmsghdr *nlh;
+ int ret;
+ unsigned int portid, qnum;
+
+ if (argc != 2) {
+ printf("Usage: %s [queue_num]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ qnum = atoi(argv[1]);
+
+ nl = mnl_socket_open(NETLINK_NETFILTER);
+ if (nl == NULL) {
+ perror("mnl_socket_open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
+ perror("mnl_socket_bind");
+ exit(EXIT_FAILURE);
+ }
+ portid = mnl_socket_get_portid(nl);
+
+ nlh = nflog_build_cfg_pf_request(buf, NFULNL_CFG_CMD_PF_UNBIND);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_send");
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = nflog_build_cfg_pf_request(buf, NFULNL_CFG_CMD_PF_BIND);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_send");
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = nflog_build_cfg_request(buf, NFULNL_CFG_CMD_BIND, qnum);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_send");
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = nflog_build_cfg_params(buf, NFULNL_COPY_PACKET, 0xFFFF, qnum);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_send");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ if (ret == -1) {
+ perror("mnl_socket_recvfrom");
+ exit(EXIT_FAILURE);
+ }
+ while (ret > 0) {
+ ret = mnl_cb_run(buf, ret, 0, portid, log_cb, NULL);
+ if (ret < 0){
+ perror("mnl_cb_run");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ if (ret == -1) {
+ perror("mnl_socket_recvfrom");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ mnl_socket_close(nl);
+
+ return 0;
+}
diff --git a/examples/rtnl/rtnl-link-set.c b/examples/rtnl/rtnl-link-set.c
index 00afe27..28b376c 100644
--- a/examples/rtnl/rtnl-link-set.c
+++ b/examples/rtnl/rtnl-link-set.c
@@ -63,7 +63,8 @@ int main(int argc, char *argv[])
}
portid = mnl_socket_get_portid(nl);
- mnl_nlmsg_fprintf(stdout, nlh);
+ mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
+ sizeof(struct ifinfomsg));
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_send");
diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h
index 7094af2..74662d3 100644
--- a/include/libmnl/libmnl.h
+++ b/include/libmnl/libmnl.h
@@ -63,8 +63,8 @@ extern void *mnl_nlmsg_get_payload(const struct nlmsghdr *nlh);
extern void *mnl_nlmsg_get_payload_offset(const struct nlmsghdr *nlh, size_t offset);
extern void *mnl_nlmsg_get_payload_tail(const struct nlmsghdr *nlh);
-/* Netlink dump message */
-extern void mnl_nlmsg_fprintf(FILE *fd, const struct nlmsghdr *nlh);
+/* Netlink message printer */
+extern void mnl_nlmsg_fprintf(FILE *fd, const void *data, size_t datalen, size_t extra_header_size);
/*
* Netlink attributes API
diff --git a/src/nlmsg.c b/src/nlmsg.c
index f89a9ec..a3dc0c1 100644
--- a/src/nlmsg.c
+++ b/src/nlmsg.c
@@ -233,37 +233,145 @@ bool mnl_nlmsg_portid_ok(const struct nlmsghdr *nlh, unsigned int portid)
return nlh->nlmsg_pid && portid ? nlh->nlmsg_pid == portid : true;
}
-/**
- * mnl_nlmsg_fprintf - print netlink message to file
- * \param nlh pointer to netlink message that we want to print
- *
- * This function prints the netlink header to a file handle.
- * It may be useful for debugging purposes.
- */
-void mnl_nlmsg_fprintf(FILE *fd, const struct nlmsghdr *nlh)
+static void mnl_nlmsg_fprintf_header(FILE *fd, const struct nlmsghdr *nlh)
{
- size_t i;
+ fprintf(fd, "----------------\t------------------\n");
+ fprintf(fd, "| %.010u |\t| message length |\n", nlh->nlmsg_len);
+ fprintf(fd, "| %.05u | %c%c%c%c |\t| type | flags |\n",
+ nlh->nlmsg_type,
+ nlh->nlmsg_flags & NLM_F_REQUEST ? 'R' : '-',
+ nlh->nlmsg_flags & NLM_F_MULTI ? 'M' : '-',
+ nlh->nlmsg_flags & NLM_F_ACK ? 'A' : '-',
+ nlh->nlmsg_flags & NLM_F_ECHO ? 'E' : '-');
+ fprintf(fd, "| %.010u |\t| sequence number|\n", nlh->nlmsg_seq);
+ fprintf(fd, "| %.010u |\t| port ID |\n", nlh->nlmsg_pid);
+ fprintf(fd, "----------------\t------------------\n");
+}
- fprintf(fd, "========= netlink header ==========\n");
- fprintf(fd, "length(32 bits)=%.08u\n", nlh->nlmsg_len);
- fprintf(fd, "type(16 bits)=%.04u flags(16 bits)=%.04x\n",
- nlh->nlmsg_type, nlh->nlmsg_flags);
- fprintf(fd, "sequence number(32 bits)=%.08x\n", nlh->nlmsg_seq);
- fprintf(fd, "port ID(32 bits)=%.08u\n", nlh->nlmsg_pid);
- fprintf(fd, "===================================\n");
+static void
+mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh,
+ size_t extra_header_size)
+{
+ int rem = 0;
+ unsigned int i;
for (i=sizeof(struct nlmsghdr); i<nlh->nlmsg_len; i+=4) {
char *b = (char *) nlh;
+ struct nlattr *attr = (struct nlattr *) (b+i);
+
+ /* netlink control message. */
+ if (nlh->nlmsg_type < NLMSG_MIN_TYPE) {
+ fprintf(fd, "| %.2x %.2x %.2x %.2x |\t",
+ 0xff & b[i], 0xff & b[i+1],
+ 0xff & b[i+2], 0xff & b[i+3]);
+ fprintf(fd, "| |\n");
+ /* special handling for the extra header. */
+ } else if (extra_header_size > 0) {
+ extra_header_size -= 4;
+ fprintf(fd, "| %.2x %.2x %.2x %.2x |\t",
+ 0xff & b[i], 0xff & b[i+1],
+ 0xff & b[i+2], 0xff & b[i+3]);
+ fprintf(fd, "| extra header |\n");
+ /* this seems like an attribute header. */
+ } else if (rem == 0 && (attr->nla_type & NLA_TYPE_MASK) != 0) {
+ fprintf(fd, "|%c[%d;%dm"
+ "%.5u"
+ "%c[%dm"
+ "|"
+ "%c[%d;%dm"
+ "%c%c"
+ "%c[%dm"
+ "|"
+ "%c[%d;%dm"
+ "%.5u"
+ "%c[%dm|\t",
+ 27, 1, 31,
+ attr->nla_len,
+ 27, 0,
+ 27, 1, 32,
+ attr->nla_type & NLA_F_NESTED ? 'N' : '-',
+ attr->nla_type &
+ NLA_F_NET_BYTEORDER ? 'B' : '-',
+ 27, 0,
+ 27, 1, 34,
+ attr->nla_type & NLA_TYPE_MASK,
+ 27, 0);
+ fprintf(fd, "|len |flags| type|\n");
+
+ if (!(attr->nla_type & NLA_F_NESTED)) {
+ rem = NLA_ALIGN(attr->nla_len) -
+ sizeof(struct nlattr);
+ }
+ /* this is the attribute payload. */
+ } else if (rem > 0) {
+ rem -= 4;
+ fprintf(fd, "| %.2x %.2x %.2x %.2x |\t",
+ 0xff & b[i], 0xff & b[i+1],
+ 0xff & b[i+2], 0xff & b[i+3]);
+ fprintf(fd, "| data |");
+ fprintf(fd, "\t %c %c %c %c\n",
+ isalnum(b[i]) ? b[i] : 0,
+ isalnum(b[i+1]) ? b[i+1] : 0,
+ isalnum(b[i+2]) ? b[i+2] : 0,
+ isalnum(b[i+3]) ? b[i+3] : 0);
+ }
+ }
+ fprintf(fd, "----------------\t------------------\n");
+}
- fprintf(fd, "(%03zu) %.2x %.2x %.2x %.2x | ", i,
- 0xff & b[i], 0xff & b[i+1],
- 0xff & b[i+2], 0xff & b[i+3]);
+/**
+ * mnl_nlmsg_fprintf - print netlink message to file
+ * \param fd pointer to file type
+ * \param data pointer to the buffer that contains messages to be printed
+ * \param datalen length of data stored in the buffer
+ * \param extra_header_size size of the extra header (if any)
+ *
+ * This function prints the netlink header to a file handle.
+ * It may be useful for debugging purposes. One example of the output
+ * is the following:
+ *
+ *\verbatim
+---------------- ------------------
+| 0000000040 | | message length |
+| 00016 | R-A- | | type | flags |
+| 1289148991 | | sequence number|
+| 0000000000 | | port ID |
+---------------- ------------------
+| 00 00 00 00 | | extra header |
+| 00 00 00 00 | | extra header |
+| 01 00 00 00 | | extra header |
+| 01 00 00 00 | | extra header |
+|00008|--|00003| |len |flags| type|
+| 65 74 68 30 | | data | e t h 0
+---------------- ------------------
+\endverbatim
+ *
+ * This example above shows the netlink message that is send to kernel-space
+ * to set up the link interface eth0. The netlink and attribute header data
+ * are displayed in base 10 whereas the extra header and the attribute payload
+ * are expressed in base 16. The possible flags in the netlink header are:
+ *
+ * - R, that indicates that NLM_F_REQUEST is set.
+ * - M, that indicates that NLM_F_MULTI is set.
+ * - A, that indicates that NLM_F_ACK is set.
+ * - E, that indicates that NLM_F_ECHO is set.
+ *
+ * The lack of one flag is displayed with '-'. On the other hand, the possible
+ * attribute flags available are:
+ *
+ * - N, that indicates that NLA_F_NESTED is set.
+ * - B, that indicates that NLA_F_NET_BYTEORDER is set.
+ */
+void mnl_nlmsg_fprintf(FILE *fd, const void *data,
+ size_t datalen, size_t extra_header_size)
+{
+ const struct nlmsghdr *nlh = data;
+ int len = datalen;
- fprintf(fd, "%c %c %c %c\n",
- isalnum(b[i]) ? b[i] : 0,
- isalnum(b[i+1]) ? b[i+1] : 0,
- isalnum(b[i+2]) ? b[i+2] : 0,
- isalnum(b[i+3]) ? b[i+3] : 0);
+ while (mnl_nlmsg_ok(nlh, len)) {
+ mnl_nlmsg_fprintf_header(fd, nlh);
+ mnl_nlmsg_fprintf_payload(fd, nlh, extra_header_size);
+ nlh = mnl_nlmsg_next(nlh, &len);
}
}