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.
30 * pktb_alloc - allocate a new packet buffer
31 * \param family Indicate what family, eg. AF_BRIDGE, AF_INET, AF_INET6, ...
32 * \param data Pointer to packet data
33 * \param len Packet length
34 * \param extra Extra memory in the tail to be allocated (for mangling)
36 * This function returns a packet buffer that contains the packet data and
37 * some extra memory room in the tail (in case of requested).
39 * \return a pointer to a new queue handle or NULL on failure.
42 pktb_alloc(int family, void *data, size_t len, size_t extra)
44 struct pkt_buff *pktb;
47 pktb = calloc(1, sizeof(struct pkt_buff) + len + extra);
51 /* Better make sure alignment is correct. */
52 pkt_data = (uint8_t *)pktb + sizeof(struct pkt_buff);
53 memcpy(pkt_data, data, len);
56 pktb->data_len = len + extra;
58 pktb->head = pkt_data;
59 pktb->data = pkt_data;
60 pktb->tail = pktb->head + len;
64 pktb->network_header = pktb->data;
67 struct ethhdr *ethhdr = (struct ethhdr *)pktb->data;
69 pktb->mac_header = pktb->data;
71 switch(ethhdr->h_proto) {
73 pktb->network_header = pktb->data + ETH_HLEN;
76 /* This protocol is unsupported. */
86 uint8_t *pktb_data(struct pkt_buff *pktb)
91 uint32_t pktb_len(struct pkt_buff *pktb)
96 void pktb_free(struct pkt_buff *pktb)
101 void pktb_push(struct pkt_buff *pktb, unsigned int len)
107 void pktb_pull(struct pkt_buff *pktb, unsigned int len)
113 void pktb_put(struct pkt_buff *pktb, unsigned int len)
119 void pktb_trim(struct pkt_buff *pktb, unsigned int len)
124 unsigned int pktb_tailroom(struct pkt_buff *pktb)
126 return pktb->data_len - pktb->len;
129 uint8_t *pktb_mac_header(struct pkt_buff *pktb)
131 return pktb->mac_header;
134 uint8_t *pktb_network_header(struct pkt_buff *pktb)
136 return pktb->network_header;
139 uint8_t *pktb_transport_header(struct pkt_buff *pktb)
141 return pktb->transport_header;
144 static int pktb_expand_tail(struct pkt_buff *pkt, int extra)
146 /* No room in packet, cannot mangle it. We don't support dynamic
147 * reallocation. Instead, increase the size of the extra room in
148 * the tail in pktb_alloc.
150 if (pkt->len + extra > pkt->data_len)
154 pkt->tail = pkt->tail + extra;
158 static int enlarge_pkt(struct pkt_buff *pkt, unsigned int extra)
160 if (pkt->len + extra > 65535)
163 if (!pktb_expand_tail(pkt, extra - pktb_tailroom(pkt)))
169 int pktb_mangle(struct pkt_buff *pkt,
170 unsigned int dataoff,
171 unsigned int match_offset,
172 unsigned int match_len,
173 const char *rep_buffer,
174 unsigned int rep_len)
178 if (rep_len > match_len &&
179 rep_len - match_len > pktb_tailroom(pkt) &&
180 !enlarge_pkt(pkt, rep_len - match_len))
183 data = pkt->network_header + dataoff;
185 /* move post-replacement */
186 memmove(data + match_offset + rep_len,
187 data + match_offset + match_len,
188 pkt->tail - (pkt->network_header + dataoff +
189 match_offset + match_len));
191 /* insert data from buffer */
192 memcpy(data + match_offset, rep_buffer, rep_len);
194 /* update pkt info */
195 if (rep_len > match_len)
196 pktb_put(pkt, rep_len - match_len);
198 pktb_trim(pkt, pkt->len + rep_len - match_len);
203 EXPORT_SYMBOL(pktb_mangle);
205 bool pktb_mangled(const struct pkt_buff *pkt)
209 EXPORT_SYMBOL(pktb_mangled);