SetSourceIp.readme 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. SetSourceIP is an example program and test tool that will set the source IP address in the socket of clients using the HTTPClientSession or HTTPSClientSession classes.
  2. Setting the source IP address in a client is not a common thing to do, but could be useful in the following sitations:
  3. 1) In a multi-homed system where multiple network interfaces are used and the client http/https traffic must go out through the interface that is not considered the "default" routed interface. This is sometimes referred to as traffic separation or traffic isolation. Setting the source IP address is one part of the puzzle in order to allow this to work. Additonal OS routing setup is required to successfully make this work.
  4. 2) In a clustered environment, where two hosts provide a client service in a Active/Standby situation and it is desirable to present a single source IP address to the remote server and/or intermediate firewalls. This might be referred to as Floating, Mobile, or Virtual IP addressing.
  5. 3) In a host where a network interface supports multiple IP addresses and the client wants to assign the source IP address for some application specific reason.
  6. For more in depth information, please do additional research with keywords such as:
  7. Linux Multihomed
  8. Routing with multiple network interfaces
  9. Linux Advanced Routing & Traffic Control
  10. What follows is an example of the routing setup required in a Linux VM in order to experiment and get comfortable with this type of source IP address routing.
  11. WARNING: Thoroughly verify and validate the procedure and all the included commands in a test environment before using in any sort of production system. No promises are made to the fitness of any information that is provided in this document.
  12. 1- Create a VM in your vm environment of choice. (e.g. Virtual Box, VmWare Server)
  13. 2- Make sure that the VM has two network interfaces defined and that they are attached to through NAT. NAT is required so that they both can reach the Internet when required.
  14. 3- Install your OS of choice on the VM. (e.g. OpenSuse Leap 42.3)
  15. 4- After the VM is started up, check that the two network interfaces are well defined (i.e. they are 'UP', they have a reasonable IP address, netmask, broadcast address.) Usually they will be named 'eth0' and 'eth1', but it might be something else in your OS. Make sure that the Internet can be reachable at least on one of the interfaces.
  16. Command:
  17. ifconfig -a
  18. 5- Find which one of the two interfaces is the 'default' destination router. Usually it will be the 'eth0' interface, but it might be the 'eth1' one instead.
  19. Command:
  20. netstat -r
  21. 6- For the remainer of this procedure let's assume that the 'eth0' interface is the default route for all traffic.
  22. 7- With 'eth0' the default route, then 'eth1' will be used as the alternate route.
  23. 8- Here is an example output that you might have with some network commands:
  24. Command:
  25. ifconfig eth0
  26. Example Output:
  27. eth0 Link encap:Ethernet HWaddr 08:00:27:79:1A:85
  28. inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
  29. inet6 addr: fe80::a00:27ff:fe79:1a85/64 Scope:Link
  30. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  31. RX packets:9833 errors:0 dropped:0 overruns:0 frame:0
  32. TX packets:7859 errors:0 dropped:0 overruns:0 carrier:0
  33. collisions:0 txqueuelen:1000
  34. RX bytes:4502896 (4.2 Mb) TX bytes:1322623 (1.2 Mb)
  35. Command:
  36. ifconfig eth1
  37. Example Output:
  38. eth1 Link encap:Ethernet HWaddr 08:00:27:AE:16:3E
  39. inet addr:10.0.3.15 Bcast:10.0.3.255 Mask:255.255.255.0
  40. inet6 addr: fe80::a00:27ff:feae:163e/64 Scope:Link
  41. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  42. RX packets:1 errors:0 dropped:0 overruns:0 frame:0
  43. TX packets:41 errors:0 dropped:0 overruns:0 carrier:0
  44. collisions:0 txqueuelen:1000
  45. RX bytes:590 (590.0 b) TX bytes:7683 (7.5 Kb)
  46. Command:
  47. netstat -r
  48. Example Output:
  49. Kernel IP routing table
  50. Destination Gateway Genmask Flags MSS Window irtt Iface
  51. default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
  52. 10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
  53. 10.0.3.0 * 255.255.255.0 U 0 0 0 eth1
  54. Command:
  55. ip route
  56. Example Output:
  57. default via 10.0.2.2 dev eth0 proto dhcp
  58. 10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15
  59. 10.0.3.0/24 dev eth1 proto kernel scope link src 10.0.3.15
  60. 9- In the above output the following information is available.
  61. eth0 network interface IP address is 10.0.2.15
  62. eth1 network interface IP address is 10.0.3.15
  63. default router on eth0 network is 10.0.2.2
  64. 10- What is missing is the router used on the 'eth1' interface. In that case it might require digging in the VM host application (e.g. Virtual Box, VmWare Server) to see if the settings are explicitly set in the Network Management tabs or by checking in the host OS network setup (using similar network commands as on the target environment) to find the router used in that particular network. It might not be perfectly obvious in all cases, sorry about that.
  65. 11- After some investigation (using the 'ping' command), trial and error (assigning the router address from active addresses reported by the ping command), and some guessing (assuming that the router address has a similar pattern as eth0 router address) the router on the eth1 network interface was determined to be 10.0.3.2
  66. 12- List of required info is:
  67. eth0 network interface IP address is 10.0.2.15
  68. eth1 network interface IP address is 10.0.3.15
  69. default router on eth0 network is 10.0.2.2
  70. non-default router on eth1 network is 10.0.3.2
  71. 13- With this info in hand it is now possible to setup the routing on the target system.
  72. NOTE: instructions that follow are specific to the latest Linux versions. They have been tested on OpenSuse Leap 42.3, but most likely work on other versions of GNU Linux OS. Please verify the provided information against your particular OS to confirm the appropriate commands needed to configuring the routing on your particular environment.
  73. 14- Verify the content of the file /etc/iproute2/rt_tables to see the route tables entries.
  74. Command:
  75. cat /etc/iproute2/rt_tables
  76. Example Output:
  77. #
  78. # reserved values
  79. #
  80. 255 local
  81. 254 main
  82. 253 default
  83. 0 unspec
  84. #
  85. # local
  86. #
  87. #1 inr.ruhep
  88. 15- In the file output above, specifically check if there is an entry for the eth1 network interface.
  89. 16- If no eth1 routing table entry exists, then carefully append a new line to the file that will define the eth1 network in this routing table.
  90. Command:
  91. echo "1 eth1" >>/etc/iproute2/rt_tables
  92. 17- Add a new route for the eth1 network interface based on the 'eth1' routing table entry.
  93. Commands:
  94. ip route add 10.0.3.0/24 dev eth1 src 10.0.3.15 table eth1
  95. ip route add default via 10.0.3.2 dev eth1 table eth1
  96. 18- Add ip rules so that packets having source IP address 10.0.3.15 will be routed through the eth1 router, instead of the default router on eth0.
  97. Commands:
  98. ip rule add from 10.0.3.15/32 table eth1
  99. ip rule add to 10.0.3.15/32 table eth1
  100. 19- In a second terminal use the tcpdump commmand to monitor/snoop the traffic on eth1
  101. Command:
  102. tcpdump -i eth1
  103. 20- Use the SetSourceIP commmand to send traffic through eth1
  104. Command:
  105. ./SetSourceIp --sourceip 10.0.3.15 https://www.google.com/
  106. 21- Some https traffic (port 443) should be seen on eth1. Bi-directional https traffic should be seen, both incoming and outgoing traffic destined for the google.com domain.
  107. 22- Double check that no traffic is going through eth0 using tcpdump. Some DNS (domain) traffic will likely be seen on the default route.
  108. Command:
  109. tcpdump -i eth0
  110. ADDITIONAL INFO
  111. Some quick info on how to simulate creating a Floating IP (FIP), or Mobile IP (MIP) or Virtual IP (VIP) that can be (re)assigned to various hosts depending on application requirements.
  112. NOTE: the following commands assume the the routing was properly setup with the commands from the previous section of this document.
  113. 1- Add another IP address to existing eth1 interface to act as a FIP/MIP/VIP address. Now the eth1 interface will respond to both the orignal 10.0.3.15 IP address and also this new "FIP/MIP/VIP" 10.0.3.42 IP address.
  114. Command:
  115. ifconfig eth1 add 10.0.3.42 broadcast 10.0.3.255 netmask 255.255.255.0
  116. 2- Check the network setup to see the additional IP address
  117. Command:
  118. ifconfig -a
  119. 3- An entry such as the one below should be presented.
  120. Example Output:
  121. eth1:0 Link encap:Ethernet HWaddr 08:00:27:AE:16:3E
  122. inet addr:10.0.3.42 Bcast:10.0.3.255 Mask:255.255.255.0
  123. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  124. 4- Add ip rules so that packets having source IP address 10.0.3.42 will be routed through the eth1 router, instead of the default router on eth0.
  125. Commands:
  126. ip rule add from 10.0.3.42/32 table eth1
  127. ip rule add to 10.0.3.42/32 table eth1
  128. 5- In a second terminal use the tcpdump commmand to monitor/snoop the traffic on eth1
  129. Command:
  130. tcpdump -i eth1
  131. 6- Use the SetSourceIP commmand to send traffic through eth1
  132. Command:
  133. ./SetSourceIp --sourceip 10.0.3.42 https://www.google.com/
  134. 7- Some https traffic (port 443) should be seen on eth1. Bi-directional https traffic should be seen, both incoming and outgoing traffic destined for the google.com domain.
  135. 8- Double check that no traffic is going through eth0 using tcpdump. Some DNS (domain) traffic will likely be seen on the default route.
  136. Command:
  137. tcpdump -i eth0
  138. Good Luck!!!
  139. Rocco Corsi
  140. December 16, 2017