Skip to contentSkip to conversation

NF_IP_POST ROUTING - Send packet back out on same interface it came in on

DISCUSSION Robert SZABO 2 participants 4 messages Apr 8, 2010 netfilter

lore
Share & tools
lore
#0 lore raw

Problem solved. Thanks for all your input!

----- Original Message ----

Quoted 6 lines

From: Robert SZABO <[email protected]>
To: [email protected]
Sent: Tue, March 16, 2010 3:10:03 PM
Subject: NF_IP_POST ROUTING - Send packet back out on same interface it came in on

Hi all,

I am working on a kernel module that would allow me to

Quoted 1 line

send a packet

back out on the interface it came in on. (i.e. a simple

Quoted 1 line

heartbeat

response, where I have no control over the server as its not

Quoted 1 line

mine

to play with.

I have currently configured a bridge using

Quoted 1 line

bridge-utils for eth0 and

eth1 called br0.

I then created a netfilter

Quoted 1 line

kernel module with 2 hooks, one for

NF_IP_FORWARD which intercepts the packet

Quoted 1 line

on the bridge and

queues it, and the second one for NF_IP_POST_ROUTING to

Quoted 1 line

handle

the altered payload.

A user space program manipulates the

Quoted 1 line

queued packets and rebuilds

the ip and tcp sections including the checksum

Quoted 1 line

calculations,

ip address and port manipulation and then passes it back

Quoted 1 line

in

the verdict.

The post routing hook then needs to detect that

Quoted 1 line

packet and send

it back on the same interface the original request it was

Quoted 1 line

sent on.

I have a simple routine that I was hoping would work to alter

Quoted 1 line

the

mac addresses and input/output devices and put the response back on

Quoted 1 line

the queue but it fails to show up in tcpdump.

int

Quoted 1 line

swapAndQueuePacket(struct sk_buff *skb)

{
u_char

Quoted 1 line

tmp[6];

    struct net_device *odev,*idev;
    struct 
Quoted 1 line

ethhdr *ethdr;

    int rc = NOT_OK;


    odev = 
Quoted 1 line

dev_get_by_name(getIngresIf());

idev =

Quoted 1 line

dev_get_by_name(getEgresIf());

ethdr = (struct ethhdr

Quoted 1 line

*)skb->mac.raw;

    if (ethdr != NULL)
Quoted 1 line

goto swap_finish;

skb->dev=odev;

Quoted 1 line

skb->input_dev=idev;

    skb->pkt_type = 
Quoted 1 line

PACKET_OTHERHOST;

    skb->protocol = 
Quoted 1 line

__constant_htons(ETH_P_IP);

    skb->priority = 0;
Quoted 1 line

skb->csum = skb_checksum (skb,

skb->nh.iph->ihl*4,

Quoted 4 lines

skb->len - skb->nh.iph->ihl * 4, 0);

memcpy(tmp,ethdr->h_dest,ETH_ALEN);

memcpy

Quoted 1 line

(ethdr->h_dest, ethdr->h_source, ETH_ALEN);

memcpy

Quoted 1 line

(ethdr->h_source, tmp, ETH_ALEN);

    if (0 > 
Quoted 1 line

dev_queue_xmit(skb)) goto swap_out;

goto

Quoted 1 line

swap_finish;

swap_out:
rc = OK;

swap_finish:

Quoted 1 line

return rc;

}

Note: I am leery to rebuild the Ethernet header as

Quoted 1 line

I may have many

QinQ vlan tags to consider.

The code that engineers

Quoted 1 line

the IP/TCP payload is sound as that code

I have tested in my other bridge

Quoted 1 line

implementation which was

completely done in user space. Bridge in User Space

Quoted 1 line

is incredibly slow..

thus the reason for keeping the
bridge contained at

Quoted 1 line

kernel level.

Any help/pointers is greatly appreciated.

#1 lore raw
Quoted 4 lines · Robert SZABO
Quoted 2 lines

Subject: NF_IP_POST ROUTING - Send packet back out on same interface it came in on

Problem solved. Thanks for all your input!

You cannot just send "to (ethernet) interfaces", because the hardware would not know where to send it to once the packet *is* in the interface. Don'tcha think?
You need a target for those, like MAC address.

author
#2 lore raw

Like I said. I solved the problem and it works a treat. Yes I swapped the Mac addresses and the input/output devices.

-----Original Message-----
From: Jan Engelhardt [mailto:[email protected]]
Sent: Thursday, April 08, 2010 8:50 AM
To: Robert SZABO
Cc: [email protected]
Subject: Re: NF_IP_POST ROUTING - Send packet back out on same interface it came in on

Quoted 2 lines · Robert SZABO
Quoted 2 lines

Subject: NF_IP_POST ROUTING - Send packet back out on same interface it

came in on

Quoted 2 lines

Problem solved. Thanks for all your input!

You cannot just send "to (ethernet) interfaces", because the hardware would not know where to send it to once the packet *is* in the interface. Don'tcha think?
You need a target for those, like MAC address.

author
#3 lore raw
In reply to Jan Engelhardt

Error was simple logic:

if (ethdr != NULL)

Should have been

if (ethdr == NULL)

I could not see the forest through the trees.

Cheers

-----Original Message-----
From: Jan Engelhardt [mailto:[email protected]]
Sent: Thursday, April 08, 2010 8:50 AM
To: Robert SZABO
Cc: [email protected]
Subject: Re: NF_IP_POST ROUTING - Send packet back out on same interface it came in on

Quoted 2 lines · Robert SZABO
Quoted 2 lines

Subject: NF_IP_POST ROUTING - Send packet back out on same interface it

came in on

Quoted 2 lines

Problem solved. Thanks for all your input!

You cannot just send "to (ethernet) interfaces", because the hardware would not know where to send it to once the packet *is* in the interface. Don'tcha think?
You need a target for those, like MAC address.