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 */
15 #include <netinet/if_ether.h>
16 #include <netinet/ip.h>
17 #include <netinet/tcp.h>
22 * \defgroup pktbuff User-space network packet buffer
24 * This library provides the user-space network packet buffer. This abstraction
25 * is strongly inspired by Linux kernel network buffer, the so-called sk_buff.
29 * pktb_alloc - allocate a new packet buffer
30 * \param family Indicate what family, eg. AF_BRIDGE, AF_INET, AF_INET6, ...
31 * \param data Pointer to packet data
32 * \param len Packet length
33 * \param extra Extra memory in the tail to be allocated (for mangling)
35 * This function returns a packet buffer that contains the packet data and
36 * some extra memory room in the tail (in case of requested).
38 * \return a pointer to a new queue handle or NULL on failure.
41 pktb_alloc(int family, void *data, size_t len, size_t extra)
43 struct pkt_buff *pktb;
46 pktb = calloc(1, sizeof(struct pkt_buff) + len + extra);
50 /* Better make sure alignment is correct. */
51 pkt_data = (uint8_t *)pktb + sizeof(struct pkt_buff);
52 memcpy(pkt_data, data, len);
55 pktb->data_len = len + extra;
57 pktb->head = pkt_data;
58 pktb->data = pkt_data;
59 pktb->tail = pktb->head + len;
63 pktb->network_header = pktb->data;
66 struct ethhdr *ethhdr = (struct ethhdr *)pktb->data;
68 pktb->mac_header = pktb->data;
70 switch(ethhdr->h_proto) {
72 pktb->network_header = pktb->data + ETH_HLEN;
75 /* This protocol is unsupported. */
85 uint8_t *pktb_data(struct pkt_buff *pktb)
90 uint32_t pktb_len(struct pkt_buff *pktb)
95 void pktb_free(struct pkt_buff *pktb)
100 void pktb_push(struct pkt_buff *pktb, unsigned int len)
105 void pktb_pull(struct pkt_buff *pktb, unsigned int len)
110 void pktb_put(struct pkt_buff *pktb, unsigned int len)
115 void pktb_trim(struct pkt_buff *pktb, unsigned int len)
120 unsigned int pktb_tailroom(struct pkt_buff *pktb)
122 return pktb->data_len - pktb->len;
125 uint8_t *pktb_mac_header(struct pkt_buff *pktb)
127 return pktb->mac_header;
130 uint8_t *pktb_network_header(struct pkt_buff *pktb)
132 return pktb->network_header;
135 uint8_t *pktb_transport_header(struct pkt_buff *pktb)
137 return pktb->transport_header;