hostap.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/bin/sh
  2. append DRIVERS "prism2"
  3. find_prism2_phy() {
  4. local device="$1"
  5. local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
  6. config_get phy "$device" phy
  7. [ -z "$phy" -a -n "$macaddr" ] && {
  8. cd /proc/net/hostap
  9. for phy in $(ls -d wlan* 2>&-); do
  10. [ "$macaddr" = "$(cat /sys/class/net/${phy}/address)" ] || continue
  11. config_set "$device" phy "$phy"
  12. break
  13. done
  14. config_get phy "$device" phy
  15. }
  16. [ -n "$phy" -a -d "/proc/net/hostap/$phy" ] || {
  17. echo "phy for wifi device $1 not found"
  18. return 1
  19. }
  20. [ -z "$macaddr" ] && {
  21. config_set "$device" macaddr "$(cat /sys/class/net/${phy}/address)"
  22. }
  23. return 0
  24. }
  25. scan_prism2() {
  26. local device="$1"
  27. local mainvif
  28. local wds
  29. [ ${device%[0-9]} = "wlan" ] && config_set "$device" phy "$device" || find_prism2_phy "$device" || {
  30. config_unset "$device" vifs
  31. return 0
  32. }
  33. config_get phy "$device" phy
  34. config_get vifs "$device" vifs
  35. local _c=0
  36. for vif in $vifs; do
  37. config_get_bool disabled "$vif" disabled 0
  38. [ $disabled = 0 ] || continue
  39. config_get mode "$vif" mode
  40. case "$mode" in
  41. adhoc|sta|ap|monitor)
  42. # Only one vif is allowed on AP, station, Ad-hoc or monitor mode
  43. [ -z "$mainvif" ] && {
  44. mainvif="$vif"
  45. config_set "$vif" ifname "$phy"
  46. }
  47. ;;
  48. wds)
  49. config_get ssid "$vif" ssid
  50. [ -z "$ssid" ] && continue
  51. config_set "$vif" ifname "${phy}wds${_c}"
  52. _c=$(($_c + 1))
  53. addr="$ssid"
  54. ${addr:+append wds "$vif"}
  55. ;;
  56. *) echo "$device($vif): Invalid mode, ignored."; continue;;
  57. esac
  58. done
  59. config_set "$device" vifs "${mainvif:+$mainvif }${wds:+$wds}"
  60. }
  61. disable_prism2() (
  62. local device="$1"
  63. find_prism2_phy "$device" || return 0
  64. config_get phy "$device" phy
  65. set_wifi_down "$device"
  66. include /lib/network
  67. while read line < /proc/net/hostap/${phy}/wds; do
  68. set $line
  69. [ -f "/var/run/wifi-${1}.pid" ] &&
  70. kill "$(cat "/var/run/wifi-${1}.pid")"
  71. ip link set dev "$1" down
  72. unbridge "$1"
  73. iwpriv "$phy" wds_del "$2"
  74. done
  75. unbridge "$phy"
  76. return 0
  77. )
  78. enable_prism2() {
  79. local device="$1"
  80. find_prism2_phy "$device" || return 0
  81. config_get phy "$device" phy
  82. config_get rxantenna "$device" rxantenna
  83. config_get txantenna "$device" txantenna
  84. config_get_bool diversity "$device" diversity
  85. [ -n "$diversity" ] && {
  86. rxantenna="1"
  87. txantenna="1"
  88. }
  89. [ -n "$rxantenna" ] && iwpriv "$phy" antsel_rx "$rxantenna"
  90. [ -n "$txantenna" ] && iwpriv "$phy" antsel_tx "$txantenna"
  91. config_get channel "$device" channel
  92. [ -n "$channel" ] && iwconfig "$phy" channel "$channel" >/dev/null 2>/dev/null
  93. config_get txpower "$device" txpower
  94. [ -n "$txpower" ] && iwconfig "$phy" txpower "${txpower%%.*}"
  95. config_get vifs "$device" vifs
  96. local first=1
  97. for vif in $vifs; do
  98. config_get ifname "$vif" ifname
  99. config_get ssid "$vif" ssid
  100. config_get mode "$vif" mode
  101. [ "$mode" = "wds" ] || iwconfig "$phy" essid ${ssid:+-- }"${ssid:-any}"
  102. case "$mode" in
  103. sta)
  104. iwconfig "$phy" mode managed
  105. config_get addr "$device" bssid
  106. [ -z "$addr" ] || {
  107. iwconfig "$phy" ap "$addr"
  108. }
  109. ;;
  110. ap) iwconfig "$phy" mode master;;
  111. wds) iwpriv "$phy" wds_add "$ssid";;
  112. adhoc) iwconfig "$phy" mode ad-hoc;;
  113. *) iwconfig "$phy" mode "$mode";;
  114. esac
  115. [ "$first" = 1 ] && {
  116. config_get rate "$vif" rate
  117. [ -n "$rate" ] && iwconfig "$phy" rate "${rate%%.*}"
  118. config_get_bool hidden "$vif" hidden 0
  119. iwpriv "$phy" enh_sec "$hidden"
  120. config_get frag "$vif" frag
  121. [ -n "$frag" ] && iwconfig "$phy" frag "${frag%%.*}"
  122. config_get rts "$vif" rts
  123. [ -n "$rts" ] && iwconfig "$phy" rts "${rts%%.*}"
  124. config_get maclist "$vif" maclist
  125. [ -n "$maclist" ] && {
  126. # flush MAC list
  127. iwpriv "$phy" maccmd 3
  128. for mac in $maclist; do
  129. iwpriv "$phy" addmac "$mac"
  130. done
  131. }
  132. config_get macpolicy "$vif" macpolicy
  133. case "$macpolicy" in
  134. allow)
  135. iwpriv "$phy" maccmd 2
  136. ;;
  137. deny)
  138. iwpriv "$phy" maccmd 1
  139. ;;
  140. *)
  141. # default deny policy if mac list exists
  142. [ -n "$maclist" ] && iwpriv "$phy" maccmd 1
  143. ;;
  144. esac
  145. # kick all stations if we have policy explicitly set
  146. [ -n "$macpolicy" ] && iwpriv "$phy" maccmd 4
  147. }
  148. config_get enc "$vif" encryption
  149. case "$enc" in
  150. WEP|wep)
  151. for idx in 1 2 3 4; do
  152. config_get key "$vif" "key${idx}"
  153. iwconfig "$ifname" enc "[$idx]" "${key:-off}"
  154. done
  155. config_get key "$vif" key
  156. key="${key:-1}"
  157. case "$key" in
  158. [1234]) iwconfig "$ifname" enc "[$key]";;
  159. *) iwconfig "$ifname" enc "$key";;
  160. esac
  161. ;;
  162. psk*|wpa*)
  163. start_hostapd=1
  164. config_get key "$vif" key
  165. ;;
  166. esac
  167. local net_cfg bridge
  168. net_cfg="$(find_net_config "$vif")"
  169. [ -z "$net_cfg" ] || {
  170. bridge="$(bridge_interface "$net_cfg")"
  171. config_set "$vif" bridge "$bridge"
  172. start_net "$ifname" "$net_cfg"
  173. }
  174. set_wifi_up "$vif" "$ifname"
  175. case "$mode" in
  176. ap)
  177. if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
  178. hostapd_setup_vif "$vif" hostap || {
  179. echo "enable_prism2($device): Failed to set up hostapd for interface $ifname" >&2
  180. # make sure this wifi interface won't accidentally stay open without encryption
  181. ip link set dev "$ifname" down
  182. continue
  183. }
  184. fi
  185. ;;
  186. wds|sta)
  187. if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
  188. wpa_supplicant_setup_vif "$vif" wext || {
  189. echo "enable_prism2($device): Failed to set up wpa_supplicant for interface $ifname" >&2
  190. ip link set dev "$ifname" down
  191. continue
  192. }
  193. fi
  194. ;;
  195. esac
  196. first=0
  197. done
  198. }
  199. check_prism2_device() {
  200. [ ${1%[0-9]} = "wlan" ] && config_set "$1" phy "$1"
  201. config_get phy "$1" phy
  202. [ -z "$phy" ] && {
  203. find_prism2_phy "$1" >/dev/null || return 0
  204. config_get phy "$1" phy
  205. }
  206. [ "$phy" = "$dev" ] && found=1
  207. }
  208. detect_prism2() {
  209. devidx=0
  210. config_load wireless
  211. while :; do
  212. config_get type "radio$devidx" type
  213. [ -n "$type" ] || break
  214. devidx=$(($devidx + 1))
  215. done
  216. cd /proc/net/hostap
  217. [ -d wlan* ] || return
  218. for dev in $(ls -d wlan* 2>&-); do
  219. found=0
  220. config_foreach check_prism2_device wifi-device
  221. [ "$found" -gt 0 ] && continue
  222. cat <<EOF
  223. config wifi-device radio$devidx
  224. option type prism2
  225. option channel 11
  226. option macaddr $(cat /sys/class/net/${dev}/address)
  227. # REMOVE THIS LINE TO ENABLE WIFI:
  228. option disabled 1
  229. config wifi-iface
  230. option device radio$devidx
  231. option network lan
  232. option mode ap
  233. option ssid OpenWrt
  234. option encryption none
  235. EOF
  236. devidx=$(($devidx + 1))
  237. done
  238. }