madwifi.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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=11g;;
  88. esac
  89. iwconfig "$ifname" channel 0
  90. iwpriv "$ifname" mode "$agmode"
  91. iwpriv "$ifname" pureg "$pureg"
  92. iwconfig "$ifname" channel "$channel"
  93. }
  94. config_get_bool hidden "$vif" hidden
  95. iwpriv "$ifname" hide_ssid "$hidden"
  96. config_get wds "$vif" wds
  97. case "$wds" in
  98. 1|on|enabled) wds=1;;
  99. *) wds=0;;
  100. esac
  101. iwpriv "$ifname" wds "$wds"
  102. wpa=
  103. case "$enc" in
  104. WEP|wep)
  105. for idx in 1 2 3 4; do
  106. config_get key "$vif" "key${idx}"
  107. iwconfig "$ifname" enc "[$idx]" "${key:-off}"
  108. done
  109. config_get key "$vif" key
  110. key="${key:-1}"
  111. case "$key" in
  112. [1234]) iwconfig "$ifname" enc "[$key]";;
  113. *) iwconfig "$ifname" enc "$key";;
  114. esac
  115. ;;
  116. PSK|psk|PSK2|psk2)
  117. config_get key "$vif" key
  118. ;;
  119. esac
  120. case "$mode" in
  121. wds)
  122. config_get addr "$vif" bssid
  123. iwpriv "$ifname" wds_add "$addr"
  124. ;;
  125. *)
  126. config_get ssid "$vif" ssid
  127. ;;
  128. adhoc)
  129. config_get addr "$vif" bssid
  130. [ -z "$addr" ] || {
  131. iwconfig "$ifname" ap "$addr"
  132. }
  133. ;;
  134. esac
  135. [ "$mode" = "sta" ] && {
  136. config_get_bool bgscan "$vif" bgscan 1
  137. iwpriv "$ifname" bgscan "$bgscan"
  138. }
  139. config_get_bool antdiv "$device" diversity 1
  140. sysctl -w dev."$device".diversity="$antdiv" >&-
  141. config_get antrx "$device" rxantenna
  142. if [ -n "$antrx" ]; then
  143. sysctl -w dev."$device".rxantenna="$antrx" >&-
  144. fi
  145. config_get anttx "$device" txantenna
  146. if [ -n "$anttx" ]; then
  147. sysctl -w dev."$device".txantenna="$anttx" >&-
  148. fi
  149. config_get distance "$device" distance
  150. if [ -n "$distance" ]; then
  151. athctrl -i "$device" -d "$distance" >&-
  152. fi
  153. config_get txpwr "$vif" txpower
  154. if [ -n "$txpwr" ]; then
  155. iwconfig "$ifname" txpower "${txpwr%%.*}"
  156. fi
  157. ifconfig "$ifname" up
  158. iwconfig "$ifname" channel "$channel"
  159. local net_cfg bridge
  160. net_cfg="$(find_net_config "$vif")"
  161. [ -z "$net_cfg" ] || {
  162. bridge="$(bridge_interface "$net_cfg")"
  163. config_set "$vif" bridge "$bridge"
  164. start_net "$ifname" "$net_cfg"
  165. }
  166. iwconfig "$ifname" essid "$ssid"
  167. case "$mode" in
  168. ap)
  169. hostapd_setup_vif "$vif" madwifi || {
  170. echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
  171. # make sure this wifi interface won't accidentally stay open without encryption
  172. ifconfig "$ifname" down
  173. wlanconfig "$ifname" destroy
  174. continue
  175. }
  176. ;;
  177. wds|sta)
  178. case "$enc" in
  179. PSK|psk|PSK2|psk2)
  180. case "$enc" in
  181. PSK|psk)
  182. proto='proto=WPA';;
  183. PSK2|psk2)
  184. proto='proto=RSN';;
  185. esac
  186. cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
  187. ctrl_interface=/var/run/wpa_supplicant
  188. network={
  189. scan_ssid=1
  190. ssid="$ssid"
  191. key_mgmt=WPA-PSK
  192. $proto
  193. psk="$key"
  194. }
  195. EOF
  196. ;;
  197. WPA|wpa|WPA2|wpa2)
  198. #add wpa_supplicant calls here
  199. ;;
  200. esac
  201. [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -Bw -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
  202. ;;
  203. esac
  204. first=0
  205. done
  206. }
  207. detect_atheros() {
  208. cd /proc/sys/dev
  209. [ -d ath ] || return
  210. for dev in $(ls -d wifi* 2>&-); do
  211. config_get type "$dev" type
  212. [ "$type" = atheros ] && return
  213. cat <<EOF
  214. config wifi-device $dev
  215. option type atheros
  216. option channel 5
  217. # option diversity 1
  218. # option txantenna 0
  219. # option rxantenna 0
  220. # option distance 2000
  221. # disable radio to prevent an open ap after reflashing:
  222. option disabled 1
  223. config wifi-iface
  224. option device $dev
  225. option network lan
  226. option mode ap
  227. option ssid OpenWrt
  228. option hidden 0
  229. # option txpower 15
  230. # option bgscan enable
  231. option encryption none
  232. EOF
  233. done
  234. }