pktb_alloc2()
cover pktb_alloc2()
This iteration implements delayed data copy.
It obviates the need to explain to the users that they need to do their own memcpy() and supply an 'extra' argument as suggested in regard to pktb_setup().
The user can examine data and decide whether a mangle is needed without having done a memcpy() even if the mangle lengthens the packet.
examples/nf-queue.c is still to be done.
Duncan Roe (1):
src: add pktb_alloc2() and pktb_head_size()
fixmanpages.sh | 6 +- include/libnetfilter_queue/pktbuff.h | 4 + src/extra/ipv4.c | 8 +- src/extra/ipv6.c | 8 +- src/extra/pktbuff.c | 213 +++++++++++++++++++++++++++++------ src/extra/tcp.c | 18 +++ src/extra/udp.c | 18 +++ src/internal.h | 2 + src/nlmsg.c | 14 ++- 9 files changed, 245 insertions(+), 46 deletions(-)
--
2.14.5
PATCH 1/1 src: add pktb_alloc2() and pktb_head_size()
pktb_alloc2() avoids the malloc/free overhead in pktb_alloc() and also eliminates memcpy() of the payload except when mangling increases the packet length.
- pktb_mangle() does the memcpy() if need be.
Packet metadata is altered in this case
- All the _mangle functions are altered to account for possible change tp packet metadata
- Documentation is updated
pktb_head_size() returns the size of the pkt_buff opaque object. This is a suitable value for pktb_alloc2()'s buflen argument, except if mangling may increase packet size in which case use new NFQ_BUFFER_SIZE macro.
--- fixmanpages.sh | 6 +- include/libnetfilter_queue/pktbuff.h | 4 + src/extra/ipv4.c | 8 +- src/extra/ipv6.c | 8 +- src/extra/pktbuff.c | 213 +++++++++++++++++++++++++++++------ src/extra/tcp.c | 18 +++ src/extra/udp.c | 18 +++ src/internal.h | 2 + src/nlmsg.c | 14 ++- 9 files changed, 245 insertions(+), 46 deletions(-)
fixmanpages.sh +3 -3
@@ -31,11 +31,11 @@ function main add2group nfq_nlmsg_verdict_put_mark nfq_nlmsg_verdict_put_pkt setgroup nlmsg nfq_nlmsg_parse add2group nfq_nlmsg_put - setgroup pktbuff pktb_alloc - add2group pktb_data pktb_len pktb_mangle pktb_mangled - add2group pktb_free + setgroup pktbuff pktb_alloc2 + add2group pktb_data pktb_len pktb_mangle pktb_mangled pktb_head_size setgroup otherfns pktb_tailroom add2group pktb_mac_header pktb_network_header pktb_transport_header + add2group pktb_alloc pktb_free setgroup uselessfns pktb_push add2group pktb_pull pktb_put pktb_trim setgroup tcp nfq_tcp_get_hdr
include/libnetfilter_queue/pktbuff.h +4 -0
@@ -4,8 +4,11 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_alloc2 (int family,void *buf,size_t buflen,void *data,size_t len);void pktb_free (struct pkt_buff *pktb); +#define NFQ_BUFFER_SIZE (0xffff + (MNL_SOCKET_BUFFER_SIZE / 2) +uint8_t *pktb_data (struct pkt_buff *pktb);uint32_t pktb_len (struct pkt_buff *pktb);
@@ -22,5 +25,6 @@ uint8_t *pktb_transport_header(struct pkt_buff *pktb);int pktb_mangle (struct pkt_buff *pktb,int dataoff,unsigned int match_offset,unsigned int match_len,const char *rep_buffer,unsigned int rep_len);bool pktb_mangled (const struct pkt_buff *pktb); +size_t pktb_head_size (void );#endif
src/extra/ipv4.c +6 -2
@@ -35,6 +35,10 @@* determine the layer 4 protocol. The validation is that the buffer is big * enough for the declared lengths in the header, i.e. an extra check for packet * truncation. +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. (Specifically, +* invalidation occurs if the packet is being mangled to be longer than it was +* originally (only happens once per packet)). */ EXPORT_SYMBOL struct iphdr *nfq_ip_get_hdr(struct pkt_buff *pktb)
@@ -128,13 +132,13 @@ int nfq_ip_mangle(struct pkt_buff *pktb, unsigned int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len) { - struct iphdr *iph = (struct iphdr *) pktb->network_header; - if (!pktb_mangle(pktb, dataoff, match_offset, match_len, rep_buffer,rep_len )) return 0; /* fix IP hdr checksum information */ + /* N.B. set iph *after* pktb_mangle which can change network_header */ + struct iphdr *iph = (struct iphdr *) pktb->network_header; iph->tot_len =htons (pktb_tail (pktb) - pktb->network_header);nfq_ip_set_checksum (iph);
src/extra/ipv6.c +6 -2
@@ -31,6 +31,10 @@* \param pktb: Pointer to user-space network packet buffer * * \returns pointer to IPv6 header if a valid header found, else NULL. +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. (Specifically, +* invalidation occurs if the packet is being mangled to be longer than it was +* originally (only happens once per packet)). */ EXPORT_SYMBOL struct ip6_hdr *nfq_ip6_get_hdr(struct pkt_buff *pktb)
@@ -132,13 +136,13 @@ int nfq_ip6_mangle(struct pkt_buff *pktb, unsigned int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len) { - struct ip6_hdr *ip6h = (struct ip6_hdr *)pktb->network_header; - if (!pktb_mangle(pktb, dataoff, match_offset, match_len, rep_buffer,rep_len )) return 0; /* Fix IPv6 hdr length information */ + /* N.B. set ip6h *after* pktb_mangle which can change network_header */ + struct ip6_hdr *ip6h = (struct ip6_hdr *)pktb->network_header; ip6h->ip6_plen =htons (pktb_tail (pktb) - pktb->network_header -sizeof *ip6h);
src/extra/pktbuff.c +180 -33
296 lines not on this page · raw message
src/extra/tcp.c +18 -0
@@ -38,6 +38,10 @@* not set or if a minimal length check fails. * \note You have to call nfq_ip_set_transport_header() or * nfq_ip6_set_transport_header() first to set the TCP header. +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. (Specifically, +* invalidation occurs if the packet is being mangled to be longer than it was +* originally (only happens once per packet)). */ EXPORT_SYMBOL struct tcphdr *nfq_tcp_get_hdr(struct pkt_buff *pktb)
@@ -57,6 +61,10 @@ struct tcphdr *nfq_tcp_get_hdr(struct pkt_buff *pktb)* \param tcph: pointer to the TCP header * \param pktb: pointer to user-space network packet buffer * \returns Pointer to the TCP payload, or NULL if malformed TCP packet. +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. (Specifically, +* invalidation occurs if the packet is being mangled to be longer than it was +* originally (only happens once per packet)). */ EXPORT_SYMBOL void *nfq_tcp_get_payload(struct tcphdr *tcph, struct pkt_buff *pktb)
@@ -79,6 +87,8 @@ void *nfq_tcp_get_payload(struct tcphdr *tcph, struct pkt_buff *pktb)* \param tcph: pointer to the TCP header * \param pktb: pointer to user-space network packet buffer * \returns Length of TCP payload (user data) +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. */ EXPORT_SYMBOL unsigned int nfq_tcp_get_payload_len(struct tcphdr *tcph, struct pkt_buff *pktb)
@@ -230,6 +240,10 @@ int nfq_tcp_mangle_ipv4(struct pkt_buff *pktb, match_offset, match_len, rep_buffer, rep_len))return 0 ; +/* Re-fetch iph & tcph in case pktb_mangle changed network_header */ + iph = (struct iphdr *)pktb->network_header; + tcph = (struct tcphdr *)(pktb->network_header + iph->ihl*4 ); +nfq_tcp_compute_checksum_ipv4 (tcph, iph);return 1 ;
@@ -269,6 +283,10 @@ int nfq_tcp_mangle_ipv6(struct pkt_buff *pktb, match_offset, match_len, rep_buffer, rep_len))return 0 ; +/* Re-fetch ip6h & tcph in case pktb_mangle changed network_header */ + ip6h = (struct ip6_hdr *)pktb->network_header; + tcph = (struct tcphdr *)(pktb->transport_header); +nfq_tcp_compute_checksum_ipv6 (tcph, ip6h);return 1 ;
src/extra/udp.c +18 -0
@@ -38,6 +38,10 @@* not set or if a minimal length check fails. * \note You have to call nfq_ip_set_transport_header() or * nfq_ip6_set_transport_header() first to set the UDP header. +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. (Specifically, +* invalidation occurs if the packet is being mangled to be longer than it was +* originally (only happens once per packet)). */ EXPORT_SYMBOL struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)
@@ -57,6 +61,10 @@ struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)* \param udph: Pointer to UDP header * \param pktb: Pointer to userspace network packet buffer * \returns Pointer to the UDP payload, or NULL if malformed UDP packet. +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. (Specifically, +* invalidation occurs if the packet is being mangled to be longer than it was +* originally (only happens once per packet)). */ EXPORT_SYMBOL void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
@@ -79,6 +87,8 @@ void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)* \param udph: Pointer to UDP header * \param pktb: Pointer to userspace network packet buffer * \returns Length of UDP payload (user data) +* \note Calling any of the **mangle** functions may invalidate the result, +* so one should call the function again after mangling. */ EXPORT_SYMBOL unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
@@ -163,6 +173,10 @@ int nfq_udp_mangle_ipv4(struct pkt_buff *pktb, match_offset, match_len, rep_buffer, rep_len))return 0 ; +/* Re-fetch iph & udph in case pktb_mangle changed network_header */ + iph = (struct iphdr *)pktb->network_header; + udph = (struct udphdr *)(pktb->network_header + iph->ihl*4 ); +nfq_udp_compute_checksum_ipv4 (udph, iph);return 1 ;
@@ -201,6 +215,10 @@ int nfq_udp_mangle_ipv6(struct pkt_buff *pktb, match_offset, match_len, rep_buffer, rep_len))return 0 ; +/* Re-fetch ip6h & udph in case pktb_mangle changed network_header */ + ip6h = (struct ip6_hdr *)pktb->network_header; + udph = (struct udphdr *)(pktb->transport_header); +nfq_udp_compute_checksum_ipv6 (udph, ip6h);return 1 ;
src/internal.h +2 -0
@@ -27,8 +27,10 @@ struct pkt_buff {uint32_t len;uint32_t data_len; +uint32_t buf_len;bool mangled; +bool copied; };static inline uint8_t *pktb_tail (struct pkt_buff *pktb)
src/nlmsg.c +8 -6
@@ -106,26 +106,28 @@ EXPORT_SYMBOL* * This code snippet uses nfq_udp_mangle_ipv4. See nf-queue.c for * context: -* \verbatim -// main calls queue_cb (line 64) to process an enqueued packet: +* \code {.c} +// main calls queue_cb (line 49) to process an enqueued packet: // Extra variables +struct pkt_buff *pktb; uint8_t *payload, *rep_data; unsigned int match_offset, match_len, rep_len; +char pktbuf[NFQ_BUFFER_SIZE]; // The next line was commented-out (with payload void*) payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); -// Copy data to a packet buffer (allow 255 bytes for mangling). -pktb = pktb_alloc(AF_INET, payload, plen, 255); +// Set up a packet buffer (the large pktbuf allows for any mangling). +pktb = pktb_alloc2(AF_INET, pktbuf, sizeof pktbuf, payload, plen); // (decide that this packet needs mangling) nfq_udp_mangle_ipv4(pktb, match_offset, match_len, rep_data, rep_len); // nfq_udp_mangle_ipv4 updates packet length, no need to track locally -// Eventually nfq_send_verdict (line 39) gets called +// Eventually nfq_send_verdict (line 24) gets called // The received packet may or may not have been modified. // Add this code before nfq_nlmsg_verdict_put call: if (pktb_mangled(pktb)) nfq_nlmsg_verdict_put_pkt(nlh, pktb_data(pktb), pktb_len(pktb)); -\endverbatim +\endcode */ void nfq_nlmsg_verdict_put_pkt (struct nlmsghdr *nlh,const void *pkt,uint32_t plen)
Signature · 1 line
2.14.5
Pablo Neira Ayuso
maintainerRe: [PATCH libnetfilter_queue 1/1] src: add pktb_alloc2() and pktb_head_size()
Quoted 9 lines · Duncan Roe
pktb_alloc2()avoids the malloc/free overhead inpktb_alloc()and also eliminatesmemcpy()of the payload except when mangling increases the packet length.-
pktb_mangle()does thememcpy()if need be.
Packet metadata is altered in this case
- All the_manglefunctions are altered to account for possible change tp packet metadata
- Documentation is updated
Many chunks of this patchset look very much the same I posted. I'll apply my patchset and please rebase any update on top of it.
Thanks.
Re: [PATCH libnetfilter_queue 1/1] src: add pktb_alloc2() and pktb_head_size()
Quoted 5 lines · Pablo Neira Ayuso
Quoted 4 lines · Duncan Roe
pktb_alloc2()avoids the malloc/free overhead inpktb_alloc()and also eliminatesmemcpy()of the payload except when mangling increases the packet length.
Quoted 8 lines
Quoted 5 lines
-
pktb_mangle()does thememcpy()if need be.
Packet metadata is altered in this case
- All the_manglefunctions are altered to account for possible change tp packet metadata
- Documentation is updatedMany chunks of this patchset look very much the same I posted. I'll apply my patchset and please rebase any update on top of it.
Your patchset is crap.
No documentation, giant holes that I haven't had time to reply to... Apply this junk and I am walking away from netfilter.
Quoted 2 lines
Thanks.
-Duncan
Re: [PATCH libnetfilter_queue 1/1] src: add pktb_alloc2() and pktb_head_size()
In reply to Pablo Neira AyusoHi Pablo,
Quoted 11 lines · Pablo Neira Ayuso
Quoted 9 lines · Duncan Roe
pktb_alloc2()avoids the malloc/free overhead inpktb_alloc()and also eliminatesmemcpy()of the payload except when mangling increases the packet length.-
pktb_mangle()does thememcpy()if need be.
Packet metadata is altered in this case
- All the_manglefunctions are altered to account for possible change tp packet metadata
- Documentation is updated
Quoted 4 lines
Many chunks of this patchset look very much the same I posted. I'll apply my patchset and please rebase any update on top of it.
Thanks.
Apologies for the colourful language in the previous post. I could see something was very wrong but was too tired to formulate a reasoned response.
My concern is with the calling sequence of pktb_setup().
There's a detailed explanation in one of the patch reviews I just posted. As long as you're happy with that bit, commit what you like.
If it would save you time, the following patch applied on top of my initial patch aligns with what you posted, after making the corrections in my reviews. You might like to try them and see what you think.
Or, just apply yours and I'll rebase. But that *will* include changing the calling sequence of pktb_setup().
Cheers ... Duncan.
Duncan Roe (1):
src & doc: Rename pktb_alloc2 to pktb_setup
fixmanpages.sh | 2 +- include/libnetfilter_queue/pktbuff.h | 4 +++- src/extra/pktbuff.c | 37 ++++++++++++++++++------------------ src/nlmsg.c | 2 +- 4 files changed, 23 insertions(+), 22 deletions(-)
--
2.14.5
PATCH 1/1 src & doc: Rename pktb_alloc2 to pktb_setup
--- fixmanpages.sh | 2 +- include/libnetfilter_queue/pktbuff.h | 4 +++- src/extra/pktbuff.c | 37 ++++++++++++++++++------------------ src/nlmsg.c | 2 +- 4 files changed, 23 insertions(+), 22 deletions(-)
fixmanpages.sh +1 -1
@@ -31,7 +31,7 @@ function main add2group nfq_nlmsg_verdict_put_mark nfq_nlmsg_verdict_put_pkt setgroup nlmsg nfq_nlmsg_parse add2group nfq_nlmsg_put - setgroup pktbuff pktb_alloc2 + setgroup pktbuff pktb_setup add2group pktb_data pktb_len pktb_mangle pktb_mangled pktb_head_size setgroup otherfns pktb_tailroom add2group pktb_mac_header pktb_network_header pktb_transport_header
include/libnetfilter_queue/pktbuff.h +3 -1
@@ -4,10 +4,12 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); -struct pkt_buff *pktb_alloc2 (int family,void *buf,size_t buflen,void *data,size_t len);void pktb_free (struct pkt_buff *pktb);#define NFQ_BUFFER_SIZE (0xffff + (MNL_SOCKET_BUFFER_SIZE / 2) +struct pkt_buff *pktb_setup (int family,void *buf,size_t buflen,void *data,size_t len); + +#define pktb_head_alloc () (struct pkt_buff *)(malloc(pktb_head_size()))uint8_t *pktb_data (struct pkt_buff *pktb);uint32_t pktb_len (struct pkt_buff *pktb);
src/extra/pktbuff.c +18 -19
@@ -31,18 +31,17 @@* @{ */ -static int pktb_setup_family (struct pkt_buff *pktb,int family) +static int __pktb_setup (int family,struct pkt_buff *pktb) {struct ethhdr *ethhdr; -switch (family) { +switch (family) {case AF_INET :case AF_INET6 : pktb->network_header = pktb->data;break ;case AF_BRIDGE : ethhdr = (struct ethhdr *)pktb->data; - pktb->mac_header = pktb->data;switch (ethhdr->h_proto) {
@@ -92,7 +91,7 @@ static void pktb_setup_metadata(struct pkt_buff *pktb, void *pkt_data,* \n * __EPROTONOSUPPORT__ _family_ was __AF_BRIDGE__ and this is not an IP packet * (v4 or v6) -* \note __pktb_alloc__ is deprecated. Use pktb_alloc2() in new code +* \note __pktb_alloc__ is deprecated. Use pktb_setup() in new code * \sa __calloc__(3) */ EXPORT_SYMBOL
@@ -112,7 +111,7 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)pktb_setup_metadata (pktb, pkt_data, len, extra); -if (pktb_setup_family (pktb, family) <0 ) { +if (__pktb_setup (family, pktb) <0 ) {free (pktb);return NULL ; }
@@ -125,7 +124,17 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)*/ /** -* pktb_alloc2 - make a packet buffer from an existing buffer +* pktb_head_size - get size of struct pkt_buff +* \return Size of (opaque) __struct pkt_buff__ +*/ +EXPORT_SYMBOL +size_t pktb_head_size(void) +{ +return sizeof (struct pkt_buff ); +} + +/** +* pktb_setup - make a packet buffer from an existing buffer * \param family Indicate what family. Currently supported families are * AF_BRIDGE, AF_INET & AF_INET6. * \param buf Buffer to hold packet metadata, and packet contents _if_
@@ -176,7 +185,7 @@ static char buf[NFQ_BUFFER_SIZE];* \sa nfq_nlmsg_verdict_put_pkt() (has sample code using __pktb_alloc2__) */ EXPORT_SYMBOL -struct pkt_buff *pktb_alloc2(int family, void *buf, size_t buflen, +struct pkt_buff *pktb_setup(int family, void *buf, size_t buflen,void *data,size_t len) {struct pkt_buff *pktb;
@@ -194,7 +203,7 @@ struct pkt_buff *pktb_alloc2(int family, void *buf, size_t buflen,pktb_setup_metadata (pktb, pkt_data, len,0 ); pktb->buf_len = buflen; -if (pktb_setup_family (pktb, family) <0 ) +if (__pktb_setup (family, pktb) <0 ) pktb =NULL ;return pktb; }
@@ -231,16 +240,6 @@ uint32_t pktb_len(struct pkt_buff *pktb)return pktb->len; } -/** -* pktb_head_size - get size of struct pkt_buff -* \return Size of (opaque) __struct pkt_buff__ -*/ -EXPORT_SYMBOL -size_t pktb_head_size(void) -{ -return sizeof (struct pkt_buff ); -} -/** * \defgroup otherfns Other functions *
@@ -261,7 +260,7 @@ size_t pktb_head_size(void)* pktb_free - release packet buffer [DEPRECATED] * \param pktb Pointer to userspace packet buffer * \note __pktb_free__ is deprecated. -* It is not required and must not be used with pktb_alloc2() +* It is not required and must not be used with pktb_setup() */ EXPORT_SYMBOL void pktb_free(struct pkt_buff *pktb)
src/nlmsg.c +1 -1
@@ -117,7 +117,7 @@ EXPORT_SYMBOL// The next line was commented-out (with payload void*) payload =mnl_attr_get_payload (attr[NFQA_PAYLOAD ]);// Set up a packet buffer (the large pktbuf allows for any mangling). - pktb =pktb_alloc2 (AF_INET , pktbuf,sizeof pktbuf, payload, plen); + pktb =pktb_setup (AF_INET , pktbuf,sizeof pktbuf, payload, plen);// (decide that this packet needs mangling) nfq_udp_mangle_ipv4 (pktb, match_offset, match_len, rep_data, rep_len);// nfq_udp_mangle_ipv4 updates packet length, no need to track locally
Signature · 1 line
2.14.5