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

Archive Download this file



interactive