LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Mark all incoming packets on connection (https://www.linuxquestions.org/questions/linux-networking-3/mark-all-incoming-packets-on-connection-800355/)

andrewhiggs 04-06-2010 09:57 AM

Mark all incoming packets on connection
 
Hi All,

I have found a few articles and tutorials which seem to do what I need but can't seem to get them to work.

My Setup :

I have a router which makes two ppp connections. PPP0 is my default route and is an uncapped ADSL. PPP1 is a Local Only (South Africa) account which has DNS resolving to its IP. PPP1 allows certain connections in. I want all packets coming in on PPP1 to be marked so that after they have been routed through our local servers they can go back out over PPP1.

Both connections use dynamically assigned ip addresses. I want to use PPP0 to make a connection to one of our stores, but when our stores connect to us they will be using PPP1. All packets from these incoming connections will need to be routed back over PPP1.

Hope that makes sense. If anything is unclear please ask.

Your assistance would be greatly appreciated.

Regards

Andrew Higgs

bakdong 04-06-2010 11:52 AM

That's quite a set up! Have you looked at the LARTC site (Linux Advanced Routing and Traffic Control) It has something similar there that you could probably adapt.

andrewhiggs 04-07-2010 07:08 AM

Hi Bakdong,

I have looked at these examples and similar ones. But they all seem to be doing Load Balancing and not quite what I am doing (trying to do).

I need to mark all incoming packets. These packets are then redirected to a local machine. When they come back to the router they need to go back out on the original incoming interface (and not the default route). Not sure if I am missing something but I can't seem to get it to work.

I will experiment a little more and maybe post a script later for people to look at. Perhaps this will help.

Regards

bakdong 04-07-2010 11:49 PM

It's definitely there. You will need a combination of ip rule tables, iptables to mark the packets, and conntrack. The load balancing part swaps the default route periodically with an optional weighting scheme. You don't have to incorporate that if you don't want it.

andrewhiggs 04-08-2010 03:35 AM

Hi Bakdong,

Thanks for the reply.

My ASCII art sucks but here goes. This is a diagram of the network:

Code:

mail----+
        |                                      +-----------------+
        |                                      |                |
        |              +------------ppp0------+      I        +--------store1
        |              |                      |      N        |
        |          +--------------+          |      T        |
        |          |              |          |      E        |
user1---+-----------+ router (vpn) |          |      R        |
        |          |              |          |      N        |
        |          +--------------+          |      E        |
        |              |                      |      T        |
        |              +------------ppp1------+                +--------store2
        |                                      |                |
        |                                      +-----------------+
user2---+

I want user1 and user2 to be able to make a connection to store1 and store2 using ppp0. In other words if the connection is initiated from the local network it should be routed over the default gateway (ppp0). This is easy to do.

The problem comes in when store1 or store2 make a connection to the mail server. They connect via ppp1 (their is a dns resolvable name on this connection which I can't have on ppp0). The packets from this connection are then redirected to mail. When mail routes back to the store, the default gateway is used and the connection is broken. The solution to this is add a route to allow traffic to store1 and store2 to go via ppp1, but then all connections from inside the local network are also affected.

So I am thinking I need to mark all new incoming connections on ppp1 so that they can be routed back to ppp1 later. What I have tried so far is this :
Code:

#!/bin/sh

/usr/bin/echo 1 > /proc/sys/net/ipv4/ip_dynaddr
/usr/bin/echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter

/usr/sbin/iptables-restore firewall.rules

/sbin/ip rule del from all fwmark 1 2>/dev/null
/sbin/ip rule del from all fwmark 2 2>/dev/null
/sbin/ip rule add fwmark 1 table Uncapped
/sbin/ip rule add fwmark 2 table LocalOnly
/sbin/ip route flush cache

# THESE TWO LINES WILL BE SET IN IP_UP

#/sbin/ip route add table Uncapped default dev ppp0
#/sbin/ip route add table LocalOnly default dev ppp1

The file firewall.rules referred above includes the following in it's mangle section :
Code:

*mangle
:PREROUTING ACCEPT [81132:42738999]
:INPUT ACCEPT [16988:5371568]
:FORWARD ACCEPT [64012:37349459]
:OUTPUT ACCEPT [12841:2317402]
:POSTROUTING ACCEPT [76837:39665453]
-N Uncapped
-A Uncapped -j MARK --set-mark 1
-A Uncapped -j CONNMARK --save-mark
-N LocalOnly
-A LocalOnly -j MARK --set-mark 2
-A LocalOnly -j CONNMARK --save-mark
-A PREROUTING -i eth1 -p tcp -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
-A PREROUTING -i ppp1 -p tcp -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j LocalOnly
COMMIT

What am I doing wrong? How does all of this affect the main routing table?

Regards

Andrew Higgs

zhjim 04-08-2010 03:53 AM

If the stores trying to connect to the mail server and from there back to them over ppp1 why not just have all the packets coming from the mail server go through ppp1?

bakdong 04-08-2010 07:19 AM

You've got no pref numbers in your ip rule statements. Not sure that's the problem though. What's the output of :

ip rule
ip route show table Uncapped/Localonly

andrewhiggs 04-08-2010 08:40 AM

Quote:

Originally Posted by zhjim (Post 3928325)
If the stores trying to connect to the mail server and from there back to them over ppp1 why not just have all the packets coming from the mail server go through ppp1?

I must truly be missing something simple here. Or I really don't understand something. I have simplified the solution as above. Thanks it is a very good suggestion.

Here are my scripts to hopefully achieve the above :

mangle has been stripped to the following:
Code:

*mangle
:PREROUTING ACCEPT [81132:42738999]
:INPUT ACCEPT [16988:5371568]
:FORWARD ACCEPT [64012:37349459]
:OUTPUT ACCEPT [12841:2317402]
:POSTROUTING ACCEPT [76837:39665453]
-N LocalOnly
-A LocalOnly -j MARK --set-mark 2
-A LocalOnly -j CONNMARK --save-mark
-A PREROUTING -i eth1 -s 172.16.48.200 -p tcp -m state --state ESTABLISHED,RELATED -j LocalOnly
COMMIT

Store1 (196.210.xxx.xxx) and Store2 (196.215.xxx.xxx) are both able to make their connections as expected while the main routing table has their routes set to ppp1. To test I thought I would activate the mangling in the firewall and remove Store2's (route del -net 196.215.0.0/16) main routing entry. When I do this Store1 remains connected but Store2 loses it's connection.

What am I missing? If my hair was longer I would be pulling it out. :-)

Regards

Andrew Higgs

andrewhiggs 04-08-2010 08:47 AM

Quote:

Originally Posted by bakdong (Post 3928472)
You've got no pref numbers in your ip rule statements. Not sure that's the problem though. What's the output of :

ip rule
ip route show table Uncapped/Localonly

What exactly is the pref number? Is it not 1 for Uncapped and 2 for LocalOnly?

The output of ip rule :
Code:

0:        from all lookup local
32764:        from all fwmark 0x2 lookup LocalOnly
32765:        from all fwmark 0x1 lookup Uncapped
32766:        from all lookup main
32767:        from all lookup default

The output of ip route show table LocalOnly:
Code:

default dev ppp1  scope link
The output of ip route show table Uncapped:
Code:

default dev ppp0  scope link
Thanks.

Regards

Andrew Higgs

bakdong 04-08-2010 12:47 PM

Ok, so it's nothing to do with the pref number (32764-7 in your case) I can't see why it's not working. Sorry.

andrewhiggs 04-08-2010 12:56 PM

Quote:

Originally Posted by bakdong (Post 3928797)
Ok, so it's nothing to do with the pref number (32764-7 in your case) I can't see why it's not working. Sorry.

Thanks for the assistance. I appreciate the effort you put into trying to help me out.

zhjim 04-09-2010 03:02 AM

Maybe something like this will settle the dust.

Code:

ip rule add from mail.server.i.p to sto.re.1.ip table LocalOnly
ip rule add from mail.server.i.p to sto.re.2.ip table LocalOnly

This would prevent you from marking. If this does not suite I guess we best start from scratch?

bakdong 04-09-2010 03:20 AM

Stabbing in the dark now.... do you have specific routes defined in the main table? If you have, wouldn't these take priority over the default routes?

andrewhiggs 04-09-2010 04:49 AM

Quote:

Originally Posted by zhjim (Post 3929580)
Maybe something like this will settle the dust.

Code:

ip rule add from mail.server.i.p to sto.re.1.ip table LocalOnly
ip rule add from mail.server.i.p to sto.re.2.ip table LocalOnly

This would prevent you from marking. If this does not suite I guess we best start from scratch?

Thanks zhjim.

I have no problem with not needing to mark packets. In fact the simpler the solution the better. :-)

I have reset my firewall to remove all marking etc. I have flushed all rules relating to marking. This is what ip rule puts out now :
Code:

0:        from all lookup local
32763:        from 172.16.48.200 lookup LocalOnly
32766:        from all lookup main
32767:        from all lookup default

My main routing table still contains routes which send packets destined for 196.210.0.0 and 196.215.0.0 to ppp1. I assume these should be removed. If I do this routing to those ip addresses goes through the default gateway. :-(

What should be in ip route show table LocalOnly? This is what it currently has in it :
Code:

default dev ppp1  scope link
Should there be anything else? If I can have a purely routing solution (i.e. no change to the firewall) all the better.

Thanks again.

Regards

Andrew

andrewhiggs 04-09-2010 04:53 AM

Quote:

Originally Posted by bakdong (Post 3929592)
Stabbing in the dark now.... do you have specific routes defined in the main table? If you have, wouldn't these take priority over the default routes?

Thanks bakdong,

Yes. The main routing table routes all packets destined for 196.210.0.0 and 196.215.0.0 to ppp1. The end goal is to have these removed so that connections initiated from our internal network (i.e. user1 and user2) are routed via ppp0.

Regards

Andrew


All times are GMT -5. The time now is 12:08 AM.