LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-15-2013, 11:07 AM   #1
Jroch
LQ Newbie
 
Registered: Jun 2013
Posts: 4

Rep: Reputation: Disabled
Centos static route to nic depending port


Hi,

I was wondering if we could route traffic to a certain nic (outgoing) drpending the port used

Example :

Trafic to 192.168.1.2:8080 on eth0
Trafic to 192.168.1.2:8090 on eth1

Thank you
 
Old 06-16-2013, 04:49 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,344

Rep: Reputation: Disabled
Your question is a bit ambiguous, as you're asking about routing but your example seems to suggest load balancing across interfaces based on TCP port numbers.

In the first case the answer is yes. You can route traffic to different gateways using policy routing, and destination port number is one of the (meny) criteria you can use to select a gateway/routing table.

I don't believe bonded ethernet interfaces can load balance based on the destination port number, but I could be wrong.
 
Old 06-16-2013, 05:46 AM   #3
Jroch
LQ Newbie
 
Registered: Jun 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
In fact i have one machine with 4 nics and i have a dsl line on each nic. I need to send 4 tcp streams but the destination of each tcp stream is the same (anorher server having a fiber connection) i know about shareband systems but i guess something is doable with routing
 
Old 06-16-2013, 06:04 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,344

Rep: Reputation: Disabled
Policy routing will work in that scenario.

You can create four different routing tables (with the ip route command), each with a different gateway, and direct packets to the desired table with routing rules (created with the ip rule command). IP rules can't reference port numbers directly, but you can use iptables to mark packets as desired and use the "fwmark" selector.

As to how this is best done in CentOS, I'm afraid I have no idea.
 
Old 06-16-2013, 06:28 AM   #5
Jroch
LQ Newbie
 
Registered: Jun 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
So with ip routing i can say that trafic on port x goes to nic x and trafic for port y goes to port y ?
Is that working only for port ? Or can we make that with dns too ?
Example :

The distant server is assigned 4 dns names and i route each name on a pecific nic (just an idea)

Coz all i was able to do is route trafic to a specific ip to a specific nic with routing
 
Old 06-16-2013, 06:45 AM   #6
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,344

Rep: Reputation: Disabled
Quote:
Originally Posted by Jroch View Post
So with ip routing i can say that trafic on port x goes to nic x and trafic for port y goes to port y ?
You can say that traffic to different ports go through different gateways, yes.

Assume a scenario where you have 2 NICs, eth1 and eth2, each connected to a DSL line. eth1 has the IP address 1.1.1.1 and eth2 has 2.2.2.2. Using 1.1.1.254 as your gateway would route all Internet traffic through eth1, while having 2.2.2.254 as your gateway would route traffic through eth2.

Policy routing means having separate routing tables, and decide based on some criteria which packets get processed by which table. In this case, you could create two tables with different gateways:
Code:
ip route add table 10 0.0.0.0/0 via 1.1.1.254
ip route add table 20 0.0.0.0/0 via 2.2.2.254
These tables are not used until you create an IP rule directing certain types of packets to a specific table.

Assume we want traffic to IP address 5.5.5.5 and port 80 to be processed by table 10, while traffic to the same IP address on port 443 should be handled by table 20. This can be accomplished by firewall-marking the packets and then use the IP rule "fwmark" selector:
Code:
iptables -t mangle -A FORWARD -d 5.5.5.5/32 -p tcp --dport 80 -j MARK --set-mark 80
iptables -t mangle -A FORWARD -d 5.5.5.5/32 -p tcp --dport 443 -j MARK --set-mark 443
ip rule add fwmark 80 table 10
ip rule add fwmark 443 table 20
I'm assuming you're routing traffic from some other network, hence the use of the FORWARD chain. For locally generated traffic, the OUTPUT chain must be used instead.
Quote:
Originally Posted by Jroch View Post
Is that working only for port ? Or can we make that with dns too ?
Example :

The distant server is assigned 4 dns names and i route each name on a pecific nic (just an idea)
DNS names are translated into IP addresses before any routing is performed. You cannot route based on names.
 
Old 06-16-2013, 07:15 AM   #7
Jroch
LQ Newbie
 
Registered: Jun 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi,

Thank you for you answe, that make sense

I never used iptables on centos but i assume that table 10 means first line in your conf example ?
 
Old 06-16-2013, 08:15 AM   #8
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,344

Rep: Reputation: Disabled
Not quite sure what you're asking, but the line:
Code:
iptables -t mangle -A FORWARD -d 5.5.5.5/32 -p tcp --dport 80 -j MARK --set-mark 80
...means "alter packets going through the routing engine destined for IP address 5.5.5.5 and TCP port 80, and mark them with the number 80". 80 is just a randomly chosen number that I refer to in the IP rule later.

The line:
Code:
ip rule add fwmark 80 table 10
...says "make sure packets marked with the firewall mark '80' are sent to table 10", which indeed refers to the table created by the very first line in my example (ip route add table 10 0.0.0.0/0 via 1.1.1.254). Again, the number "10" is just a randomy chosen identifier for that routing table.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB modem port static mapping in CentOS-6.4 sekarlinux Linux - Kernel 1 06-13-2013 09:45 AM
CentOS 6 - Persistent Static Route karim.wallani Linux - Newbie 4 05-24-2013 08:52 AM
CentOS 6 - Persistent Static Route karim.wallani Linux - Networking 5 06-29-2012 04:42 PM
Default route /gateway for Multi NIC port Linux server lkwashok Linux - Networking 1 09-22-2010 07:30 AM
2 NIC's - one gateway, setup Static route to Host baba_jaga Linux - Newbie 1 04-14-2009 02:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration