You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.1 KiB

  1. #!/bin/sh
  2. #
  3. # Dual Channel Wi-Fi Startup Script
  4. #
  5. # This script creates the proper network bridge configuration
  6. # necessary for Dual Channel Wi-Fi, and starts the dcwapd daemon
  7. #
  8. verbose=1
  9. uciconfig=dcwapd
  10. result=
  11. # NOTE: all functions write the result to the $result variable
  12. get_channelsets()
  13. {
  14. # default to empty
  15. result=
  16. channelsets=$(uci show $uciconfig | grep "=channel-set$")
  17. for channelset in $channelsets; do
  18. channelset=$(echo "$channelset" | sed -rn "s/$uciconfig\.(.*)=.*/\1/p")
  19. result="$result $channelset"
  20. done
  21. if [ $verbose -eq 1 ]; then
  22. echo "Channel Sets: $result" 2>&1 | logger
  23. fi
  24. }
  25. # $1 : the channel set name
  26. get_channelset_enabled()
  27. {
  28. # default to disabled
  29. result=0
  30. if [ -n "$1" ]; then
  31. result=$(uci get $uciconfig."$1".enabled)
  32. fi
  33. if [ $verbose -eq 1 ]; then
  34. echo "Channel Set \"$1\" Enabled: $result" 2>&1 | logger
  35. fi
  36. }
  37. # $1 : the channel set name
  38. get_primary_bridge()
  39. {
  40. result=
  41. if [ -n "$1" ]; then
  42. result=$(uci get $uciconfig."$1".bridge)
  43. fi
  44. if [ $verbose -eq 1 ]; then
  45. echo "Channel Set \"$1\" Primary Bridge: $result" 2>&1 | logger
  46. fi
  47. }
  48. # $1 : the channel set name
  49. get_datachannels()
  50. {
  51. # default to empty
  52. result=
  53. if [ -n "$1" ]; then
  54. result=$(uci get $uciconfig."$1".data_channels)
  55. fi
  56. if [ $verbose -eq 1 ]; then
  57. echo "Channel Set \"$1\" Data Channels: $result" 2>&1 | logger
  58. fi
  59. }
  60. # $1 : the wlan interface name
  61. get_wifi_iface_num()
  62. {
  63. result=
  64. if [ -n "$1" ];then
  65. #result=$(echo "$1" | sed -n "s/wlan//p")
  66. result=$(echo "$1" | sed -rn "s/wlan([0-9]*).*/\1/p")
  67. fi
  68. }
  69. # $1 : the bridge name
  70. get_bridge_network_name()
  71. {
  72. result=
  73. if [ -n "$1" ];then
  74. result=$(echo "$1" | sed -n "s/br-//p")
  75. fi
  76. }
  77. # $1 : the wlan interface name
  78. set_iface_init_state()
  79. {
  80. result=
  81. if [ -n "$1" ]; then
  82. iface=$1
  83. # need to extract the "X" from wlanX
  84. get_wifi_iface_num "$iface"
  85. iface_num=$result
  86. if [ -n "$iface_num" ]; then
  87. # get the iface network
  88. init_net=$(uci get wireless.@wifi-iface[$iface_num].network)
  89. if [ -n "$init_net" ]; then
  90. # if the iface network is a bridge, but doesn't start with "br-"
  91. # I think we need to prepend it?
  92. net_type=$(uci get network."$init_net".type)
  93. if [ -n "$net_type" ] && [ "$net_type" = "bridge" ]; then
  94. prefix_ok=$(echo "$init_net" | grep "^br-")
  95. if [ -z "$prefix_ok" ]; then
  96. init_net="br-$init_net"
  97. fi
  98. fi
  99. fi
  100. # make sure that the init_net section exists
  101. init_net_section=$(uci get dcwapd.init_net)
  102. if [ "$init_net_section" != "init_net" ]; then
  103. # the section did not exist
  104. uci set dcwapd.init_net=init_net
  105. fi
  106. # save the initial network
  107. if [ $verbose -eq 1 ]; then
  108. echo "Saving '$iface' initial network '$init_net'" 2>&1 | logger
  109. fi
  110. uci set $uciconfig.init_net."$iface"="$init_net"
  111. uci commit
  112. # save the initial network in the result variable
  113. result=$init_net
  114. fi
  115. fi
  116. }
  117. # $1 : the wlan interface name
  118. get_iface_init_state()
  119. {
  120. result=
  121. if [ -n "$1" ];then
  122. init_net=$(uci get $uciconfig.init_net."$iface")
  123. # if the response starts with "uci: ", it was an error not the real result
  124. err=$(echo "$init_net" | grep "^uci: ")
  125. if [ -z "$err" ]; then
  126. # no error, set the result
  127. result=$init_net
  128. if [ $verbose -eq 1 ]; then
  129. echo "Got '$iface' initial network '$init_net'" 2>&1 | logger
  130. fi
  131. fi
  132. fi
  133. }
  134. # $1 : the name of the data channel name to bring up
  135. datachannel_up()
  136. {
  137. if [ -n "$1" ]; then
  138. bridge=$(uci get $uciconfig."$1".bridge)
  139. interfaces=$(uci get $uciconfig."$1".interfaces)
  140. if [ $verbose -eq 1 ]; then
  141. echo "Creating Data Channel Bridge: $bridge" 2>&1 | logger
  142. fi
  143. get_bridge_network_name "$bridge"
  144. netname=$result
  145. if [ -n "$netname" ]; then
  146. uci set network."$netname"=interface
  147. uci set network."$netname".type=bridge
  148. uci set network."$netname".proto=static
  149. uci set network."$netname".bridge_empty='1'
  150. fi
  151. # create the bridge
  152. uci commit
  153. /etc/init.d/network reload
  154. for iface in $interfaces; do
  155. # if iface is in a bridge, the bridge name should be stored in result
  156. set_iface_init_state "$iface"
  157. init_bridge=$result
  158. # update uci with the new bridge info
  159. get_wifi_iface_num "$iface"
  160. iface_num=$result
  161. if [ -n "$iface_num" ]; then
  162. uci set wireless.@wifi-iface[$iface_num].network="$netname"
  163. fi
  164. # manually put the interface into the data bridge
  165. # if iface is in a bridge, remove it before adding it to the data bridge
  166. if [ -n "$init_bridge" ]; then
  167. brctl delif "$init_bridge" "$iface" 2>&1 | logger
  168. fi
  169. brctl addif "$bridge" "$iface" 2>&1 | logger
  170. done
  171. # commit uci changes and reload the network
  172. uci commit
  173. /etc/init.d/network reload
  174. #/etc/init.d/network restart
  175. # while [ 1 ]; do
  176. # ifconfig "$bridge" > /dev/null 2>&1
  177. # if [ $? == 0 ]; then
  178. # break;
  179. # fi
  180. # sleep 1
  181. # done
  182. fi
  183. }
  184. # $1 : the name of the data channel to bring down
  185. datachannel_down()
  186. {
  187. if [ -n "$1" ]; then
  188. bridge=$(uci get $uciconfig."$1".bridge)
  189. interfaces=$(uci get $uciconfig."$1".interfaces)
  190. for iface in $interfaces; do
  191. if [ $verbose -eq 1 ]; then
  192. echo "Deconfiguring Data Channel Interface: $iface" 2>&1 | logger
  193. fi
  194. # manually remove the interface from the data bridge
  195. brctl delif "$bridge" "$iface" 2>&1 | logger
  196. get_iface_init_state "$iface"
  197. init_bridge=$result
  198. if [ -n "$init_bridge" ]; then
  199. # manually move the interface back to the original bridge
  200. brctl addif "$init_bridge" "$iface" 2>&1 | logger
  201. # update uci with the new bridge and interface configuration
  202. get_wifi_iface_num "$iface"
  203. iface_num=$result
  204. get_bridge_network_name "$init_bridge"
  205. netname=$result
  206. if [ -n "$iface_num" ] && [ -n "$netname" ]; then
  207. uci set wireless.@wifi-iface[$iface_num].network="$netname"
  208. fi
  209. fi
  210. done
  211. if [ $verbose -eq 1 ]; then
  212. echo "Deconfiguring Data Channel Bridge: $bridge" 2>&1 | logger
  213. fi
  214. # delete the bridge from uci
  215. get_bridge_network_name "$bridge"
  216. netname=$result
  217. if [ -n "$netname" ]; then
  218. uci delete network."$netname"
  219. fi
  220. # commit uci changes and reload the network
  221. uci commit
  222. /etc/init.d/network reload
  223. #`/etc/init.d/network restart`
  224. fi
  225. }