Blame src/extra/pktbuff.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 by
Packit Service a5df35
 * 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
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
#include <errno.h>
Packit Service a5df35
#include <stdlib.h>
Packit Service a5df35
#include <string.h> /* for memcpy */
Packit Service a5df35
#include <stdbool.h>
Packit Service a5df35
Packit Service a5df35
#include <netinet/if_ether.h>
Packit Service a5df35
#include <netinet/ip.h>
Packit Service a5df35
#include <netinet/tcp.h>
Packit Service a5df35
Packit Service a5df35
#include "internal.h"
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * \defgroup pktbuff User-space network packet buffer
Packit Service a5df35
 *
Packit Service a5df35
 * This library provides the user-space network packet buffer. This abstraction
Packit Service a5df35
 * is strongly inspired by Linux kernel network buffer, the so-called sk_buff.
Packit Service a5df35
 *
Packit Service a5df35
 * @{
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_alloc - allocate a new packet buffer
Packit Service a5df35
 * \param family Indicate what family. Currently supported families are
Packit Service a5df35
 * AF_BRIDGE, AF_INET & AF_INET6.
Packit Service a5df35
 * \param data Pointer to packet data
Packit Service a5df35
 * \param len Packet length
Packit Service a5df35
 * \param extra Extra memory in the tail to be allocated (for mangling)
Packit Service a5df35
 *
Packit Service a5df35
 * This function returns a packet buffer that contains the packet data and
Packit Service a5df35
 * some extra memory room in the tail (if requested).
Packit Service a5df35
 *
Packit Service a5df35
 * \return Pointer to a new userspace packet buffer or NULL on failure.
Packit Service a5df35
 * \par Errors
Packit Service a5df35
 * __ENOMEM__ From __calloc__()
Packit Service a5df35
 * \n
Packit Service a5df35
 * __EPROTONOSUPPORT__ _family_ was __AF_BRIDGE__ and this is not an IP packet
Packit Service a5df35
 * (v4 or v6)
Packit Service a5df35
 * \sa __calloc__(3)
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)
Packit Service a5df35
{
Packit Service a5df35
	struct pkt_buff *pktb;
Packit Service a5df35
	struct ethhdr *ethhdr;
Packit Service a5df35
	void *pkt_data;
Packit Service a5df35
Packit Service a5df35
	pktb = calloc(1, sizeof(struct pkt_buff) + len + extra);
Packit Service a5df35
	if (pktb == NULL)
Packit Service a5df35
		return NULL;
Packit Service a5df35
Packit Service a5df35
	/* Better make sure alignment is correct. */
Packit Service a5df35
	pkt_data = (uint8_t *)pktb + sizeof(struct pkt_buff);
Packit Service a5df35
	memcpy(pkt_data, data, len);
Packit Service a5df35
Packit Service a5df35
	pktb->len = len;
Packit Service a5df35
	pktb->data_len = len + extra;
Packit Service a5df35
Packit Service a5df35
	pktb->data = pkt_data;
Packit Service a5df35
Packit Service a5df35
	switch(family) {
Packit Service a5df35
	case AF_INET:
Packit Service a5df35
	case AF_INET6:
Packit Service a5df35
		pktb->network_header = pktb->data;
Packit Service a5df35
		break;
Packit Service a5df35
	case AF_BRIDGE:
Packit Service a5df35
		ethhdr = (struct ethhdr *)pktb->data;
Packit Service a5df35
		pktb->mac_header = pktb->data;
Packit Service a5df35
Packit Service a5df35
		switch(ethhdr->h_proto) {
Packit Service a5df35
		case ETH_P_IP:
Packit Service a5df35
		case ETH_P_IPV6:
Packit Service a5df35
			pktb->network_header = pktb->data + ETH_HLEN;
Packit Service a5df35
			break;
Packit Service a5df35
		default:
Packit Service a5df35
			/* This protocol is unsupported. */
Packit Service a5df35
			errno = EPROTONOSUPPORT;
Packit Service a5df35
			free(pktb);
Packit Service a5df35
			return NULL;
Packit Service a5df35
		}
Packit Service a5df35
		break;
Packit Service a5df35
	}
Packit Service a5df35
	return pktb;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_data - get pointer to network packet
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return Pointer to start of network packet data within __pktb__
Packit Service a5df35
 * \par
Packit Service a5df35
 * It is appropriate to use _pktb_data_ as the second argument of
Packit Service a5df35
 * nfq_nlmsg_verdict_put_pkt()
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
uint8_t *pktb_data(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->data;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_len - get length of packet buffer
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return Length of packet contained within __pktb__
Packit Service a5df35
 * \par
Packit Service a5df35
 * It is appropriate to use _pktb_len_ as the third argument of
Packit Service a5df35
 * nfq_nlmsg_verdict_put_pkt()
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
uint32_t pktb_len(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->len;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_free - release packet buffer
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void pktb_free(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	free(pktb);
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * \defgroup otherfns Other functions
Packit Service a5df35
 *
Packit Service a5df35
 * The library provides a number of other functions which many user-space
Packit Service a5df35
 * programs will never need. These divide into 2 groups:
Packit Service a5df35
 * \n
Packit Service a5df35
 * 1. Functions to get values of members of opaque __struct pktbuff__, described
Packit Service a5df35
 * below
Packit Service a5df35
 * \n
Packit Service a5df35
 * 2. Internal functions, described in Module __Internal functions__
Packit Service a5df35
 *
Packit Service a5df35
 * @{
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * \defgroup uselessfns Internal functions
Packit Service a5df35
 *
Packit Service a5df35
 * \warning Do not use these functions. Instead, always use the mangle
Packit Service a5df35
 * function appropriate to the level at which you are working.
Packit Service a5df35
 * \n
Packit Service a5df35
 * pktb_mangle() uses all the below functions except _pktb_pull_, which is not
Packit Service a5df35
 * used by anything.
Packit Service a5df35
 *
Packit Service a5df35
 * @{
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_push - decrement pointer to packet buffer
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \param len Number of bytes to subtract from packet start address
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void pktb_push(struct pkt_buff *pktb, unsigned int len)
Packit Service a5df35
{
Packit Service a5df35
	pktb->data -= len;
Packit Service a5df35
	pktb->len += len;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_pull - increment pointer to packet buffer
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \param len Number of bytes to add to packet start address
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void pktb_pull(struct pkt_buff *pktb, unsigned int len)
Packit Service a5df35
{
Packit Service a5df35
	pktb->data += len;
Packit Service a5df35
	pktb->len -= len;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_put - add extra bytes to the tail of the packet buffer
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \param len Number of bytes to add to packet tail (and length)
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void pktb_put(struct pkt_buff *pktb, unsigned int len)
Packit Service a5df35
{
Packit Service a5df35
	pktb->len += len;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_trim - set new length for this packet buffer
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \param len New packet length (tail is adjusted to reflect this)
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
void pktb_trim(struct pkt_buff *pktb, unsigned int len)
Packit Service a5df35
{
Packit Service a5df35
	pktb->len = len;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * @}
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_tailroom - get room available for packet expansion
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return room in bytes after the tail of the packet buffer
Packit Service a5df35
 * \n
Packit Service a5df35
 * This starts off as the __extra__ argument to pktb_alloc().
Packit Service a5df35
 * Programmers should ensure this __extra__ argument is sufficient for any
Packit Service a5df35
 * packet mangle, as packet buffers cannot be expanded dynamically.
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
unsigned int pktb_tailroom(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->data_len - pktb->len;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_mac_header - get address of layer 2 header (if any)
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return Pointer to MAC header or NULL if no such header present.
Packit Service a5df35
 * \n
Packit Service a5df35
 * Only packet buffers in family __AF_BRIDGE__ have a non-NULL MAC header.
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
uint8_t *pktb_mac_header(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->mac_header;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_network_header - get address of layer 3 header
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return Pointer to layer 3 header or NULL if the packet buffer was created
Packit Service a5df35
 * with an unsupported family
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
uint8_t *pktb_network_header(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->network_header;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_transport_header - get address of layer 4 header (if known)
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return Pointer to layer 4 header or NULL if not (yet) set
Packit Service a5df35
 * \note
Packit Service a5df35
 * Unlike the lower-level headers, it is the programmer's responsibility to
Packit Service a5df35
 * create the level 4 (transport) header pointer by caling e.g.
Packit Service a5df35
 * nfq_ip_set_transport_header()
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
uint8_t *pktb_transport_header(struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->transport_header;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * @}
Packit Service a5df35
 */
Packit Service a5df35
Packit Service a5df35
static int pktb_expand_tail(struct pkt_buff *pktb, int extra)
Packit Service a5df35
{
Packit Service a5df35
	/* No room in packet, cannot mangle it. We don't support dynamic
Packit Service a5df35
	 * reallocation. Instead, increase the size of the extra room in
Packit Service a5df35
	 * the tail in pktb_alloc.
Packit Service a5df35
	 */
Packit Service a5df35
	if (pktb->len + extra > pktb->data_len)
Packit Service a5df35
		return 0;
Packit Service a5df35
Packit Service a5df35
	pktb->len += extra;
Packit Service a5df35
	return 1;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
static int enlarge_pkt(struct pkt_buff *pktb, unsigned int extra)
Packit Service a5df35
{
Packit Service a5df35
	if (pktb->len + extra > 65535)
Packit Service a5df35
		return 0;
Packit Service a5df35
Packit Service a5df35
	if (!pktb_expand_tail(pktb, extra - pktb_tailroom(pktb)))
Packit Service a5df35
		return 0;
Packit Service a5df35
Packit Service a5df35
	return 1;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_mangle - adjust contents of a packet
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \param dataoff Supplementary offset, usually offset from layer 3 (IP) header
Packit Service a5df35
 * to the layer 4 (TCP or UDP) header. Specify zero to access the layer 3
Packit Service a5df35
 * header. If \b pktb was created in family \b AF_BRIDGE, specify
Packit Service a5df35
 * \b -ETH_HLEN (a negative offset) to access the layer 2 (MAC) header.
Packit Service a5df35
 * \param match_offset Further offset to content that you want to mangle
Packit Service a5df35
 * \param match_len Length of the existing content you want to mangle
Packit Service a5df35
 * \param rep_buffer Pointer to data you want to use to replace current content
Packit Service a5df35
 * \param rep_len Length of data you want to use to replace current content
Packit Service a5df35
 * \returns 1 for success and 0 for failure. Failure will occur if the \b extra
Packit Service a5df35
 * argument to the pktb_alloc() call that created \b pktb is less than the
Packit Service a5df35
 * excess of \b rep_len over \b match_len
Packit Service a5df35
 \warning pktb_mangle does not update any checksums. Developers should use the
Packit Service a5df35
 appropriate mangler for the protocol level: nfq_ip_mangle(),
Packit Service a5df35
 nfq_tcp_mangle_ipv4() or nfq_udp_mangle_ipv4(). IPv6 versions are planned.
Packit Service a5df35
 \n
Packit Service a5df35
 It is appropriate to use pktb_mangle to change the MAC header.
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
int pktb_mangle(struct pkt_buff *pktb,
Packit Service a5df35
		int dataoff,
Packit Service a5df35
		unsigned int match_offset,
Packit Service a5df35
		unsigned int match_len,
Packit Service a5df35
		const char *rep_buffer,
Packit Service a5df35
		unsigned int rep_len)
Packit Service a5df35
{
Packit Service a5df35
	unsigned char *data;
Packit Service a5df35
Packit Service a5df35
	if (rep_len > match_len &&
Packit Service a5df35
	    rep_len - match_len > pktb_tailroom(pktb) &&
Packit Service a5df35
	    !enlarge_pkt(pktb, rep_len - match_len))
Packit Service a5df35
		return 0;
Packit Service a5df35
Packit Service a5df35
	data = pktb->network_header + dataoff;
Packit Service a5df35
Packit Service a5df35
	/* move post-replacement */
Packit Service a5df35
	memmove(data + match_offset + rep_len,
Packit Service a5df35
		data + match_offset + match_len,
Packit Service a5df35
		pktb_tail(pktb) - (pktb->network_header + dataoff +
Packit Service a5df35
			     match_offset + match_len));
Packit Service a5df35
Packit Service a5df35
	/* insert data from buffer */
Packit Service a5df35
	memcpy(data + match_offset, rep_buffer, rep_len);
Packit Service a5df35
Packit Service a5df35
	/* update packet info */
Packit Service a5df35
	if (rep_len > match_len)
Packit Service a5df35
		pktb_put(pktb, rep_len - match_len);
Packit Service a5df35
	else
Packit Service a5df35
		pktb_trim(pktb, pktb->len + rep_len - match_len);
Packit Service a5df35
Packit Service a5df35
	pktb->mangled = true;
Packit Service a5df35
	return 1;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * pktb_mangled - test whether packet has been mangled
Packit Service a5df35
 * \param pktb Pointer to userspace packet buffer
Packit Service a5df35
 * \return __true__ if packet has been mangled (modified), else __false__
Packit Service a5df35
 * \par
Packit Service a5df35
 * When assembling a verdict, it is not necessary to return the contents of
Packit Service a5df35
 * un-modified packets. Use _pktb_mangled_ to decide whether packet contents
Packit Service a5df35
 * need to be returned.
Packit Service a5df35
 */
Packit Service a5df35
EXPORT_SYMBOL
Packit Service a5df35
bool pktb_mangled(const struct pkt_buff *pktb)
Packit Service a5df35
{
Packit Service a5df35
	return pktb->mangled;
Packit Service a5df35
}
Packit Service a5df35
Packit Service a5df35
/**
Packit Service a5df35
 * @}
Packit Service a5df35
 */