madwifi.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. iwconfig "$ifname" enc "${key:-1}"
  111. ;;
  112. esac
  113. case "$mode" in
  114. wds)
  115. config_get addr "$vif" bssid
  116. iwpriv "$ifname" wds_add "$addr"
  117. ;;
  118. *)
  119. config_get ssid "$vif" ssid
  120. ;;
  121. esac
  122. ifconfig "$ifname" up
  123. local net_cfg bridge
  124. net_cfg="$(find_net_config "$vif")"
  125. [ -z "$net_cfg" ] || {
  126. bridge="$(bridge_interface "$net_cfg")"
  127. config_set "$vif" bridge "$bridge"
  128. start_net "$ifname" "$net_cfg"
  129. }
  130. iwconfig "$ifname" essid "$ssid"
  131. case "$mode" in
  132. ap)
  133. hostapd_setup_vif "$vif" madwifi || {
  134. echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
  135. # make sure this wifi interface won't accidentally stay open without encryption
  136. ifconfig "$ifname" down
  137. wlanconfig "$ifname" destroy
  138. continue
  139. }
  140. ;;
  141. wds|sta)
  142. # FIXME: implement wpa_supplicant calls here
  143. ;;
  144. esac
  145. first=0
  146. done
  147. }
  148. detect_atheros() {
  149. cd /proc/sys/dev
  150. [ -d ath ] || return
  151. for dev in wifi*; do
  152. config_get type "$dev" type
  153. [ "$type" = atheros ] && return
  154. cat <<EOF
  155. config wifi-device $dev
  156. option type atheros
  157. option channel 5
  158. config wifi-iface
  159. option device $dev
  160. # option network lan
  161. option mode ap
  162. option ssid OpenWrt
  163. option hidden 0
  164. option encryption none
  165. EOF
  166. done
  167. }