mac80211.sh 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. #!/bin/sh
  2. . /lib/netifd/netifd-wireless.sh
  3. . /lib/netifd/hostapd.sh
  4. . /lib/netifd/mac80211.sh
  5. init_wireless_driver "$@"
  6. MP_CONFIG_INT="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
  7. mesh_max_retries mesh_ttl mesh_element_ttl mesh_hwmp_max_preq_retries
  8. mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
  9. mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
  10. mesh_hwmp_rann_interval mesh_gate_announcements mesh_sync_offset_max_neighor
  11. mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
  12. mesh_hwmp_confirmation_interval mesh_awake_window mesh_plink_timeout"
  13. MP_CONFIG_BOOL="mesh_auto_open_plinks mesh_fwding"
  14. MP_CONFIG_STRING="mesh_power_mode"
  15. NEWAPLIST=
  16. OLDAPLIST=
  17. NEWSPLIST=
  18. OLDSPLIST=
  19. NEWUMLIST=
  20. OLDUMLIST=
  21. drv_mac80211_init_device_config() {
  22. hostapd_common_add_device_config
  23. config_add_string path phy 'macaddr:macaddr'
  24. config_add_string hwmode
  25. config_add_string tx_burst
  26. config_add_int beacon_int chanbw frag rts
  27. config_add_int rxantenna txantenna antenna_gain txpower distance
  28. config_add_boolean noscan ht_coex acs_exclude_dfs
  29. config_add_array ht_capab
  30. config_add_array channels
  31. config_add_boolean \
  32. rxldpc \
  33. short_gi_80 \
  34. short_gi_160 \
  35. tx_stbc_2by1 \
  36. su_beamformer \
  37. su_beamformee \
  38. mu_beamformer \
  39. mu_beamformee \
  40. vht_txop_ps \
  41. htc_vht \
  42. rx_antenna_pattern \
  43. tx_antenna_pattern
  44. config_add_int vht_max_a_mpdu_len_exp vht_max_mpdu vht_link_adapt vht160 rx_stbc tx_stbc
  45. config_add_boolean \
  46. ldpc \
  47. greenfield \
  48. short_gi_20 \
  49. short_gi_40 \
  50. max_amsdu \
  51. dsss_cck_40
  52. }
  53. drv_mac80211_init_iface_config() {
  54. hostapd_common_add_bss_config
  55. config_add_string 'macaddr:macaddr' ifname
  56. config_add_boolean wds powersave enable
  57. config_add_string wds_bridge
  58. config_add_int maxassoc
  59. config_add_int max_listen_int
  60. config_add_int dtim_period
  61. config_add_int start_disabled
  62. # mesh
  63. config_add_string mesh_id
  64. config_add_int $MP_CONFIG_INT
  65. config_add_boolean $MP_CONFIG_BOOL
  66. config_add_string $MP_CONFIG_STRING
  67. }
  68. mac80211_add_capabilities() {
  69. local __var="$1"; shift
  70. local __mask="$1"; shift
  71. local __out= oifs
  72. oifs="$IFS"
  73. IFS=:
  74. for capab in "$@"; do
  75. set -- $capab
  76. [ "$(($4))" -gt 0 ] || continue
  77. [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] || continue
  78. __out="$__out[$1]"
  79. done
  80. IFS="$oifs"
  81. export -n -- "$__var=$__out"
  82. }
  83. mac80211_hostapd_setup_base() {
  84. local phy="$1"
  85. json_select config
  86. [ "$auto_channel" -gt 0 ] && channel=acs_survey
  87. [ "$auto_channel" -gt 0 ] && json_get_values channel_list channels
  88. [ "$auto_channel" -gt 0 ] && json_get_vars acs_exclude_dfs
  89. [ -n "$acs_exclude_dfs" ] && [ "$acs_exclude_dfs" -gt 0 ] &&
  90. append base_cfg "acs_exclude_dfs=1" "$N"
  91. json_get_vars noscan ht_coex
  92. json_get_values ht_capab_list ht_capab tx_burst
  93. set_default noscan 0
  94. [ "$noscan" -gt 0 ] && hostapd_noscan=1
  95. [ "$tx_burst" = 0 ] && tx_burst=
  96. ieee80211n=1
  97. ht_capab=
  98. case "$htmode" in
  99. VHT20|HT20) ;;
  100. HT40*|VHT40|VHT80|VHT160)
  101. case "$hwmode" in
  102. a)
  103. case "$(( ($channel / 4) % 2 ))" in
  104. 1) ht_capab="[HT40+]";;
  105. 0) ht_capab="[HT40-]";;
  106. esac
  107. ;;
  108. *)
  109. case "$htmode" in
  110. HT40+) ht_capab="[HT40+]";;
  111. HT40-) ht_capab="[HT40-]";;
  112. *)
  113. if [ "$channel" -lt 7 ]; then
  114. ht_capab="[HT40+]"
  115. else
  116. ht_capab="[HT40-]"
  117. fi
  118. ;;
  119. esac
  120. ;;
  121. esac
  122. [ "$auto_channel" -gt 0 ] && ht_capab="[HT40+]"
  123. ;;
  124. *) ieee80211n= ;;
  125. esac
  126. [ -n "$ieee80211n" ] && {
  127. append base_cfg "ieee80211n=1" "$N"
  128. set_default ht_coex 0
  129. append base_cfg "ht_coex=$ht_coex" "$N"
  130. json_get_vars \
  131. ldpc:1 \
  132. greenfield:0 \
  133. short_gi_20:1 \
  134. short_gi_40:1 \
  135. tx_stbc:1 \
  136. rx_stbc:3 \
  137. max_amsdu:1 \
  138. dsss_cck_40:1
  139. ht_cap_mask=0
  140. for cap in $(iw phy "$phy" info | grep 'Capabilities:' | cut -d: -f2); do
  141. ht_cap_mask="$(($ht_cap_mask | $cap))"
  142. done
  143. cap_rx_stbc=$((($ht_cap_mask >> 8) & 3))
  144. [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
  145. ht_cap_mask="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
  146. mac80211_add_capabilities ht_capab_flags $ht_cap_mask \
  147. LDPC:0x1::$ldpc \
  148. GF:0x10::$greenfield \
  149. SHORT-GI-20:0x20::$short_gi_20 \
  150. SHORT-GI-40:0x40::$short_gi_40 \
  151. TX-STBC:0x80::$tx_stbc \
  152. RX-STBC1:0x300:0x100:1 \
  153. RX-STBC12:0x300:0x200:1 \
  154. RX-STBC123:0x300:0x300:1 \
  155. MAX-AMSDU-7935:0x800::$max_amsdu \
  156. DSSS_CCK-40:0x1000::$dsss_cck_40
  157. ht_capab="$ht_capab$ht_capab_flags"
  158. [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
  159. }
  160. # 802.11ac
  161. enable_ac=0
  162. idx="$channel"
  163. case "$htmode" in
  164. VHT20) enable_ac=1;;
  165. VHT40)
  166. case "$(( ($channel / 4) % 2 ))" in
  167. 1) idx=$(($channel + 2));;
  168. 0) idx=$(($channel - 2));;
  169. esac
  170. enable_ac=1
  171. append base_cfg "vht_oper_chwidth=0" "$N"
  172. append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
  173. ;;
  174. VHT80)
  175. case "$(( ($channel / 4) % 4 ))" in
  176. 1) idx=$(($channel + 6));;
  177. 2) idx=$(($channel + 2));;
  178. 3) idx=$(($channel - 2));;
  179. 0) idx=$(($channel - 6));;
  180. esac
  181. enable_ac=1
  182. append base_cfg "vht_oper_chwidth=1" "$N"
  183. append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
  184. ;;
  185. VHT160)
  186. case "$channel" in
  187. 36|40|44|48|52|56|60|64) idx=50;;
  188. 100|104|108|112|116|120|124|128) idx=114;;
  189. esac
  190. enable_ac=1
  191. append base_cfg "vht_oper_chwidth=2" "$N"
  192. append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
  193. ;;
  194. esac
  195. if [ "$enable_ac" != "0" ]; then
  196. json_get_vars \
  197. rxldpc:1 \
  198. short_gi_80:1 \
  199. short_gi_160:1 \
  200. tx_stbc_2by1:1 \
  201. su_beamformer:1 \
  202. su_beamformee:1 \
  203. mu_beamformer:1 \
  204. mu_beamformee:1 \
  205. vht_txop_ps:1 \
  206. htc_vht:1 \
  207. rx_antenna_pattern:1 \
  208. tx_antenna_pattern:1 \
  209. vht_max_a_mpdu_len_exp:7 \
  210. vht_max_mpdu:11454 \
  211. rx_stbc:4 \
  212. vht_link_adapt:3 \
  213. vht160:2
  214. set_default tx_burst 2.0
  215. append base_cfg "ieee80211ac=1" "$N"
  216. vht_cap=0
  217. for cap in $(iw phy "$phy" info | awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
  218. vht_cap="$(($vht_cap | $cap))"
  219. done
  220. cap_rx_stbc=$((($vht_cap >> 8) & 7))
  221. [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
  222. vht_cap="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
  223. mac80211_add_capabilities vht_capab $vht_cap \
  224. RXLDPC:0x10::$rxldpc \
  225. SHORT-GI-80:0x20::$short_gi_80 \
  226. SHORT-GI-160:0x40::$short_gi_160 \
  227. TX-STBC-2BY1:0x80::$tx_stbc_2by1 \
  228. SU-BEAMFORMER:0x800::$su_beamformer \
  229. SU-BEAMFORMEE:0x1000::$su_beamformee \
  230. MU-BEAMFORMER:0x80000::$mu_beamformer \
  231. MU-BEAMFORMEE:0x100000::$mu_beamformee \
  232. VHT-TXOP-PS:0x200000::$vht_txop_ps \
  233. HTC-VHT:0x400000::$htc_vht \
  234. RX-ANTENNA-PATTERN:0x10000000::$rx_antenna_pattern \
  235. TX-ANTENNA-PATTERN:0x20000000::$tx_antenna_pattern \
  236. RX-STBC-1:0x700:0x100:1 \
  237. RX-STBC-12:0x700:0x200:1 \
  238. RX-STBC-123:0x700:0x300:1 \
  239. RX-STBC-1234:0x700:0x400:1 \
  240. # supported Channel widths
  241. vht160_hw=0
  242. [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
  243. vht160_hw=1
  244. [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
  245. vht160_hw=2
  246. [ "$vht160_hw" = 1 ] && vht_capab="$vht_capab[VHT160]"
  247. [ "$vht160_hw" = 2 ] && vht_capab="$vht_capab[VHT160-80PLUS80]"
  248. # maximum MPDU length
  249. vht_max_mpdu_hw=3895
  250. [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
  251. vht_max_mpdu_hw=7991
  252. [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
  253. vht_max_mpdu_hw=11454
  254. [ "$vht_max_mpdu_hw" != 3895 ] && \
  255. vht_capab="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
  256. # maximum A-MPDU length exponent
  257. vht_max_a_mpdu_len_exp_hw=0
  258. [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
  259. vht_max_a_mpdu_len_exp_hw=1
  260. [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
  261. vht_max_a_mpdu_len_exp_hw=2
  262. [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
  263. vht_max_a_mpdu_len_exp_hw=3
  264. [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
  265. vht_max_a_mpdu_len_exp_hw=4
  266. [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
  267. vht_max_a_mpdu_len_exp_hw=5
  268. [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
  269. vht_max_a_mpdu_len_exp_hw=6
  270. [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
  271. vht_max_a_mpdu_len_exp_hw=7
  272. vht_capab="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
  273. # whether or not the STA supports link adaptation using VHT variant
  274. vht_link_adapt_hw=0
  275. [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
  276. vht_link_adapt_hw=2
  277. [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
  278. vht_link_adapt_hw=3
  279. [ "$vht_link_adapt_hw" != 0 ] && \
  280. vht_capab="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
  281. [ -n "$vht_capab" ] && append base_cfg "vht_capab=$vht_capab" "$N"
  282. fi
  283. hostapd_prepare_device_config "$hostapd_conf_file" nl80211
  284. cat >> "$hostapd_conf_file" <<EOF
  285. ${channel:+channel=$channel}
  286. ${channel_list:+chanlist=$channel_list}
  287. ${hostapd_noscan:+noscan=1}
  288. ${tx_burst:+tx_queue_data2_burst=$tx_burst}
  289. $base_cfg
  290. EOF
  291. json_select ..
  292. radio_md5sum=$(md5sum $hostapd_conf_file | cut -d" " -f1)
  293. echo "radio_config_id=${radio_md5sum}" >> $hostapd_conf_file
  294. }
  295. mac80211_hostapd_setup_bss() {
  296. local phy="$1"
  297. local ifname="$2"
  298. local macaddr="$3"
  299. local type="$4"
  300. hostapd_cfg=
  301. append hostapd_cfg "$type=$ifname" "$N"
  302. hostapd_set_bss_options hostapd_cfg "$vif" || return 1
  303. json_get_vars wds wds_bridge dtim_period max_listen_int start_disabled
  304. set_default wds 0
  305. set_default start_disabled 0
  306. [ "$wds" -gt 0 ] && {
  307. append hostapd_cfg "wds_sta=1" "$N"
  308. [ -n "$wds_bridge" ] && append hostapd_cfg "wds_bridge=$wds_bridge" "$N"
  309. }
  310. [ "$staidx" -gt 0 -o "$start_disabled" -eq 1 ] && append hostapd_cfg "start_disabled=1" "$N"
  311. cat >> /var/run/hostapd-$phy.conf <<EOF
  312. $hostapd_cfg
  313. bssid=$macaddr
  314. ${dtim_period:+dtim_period=$dtim_period}
  315. ${max_listen_int:+max_listen_interval=$max_listen_int}
  316. EOF
  317. }
  318. mac80211_get_addr() {
  319. local phy="$1"
  320. local idx="$(($2 + 1))"
  321. head -n $(($macidx + 1)) /sys/class/ieee80211/${phy}/addresses | tail -n1
  322. }
  323. mac80211_generate_mac() {
  324. local phy="$1"
  325. local id="${macidx:-0}"
  326. local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
  327. local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
  328. [ "$mask" = "00:00:00:00:00:00" ] && {
  329. mask="ff:ff:ff:ff:ff:ff";
  330. [ "$(wc -l < /sys/class/ieee80211/${phy}/addresses)" -gt 1 ] && {
  331. addr="$(mac80211_get_addr "$phy" "$id")"
  332. [ -n "$addr" ] && {
  333. echo "$addr"
  334. return
  335. }
  336. }
  337. }
  338. local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
  339. local mask1=$1
  340. local mask6=$6
  341. local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
  342. macidx=$(($id + 1))
  343. [ "$((0x$mask1))" -gt 0 ] && {
  344. b1="0x$1"
  345. [ "$id" -gt 0 ] && \
  346. b1=$(($b1 ^ ((($id - !($b1 & 2)) << 2) | 0x2)))
  347. printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
  348. return
  349. }
  350. [ "$((0x$mask6))" -lt 255 ] && {
  351. printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
  352. return
  353. }
  354. off2=$(( (0x$6 + $id) / 0x100 ))
  355. printf "%s:%s:%s:%s:%02x:%02x" \
  356. $1 $2 $3 $4 \
  357. $(( (0x$5 + $off2) % 0x100 )) \
  358. $(( (0x$6 + $id) % 0x100 ))
  359. }
  360. find_phy() {
  361. [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
  362. [ -n "$path" ] && {
  363. phy="$(mac80211_path_to_phy "$path")"
  364. [ -n "$phy" ] && return 0
  365. }
  366. [ -n "$macaddr" ] && {
  367. for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
  368. grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
  369. done
  370. }
  371. return 1
  372. }
  373. mac80211_check_ap() {
  374. has_ap=1
  375. }
  376. mac80211_iw_interface_add() {
  377. local phy="$1"
  378. local ifname="$2"
  379. local type="$3"
  380. local wdsflag="$4"
  381. local rc
  382. iw phy "$phy" interface add "$ifname" type "$type" $wdsflag
  383. rc="$?"
  384. [ "$rc" = 233 ] && {
  385. # Device might have just been deleted, give the kernel some time to finish cleaning it up
  386. sleep 1
  387. iw phy "$phy" interface add "$ifname" type "$type" $wdsflag
  388. rc="$?"
  389. }
  390. [ "$rc" = 233 ] && {
  391. # Keep matching pre-existing interface
  392. [ -d "/sys/class/ieee80211/${phy}/device/net/${ifname}" ] && \
  393. case "$(iw dev wlan0 info | grep "^\ttype" | cut -d' ' -f2- 2>/dev/null)" in
  394. "AP")
  395. [ "$type" = "__ap" ] && rc=0
  396. ;;
  397. "IBSS")
  398. [ "$type" = "adhoc" ] && rc=0
  399. ;;
  400. "managed")
  401. [ "$type" = "managed" ] && rc=0
  402. ;;
  403. "mesh point")
  404. [ "$type" = "mp" ] && rc=0
  405. ;;
  406. "monitor")
  407. [ "$type" = "monitor" ] && rc=0
  408. ;;
  409. esac
  410. }
  411. [ "$rc" = 233 ] && {
  412. iw dev "$ifname" del
  413. sleep 1
  414. iw phy "$phy" interface add "$ifname" type "$type" $wdsflag
  415. rc="$?"
  416. }
  417. [ "$rc" = 233 ] && {
  418. # Device might not support virtual interfaces, so the interface never got deleted in the first place.
  419. # Check if the interface already exists, and avoid failing in this case.
  420. ip link show dev "$ifname" >/dev/null 2>/dev/null && rc=0
  421. }
  422. [ "$rc" != 0 ] && wireless_setup_failed INTERFACE_CREATION_FAILED
  423. return $rc
  424. }
  425. mac80211_prepare_vif() {
  426. json_select config
  427. json_get_vars ifname mode ssid wds powersave macaddr enable
  428. [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
  429. if_idx=$((${if_idx:-0} + 1))
  430. set_default wds 0
  431. set_default powersave 0
  432. json_select ..
  433. [ -n "$macaddr" ] || {
  434. macaddr="$(mac80211_generate_mac $phy)"
  435. macidx="$(($macidx + 1))"
  436. }
  437. json_add_object data
  438. json_add_string ifname "$ifname"
  439. json_close_object
  440. json_select config
  441. # It is far easier to delete and create the desired interface
  442. case "$mode" in
  443. adhoc)
  444. mac80211_iw_interface_add "$phy" "$ifname" adhoc || return
  445. ;;
  446. ap)
  447. # Hostapd will handle recreating the interface and
  448. # subsequent virtual APs belonging to the same PHY
  449. if [ -n "$hostapd_ctrl" ]; then
  450. type=bss
  451. else
  452. type=interface
  453. fi
  454. mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
  455. NEWAPLIST="${NEWAPLIST}$ifname "
  456. [ -n "$hostapd_ctrl" ] || {
  457. ap_ifname="${ifname}"
  458. hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
  459. }
  460. ;;
  461. mesh)
  462. mac80211_iw_interface_add "$phy" "$ifname" mp || return
  463. ;;
  464. monitor)
  465. mac80211_iw_interface_add "$phy" "$ifname" monitor || return
  466. ;;
  467. sta)
  468. local wdsflag=
  469. [ "$enable" = 0 ] || staidx="$(($staidx + 1))"
  470. [ "$wds" -gt 0 ] && wdsflag="4addr on"
  471. mac80211_iw_interface_add "$phy" "$ifname" managed "$wdsflag" || return
  472. if [ "$wds" -gt 0 ]; then
  473. iw "$ifname" set 4addr on
  474. else
  475. iw "$ifname" set 4addr off
  476. fi
  477. [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
  478. iw "$ifname" set power_save "$powersave"
  479. ;;
  480. esac
  481. case "$mode" in
  482. monitor|mesh)
  483. [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $iw_htmode
  484. ;;
  485. esac
  486. if [ "$mode" != "ap" ]; then
  487. # ALL ap functionality will be passed to hostapd
  488. # All interfaces must have unique mac addresses
  489. # which can either be explicitly set in the device
  490. # section, or automatically generated
  491. ip link set dev "$ifname" address "$macaddr"
  492. fi
  493. json_select ..
  494. }
  495. mac80211_setup_supplicant() {
  496. local enable=$1
  497. local add_sp=0
  498. local spobj="$(ubus -S list | grep wpa_supplicant.${ifname})"
  499. wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
  500. wpa_supplicant_prepare_interface "$ifname" nl80211 || {
  501. iw dev "$ifname" del
  502. return 1
  503. }
  504. if [ "$mode" = "sta" ]; then
  505. wpa_supplicant_add_network "$ifname"
  506. else
  507. wpa_supplicant_add_network "$ifname" "$freq" "$htmode" "$noscan"
  508. fi
  509. NEWSPLIST="${NEWSPLIST}$ifname "
  510. if [ "${NEWAPLIST%% *}" != "${OLDAPLIST%% *}" ]; then
  511. [ "$spobj" ] && ubus call wpa_supplicant.${phy} config_del "{\"iface\":\"$ifname\"}"
  512. add_sp=1
  513. fi
  514. [ "$enable" = 0 ] && {
  515. ubus call wpa_supplicant.${phy} config_del "{\"iface\":\"$ifname\"}"
  516. ip link set dev "$ifname" down
  517. return 0
  518. }
  519. [ -z "$spobj" ] && add_sp=1
  520. if [ "$add_sp" = "1" ]; then
  521. wpa_supplicant_run "$ifname" "$hostapd_ctrl"
  522. else
  523. ubus call $spobj reload
  524. fi
  525. }
  526. mac80211_setup_supplicant_noctl() {
  527. local enable=$1
  528. local spobj="$(ubus -S list | grep wpa_supplicant.${ifname})"
  529. wpa_supplicant_prepare_interface "$ifname" nl80211 || {
  530. iw dev "$ifname" del
  531. return 1
  532. }
  533. wpa_supplicant_add_network "$ifname" "$freq" "$htmode" "$noscan"
  534. NEWSPLIST="${NEWSPLIST}$ifname "
  535. [ "$enable" = 0 ] && {
  536. ubus call wpa_supplicant.${phy} config_del "{\"iface\":\"$ifname\"}"
  537. ip link set dev "$ifname" down
  538. return 0
  539. }
  540. if [ -z "$spobj" ]; then
  541. wpa_supplicant_run "$ifname"
  542. else
  543. ubus call $spobj reload
  544. fi
  545. }
  546. mac80211_prepare_iw_htmode() {
  547. case "$htmode" in
  548. VHT20|HT20) iw_htmode=HT20;;
  549. HT40*|VHT40|VHT160)
  550. case "$hwmode" in
  551. a)
  552. case "$(( ($channel / 4) % 2 ))" in
  553. 1) iw_htmode="HT40+" ;;
  554. 0) iw_htmode="HT40-";;
  555. esac
  556. ;;
  557. *)
  558. case "$htmode" in
  559. HT40+) iw_htmode="HT40+";;
  560. HT40-) iw_htmode="HT40-";;
  561. *)
  562. if [ "$channel" -lt 7 ]; then
  563. iw_htmode="HT40+"
  564. else
  565. iw_htmode="HT40-"
  566. fi
  567. ;;
  568. esac
  569. ;;
  570. esac
  571. [ "$auto_channel" -gt 0 ] && iw_htmode="HT40+"
  572. ;;
  573. VHT80)
  574. iw_htmode="80MHZ"
  575. ;;
  576. NONE|NOHT)
  577. iw_htmode="NOHT"
  578. ;;
  579. *) iw_htmode="" ;;
  580. esac
  581. }
  582. mac80211_setup_adhoc() {
  583. local enable=$1
  584. json_get_vars bssid ssid key mcast_rate
  585. NEWUMLIST="${NEWUMLIST}$ifname "
  586. [ "$enable" = 0 ] && {
  587. ip link set dev "$ifname" down
  588. return 0
  589. }
  590. keyspec=
  591. [ "$auth_type" = "wep" ] && {
  592. set_default key 1
  593. case "$key" in
  594. [1234])
  595. local idx
  596. for idx in 1 2 3 4; do
  597. json_get_var ikey "key$idx"
  598. [ -n "$ikey" ] && {
  599. ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
  600. [ $idx -eq $key ] && ikey="d:$ikey"
  601. append keyspec "$ikey"
  602. }
  603. done
  604. ;;
  605. *)
  606. append keyspec "d:0:$(prepare_key_wep "$key")"
  607. ;;
  608. esac
  609. }
  610. brstr=
  611. for br in $basic_rate_list; do
  612. wpa_supplicant_add_rate brstr "$br"
  613. done
  614. mcval=
  615. [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval "$mcast_rate"
  616. iw dev "$ifname" ibss join "$ssid" $freq $iw_htmode fixed-freq $bssid \
  617. beacon-interval $beacon_int \
  618. ${brstr:+basic-rates $brstr} \
  619. ${mcval:+mcast-rate $mcval} \
  620. ${keyspec:+keys $keyspec}
  621. }
  622. mac80211_setup_mesh() {
  623. local enable=$1
  624. json_get_vars ssid mesh_id mcast_rate
  625. NEWUMLIST="${NEWUMLIST}$ifname "
  626. [ "$enable" = 0 ] && {
  627. ip link set dev "$ifname" down
  628. return 0
  629. }
  630. mcval=
  631. [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval "$mcast_rate"
  632. [ -n "$mesh_id" ] && ssid="$mesh_id"
  633. iw dev "$ifname" mesh join "$ssid" freq $freq $iw_htmode \
  634. ${mcval:+mcast-rate $mcval} \
  635. beacon-interval $beacon_int
  636. }
  637. mac80211_setup_vif() {
  638. local name="$1"
  639. local failed
  640. local action=up
  641. json_select data
  642. json_get_vars ifname
  643. json_select ..
  644. json_select config
  645. json_get_vars mode
  646. json_get_var vif_txpower
  647. json_get_var vif_enable enable 1
  648. [ "$vif_enable" = 1 ] || action=down
  649. if [ "$mode" != "ap" ] || [ "$ifname" = "$ap_ifname" ]; then
  650. logger ip link set dev "$ifname" $action
  651. ip link set dev "$ifname" "$action" || {
  652. wireless_setup_vif_failed IFUP_ERROR
  653. json_select ..
  654. return
  655. }
  656. [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
  657. fi
  658. case "$mode" in
  659. mesh)
  660. wireless_vif_parse_encryption
  661. freq="$(get_freq "$phy" "$channel")"
  662. if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ] || chan_is_dfs "$phy" "$channel"; then
  663. mac80211_setup_supplicant $vif_enable || failed=1
  664. else
  665. mac80211_setup_mesh $vif_enable
  666. fi
  667. for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
  668. json_get_var mp_val "$var"
  669. [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
  670. done
  671. ;;
  672. adhoc)
  673. wireless_vif_parse_encryption
  674. if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
  675. freq="$(get_freq "$phy" "$channel")"
  676. mac80211_setup_supplicant_noctl $vif_enable || failed=1
  677. else
  678. mac80211_setup_adhoc $vif_enable
  679. fi
  680. ;;
  681. sta)
  682. mac80211_setup_supplicant $vif_enable || failed=1
  683. ;;
  684. esac
  685. json_select ..
  686. [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
  687. }
  688. get_freq() {
  689. local phy="$1"
  690. local chan="$2"
  691. iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
  692. }
  693. chan_is_dfs() {
  694. local phy="$1"
  695. local chan="$2"
  696. iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep -q "MHz.*radar detection"
  697. return $!
  698. }
  699. mac80211_vap_cleanup() {
  700. local service="$1"
  701. local vaps="$2"
  702. for wdev in $vaps; do
  703. [ "$service" != "none" ] && ubus call ${service}.${phy} config_remove "{\"iface\":\"$wdev\"}"
  704. ip link set dev "$wdev" down 2>/dev/null
  705. iw dev "$wdev" del
  706. done
  707. }
  708. mac80211_interface_cleanup() {
  709. local phy="$1"
  710. local primary_ap=$(uci -q -P /var/state get wireless._${phy}.aplist)
  711. primary_ap=${primary_ap%% *}
  712. mac80211_vap_cleanup hostapd "${primary_ap}"
  713. mac80211_vap_cleanup wpa_supplicant "$(uci -q -P /var/state get wireless._${phy}.splist)"
  714. mac80211_vap_cleanup none "$(uci -q -P /var/state get wireless._${phy}.umlist)"
  715. }
  716. mac80211_set_noscan() {
  717. hostapd_noscan=1
  718. }
  719. drv_mac80211_cleanup() {
  720. hostapd_common_cleanup
  721. }
  722. drv_mac80211_setup() {
  723. json_select config
  724. json_get_vars \
  725. phy macaddr path \
  726. country chanbw distance \
  727. txpower antenna_gain \
  728. rxantenna txantenna \
  729. frag rts beacon_int:100 htmode
  730. json_get_values basic_rate_list basic_rate
  731. json_select ..
  732. find_phy || {
  733. echo "Could not find PHY for device '$1'"
  734. wireless_set_retry 0
  735. return 1
  736. }
  737. [ -z "$(uci -q -P /var/state show wireless._${phy})" ] && {
  738. uci -q -P /var/state set wireless._${phy}=phy
  739. wireless_set_data phy="$phy"
  740. }
  741. OLDAPLIST=$(uci -q -P /var/state get wireless._${phy}.aplist)
  742. OLDSPLIST=$(uci -q -P /var/state get wireless._${phy}.splist)
  743. OLDUMLIST=$(uci -q -P /var/state get wireless._${phy}.umlist)
  744. local wdev
  745. local cwdev
  746. local found
  747. for wdev in $(list_phy_interfaces "$phy"); do
  748. found=0
  749. for cwdev in $OLDAPLIST $OLDSPLIST $OLDUMLIST; do
  750. if [ "$wdev" = "$cwdev" ]; then
  751. found=1
  752. break
  753. fi
  754. done
  755. if [ "$found" = "0" ]; then
  756. ip link set dev "$wdev" down
  757. iw dev "$wdev" del
  758. fi
  759. done
  760. # convert channel to frequency
  761. [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
  762. [ -n "$country" ] && {
  763. iw reg get | grep -q "^country $country:" || {
  764. iw reg set "$country"
  765. sleep 1
  766. }
  767. }
  768. hostapd_conf_file="/var/run/hostapd-$phy.conf"
  769. no_ap=1
  770. macidx=0
  771. staidx=0
  772. [ -n "$chanbw" ] && {
  773. for file in /sys/kernel/debug/ieee80211/$phy/ath9k*/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
  774. [ -f "$file" ] && echo "$chanbw" > "$file"
  775. done
  776. }
  777. set_default rxantenna 0xffffffff
  778. set_default txantenna 0xffffffff
  779. set_default distance 0
  780. set_default antenna_gain 0
  781. [ "$txantenna" = "all" ] && txantenna=0xffffffff
  782. [ "$rxantenna" = "all" ] && rxantenna=0xffffffff
  783. iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
  784. iw phy "$phy" set antenna_gain $antenna_gain
  785. iw phy "$phy" set distance "$distance"
  786. if [ -n "$txpower" ]; then
  787. iw phy "$phy" set txpower fixed "${txpower%%.*}00"
  788. else
  789. iw phy "$phy" set txpower auto
  790. fi
  791. [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
  792. [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
  793. has_ap=
  794. hostapd_ctrl=
  795. ap_ifname=
  796. hostapd_noscan=
  797. for_each_interface "ap" mac80211_check_ap
  798. rm -f "$hostapd_conf_file"
  799. for_each_interface "sta adhoc mesh" mac80211_set_noscan
  800. [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
  801. mac80211_prepare_iw_htmode
  802. for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
  803. NEWAPLIST=
  804. for_each_interface "ap" mac80211_prepare_vif
  805. NEW_MD5=$(test -e "${hostapd_conf_file}" && md5sum ${hostapd_conf_file})
  806. OLD_MD5=$(uci -q -P /var/state get wireless._${phy}.md5)
  807. if [ "${NEWAPLIST}" != "${OLDAPLIST}" ]; then
  808. mac80211_vap_cleanup hostapd "${OLDAPLIST}"
  809. [ -n "${NEWAPLIST}" ] && mac80211_iw_interface_add "$phy" "${NEWAPLIST%% *}" __ap || return
  810. fi
  811. local add_ap=0
  812. local primary_ap=${NEWAPLIST%% *}
  813. [ -n "$hostapd_ctrl" ] && {
  814. local no_reload=1
  815. if [ -n "$(ubus list | grep hostapd.$primary_ap)" ]; then
  816. [ "${NEW_MD5}" = "${OLD_MD5}" ] || {
  817. ubus call hostapd.$primary_ap reload
  818. no_reload=$?
  819. mac80211_vap_cleanup hostapd "${OLDAPLIST}"
  820. [ -n "${NEWAPLIST}" ] && mac80211_iw_interface_add "$phy" "${NEWAPLIST%% *}" __ap || return
  821. }
  822. fi
  823. if [ "$no_reload" != "0" ]; then
  824. add_ap=1
  825. ubus wait_for hostapd.$phy
  826. ubus call hostapd.${phy} config_add "{\"iface\":\"$primary_ap\", \"config\":\"${hostapd_conf_file}\"}"
  827. local hostapd_pid=$(ubus call service list '{"name": "hostapd"}' | jsonfilter -l 1 -e "@['hostapd'].instances['hostapd-${phy}'].pid")
  828. wireless_add_process "$hostapd_pid" "/usr/sbin/hostapd" 1
  829. fi
  830. ret="$?"
  831. [ "$ret" != 0 ] && {
  832. wireless_setup_failed HOSTAPD_START_FAILED
  833. return
  834. }
  835. }
  836. uci -q -P /var/state set wireless._${phy}.aplist="${NEWAPLIST}"
  837. uci -q -P /var/state set wireless._${phy}.md5="${NEW_MD5}"
  838. [ "${add_ap}" = 1 ] && sleep 1
  839. for_each_interface "ap" mac80211_setup_vif
  840. NEWSPLIST=
  841. NEWUMLIST=
  842. for_each_interface "sta adhoc mesh monitor" mac80211_setup_vif
  843. uci -q -P /var/state set wireless._${phy}.splist="${NEWSPLIST}"
  844. uci -q -P /var/state set wireless._${phy}.umlist="${NEWUMLIST}"
  845. local foundvap
  846. local dropvap=""
  847. for oldvap in $OLDSPLIST; do
  848. foundvap=0
  849. for newvap in $NEWSPLIST; do
  850. [ "$oldvap" = "$newvap" ] && foundvap=1
  851. done
  852. [ "$foundvap" = "0" ] && dropvap="$dropvap $oldvap"
  853. done
  854. [ -n "$dropvap" ] && mac80211_vap_cleanup wpa_supplicant "$dropvap"
  855. wireless_set_up
  856. }
  857. _list_phy_interfaces() {
  858. local phy="$1"
  859. if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
  860. ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
  861. else
  862. ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
  863. fi
  864. }
  865. list_phy_interfaces() {
  866. local phy="$1"
  867. for dev in $(_list_phy_interfaces "$phy"); do
  868. readlink "/sys/class/net/${dev}/phy80211" | grep -q "/${phy}\$" || continue
  869. echo "$dev"
  870. done
  871. }
  872. drv_mac80211_teardown() {
  873. wireless_process_kill_all
  874. json_select data
  875. json_get_vars phy
  876. json_select ..
  877. mac80211_interface_cleanup "$phy"
  878. uci -q -P /var/state revert wireless._${phy}
  879. }
  880. add_driver mac80211