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