summaryrefslogtreecommitdiffstats
path: root/src/extra/pktbuff.c
blob: 809953b1f7d76d065a172c9ca1c3bbcad52c0815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
 */

#include <stdlib.h>
#include <string.h> /* for memcpy */

#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include "internal.h"

/**
 * \defgroup pktbuff User-space network packet buffer
 *
 * This library provides the user-space network packet buffer. This abstraction
 * is strongly inspired by Linux kernel network buffer, the so-called sk_buff.
 */

/**
 * pktb_alloc - allocate a new packet buffer
 * \param family Indicate what family, eg. AF_BRIDGE, AF_INET, AF_INET6, ...
 * \param data Pointer to packet data
 * \param len Packet length
 * \param extra Extra memory in the tail to be allocated (for mangling)
 *
 * This function returns a packet buffer that contains the packet data and
 * some extra memory room in the tail (in case of requested).
 *
 * \return a pointer to a new queue handle or NULL on failure.
 */
struct pkt_buff *
pktb_alloc(int family, void *data, size_t len, size_t extra)
{
	struct pkt_buff *pktb;
	void *pkt_data;

	pktb = calloc(1, sizeof(struct pkt_buff) + len + extra);
	if (pktb == NULL)
		return NULL;

	/* Better make sure alignment is correct. */
	pkt_data = (uint8_t *)pktb + sizeof(struct pkt_buff);
	memcpy(pkt_data, data, len);

	pktb->len = len;
	pktb->data_len = len + extra;

	pktb->head = pkt_data;
	pktb->data = pkt_data;
	pktb->tail = pktb->head + len;

	switch(family) {
	case AF_INET:
		pktb->network_header = pktb->data;
		break;
	case AF_BRIDGE: {
		struct ethhdr *ethhdr = (struct ethhdr *)pktb->data;

		pktb->mac_header = pktb->data;

		switch(ethhdr->h_proto) {
		case ETH_P_IP:
			pktb->network_header = pktb->data + ETH_HLEN;
			break;
		default:
			/* This protocol is unsupported. */
			free(pktb);
			return NULL;
		}
		break;
	}
	}
	return pktb;
}

uint8_t *pktb_data(struct pkt_buff *pktb)
{
	return pktb->data;
}

uint32_t pktb_len(struct pkt_buff *pktb)
{
	return pktb->len;
}

void pktb_free(struct pkt_buff *pktb)
{
	free(pktb);
}

void pktb_push(struct pkt_buff *pktb, unsigned int len)
{
	pktb->data += len;
}

void pktb_pull(struct pkt_buff *pktb, unsigned int len)
{
	pktb->data -= len;
}

void pktb_put(struct pkt_buff *pktb, unsigned int len)
{
	pktb->tail += len;
}

void pktb_trim(struct pkt_buff *pktb, unsigned int len)
{
	pktb->len = len;
}

unsigned int pktb_tailroom(struct pkt_buff *pktb)
{
	return pktb->data_len - pktb->len;
}

uint8_t *pktb_mac_header(struct pkt_buff *pktb)
{
	return pktb->mac_header;
}

uint8_t *pktb_network_header(struct pkt_buff *pktb)
{
	return pktb->network_header;
}

uint8_t *pktb_transport_header(struct pkt_buff *pktb)
{
	return pktb->transport_header;
}

/**
 * @}
 */