Skip to contentSkip to conversation

2 problems with my net/conntrack helper for MSN

PATCH Filip Sneppe (Cronos) 3 participants 4 messages May 31, 2003 – Jun 1, 2003

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi,

I am trying to write a conntrack/NAT helper for MSN Messenger file transfers, and I am unable to solve two problems at the moment.

Problem 1. Summary: Can I call ip_conntrack_change_expectation()
from ip_conntrack_msn, and if so, how do I do this properly ?

First, two small examples on how MSN file transfer works:

Microsoft
Switchboard
Servers
(Internet)
^ ^
| |
| |
NAT NAT
box box (MASQUERADE)
A B
| |
| |
Host Host
A B

When logging into MSN, host A and B initiate a connection to the MS switchboard servers.

If A wants to send a file to host B, and B accepts this, Host A sends its IP+port to a switchboard server. On Nat box A, this allows us to set up an expectation like this:

?:? -> IP_A:port_A

We have no idea what IP may be connecting to host A as we only have a connection to the switchboard server.

The switchboard server sends that message on to host B, so NAT box B is able to set up an expectation like this:

IP_B:? -> IP_A:port_A

(where IP_A and port_A can be the public IP of NAT box A if ip_nat_msn were fully functional yet).

Now imagine a scenario where Host A and B are behind the same NAT box:

        Microsoft
       Switchboard
         Servers
        (Internet)
           ^ ^
           | |
           | |
           NAT  (MASQUERADE) 
           box  
          /   \
         /     \
        |       |
       Host    Host
        A       B

Now what happens is, when host A offers its file for download, an expectation is created on the NAT box when Host A offers the accepted file:

?:? -> IP_A:port_A

But a switchboard server will inform host B that it can connect to host A to download the file. ip_conntrack_msn tries to create a second expectation:

IP_B:? -> IP_A:port_A

The ip_conntrack_expect_related() call fails with -EBUSY, since the expectation clashes with an already existing expectation "?:? -> IP_A:port_A".
(see help() function in ip_conntrack_msn.c).

Now, my idea is that, if both hosts are behind the same NAT machine, and the creation of the second expectation fails with -EBUSY, the expectation that already exists should be changed to incorporate the additional information about the IP address of host B.

In other words, my take on this is:

- (a) If a MASQ box has a host behind it that offers a file, it can only set up an expectation with the IP+port of the host offering the file.
- (b) If a MASQ box has a host behind it that will download a file, it can set up an expectation with both IP addresses and the TCP port of the offering host.
- (c) If a MASQ box has both sending and receiving hosts behind it, but cannot know this beforehand, the chronology should be that:
- an expectation is set up as in (a)
- if the expectation setup in (b) fails, the existing expectation from (a) should be modified from within ip_conntrack_msn to include some more information about the host that will download a file.

Now, I don't know how to call ip_conntrack_change_expected() from within a conntracker. More specifically, I don't know how to get a pointer to the existing expectation in a clean way (well, I couldn't even get to this info by copying bits and pieces from ip_conntrack_core.c since things turned out to be really messy quickly). What would be the recommended way to do this ?

Note that I'm not the great netfilter hacker I would like to be, but I just want to contribute something back every once in a while that hopefully some people may find useful, so I hope I'll be able to understand any possible hints/solutions :-)

Problem 2. Summary: I don't think my NAT helper's "help" function
is getting called !? What is happening ?

When I test things in a setup with two machines (192.168.0.10 and 172.16.0.33) behind a NAT Internet gateway that has a simple -j MASQUARADE on the Internet interface, I
get these loggings from ip_conntrack_msn and ip_nat_msn:

For the first expectation:

ip_conntrack_msn: Direction: ORIG
ip_conntrack_msn: tuple d23191f0: 6 172.16.0.33:32770 -> 207.46.108.52:1863
ip_conntrack_msn: tuple d231920c: 6 207.46.108.52:1863 -> 195.162.210.151:32770
ip_conntrack_msn: Skipping '\r' or '\n' at beginning of payload
ip_conntrack_msn: parse_msn_header(): Entered
ip_conntrack_msn: Leaving next_line() 3540721256 219 3540721237 17
ip_conntrack_msn: Leaving next_line() 3540721306 169 3540721256 48
ip_conntrack_msn: parse_msn_header(): Found an invitation!!
ip_conntrack_msn: Leaving next_line() 3540721308 167 3540721306 0
ip_conntrack_msn: Leaving next_line() 3540721336 139 3540721308 26
ip_conntrack_msn: parse_msn_header(): Found Invitation-Command, accepted = 1
ip_conntrack_msn: Leaving next_line() 3540721363 112 3540721336 25
ip_conntrack_msn: Leaving next_line() 3540721388 87 3540721363 23
ip_conntrack_msn: parse_msn_header(): Found 'IP-Address: 172.16.0.33'
ip_conntrack_msn: Leaving next_line() 3540721400 75 3540721388 10
ip_conntrack_msn: parse_msn_header(): Found 'Port: 6891'
ip_conntrack_msn: Leaving next_line() 3540721419 56 3540721400 17
ip_conntrack_msn: Leaving next_line() 3540721446 29 3540721419 25
ip_conntrack_msn: Leaving next_line() 3540721473 2 3540721446 25
ip_conntrack_msn: Leaving next_line() 3540721475 0 3540721473 0
ip_conntrack_msn: parce_msn_header(): Returning bytes_todo: 176
ip_conntrack_msn: help(): bytes_todo: 176 - accepted: 1 - address:
172.16.0.33 - port: 6891
ip_conntrack_msn: help(): 172.16.0.33:6891 offers file in ORIG at offsets 139 and 158
ip_conntrack_msn: help(): Adding expectation ?:? -> 172.16.0.33:6891
ip_conntrack_msn: help(): ip_conntrack_expect_related() returned 0

And for the second expectation:

ip_conntrack_msn: Direction: REPLY
ip_conntrack_msn: tuple d2319830: 6 192.168.0.10:33021 -> 207.46.108.52:1863
ip_conntrack_msn: tuple d231984c: 6 207.46.108.52:1863 -> 195.162.210.151:33021
ip_conntrack_msn: parse_msn_header(): Entered
ip_conntrack_msn: Leaving next_line() 3540721287 238 3540721224 61
ip_conntrack_msn: Leaving next_line() 3540721306 219 3540721287 17
ip_conntrack_msn: Leaving next_line() 3540721356 169 3540721306 48
ip_conntrack_msn: parse_msn_header(): Found an invitation!!
ip_conntrack_msn: Leaving next_line() 3540721358 167 3540721356 0
ip_conntrack_msn: Leaving next_line() 3540721386 139 3540721358 26
ip_conntrack_msn: parse_msn_header(): Found Invitation-Command, accepted = 1
ip_conntrack_msn: Leaving next_line() 3540721413 112 3540721386 25
ip_conntrack_msn: Leaving next_line() 3540721438 87 3540721413 23
ip_conntrack_msn: parse_msn_header(): Found 'IP-Address: 172.16.0.33'
ip_conntrack_msn: Leaving next_line() 3540721450 75 3540721438 10
ip_conntrack_msn: parse_msn_header(): Found 'Port: 6891'
ip_conntrack_msn: Leaving next_line() 3540721469 56 3540721450 17
ip_conntrack_msn: Leaving next_line() 3540721496 29 3540721469 25
ip_conntrack_msn: Leaving next_line() 3540721523 2 3540721496 25
ip_conntrack_msn: Leaving next_line() 3540721525 0 3540721523 0
ip_conntrack_msn: parce_msn_header(): Returning bytes_todo: -31
ip_conntrack_msn: help(): bytes_todo: 65505 - accepted: 1 - address:
172.16.0.33 - port: 6891
ip_conntrack_msn: help(): 172.16.0.33:6891 offers file in REPLY at offsets 201 and 220
ip_conntrack_msn: help(): Adding expectation 192.168.0.10:? -> 172.16.0.33:6891
ip_conntrack_msn: help(): ip_conntrack_expect_related() returned -16

Then I have:

ip_conntrack_msn: Direction: ORIG
ip_conntrack_msn: tuple d2319830: 6 192.168.0.10:33021 -> 207.46.108.52:1863
ip_conntrack_msn: tuple d231984c: 6 207.46.108.52:1863 -> 195.162.210.151:33021
ip_conntrack_msn: Skipping '\r' or '\n' at beginning of payload
ip_conntrack_msn: Leaving next_line() 3354879790 4294967226 3354879721 67
ip_conntrack_msn: parse_msn_command(): After next_line():
ip_nat_msn: msn_nat_expected: We have a connection!
ip_nat_msn: msn_nat_expected: We have a connection!

What I do not understand at all is that the ip_nat_msn's module help() function has this statement:

DEBUGP("ip_nat_msn: msn_nat_help(): Entered\n");

at the beginning of the function, but it appears to never get called, even though ip_nat_msn is loaded. Yet, there are two messages logged from the ip_nat_expected() function in the NAT helper.
I have absolutely no idea why I cannot see any logging messages from the NAT module's help() function...

I hope someone can give me some ideas/hints on how to solve these problems.

Thanks in advance,
Filip

Note that for people who want to test this conntracker, there is at least one MSN client under Linux: amsn. It's the client used by my while writing this. Despite my problems described above, the conntrack part of the code should actually work in practise (still some bugs to fix though (most importantly the bytes_todo counter that is supposed to track messages spread over multiple packets is not decremented correctly!)...).

Documentation/Configure.help +10 -0
diff -urN -X dontdiff linux-2.4.20-orig/Documentation/Configure.help linux-2.4.20-msn/Documentation/Configure.help
@@ -2508,6 +2508,16 @@
   If you want to compile it as a module, say M here and read
   <file:Documentation/modules.txt>.  If unsure, say `N'.
 
+MSN Messenger protocol support
+CONFIG_IP_NF_MSN
+  Tracking MSN Messenger file transfers is problematic:  a special 
+  helper is needed to track the random ports are used to send the 
+  files. This option allows users to track MSN Messenger file 
+  transfer traffic.
+
+  If you want to compile it as a module, say M here and read
+  <file:Documentation/modules.txt>.  If unsure, say `Y'.
+
 IRC Send/Chat protocol support
 CONFIG_IP_NF_IRC
   There is a commonly-used extension to IRC called
Makefile +1 -1
diff -urN -X dontdiff linux-2.4.20-orig/Makefile linux-2.4.20-msn/Makefile
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 4
 SUBLEVEL = 20
-EXTRAVERSION =
+EXTRAVERSION = -msn
 
 KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 
include/linux/netfilter_ipv4/ip_conntrack.h +3 -0
diff -urN -X dontdiff linux-2.4.20-orig/include/linux/netfilter_ipv4/ip_conntrack.h linux-2.4.20-msn/include/linux/netfilter_ipv4/ip_conntrack.h
@@ -58,12 +58,14 @@
 };
 
 /* Add protocol helper include file here */
+#include <linux/netfilter_ipv4/ip_conntrack_msn.h>
 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
 
 /* per expectation: application helper private data */
 union ip_conntrack_expect_help {
 	/* insert conntrack helper private data (expect) here */
+	struct ip_ct_msn_expect exp_msn_info;
 	struct ip_ct_ftp_expect exp_ftp_info;
 	struct ip_ct_irc_expect exp_irc_info;
 
@@ -77,6 +79,7 @@
 /* per conntrack: application helper private data */
 union ip_conntrack_help {
 	/* insert conntrack helper private data (master) here */
+	struct ip_ct_msn_master ct_msn_info;
 	struct ip_ct_ftp_master ct_ftp_info;
 	struct ip_ct_irc_master ct_irc_info;
 };
include/linux/netfilter_ipv4/ip_conntrack_msn.h +27 -0
diff -urN -X dontdiff linux-2.4.20-orig/include/linux/netfilter_ipv4/ip_conntrack_msn.h linux-2.4.20-msn/include/linux/netfilter_ipv4/ip_conntrack_msn.h
@@ -0,0 +1,27 @@
+#ifndef _IP_CONNTRACK_MSN_H
+#define _IP_CONNTRACK_MSN_H
+/* MSN Messenger tracking. */
+
+#ifdef __KERNEL__
+#include <linux/netfilter_ipv4/lockhelp.h>
+
+DECLARE_LOCK_EXTERN(ip_msn_lock);
+
+#define MSN_PORT                         1863
+#endif
+
+/* This structure is per expected connection */
+struct ip_ct_msn_expect {
+	u_int16_t port;            /* port number of this expectation */
+	u_int16_t offset_address;  /* offset of embedded IP address in packet */
+	u_int16_t length_address;  /* length of embedded IP address in packet */
+	u_int16_t offset_port;     /* offset of embedded port number in packet */
+	u_int16_t length_port;     /* length of embedded port number in packet */
+};
+
+/* This structure exists only once per master */
+struct ip_ct_msn_master {
+	u_int16_t bytes_todo;      /* if message is split over multiple packets */
+};
+
+#endif /* _IP_CONNTRACK_MSN_H */
net/ipv4/netfilter/Config.in +8 -0
diff -urN -X dontdiff linux-2.4.20-orig/net/ipv4/netfilter/Config.in linux-2.4.20-msn/net/ipv4/netfilter/Config.in
@@ -8,6 +8,7 @@
 if [ "$CONFIG_IP_NF_CONNTRACK" != "n" ]; then
   dep_tristate '  FTP protocol support' CONFIG_IP_NF_FTP $CONFIG_IP_NF_CONNTRACK
   dep_tristate '  IRC protocol support' CONFIG_IP_NF_IRC $CONFIG_IP_NF_CONNTRACK
+  dep_tristate '  MSN Messenger protocol support' CONFIG_IP_NF_MSN $CONFIG_IP_NF_CONNTRACK
 fi
 
 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
@@ -67,6 +68,13 @@
           define_tristate CONFIG_IP_NF_NAT_IRC $CONFIG_IP_NF_NAT
         fi
       fi
+      if [ "$CONFIG_IP_NF_MSN" = "m" ]; then
+        define_tristate CONFIG_IP_NF_NAT_MSN m
+      else
+        if [ "$CONFIG_IP_NF_MSN" = "y" ]; then
+          define_tristate CONFIG_IP_NF_NAT_MSN $CONFIG_IP_NF_NAT
+        fi
+      fi
       # If they want FTP, set to $CONFIG_IP_NF_NAT (m or y), 
       # or $CONFIG_IP_NF_FTP (m or y), whichever is weaker.  Argh.
       if [ "$CONFIG_IP_NF_FTP" = "m" ]; then
net/ipv4/netfilter/Makefile +5 -0
diff -urN -X dontdiff linux-2.4.20-orig/net/ipv4/netfilter/Makefile linux-2.4.20-msn/net/ipv4/netfilter/Makefile
@@ -32,6 +32,10 @@
 obj-$(CONFIG_IP_NF_CONNTRACK) += ip_conntrack.o
 
 # connection tracking helpers
+obj-$(CONFIG_IP_NF_MSN) += ip_conntrack_msn.o
+ifdef CONFIG_IP_NF_NAT_MSN
+	export-objs += ip_conntrack_msn.o
+endif
 obj-$(CONFIG_IP_NF_FTP) += ip_conntrack_ftp.o
 ifdef CONFIG_IP_NF_NAT_FTP
 	export-objs += ip_conntrack_ftp.o
@@ -45,6 +49,7 @@
 # NAT helpers 
 obj-$(CONFIG_IP_NF_NAT_FTP) += ip_nat_ftp.o
 obj-$(CONFIG_IP_NF_NAT_IRC) += ip_nat_irc.o
+obj-$(CONFIG_IP_NF_NAT_MSN) += ip_nat_msn.o
 
 # generic IP tables 
 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
net/ipv4/netfilter/ip_conntrack_msn.c +509 -0

509 lines not on this page · raw message

net/ipv4/netfilter/ip_nat_msn.c +207 -0

207 lines not on this page · raw message

#1 lore raw
Quoted 1 line · Filip Sneppe (Cronos)

Hi,

Hi.

Quoted 6 lines
        Microsoft
       Switchboard
         Servers
        (Internet)
           ^ ^
           | |
Quoted 8 lines
           | |
           NAT  (MASQUERADE) 
           box  
          /   \
         /     \
        |       |
       Host    Host
        A       B

This problem isn't only relevant for MSN, also for every other nat helper. The solution is acutally quite simple:

iptables -t nat -A POSTROUTING -s local_net -d local_net -j SNAT --to-source public_ip

This also helps when you want to porforward from local_net instead from the outside.

The helper doesn't have to differentiate.

Quoted 1 line

Filip

Bye,

Peter Surda (Shurdeek) <[email protected]>, ICQ 10236103, +436505122023

Signature · 1 line
              To boldly go where I surely don't belong.
#2 lore raw
In reply to Filip Sneppe (Cronos)
Quoted 5 lines · Filip Sneppe (Cronos)

I am trying to write a conntrack/NAT helper for MSN Messenger file transfers, and I am unable to solve two problems at the moment.

Problem 1. Summary: Can I call ip_conntrack_change_expectation()
from ip_conntrack_msn, and if so, how do I do this properly ?

hm, I don't see why you shouldn't be able to do so.

Quoted 9 lines

If A wants to send a file to host B, and B accepts this, Host A sends its IP+port to a switchboard server. On Nat box A, this allows us to set up an expectation like this:

?:? -> IP_A:port_A

We have no idea what IP may be connecting to host A as we only have a connection to the switchboard server.

exactly like the IRC DCC case

Quoted 7 lines

Now imagine a scenario where Host A and B are behind the same NAT box:

Now what happens is, when host A offers its file for download, an expectation is created on the NAT box when Host A offers the accepted file:

Quoted 7 lines

?:? -> IP_A:port_A

But a switchboard server will inform host B that it can connect to host A to download the file. ip_conntrack_msn tries to create a second expectation:

IP_B:? -> IP_A:port_A

This is true. It was never solved for the IRC case. I though it is a not too common case, and didn't further care about the problem.

Quoted 5 lines

Now, my idea is that, if both hosts are behind the same NAT machine, and the creation of the second expectation fails with -EBUSY, the expectation that already exists should be changed to incorporate the additional information about the IP address of host B.

This should work, yes.

Quoted 7 lines

Now, I don't know how to call ip_conntrack_change_expected() from within a conntracker. More specifically, I don't know how to get a pointer to the existing expectation in a clean way (well, I couldn't even get to this info by copying bits and pieces from ip_conntrack_core.c since things turned out to be really messy quickly). What would be the recommended way to do this ?

It isn't too difficult. You iterate over the sibling_list and call exp_for_packet() on every item.

The pptp code needs to delete expectations on the list from within the conntrack helper, see
http://cvs.netfilter.org/netfilter-extensions/helpers/pptp/ip_conntrack_pptp.c
and search for other_exp.

Since you're using F_REUSE_EXPECT, I assume that there can always be one file transfer per client connection. This would mean that even calling exp_for_packet is not necessarry.

Quoted 5 lines

Problem 2. Summary: I don't think my NAT helper's "help" function
is getting called !? What is happening ?

I have absolutely no idea why I cannot see any logging messages from the NAT module's help() function...

Neither have I, sorry.

Quoted 2 lines

Thanks in advance,
Filip

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#3 lore raw
Quoted 4 lines · Harald Welte
Quoted 4 lines

Now what happens is, when host A offers its file for download, an expectation is created on the NAT box when Host A offers the accepted file:

Quoted 8 lines
Quoted 7 lines

?:? -> IP_A:port_A

But a switchboard server will inform host B that it can connect to host A to download the file. ip_conntrack_msn tries to create a second expectation:

IP_B:? -> IP_A:port_A

This is true. It was never solved for the IRC case.

See my previous post, the mentioned rule should solve it.

Quoted 1 line

- Harald Welte <[email protected]> http://www.netfilter.org/

Bye,

Peter Surda (Shurdeek) <[email protected]>, ICQ 10236103, +436505122023

Signature · 1 line
       My mind is like a steel trap - rusty and illegal in 37 states.