Root/package/mac80211/files/lib/wifi/mac80211.sh

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

Archive Download this file



interactive