Skip to content

Instantly share code, notes, and snippets.

@garronej
Last active February 20, 2019 17:17
Show Gist options
  • Save garronej/ff2c5444d00251315125f436beed65ce to your computer and use it in GitHub Desktop.
Save garronej/ff2c5444d00251315125f436beed65ce to your computer and use it in GitHub Desktop.
Setup EC2 instance with two network interfaces.
>>> See comments
src: https://unix.stackexchange.com/questions/4420/reply-on-same-interface-as-incoming
Detailed infos: http://irp.nain-t.net/doku.php/100iproute:020_iproute2
@garronej
Copy link
Author

NOTE: si on se trouve dans le cas ou on ne fixe pas la l'interface source lors de la request et que l'adresse de destination à un local link qui match alors les règles de class via sont ignorée.

@garronej
Copy link
Author

garronej commented Mar 1, 2018

NOTE: Mettons qu'on cherche a envoyer un packet vers 172.31.20.2 depuis l'interface eth1 ( 172.31.19.2 )
on vas chercher les règles de classe dev eth1 scop link avant de chercher les règle de classe via dev eth1 MAIS
on applique cette ordre pour chaque table.

C'est à dire que si dans la table t_eth1 on a seulement:
default via 172.31.16.1 dev eth1

On ne vas pas aller chercher dans la table main pour les règles de class scop link on vas diréctement validé la règle via et vérouiller l'adresse mac de destination sur 172.31.16.1 au lieu de 172.31.20.2. En suite on vas utiliser la règle 172.31.16.0/20 dev eth1 proto kernel scope link src 172.31.19.2 pour envoyer le packet.

Du point de vue des adresse IP source et déstination et de l'adress mac source on est bon mais on vas envoyer en couche 2 le packet a la gateway au lieux de l'envoyer diréctement à la déstination.

Du coup la solution est d'ajouter la règle:
172.31.16.0/20 dev eth1 proto kernel scope link src 172.31.19.2 a la table t_eth1.

Hipothèse confirmé par tests:

image

@garronej
Copy link
Author

garronej commented Mar 1, 2018

ip rule list
image

@garronej
Copy link
Author

garronej commented Mar 1, 2018

Bien penser a voir si le controle source/dest est désactivée sur toutes les interfaces AWS avec les quelles on fait des manips!

@garronej
Copy link
Author

garronej commented Mar 1, 2018

Route configuration load balancer:
image

@garronej
Copy link
Author

garronej commented Mar 3, 2018

Deploy semasim :

On load balancer:
/etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto eth0
iface eth0 inet dhcp
  #172.31.19.1 - 52.58.64.189 ( semasim.com )

  # Packet that have been marked "0x1" are looked up by table t_marked
  up ip rule add fwmark 1 table t_marked

  # talbe t_marked consist in sending every packet to localhost
  # ( so they can be handled by nginx )
  up ip route add local 0.0.0.0/0 dev lo table t_marked

  # We mark 0x1 every packet that match the folowing:
  # 1) Arive on this interface. ( --in-interface eth0 )
  # 2) Are not adressed to this interface. ( ! -d 172.31.19.1/32 )
  # 3) Come from one of the interfaces affected to runing semasim instances. ( -s 172.31.20.0/24 )
  up iptables -t mangle -A PREROUTING -p tcp --in-interface eth0 ! -d 172.31.19.1/32 -s 172.31.20.0/24 -j MARK --set-xmark 0x1/0xffffffff
  up iptables -t mangle -A PREROUTING -p udp --in-interface eth0 ! -d 172.31.19.1/32 -s 172.31.20.0/24 -j MARK --set-xmark 0x1/0xffffffff

  # When a packet toward a semasim instance has to be sent
  # by nginx we ensure that it is sent via eth0
  up ip rule add from all table t_pri
  up ip route add 172.31.20.0/24 dev eth0  proto kernel  scope link  src 172.31.19.1 table t_pri

iface eth1 inet dhcp
#172.31.19.2 - 52.57.54.73 ( sip.semasim.com )

  # We ensure that packet originated from interface eth1 are
  # correctly routed.
  up ip rule add from 172.31.19.2 table t_eth1
  up ip route add default via 172.31.16.1 dev eth1 table t_eth1
  up ip route add 172.31.16.0/20 dev eth1  proto kernel  scope link  src 172.31.19.2 table t_eth1

iface eth2 inet dhcp
iface eth3 inet dhcp
iface eth4 inet dhcp
iface eth5 inet dhcp
iface eth6 inet dhcp
iface eth7 inet dhcp

/etc/iproute2/rt_tables

#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
#
200 t_pri
201 t_marked
202 t_eth1

@garronej
Copy link
Author

garronej commented Mar 3, 2018

For deploy semasim !

On instances:

/etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto eth0
iface eth0 inet dhcp
#172.31.21.X - access only via VPN

  #Add route to the VPN gateway
  up ip route add 10.8.0.0/24 via 172.31.19.1 dev eth0

iface eth1 inet dhcp
  #172.31.20.X - interface dedicated to running semasim instances
  #This interface cannot access internet.

  # We route all trafic originated from this interface to the
  # load balancer except for local trafic that we route normaly.
  # [/etc/iproute2/rt_ables ] 200 t_eth1
  up ip rule add from 172.31.20.0/24 table t_eth1
  up ip route add default via 172.31.19.1 dev eth1 table t_eth1
  up ip route add 172.31.16.0/20 dev eth1  proto kernel  scope link  table t_eth1

iface eth2 inet dhcp
iface eth3 inet dhcp
iface eth4 inet dhcp
iface eth5 inet dhcp
iface eth6 inet dhcp
iface eth7 inet dhcp

/etc/iproute2/rt_tables

#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
#
200 t_eth1

@garronej
Copy link
Author

garronej commented Mar 4, 2018

Pour savoir dans quelle ordre sont évaluer les tables on fait ip rule list le nombre gauche représente la prio, évaluer dans l'ordre croissant.
Pour changer la priorité d'une table quand on fait ip rule add from ... table .... prio XXXX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment