2 . /lib/netifd/netifd-wireless.sh
3 . /lib/netifd/hostapd.sh
5 init_wireless_driver "$@"
7 MP_CONFIG_INT="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
8 mesh_max_retries mesh_ttl mesh_element_ttl mesh_hwmp_max_preq_retries
9 mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
10 mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
11 mesh_hwmp_rann_interval mesh_gate_announcements mesh_sync_offset_max_neighor
12 mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
13 mesh_hwmp_confirmation_interval mesh_awake_window mesh_plink_timeout"
14 MP_CONFIG_BOOL="mesh_auto_open_plinks mesh_fwding"
15 MP_CONFIG_STRING="mesh_power_mode"
17 drv_mac80211_init_device_config() {
18 hostapd_common_add_device_config
20 config_add_string path phy macaddr
21 config_add_string hwmode
22 config_add_int beacon_int chanbw frag rts
23 config_add_int rxantenna txantenna antenna_gain txpower
24 config_add_boolean noscan
25 config_add_array ht_capab
28 drv_mac80211_init_iface_config() {
29 hostapd_common_add_bss_config
31 config_add_string macaddr ifname
33 config_add_boolean wds powersave
34 config_add_int maxassoc
35 config_add_int max_listen_int
36 config_add_int dtim_interval
39 config_add_int $MP_CONFIG_INT
40 config_add_boolean $MP_CONFIG_BOOL
41 config_add_string $MP_CONFIG_STRING
44 mac80211_hostapd_setup_base() {
49 [ "$auto_channel" -gt 0 ] && channel=acs_survey
51 [ "$enable_ht" -gt 0 ] && {
52 json_get_vars noscan htmode
53 json_get_values ht_capab_list ht_capab
55 append base_cfg "ieee80211n=1" "$N"
58 [ -n "$htmode" ] && ht_capab="[$htmode]"
59 for cap in $ht_capab_list; do
60 ht_capab="$ht_capab[$cap]"
63 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
66 hostapd_prepare_device_config "$hostapd_conf_file" nl80211
67 cat >> "$hostapd_conf_file" <<EOF
68 ${channel:+channel=$channel}
69 ${noscan:+noscan=$noscan}
76 mac80211_hostapd_setup_bss() {
83 append hostapd_cfg "$type=$ifname" "$N"
85 hostapd_set_bss_options hostapd_cfg "$vif" || return 1
86 json_get_vars wds dtim_period max_listen_int
88 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
89 [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=1" "$N"
91 cat >> /var/run/hostapd-$phy.conf <<EOF
94 ${dtim_period:+dtim_period=$dtim_period}
95 ${max_listen_int:+max_listen_interval=$max_listen_int}
99 mac80211_generate_mac() {
101 local id="${macidx:-0}"
103 local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
104 local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
106 [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
107 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
112 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
115 [ "$((0x$mask1))" -gt 0 ] && {
118 b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
119 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
123 [ "$((0x$mask6))" -lt 255 ] && {
124 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
128 off2=$(( (0x$6 + $id) / 0x100 ))
129 printf "%s:%s:%s:%s:%02x:%02x" \
131 $(( (0x$5 + $off2) % 0x100 )) \
132 $(( (0x$6 + $id) % 0x100 ))
136 [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
137 [ -n "$path" -a -d "/sys/devices/$path/ieee80211" ] && {
138 phy="$(ls /sys/devices/$path/ieee80211 | grep -m 1 phy)"
139 [ -n "$phy" ] && return 0
141 [ -n "$macaddr" ] && {
142 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
143 grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
149 mac80211_check_ap() {
153 mac80211_prepare_vif() {
156 json_get_vars ifname mode ssid wds powersave macaddr
158 [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
159 if_idx=$((${if_idx:-0} + 1))
162 set_default powersave 0
166 [ -n "$macaddr" ] || {
167 macaddr="$(mac80211_generate_mac $phy)"
168 macidx="$(($macidx + 1))"
172 json_add_string ifname "$ifname"
176 # It is far easier to delete and create the desired interface
179 iw phy "$phy" interface add "$ifname" type adhoc
182 # Hostapd will handle recreating the interface and
183 # subsequent virtual APs belonging to the same PHY
184 if [ -n "$hostapd_ctrl" ]; then
190 mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
192 [ -n "$hostapd_ctrl" ] || {
193 iw phy "$phy" interface add "$ifname" type managed
194 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
198 json_get_vars key mesh_id
199 if [ -n "$key" ]; then
200 iw phy "$phy" interface add "$ifname" type mp
202 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
206 iw phy "$phy" interface add "$ifname" type monitor
210 staidx="$(($staidx + 1))"
211 [ "$wds" -gt 0 ] && wdsflag="4addr on"
212 iw phy "$phy" interface add "$ifname" type managed $wdsflag
213 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
214 iw "$ifname" set power_save "$powersave"
220 [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $htmode
224 if [ "$mode" != "ap" ]; then
225 # ALL ap functionality will be passed to hostapd
226 # All interfaces must have unique mac addresses
227 # which can either be explicitly set in the device
228 # section, or automatically generated
229 ifconfig "$ifname" hw ether "$macaddr"
235 mac80211_setup_supplicant() {
236 wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
237 wpa_supplicant_add_network "$ifname"
238 wpa_supplicant_run "$ifname" ${hostapd_ctrl:+-H $hostapd_ctrl}
241 mac80211_setup_adhoc() {
242 json_get_vars bssid ssid basic_rate key
245 [ "$auth_type" == "wep" ] && {
250 for idx in 1 2 3 4; do
251 json_get_var ikey "key$idx"
254 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
255 [ $idx -eq $key ] && ikey="d:$ikey"
256 append keyspec "$ikey"
261 append keyspec "d:0:$(prepare_key_wep "$key")"
267 for br in $basic_rate; do
268 hostapd_add_rate brstr "$br"
272 [ -n "$mcast_rate" ] && hostapd_add_rate mcval "$mcast_rate"
274 iw dev "$ifname" ibss join "$ssid" $freq $htmode fixed-freq $bssid \
275 ${beacon_int:+beacon-interval $beacon_int} \
276 ${brstr:+basic-rates $brstr} \
277 ${mcval:+mcast-rate $mcval} \
278 ${keyspec:+keys $keyspec}
281 mac80211_setup_vif() {
290 json_get_var vif_txpower txpower
292 ifconfig "$ifname" up || {
293 wireless_setup_vif_failed IFUP_ERROR
298 set_default vif_txpower "$txpower"
299 [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
303 for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
304 json_get_var mp_val "$var"
305 [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
310 wireless_vif_parse_encryption
311 if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
312 mac80211_setup_supplicant || failed=1
318 mac80211_setup_supplicant || failed=1
323 [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
329 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
332 mac80211_interface_cleanup() {
335 for wdev in $(list_phy_interfaces "$phy"); do
336 ifconfig "$wdev" down 2>/dev/null
341 drv_mac80211_cleanup() {
342 hostapd_common_cleanup
345 drv_mac80211_setup() {
349 country chanbw distance \
350 txpower antenna_gain \
351 rxantenna txantenna \
356 echo "Could not find PHY for device '$1'"
361 wireless_set_data phy="$phy"
362 mac80211_interface_cleanup "$phy"
364 # convert channel to frequency
365 [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
367 [ -n "$country" ] && {
368 iw reg get | grep -q "^country $country:" || {
369 iw reg set "$country"
374 hostapd_conf_file="/var/run/hostapd-$phy.conf"
380 [ -n "$chanbw" ] && {
381 for file in /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
382 [ -f "$file" ] && echo "$chanbw" > "$file"
386 set_default rxantenna all
387 set_default txantenna all
388 set_default distance 0
389 set_default antenna_gain 0
391 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
392 iw phy "$phy" set antenna_gain $antenna_gain
393 iw phy "$phy" set distance "$distance"
395 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
396 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
400 for_each_interface "ap" mac80211_check_ap
402 rm -f "$hostapd_conf_file"
403 [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
405 for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
406 for_each_interface "ap" mac80211_prepare_vif
408 [ -n "$hostapd_ctrl" ] && {
409 /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
411 wireless_add_process "$(cat /var/run/wifi-$phy.pid)" "/usr/sbin/hostapd" 1
413 wireless_setup_failed HOSTAPD_START_FAILED
418 for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
423 list_phy_interfaces() {
425 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
426 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
428 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
432 drv_mac80211_teardown() {
433 wireless_process_kill_all
439 mac80211_interface_cleanup "$phy"