wifi 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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|down|reload|status]
  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 "$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_notice() {
  127. >&2 echo "WARNING: Wifi detect is deprecated. Use wifi config instead"
  128. >&2 echo "For more information, see commit 5f8f8a366136a07df661e31decce2458357c167a"
  129. exit 1
  130. }
  131. wifi_config() {
  132. [ ! -f /etc/config/wireless ] && touch /etc/config/wireless
  133. for driver in $DRIVERS; do (
  134. if eval "type detect_$driver" 2>/dev/null >/dev/null; then
  135. eval "detect_$driver" || echo "$driver: Detect failed" >&2
  136. else
  137. echo "$driver: Hardware detection not supported" >&2
  138. fi
  139. ); done
  140. }
  141. start_net() {(
  142. local iface="$1"
  143. local config="$2"
  144. local vifmac="$3"
  145. [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
  146. [ -z "$config" ] || {
  147. include /lib/network
  148. scan_interfaces
  149. for config in $config; do
  150. setup_interface "$iface" "$config" "" "$vifmac"
  151. done
  152. }
  153. )}
  154. set_wifi_up() {
  155. local cfg="$1"
  156. local ifname="$2"
  157. uci_set_state wireless "$cfg" up 1
  158. uci_set_state wireless "$cfg" ifname "$ifname"
  159. }
  160. set_wifi_down() {
  161. local cfg="$1"
  162. local vifs vif vifstr
  163. [ -f "/var/run/wifi-${cfg}.pid" ] &&
  164. kill "$(cat "/var/run/wifi-${cfg}.pid")" 2>/dev/null
  165. uci_revert_state wireless "$cfg"
  166. config_get vifs "$cfg" vifs
  167. for vif in $vifs; do
  168. uci_revert_state wireless "$vif"
  169. done
  170. }
  171. scan_wifi() {
  172. local cfgfile="$1"
  173. DEVICES=
  174. config_cb() {
  175. local type="$1"
  176. local section="$2"
  177. # section start
  178. case "$type" in
  179. wifi-device)
  180. append DEVICES "$section"
  181. config_set "$section" vifs ""
  182. config_set "$section" ht_capab ""
  183. ;;
  184. esac
  185. # section end
  186. config_get TYPE "$CONFIG_SECTION" TYPE
  187. case "$TYPE" in
  188. wifi-iface)
  189. config_get device "$CONFIG_SECTION" device
  190. config_get vifs "$device" vifs
  191. append vifs "$CONFIG_SECTION"
  192. config_set "$device" vifs "$vifs"
  193. ;;
  194. esac
  195. }
  196. config_load "${cfgfile:-wireless}"
  197. }
  198. DEVICES=
  199. DRIVERS=
  200. include /lib/wifi
  201. scan_wifi
  202. case "$1" in
  203. down) wifi_updown "disable" "$2";;
  204. detect) wifi_detect_notice ;;
  205. config) wifi_config ;;
  206. status) ubus_wifi_cmd "status" "$2";;
  207. reload) wifi_reload "$2";;
  208. reload_legacy) wifi_reload_legacy "$2";;
  209. --help|help) usage;;
  210. *) ubus call network reload; wifi_updown "enable" "$2";;
  211. esac