mac80211: fix ad-hoc network setup with netifd (#14564)
[openwrt/svn-archive/archive.git] / package / kernel / mac80211 / files / lib / netifd / wireless / mac80211.sh
1 #!/bin/sh
2 . /lib/netifd/netifd-wireless.sh
3 . /lib/netifd/hostapd.sh
4
5 init_wireless_driver "$@"
6
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"
16
17 drv_mac80211_init_device_config() {
18 hostapd_common_add_device_config
19
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
26 }
27
28 drv_mac80211_init_iface_config() {
29 hostapd_common_add_bss_config
30
31 config_add_string macaddr
32
33 config_add_boolean wds powersave
34 config_add_int maxassoc
35 config_add_int max_listen_int
36 config_add_int dtim_interval
37
38 # mesh
39 config_add_int $MP_CONFIG_INT
40 config_add_boolean $MP_CONFIG_BOOL
41 config_add_string $MP_CONFIG_STRING
42 }
43
44 mac80211_hostapd_setup_base() {
45 local phy="$1"
46
47 json_select config
48
49 [ "$auto_channel" -gt 0 ] && channel=acs_survey
50
51 [ "$enable_ht" -gt 0 ] && {
52 json_get_vars noscan htmode
53 json_get_values ht_capab_list ht_capab
54
55 append base_cfg "ieee80211n=1" "$N"
56
57 ht_capab=
58 [ -n "$htmode" ] && ht_capab="[$htmode]"
59 for cap in $ht_capab_list; do
60 ht_capab="$ht_capab[$cap]"
61 done
62
63 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
64 }
65
66 hostapd_prepare_device_config "$hostapd_conf_file" nl80211
67 cat >> "$hostapd_conf_file" <<EOF
68 ${channel:+channel=$channel}
69 ${noscan:+noscan=$noscan}
70 $base_cfg
71
72 EOF
73 json_select ..
74 }
75
76 mac80211_hostapd_setup_bss() {
77 local phy="$1"
78 local ifname="$2"
79 local macaddr="$3"
80 local type="$4"
81
82 hostapd_cfg=
83 append hostapd_cfg "$type=$ifname" "$N"
84
85 hostapd_set_bss_options hostapd_cfg "$vif" || return 1
86 json_get_vars wds dtim_period max_listen_int
87
88 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
89 [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=1" "$N"
90
91 cat >> /var/run/hostapd-$phy.conf <<EOF
92 $hostapd_cfg
93 bssid=$macaddr
94 ${dtim_period:+dtim_period=$dtim_period}
95 ${max_listen_int:+max_listen_interval=$max_listen_int}
96 EOF
97 }
98
99 mac80211_generate_mac() {
100 local phy="$1"
101 local id="${macidx:-0}"
102
103 local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
104 local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
105
106 [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
107 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
108
109 local mask1=$1
110 local mask6=$6
111
112 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
113
114 macidx=$(($id + 1))
115 [ "$((0x$mask1))" -gt 0 ] && {
116 b1="0x$1"
117 [ "$id" -gt 0 ] && \
118 b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
119 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
120 return
121 }
122
123 [ "$((0x$mask6))" -lt 255 ] && {
124 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
125 return
126 }
127
128 off2=$(( (0x$6 + $id) / 0x100 ))
129 printf "%s:%s:%s:%s:%02x:%02x" \
130 $1 $2 $3 $4 \
131 $(( (0x$5 + $off2) % 0x100 )) \
132 $(( (0x$6 + $id) % 0x100 ))
133 }
134
135 find_phy() {
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
140 }
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
144 done
145 }
146 return 1
147 }
148
149 mac80211_check_ap() {
150 has_ap=1
151 }
152
153 mac80211_prepare_vif() {
154 json_select config
155
156 json_get_vars ifname mode ssid wds powersave macaddr
157
158 [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
159 if_idx=$((${if_idx:-0} + 1))
160
161 set_default wds 0
162 set_default powersave 0
163
164 json_select ..
165
166 [ -n "$macaddr" ] || {
167 macaddr="$(mac80211_generate_mac $phy)"
168 macidx="$(($macidx + 1))"
169 }
170
171 json_add_object data
172 json_add_string ifname "$ifname"
173 json_close_object
174 json_select config
175
176 # It is far easier to delete and create the desired interface
177 case "$mode" in
178 adhoc)
179 iw phy "$phy" interface add "$ifname" type adhoc
180 ;;
181 ap)
182 # Hostapd will handle recreating the interface and
183 # subsequent virtual APs belonging to the same PHY
184 if [ -n "$hostapd_ctrl" ]; then
185 type=bss
186 else
187 type=interface
188 fi
189
190 mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
191
192 [ -n "$hostapd_ctrl" ] || {
193 iw phy "$phy" interface add "$ifname" type managed
194 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
195 }
196 ;;
197 mesh)
198 json_get_vars key mesh_id
199 if [ -n "$key" ]; then
200 iw phy "$phy" interface add "$ifname" type mp
201 else
202 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
203 fi
204 ;;
205 monitor)
206 iw phy "$phy" interface add "$ifname" type monitor
207 ;;
208 sta)
209 local wdsflag=
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"
215 ;;
216 esac
217
218 case "$mode" in
219 monitor|mesh)
220 [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $htmode
221 ;;
222 esac
223
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"
230 fi
231
232 json_select ..
233 }
234
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}
239 }
240
241 mac80211_setup_adhoc() {
242 json_get_vars bssid ssid basic_rate key
243
244 keyspec=
245 [ "$auth_type" == "wep" ] && {
246 set_default key 1
247 case "$key" in
248 [1234])
249 local idx
250 for idx in 1 2 3 4; do
251 json_get_var ikey "key$idx"
252
253 [ -n "$ikey" ] && {
254 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
255 [ $idx -eq $key ] && ikey="d:$ikey"
256 append keyspec "$ikey"
257 }
258 done
259 ;;
260 *)
261 append keyspec "d:0:$(prepare_key_wep "$key")"
262 ;;
263 esac
264 }
265
266 brstr=
267 for br in $basic_rate; do
268 hostapd_add_rate brstr "$br"
269 done
270
271 mcval=
272 [ -n "$mcast_rate" ] && hostapd_add_rate mcval "$mcast_rate"
273
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}
279 }
280
281 mac80211_setup_vif() {
282 local name="$1"
283
284 json_select data
285 json_get_vars ifname
286 json_select ..
287
288 json_select config
289 json_get_vars mode
290 json_get_var vif_txpower txpower
291
292 ifconfig "$ifname" up || {
293 wireless_setup_vif_failed IFUP_ERROR
294 json_select ..
295 return
296 }
297
298 set_default vif_txpower "$txpower"
299 [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
300
301 case "$mode" in
302 mesh)
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"
306 done
307 # todo: authsae
308 ;;
309 adhoc)
310 wireless_vif_parse_encryption
311 if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
312 mac80211_setup_supplicant || failed=1
313 else
314 mac80211_setup_adhoc
315 fi
316 ;;
317 sta)
318 mac80211_setup_supplicant || failed=1
319 ;;
320 esac
321
322 json_select ..
323 [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
324 }
325
326 get_freq() {
327 local phy="$1"
328 local chan="$2"
329 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
330 }
331
332 mac80211_interface_cleanup() {
333 local phy="$1"
334
335 for wdev in $(list_phy_interfaces "$phy"); do
336 ifconfig "$wdev" down 2>/dev/null
337 iw dev "$wdev" del
338 done
339 }
340
341 drv_mac80211_cleanup() {
342 hostapd_common_cleanup
343 }
344
345 drv_mac80211_setup() {
346 json_select config
347 json_get_vars \
348 phy macaddr path \
349 country chanbw distance \
350 txpower antenna_gain \
351 rxantenna txantenna \
352 frag rts beacon_int
353 json_select ..
354
355 find_phy || {
356 echo "Could not find PHY for device '$1'"
357 wireless_set_retry 0
358 return 1
359 }
360
361 wireless_set_data phy="$phy"
362 mac80211_interface_cleanup "$phy"
363
364 # convert channel to frequency
365 [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
366
367 [ -n "$country" ] && {
368 iw reg get | grep -q "^country $country:" || {
369 iw reg set "$country"
370 sleep 1
371 }
372 }
373
374 hostapd_conf_file="/var/run/hostapd-$phy.conf"
375
376 no_ap=1
377 macidx=0
378 staidx=0
379
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"
383 done
384 }
385
386 set_default rxantenna all
387 set_default txantenna all
388 set_default distance 0
389 set_default antenna_gain 0
390
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"
394
395 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
396 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
397
398 has_ap=
399 hostapd_ctrl=
400 for_each_interface "ap" mac80211_check_ap
401
402 rm -f "$hostapd_conf_file"
403 [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
404
405 for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
406 for_each_interface "ap" mac80211_prepare_vif
407
408 [ -n "$hostapd_ctrl" ] && {
409 /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
410 ret="$?"
411 wireless_add_process "$(cat /var/run/wifi-$phy.pid)" "/usr/sbin/hostapd" 1
412 [ "$ret" != 0 ] && {
413 wireless_setup_failed HOSTAPD_START_FAILED
414 return
415 }
416 }
417
418 for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
419
420 wireless_set_up
421 }
422
423 list_phy_interfaces() {
424 local phy="$1"
425 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
426 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
427 else
428 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
429 fi
430 }
431
432 drv_mac80211_teardown() {
433 wireless_process_kill_all
434
435 json_select data
436 json_get_vars phy
437 json_select ..
438
439 mac80211_interface_cleanup "$phy"
440 }
441
442 add_driver mac80211