madwifi.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. hostapd_setup_vif() {
  42. local vif="$1"
  43. local driver="$2"
  44. local hostapd_cfg=
  45. # Examples:
  46. # psk-mixed/tkip => WPA1+2 PSK, TKIP
  47. # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
  48. # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
  49. # ...
  50. # TODO: move this parsing function somewhere generic, so that
  51. # later it can be reused by drivers that don't use hostapd
  52. # crypto defaults: WPA2 vs WPA1
  53. case "$enc" in
  54. wpa2*|WPA2*|*PSK2*|*psk2*)
  55. wpa=2
  56. crypto="CCMP"
  57. ;;
  58. *mixed*)
  59. wpa=3
  60. crypto="CCMP TKIP"
  61. ;;
  62. *)
  63. wpa=1
  64. crypto="TKIP"
  65. ;;
  66. esac
  67. # explicit override for crypto setting
  68. case "$enc" in
  69. *tkip+aes|*TKIP+AES|*tkip+ccmp|*TKIP+CCMP) crypto="CCMP TKIP";;
  70. *tkip|*TKIP) crypto="TKIP";;
  71. *aes|*AES|*ccmp|*CCMP) crypto="CCMP";;
  72. esac
  73. # use crypto/auth settings for building the hostapd config
  74. case "$enc" in
  75. *psk*|*PSK*)
  76. config_get psk "$vif" key
  77. append hostapd_cfg "wpa_passphrase=$psk" "$N"
  78. ;;
  79. *wpa*|*WPA*)
  80. # FIXME: add wpa+radius here
  81. ;;
  82. *)
  83. return 0;
  84. ;;
  85. esac
  86. config_get ifname "$vif" ifname
  87. config_get bridge "$vif" bridge
  88. config_get ssid "$vif" ssid
  89. cat > /var/run/hostapd-$ifname.conf <<EOF
  90. driver=$driver
  91. interface=$ifname
  92. ${bridge:+bridge=$bridge}
  93. ssid=$ssid
  94. debug=0
  95. wpa=$wpa
  96. wpa_pairwise=$crypto
  97. $hostapd_cfg
  98. EOF
  99. hostapd -B /var/run/hostapd-$ifname.conf
  100. }
  101. disable_atheros() (
  102. local device="$1"
  103. # kill all running hostapd and wpa_supplicant processes that
  104. # are running on atheros vifs
  105. for pid in `pidof hostapd wpa_supplicant`; do
  106. grep ath /proc/$pid/cmdline >/dev/null && \
  107. kill $pid
  108. done
  109. include /lib/network
  110. cd /proc/sys/net
  111. for dev in *; do
  112. grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
  113. ifconfig "$dev" down
  114. unbridge "$dev"
  115. wlanconfig "$dev" destroy
  116. }
  117. done
  118. return 0
  119. )
  120. enable_atheros() {
  121. config_get channel "$device" channel
  122. config_get vifs "$device" vifs
  123. disable_atheros "$device"
  124. for vif in $vifs; do
  125. nosbeacon=
  126. config_get ifname "$vif" ifname
  127. config_get enc "$vif" encryption
  128. config_get mode "$vif" mode
  129. [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
  130. config_get ifname "$vif" ifname
  131. ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
  132. [ $? -ne 0 ] && {
  133. echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
  134. continue
  135. }
  136. config_set "$vif" ifname "$ifname"
  137. config_get "$device" mode
  138. iwpriv "$ifname" mode "${mode:-11g}"
  139. config_get wds "$vif" wds
  140. case "$wds" in
  141. 1|on|enabled) wds=1;;
  142. *) wds=0;;
  143. esac
  144. iwpriv "$ifname" wds "$wds"
  145. wpa=
  146. case "$enc" in
  147. WEP|wep)
  148. for idx in 1 2 3 4; do
  149. config_get key "$vif" "key${idx}"
  150. iwconfig "$ifname" enc "[$idx]" "${key:-off}"
  151. done
  152. config_get key "$vif" key
  153. iwconfig "$ifname" enc "[${key:-1}]"
  154. ;;
  155. esac
  156. case "$mode" in
  157. wds)
  158. config_get addr "$vif" bssid
  159. iwpriv "$ifname" wds_add "$addr"
  160. ;;
  161. *)
  162. config_get ssid "$vif" ssid
  163. ;;
  164. esac
  165. iwconfig "$ifname" channel "$channel"
  166. ifconfig "$ifname" up
  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. case "$mode" in
  175. ap)
  176. hostapd_setup_vif "$vif" madwifi || {
  177. echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
  178. # make sure this wifi interface won't accidentally stay open without encryption
  179. ifconfig "$ifname" down
  180. wlanconfig "$ifname" destroy
  181. continue
  182. }
  183. ;;
  184. wds|sta)
  185. iwconfig "$ifname" essid "$ssid"
  186. # FIXME: implement wpa_supplicant calls here
  187. ;;
  188. esac
  189. done
  190. }
  191. detect_atheros() {
  192. cd /proc/sys/dev
  193. [ -d ath ] || return
  194. for dev in wifi*; do
  195. config_get type "$dev" type
  196. [ "$type" = atheros ] && return
  197. cat <<EOF
  198. config wifi-device $dev
  199. option type atheros
  200. option channel 5
  201. config wifi-iface
  202. option device $dev
  203. # option network lan
  204. option mode ap
  205. option ssid OpenWrt
  206. option hidden 0
  207. option encryption none
  208. EOF
  209. done
  210. }