Skip to contentSkip to conversation

how to update payload using QUEUE

DISCUSSION OpenSource Dev 2 participants 5 messages Nov 9, 2008 – Nov 11, 2008 netfilter

lore
Share & tools
lore
#0 lore raw

I am trying to use netfilter QUEUE to process IP payload in user-space. So far I have successfully retrived the packet (payload), done with my processing but I don't know how to put it back (modified payload) back to original outbound packet.

this is what I have tried:

--------------
len = nfq_get_payload(tb, &payload);

if (len >= 0 ) {
    new_len = process_payload(payload, *newPayload, len);
   //trying memcpy
   memcpy(payload, newPayload, len);
   pkt->ip->check = 0;
   free(newPayload);

}

return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
--------------

I tried using memcpy() to copy new payload data to existing, but I think that is being done on user space only. or do I need to call some nfa_set.. function ?
I will be glad if someone could point me to right direction.

Thank you,

Bikrant

#1 lore raw

OD> I am trying to use netfilter QUEUE to process IP payload in OD> user-space. So far I have successfully retrived the packet (payload), OD> done with my processing but I don't know how to put it back (modified OD> payload) back to original outbound packet.
OD> this is what I have tried:
OD> --------------
OD> len = nfq_get_payload(tb, &payload);
OD> if (len >>= 0 ) {
OD> new_len = process_payload(payload, *newPayload, len);

OD>    //trying memcpy
OD>    memcpy(payload, newPayload, len);
OD>    pkt->ip->check = 0;
OD>    free(newPayload);

OD> }
OD> return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
OD> --------------
OD> I tried using memcpy() to copy new payload data to existing, but I OD> think that is being done on user space only. or do I need to call OD> some nfa_set.. function ?
OD> I will be glad if someone could point me to right direction.

OD> Thank you,

Cut from libnetfilter_queue.c
================================
int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id,
u_int32_t verdict, u_int32_t data_len, unsigned char *buf)
================================

You call this function in this way:
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);

I think right function call:
===================
ret = nfq_set_verdict(qh, id, NF_ACCEPT, new_len, newPayload); free(newPayload);
return ret;
===================
and my be without any memcpy()

Try this...

#2 lore raw
Quoted 41 lines · Kuzin Andrey

OD> I am trying to use netfilter QUEUE to process IP payload in OD> user-space. So far I have successfully retrived the packet (payload), OD> done with my processing but I don't know how to put it back (modified OD> payload) back to original outbound packet.
OD> this is what I have tried:
OD> --------------
OD> len = nfq_get_payload(tb, &payload);
OD> if (len >>= 0 ) {
OD> new_len = process_payload(payload, *newPayload, len);

OD>    //trying memcpy
OD>    memcpy(payload, newPayload, len);
OD>    pkt->ip->check = 0;
OD>    free(newPayload);

OD> }
OD> return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
OD> --------------
OD> I tried using memcpy() to copy new payload data to existing, but I OD> think that is being done on user space only. or do I need to call OD> some nfa_set.. function ?
OD> I will be glad if someone could point me to right direction.

OD> Thank you,

Cut from libnetfilter_queue.c
================================
int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id,
u_int32_t verdict, u_int32_t data_len, unsigned char *buf)
================================

You call this function in this way:
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);

I think right function call:
===================
ret = nfq_set_verdict(qh, id, NF_ACCEPT, new_len, newPayload); free(newPayload);
return ret;
===================
and my be without any memcpy()

Quoted 1 line

Try this...

Thank you all. It worked for me.
Now I think I am in right direction :)

However, now I am having problem with checksum. After processing payload, I set the iphdr checksum value to 0 hoping system would automatically compute the check sum but it is not. Tcpdump shows "bad checksum" value (0).

Or do I need to compute and set checksum manually. Ideally I would like to offload-checksum when possible.

Looking for your suggestions.

thanks again :)

Bikrant

#3 lore raw
Quoted 18 lines
Quoted 18 lines

Cut from libnetfilter_queue.c
================================
int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id,
u_int32_t verdict, u_int32_t data_len, unsigned char *buf)
================================

You call this function in this way:
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);

I think right function call:
===================
ret = nfq_set_verdict(qh, id, NF_ACCEPT, new_len, newPayload); free(newPayload);
return ret;
===================
and my be without any memcpy()

Quoted 1 line
Quoted 1 line

Try this...

OD> Thank you all. It worked for me.
OD> Now I think I am in right direction :)

OD> However, now I am having problem with checksum. After processing OD> payload, I set the iphdr checksum value to 0 hoping system would OD> automatically compute the check sum but it is not. Tcpdump shows "bad OD> checksum" value (0).

OD> Or do I need to compute and set checksum manually. Ideally I would OD> like to offload-checksum when possible.
OD> Looking for your suggestions.
OD> thanks again :)
OD> Bikrant

I found some useful links for you !

Here use checksum compute for tcp and ip packets (some code from TCPDUMP):
http://code.google.com/p/quefuzz/source/browse/trunk/quefuzz.c?r=5

Here use another cksum() function:
https://trac.luffy.cx/udpproxy/browser/src
https://trac.luffy.cx/udpproxy/browser/src/cksum.c
https://trac.luffy.cx/udpproxy/browser/src/proxy.c

Question is what function calculate checksum faster and rigth for nf_queue ?!

May be this help to you !
Write something about that. I also need this experience, but i don't have any time for experiments now.

Any comments from nfq developers also appreciated. Why your nfq_set_verdict with payload don't compute checksum automatically ???!!!
Google say what whole the world have difficults with that.

#4 lore raw
Quoted 4 lines

I found some useful links for you !

Here use checksum compute for tcp and ip packets (some code from TCPDUMP):
http://code.google.com/p/quefuzz/source/browse/trunk/quefuzz.c?r=5

This really works great. I will sure try some other methods too and try to find out which one is better.

Right now I can ping two machines with modified ICMP payload and new checksum value :)
But TCP is not working yet. I am pretty sure I have some code mixup somewhere.

Thanks for your help!

Bikrant

Quoted 17 lines

Here use another cksum() function:
https://trac.luffy.cx/udpproxy/browser/src
https://trac.luffy.cx/udpproxy/browser/src/cksum.c
https://trac.luffy.cx/udpproxy/browser/src/proxy.c

Question is what function calculate checksum faster and rigth for nf_queue ?!

May be this help to you !
Write something about that. I also need this experience, but i don't have any time for experiments now.

Any comments from nfq developers also appreciated. Why your nfq_set_verdict with payload don't compute checksum automatically ???!!!
Google say what whole the world have difficults with that.