5d4fcee2713206697a4484652cb9fe0979b304d1
[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 monitor mesh
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 }${sta:+$sta }${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 wifi_fixup_hwmode "$device" "g"
58 for vif in $vifs; do
59 ifconfig "$ifname" down 2>/dev/null
60 config_get ifname "$vif" ifname
61 config_get enc "$vif" encryption
62 config_get eap_type "$vif" eap_type
63 config_get mode "$vif" mode
64
65 config_get ifname "$vif" ifname
66 [ $? -ne 0 ] && {
67 echo "enable_mac80211($device): Failed to set up $mode vif $ifname" >&2
68 continue
69 }
70 config_set "$vif" ifname "$ifname"
71
72 [ "$first" = 1 ] && {
73 # only need to change freq band and channel on the first vif
74 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
75 if [ "$mode" = adhoc ]; then
76 iwlist "$ifname" scan >/dev/null 2>/dev/null
77 sleep 1
78 iwconfig "$ifname" mode ad-hoc >/dev/null 2>/dev/null
79 fi
80 # mesh interface should be created only for the first interface
81 if [ "$mode" = mesh ]; then
82 config_get mesh_id "$vif" mesh_id
83 if [ -n "$mesh_id" ]; then
84 iw dev "$ifname" interface add msh$mesh_idx type mp mesh_id $mesh_id
85 fi
86 fi
87 sleep 1
88 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
89 }
90 if [ "$mode" = sta ]; then
91 iwconfig "$ifname" mode managed >/dev/null 2>/dev/null
92 else
93 iwconfig "$ifname" mode $mode >/dev/null 2>/dev/null
94 fi
95
96 wpa=
97 case "$enc" in
98 WEP|wep)
99 for idx in 1 2 3 4; do
100 config_get key "$vif" "key${idx}"
101 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
102 done
103 config_get key "$vif" key
104 key="${key:-1}"
105 case "$key" in
106 [1234]) iwconfig "$ifname" enc "[$key]";;
107 *) iwconfig "$ifname" enc "$key";;
108 esac
109 ;;
110 PSK|psk|PSK2|psk2)
111 config_get key "$vif" key
112 ;;
113 esac
114
115 case "$mode" in
116 adhoc)
117 config_get addr "$vif" bssid
118 [ -z "$addr" ] || {
119 iwconfig "$ifname" ap "$addr"
120 }
121 ;;
122 esac
123 config_get ssid "$vif" ssid
124
125 config_get vif_txpower "$vif" txpower
126 # use vif_txpower (from wifi-iface) to override txpower (from
127 # wifi-device) if the latter doesn't exist
128 txpower="${txpower:-$vif_txpower}"
129 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
130
131 config_get frag "$vif" frag
132 if [ -n "$frag" ]; then
133 iwconfig "$ifname" frag "${frag%%.*}"
134 fi
135
136 config_get rts "$vif" rts
137 if [ -n "$rts" ]; then
138 iwconfig "$ifname" rts "${rts%%.*}"
139 fi
140
141 ifconfig "$ifname" up
142 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
143
144 local net_cfg bridge
145 net_cfg="$(find_net_config "$vif")"
146 [ -z "$net_cfg" ] || {
147 bridge="$(bridge_interface "$net_cfg")"
148 config_set "$vif" bridge "$bridge"
149 start_net "$ifname" "$net_cfg"
150 }
151 iwconfig "$ifname" essid "$ssid"
152 set_wifi_up "$vif" "$ifname"
153 case "$mode" in
154 ap)
155 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
156 hostapd_setup_vif "$vif" nl80211 || {
157 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
158 # make sure this wifi interface won't accidentally stay open without encryption
159 ifconfig "$ifname" down
160 continue
161 }
162 fi
163 ;;
164 sta)
165 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
166 wpa_supplicant_setup_vif "$vif" wext || {
167 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
168 # make sure this wifi interface won't accidentally stay open without encryption
169 ifconfig "$ifname" down
170 continue
171 }
172 fi
173 ;;
174 mesh)
175 # special case where physical interface should be down for mesh to work
176 ifconfig "$ifname" down
177 ifconfig "msh$mesh_idx" up
178 iwlist msh$mesh_idx scan 2>/dev/null >/dev/null
179 ;;
180 esac
181 first=0
182 mesh_idx=$(expr $mesh_idx + 1)
183 done
184 }
185
186
187 detect_mac80211() {
188 cd /sys/class/net
189 for dev in $(ls -d wlan* 2>&-); do
190 config_get type "$dev" type
191 [ "$type" = mac80211 ] && return
192 cat <<EOF
193 config wifi-device $dev
194 option type mac80211
195 option channel 5
196
197 # REMOVE THIS LINE TO ENABLE WIFI:
198 option disabled 1
199
200 config wifi-iface
201 option device $dev
202 option network lan
203 option mode ap
204 option ssid OpenWrt
205 option encryption none
206 EOF
207 done
208 }
This page took 0.054547 seconds and 3 git commands to generate.