wifi 5.2 KB

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