netfilter_queue ipv4 optional header removal

218 views Asked by At

I'm implementing netfilter_queue-based user program that deletes ipv4 optional header 'Time Stamp'

ping works well with this program, because it uses ICMP transmission.

But TCP-based applications doesn't work. I've checked it with wireshark, and this program deletes timestamp well. Instead, TCP-based application doesn't send ACK for that packet, and remote server retransmits the same packet indefinitely.

Is there any missing procedures for TCP packet processing? I just modified IPv4 header part only. Then why the tcp transmission doesn't work at all?

My main code is:

static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
          struct nfq_data *nfa, void *data)
{
    
    unsigned int timestamp = 0;
    bool ptype = true;
    int pnow = 20;
    int plast = 20;
    int ihl;
    
    struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);

    unsigned char* rawBuff = NULL;
    int len;
    len = nfq_get_payload(nfa, &rawBuff);
    if(len < 0) printf("Failed to get payload");

    struct pkt_buff* pkBuff = pktb_alloc(AF_INET, rawBuff, len, 0x20);
    struct iphdr* ip = nfq_ip_get_hdr(pkBuff);
    
    ihl = ip->ihl;
    uint8_t* buff = NULL;
    
    if( (ip->daddr != 0x0101007f) && (ip->daddr != 0x0100007f) && (ip->daddr != 0x0100A9C0) && (ip->saddr != 0x0100A9C0)) { // filter_out dns
        if(ip->version == 4) {
            if(ihl != 5) { // if ipv4 packet header is longer than default packet header
                buff = pktb_data(pkBuff); // packet buffer
                plast = ihl * 4;
                while(pnow != plast) {
                    if(buff[pnow] == 0x44) { // timestamp type
                        ptype = false;
                        break;
                    }
                    else {
                        if(buff[pnow+1] == 0) {
                            pnow = pnow + 4;
                        }
                        else {
                            pnow = pnow + buff[pnow+1];
                        }
                    }
                }
            }
            if(!ptype) {
                timestamp = buff[pnow + 4] << 24 | buff[pnow + 5] << 16 | buff[pnow + 6] << 8 | buff[pnow + 7];
                if(timestamp > 100000) { // if TS is big, delete it.
                    ip->ihl -= 2;
                    nfq_ip_mangle(pkBuff, pnow, 0, 8, "", 0);
                }
            }
        }
    }
    
    nfq_ip_set_checksum(ip);
    if(nfq_ip_set_transport_header(pkBuff, ip) < 0) printf("Failed to set transport header");

    int result = 0;
    result = nfq_set_verdict(qh, ntohl(ph->packet_id), NF_ACCEPT, pktb_len(pkBuff), pktb_data(pkBuff));
    pktb_free(pkBuff);
    return result;
}

iptables setting is:

sudo iptables -t mangle -A PREROUTING -j NFQUEUE -p all --queue-num 0
sudo iptables -t mangle -A POSTROUTING -j NFQUEUE -p all --queue-num 0
2

There are 2 answers

0
CStriker On

It seems that netfilter_queue is malfunctioning. After debugging Kernel, I could identify that skbuff's network_header pointer is not updated even I changed netfilter_queue's equivalent pointer. Mangled packet is dropped by packet length checking code.

0
vaselo On

Shouldn't you place the TCP header just after the IP header?

 0...............20.........28..........48..........
 +---------------+----------+-----------+-----------+
 |IP HEADER      |IP OPTIONS| TCP HEADER|TCP OPTIONS|
 +---------------+----------+-----------+-----------+

So, decreasing ihl value, you're creating a gap between IP header and TCP header. You need to use memmove along with decreasing ihl value.