2f369d65def25ba049ced79dde36e572fccda797
[openwrt.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 distance
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 ifname
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         set_default wds 0
89
90         [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
91         [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=1" "$N"
92
93         cat >> /var/run/hostapd-$phy.conf <<EOF
94 $hostapd_cfg
95 bssid=$macaddr
96 ${dtim_period:+dtim_period=$dtim_period}
97 ${max_listen_int:+max_listen_interval=$max_listen_int}
98 EOF
99 }
100
101 mac80211_generate_mac() {
102         local phy="$1"
103         local id="${macidx:-0}"
104
105         local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
106         local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
107
108         [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
109         local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
110
111         local mask1=$1
112         local mask6=$6
113
114         local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
115
116         macidx=$(($id + 1))
117         [ "$((0x$mask1))" -gt 0 ] && {
118                 b1="0x$1"
119                 [ "$id" -gt 0 ] && \
120                         b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
121                 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
122                 return
123         }
124
125         [ "$((0x$mask6))" -lt 255 ] && {
126                 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
127                 return
128         }
129
130         off2=$(( (0x$6 + $id) / 0x100 ))
131         printf "%s:%s:%s:%s:%02x:%02x" \
132                 $1 $2 $3 $4 \
133                 $(( (0x$5 + $off2) % 0x100 )) \
134                 $(( (0x$6 + $id) % 0x100 ))
135 }
136
137 find_phy() {
138         [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
139         [ -n "$path" -a -d "/sys/devices/$path/ieee80211" ] && {
140                 phy="$(ls /sys/devices/$path/ieee80211 | grep -m 1 phy)"
141                 [ -n "$phy" ] && return 0
142         }
143         [ -n "$macaddr" ] && {
144                 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
145                         grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
146                 done
147         }
148         return 1
149 }
150
151 mac80211_check_ap() {
152         has_ap=1
153 }
154
155 mac80211_prepare_vif() {
156         json_select config
157
158         json_get_vars ifname mode ssid wds powersave macaddr
159
160         [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
161         if_idx=$((${if_idx:-0} + 1))
162
163         set_default wds 0
164         set_default powersave 0
165
166         json_select ..
167
168         [ -n "$macaddr" ] || {
169                 macaddr="$(mac80211_generate_mac $phy)"
170                 macidx="$(($macidx + 1))"
171         }
172
173         json_add_object data
174         json_add_string ifname "$ifname"
175         json_close_object
176         json_select config
177
178         # It is far easier to delete and create the desired interface
179         case "$mode" in
180                 adhoc)
181                         iw phy "$phy" interface add "$ifname" type adhoc
182                 ;;
183                 ap)
184                         # Hostapd will handle recreating the interface and
185                         # subsequent virtual APs belonging to the same PHY
186                         if [ -n "$hostapd_ctrl" ]; then
187                                 type=bss
188                         else
189                                 type=interface
190                         fi
191
192                         mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
193
194                         [ -n "$hostapd_ctrl" ] || {
195                                 iw phy "$phy" interface add "$ifname" type managed
196                                 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
197                         }
198                 ;;
199                 mesh)
200                         json_get_vars key mesh_id
201                         if [ -n "$key" ]; then
202                                 iw phy "$phy" interface add "$ifname" type mp
203                         else
204                                 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
205                         fi
206                 ;;
207                 monitor)
208                         iw phy "$phy" interface add "$ifname" type monitor
209                 ;;
210                 sta)
211                         local wdsflag=
212                         staidx="$(($staidx + 1))"
213                         [ "$wds" -gt 0 ] && wdsflag="4addr on"
214                         iw phy "$phy" interface add "$ifname" type managed $wdsflag
215                         [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
216                         iw "$ifname" set power_save "$powersave"
217                 ;;
218         esac
219
220         case "$mode" in
221                 monitor|mesh)
222                         [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $htmode
223                 ;;
224         esac
225
226         if [ "$mode" != "ap" ]; then
227                 # ALL ap functionality will be passed to hostapd
228                 # All interfaces must have unique mac addresses
229                 # which can either be explicitly set in the device
230                 # section, or automatically generated
231                 ifconfig "$ifname" hw ether "$macaddr"
232         fi
233
234         json_select ..
235 }
236
237 mac80211_setup_supplicant() {
238         wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
239         wpa_supplicant_add_network "$ifname"
240         wpa_supplicant_run "$ifname" ${hostapd_ctrl:+-H $hostapd_ctrl}
241 }
242
243 mac80211_setup_adhoc() {
244         json_get_vars bssid ssid basic_rate key mcast_rate
245
246         keyspec=
247         [ "$auth_type" == "wep" ] && {
248                 set_default key 1
249                 case "$key" in
250                         [1234])
251                                 local idx
252                                 for idx in 1 2 3 4; do
253                                         json_get_var ikey "key$idx"
254
255                                         [ -n "$ikey" ] && {
256                                                 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
257                                                 [ $idx -eq $key ] && ikey="d:$ikey"
258                                                 append keyspec "$ikey"
259                                         }
260                                 done
261                         ;;
262                         *)
263                                 append keyspec "d:0:$(prepare_key_wep "$key")"
264                         ;;
265                 esac
266         }
267
268         brstr=
269         for br in $basic_rate; do
270                 hostapd_add_rate brstr "$br"
271         done
272
273         mcval=
274         [ -n "$mcast_rate" ] && hostapd_add_rate mcval "$mcast_rate"
275
276         iw dev "$ifname" ibss join "$ssid" $freq $htmode fixed-freq $bssid \
277                 ${beacon_int:+beacon-interval $beacon_int} \
278                 ${brstr:+basic-rates $brstr} \
279                 ${mcval:+mcast-rate $mcval} \
280                 ${keyspec:+keys $keyspec}
281 }
282
283 mac80211_setup_vif() {
284         local name="$1"
285
286         json_select data
287         json_get_vars ifname
288         json_select ..
289
290         json_select config
291         json_get_vars mode
292         json_get_var vif_txpower txpower
293
294         ifconfig "$ifname" up || {
295                 wireless_setup_vif_failed IFUP_ERROR
296                 json_select ..
297                 return
298         }
299
300         set_default vif_txpower "$txpower"
301         [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
302
303         case "$mode" in
304                 mesh)
305                         for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
306                                 json_get_var mp_val "$var"
307                                 [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
308                         done
309                         # todo: authsae
310                 ;;
311                 adhoc)
312                         wireless_vif_parse_encryption
313                         if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
314                                 mac80211_setup_supplicant || failed=1
315                         else
316                                 mac80211_setup_adhoc
317                         fi
318                 ;;
319                 sta)
320                         mac80211_setup_supplicant || failed=1
321                 ;;
322         esac
323
324         json_select ..
325         [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
326 }
327
328 get_freq() {
329         local phy="$1"
330         local chan="$2"
331         iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
332 }
333
334 mac80211_interface_cleanup() {
335         local phy="$1"
336
337         for wdev in $(list_phy_interfaces "$phy"); do
338                 ifconfig "$wdev" down 2>/dev/null
339                 iw dev "$wdev" del
340         done
341 }
342
343 drv_mac80211_cleanup() {
344         hostapd_common_cleanup
345 }
346
347 drv_mac80211_setup() {
348         json_select config
349         json_get_vars \
350                 phy macaddr path \
351                 country chanbw distance \
352                 txpower antenna_gain \
353                 rxantenna txantenna \
354                 frag rts beacon_int
355         json_select ..
356
357         find_phy || {
358                 echo "Could not find PHY for device '$1'"
359                 wireless_set_retry 0
360                 return 1
361         }
362
363         wireless_set_data phy="$phy"
364         mac80211_interface_cleanup "$phy"
365
366         # convert channel to frequency
367         [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
368
369         [ -n "$country" ] && {
370                 iw reg get | grep -q "^country $country:" || {
371                         iw reg set "$country"
372                         sleep 1
373                 }
374         }
375
376         hostapd_conf_file="/var/run/hostapd-$phy.conf"
377
378         no_ap=1
379         macidx=0
380         staidx=0
381
382         [ -n "$chanbw" ] && {
383                 for file in /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
384                         [ -f "$file" ] && echo "$chanbw" > "$file"
385                 done
386         }
387
388         set_default rxantenna all
389         set_default txantenna all
390         set_default distance 0
391         set_default antenna_gain 0
392
393         iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
394         iw phy "$phy" set antenna_gain $antenna_gain
395         iw phy "$phy" set distance "$distance"
396
397         [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
398         [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
399
400         has_ap=
401         hostapd_ctrl=
402         for_each_interface "ap" mac80211_check_ap
403
404         rm -f "$hostapd_conf_file"
405         [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
406
407         for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
408         for_each_interface "ap" mac80211_prepare_vif
409
410         [ -n "$hostapd_ctrl" ] && {
411                 /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
412                 ret="$?"
413                 wireless_add_process "$(cat /var/run/wifi-$phy.pid)" "/usr/sbin/hostapd" 1
414                 [ "$ret" != 0 ] && {
415                         wireless_setup_failed HOSTAPD_START_FAILED
416                         return
417                 }
418         }
419
420         for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
421
422         wireless_set_up
423 }
424
425 list_phy_interfaces() {
426         local phy="$1"
427         if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
428                 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
429         else
430                 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
431         fi
432 }
433
434 drv_mac80211_teardown() {
435         wireless_process_kill_all
436
437         json_select data
438         json_get_vars phy
439         json_select ..
440
441         mac80211_interface_cleanup "$phy"
442 }
443
444 add_driver mac80211