Networking Forums

Networking Forums > Computer Networking > Linux Networking > nat in linux kernel

Reply
Thread Tools Display Modes

nat in linux kernel

 
 
Giacomo
Guest
Posts: n/a

 
      07-02-2005, 04:03 PM
Good morning i'm Giacomo From Italy

i am writing a simple firewall in linux kernel space (2.6.11)

i'm trying to implement DNAT, and i take struct sk_buff* skb from functions
in prerouting context.

i change the destinstion port on skb.

i printk the fields in pre routing and in input: all things as expected:
original port in pre and changed port in input.

the problem is that packet seems to disappear: it does not enter the output
hook.

For example

i map port 100 to 22 and do SSH from IP2 to IP1. On IP1 I do dnat from port
100 to 22

IP2: ssh IP1 -p 100

on IP1 i get printed:

PRE: dest port 100 OK
INPUT dest port 22 OK!

but ssh seems not responding, it probably does not really receive packet!

WHY??

perhaps i miss something... perhaps it is not enough to simply rewrite a
field of sk_buff.

I thought it was automatic that since a packet enters input functions with a
certain destination port, although different from the port that was in pre
routing,
it got directed in the right way, in this case delivered to port 22 where
ssh is listening.

Do i have to recalculate checksum?? how??

PS: of course, i have prepared de-dnat on outgoing packets... but for now
they do not OUT-GO!

PPS: of course ssh is up and responds correctly if i don't mangle
destination port in pre routing.

Thanks in advance for any idea.

Giacomo, Italy


 
Reply With Quote
 
 
 
 
Alexander Harsch
Guest
Posts: n/a

 
      07-02-2005, 07:18 PM
Giacomo wrote:


>
> Do i have to recalculate checksum?? how??
>

Hello,

yes, you have to. In ip_local_input() which is the next function after the
packets leaves your hook, the checksum of your IP header will be
calculated. If it won't match, the packet will be discarded. To figure out
how to calculate the checksum, parse the ip_local_input function and see
where the checksum is calculated. Do it the same way and insert this value
in the checksum field of the ip header. Alex
 
Reply With Quote
 
Alexander Harsch
Guest
Posts: n/a

 
      07-02-2005, 07:35 PM
Giacomo wrote:

>
> Do i have to recalculate checksum?? how??
>

Ok, I just figured out that checksum is calculated in ip_rcv(), which is
called before the PRE_ROUTING hook. So, the packets propable reach the ssh
daemon. But in the TCP connection, the ssh daemon will send from port ssh
to as the source port. The sender will expect to receive an answer from
port 100. You can figure this out with tcpdump on the sending side.

Alex

 
Reply With Quote
 
Giacomo
Guest
Posts: n/a

 
      07-03-2005, 06:31 AM
yes you are right, i de dnatted connection, so sender should receive answer
to port 100.
The problem is that the answer does not come back.
I've seen this a lso with tcpdump. no coming back packets neither towards
source port 22 nor 100.
If checksum is calculated before ip_rcv(), then i think i should recalculate
it, since i alter packet after prerouting hook.

I found some hints on the web, but i'm yet not sure which is the right
way/the right functions to call.
I alter both ports (TCP layer) and address (IP).

Thanks a lot.

Giacomo.



"Alexander Harsch" <(E-Mail Removed)> ha scritto nel messaggio
news:da6q95$qma$(E-Mail Removed)...
> Giacomo wrote:
>
>>
>> Do i have to recalculate checksum?? how??
>>

> Ok, I just figured out that checksum is calculated in ip_rcv(), which is
> called before the PRE_ROUTING hook. So, the packets propable reach the ssh
> daemon. But in the TCP connection, the ssh daemon will send from port ssh
> to as the source port. The sender will expect to receive an answer from
> port 100. You can figure this out with tcpdump on the sending side.
>
> Alex
>



 
Reply With Quote
 
joy
Guest
Posts: n/a

 
      07-04-2005, 09:58 AM
Giacomo wrote:
> Good morning i'm Giacomo From Italy
>
> i am writing a simple firewall in linux kernel space (2.6.11)
>
> i'm trying to implement DNAT, and i take struct sk_buff* skb from functions
> in prerouting context.
>
> i change the destinstion port on skb.
>
> i printk the fields in pre routing and in input: all things as expected:
> original port in pre and changed port in input.
>
> the problem is that packet seems to disappear: it does not enter the output
> hook.
>
> For example
>
> i map port 100 to 22 and do SSH from IP2 to IP1. On IP1 I do dnat from port
> 100 to 22
>
> IP2: ssh IP1 -p 100
>
> on IP1 i get printed:
>
> PRE: dest port 100 OK
> INPUT dest port 22 OK!
>
> but ssh seems not responding, it probably does not really receive packet!
>
> WHY??
>
> perhaps i miss something... perhaps it is not enough to simply rewrite a
> field of sk_buff.
>
> I thought it was automatic that since a packet enters input functions with a
> certain destination port, although different from the port that was in pre
> routing,
> it got directed in the right way, in this case delivered to port 22 where
> ssh is listening.
>
> Do i have to recalculate checksum?? how??
>
> PS: of course, i have prepared de-dnat on outgoing packets... but for now
> they do not OUT-GO!
>
> PPS: of course ssh is up and responds correctly if i don't mangle
> destination port in pre routing.
>
> Thanks in advance for any idea.
>
> Giacomo, Italy
>
>

hello jacopo....

have you try to sniff?what you see?

peppe
 
Reply With Quote
 
Giacomo
Guest
Posts: n/a

 
      07-04-2005, 10:11 AM
thanks joy!
Yes, i sniff and see prerouting with old ip and ports, then packets goes in
INPUT with new values... but
then i can't see any response back.

I think i must recalculate the checksum, but i don't know what are the right
functions!

Thanks a lot!

Giacomo


"joy" <joy79a_nospam_@libero.it> ha scritto nel messaggio
news:%K7ye.82$(E-Mail Removed)...
> Giacomo wrote:
>> Good morning i'm Giacomo From Italy
>>
>> i am writing a simple firewall in linux kernel space (2.6.11)
>>
>> i'm trying to implement DNAT, and i take struct sk_buff* skb from
>> functions in prerouting context.
>>
>> i change the destinstion port on skb.
>>
>> i printk the fields in pre routing and in input: all things as expected:
>> original port in pre and changed port in input.
>>
>> the problem is that packet seems to disappear: it does not enter the
>> output hook.
>>
>> For example
>>
>> i map port 100 to 22 and do SSH from IP2 to IP1. On IP1 I do dnat from
>> port 100 to 22
>>
>> IP2: ssh IP1 -p 100
>>
>> on IP1 i get printed:
>>
>> PRE: dest port 100 OK
>> INPUT dest port 22 OK!
>>
>> but ssh seems not responding, it probably does not really receive packet!
>>
>> WHY??
>>
>> perhaps i miss something... perhaps it is not enough to simply rewrite a
>> field of sk_buff.
>>
>> I thought it was automatic that since a packet enters input functions
>> with a certain destination port, although different from the port that
>> was in pre routing,
>> it got directed in the right way, in this case delivered to port 22 where
>> ssh is listening.
>>
>> Do i have to recalculate checksum?? how??
>>
>> PS: of course, i have prepared de-dnat on outgoing packets... but for now
>> they do not OUT-GO!
>>
>> PPS: of course ssh is up and responds correctly if i don't mangle
>> destination port in pre routing.
>>
>> Thanks in advance for any idea.
>>
>> Giacomo, Italy

> hello jacopo....
>
> have you try to sniff?what you see?
>
> peppe



 
Reply With Quote
 
santa19992000@yahoo.com
Guest
Posts: n/a

 
      07-04-2005, 12:29 PM
Did you change just the port numbers or did you implemented NAT as a
separate loadable module?. Since when you do SSH from different
machine, it might be using default prot numbers I think, could you do
"netstat -a" and compare the output with default port numbers and
modified port numbers.

 
Reply With Quote
 
Alexander Harsch
Guest
Posts: n/a

 
      07-04-2005, 04:38 PM
Giacomo wrote:

> thanks joy!
> Yes, i sniff and see prerouting with old ip and ports, then packets goes
> in INPUT with new values... but
> then i can't see any response back.
>
> I think i must recalculate the checksum, but i don't know what are the
> right functions!
>
> Thanks a lot!
>
> Giacomo
>
>
> "joy" <joy79a_nospam_@libero.it> ha scritto nel messaggio
> news:%K7ye.82$(E-Mail Removed)...
>> Giacomo wrote:
>>> Good morning i'm Giacomo From Italy
>>>
>>> i am writing a simple firewall in linux kernel space (2.6.11)
>>>
>>> i'm trying to implement DNAT, and i take struct sk_buff* skb from
>>> functions in prerouting context.
>>>
>>> i change the destinstion port on skb.
>>>
>>> i printk the fields in pre routing and in input: all things as expected:
>>> original port in pre and changed port in input.
>>>
>>> the problem is that packet seems to disappear: it does not enter the
>>> output hook.
>>>
>>> For example
>>>
>>> i map port 100 to 22 and do SSH from IP2 to IP1. On IP1 I do dnat from
>>> port 100 to 22
>>>
>>> IP2: ssh IP1 -p 100
>>>
>>> on IP1 i get printed:
>>>
>>> PRE: dest port 100 OK
>>> INPUT dest port 22 OK!
>>>
>>> but ssh seems not responding, it probably does not really receive
>>> packet!
>>>
>>> WHY??
>>>
>>> perhaps i miss something... perhaps it is not enough to simply rewrite a
>>> field of sk_buff.
>>>
>>> I thought it was automatic that since a packet enters input functions
>>> with a certain destination port, although different from the port that
>>> was in pre routing,
>>> it got directed in the right way, in this case delivered to port 22
>>> where ssh is listening.
>>>
>>> Do i have to recalculate checksum?? how??
>>>
>>> PS: of course, i have prepared de-dnat on outgoing packets... but for
>>> now they do not OUT-GO!
>>>
>>> PPS: of course ssh is up and responds correctly if i don't mangle
>>> destination port in pre routing.
>>>
>>> Thanks in advance for any idea.
>>>
>>> Giacomo, Italy

>> hello jacopo....
>>
>> have you try to sniff?what you see?
>>
>> peppe

tcp_v4_send_check() will calculate the checksum. See net/ipv4/tcp_ipv4.c
Alex
 
Reply With Quote
 
Giacomo
Guest
Posts: n/a

 
      07-04-2005, 08:41 PM
Hello joy.
I calculated tcp checksum. Now port redirection works fine.

I have still problems with address redirection. Example:

IP1: the one with firewall.
IP2 requests connection
IP3 target of redirection

rule on IP1: when receiving a connection on port 25 redirect to IP3 port 22.

On IP2 i do a ssh IP1 -p 25

On IP1 port 25 is translated into 22 and i see packet FORWARDED to IP3.

ON sniffer TCPDUMP on pc IP3 i see SYN from IP2 to IP3 (correct because of
redirection)

then Syn ack from IP3 to IP2 (addresses and ports get translated by IP1 so
IP3 should respond directly to IP2)

finally, and here the problem, i see a reset from IP2 to IP3!

What am i wrong with?

There must be something wrong: should i make every packet pass through the
firewall?

My home scenario is quite unusual: usually redirection happens from an
extern network towards a private one.
In that case, if the first packet of a connection gets Destination natted,
response goes out directly to the source address in the first packet, that
is,
the external network, through the gateway.

thanks a lot fr any suggestion.

Giacomo.




"joy" <joy79a_nospam_@libero.it> ha scritto nel messaggio
news:%K7ye.82$(E-Mail Removed)...
> Giacomo wrote:
>> Good morning i'm Giacomo From Italy
>>
>> i am writing a simple firewall in linux kernel space (2.6.11)
>>
>> i'm trying to implement DNAT, and i take struct sk_buff* skb from
>> functions in prerouting context.
>>
>> i change the destinstion port on skb.
>>
>> i printk the fields in pre routing and in input: all things as expected:
>> original port in pre and changed port in input.
>>
>> the problem is that packet seems to disappear: it does not enter the
>> output hook.
>>
>> For example
>>
>> i map port 100 to 22 and do SSH from IP2 to IP1. On IP1 I do dnat from
>> port 100 to 22
>>
>> IP2: ssh IP1 -p 100
>>
>> on IP1 i get printed:
>>
>> PRE: dest port 100 OK
>> INPUT dest port 22 OK!
>>
>> but ssh seems not responding, it probably does not really receive packet!
>>
>> WHY??
>>
>> perhaps i miss something... perhaps it is not enough to simply rewrite a
>> field of sk_buff.
>>
>> I thought it was automatic that since a packet enters input functions
>> with a certain destination port, although different from the port that
>> was in pre routing,
>> it got directed in the right way, in this case delivered to port 22 where
>> ssh is listening.
>>
>> Do i have to recalculate checksum?? how??
>>
>> PS: of course, i have prepared de-dnat on outgoing packets... but for now
>> they do not OUT-GO!
>>
>> PPS: of course ssh is up and responds correctly if i don't mangle
>> destination port in pre routing.
>>
>> Thanks in advance for any idea.
>>
>> Giacomo, Italy

> hello jacopo....
>
> have you try to sniff?what you see?
>
> peppe



 
Reply With Quote
 
Giacomo
Guest
Posts: n/a

 
      07-04-2005, 08:43 PM
Thanks i understood i made a mistake!
Thanks a lot!

Giacomo


"Giacomo" <(E-Mail Removed)> ha scritto nel messaggio
news:hahye.3240$%(E-Mail Removed)...
> Hello joy.
> I calculated tcp checksum. Now port redirection works fine.
>
> I have still problems with address redirection. Example:
>
> IP1: the one with firewall.
> IP2 requests connection
> IP3 target of redirection
>
> rule on IP1: when receiving a connection on port 25 redirect to IP3 port
> 22.
>
> On IP2 i do a ssh IP1 -p 25
>
> On IP1 port 25 is translated into 22 and i see packet FORWARDED to IP3.
>
> ON sniffer TCPDUMP on pc IP3 i see SYN from IP2 to IP3 (correct because of
> redirection)
>
> then Syn ack from IP3 to IP2 (addresses and ports get translated by IP1
> so IP3 should respond directly to IP2)
>
> finally, and here the problem, i see a reset from IP2 to IP3!
>
> What am i wrong with?
>
> There must be something wrong: should i make every packet pass through the
> firewall?
>
> My home scenario is quite unusual: usually redirection happens from an
> extern network towards a private one.
> In that case, if the first packet of a connection gets Destination natted,
> response goes out directly to the source address in the first packet, that
> is,
> the external network, through the gateway.
>
> thanks a lot fr any suggestion.
>
> Giacomo.
>
>
>
>
> "joy" <joy79a_nospam_@libero.it> ha scritto nel messaggio
> news:%K7ye.82$(E-Mail Removed)...
>> Giacomo wrote:
>>> Good morning i'm Giacomo From Italy
>>>
>>> i am writing a simple firewall in linux kernel space (2.6.11)
>>>
>>> i'm trying to implement DNAT, and i take struct sk_buff* skb from
>>> functions in prerouting context.
>>>
>>> i change the destinstion port on skb.
>>>
>>> i printk the fields in pre routing and in input: all things as expected:
>>> original port in pre and changed port in input.
>>>
>>> the problem is that packet seems to disappear: it does not enter the
>>> output hook.
>>>
>>> For example
>>>
>>> i map port 100 to 22 and do SSH from IP2 to IP1. On IP1 I do dnat from
>>> port 100 to 22
>>>
>>> IP2: ssh IP1 -p 100
>>>
>>> on IP1 i get printed:
>>>
>>> PRE: dest port 100 OK
>>> INPUT dest port 22 OK!
>>>
>>> but ssh seems not responding, it probably does not really receive
>>> packet!
>>>
>>> WHY??
>>>
>>> perhaps i miss something... perhaps it is not enough to simply rewrite a
>>> field of sk_buff.
>>>
>>> I thought it was automatic that since a packet enters input functions
>>> with a certain destination port, although different from the port that
>>> was in pre routing,
>>> it got directed in the right way, in this case delivered to port 22
>>> where ssh is listening.
>>>
>>> Do i have to recalculate checksum?? how??
>>>
>>> PS: of course, i have prepared de-dnat on outgoing packets... but for
>>> now they do not OUT-GO!
>>>
>>> PPS: of course ssh is up and responds correctly if i don't mangle
>>> destination port in pre routing.
>>>
>>> Thanks in advance for any idea.
>>>
>>> Giacomo, Italy

>> hello jacopo....
>>
>> have you try to sniff?what you see?
>>
>> peppe

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux from kernel.org and ipv6 Venkat Linux Networking 0 07-23-2008 10:39 PM
Linux Kernel 2.6.15-7 Jason Linux Networking 1 06-30-2006 10:48 PM
Is there any Linux Distri available to run several instances of Linux Kernel?. GS Linux Networking 5 05-20-2005 11:18 AM
IP fragmentation with linux kernel 2.4.x Alain FORCIOLI Linux Networking 1 01-21-2004 03:01 AM
Linux kernel 2.4.21 problem? Praveen Sharma Linux Networking 4 07-10-2003 10:29 PM



1 2 3 4 5 6 7 8 9 10 11