Advertisement
Guest User

Selective routing script

a guest
Feb 9th, 2015
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # (Credit to SmallNetBuilder member DJR747 and Jobongo's Wiki - Howto Set SSID VPN)
  4.  
  5. # This script 'VPN_Select.sh' allows selective VPN routing:
  6. #
  7. # A single device, or
  8. # A range of devices, or
  9. # A subnet
  10. # A Guest WiFi SSID
  11. #
  12. # The optional args passed to this script is a list of Guest SSID interfaces:
  13. # e.g.
  14. # wl1.3 # 5GHz Guest #3 will use this VPN (if defined in /etc/dnsmasq.conf)
  15. # wl0.1 wl1.3 # 2.4GHz Guest #1 and 5GHz Guest #3 will use this VPN (if defined in /etc/dnsmasq.conf)
  16. # *" # All Guests defined in /etc/dnsmasq.conf will use this VPN.
  17.  
  18. # The following two custom variables are used:
  19. #
  20. # e.g. Table 100 will be used and any device tagged with 'fwmark 1' (ON/OFF) will use this VPN Client
  21.  
  22. #MY_VPNTAB=100 # Now read from /jffs/configs/VPNSelect
  23. #TAG_MARK=1 # Now read from /jffs/configs/VPNSelect
  24.  
  25.  
  26. # To overcome timing issues, this method relies on the VPN Client custom configuration to call this script.
  27.  
  28. # The following VPN client custom configuration directives MUST be defined:
  29. # *************************************************************************
  30. # route-nopull
  31. # script-security 2
  32. # route-up "/jffs/scripts/VPN_Select.sh [Guest_SSID_interfaces]"
  33. # **************************************************************************
  34.  
  35. # These optional VPN client custom configuration directives help to keep the VPN connection alive,
  36. # although the Host VPN server can terminate the connection based on their lease renewal policy
  37.  
  38. #inactive 0
  39. #keepalive 5 60
  40.  
  41.  
  42.  
  43. # Also /jffs/configs/dnsmasq.conf must also define the Guest WiFi interface to be used by this VPN
  44. # So include the following three directives in /jffs/configs/dnsmasq.config.add
  45. #
  46. # e.g. wl0.1 represents 2.4GHz Guest #1 aka '241' octet
  47. # so
  48. # wl1.3 5GHz Guest #3 would use '53' octet ...to aid debugging! connection by I/P
  49. #
  50. # interface=wl0.1
  51. # dhcp-range=wl0.1,192.168.241.2,192.168.241.20,255.255.255.0,21600s
  52. # dhcp-option=wl0.1,3,192.168.241.1
  53.  
  54.  
  55. # NOTE: These commands may be specified in wan-start to block ALL WAN access except http/https until the VPN Client is UP
  56. # i.e. when this script runs.
  57.  
  58. # iptables -I FORWARD -i br0 -s xxx.xxx.xxx.0/24 -o `nvram get wan0_ifname` -j DROP
  59. # iptables -I FORWARD -s xxx.xxx.xxx.0/24 -o ! $dev -p tcp --dport 80 -j ACCEPT
  60. # iptables -I FORWARD -s xxx.xxx.xxx.0/24 -o ! $dev -p tcp --dport 443 -j ACCEPT
  61.  
  62. # iptables -I FORWARD -s xxx.xxx.xxx.0/24 -o ! $dev -p tcp -m multiport --dport 80,443,25,110,143,8888 -j ACCEPT
  63.  
  64.  
  65. logger -s -t "($(basename $0))" $$ OpenVPN Client`echo -n $dev | tail -c -1` Selective routing starting.... " $0${*:+ $*}"
  66.  
  67. MYROUTER=$(nvram get computer_name)
  68. USEPATH="/tmp/mnt/$MYROUTER"
  69.  
  70.  
  71. # Get the values from /jffs/scripts/configs/VPNSelect
  72. VPN_ID=`echo -n $dev | tail -c -1`
  73. MY_VPNTAB=`grep -i "VPN$VPN_ID" /jffs/configs/VPNSelect | awk '{print $2}'`
  74. TAG_MARK=`grep -i "VPN$VPN_ID" /jffs/configs/VPNSelect | awk '{print $3}'`
  75. #MY_VPNTAB=100 # Now read from /jffs/configs/VPNSelect
  76. #TAG_MARK=1 # Now read from /jffs/configs/VPNSelect
  77.  
  78. # Use the OpenVPN environment variables
  79. if [ "X$dev" = "X" ]; then
  80. logger -s -t "($(basename $0))" $$ "*** ERROR not called by VPN Client route-up?...ABORTing!"
  81. exit
  82. fi
  83. # Create new table to route VPN traffic when tagged with MARK. (Credit to SmallNetBuilder member DJR747)
  84. # or to be associated with a WiFi Guest SSID.
  85. ip route flush table $MY_VPNTAB
  86. ip rule del fwmark $TAG_MARK $MY_VPNTAB
  87. ip rule del table $MY_VPNTAB
  88.  
  89. ip route flush cache
  90. iptables -t mangle -F PREROUTING
  91.  
  92.  
  93. # Disable Reverse Path Filtering on current VPN network interface:
  94. echo 0 > /proc/sys/net/ipv4/conf/$dev/rp_filter
  95.  
  96. logger -s -t "($(basename $0))" $$ "CMD: ip route add default via $ifconfig_local dev $dev table $MY_VPNTAB"
  97. ip route add default via $ifconfig_local dev $dev table $MY_VPNTAB
  98. logger -s -t "($(basename $0))" $$ "CMD: ip rule add fwmark $TAG_MARK table $MY_VPNTAB"
  99. ip rule add fwmark $TAG_MARK table $MY_VPNTAB
  100.  
  101. # Mark the Guest SSID(s) to use this VPN Client
  102.  
  103. GUEST_IFS=${*:+ $*} # List of Guest WiFi interfaces to use this VPN
  104.  
  105. if [ "$GUEST_IFS" != "" ];then
  106. for GUEST_IF in $GUEST_IFS
  107. do
  108. SSID=$(nvram get $GUEST_IF"_ssid")
  109. logger -s -t "($(basename $0))" $$ "SSID $SSID is being configured to use OpenVPN Client`echo -n $dev | tail -c -1`....."
  110. # Validate the Guest SSID(s) to be forced to use this VPN
  111. GUEST_IF_IP=`grep -i "dhcp-option=$GUEST_IF,3" /etc/dnsmasq.conf | awk 'BEGIN { FS = "," } {print $3}'` # Extract I/P from 'dhcp-option=$GUEST_IF,3,10.88.241.1'
  112. GUEST_SUBNET_PREFIX=`echo $GUEST_IF_IP | awk 'BEGIN { FS = "." } {print $1"."$2"."$3}'` # Extract first three octets of I/P
  113. logger -s -t "($(basename $0))" $$ " Lookup '$GUEST_IF' in DNSMASQ returned:>$GUEST_IF_IP< and Subnet Prefix >$GUEST_SUBNET_PREFIX<"
  114. if [ "$GUEST_IF_IP" != "" ];then
  115. logger -s -t "($(basename $0))" $$ " CMD: ip rule add dev $GUEST_IF table $MY_VPNTAB"
  116. ip rule del dev $GUEST_IF
  117. ip rule add dev $GUEST_IF table $MY_VPNTAB
  118. logger -s -t "($(basename $0))" $$ "SSID $SSID is configured to use OpenVPN Client`echo -n $dev | tail -c -1`."
  119. else
  120. logger -s -t "($(basename $0))" $$ "*** ERROR SSID $SSID cannot be configured to use OpenVPN Client`echo -n $dev | tail -c -1`."
  121. fi
  122. done
  123. fi
  124.  
  125. # To list if custom MARK tables exist use
  126. #
  127. # ip rule
  128. logger -s -t "($(basename $0))" $$ "CMD: ip rule"
  129. logger -s -t "($(basename $0))" $$ " `ip rule`"
  130.  
  131. # To list if custom route tables exist use
  132. #
  133. # ip route show table $MY_VPNTAB
  134. logger -s -t "($(basename $0))" $$ "CMD: ip route show table $MY_VPNTAB"
  135. logger -s -t "($(basename $0))" $$ " `ip route show table $MY_VPNTAB`"
  136.  
  137. # If the VPN isn't firewalled by the ISP, then this rule should protect inbound access to the router
  138. #iptables -I INPUT -i $dev -p tcp --dport 0:1023 -m state --state NEW -j DROP
  139.  
  140. # Select the I/P devices to be routed.
  141. # ====================================
  142.  
  143.  
  144. if [ -e "/jffs/scripts/VPN_Select_ON_OFF.sh" ]; then
  145. # Call VPN_select_ON_OFF [host.dnsmasq | ip_address | KEY_tag] [ON | OFF] {FORCE}
  146. #
  147. #
  148. # where Host.dnsmasq will be matched against /etc/hosts.dnsmasq contents
  149. #
  150. # Key_tag will be matched against /mnt/$MYROUTER/VPN_MASKS.txt
  151. #
  152. # FORCE will ensure designated target will ONLY use the VPN.
  153. #
  154. # Use HOSTS.DNSMASQ lookup to resolve PS3-Bedroom and ensure it ONLY uses the VPN
  155. if [ -e "/jffs/scripts/VPN_Select_ON_OFF.sh" ]; then
  156. /jffs/scripts/VPN_Select_ON_OFF.sh PS3-Bedroom ON FORCE
  157. # Others that are not individual I/P addresses.......
  158. #/jffs/scripts/VPN_Select_ON_OFF.sh ALL ON
  159. #/jffs/scripts/VPN_Select_ON_OFF.sh ANDROID ON
  160. fi
  161. else
  162. # Explicitly select the I/P devices to be routed.
  163.  
  164. logger -s -t "($(basename $0))" $$ "Manually issue iptables -t mangle -A PREROUTING -i br0 commands"
  165. # Examples for routing a specific device, all devices on the subnet or a range of devices via VPN
  166. #iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range xxx.xxx.xxx.xxx -j MARK --set-mark $TAG_MARK
  167. #iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range xxx.xxx.xxx.0/24 -j MARK --set-mark $TAG_MARK
  168. #iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range xxx.xxx.xxx.nnn-xxx.xxx.xxx.nnn+y -j MARK --set-mark $TAG_MARK
  169.  
  170. # Example for routing a specific port via VPN
  171. # iptables -t mangle -A PREROUTING -i br0 -s xxx.xxx.xxx.xxx -p tcp --dport ppppp -j MARK --set-mark $TAG_MARK
  172. fi
  173.  
  174.  
  175. # Debug the routing tables
  176. if [ -e "/jffs/scripts/IPTablesDump.sh" ]; then
  177. /jffs/scripts/IPTablesDump.sh "VPN_Client_Select"`echo -n $dev | tail -c -1`
  178. fi
  179.  
  180. logger -s -t "($(basename $0))" $$ OpenVPN Client`echo -n $dev | tail -c -1` Selective routing completed....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement