wifi 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. . /lib/functions.sh
  4. usage() {
  5. cat <<EOF
  6. Usage: $0 [down|detect]
  7. enables (default), disables or detects a wifi configuration.
  8. EOF
  9. exit 1
  10. }
  11. find_net_config() {(
  12. local vif="$1"
  13. local cfg
  14. local ifname
  15. config_get cfg "$vif" network
  16. [ -z "$cfg" ] && {
  17. include /lib/network
  18. scan_interfaces
  19. config_get ifname "$vif" ifname
  20. cfg="$(find_config "$ifname")"
  21. }
  22. [ -z "$cfg" ] && return 0
  23. echo "$cfg"
  24. )}
  25. bridge_interface() {(
  26. local cfg="$1"
  27. [ -z "$cfg" ] && return 0
  28. include /lib/network
  29. scan_interfaces
  30. for cfg in $cfg; do
  31. config_get iftype "$cfg" type
  32. [ "$iftype" = bridge ] && config_get "$cfg" ifname
  33. prepare_interface_bridge "$cfg"
  34. return $?
  35. done
  36. )}
  37. prepare_key_wep() {
  38. local key="$1"
  39. local hex=1
  40. echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
  41. [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
  42. [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
  43. [ "${key:0:2}" = "s:" ] && key="${key#s:}"
  44. key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
  45. }
  46. echo "$key"
  47. }
  48. wifi_fixup_hwmode() {
  49. local device="$1"
  50. local default="$2"
  51. local hwmode hwmode_11n
  52. config_get channel "$device" channel
  53. config_get hwmode "$device" hwmode
  54. case "$hwmode" in
  55. 11bg) hwmode=bg;;
  56. 11a) hwmode=a;;
  57. 11b) hwmode=b;;
  58. 11g) hwmode=g;;
  59. 11n*)
  60. hwmode_11n="${hwmode##11n}"
  61. case "$hwmode_11n" in
  62. a|g) ;;
  63. default) hwmode_11n="$default"
  64. esac
  65. config_set "$device" hwmode_11n "$hwmode_11n"
  66. ;;
  67. *)
  68. hwmode=
  69. if [ "${channel:-0}" -gt 0 ]; then
  70. if [ "${channel:-0}" -gt 14 ]; then
  71. hwmode=a
  72. else
  73. hwmode=g
  74. fi
  75. else
  76. hwmode="$default"
  77. fi
  78. ;;
  79. esac
  80. config_set "$device" hwmode "$hwmode"
  81. }
  82. wifi_updown() {
  83. [ enable = "$1" ] && {
  84. wifi_updown disable "$2"
  85. scan_wifi
  86. }
  87. for device in ${2:-$DEVICES}; do (
  88. config_get disabled "$device" disabled
  89. [ 1 == "$disabled" ] && {
  90. echo "'$device' is disabled"
  91. set disable
  92. }
  93. config_get iftype "$device" type
  94. if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
  95. eval "scan_$iftype '$device'"
  96. eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
  97. else
  98. echo "$device($iftype): Interface type not supported"
  99. fi
  100. ); done
  101. }
  102. wifi_detect() {
  103. for driver in ${2:-$DRIVERS}; do (
  104. if eval "type detect_$driver" 2>/dev/null >/dev/null; then
  105. eval "detect_$driver" || echo "$driver: Detect failed" >&2
  106. else
  107. echo "$driver: Hardware detection not supported" >&2
  108. fi
  109. ); done
  110. }
  111. start_net() {(
  112. local iface="$1"
  113. local config="$2"
  114. local vifmac="$3"
  115. [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
  116. [ -z "$config" ] || {
  117. include /lib/network
  118. scan_interfaces
  119. for config in $config; do
  120. setup_interface "$iface" "$config" "" "$vifmac"
  121. done
  122. }
  123. )}
  124. set_wifi_up() {
  125. local cfg="$1"
  126. local ifname="$2"
  127. uci_set_state wireless "$cfg" up 1
  128. uci_set_state wireless "$cfg" ifname "$ifname"
  129. }
  130. set_wifi_down() {
  131. local cfg="$1"
  132. local vifs vif vifstr
  133. [ -f "/var/run/wifi-${cfg}.pid" ] &&
  134. kill "$(cat "/var/run/wifi-${cfg}.pid")" 2>/dev/null
  135. uci_revert_state wireless "$cfg"
  136. config_get vifs "$cfg" vifs
  137. for vif in $vifs; do
  138. uci_revert_state wireless "$vif"
  139. done
  140. }
  141. scan_wifi() {
  142. local cfgfile="$1"
  143. DEVICES=
  144. config_cb() {
  145. local type="$1"
  146. local section="$2"
  147. # section start
  148. case "$type" in
  149. wifi-device)
  150. append DEVICES "$section"
  151. config_set "$section" vifs ""
  152. config_set "$section" ht_capab ""
  153. ;;
  154. esac
  155. # section end
  156. config_get TYPE "$CONFIG_SECTION" TYPE
  157. case "$TYPE" in
  158. wifi-iface)
  159. config_get device "$CONFIG_SECTION" device
  160. config_get vifs "$device" vifs
  161. append vifs "$CONFIG_SECTION"
  162. config_set "$device" vifs "$vifs"
  163. ;;
  164. esac
  165. }
  166. config_load "${cfgfile:-wireless}"
  167. }
  168. DEVICES=
  169. DRIVERS=
  170. include /lib/wifi
  171. scan_wifi
  172. case "$1" in
  173. down) wifi_updown "disable" "$2";;
  174. detect) wifi_detect "$2";;
  175. --help|help) usage;;
  176. *) wifi_updown "enable" "$2";;
  177. esac