ynh-hotspot 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. case "$1" in
  43. start)
  44. if is_running; then
  45. echo "Already correctly set"
  46. else
  47. if ! is_ndproxy_set; then
  48. echo "Set NDP proxy"
  49. ip -6 neigh add proxy <TPL:IP6_ADDR> dev <TPL:WIFI_DEVICE>
  50. fi
  51. if ! is_nat_set; then
  52. echo "Set NAT"
  53. iptables -t nat -A POSTROUTING -o <TPL:WIRED_DEVICE> -j MASQUERADE
  54. fi
  55. if ! is_ip4nataddr_set; then
  56. echo "Set IPv4 NAT address"
  57. ip a a <TPL:IP4_NAT_PREFIX>.1/24 dev <TPL:WIFI_DEVICE>
  58. fi
  59. if ! is_ip6addr_set; then
  60. echo "Set IPv6 address"
  61. ip a a <TPL:IP6_ADDR>/64 dev <TPL:WIFI_DEVICE>
  62. fi
  63. if ! is_forwarding_set; then
  64. echo "Set forwarding"
  65. sysctl -w net.ipv6.conf.all.forwarding=1 &> /dev/null
  66. sysctl -w net.ipv4.conf.all.forwarding=1 &> /dev/null
  67. fi
  68. if ! is_hostapd_running; then
  69. echo "Run hostapd"
  70. service hostapd start
  71. fi
  72. # must be running after hostapd
  73. if ! is_radvd_running; then
  74. echo "Run radvd"
  75. sleep 1
  76. service radvd start
  77. fi
  78. # "options routers" addr (is_ip6addr_set) must be set before
  79. if ! is_dhcpd_running; then
  80. echo "Run dhcpd"
  81. service isc-dhcp-server start
  82. fi
  83. fi
  84. ;;
  85. stop)
  86. if is_ndproxy_set; then
  87. echo "Unset NDP proxy"
  88. ip -6 neigh del proxy <TPL:IP6_ADDR> dev <TPL:WIFI_DEVICE>
  89. fi
  90. if is_nat_set; then
  91. echo "Unset NAT"
  92. iptables -t nat -D POSTROUTING -o <TPL:WIRED_DEVICE> -j MASQUERADE
  93. fi
  94. if is_ip4nataddr_set; then
  95. echo "Unset IPv4 NAT address"
  96. ip a d <TPL:IP4_NAT_PREFIX>.1/24 dev <TPL:WIFI_DEVICE>
  97. fi
  98. if is_ip6addr_set; then
  99. echo "Unset IPv6 address"
  100. ip a d <TPL:IP6_ADDR>/64 dev <TPL:WIFI_DEVICE>
  101. fi
  102. if is_forwarding_set; then
  103. echo "Unset forwarding"
  104. sysctl -w net.ipv6.conf.all.forwarding=0 &> /dev/null
  105. sysctl -w net.ipv4.conf.all.forwarding=0 &> /dev/null
  106. fi
  107. if is_hostapd_running; then
  108. echo "Stop hostapd"
  109. service hostapd stop
  110. fi
  111. if is_radvd_running; then
  112. echo "Stop radvd"
  113. service radvd stop
  114. fi
  115. if is_dhcpd_running; then
  116. echo "Stop dhcpd"
  117. service isc-dhcp-server stop
  118. fi
  119. ;;
  120. restart)
  121. $0 stop
  122. $0 start
  123. ;;
  124. status)
  125. exitcode=0
  126. if is_ndproxy_set; then
  127. echo "NDP proxy is correctly set"
  128. else
  129. echo "NDP proxy is NOT set"
  130. exitcode=1
  131. fi
  132. if is_nat_set; then
  133. echo "NAT is correctly set"
  134. else
  135. echo "NAT is NOT set"
  136. exitcode=1
  137. fi
  138. if is_ip4nataddr_set; then
  139. echo "IPv4 NAT address is correctly set"
  140. else
  141. echo "IPv4 NAT address is NOT set"
  142. exitcode=1
  143. fi
  144. if is_ip6addr_set; then
  145. echo "IPv6 address is correctly set"
  146. else
  147. echo "IPv6 address is NOT set"
  148. exitcode=1
  149. fi
  150. if is_forwarding_set; then
  151. echo "Forwarding is correctly set"
  152. else
  153. echo "Forwarding is NOT set"
  154. exitcode=1
  155. fi
  156. if is_hostapd_running; then
  157. echo "Hostapd is running"
  158. else
  159. echo "Hostapd is NOT running"
  160. exitcode=1
  161. fi
  162. if is_radvd_running; then
  163. echo "Radvd is running"
  164. else
  165. echo "Radvd is NOT running"
  166. exitcode=1
  167. fi
  168. if is_dhcpd_running; then
  169. echo "Dhcpd is running"
  170. else
  171. echo "Dhcpd is NOT running"
  172. exitcode=1
  173. fi
  174. exit ${exitcode}
  175. ;;
  176. *)
  177. echo "Usage: $0 {start|stop|restart|status}"
  178. exit 1
  179. ;;
  180. esac
  181. exit 0