mac80211.sh 27 KB

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