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)
106 void pktb_pull(struct pkt_buff *pktb, unsigned int len)
111 void pktb_put(struct pkt_buff *pktb, unsigned int len)
116 void pktb_trim(struct pkt_buff *pktb, unsigned int len)
121 unsigned int pktb_tailroom(struct pkt_buff *pktb)
123 return pktb->data_len - pktb->len;
126 uint8_t *pktb_mac_header(struct pkt_buff *pktb)
128 return pktb->mac_header;
131 uint8_t *pktb_network_header(struct pkt_buff *pktb)
133 return pktb->network_header;
136 uint8_t *pktb_transport_header(struct pkt_buff *pktb)
138 return pktb->transport_header;
141 static int pktb_expand_tail(struct pkt_buff *pkt, int extra)
143 /* XXX: support reallocation case. */
145 pkt->tail = pkt->tail + extra;
149 static int enlarge_pkt(struct pkt_buff *pkt, unsigned int extra)
151 if (pkt->len + extra > 65535)
154 if (pktb_expand_tail(pkt, extra - pktb_tailroom(pkt)))
160 int pktb_mangle(struct pkt_buff *pkt,
161 unsigned int dataoff,
162 unsigned int match_offset,
163 unsigned int match_len,
164 const char *rep_buffer,
165 unsigned int rep_len)
169 if (rep_len > match_len &&
170 rep_len - match_len > pktb_tailroom(pkt) &&
171 !enlarge_pkt(pkt, rep_len - match_len))
174 data = pkt->network_header + dataoff;
176 /* move post-replacement */
177 memmove(data + match_offset + rep_len,
178 data + match_offset + match_len,
179 pkt->tail - (pkt->network_header + dataoff +
180 match_offset + match_len));
182 /* insert data from buffer */
183 memcpy(data + match_offset, rep_buffer, rep_len);
185 /* update pkt info */
186 if (rep_len > match_len)
187 pktb_put(pkt, rep_len - match_len);
189 pktb_trim(pkt, pkt->len + rep_len - match_len);
194 EXPORT_SYMBOL(pktb_mangle);
196 bool pktb_mangled(const struct pkt_buff *pkt)
200 EXPORT_SYMBOL(pktb_mangled);