madwifi.sh 6.2 KB

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