Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, I use iptables to put UDP packets into QUEUE. Then I take them,modify payload and want to put them back in QUEUE. Do I need to recalculate UDP checksum? ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    1

    Libipq: payload modification


    Hello,

    I use iptables to put UDP packets into QUEUE. Then I take them,modify payload and want to put them back in QUEUE. Do I need to recalculate UDP checksum? If yes, could any of you give me a link of C code with UDP checksum?

    Also should I use NF_QUEUE as a verdict?
    Code:
    status = ipq_set_verdict(h, m->packet_id,NF_QUEUE,m->data_len,BUFSIZE);
    MC

  2. #2
    Just Joined!
    Join Date
    Aug 2012
    Posts
    15
    uint16_t udp_checksum ( const void * buff,
    size_t len,
    in_addr_t src_addr,
    in_addr_t dest_addr
    )


    Calculate the UDP checksum (calculated with the whole packet).

    Parameters:
    buff The UDP packet.
    len The UDP packet length.
    src_addr The IP source address (in network format).
    dest_addr The IP destination address (in network format).

    Returns:
    The result of the checksum.

    Definition at line 29 of file udp.c.

    00030 {
    00031 const uint16_t *buf=buff;
    00032 uint16_t *ip_src=(void *)&src_addr, *ip_dst=(void *)&dest_addr;
    00033 uint32_t sum;
    00034 size_t length=len;
    00035
    00036 // Calculate the sum //
    00037 sum = 0;
    00038 while (len > 1)
    00039 {
    00040 sum += *buf++;
    00041 if (sum & 0x80000000)
    00042 sum = (sum & 0xFFFF) + (sum >> 16);
    00043 len -= 2;
    00044 }
    00045
    00046 if ( len & 1 )
    00047 // Add the padding if the packet lenght is odd //
    00048 sum += *((uint8_t *)buf);
    00049
    00050 // Add the pseudo-header //
    00051 sum += *(ip_src++);
    00052 sum += *ip_src;
    00053
    00054 sum += *(ip_dst++);
    00055 sum += *ip_dst;
    00056
    00057 sum += htons(IPPROTO_UDP);
    00058 sum += htons(length);
    00059
    00060 // Add the carries //
    00061 while (sum >> 16)
    00062 sum = (sum & 0xFFFF) + (sum >> 16);
    00063
    00064 // Return the one's complement of sum //
    00065 return ( (uint16_t)(~sum) );
    00066 }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •