madwifi.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/sh
  2. append DRIVERS "atheros"
  3. scan_atheros() {
  4. local device="$1"
  5. local wds
  6. local adhoc sta ap
  7. config_get vifs "$device" vifs
  8. for vif in $vifs; do
  9. config_get ifname "$vif" ifname
  10. config_set "$vif" ifname "${ifname:-ath}"
  11. config_get mode "$vif" mode
  12. case "$mode" in
  13. adhoc|sta|ap)
  14. append $mode "$vif"
  15. ;;
  16. wds)
  17. config_get addr "$vif" bssid
  18. config_get ssid "$vif" ssid
  19. [ -z "$addr" -a -n "$ssid" ] && {
  20. config_set "$vif" wds 1
  21. config_set "$vif" mode sta
  22. mode="sta"
  23. addr="$ssid"
  24. }
  25. ${addr:+append $mode "$vif"}
  26. ;;
  27. *) echo "$device($vif): Invalid mode, ignored."; continue;;
  28. esac
  29. done
  30. case "${adhoc:+1}:${sta:+1}:${ap+1}" in
  31. # valid mode combinations
  32. 1::) wds="";;
  33. :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
  34. :1:);;
  35. ::1);;
  36. ::);;
  37. *) echo "$device: Invalid mode combination in config"; return 1;;
  38. esac
  39. config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${wds:+$wds }"
  40. }
  41. disable_atheros() (
  42. local device="$1"
  43. # kill all running hostapd and wpa_supplicant processes that
  44. # are running on atheros vifs
  45. for pid in `pidof hostapd wpa_supplicant`; do
  46. grep ath /proc/$pid/cmdline >/dev/null && \
  47. kill $pid
  48. done
  49. include /lib/network
  50. cd /proc/sys/net
  51. for dev in *; do
  52. grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
  53. ifconfig "$dev" down
  54. unbridge "$dev"
  55. wlanconfig "$dev" destroy
  56. }
  57. done
  58. return 0
  59. )
  60. enable_atheros() {
  61. config_get channel "$device" channel
  62. config_get vifs "$device" vifs
  63. disable_atheros "$device"
  64. local first=1
  65. for vif in $vifs; do
  66. nosbeacon=
  67. config_get ifname "$vif" ifname
  68. config_get enc "$vif" encryption
  69. config_get mode "$vif" mode
  70. [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
  71. config_get ifname "$vif" ifname
  72. ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
  73. [ $? -ne 0 ] && {
  74. echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
  75. continue
  76. }
  77. config_set "$vif" ifname "$ifname"
  78. [ "$first" = 1 ] && {
  79. # only need to change freq band and channel on the first vif
  80. config_get agmode "$device" mode
  81. pureg=0
  82. case "$agmode" in
  83. *b) agmode=11b;;
  84. *bg) agmode=11g;;
  85. *g) agmode=11g; pureg=1;;
  86. *a) agmode=11a;;
  87. *) agmode=auto;;
  88. esac
  89. iwconfig "$ifname" channel 0 >/dev/null 2>/dev/null
  90. ifconfig "$ifname" up
  91. iwpriv "$ifname" mode "$agmode"
  92. iwpriv "$ifname" pureg "$pureg"
  93. iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
  94. }
  95. config_get_bool hidden "$vif" hidden
  96. iwpriv "$ifname" hide_ssid "$hidden"
  97. config_get wds "$vif" wds
  98. case "$wds" in
  99. 1|on|enabled) wds=1;;
  100. *) wds=0;;
  101. esac
  102. iwpriv "$ifname" wds "$wds"
  103. wpa=
  104. case "$enc" in
  105. WEP|wep)
  106. for idx in 1 2 3 4; do
  107. config_get key "$vif" "key${idx}"
  108. iwconfig "$ifname" enc "[$idx]" "${key:-off}"
  109. done
  110. config_get key "$vif" key
  111. key="${key:-1}"
  112. case "$key" in
  113. [1234]) iwconfig "$ifname" enc "[$key]";;
  114. *) iwconfig "$ifname" enc "$key";;
  115. esac
  116. ;;
  117. PSK|psk|PSK2|psk2)
  118. config_get key "$vif" key
  119. ;;
  120. esac
  121. case "$mode" in
  122. wds)
  123. config_get addr "$vif" bssid
  124. iwpriv "$ifname" wds_add "$addr"
  125. ;;
  126. adhoc)
  127. config_get addr "$vif" bssid
  128. [ -z "$addr" ] || {
  129. iwconfig "$ifname" ap "$addr"
  130. }
  131. ;;
  132. *)
  133. config_get ssid "$vif" ssid
  134. ;;
  135. esac
  136. [ "$mode" = "sta" ] && {
  137. config_get_bool bgscan "$vif" bgscan 1
  138. iwpriv "$ifname" bgscan "$bgscan"
  139. }
  140. config_get_bool antdiv "$device" diversity 1
  141. sysctl -w dev."$device".diversity="$antdiv" >&-
  142. config_get antrx "$device" rxantenna
  143. if [ -n "$antrx" ]; then
  144. sysctl -w dev."$device".rxantenna="$antrx" >&-
  145. fi
  146. config_get anttx "$device" txantenna
  147. if [ -n "$anttx" ]; then
  148. sysctl -w dev."$device".txantenna="$anttx" >&-
  149. fi
  150. config_get distance "$device" distance
  151. if [ -n "$distance" ]; then
  152. athctrl -i "$device" -d "$distance" >&-
  153. fi
  154. config_get txpwr "$vif" txpower
  155. if [ -n "$txpwr" ]; then
  156. iwconfig "$ifname" txpower "${txpwr%%.*}"
  157. fi
  158. ifconfig "$ifname" up
  159. iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
  160. local net_cfg bridge
  161. net_cfg="$(find_net_config "$vif")"
  162. [ -z "$net_cfg" ] || {
  163. bridge="$(bridge_interface "$net_cfg")"
  164. config_set "$vif" bridge "$bridge"
  165. start_net "$ifname" "$net_cfg"
  166. }
  167. iwconfig "$ifname" essid "$ssid"
  168. case "$mode" in
  169. ap)
  170. config_get_bool isolate "$vif" isolate 0
  171. iwpriv "$ifname" ap_bridge "$((isolate^1))"
  172. if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
  173. hostapd_setup_vif "$vif" madwifi || {
  174. echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
  175. # make sure this wifi interface won't accidentally stay open without encryption
  176. ifconfig "$ifname" down
  177. wlanconfig "$ifname" destroy
  178. continue
  179. }
  180. fi
  181. ;;
  182. wds|sta)
  183. case "$enc" in
  184. PSK|psk|PSK2|psk2)
  185. case "$enc" in
  186. PSK|psk)
  187. proto='proto=WPA';;
  188. PSK2|psk2)
  189. proto='proto=RSN';;
  190. esac
  191. cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
  192. ctrl_interface=/var/run/wpa_supplicant
  193. network={
  194. scan_ssid=1
  195. ssid="$ssid"
  196. key_mgmt=WPA-PSK
  197. $proto
  198. psk="$key"
  199. }
  200. EOF
  201. ;;
  202. WPA|wpa|WPA2|wpa2)
  203. #add wpa_supplicant calls here
  204. ;;
  205. esac
  206. [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -Bw -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
  207. ;;
  208. esac
  209. first=0
  210. done
  211. }
  212. detect_atheros() {
  213. cd /proc/sys/dev
  214. [ -d ath ] || return
  215. for dev in $(ls -d wifi* 2>&-); do
  216. config_get type "$dev" type
  217. [ "$type" = atheros ] && return
  218. cat <<EOF
  219. config wifi-device $dev
  220. option type atheros
  221. option channel 5
  222. # option diversity 1
  223. # option txantenna 0
  224. # option rxantenna 0
  225. # option distance 2000
  226. # disable radio to prevent an open ap after reflashing:
  227. option disabled 1
  228. config wifi-iface
  229. option device $dev
  230. option network lan
  231. option mode ap
  232. option ssid OpenWrt
  233. option hidden 0
  234. # option txpower 15
  235. # option bgscan enable
  236. option encryption none
  237. EOF
  238. done
  239. }