mac80211.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #!/bin/sh
  2. append DRIVERS "mac80211"
  3. mac80211_hostapd_setup_base() {
  4. local phy="$1"
  5. local ifname="$2"
  6. cfgfile="/var/run/hostapd-$phy.conf"
  7. config_get device "$vif" device
  8. config_get country "$device" country
  9. config_get hwmode "$device" hwmode
  10. config_get channel "$device" channel
  11. [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
  12. [ "$channel" = auto ] && channel=
  13. [ -n "$hwmode" ] && {
  14. config_get hwmode_11n "$device" hwmode_11n
  15. [ -n "$hwmode_11n" ] && {
  16. hwmode="$hwmode_11n"
  17. append base_cfg "ieee80211n=1" "$N"
  18. config_get htmode "$device" htmode
  19. config_get ht_capab_list "$device" ht_capab
  20. case "$htmode" in
  21. HT20|HT40+|HT40-) ht_capab="[$htmode]";;
  22. *)ht_capab=;;
  23. esac
  24. for cap in $ht_capab_list; do
  25. ht_capab="$ht_capab[$cap]"
  26. done
  27. [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
  28. }
  29. }
  30. cat > "$cfgfile" <<EOF
  31. ctrl_interface=/var/run/hostapd-$phy
  32. driver=nl80211
  33. wmm_ac_bk_cwmin=4
  34. wmm_ac_bk_cwmax=10
  35. wmm_ac_bk_aifs=7
  36. wmm_ac_bk_txop_limit=0
  37. wmm_ac_bk_acm=0
  38. wmm_ac_be_aifs=3
  39. wmm_ac_be_cwmin=4
  40. wmm_ac_be_cwmax=10
  41. wmm_ac_be_txop_limit=0
  42. wmm_ac_be_acm=0
  43. wmm_ac_vi_aifs=2
  44. wmm_ac_vi_cwmin=3
  45. wmm_ac_vi_cwmax=4
  46. wmm_ac_vi_txop_limit=94
  47. wmm_ac_vi_acm=0
  48. wmm_ac_vo_aifs=2
  49. wmm_ac_vo_cwmin=2
  50. wmm_ac_vo_cwmax=3
  51. wmm_ac_vo_txop_limit=47
  52. wmm_ac_vo_acm=0
  53. tx_queue_data3_aifs=7
  54. tx_queue_data3_cwmin=15
  55. tx_queue_data3_cwmax=1023
  56. tx_queue_data3_burst=0
  57. tx_queue_data2_aifs=3
  58. tx_queue_data2_cwmin=15
  59. tx_queue_data2_cwmax=63
  60. tx_queue_data2_burst=0
  61. tx_queue_data1_aifs=1
  62. tx_queue_data1_cwmin=7
  63. tx_queue_data1_cwmax=15
  64. tx_queue_data1_burst=3.0
  65. tx_queue_data0_aifs=1
  66. tx_queue_data0_cwmin=3
  67. tx_queue_data0_cwmax=7
  68. tx_queue_data0_burst=1.5
  69. ${hwmode:+hw_mode=$hwmode}
  70. ${channel:+channel=$channel}
  71. ${country:+country_code=$country}
  72. $base_cfg
  73. EOF
  74. }
  75. mac80211_hostapd_setup_bss() {
  76. local phy="$1"
  77. local vif="$2"
  78. hostapd_cfg=
  79. cfgfile="/var/run/hostapd-$phy.conf"
  80. config_get ifname "$vif" ifname
  81. if [ -f "$cfgfile" ]; then
  82. append hostapd_cfg "bss=$ifname" "$N"
  83. else
  84. mac80211_hostapd_setup_base "$phy" "$ifname"
  85. append hostapd_cfg "interface=$ifname" "$N"
  86. fi
  87. local net_cfg bridge
  88. net_cfg="$(find_net_config "$vif")"
  89. [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
  90. config_set "$vif" bridge "$bridge"
  91. hostapd_set_bss_options hostapd_cfg "$vif"
  92. config_get_bool wds "$vif" wds 0
  93. [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
  94. config_get macaddr "$vif" macaddr
  95. config_get_bool hidden "$vif" hidden 0
  96. cat >> /var/run/hostapd-$phy.conf <<EOF
  97. $hostapd_cfg
  98. wmm_enabled=1
  99. bssid=$macaddr
  100. ignore_broadcast_ssid=$hidden
  101. EOF
  102. }
  103. mac80211_start_vif() {
  104. local vif="$1"
  105. local ifname="$2"
  106. local net_cfg
  107. net_cfg="$(find_net_config "$vif")"
  108. [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
  109. set_wifi_up "$vif" "$ifname"
  110. }
  111. find_mac80211_phy() {
  112. local device="$1"
  113. local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
  114. config_get phy "$device" phy
  115. [ -z "$phy" -a -n "$macaddr" ] && {
  116. for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
  117. [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
  118. config_set "$device" phy "$phy"
  119. break
  120. done
  121. config_get phy "$device" phy
  122. }
  123. [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
  124. echo "PHY for wifi device $1 not found"
  125. return 1
  126. }
  127. [ -z "$macaddr" ] && {
  128. config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
  129. }
  130. return 0
  131. }
  132. scan_mac80211() {
  133. local device="$1"
  134. local adhoc sta ap monitor mesh
  135. config_get vifs "$device" vifs
  136. for vif in $vifs; do
  137. config_get mode "$vif" mode
  138. case "$mode" in
  139. adhoc|sta|ap|monitor|mesh)
  140. append $mode "$vif"
  141. ;;
  142. *) echo "$device($vif): Invalid mode, ignored."; continue;;
  143. esac
  144. done
  145. config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
  146. }
  147. disable_mac80211() (
  148. local device="$1"
  149. find_mac80211_phy "$device" || return 0
  150. config_get phy "$device" phy
  151. set_wifi_down "$device"
  152. # kill all running hostapd and wpa_supplicant processes that
  153. # are running on atheros/mac80211 vifs
  154. for pid in `pidof hostapd wpa_supplicant`; do
  155. grep "$phy" /proc/$pid/cmdline >/dev/null && \
  156. kill $pid
  157. done
  158. include /lib/network
  159. for wdev in $(ls /sys/class/ieee80211/${phy}/device/net 2>/dev/null); do
  160. ifconfig "$wdev" down 2>/dev/null
  161. unbridge "$dev"
  162. iw dev "$wdev" del
  163. done
  164. return 0
  165. )
  166. get_freq() {
  167. local phy="$1"
  168. local chan="$2"
  169. iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
  170. }
  171. enable_mac80211() {
  172. local device="$1"
  173. config_get channel "$device" channel
  174. config_get vifs "$device" vifs
  175. config_get txpower "$device" txpower
  176. config_get country "$device" country
  177. config_get distance "$device" distance
  178. find_mac80211_phy "$device" || return 0
  179. config_get phy "$device" phy
  180. local i=0
  181. local macidx=0
  182. local apidx=0
  183. fixed=""
  184. [ -n "$country" ] && iw reg set "$country"
  185. [ "$channel" = "auto" -o "$channel" = "0" ] || {
  186. fixed=1
  187. }
  188. [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
  189. export channel fixed
  190. # convert channel to frequency
  191. local freq="$(get_freq "$phy" "${fixed:+$channel}")"
  192. wifi_fixup_hwmode "$device" "g"
  193. for vif in $vifs; do
  194. while [ -d "/sys/class/net/wlan$i" ]; do
  195. i=$(($i + 1))
  196. done
  197. config_get ifname "$vif" ifname
  198. [ -n "$ifname" ] || {
  199. ifname="wlan$i"
  200. }
  201. config_set "$vif" ifname "$ifname"
  202. config_get enc "$vif" encryption
  203. config_get mode "$vif" mode
  204. config_get ssid "$vif" ssid
  205. # It is far easier to delete and create the desired interface
  206. case "$mode" in
  207. adhoc)
  208. iw phy "$phy" interface add "$ifname" type adhoc
  209. ;;
  210. ap)
  211. # Hostapd will handle recreating the interface and
  212. # it's accompanying monitor
  213. apidx="$(($apidx + 1))"
  214. [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
  215. ;;
  216. mesh)
  217. config_get mesh_id "$vif" mesh_id
  218. iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
  219. ;;
  220. monitor)
  221. iw phy "$phy" interface add "$ifname" type monitor
  222. ;;
  223. sta)
  224. local wdsflag
  225. config_get_bool wds "$vif" wds 0
  226. [ "$wds" -gt 0 ] && wdsflag="4addr on"
  227. iw phy "$phy" interface add "$ifname" type managed $wdsflag
  228. config_get_bool powersave "$vif" powersave 0
  229. [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
  230. iwconfig "$ifname" power "$powersave"
  231. ;;
  232. esac
  233. # All interfaces must have unique mac addresses
  234. # which can either be explicitly set in the device
  235. # section, or automatically generated
  236. config_get macaddr "$device" macaddr
  237. local mac_1="${macaddr%%:*}"
  238. local mac_2="${macaddr#*:}"
  239. config_get vif_mac "$vif" macaddr
  240. [ -n "$vif_mac" ] || {
  241. if [ "$macidx" -gt 0 ]; then
  242. offset="$(( 2 + $macidx * 4 ))"
  243. else
  244. offset="0"
  245. fi
  246. vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
  247. macidx="$(($macidx + 1))"
  248. }
  249. [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
  250. config_set "$vif" macaddr "$vif_mac"
  251. # Valid values are:
  252. # wpa / wep / none
  253. #
  254. # !! ap !!
  255. #
  256. # ALL ap functionality will be passed to hostapd
  257. #
  258. # !! mesh / adhoc / station !!
  259. # none -> NO encryption
  260. #
  261. # wep + keymgmt = '' -> we use iw to connect to the
  262. # network.
  263. #
  264. # wep + keymgmt = 'NONE' -> wpa_supplicant will be
  265. # configured to handle the wep connection
  266. if [ ! "$mode" = "ap" ]; then
  267. # We attempt to set the channel for all interfaces, although
  268. # mac80211 may not support it or the driver might not yet
  269. # for ap mode this is handled by hostapd
  270. [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
  271. local key keystring
  272. case "$enc" in
  273. *wep*)
  274. config_get keymgmt "$vif" keymgmt
  275. if [ -z "$keymgmt" ]; then
  276. config_get key "$vif" key
  277. key="${key:-1}"
  278. case "$key" in
  279. [1234])
  280. for idx in 1 2 3 4; do
  281. local zidx
  282. zidx=$(($idx - 1))
  283. config_get ckey "$vif" "key${idx}"
  284. if [ -n "$ckey" ]; then
  285. [ $idx -eq $key ] && zidx="d:${zidx}"
  286. append keystring "${zidx}:$(prepare_key_wep "$ckey")"
  287. fi
  288. done
  289. ;;
  290. *)
  291. keystring="d:0:$(prepare_key_wep "$key")"
  292. ;;
  293. esac
  294. fi
  295. ;;
  296. *psk*|*wpa*)
  297. config_get key "$vif" key
  298. ;;
  299. esac
  300. fi
  301. # txpower is not yet implemented in iw
  302. config_get vif_txpower "$vif" txpower
  303. # use vif_txpower (from wifi-iface) to override txpower (from
  304. # wifi-device) if the latter doesn't exist
  305. txpower="${txpower:-$vif_txpower}"
  306. [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
  307. config_get frag "$vif" frag
  308. if [ -n "$frag" ]; then
  309. iw phy "$phy" set frag "${frag%%.*}"
  310. fi
  311. config_get rts "$vif" rts
  312. if [ -n "$rts" ]; then
  313. iw phy "$phy" set rts "${rts%%.*}"
  314. fi
  315. ifconfig "$ifname" up
  316. [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
  317. case "$mode" in
  318. adhoc)
  319. config_get bssid "$vif" bssid
  320. iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid
  321. ;;
  322. sta|mesh)
  323. config_get bssid "$vif" bssid
  324. case "$enc" in
  325. *wep*)
  326. if [ -z "$keymgmt" ]; then
  327. [ -n "$keystring" ] &&
  328. iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key $keystring
  329. else
  330. if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
  331. wpa_supplicant_setup_vif "$vif" wext || {
  332. echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
  333. # make sure this wifi interface won't accidentally stay open without encryption
  334. ifconfig "$ifname" down
  335. continue
  336. }
  337. fi
  338. fi
  339. ;;
  340. *wpa*|*psk*)
  341. config_get key "$vif" key
  342. if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
  343. wpa_supplicant_setup_vif "$vif" wext || {
  344. echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
  345. # make sure this wifi interface won't accidentally stay open without encryption
  346. ifconfig "$ifname" down
  347. continue
  348. }
  349. fi
  350. ;;
  351. *)
  352. iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid
  353. ;;
  354. esac
  355. ;;
  356. esac
  357. done
  358. local start_hostapd=
  359. rm -f /var/run/hostapd-$phy.conf
  360. for vif in $vifs; do
  361. config_get mode "$vif" mode
  362. [ "$mode" = "ap" ] || continue
  363. mac80211_hostapd_setup_bss "$phy" "$vif"
  364. start_hostapd=1
  365. done
  366. [ -n "$start_hostapd" ] || return 0
  367. hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
  368. echo "Failed to start hostapd for $phy"
  369. return
  370. }
  371. sleep 2
  372. for vif in $vifs; do
  373. config_get mode "$vif" mode
  374. config_get ifname "$vif" ifname
  375. [ "$mode" = "ap" ] || continue
  376. mac80211_start_vif "$vif" "$ifname"
  377. done
  378. }
  379. check_device() {
  380. config_get phy "$1" phy
  381. [ -z "$phy" ] && {
  382. find_mac80211_phy "$1" >/dev/null || return 0
  383. config_get phy "$1" phy
  384. }
  385. [ "$phy" = "$dev" ] && found=1
  386. }
  387. detect_mac80211() {
  388. devidx=0
  389. config_load wireless
  390. while :; do
  391. config_get type "radio$devidx" type
  392. [ -n "$type" ] || break
  393. devidx=$(($devidx + 1))
  394. done
  395. for dev in $(ls /sys/class/ieee80211); do
  396. found=0
  397. config_foreach check_device wifi-device
  398. [ "$found" -gt 0 ] && continue
  399. mode_11n=""
  400. mode_band="g"
  401. channel="5"
  402. ht_cap=0
  403. for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
  404. ht_cap="$(($ht_cap | $cap))"
  405. done
  406. ht_capab="";
  407. [ "$ht_cap" -gt 0 ] && {
  408. mode_11n="n"
  409. append ht_capab " option htmode HT20" "$N"
  410. list=" list ht_capab"
  411. [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
  412. [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
  413. [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
  414. [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
  415. }
  416. iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
  417. cat <<EOF
  418. config wifi-device radio$devidx
  419. option type mac80211
  420. option channel ${channel}
  421. option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
  422. option hwmode 11${mode_11n}${mode_band}
  423. $ht_capab
  424. # REMOVE THIS LINE TO ENABLE WIFI:
  425. option disabled 1
  426. config wifi-iface
  427. option device radio$devidx
  428. option network lan
  429. option mode ap
  430. option ssid OpenWrt
  431. option encryption none
  432. EOF
  433. devidx=$(($devidx + 1))
  434. done
  435. }