Blame examples/nf-queue.c

Packit Service a5df35
#include <errno.h>
Packit Service a5df35
#include <stdio.h>
Packit Service a5df35
#include <stdlib.h>
Packit Service a5df35
#include <unistd.h>
Packit Service a5df35
#include <string.h>
Packit Service a5df35
#include <time.h>
Packit Service a5df35
#include <arpa/inet.h>
Packit Service a5df35
Packit Service a5df35
#include <libmnl/libmnl.h>
Packit Service a5df35
#include <linux/netfilter.h>
Packit Service a5df35
#include <linux/netfilter/nfnetlink.h>
Packit Service a5df35
Packit Service a5df35
#include <linux/types.h>
Packit Service a5df35
#include <linux/netfilter/nfnetlink_queue.h>
Packit Service a5df35
Packit Service a5df35
#include <libnetfilter_queue/libnetfilter_queue.h>
Packit Service a5df35
Packit Service a5df35
/* only for NFQA_CT, not needed otherwise: */
Packit Service a5df35
#include <linux/netfilter/nfnetlink_conntrack.h>
Packit Service a5df35
Packit Service a5df35
static struct mnl_socket *nl;
Packit Service a5df35
Packit Service a5df35
static void
Packit Service a5df35
nfq_send_verdict(int queue_num, uint32_t id)
Packit Service a5df35
{
Packit Service a5df35
	char buf[MNL_SOCKET_BUFFER_SIZE];
Packit Service a5df35
	struct nlmsghdr *nlh;
Packit Service a5df35
	struct nlattr *nest;
Packit Service a5df35
Packit Service a5df35
	nlh = nfq_nlmsg_put(buf, NFQNL_MSG_VERDICT, queue_num);
Packit Service a5df35
	nfq_nlmsg_verdict_put(nlh, id, NF_ACCEPT);
Packit Service a5df35
Packit Service a5df35
	/* example to set the connmark. First, start NFQA_CT section: */
Packit Service a5df35
	nest = mnl_attr_nest_start(nlh, NFQA_CT);
Packit Service a5df35
Packit Service a5df35
	/* then, add the connmark attribute: */
Packit Service a5df35
	mnl_attr_put_u32(nlh, CTA_MARK, htonl(42));
Packit Service a5df35
	/* more conntrack attributes, e.g. CTA_LABELS could be set here */
Packit Service a5df35
Packit Service a5df35
	/* end conntrack section */
Packit Service a5df35
	mnl_attr_nest_end(nlh, nest);
Packit Service a5df35
Packit Service a5df35
	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
Packit Service a5df35
		perror("mnl_socket_send");
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
static int queue_cb(const struct nlmsghdr *nlh, void *data)
Packit Service a5df35
{
Packit Service a5df35
	struct nfqnl_msg_packet_hdr *ph = NULL;
Packit Service a5df35
	struct nlattr *attr[NFQA_MAX+1] = {};
Packit Service a5df35
	uint32_t id = 0, skbinfo;
Packit Service a5df35
	struct nfgenmsg *nfg;
Packit Service a5df35
	uint16_t plen;
Packit Service a5df35
Packit Service a5df35
	if (nfq_nlmsg_parse(nlh, attr) < 0) {
Packit Service a5df35
		perror("problems parsing");
Packit Service a5df35
		return MNL_CB_ERROR;
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	nfg = mnl_nlmsg_get_payload(nlh);
Packit Service a5df35
Packit Service a5df35
	if (attr[NFQA_PACKET_HDR] == NULL) {
Packit Service a5df35
		fputs("metaheader not set\n", stderr);
Packit Service a5df35
		return MNL_CB_ERROR;
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
Packit Service a5df35
Packit Service a5df35
	plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
Packit Service a5df35
	/* void *payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); */
Packit Service a5df35
Packit Service a5df35
	skbinfo = attr[NFQA_SKB_INFO] ? ntohl(mnl_attr_get_u32(attr[NFQA_SKB_INFO])) : 0;
Packit Service a5df35
Packit Service a5df35
	if (attr[NFQA_CAP_LEN]) {
Packit Service a5df35
		uint32_t orig_len = ntohl(mnl_attr_get_u32(attr[NFQA_CAP_LEN]));
Packit Service a5df35
		if (orig_len != plen)
Packit Service a5df35
			printf("truncated ");
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	if (skbinfo & NFQA_SKB_GSO)
Packit Service a5df35
		printf("GSO ");
Packit Service a5df35
Packit Service a5df35
	id = ntohl(ph->packet_id);
Packit Service a5df35
	printf("packet received (id=%u hw=0x%04x hook=%u, payload len %u",
Packit Service a5df35
		id, ntohs(ph->hw_protocol), ph->hook, plen);
Packit Service a5df35
Packit Service a5df35
	/*
Packit Service a5df35
	 * ip/tcp checksums are not yet valid, e.g. due to GRO/GSO.
Packit Service a5df35
	 * The application should behave as if the checksums are correct.
Packit Service a5df35
	 *
Packit Service a5df35
	 * If these packets are later forwarded/sent out, the checksums will
Packit Service a5df35
	 * be corrected by kernel/hardware.
Packit Service a5df35
	 */
Packit Service a5df35
	if (skbinfo & NFQA_SKB_CSUMNOTREADY)
Packit Service a5df35
		printf(", checksum not ready");
Packit Service a5df35
	puts(")");
Packit Service a5df35
Packit Service a5df35
	nfq_send_verdict(ntohs(nfg->res_id), id);
Packit Service a5df35
Packit Service a5df35
	return MNL_CB_OK;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
int main(int argc, char *argv[])
Packit Service a5df35
{
Packit Service a5df35
	char *buf;
Packit Service a5df35
	/* largest possible packet payload, plus netlink data overhead: */
Packit Service a5df35
	size_t sizeof_buf = 0xffff + (MNL_SOCKET_BUFFER_SIZE/2);
Packit Service a5df35
	struct nlmsghdr *nlh;
Packit Service a5df35
	int ret;
Packit Service a5df35
	unsigned int portid, queue_num;
Packit Service a5df35
Packit Service a5df35
	if (argc != 2) {
Packit Service a5df35
		printf("Usage: %s [queue_num]\n", argv[0]);
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
	queue_num = atoi(argv[1]);
Packit Service a5df35
Packit Service a5df35
	nl = mnl_socket_open(NETLINK_NETFILTER);
Packit Service a5df35
	if (nl == NULL) {
Packit Service a5df35
		perror("mnl_socket_open");
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
Packit Service a5df35
		perror("mnl_socket_bind");
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
	portid = mnl_socket_get_portid(nl);
Packit Service a5df35
Packit Service a5df35
	buf = malloc(sizeof_buf);
Packit Service a5df35
	if (!buf) {
Packit Service a5df35
		perror("allocate receive buffer");
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
Packit Service a5df35
	nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_BIND);
Packit Service a5df35
Packit Service a5df35
	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
Packit Service a5df35
		perror("mnl_socket_send");
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
Packit Service a5df35
	nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
Packit Service a5df35
Packit Service a5df35
	mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO));
Packit Service a5df35
	mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO));
Packit Service a5df35
Packit Service a5df35
	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
Packit Service a5df35
		perror("mnl_socket_send");
Packit Service a5df35
		exit(EXIT_FAILURE);
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	/* ENOBUFS is signalled to userspace when packets were lost
Packit Service a5df35
	 * on kernel side.  In most cases, userspace isn't interested
Packit Service a5df35
	 * in this information, so turn it off.
Packit Service a5df35
	 */
Packit Service a5df35
	ret = 1;
Packit Service a5df35
	mnl_socket_setsockopt(nl, NETLINK_NO_ENOBUFS, &ret, sizeof(int));
Packit Service a5df35
Packit Service a5df35
	for (;;) {
Packit Service a5df35
		ret = mnl_socket_recvfrom(nl, buf, sizeof_buf);
Packit Service a5df35
		if (ret == -1) {
Packit Service a5df35
			perror("mnl_socket_recvfrom");
Packit Service a5df35
			exit(EXIT_FAILURE);
Packit Service a5df35
		}
Packit Service a5df35
Packit Service a5df35
		ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
Packit Service a5df35
		if (ret < 0){
Packit Service a5df35
			perror("mnl_cb_run");
Packit Service a5df35
			exit(EXIT_FAILURE);
Packit Service a5df35
		}
Packit Service a5df35
	}
Packit Service a5df35
Packit Service a5df35
	mnl_socket_close(nl);
Packit Service a5df35
Packit Service a5df35
	return 0;
Packit Service a5df35
}