hostapd: fix mcast_rate setting
[openwrt.git] / package / network / services / hostapd / files / netifd.sh
1 hostapd_add_rate() {
2         local var="$1"
3         local val="$(($2 / 1000))"
4         local sub="$((($2 / 100) % 10))"
5         append $var "$val" ","
6         [ $sub -gt 0 ] && append $var "."
7 }
8
9 hostapd_append_wep_key() {
10         local var="$1"
11
12         wep_keyidx=0
13         set_default key 1
14         case "$key" in
15                 [1234])
16                         for idx in 1 2 3 4; do
17                                 local zidx
18                                 zidx=$(($idx - 1))
19                                 json_get_var ckey "key${idx}"
20                                 [ -n "$ckey" ] && \
21                                         append $var "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N$T"
22                         done
23                         wep_keyidx=$((key - 1))
24                 ;;
25                 *)
26                         append $var "wep_key0=$(prepare_key_wep "$key")" "$N$T"
27                 ;;
28         esac
29 }
30
31 hostapd_add_log_config() {
32         config_add_boolean \
33                 log_80211 \
34                 log_8021x \
35                 log_radius \
36                 log_wpa \
37                 log_driver \
38                 log_iapp \
39                 log_mlme
40
41         config_add_int log_level
42 }
43
44 hostapd_common_add_device_config() {
45         config_add_array basic_rate
46
47         config_add_string country
48         config_add_boolean country_ie
49
50         hostapd_add_log_config
51 }
52
53 hostapd_prepare_device_config() {
54         local config="$1"
55         local driver="$2"
56
57         local base="${config%%.conf}"
58         local base_cfg=
59
60         json_get_vars country country_ie beacon_int basic_rate
61
62         hostapd_set_log_options base_cfg
63
64         set_default country_ie 1
65         [ -n "$country" ] && {
66                 append base_cfg "country_code=$country" "$N"
67                 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
68         }
69         [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
70
71         local brlist= br
72         for br in $basic_rate_list; do
73                 hostapd_add_rate brlist "$br"
74         done
75         [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
76         [ -n "$beacon_int" ] && append base_cfg "beacon_int=$beacon_int" "$N"
77
78         cat > "$config" <<EOF
79 driver=$driver
80 $base_cfg
81 EOF
82 }
83
84 hostapd_common_add_bss_config() {
85         config_add_string bssid ssid
86         config_add_boolean wds wmm hidden
87
88         config_add_int maxassoc max_inactivity
89         config_add_boolean disassoc_low_ack isolate short_preamble
90
91         config_add_int \
92                 wep_rekey eap_reauth_period \
93                 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
94
95         config_add_boolean rsn_preauth auth_cache
96         config_add_int ieee80211w
97
98         config_add_string auth_server server
99         config_add_string auth_secret
100         config_add_int auth_port port
101
102         config_add_string acct_server
103         config_add_string acct_secret
104         config_add_int acct_port
105
106         config_add_string dae_client
107         config_add_string dae_secret
108         config_add_int dae_port
109
110         config_add_string nasid
111         config_add_string iapp_interface
112         config_add_string eap_type ca_cert client_cert identity auth priv_key priv_key_pwd
113
114         config_add_string key1 key2 key3 key4 password
115
116         config_add_boolean wps_pushbutton wps_label ext_registrar
117         config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
118
119         config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
120
121         config_add_string macfilter macfile
122         config_add_array maclist
123
124         config_add_int mcast_rate
125         config_add_array basic_rate
126 }
127
128 hostapd_set_bss_options() {
129         local var="$1"
130         local phy="$2"
131         local vif="$3"
132
133         wireless_vif_parse_encryption
134
135         local bss_conf
136         local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey
137
138         json_get_vars \
139                 wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey \
140                 maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
141                 wps_pushbutton wps_label ext_registrar \
142                 wps_device_type wps_device_name wps_manufacturer wps_pin \
143                 macfilter ssid wmm hidden short_preamble
144
145         set_default isolate 0
146         set_default maxassoc 0
147         set_default max_inactivity 0
148         set_default short_preamble 1
149         set_default disassoc_low_ack 1
150         set_default hidden 0
151         set_default wmm 1
152
153         append bss_conf "ctrl_interface=/var/run/hostapd"
154         if [ "$isolate" -gt 0 ]; then
155                 append bss_conf "ap_isolate=$isolate" "$N"
156         fi
157         if [ "$maxassoc" -gt 0 ]; then
158                 append bss_conf "max_num_sta=$maxassoc" "$N"
159         fi
160         if [ "$max_inactivity" -gt 0 ]; then
161                 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
162         fi
163
164         append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
165         append bss_conf "preamble=$short_preamble" "$N"
166         append bss_conf "wmm_enabled=$wmm" "$N"
167         append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
168
169         [ "$wpa" -gt 0 ] && {
170                 [ -n "$wpa_group_rekey"  ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
171                 [ -n "$wpa_pair_rekey"   ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey"    "$N"
172                 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey"  "$N"
173         }
174
175         case "$auth_type" in
176                 none)
177                         wps_possible=1
178                         # Here we make the assumption that if we're in open mode
179                         # with WPS enabled, we got to be in unconfigured state.
180                         wps_not_configured=1
181                 ;;
182                 psk)
183                         json_get_vars key
184                         if [ ${#key} -lt 8 ]; then
185                                 wireless_setup_vif_failed INVALID_WPA_PSK
186                                 return 1
187                         elif [ ${#key} -eq 64 ]; then
188                                 append bss_conf "wpa_psk=$key" "$N"
189                         else
190                                 append bss_conf "wpa_passphrase=$key" "$N"
191                         fi
192                         wps_possible=1
193                 ;;
194                 eap)
195                         json_get_vars \
196                                 auth_server auth_secret auth_port \
197                                 acct_server acct_secret acct_port \
198                                 dae_client dae_secret dae_port \
199                                 nasid rsn_preauth iapp_interface \
200                                 eap_reauth_period
201
202                         # legacy compatibility
203                         [ -n "$auth_server" ] || json_get_var auth_server server
204                         [ -n "$auth_port" ] || json_get_var auth_port port
205                         [ -n "$auth_secret" ] || json_get_var auth_secret key
206
207                         set_default auth_port 1812
208                         set_default acct_port 1813
209                         set_default dae_port 3799
210
211                         append bss_conf "auth_server_addr=$auth_server" "$N"
212                         append bss_conf "auth_server_port=$auth_port" "$N"
213                         append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
214
215                         [ -n "$acct_server" ] && {
216                                 append bss_conf "acct_server_addr=$acct_server" "$N"
217                                 append bss_conf "acct_server_port=$acct_port" "$N"
218                                 [ -n "$acct_secret" ] && \
219                                         append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
220                         }
221
222                         [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
223
224                         [ -n "$dae_client" -a -n "$dae_secret" ] && {
225                                 append bss_conf "radius_das_port=$dae_port" "$N"
226                                 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
227                         }
228
229                         append bss_conf "nas_identifier=$nasid" "$N"
230                         append bss_conf "eapol_key_index_workaround=1" "$N"
231                         append bss_conf "ieee8021x=1" "$N"
232                         append bss_conf "wpa_key_mgmt=WPA-EAP" "$N"
233                 ;;
234                 wep)
235                         local wep_keyidx=0
236                         json_get_vars key
237                         hostapd_append_wep_key bss_conf
238                         append bss_conf "wep_default_key=$wep_keyidx" "$N"
239                         [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
240                 ;;
241         esac
242
243         local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
244         append bss_conf "auth_algs=${auth_algs:-1}" "$N"
245         append bss_conf "wpa=$wpa" "$N"
246         [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
247
248         set_default wps_pushbutton 0
249         set_default wps_label 0
250
251         config_methods=
252         [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
253         [ "$wps_label" -gt 0 ] && append config_methods label
254
255         [ -n "$wps_possible" -a -n "$config_methods" ] && {
256                 set_default ext_registrar 0
257                 set_default wps_device_type "6-0050F204-1"
258                 set_default wps_device_name "OpenWrt AP"
259                 set_default wps_manufacturer "openwrt.org"
260                 set_default wps_pin "12345670"
261
262                 wps_state=2
263                 [ -n "$wps_configured" ] && wps_state=1
264
265                 [ "$ext_registrar" -gt 0 -a -n "$bridge" ] && append bss_conf "upnp_iface=$bridge" "$N"
266
267                 append bss_conf "eap_server=1" "$N"
268                 append bss_conf "ap_pin=$wps_pin" "$N"
269                 append bss_conf "wps_state=$wps_state" "$N"
270                 append bss_conf "ap_setup_locked=0" "$N"
271                 append bss_conf "device_type=$wps_device_type" "$N"
272                 append bss_conf "device_name=$wps_device_name" "$N"
273                 append bss_conf "manufacturer=$wps_manufacturer" "$N"
274                 append bss_conf "config_methods=$config_methods" "$N"
275         }
276
277         append bss_conf "ssid=$ssid" "$N"
278         [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
279         [ -n "$iapp_interface" ] &&  {
280                 iapp_interface="$(uci_get_state network "$iapp_interface" ifname "$iapp_interface")"
281                 [ -n "$iapp_interface" ] && append bss_conf "iapp_interface=$iapp_interface" "$N"
282         }
283
284         if [ "$wpa" -ge "2" ]; then
285                 if [ -n "$bridge" -a "$rsn_preauth" = 1 ]; then
286                         set_default auth_cache 1
287                         append bss_conf "rsn_preauth=1" "$N"
288                         append bss_conf "rsn_preauth_interfaces=$bridge" "$N"
289                 else
290                         set_default auth_cache 0
291                 fi
292
293                 append bss_conf "okc=$auth_cache" "$N"
294                 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
295
296                 # RSN -> allow management frame protection
297                 json_get_var ieee80211w
298                 case "$ieee80211w" in
299                         [012])
300                                 json_get_vars ieee80211w_max_timeout ieee80211w_retry_timeout
301                                 append bss_conf "ieee80211w=$ieee80211w" "$N"
302                                 [ "$ieee80211w" -gt "0" ] && {
303                                         [ -n "$ieee80211w_max_timeout" ] && \
304                                                 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
305                                         [ -n "$ieee80211w_retry_timeout" ] && \
306                                                 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
307                                 }
308                         ;;
309                 esac
310         fi
311
312         _macfile="/var/run/hostapd-$ifname.maclist"
313         case "$macfilter" in
314                 allow)
315                         append bss_conf "macaddr_acl=1" "$N"
316                         append bss_conf "accept_mac_file=$_macfile" "$N"
317                 ;;
318                 deny)
319                         append bss_conf "macaddr_acl=0" "$N"
320                         append bss_conf "deny_mac_file=$_macfile" "$N"
321                 ;;
322                 *)
323                         _macfile=""
324                 ;;
325         esac
326
327         [ -n "$_macfile" ] && {
328                 json_get_vars macfile
329                 json_get_values maclist maclist
330
331                 rm -f "$_macfile"
332                 (
333                         for mac in $maclist; do
334                                 echo "$mac"
335                         done
336                         [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
337                 ) > "$_macfile"
338         }
339
340         append "$var" "$bss_conf" "$N"
341         return 0
342 }
343
344 hostapd_set_log_options() {
345         local var="$1"
346
347         local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
348         json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
349
350         set_default log_level 2
351         set_default log_80211  1
352         set_default log_8021x  1
353         set_default log_radius 1
354         set_default log_wpa    1
355         set_default log_driver 1
356         set_default log_iapp   1
357         set_default log_mlme   1
358
359         local log_mask=$((       \
360                 ($log_80211  << 0) | \
361                 ($log_8021x  << 1) | \
362                 ($log_radius << 2) | \
363                 ($log_wpa    << 3) | \
364                 ($log_driver << 4) | \
365                 ($log_iapp   << 5) | \
366                 ($log_mlme   << 6)   \
367         ))
368
369         append "$var" "logger_syslog=$log_mask" "$N"
370         append "$var" "logger_syslog_level=$log_level" "$N"
371         append "$var" "logger_stdout=$log_mask" "$N"
372         append "$var" "logger_stdout_level=$log_level" "$N"
373
374         return 0
375 }
376
377 _wpa_supplicant_common() {
378         local ifname="$1"
379
380         _rpath="/var/run/wpa_supplicant"
381         _config="${_rpath}-$ifname.conf"
382 }
383
384 wpa_supplicant_teardown_interface() {
385         _wpa_supplicant_common "$1"
386         rm -rf "$_rpath" "$_config"
387 }
388
389 wpa_supplicant_prepare_interface() {
390         local ifname="$1"
391         _w_driver="$2"
392
393         _wpa_supplicant_common "$1"
394
395         json_get_vars mode wds
396
397         [ -n "$network_bridge" ] && {
398                 fail=
399                 case "$mode" in
400                         adhoc)
401                                 fail=1
402                         ;;
403                         sta)
404                                 [ "$wds" = 1 ] || fail=1
405                         ;;
406                 esac
407
408                 [ -n "$fail" ] && {
409                         wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
410                         return 1
411                 }
412         }
413
414         local ap_scan=
415
416         _w_mode="$mode"
417         _w_modestr=
418
419         [[ "$mode" = adhoc ]] && {
420                 ap_scan="ap_scan=2"
421
422                 _w_modestr="mode=1"
423         }
424
425         wpa_supplicant_teardown_interface "$ifname"
426         cat > "$_config" <<EOF
427 $ap_scan
428 EOF
429         return 0
430 }
431
432 wpa_supplicant_add_network() {
433         local ifname="$1"
434
435         _wpa_supplicant_common "$1"
436         wireless_vif_parse_encryption
437
438         json_get_vars \
439                 ssid bssid key \
440                 basic_rate mcast_rate \
441                 ieee80211w
442
443         local key_mgmt='NONE'
444         local enc_str=
445         local network_data=
446         local T="       "
447
448         local wpa_key_mgmt="WPA-PSK"
449         local scan_ssid="1"
450         local freq
451
452         [[ "$_w_mode" = "adhoc" ]] && {
453                 append network_data "mode=1" "$N$T"
454                 [ -n "$channel" ] && {
455                         freq="$(get_freq "$phy" "$channel")"
456                         append network_data "fixed_freq=1" "$N$T"
457                         append network_data "frequency=$freq" "$N$T"
458                 }
459
460                 scan_ssid=0
461
462                 [ "$_w_driver" = "nl80211" ] || wpa_key_mgmt="WPA-NONE"
463         }
464
465         [[ "$_w_mode" = adhoc ]] && append network_data "$_w_modestr" "$N$T"
466
467         case "$auth_type" in
468                 none) ;;
469                 wep)
470                         local wep_keyidx=0
471                         hostapd_append_wep_key network_data
472                         append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
473                 ;;
474                 psk)
475                         local passphrase
476
477                         key_mgmt="$wpa_key_mgmt"
478                         if [ ${#key} -eq 64 ]; then
479                                 passphrase="psk=${key}"
480                         else
481                                 passphrase="psk=\"${key}\""
482                         fi
483                         append network_data "$passphrase" "$N$T"
484                 ;;
485                 eap)
486                         key_mgmt='WPA-EAP'
487
488                         json_get_vars eap_type identity ca_cert
489                         [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
490                         [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
491                         case "$eap_type" in
492                                 tls)
493                                         json_get_vars client_cert priv_key priv_key_pwd
494                                         append network_data "client_cert=\"$client_cert\"" "$N$T"
495                                         append network_data "private_key=\"$priv_key\"" "$N$T"
496                                         append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
497                                 ;;
498                                 peap|ttls)
499                                         json_get_vars auth password
500                                         set_default auth MSCHAPV2
501                                         append network_data "phase2=\"$auth\"" "$N$T"
502                                         append network_data "password=\"$password\"" "$N$T"
503                                 ;;
504                         esac
505                         append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
506                 ;;
507         esac
508
509         case "$wpa" in
510                 1)
511                         append network_data "proto=WPA" "$N$T"
512                 ;;
513                 2)
514                         append network_data "proto=RSN" "$N$T"
515                 ;;
516         esac
517
518         case "$ieee80211w" in
519                 [012])
520                         [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
521                 ;;
522         esac
523
524         local beacon_int brates mrate
525         [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
526         [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
527
528
529         [ -n "$basic_rate" ] && {
530                 local br rate_list=
531                 for br in $basic_rate; do
532                         hostapd_add_rate rate_list "$br"
533                 done
534                 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
535         }
536
537         [ -n "$mcast_rate" ] && {
538                 local mc_rate=
539                 hostapd_add_rate mc_rate "$mcast_rate"
540                 append network_data "mcast_rate=$mc_rate" "$N$T"
541         }
542
543         local ht_str
544         [ -n "$ht" ] && append network_data "htmode=$ht" "$N$T"
545
546         cat >> "$_config" <<EOF
547 network={
548         scan_ssid=$scan_ssid
549         ssid="$ssid"
550         key_mgmt=$key_mgmt
551         $network_data
552 }
553 EOF
554         return 0
555 }
556
557 wpa_supplicant_run() {
558         local ifname="$1"; shift
559
560         _wpa_supplicant_common "$ifname"
561
562         /usr/sbin/wpa_supplicant -B \
563                 ${network_bridge:+-b $network_bridge} \
564                 -P "/var/run/wpa_supplicant-${ifname}.pid" \
565                 -D ${_w_driver:-wext} \
566                 -i "$ifname" \
567                 -c "$_config" \
568                 -C "$_rpath" \
569                 "$@"
570
571         ret="$?"
572         wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
573
574         [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
575
576         return $ret
577 }
578
579 hostapd_common_cleanup() {
580         killall hostapd wpa_supplicant
581 }