Blame src/nlmsg.c

Packit Service a5df35
/*
Packit Service a5df35
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
Packit Service a5df35
 *
Packit Service a5df35
 * This program is free software; you can redistribute it and/or modify
Packit Service a5df35
 * it under the terms of the GNU General Public License as published
Packit Service a5df35
 * by the Free Software Foundation; either version 2 of the License, or
Packit Service a5df35
 * (at your option) any later version.
Packit Service a5df35
 */
Packit Service a5df35
#include <arpa/inet.h>
Packit Service a5df35
#include <time.h>
Packit Service a5df35
#include <endian.h>
Packit Service a5df35
#include <stdlib.h>
Packit Service a5df35
#include <string.h>
Packit Service a5df35
Packit Service a5df35
#include <libmnl/libmnl.h>
Packit Service a5df35
Packit Service a5df35
#ifndef __aligned_be64
Packit Service a5df35
#define __aligned_be64 __be64 __attribute__((aligned(8)))
Packit Service a5df35
#define __aligned_le64 __le64 __attribute__((aligned(8)))
Packit Service a5df35
#endif
Packit Service a5df35
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
#include "internal.h"
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * \defgroup nfq_verd Verdict helpers
Packit Service a5df35
 * @{
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_verdict_put - Put a verdict into a Netlink message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param id ID assigned to packet by netfilter
Packit Service a5df35
 * \param verdict verdict to return to netfilter (see \b Verdicts below)
Packit Service a5df35
 * \par Verdicts
Packit Service a5df35
 * __NF_DROP__ Drop the packet. This is final.
Packit Service a5df35
 * \n
Packit Service a5df35
 * __NF_ACCEPT__ Accept the packet. Processing of the current base chain
Packit Service a5df35
 * and any called chains terminates,
Packit Service a5df35
 * but the packet may still be processed by subsequently invoked base chains.
Packit Service a5df35
 * \n
Packit Service a5df35
 * __NF_STOP__ Like __NF_ACCEPT__, but skip any further base chains using the
Packit Service a5df35
 * current hook.
Packit Service a5df35
 * \n
Packit Service a5df35
 * __NF_REPEAT__ Like __NF_ACCEPT__, but re-queue this packet to the
Packit Service a5df35
 * current base chain. One way to prevent a re-queueing loop is to
Packit Service a5df35
 * also set a packet mark using nfq_nlmsg_verdict_put_mark() and have the
Packit Service a5df35
 * program test for this mark in \c attr[NFQA_MARK]; or have the nefilter rules
Packit Service a5df35
 * do this test.
Packit Service a5df35
 * \n
Packit Service a5df35
 * __NF_QUEUE_NR__(*new_queue*) Like __NF_ACCEPT__, but queue this packet to
Packit Service a5df35
 * queue number *new_queue*. As with the command-line \b queue \b num verdict,
Packit Service a5df35
 * if no process is listening to that queue then the packet is discarded; but
Packit Service a5df35
 * again like with the command-line, one may OR in a flag to bypass *new_queue*
Packit Service a5df35
 *  if there is no listener, as in this snippet:
Packit Service a5df35
 * \verbatim
Packit Service a5df35
       nfq_nlmsg_verdict_put(nlh, id, NF_QUEUE_NR(new_queue) |
Packit Service a5df35
	       NF_VERDICT_FLAG_QUEUE_BYPASS);
Packit Service a5df35
\endverbatim
Packit Service a5df35
 *
Packit Service a5df35
 * See examples/nf-queue.c, line
Packit Service a5df35
 * 46
Packit Service a5df35
 * for an example of how to use this function in context.
Packit Service a5df35
 * The calling sequence is \b main --> \b mnl_cb_run --> \b queue_cb -->
Packit Service a5df35
 * \b nfq_send_verdict --> \b nfq_nlmsg_verdict_put
Packit Service a5df35
 * (\b cb being short for \b callback).
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void nfq_nlmsg_verdict_put(struct nlmsghdr *nlh, int id, int verdict)
Packit Service a5df35
{
Packit Service a5df35
	struct nfqnl_msg_verdict_hdr vh = {
Packit Service a5df35
		.verdict	= htonl(verdict),
Packit Service a5df35
		.id		= htonl(id),
Packit Service a5df35
	};
Packit Service a5df35
	mnl_attr_put(nlh, NFQA_VERDICT_HDR, sizeof(vh), &vh;;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_verdict_put_mark - Put a packet mark into a netlink message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param mark Value of mark to put
Packit Service a5df35
 *
Packit Service a5df35
 * The mark becomes part of the packet's metadata, and may be tested by the *nft
Packit Service a5df35
 * primary expression* **meta mark**
Packit Service a5df35
 * \sa __nft__(1)
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void nfq_nlmsg_verdict_put_mark(struct nlmsghdr *nlh, uint32_t mark)
Packit Service a5df35
{
Packit Service a5df35
	mnl_attr_put_u32(nlh, NFQA_MARK, htonl(mark));
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_verdict_put_pkt - Put replacement packet content into a netlink
Packit Service a5df35
 * message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param pkt Pointer to start of modified IP datagram
Packit Service a5df35
 * \param plen Length of modified IP datagram
Packit Service a5df35
 *
Packit Service a5df35
 * There is only ever a need to return packet content if it has been modified.
Packit Service a5df35
 * Usually one of the nfq_*_mangle_* functions does the modifying.
Packit Service a5df35
 *
Packit Service a5df35
 * This code snippet uses nfq_udp_mangle_ipv4. See nf-queue.c for
Packit Service a5df35
 * context:
Packit Service a5df35
 * \verbatim
Packit Service a5df35
// main calls queue_cb (line 64) to process an enqueued packet:
Packit Service a5df35
	// Extra variables
Packit Service a5df35
	uint8_t *payload, *rep_data;
Packit Service a5df35
	unsigned int match_offset, match_len, rep_len;
Packit Service a5df35
Packit Service a5df35
	// The next line was commented-out (with payload void*)
Packit Service a5df35
	payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
Packit Service a5df35
	// Copy data to a packet buffer (allow 255 bytes for mangling).
Packit Service a5df35
	pktb = pktb_alloc(AF_INET, payload, plen, 255);
Packit Service a5df35
	// (decide that this packet needs mangling)
Packit Service a5df35
	nfq_udp_mangle_ipv4(pktb, match_offset, match_len, rep_data, rep_len);
Packit Service a5df35
	// nfq_udp_mangle_ipv4 updates packet length, no need to track locally
Packit Service a5df35
Packit Service a5df35
	// Eventually nfq_send_verdict (line 39) gets called
Packit Service a5df35
	// The received packet may or may not have been modified.
Packit Service a5df35
	// Add this code before nfq_nlmsg_verdict_put call:
Packit Service a5df35
	if (pktb_mangled(pktb))
Packit Service a5df35
		nfq_nlmsg_verdict_put_pkt(nlh, pktb_data(pktb), pktb_len(pktb));
Packit Service a5df35
\endverbatim
Packit Service a5df35
 */
Packit Service a5df35
void nfq_nlmsg_verdict_put_pkt(struct nlmsghdr *nlh, const void *pkt,
Packit Service a5df35
			       uint32_t plen)
Packit Service a5df35
{
Packit Service a5df35
	mnl_attr_put(nlh, NFQA_PAYLOAD, plen, pkt);
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * @}
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * \defgroup nfq_cfg Config helpers
Packit Service a5df35
 * @{
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_cfg_put_cmd Add netlink config command to netlink message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param pf Packet family (e.g. AF_INET)
Packit Service a5df35
 * \param cmd nfqueue nfnetlink command.
Packit Service a5df35
 *
Packit Service a5df35
 * Possible commands are:
Packit Service a5df35
 *
Packit Service a5df35
 * - NFQNL_CFG_CMD_NONE: Do nothing. It can be useful to know if the queue
Packit Service a5df35
 *   subsystem is working.
Packit Service a5df35
 * - NFQNL_CFG_CMD_BIND: Binds the program to a specific queue.
Packit Service a5df35
 * - NFQNL_CFG_CMD_UNBIND: Unbinds the program to a specifiq queue.
Packit Service a5df35
 *
Packit Service a5df35
 * Obsolete commands:
Packit Service a5df35
 * - NFQNL_CFG_CMD_PF_BIND: Binds to process packets belonging to the given
Packit Service a5df35
 *   protocol family (ie. PF_INET, PF_INET6, etc).
Packit Service a5df35
 * - NFQNL_CFG_CMD_PF_UNBIND: Unbinds from processing packets belonging to the
Packit Service a5df35
 *   given protocol family.  Both commands are ignored by Linux kernel 3.8 and
Packit Service a5df35
 *   later versions.
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void nfq_nlmsg_cfg_put_cmd(struct nlmsghdr *nlh, uint16_t pf, uint8_t cmd)
Packit Service a5df35
{
Packit Service a5df35
	struct nfqnl_msg_config_cmd command = {
Packit Service a5df35
		.command = cmd,
Packit Service a5df35
		.pf = htons(pf),
Packit Service a5df35
	};
Packit Service a5df35
	mnl_attr_put(nlh, NFQA_CFG_CMD, sizeof(command), &command);
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_cfg_put_params Add parameter to netlink message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param mode one of NFQNL_COPY_NONE, NFQNL_COPY_META or NFQNL_COPY_PACKET
Packit Service a5df35
 * \param range value of parameter
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void nfq_nlmsg_cfg_put_params(struct nlmsghdr *nlh, uint8_t mode, int range)
Packit Service a5df35
{
Packit Service a5df35
	struct nfqnl_msg_config_params params = {
Packit Service a5df35
		.copy_range = htonl(range),
Packit Service a5df35
		.copy_mode = mode,
Packit Service a5df35
	};
Packit Service a5df35
	mnl_attr_put(nlh, NFQA_CFG_PARAMS, sizeof(params), &params);
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_cfg_put_qmaxlen Add queue maximum length to netlink message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param queue_maxlen Maximum queue length
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void nfq_nlmsg_cfg_put_qmaxlen(struct nlmsghdr *nlh, uint32_t queue_maxlen)
Packit Service a5df35
{
Packit Service a5df35
	mnl_attr_put_u32(nlh, NFQA_CFG_QUEUE_MAXLEN, htonl(queue_maxlen));
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * @}
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * \defgroup nlmsg Netlink message helper functions
Packit Service a5df35
 * @{
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
static int nfq_pkt_parse_attr_cb(const struct nlattr *attr, void *data)
Packit Service a5df35
{
Packit Service a5df35
	const struct nlattr **tb = data;
Packit Service a5df35
	int type = mnl_attr_get_type(attr);
Packit Service a5df35
Packit Service a5df35
	/* skip unsupported attribute in user-space */
Packit Service a5df35
	if (mnl_attr_type_valid(attr, NFQA_MAX) < 0)
Packit Service a5df35
		return MNL_CB_OK;
Packit Service a5df35
Packit Service a5df35
	switch(type) {
Packit Service a5df35
	case NFQA_MARK:
Packit Service a5df35
	case NFQA_IFINDEX_INDEV:
Packit Service a5df35
	case NFQA_IFINDEX_OUTDEV:
Packit Service a5df35
	case NFQA_IFINDEX_PHYSINDEV:
Packit Service a5df35
	case NFQA_IFINDEX_PHYSOUTDEV:
Packit Service a5df35
	case NFQA_CAP_LEN:
Packit Service a5df35
	case NFQA_SKB_INFO:
Packit Service a5df35
	case NFQA_SECCTX:
Packit Service a5df35
	case NFQA_UID:
Packit Service a5df35
	case NFQA_GID:
Packit Service a5df35
	case NFQA_CT_INFO:
Packit Service a5df35
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
Packit Service a5df35
			return MNL_CB_ERROR;
Packit Service a5df35
		break;
Packit Service a5df35
	case NFQA_TIMESTAMP:
Packit Service a5df35
		if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
Packit Service a5df35
		    sizeof(struct nfqnl_msg_packet_timestamp)) < 0) {
Packit Service a5df35
			return MNL_CB_ERROR;
Packit Service a5df35
		}
Packit Service a5df35
		break;
Packit Service a5df35
	case NFQA_HWADDR:
Packit Service a5df35
		if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
Packit Service a5df35
		    sizeof(struct nfqnl_msg_packet_hw)) < 0) {
Packit Service a5df35
			return MNL_CB_ERROR;
Packit Service a5df35
		}
Packit Service a5df35
		break;
Packit Service a5df35
	case NFQA_PACKET_HDR:
Packit Service a5df35
		if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
Packit Service a5df35
		    sizeof(struct nfqnl_msg_packet_hdr)) < 0) {
Packit Service a5df35
			return MNL_CB_ERROR;
Packit Service a5df35
		}
Packit Service a5df35
		break;
Packit Service a5df35
	case NFQA_PAYLOAD:
Packit Service a5df35
	case NFQA_CT:
Packit Service a5df35
	case NFQA_EXP:
Packit Service a5df35
		break;
Packit Service a5df35
	}
Packit Service a5df35
	tb[type] = attr;
Packit Service a5df35
	return MNL_CB_OK;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_parse - set packet attributes from netlink message
Packit Service a5df35
 * \param nlh Pointer to netlink message
Packit Service a5df35
 * \param attr Pointer to array of attributes to set
Packit Service a5df35
 * \returns MNL_CB_OK on success or MNL_CB_ERROR if any error occurs
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
Packit Service a5df35
{
Packit Service a5df35
	return mnl_attr_parse(nlh, sizeof(struct nfgenmsg),
Packit Service a5df35
			      nfq_pkt_parse_attr_cb, attr);
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * nfq_nlmsg_put - Convert memory buffer into a Netlink buffer
Packit Service a5df35
 * \param *buf Pointer to memory buffer
Packit Service a5df35
 * \param type Either NFQNL_MSG_CONFIG or NFQNL_MSG_VERDICT
Packit Service a5df35
 * \param queue_num Queue number
Packit Service a5df35
 * \returns Pointer to netlink message
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
struct nlmsghdr *nfq_nlmsg_put(char *buf, int type, uint32_t queue_num)
Packit Service a5df35
{
Packit Service a5df35
	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
Packit Service a5df35
	nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | type;
Packit Service a5df35
	nlh->nlmsg_flags = NLM_F_REQUEST;
Packit Service a5df35
Packit Service a5df35
	struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
Packit Service a5df35
	nfg->nfgen_family = AF_UNSPEC;
Packit Service a5df35
	nfg->version = NFNETLINK_V0;
Packit Service a5df35
	nfg->res_id = htons(queue_num);
Packit Service a5df35
Packit Service a5df35
	return nlh;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * @}
Packit Service a5df35
 */