mac80211: Add mac address filter configuration for hostapd
[openwrt.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 mac80211_hostapd_setup_base() {
5         local phy="$1"
6         local ifname="$2"
7
8         cfgfile="/var/run/hostapd-$phy.conf"
9         macfile="/var/run/hostapd-$phy.maclist"
10         [ -e "$macfile" ] && rm -f "$macfile"
11
12         config_get device "$vif" device
13         config_get country "$device" country
14         config_get hwmode "$device" hwmode
15         config_get channel "$device" channel
16         config_get_bool noscan "$device" noscan
17         [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
18         [ "$channel" = auto ] && channel=
19         [ -n "$hwmode" ] && {
20                 config_get hwmode_11n "$device" hwmode_11n
21                 [ -n "$hwmode_11n" ] && {
22                         hwmode="$hwmode_11n"
23                         append base_cfg "ieee80211n=1" "$N"
24                         config_get htmode "$device" htmode
25                         config_get ht_capab_list "$device" ht_capab
26                         case "$htmode" in
27                                 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
28                                 *)ht_capab=;;
29                         esac
30                         for cap in $ht_capab_list; do
31                                 ht_capab="$ht_capab[$cap]"
32                         done
33                         [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
34                 }
35         }
36
37         config_get macfilter "$vif" macfilter
38         case "$macfilter" in
39                 allow)
40                         append base_cfg "macaddr_acl=1" "$N"
41                         append base_cfg "accept_mac_file=$macfile" "$N"
42                         ;;
43                 deny)
44                         append base_cfg "macaddr_acl=0" "$N"
45                         append base_cfg "deny_mac_file=$macfile" "$N"
46                         ;;
47         esac
48         config_get maclist "$vif" maclist
49         [ -n "$maclist" ] && {
50                 for mac in $maclist; do
51                         echo "$mac" >> $macfile
52                 done
53         }
54         cat >> "$cfgfile" <<EOF
55 ctrl_interface=/var/run/hostapd-$phy
56 driver=nl80211
57 wmm_ac_bk_cwmin=4
58 wmm_ac_bk_cwmax=10
59 wmm_ac_bk_aifs=7
60 wmm_ac_bk_txop_limit=0
61 wmm_ac_bk_acm=0
62 wmm_ac_be_aifs=3
63 wmm_ac_be_cwmin=4
64 wmm_ac_be_cwmax=10
65 wmm_ac_be_txop_limit=0
66 wmm_ac_be_acm=0
67 wmm_ac_vi_aifs=2
68 wmm_ac_vi_cwmin=3
69 wmm_ac_vi_cwmax=4
70 wmm_ac_vi_txop_limit=94
71 wmm_ac_vi_acm=0
72 wmm_ac_vo_aifs=2
73 wmm_ac_vo_cwmin=2
74 wmm_ac_vo_cwmax=3
75 wmm_ac_vo_txop_limit=47
76 wmm_ac_vo_acm=0
77 tx_queue_data3_aifs=7
78 tx_queue_data3_cwmin=15
79 tx_queue_data3_cwmax=1023
80 tx_queue_data3_burst=0
81 tx_queue_data2_aifs=3
82 tx_queue_data2_cwmin=15
83 tx_queue_data2_cwmax=63
84 tx_queue_data2_burst=0
85 tx_queue_data1_aifs=1
86 tx_queue_data1_cwmin=7
87 tx_queue_data1_cwmax=15
88 tx_queue_data1_burst=3.0
89 tx_queue_data0_aifs=1
90 tx_queue_data0_cwmin=3
91 tx_queue_data0_cwmax=7
92 tx_queue_data0_burst=1.5
93 ${hwmode:+hw_mode=$hwmode}
94 ${channel:+channel=$channel}
95 ${country:+country_code=$country}
96 ${noscan:+noscan=$noscan}
97 $base_cfg
98
99 EOF
100 }
101
102 mac80211_hostapd_setup_bss() {
103         local phy="$1"
104         local vif="$2"
105
106         hostapd_cfg=
107         cfgfile="/var/run/hostapd-$phy.conf"
108         config_get ifname "$vif" ifname
109
110         if [ -f "$cfgfile" ]; then
111                 append hostapd_cfg "bss=$ifname" "$N"
112         else
113                 mac80211_hostapd_setup_base "$phy" "$ifname"
114                 append hostapd_cfg "interface=$ifname" "$N"
115         fi
116
117         local net_cfg bridge
118         net_cfg="$(find_net_config "$vif")"
119         [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
120         config_set "$vif" bridge "$bridge"
121
122         hostapd_set_bss_options hostapd_cfg "$vif"
123
124         config_get_bool wds "$vif" wds 0
125         [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
126
127         local macaddr hidden maxassoc wmm
128         config_get macaddr "$vif" macaddr
129         config_get maxassoc "$vif" maxassoc
130         config_get_bool hidden "$vif" hidden 0
131         config_get_bool wmm "$vif" wmm 1
132         cat >> /var/run/hostapd-$phy.conf <<EOF
133 $hostapd_cfg
134 wmm_enabled=$wmm
135 bssid=$macaddr
136 ignore_broadcast_ssid=$hidden
137 ${maxassoc:+max_num_sta=$maxassoc}
138 EOF
139 }
140
141 mac80211_start_vif() {
142         local vif="$1"
143         local ifname="$2"
144
145         local net_cfg
146         net_cfg="$(find_net_config "$vif")"
147         [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
148
149         set_wifi_up "$vif" "$ifname"
150 }
151
152 find_mac80211_phy() {
153         local device="$1"
154
155         local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
156         config_get phy "$device" phy
157         [ -z "$phy" -a -n "$macaddr" ] && {
158                 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
159                         [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
160                         config_set "$device" phy "$phy"
161                         break
162                 done
163                 config_get phy "$device" phy
164         }
165         [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
166                 echo "PHY for wifi device $1 not found"
167                 return 1
168         }
169         [ -z "$macaddr" ] && {
170                 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
171         }
172         return 0
173 }
174
175 scan_mac80211() {
176         local device="$1"
177         local adhoc sta ap monitor mesh
178
179         config_get vifs "$device" vifs
180         for vif in $vifs; do
181                 config_get mode "$vif" mode
182                 case "$mode" in
183                         adhoc|sta|ap|monitor|mesh)
184                                 append $mode "$vif"
185                         ;;
186                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
187                 esac
188         done
189
190         config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
191 }
192
193 list_phy_interfaces() {
194         local phy="$1"
195         if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
196                 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
197         else
198                 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
199         fi
200 }
201
202 disable_mac80211() (
203         local device="$1"
204
205         find_mac80211_phy "$device" || return 0
206         config_get phy "$device" phy
207
208         set_wifi_down "$device"
209         # kill all running hostapd and wpa_supplicant processes that
210         # are running on atheros/mac80211 vifs
211         for pid in `pidof hostapd`; do
212                 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
213                         kill $pid
214         done
215
216         include /lib/network
217         for wdev in $(list_phy_interfaces "$phy"); do
218                 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
219                 for pid in `pidof wpa_supplicant`; do
220                         grep "$wdev" /proc/$pid/cmdline >/dev/null && \
221                                 kill $pid
222                 done
223                 ifconfig "$wdev" down 2>/dev/null
224                 unbridge "$dev"
225                 iw dev "$wdev" del
226         done
227
228         return 0
229 )
230 get_freq() {
231         local phy="$1"
232         local chan="$2"
233         iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
234 }
235 enable_mac80211() {
236         local device="$1"
237         config_get channel "$device" channel
238         config_get vifs "$device" vifs
239         config_get txpower "$device" txpower
240         config_get country "$device" country
241         config_get distance "$device" distance
242         config_get frag "$device" frag
243         config_get rts "$device" rts
244         find_mac80211_phy "$device" || return 0
245         config_get phy "$device" phy
246         local i=0
247         local macidx=0
248         local apidx=0
249         fixed=""
250         local hostapd_ctrl=""
251
252         [ -n "$country" ] && iw reg set "$country"
253         [ "$channel" = "auto" -o "$channel" = "0" ] || {
254                 fixed=1
255         }
256
257         [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
258         [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
259         [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
260
261         export channel fixed
262         # convert channel to frequency
263         local freq="$(get_freq "$phy" "${fixed:+$channel}")"
264
265         wifi_fixup_hwmode "$device" "g"
266         for vif in $vifs; do
267                 while [ -d "/sys/class/net/wlan$i" ]; do
268                         i=$(($i + 1))
269                 done
270
271                 config_get ifname "$vif" ifname
272                 [ -n "$ifname" ] || {
273                         ifname="wlan$i"
274                 }
275                 config_set "$vif" ifname "$ifname"
276
277                 config_get mode "$vif" mode
278                 config_get ssid "$vif" ssid
279
280                 # It is far easier to delete and create the desired interface
281                 case "$mode" in
282                         adhoc)
283                                 iw phy "$phy" interface add "$ifname" type adhoc
284                         ;;
285                         ap)
286                                 # Hostapd will handle recreating the interface and
287                                 # it's accompanying monitor
288                                 apidx="$(($apidx + 1))"
289                                 i=$(($i + 1))
290                                 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
291                         ;;
292                         mesh)
293                                 config_get mesh_id "$vif" mesh_id
294                                 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
295                         ;;
296                         monitor)
297                                 iw phy "$phy" interface add "$ifname" type monitor
298                         ;;
299                         sta)
300                                 local wdsflag
301                                 config_get_bool wds "$vif" wds 0
302                                 [ "$wds" -gt 0 ] && wdsflag="4addr on"
303                                 iw phy "$phy" interface add "$ifname" type managed $wdsflag
304                                 config_get_bool powersave "$vif" powersave 0
305                                 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
306                                 iwconfig "$ifname" power "$powersave"
307                         ;;
308                 esac
309
310                 # All interfaces must have unique mac addresses
311                 # which can either be explicitly set in the device
312                 # section, or automatically generated
313                 config_get macaddr "$device" macaddr
314                 local mac_1="${macaddr%%:*}"
315                 local mac_2="${macaddr#*:}"
316
317                 config_get vif_mac "$vif" macaddr
318                 [ -n "$vif_mac" ] || {
319                         if [ "$macidx" -gt 0 ]; then
320                                 offset="$(( 2 + $macidx * 4 ))"
321                         else
322                                 offset="0"
323                         fi
324                         vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
325                         macidx="$(($macidx + 1))"
326                 }
327                 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
328                 config_set "$vif" macaddr "$vif_mac"
329
330                 # !! ap !!
331                 #
332                 # ALL ap functionality will be passed to hostapd
333                 #
334                 # !! station !!
335                 #
336                 # ALL station functionality will be passed to wpa_supplicant
337                 #
338                 if [ ! "$mode" = "ap" ]; then
339                         # We attempt to set the channel for all interfaces, although
340                         # mac80211 may not support it or the driver might not yet
341                         # for ap mode this is handled by hostapd
342                         [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
343                 fi
344
345                 config_get vif_txpower "$vif" txpower
346                 # use vif_txpower (from wifi-iface) to override txpower (from
347                 # wifi-device) if the latter doesn't exist
348                 txpower="${txpower:-$vif_txpower}"
349                 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
350         done
351
352         local start_hostapd=
353         rm -f /var/run/hostapd-$phy.conf
354         for vif in $vifs; do
355                 config_get mode "$vif" mode
356                 [ "$mode" = "ap" ] || continue
357                 mac80211_hostapd_setup_bss "$phy" "$vif"
358                 start_hostapd=1
359         done
360
361         [ -n "$start_hostapd" ] && {
362                 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
363                         echo "Failed to start hostapd for $phy"
364                         return
365                 }
366                 sleep 2
367
368                 for vif in $vifs; do
369                         config_get mode "$vif" mode
370                         config_get ifname "$vif" ifname
371                         [ "$mode" = "ap" ] || continue
372                         hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
373                         mac80211_start_vif "$vif" "$ifname"
374                 done
375         }
376
377         for vif in $vifs; do
378                 config_get mode "$vif" mode
379                 config_get ifname "$vif" ifname
380                 [ ! "$mode" = "ap" ] || continue
381                 ifconfig "$ifname" up
382
383                 if [ ! "$mode" = "ap" ]; then
384                         ifconfig "$ifname" up
385                         case "$mode" in
386                                 adhoc)
387                                         config_get bssid "$vif" bssid
388                                         config_get ssid "$vif" ssid
389                                         config_get mcast_rate "$vif" mcast_rate
390                                         local mcval=""
391                                         [ -n "$mcast_rate" ] && {
392                                                 mcval="$(($mcast_rate / 1000))"
393                                                 mcsub="$(( ($mcast_rate / 100) % 10 ))"
394                                                 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
395                                         }
396                                         iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid ${mcval:+mcast-rate $mcval}
397                                 ;;
398                                 sta)
399                                         if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
400                                                 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
401                                                         echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
402                                                         # make sure this wifi interface won't accidentally stay open without encryption
403                                                         ifconfig "$ifname" down
404                                                         continue
405                                                 }
406                                         fi
407                                 ;;
408                         esac
409                         mac80211_start_vif "$vif" "$ifname"
410                 fi
411         done
412
413 }
414
415
416 check_device() {
417         config_get phy "$1" phy
418         [ -z "$phy" ] && {
419                 find_mac80211_phy "$1" >/dev/null || return 0
420                 config_get phy "$1" phy
421         }
422         [ "$phy" = "$dev" ] && found=1
423 }
424
425 detect_mac80211() {
426         devidx=0
427         config_load wireless
428         while :; do
429                 config_get type "radio$devidx" type
430                 [ -n "$type" ] || break
431                 devidx=$(($devidx + 1))
432         done
433         for dev in $(ls /sys/class/ieee80211); do
434                 found=0
435                 config_foreach check_device wifi-device
436                 [ "$found" -gt 0 ] && continue
437
438                 mode_11n=""
439                 mode_band="g"
440                 channel="11"
441                 ht_cap=0
442                 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
443                         ht_cap="$(($ht_cap | $cap))"
444                 done
445                 ht_capab="";
446                 [ "$ht_cap" -gt 0 ] && {
447                         mode_11n="n"
448                         append ht_capab "       option htmode   HT20" "$N"
449
450                         list="  list ht_capab"
451                         [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list  LDPC" "$N"
452                         [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list        GF" "$N"
453                         [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list        SHORT-GI-20" "$N"
454                         [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list        SHORT-GI-40" "$N"
455                         [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list      TX-STBC" "$N"
456                         [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list      RX-STBC1" "$N"
457                         [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list      RX-STBC12" "$N"
458                         [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list      RX-STBC123" "$N"
459                         [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list    DSSS_CCK-40" "$N"
460                 }
461                 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
462
463                 cat <<EOF
464 config wifi-device  radio$devidx
465         option type     mac80211
466         option channel  ${channel}
467         option macaddr  $(cat /sys/class/ieee80211/${dev}/macaddress)
468         option hwmode   11${mode_11n}${mode_band}
469 $ht_capab
470         # REMOVE THIS LINE TO ENABLE WIFI:
471         option disabled 1
472
473 config wifi-iface
474         option device   radio$devidx
475         option network  lan
476         option mode     ap
477         option ssid     OpenWrt
478         option encryption none
479
480 EOF
481         devidx=$(($devidx + 1))
482         done
483 }
484