ynh-hotspot 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-hotspot
  4. # Required-Start: $network $remote_fs $syslog
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Set prerequisites for wifi hotspot.
  9. # Description: Set prerequisites for wifi hotspot.
  10. ### END INIT INFO
  11. is_ndproxy_set() {
  12. proxy=$(ip -6 neigh show proxy)
  13. [ ! -z "${proxy}" ]
  14. }
  15. is_nat_set() {
  16. iptables -nt nat -L POSTROUTING | grep -q MASQUERADE
  17. }
  18. is_ip4nataddr_set() {
  19. ip a s dev <TPL:WIFI_DEVICE> | grep -q <TPL:IP4_NAT_PREFIX>.1/24
  20. }
  21. is_ip6addr_set() {
  22. ip a s dev <TPL:WIFI_DEVICE> | grep -q <TPL:IP6_ADDR>/64
  23. }
  24. is_forwarding_set() {
  25. ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
  26. ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
  27. [ ${ip6} -eq 1 -a ${ip4} -eq 1 ]
  28. }
  29. is_hostapd_running() {
  30. service hostapd status &> /dev/null
  31. }
  32. is_radvd_running() {
  33. service radvd status &> /dev/null
  34. }
  35. is_dhcpd_running() {
  36. service isc-dhcp-server status &> /dev/null
  37. }
  38. is_running() {
  39. is_ndproxy_set && is_nat_set && is_forwarding_set\
  40. && is_hostapd_running && is_radvd_running && is_dhcpd_running
  41. }
  42. internet_device=$(ip r | awk '/default via/ { print $NF; }')
  43. internet_device_vpn=tun0
  44. internet_device_default=${internet_device}
  45. # Apply NAT on tun0 if IPv6 address if VPN is used
  46. ip l sh dev tun0 &> /dev/null
  47. if [ "$?" -eq 0 ]; then
  48. internet_device_default=${internet_device_vpn}
  49. fi
  50. case "$1" in
  51. start)
  52. if is_running; then
  53. echo "Already correctly set"
  54. else
  55. if ! is_ndproxy_set; then
  56. echo "Set NDP proxy"
  57. ip -6 neigh add proxy <TPL:IP6_ADDR> dev <TPL:WIFI_DEVICE>
  58. fi
  59. if ! is_nat_set; then
  60. echo "Set NAT"
  61. iptables -t nat -A POSTROUTING -o ${internet_device_default} -j MASQUERADE
  62. fi
  63. if ! is_ip4nataddr_set; then
  64. echo "Set IPv4 NAT address"
  65. ip a a <TPL:IP4_NAT_PREFIX>.1/24 dev <TPL:WIFI_DEVICE>
  66. fi
  67. if ! is_ip6addr_set; then
  68. echo "Set IPv6 address"
  69. ip a a <TPL:IP6_ADDR>/64 dev <TPL:WIFI_DEVICE>
  70. fi
  71. if ! is_forwarding_set; then
  72. echo "Set forwarding"
  73. sysctl -w net.ipv6.conf.all.forwarding=1 &> /dev/null
  74. sysctl -w net.ipv4.conf.all.forwarding=1 &> /dev/null
  75. fi
  76. if ! is_hostapd_running; then
  77. echo "Run hostapd"
  78. service hostapd start
  79. fi
  80. # must be running after hostapd
  81. if ! is_radvd_running; then
  82. echo "Run radvd"
  83. sleep 1
  84. service radvd start
  85. fi
  86. # "options routers" addr (is_ip6addr_set) must be set before
  87. if ! is_dhcpd_running; then
  88. echo "Run dhcpd"
  89. service isc-dhcp-server start
  90. fi
  91. fi
  92. ;;
  93. stop)
  94. if is_ndproxy_set; then
  95. echo "Unset NDP proxy"
  96. ip -6 neigh del proxy <TPL:IP6_ADDR> dev <TPL:WIFI_DEVICE>
  97. fi
  98. if is_nat_set; then
  99. echo "Unset NAT"
  100. # Depending on the presence or not of a VPN at the last startup, one of these 2 lines is unnecessary
  101. iptables -t nat -D POSTROUTING -o ${internet_device} -j MASQUERADE 2> /dev/null
  102. iptables -t nat -D POSTROUTING -o ${internet_device_vpn} -j MASQUERADE 2> /dev/null
  103. fi
  104. if is_ip4nataddr_set; then
  105. echo "Unset IPv4 NAT address"
  106. ip a d <TPL:IP4_NAT_PREFIX>.1/24 dev <TPL:WIFI_DEVICE>
  107. fi
  108. if is_ip6addr_set; then
  109. echo "Unset IPv6 address"
  110. ip a d <TPL:IP6_ADDR>/64 dev <TPL:WIFI_DEVICE>
  111. fi
  112. if is_forwarding_set; then
  113. echo "Unset forwarding"
  114. sysctl -w net.ipv6.conf.all.forwarding=0 &> /dev/null
  115. sysctl -w net.ipv4.conf.all.forwarding=0 &> /dev/null
  116. fi
  117. if is_hostapd_running; then
  118. echo "Stop hostapd"
  119. service hostapd stop
  120. fi
  121. if is_radvd_running; then
  122. echo "Stop radvd"
  123. service radvd stop
  124. fi
  125. if is_dhcpd_running; then
  126. echo "Stop dhcpd"
  127. service isc-dhcp-server stop
  128. fi
  129. ;;
  130. restart)
  131. $0 stop
  132. $0 start
  133. ;;
  134. status)
  135. exitcode=0
  136. if is_ndproxy_set; then
  137. echo "NDP proxy is correctly set"
  138. else
  139. echo "NDP proxy is NOT set"
  140. exitcode=1
  141. fi
  142. if is_nat_set; then
  143. echo "NAT is correctly set"
  144. else
  145. echo "NAT is NOT set"
  146. exitcode=1
  147. fi
  148. if is_ip4nataddr_set; then
  149. echo "IPv4 NAT address is correctly set"
  150. else
  151. echo "IPv4 NAT address is NOT set"
  152. exitcode=1
  153. fi
  154. if is_ip6addr_set; then
  155. echo "IPv6 address is correctly set"
  156. else
  157. echo "IPv6 address is NOT set"
  158. exitcode=1
  159. fi
  160. if is_forwarding_set; then
  161. echo "Forwarding is correctly set"
  162. else
  163. echo "Forwarding is NOT set"
  164. exitcode=1
  165. fi
  166. if is_hostapd_running; then
  167. echo "Hostapd is running"
  168. else
  169. echo "Hostapd is NOT running"
  170. exitcode=1
  171. fi
  172. if is_radvd_running; then
  173. echo "Radvd is running"
  174. else
  175. echo "Radvd is NOT running"
  176. exitcode=1
  177. fi
  178. if is_dhcpd_running; then
  179. echo "Dhcpd is running"
  180. else
  181. echo "Dhcpd is NOT running"
  182. exitcode=1
  183. fi
  184. exit ${exitcode}
  185. ;;
  186. *)
  187. echo "Usage: $0 {start|stop|restart|status}"
  188. exit 1
  189. ;;
  190. esac
  191. exit 0