2 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
13 #include <string.h> /* for memcpy */
16 #include <netinet/if_ether.h>
17 #include <netinet/ip.h>
18 #include <netinet/tcp.h>
23 * \defgroup pktbuff User-space network packet buffer
25 * This library provides the user-space network packet buffer. This abstraction
26 * is strongly inspired by Linux kernel network buffer, the so-called sk_buff.
32 * pktb_alloc - allocate a new packet buffer
33 * \param family Indicate what family, eg. AF_BRIDGE, AF_INET, AF_INET6, ...
34 * \param data Pointer to packet data
35 * \param len Packet length
36 * \param extra Extra memory in the tail to be allocated (for mangling)
38 * This function returns a packet buffer that contains the packet data and
39 * some extra memory room in the tail (in case of requested).
41 * \return a pointer to a new queue handle or NULL on failure.
44 pktb_alloc(int family, void *data, size_t len, size_t extra)
46 struct pkt_buff *pktb;
49 pktb = calloc(1, sizeof(struct pkt_buff) + len + extra);
53 /* Better make sure alignment is correct. */
54 pkt_data = (uint8_t *)pktb + sizeof(struct pkt_buff);
55 memcpy(pkt_data, data, len);
58 pktb->data_len = len + extra;
60 pktb->head = pkt_data;
61 pktb->data = pkt_data;
62 pktb->tail = pktb->head + len;
66 pktb->network_header = pktb->data;
69 struct ethhdr *ethhdr = (struct ethhdr *)pktb->data;
71 pktb->mac_header = pktb->data;
73 switch(ethhdr->h_proto) {
75 pktb->network_header = pktb->data + ETH_HLEN;
78 /* This protocol is unsupported. */
89 * pktb_data - return pointer to the beginning of the packet buffer
90 * \param pktb Pointer to packet buffer
92 uint8_t *pktb_data(struct pkt_buff *pktb)
98 * pktb_len - return length of the packet buffer
99 * \param pktb Pointer to packet buffer
101 uint32_t pktb_len(struct pkt_buff *pktb)
107 * pktb_free - release packet buffer
108 * \param pktb Pointer to packet buffer
110 void pktb_free(struct pkt_buff *pktb)
116 * pktb_push - update pointer to the beginning of the packet buffer
117 * \param pktb Pointer to packet buffer
119 void pktb_push(struct pkt_buff *pktb, unsigned int len)
126 * pktb_pull - update pointer to the beginning of the packet buffer
127 * \param pktb Pointer to packet buffer
129 void pktb_pull(struct pkt_buff *pktb, unsigned int len)
136 * pktb_put - add extra bytes to the tail of the packet buffer
137 * \param pktb Pointer to packet buffer
139 void pktb_put(struct pkt_buff *pktb, unsigned int len)
146 * pktb_trim - set new length for this packet buffer
147 * \param pktb Pointer to packet buffer
149 void pktb_trim(struct pkt_buff *pktb, unsigned int len)
155 * pktb_tailroom - get room in bytes in the tail of the packet buffer
156 * \param pktb Pointer to packet buffer
158 unsigned int pktb_tailroom(struct pkt_buff *pktb)
160 return pktb->data_len - pktb->len;
164 * pktb_mac_header - return pointer to layer 2 header (if any)
165 * \param pktb Pointer to packet buffer
167 uint8_t *pktb_mac_header(struct pkt_buff *pktb)
169 return pktb->mac_header;
173 * pktb_network_header - return pointer to layer 3 header
174 * \param pktb Pointer to packet buffer
176 uint8_t *pktb_network_header(struct pkt_buff *pktb)
178 return pktb->network_header;
182 * pktb_transport_header - return pointer to layer 4 header (if any)
183 * \param pktb Pointer to packet buffer
185 uint8_t *pktb_transport_header(struct pkt_buff *pktb)
187 return pktb->transport_header;
190 static int pktb_expand_tail(struct pkt_buff *pkt, int extra)
192 /* No room in packet, cannot mangle it. We don't support dynamic
193 * reallocation. Instead, increase the size of the extra room in
194 * the tail in pktb_alloc.
196 if (pkt->len + extra > pkt->data_len)
200 pkt->tail = pkt->tail + extra;
204 static int enlarge_pkt(struct pkt_buff *pkt, unsigned int extra)
206 if (pkt->len + extra > 65535)
209 if (!pktb_expand_tail(pkt, extra - pktb_tailroom(pkt)))
215 int pktb_mangle(struct pkt_buff *pkt,
216 unsigned int dataoff,
217 unsigned int match_offset,
218 unsigned int match_len,
219 const char *rep_buffer,
220 unsigned int rep_len)
224 if (rep_len > match_len &&
225 rep_len - match_len > pktb_tailroom(pkt) &&
226 !enlarge_pkt(pkt, rep_len - match_len))
229 data = pkt->network_header + dataoff;
231 /* move post-replacement */
232 memmove(data + match_offset + rep_len,
233 data + match_offset + match_len,
234 pkt->tail - (pkt->network_header + dataoff +
235 match_offset + match_len));
237 /* insert data from buffer */
238 memcpy(data + match_offset, rep_buffer, rep_len);
240 /* update pkt info */
241 if (rep_len > match_len)
242 pktb_put(pkt, rep_len - match_len);
244 pktb_trim(pkt, pkt->len + rep_len - match_len);
249 EXPORT_SYMBOL(pktb_mangle);
252 * pktb_mangled - return true if packet has been mangled
253 * \param pktb Pointer to packet buffer
255 bool pktb_mangled(const struct pkt_buff *pkt)
259 EXPORT_SYMBOL(pktb_mangled);