mac80211: rework mac address allocation
[openwrt/svn-archive/archive.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
10 config_get device "$vif" device
11 config_get country "$device" country
12 config_get hwmode "$device" hwmode
13 config_get channel "$device" channel
14 config_get beacon_int "$device" beacon_int
15 config_get basic_rate_list "$device" basic_rate
16 config_get_bool noscan "$device" noscan
17
18 hostapd_set_log_options base_cfg "$device"
19
20 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
21
22 [ "$channel" = auto ] && {
23 channel=$(iw phy "$phy" info | \
24 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
25 config_set "$device" channel "$channel"
26 }
27
28 [ -n "$hwmode" ] && {
29 config_get hwmode_11n "$device" hwmode_11n
30 [ -n "$hwmode_11n" ] && {
31 hwmode="$hwmode_11n"
32 append base_cfg "ieee80211n=1" "$N"
33 config_get htmode "$device" htmode
34 config_get ht_capab_list "$device" ht_capab
35 case "$htmode" in
36 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
37 *)ht_capab=;;
38 esac
39 for cap in $ht_capab_list; do
40 ht_capab="$ht_capab[$cap]"
41 done
42 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
43 }
44 }
45
46 local country_ie=0
47 [ -n "$country" ] && country_ie=1
48 config_get_bool country_ie "$device" country_ie "$country_ie"
49 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
50
51 local br brval brstr
52 [ -n "$basic_rate_list" ] && {
53 for br in $basic_rate_list; do
54 brval="$(($br / 100))"
55 [ -n "$brstr" ] && brstr="$brstr "
56 brstr="$brstr$brval"
57 done
58 }
59
60 cat >> "$cfgfile" <<EOF
61 ctrl_interface=/var/run/hostapd-$phy
62 driver=nl80211
63 wmm_ac_bk_cwmin=4
64 wmm_ac_bk_cwmax=10
65 wmm_ac_bk_aifs=7
66 wmm_ac_bk_txop_limit=0
67 wmm_ac_bk_acm=0
68 wmm_ac_be_aifs=3
69 wmm_ac_be_cwmin=4
70 wmm_ac_be_cwmax=10
71 wmm_ac_be_txop_limit=0
72 wmm_ac_be_acm=0
73 wmm_ac_vi_aifs=2
74 wmm_ac_vi_cwmin=3
75 wmm_ac_vi_cwmax=4
76 wmm_ac_vi_txop_limit=94
77 wmm_ac_vi_acm=0
78 wmm_ac_vo_aifs=2
79 wmm_ac_vo_cwmin=2
80 wmm_ac_vo_cwmax=3
81 wmm_ac_vo_txop_limit=47
82 wmm_ac_vo_acm=0
83 tx_queue_data3_aifs=7
84 tx_queue_data3_cwmin=15
85 tx_queue_data3_cwmax=1023
86 tx_queue_data3_burst=0
87 tx_queue_data2_aifs=3
88 tx_queue_data2_cwmin=15
89 tx_queue_data2_cwmax=63
90 tx_queue_data2_burst=0
91 tx_queue_data1_aifs=1
92 tx_queue_data1_cwmin=7
93 tx_queue_data1_cwmax=15
94 tx_queue_data1_burst=3.0
95 tx_queue_data0_aifs=1
96 tx_queue_data0_cwmin=3
97 tx_queue_data0_cwmax=7
98 tx_queue_data0_burst=1.5
99 ${hwmode:+hw_mode=$hwmode}
100 ${channel:+channel=$channel}
101 ${beacon_int:+beacon_int=$beacon_int}
102 ${country:+country_code=$country}
103 ${noscan:+noscan=$noscan}
104 ${brstr:+basic_rates=$brstr}
105 $base_cfg
106
107 EOF
108 }
109
110 mac80211_hostapd_setup_bss() {
111 local phy="$1"
112 local vif="$2"
113
114 hostapd_cfg=
115 cfgfile="/var/run/hostapd-$phy.conf"
116 config_get ifname "$vif" ifname
117
118 if [ -f "$cfgfile" ]; then
119 append hostapd_cfg "bss=$ifname" "$N"
120 else
121 mac80211_hostapd_setup_base "$phy" "$ifname"
122 append hostapd_cfg "interface=$ifname" "$N"
123 fi
124
125 local net_cfg bridge
126 net_cfg="$(find_net_config "$vif")"
127 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
128 config_set "$vif" bridge "$bridge"
129
130 hostapd_set_bss_options hostapd_cfg "$vif"
131
132 config_get_bool wds "$vif" wds 0
133 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
134
135 local macaddr hidden maxassoc wmm
136 config_get macaddr "$vif" macaddr
137 config_get maxassoc "$vif" maxassoc
138 config_get dtim_period "$vif" dtim_period
139 config_get max_listen_int "$vif" max_listen_int
140 config_get_bool hidden "$vif" hidden 0
141 config_get_bool wmm "$vif" wmm 1
142 cat >> /var/run/hostapd-$phy.conf <<EOF
143 $hostapd_cfg
144 wmm_enabled=$wmm
145 bssid=$macaddr
146 ignore_broadcast_ssid=$hidden
147 ${dtim_period:+dtim_period=$dtim_period}
148 ${max_listen_int:+max_listen_interval=$max_listen_int}
149 ${maxassoc:+max_num_sta=$maxassoc}
150 EOF
151 }
152
153 mac80211_start_vif() {
154 local vif="$1"
155 local ifname="$2"
156
157 local net_cfg
158 net_cfg="$(find_net_config "$vif")"
159 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
160
161 set_wifi_up "$vif" "$ifname"
162 }
163
164 lookup_phy() {
165 [ -n "$phy" ] && {
166 [ -d /sys/class/ieee80211/$phy ] && return
167 }
168
169 local devpath
170 config_get devpath "$device" path
171 [ -n "$devpath" -a -d "/sys/devices/$devpath/ieee80211" ] && {
172 phy="$(ls /sys/devices/$devpath/ieee80211 | grep -m 1 phy)"
173 [ -n "$phy" ] && return
174 }
175
176 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
177 [ -n "$macaddr" ] && {
178 for _phy in $(ls /sys/class/ieee80211 2>/dev/null); do
179 [ "$macaddr" = "$(cat /sys/class/ieee80211/${_phy}/macaddress)" ] || continue
180 phy="$_phy"
181 return
182 done
183 }
184 phy=
185 return
186 }
187
188 find_mac80211_phy() {
189 local device="$1"
190
191 config_get phy "$device" phy
192 lookup_phy
193 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
194 echo "PHY for wifi device $1 not found"
195 return 1
196 }
197 config_set "$device" phy "$phy"
198
199 config_get macaddr "$device" macaddr
200 [ -z "$macaddr" ] && {
201 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
202 }
203
204 return 0
205 }
206
207 scan_mac80211() {
208 local device="$1"
209 local adhoc sta ap monitor mesh disabled
210
211 config_get vifs "$device" vifs
212 for vif in $vifs; do
213 config_get_bool disabled "$vif" disabled 0
214 [ $disabled = 0 ] || continue
215
216 config_get mode "$vif" mode
217 case "$mode" in
218 adhoc|sta|ap|monitor|mesh)
219 append $mode "$vif"
220 ;;
221 *) echo "$device($vif): Invalid mode, ignored."; continue;;
222 esac
223 done
224
225 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
226 }
227
228 list_phy_interfaces() {
229 local phy="$1"
230 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
231 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
232 else
233 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
234 fi
235 }
236
237 disable_mac80211() (
238 local device="$1"
239
240 find_mac80211_phy "$device" || return 0
241 config_get phy "$device" phy
242
243 set_wifi_down "$device"
244 # kill all running hostapd and wpa_supplicant processes that
245 # are running on atheros/mac80211 vifs
246 for pid in `pidof hostapd`; do
247 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
248 kill $pid
249 done
250
251 include /lib/network
252 for wdev in $(list_phy_interfaces "$phy"); do
253 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
254 for pid in `pidof wpa_supplicant`; do
255 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
256 kill $pid
257 done
258 ifconfig "$wdev" down 2>/dev/null
259 unbridge "$dev"
260 iw dev "$wdev" del
261 done
262
263 return 0
264 )
265
266 get_freq() {
267 local phy="$1"
268 local chan="$2"
269 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
270 }
271
272 mac80211_generate_mac() {
273 local id="$1"
274 local ref="$2"
275 local mask="$3"
276
277 [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
278 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
279
280 local mask1=$1
281 local mask6=$6
282
283 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
284 [ "$((0x$mask1))" -gt 0 ] && {
285 b1=$(((0x$1 | 0x2) ^ ($id << 2)))
286 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
287 return
288 }
289
290 [ "$((0x$mask6))" -lt 255 ] && {
291 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
292 return
293 }
294
295 off2=$(( (0x$6 + $id) / 0x100 ))
296 printf "%s:%s:%s:%s:%02x:%02x" \
297 $1 $2 $3 $4 \
298 $(( (0x$5 + $off2) % 0x100 )) \
299 $(( (0x$6 + $id) % 0x100 ))
300 }
301
302 enable_mac80211() {
303 local device="$1"
304 config_get channel "$device" channel
305 config_get vifs "$device" vifs
306 config_get txpower "$device" txpower
307 config_get country "$device" country
308 config_get distance "$device" distance
309 config_get txantenna "$device" txantenna all
310 config_get rxantenna "$device" rxantenna all
311 config_get antenna_gain "$device" antenna_gain 0
312 config_get frag "$device" frag
313 config_get rts "$device" rts
314 find_mac80211_phy "$device" || return 0
315 config_get phy "$device" phy
316 local i=0
317 local macidx=0
318 local apidx=0
319 fixed=""
320 local hostapd_ctrl=""
321
322 [ -n "$country" ] && {
323 iw reg get | grep -q "^country $country:" || {
324 iw reg set "$country"
325 sleep 1
326 }
327 }
328
329 config_get chanbw "$device" chanbw
330 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
331 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
332
333 [ "$channel" = "auto" -o "$channel" = "0" ] || {
334 fixed=1
335 }
336
337 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
338 iw phy "$phy" set antenna_gain $antenna_gain
339
340 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
341 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
342 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
343
344 export channel fixed
345 # convert channel to frequency
346 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
347
348 wifi_fixup_hwmode "$device" "g"
349 for vif in $vifs; do
350 config_get ifname "$vif" ifname
351 [ -n "$ifname" ] || {
352 [ $i -gt 0 ] && ifname="wlan${phy#phy}-$i" || ifname="wlan${phy#phy}"
353 }
354 config_set "$vif" ifname "$ifname"
355
356 config_get mode "$vif" mode
357 config_get ssid "$vif" ssid
358
359 # It is far easier to delete and create the desired interface
360 case "$mode" in
361 adhoc)
362 iw phy "$phy" interface add "$ifname" type adhoc
363 ;;
364 ap)
365 # Hostapd will handle recreating the interface and
366 # it's accompanying monitor
367 apidx="$(($apidx + 1))"
368 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
369 ;;
370 mesh)
371 config_get mesh_id "$vif" mesh_id
372 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
373 ;;
374 monitor)
375 iw phy "$phy" interface add "$ifname" type monitor
376 ;;
377 sta)
378 local wdsflag
379 config_get_bool wds "$vif" wds 0
380 [ "$wds" -gt 0 ] && wdsflag="4addr on"
381 iw phy "$phy" interface add "$ifname" type managed $wdsflag
382 config_get_bool powersave "$vif" powersave 0
383 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
384 iw "$ifname" set power_save "$powersave"
385 ;;
386 esac
387
388 # All interfaces must have unique mac addresses
389 # which can either be explicitly set in the device
390 # section, or automatically generated
391 config_get macaddr "$device" macaddr
392 config_get vif_mac "$vif" macaddr
393 [ -n "$vif_mac" ] || {
394 vif_mac="$(mac80211_generate_mac $macidx $macaddr $(cat /sys/class/ieee80211/${phy}/address_mask))"
395 macidx="$(($macidx + 1))"
396 }
397 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
398 config_set "$vif" macaddr "$vif_mac"
399
400 # !! ap !!
401 #
402 # ALL ap functionality will be passed to hostapd
403 #
404 # !! station !!
405 #
406 # ALL station functionality will be passed to wpa_supplicant
407 #
408 if [ ! "$mode" = "ap" ]; then
409 # We attempt to set the channel for all interfaces, although
410 # mac80211 may not support it or the driver might not yet
411 # for ap mode this is handled by hostapd
412 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
413 fi
414
415 i=$(($i + 1))
416 done
417
418 local start_hostapd=
419 rm -f /var/run/hostapd-$phy.conf
420 for vif in $vifs; do
421 config_get mode "$vif" mode
422 [ "$mode" = "ap" ] || continue
423 mac80211_hostapd_setup_bss "$phy" "$vif"
424 start_hostapd=1
425 done
426
427 [ -n "$start_hostapd" ] && {
428 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
429 echo "Failed to start hostapd for $phy"
430 return
431 }
432 sleep 2
433
434 for vif in $vifs; do
435 config_get mode "$vif" mode
436 config_get ifname "$vif" ifname
437 [ "$mode" = "ap" ] || continue
438 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
439 mac80211_start_vif "$vif" "$ifname"
440 done
441 }
442
443 for vif in $vifs; do
444 config_get mode "$vif" mode
445 config_get ifname "$vif" ifname
446 [ "$mode" = "ap" ] || ifconfig "$ifname" up
447
448 config_get vif_txpower "$vif" txpower
449 # use vif_txpower (from wifi-iface) to override txpower (from
450 # wifi-device) if the latter doesn't exist
451 txpower="${txpower:-$vif_txpower}"
452 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
453
454 case "$mode" in
455 adhoc)
456 config_get bssid "$vif" bssid
457 config_get ssid "$vif" ssid
458 config_get beacon_int "$device" beacon_int
459 config_get basic_rate_list "$device" basic_rate
460 config_get encryption "$vif" encryption
461 config_get key "$vif" key 1
462 config_get mcast_rate "$vif" mcast_rate
463 config_get htmode "$device" htmode
464 case "$htmode" in
465 HT20|HT40+|HT40-) ;;
466 *) htmode= ;;
467 esac
468
469
470 local keyspec=""
471 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
472 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
473 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
474 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
475 # make sure this wifi interface won't accidentally stay open without encryption
476 ifconfig "$ifname" down
477 }
478 mac80211_start_vif "$vif" "$ifname"
479 continue
480 fi
481 }
482
483 [ "$encryption" == "wep" ] && {
484 case "$key" in
485 [1234])
486 local idx
487 for idx in 1 2 3 4; do
488 local ikey
489 config_get ikey "$vif" "key$idx"
490
491 [ -n "$ikey" ] && {
492 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
493 [ $idx -eq $key ] && ikey="d:$ikey"
494 append keyspec "$ikey"
495 }
496 done
497 ;;
498 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
499 esac
500 }
501
502 local br brval brsub brstr
503 [ -n "$basic_rate_list" ] && {
504 for br in $basic_rate_list; do
505 brval="$(($br / 1000))"
506 brsub="$((($br / 100) % 10))"
507 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
508 [ -n "$brstr" ] && brstr="$brstr,"
509 brstr="$brstr$brval"
510 done
511 }
512
513 local mcval=""
514 [ -n "$mcast_rate" ] && {
515 mcval="$(($mcast_rate / 1000))"
516 mcsub="$(( ($mcast_rate / 100) % 10 ))"
517 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
518 }
519
520 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
521 ${fixed:+fixed-freq} $bssid \
522 ${beacon_int:+beacon-interval $beacon_int} \
523 ${brstr:+basic-rates $brstr} \
524 ${mcval:+mcast-rate $mcval} \
525 ${keyspec:+keys $keyspec}
526 ;;
527 sta)
528 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
529 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
530 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
531 # make sure this wifi interface won't accidentally stay open without encryption
532 ifconfig "$ifname" down
533 continue
534 }
535 fi
536 ;;
537 esac
538 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
539 done
540
541 }
542
543
544 check_mac80211_device() {
545 config_get phy "$1" phy
546 [ -z "$phy" ] && {
547 find_mac80211_phy "$1" >/dev/null || return 0
548 config_get phy "$1" phy
549 }
550 [ "$phy" = "$dev" ] && found=1
551 }
552
553 detect_mac80211() {
554 devidx=0
555 config_load wireless
556 while :; do
557 config_get type "radio$devidx" type
558 [ -n "$type" ] || break
559 devidx=$(($devidx + 1))
560 done
561 for dev in $(ls /sys/class/ieee80211); do
562 found=0
563 config_foreach check_mac80211_device wifi-device
564 [ "$found" -gt 0 ] && continue
565
566 mode_11n=""
567 mode_band="g"
568 channel="11"
569 ht_cap=0
570 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
571 ht_cap="$(($ht_cap | $cap))"
572 done
573 ht_capab="";
574 [ "$ht_cap" -gt 0 ] && {
575 mode_11n="n"
576 append ht_capab " option htmode HT20" "$N"
577
578 list=" list ht_capab"
579 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
580 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
581 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
582 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
583 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
584 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
585 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
586 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
587 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
588 }
589 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
590
591 if [ -x /usr/bin/readlink ]; then
592 path="$(readlink -f /sys/class/ieee80211/${dev}/device)"
593 path="${path##/sys/devices/}"
594 dev_id=" option path '$path'"
595 else
596 dev_id=" option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)"
597 fi
598
599 cat <<EOF
600 config wifi-device radio$devidx
601 option type mac80211
602 option channel ${channel}
603 option hwmode 11${mode_11n}${mode_band}
604 $dev_id
605 $ht_capab
606 # REMOVE THIS LINE TO ENABLE WIFI:
607 option disabled 1
608
609 config wifi-iface
610 option device radio$devidx
611 option network lan
612 option mode ap
613 option ssid OpenWrt
614 option encryption none
615
616 EOF
617 devidx=$(($devidx + 1))
618 done
619 }
620