8

I have a comp with 2 eth cards, connected with patch-cord (direct eth. cable from 1st to 2nd).

The linux is installed, I want to send data from 1st network card to 2nd. And I want to force the packet to pass via cable. I can set up any ip on cards.

With ping I get counters on cards constant.

Is it possible with tcp/ip sockets?

PS. I need to write a program. which will send packets via eth, so stackoverflow-related question. There can be some OS-dependent way, they will help me too

osgx
  • 90,338
  • 53
  • 357
  • 513
  • Possible duplicate of http://serverfault.com/questions/127636/force-local-ip-traffic-to-an-external-interface – osgx Dec 16 '12 at 11:29
  • This was closed as off-topic two years after the question was answered. Yet the answers describe programs, and the asker says they are writing a program. Yes this is also on topic for other Stack Exchange site. I will flag this to be re-opened. – dcorking Oct 01 '14 at 15:28
  • dcorking, the task can be solved with using only OS-related tools, without creating new programs. The answer from z22 looks like what is needed. And at serverfault http://serverfault.com/questions/127636/force-local-ip-traffic-to-an-external-interface we have correct solution. – osgx Oct 01 '14 at 23:18
  • then the close reason is entirely misleading. I don't think we have a rule against duplicates of non-SO questions, but I would have no objection if we did. "software tools commonly used by programmers" is one of the 3 topics for this site, and IMHO when the answer puts those tools into a short shell script, it makes them even more on topic. – dcorking Oct 02 '14 at 06:53
  • I had success with `ip netns` exemplified here https://serverfault.com/a/861465/210994 – ThorSummoner Jan 03 '19 at 06:41

3 Answers3

7

Have a look in local routing table. With iproute2 tools installed do ip route show table local. As you can see, all packets destinated to your local IPs would never go thru NICs since they are marked as local.

To force packets go via ethernet card remove the appropriate route (i.e. ip route delete 192.168.122.1 dev eth0 table local). To restore this route just set the interface down and up: the kernel would do the work to insert these routes.

PoltoS
  • 1,232
  • 1
  • 12
  • 32
  • 1
    Idea seems good. Sure, it is no more possible to ping local interface, but it seems, it is neither possible to possible to ping anything on network (it seems kernel does not accept packets for this IP anymore). – Jérôme Pouiller Oct 06 '15 at 12:52
4

I tried the ip route ... table local method above. Either it doesn't work or I am doing something wrong.

The trick is to use a set of dummy IP addresses to force the kernel into routing it through the wire, and NAT to change it back to the real IP address.

Let eth0 and eth1 be the two ethernet cards; IP0 and IP1 its IP address; MAC0 and MAC1 its MAC address respectively. We will be using two dummy IP addresses: IP00 and IP11.

arp -s IP00 MAC0
arp -s IP11 MAC1
ip route add IP00 dev eth1
ip route add IP11 dev eth0
iptables -t nat -A POSTROUTING -d IP11 -j SNAT --to-source IP00
iptables -t nat -A POSTROUTING -d IP00 -j SNAT --to-source IP11
iptables -t nat -A PREROUTING -d IP00 -j DNAT --to-destination IP0
iptables -t nat -A PREROUTING -d IP11 -j DNAT --to-destination IP1

Use the dummy IP addresses IP00 and IP11 instead of the real one.

  • For those of us with iproute2, the equivalent for the `arp -s` commands is `ip neighbor add IP00 lladdr MAC0 dev eth0` (`dev` might be eth1, not sure). – kirbyfan64sos Jun 30 '19 at 23:06
0

You should be able to write a program that does that using packet sockets (protocol family PF_PACKET), but you'll have to handle the headers for the IP and higher layers yourself.

caf
  • 233,326
  • 40
  • 323
  • 462