allow mac80211 devices to be configured to do 802.11s, requires iw
[openwrt/openwrt.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 scan_mac80211() {
5 local device="$1"
6 local adhoc sta ap
7
8 config_get vifs "$device" vifs
9 for vif in $vifs; do
10
11 config_get ifname "$vif" ifname
12 config_set "$vif" ifname "${ifname:-$device}"
13
14 config_get mode "$vif" mode
15 case "$mode" in
16 adhoc|sta|ap|monitor|mesh)
17 append $mode "$vif"
18 ;;
19 *) echo "$device($vif): Invalid mode, ignored."; continue;;
20 esac
21 done
22
23 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }${monitor:+$monitor }${mesh:+$mesh}"
24 }
25
26
27 disable_mac80211() (
28 local device="$1"
29
30 set_wifi_down "$device"
31 # kill all running hostapd and wpa_supplicant processes that
32 # are running on atheros/mac80211 vifs
33 for pid in `pidof hostapd wpa_supplicant`; do
34 grep wlan /proc/$pid/cmdline >/dev/null && \
35 kill $pid
36 done
37
38 include /lib/network
39 cd /proc/sys/net
40 for dev in *; do
41 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
42 ifconfig "$dev" down
43 unbridge "$dev"
44 }
45 done
46 return 0
47 )
48
49 enable_mac80211() {
50 local device="$1"
51 config_get channel "$device" channel
52 config_get vifs "$device" vifs
53 config_get txpower "$device" txpower
54
55 local first=1
56 local mesh_idx=0
57 for vif in $vifs; do
58 ifconfig "$ifname" down 2>/dev/null
59 config_get ifname "$vif" ifname
60 config_get enc "$vif" encryption
61 config_get eap_type "$vif" eap_type
62 config_get mode "$vif" mode
63
64 config_get ifname "$vif" ifname
65 [ $? -ne 0 ] && {
66 echo "enable_mac80211($device): Failed to set up $mode vif $ifname" >&2
67 continue
68 }
69 config_set "$vif" ifname "$ifname"
70
71 [ "$first" = 1 ] && {
72 # only need to change freq band and channel on the first vif
73 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
74 if [ "$mode" = adhoc ]; then
75 iwlist "$ifname" scan >/dev/null 2>/dev/null
76 sleep 1
77 iwconfig "$ifname" mode ad-hoc >/dev/null 2>/dev/null
78 fi
79 # mesh interface should be created only for the first interface
80 if [ "$mode" = mesh ]; then
81 config_get mesh_id "$vif" mesh_id
82 if [ -n "$mesh_id" ]; then
83 iw dev "$ifname" interface add msh$mesh_idx type mp mesh_id $mesh_id
84 fi
85 fi
86 sleep 1
87 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
88 }
89 if [ "$mode" = sta ]; then
90 iwconfig "$ifname" mode managed >/dev/null 2>/dev/null
91 else
92 iwconfig "$ifname" mode $mode >/dev/null 2>/dev/null
93 fi
94
95 wpa=
96 case "$enc" in
97 WEP|wep)
98 for idx in 1 2 3 4; do
99 config_get key "$vif" "key${idx}"
100 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
101 done
102 config_get key "$vif" key
103 key="${key:-1}"
104 case "$key" in
105 [1234]) iwconfig "$ifname" enc "[$key]";;
106 *) iwconfig "$ifname" enc "$key";;
107 esac
108 ;;
109 PSK|psk|PSK2|psk2)
110 config_get key "$vif" key
111 ;;
112 esac
113
114 case "$mode" in
115 adhoc)
116 config_get addr "$vif" bssid
117 [ -z "$addr" ] || {
118 iwconfig "$ifname" ap "$addr"
119 }
120 ;;
121 esac
122 config_get ssid "$vif" ssid
123
124 config_get vif_txpower "$vif" txpower
125 # use vif_txpower (from wifi-iface) to override txpower (from
126 # wifi-device) if the latter doesn't exist
127 txpower="${txpower:-$vif_txpower}"
128 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
129
130 config_get frag "$vif" frag
131 if [ -n "$frag" ]; then
132 iwconfig "$ifname" frag "${frag%%.*}"
133 fi
134
135 config_get rts "$vif" rts
136 if [ -n "$rts" ]; then
137 iwconfig "$ifname" rts "${rts%%.*}"
138 fi
139
140 ifconfig "$ifname" up
141 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
142
143 local net_cfg bridge
144 net_cfg="$(find_net_config "$vif")"
145 [ -z "$net_cfg" ] || {
146 bridge="$(bridge_interface "$net_cfg")"
147 config_set "$vif" bridge "$bridge"
148 start_net "$ifname" "$net_cfg"
149 }
150 iwconfig "$ifname" essid "$ssid"
151 set_wifi_up "$vif" "$ifname"
152 case "$mode" in
153 ap)
154 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
155 hostapd_setup_vif "$vif" nl80211 || {
156 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
157 # make sure this wifi interface won't accidentally stay open without encryption
158 ifconfig "$ifname" down
159 continue
160 }
161 fi
162 ;;
163 sta)
164 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
165 wpa_supplicant_setup_vif "$vif" wext || {
166 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
167 # make sure this wifi interface won't accidentally stay open without encryption
168 ifconfig "$ifname" down
169 continue
170 }
171 fi
172 ;;
173 mesh)
174 # special case where physical interface should be down for mesh to work
175 ifconfig "$ifname" down
176 ifconfig "msh$mesh_idx" up
177 ;;
178 esac
179 first=0
180 mesh_idx=$(expr $mesh_idx + 1)
181 done
182 }
183
184
185 detect_mac80211() {
186 cd /sys/class/net
187 for dev in $(ls -d wlan* 2>&-); do
188 config_get type "$dev" type
189 [ "$type" = mac80211 ] && return
190 cat <<EOF
191 config wifi-device $dev
192 option type mac80211
193 option channel 5
194
195 # REMOVE THIS LINE TO ENABLE WIFI:
196 option disabled 1
197
198 config wifi-iface
199 option device $dev
200 option network lan
201 option mode ap
202 option ssid OpenWrt
203 option encryption none
204 EOF
205 done
206 }