wifi 4.5 KB

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