summaryrefslogtreecommitdiff
path: root/net/iwconfig.sh.BSD.in
blob: 3acaf6636745b21134750593d2ff7684c36aee88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
# Released under the 2-clause BSD license.

_config_vars="$_config_vars ssid mode associate_timeout preferred_aps"
_config_vars="$_config_vars blacklist_aps"

iwconfig_depend()
{
	program /sbin/ifconfig
	after plug
	before interface
	provide wireless
}

iwconfig_get_wep_status()
{
	local status="disabled"
	local mode=$(LC_ALL=C ifconfig "${IFACE}" \
	| sed -n -e 's/^[[:space:]]*authmode \([^ ]*\) privacy ON .*/\1/p')
	if [ -n "${mode}" ] ; then
		status="enabled - ${mode}"
	fi

	echo "(WEP ${status})"
}

_iwconfig_get()
{
	LC_ALL=C ifconfig "${IFACE}" | \
	sed -n -e "s/^[[:space:]]*ssid \(.*\) channel \([0-9]*\).* bssid \(..:..:..:..:..:..\)\$/\\$1/p"
}

_get_ssid()
{
	local ssid="$(_iwconfig_get 1)"
	# If the ssid has a space then it's wrapped in quotes. This is a
	# problem if the real ssid has a quote at the start or the end :/
	ssid=${ssid#\"}
	ssid=${ssid%\"}
	echo "${ssid}"
}

_get_ap_mac_address()
{
	_iwconfig_get 3
}

_get_channel()
{
	_iwconfig_get 2
}

iwconfig_report()
{
	local m="connected to"
	local ssid="$(_get_ssid)"
	local mac="$(_get_ap_mac_address "${iface}")"
	[ -n "${mac}" ] && mac=" at ${mac}"
	local wep_status="$(iwconfig_get_wep_status "${iface}")"
	local channel="$(_get_channel)"
	[ -n "${channel}" ] && channel="on channel ${channel} "

	eindent
	einfo "${IFACE} ${m} \"${ssid}\"${mac}"
	einfo "${channel}${wep_status}"
	eoutdent
}

iwconfig_get_wep_key()
{
	local mac="$1" key=
	[ -n "${mac}" ] && mac="$(echo "${mac}" | sed -e 's/://g')"
	eval key=\$mac_key_${mac}
	[ -z "${key}" ] && eval key=\$key_${SSIDVAR}
	echo "${key:--}"
}

iwconfig_user_config()
{
	local conf=
	eval set -- \$ifconfig_${SSIDVAR}
	for conf in "$@" ; do
		ifconfig "${IFACE}" ${conf}
	done
}

iwconfig_set_mode()
{
	local x= opt= unopt="hostap adhoc"
	case "$1" in
		master|hostap) unopt="adhoc" opt="hostap" ;;
		ad-hoc|adhoc) unopt="hostap" opt="adhoc" ;;
	esac
	for x in ${unopt} ; do
		ifconfig "${IFACE}" -mediaopt ${x}
	done
	for x in ${opt} ; do
		ifconfig "${IFACE}" mediaopt ${x}
	done
}

iwconfig_setup_specific()
{
	local mode="${1:-master}" channel=
	if [ -z "${SSID}" ]; then
		eerror "${IFACE} requires an SSID to be set to operate in ${mode} mode"
		eerror "adjust the ssid_${IFVAR} setting in /etc/conf.d/net"
		return 1
	fi

	iwconfig_set_mode "${mode}" || return 1

	SSIDVAR=$(shell_var "${SSID}")
	local key=$(iwconfig_get_wep_key)

	# Now set the key
	ifconfig "${IFACE}" wepkey "${key}"

	ifconfig "${IFACE}" ssid "${SSID}" || return 1

	eval channel=\$channel_${IFVAR}
	# We default the channel to 3
	ifconfig "${IFACE}" channel "${channel:-3}" || return 1

	iwconfig_user_config
	iwconfig_report "${iface}"
	return 0
}

iwconfig_associate()
{
	local mac="$1" channel="$2" caps="$3"
	local mode= w="(WEP Disabled)" key=

	SSIDVAR=$(shell_var "${SSID}")
	key=$(iwconfig_get_wep_key "${mac}")
	case "${caps}" in
		[EI]P*)
		if [ "${key}" = "-" ] ; then
			ewarn "WEP key is not set for \"${SSID}\""
			return 1
		fi
		;;
		"") ;;
		*)
		if [ "${key}" != "-" ] ; then
			key="-"
			ewarn "\"${SSID}\" is not WEP enabled"
		fi
		;;
	esac

	# Set mode accordingly
	case "${caps}" in
		*E*)
		mode="managed"
		if LC_ALL=C ifconfig "${IFACE}" | \
			grep -q "^[[:space:]]*media: .*adhoc" ; then
			ifconfig "${IFACE}" down -mediaopt adhoc up
		fi
		;;
		*I*)
		mode="adhoc"
		if ! LC_ALL=C ifconfig "${IFACE}" | \
			grep -q "^[[:space:]]*media: .*adhoc" ; then
			ifconfig "${IFACE}" down mediaopt adhoc up
		fi
		;;
		*)
		if LC_ALL=C ifconfig "${IFACE}" \
			| grep -q "^[[:space:]]*media: .*adhoc" ; then
			mode="adhoc"
		else
			mode="managed"
		fi
		;;
	esac

	if [ "${key}" = "-" ] ; then
		ifconfig "${IFACE}" wepmode off
	else
		ifconfig "${IFACE}" wepmode on
		ifconfig "${IFACE}" deftxkey 1
		w=$(iwconfig_get_wep_status)
	fi

	ebegin "Connecting to \"${SSID}\" in ${mode} mode ${w}"

	if ! ifconfig "${IFACE}" wepkey "${key}" ; then
		eerror "Invalid WEP key ${key}"
		return 1
	fi

	ifconfig "${IFACE}" ssid "${SSID}" || return 1
	iwconfig_user_config

	if [ "${SSID}" != "any" ] && type preassociate >/dev/null 2>/dev/null ; then
		veinfo "Running preassociate function"
		veindent
		( preassociate )
		local e=$?
		veoutdent
		if [ ${e} -eq 0 ] ; then
			veend 1 "preassociate \"${SSID}\" on ${IFACE} failed"
			return 1
		fi
	fi

	local timeout= i=0
	eval timeout=\$associate_timeout_${IFVAR}
	timeout=${timeout:-10}

	[ ${timeout} -eq 0 ] \
	&& vewarn "WARNING: infinite timeout set for association on ${IFACE}"

	while true; do
		_has_carrier && break
		sleep 1

		[ ${timeout} -eq 0 ] && continue
		: $(( i +=  1 ))
		[ ${i} -ge ${timeout} ] && { eend 1; return 1; }
	done

	_has_carrier || { eend 1; return 1; }
	eend 0

	if [ "${SSID}"  = "any" ]; then
		SSID="$(_get_ssid)"
		iwconfig_associate
		return $?
	fi

	iwconfig_report

	if type postassociate >/dev/null 2>/dev/null ; then
		veinfo "Running postassociate function"
		veindent
		( postassociate )
		veoutdent
	fi

	return 0
}

iwconfig_scan()
{
	local x= i=0 scan= quality=
	einfo "Scanning for access points"
	eindent

	scan="$(LC_ALL=C ifconfig -v "${IFACE}" list scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")"
	while [ ${i} -lt 3 -a -z "${scan}" ] ; do
		scan="${scan}${scan:+ }$(LC_ALL=C ifconfig -v "${IFACE}" scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")"
		: $(( i += 1 ))
	done

	APS=-1
	eval set -- ${scan}
	for line in "$@" ; do
		: $(( APS += 1 ))
		set -- ${line}
		while true ; do
			case "$1" in
				*:*:*:*:*:*) break ;;
			esac
			eval SSID_${APS}="\"\${SSID_${APS}}\${SSID_${APS}:+ }$1\""
			shift
		done
		eval MAC_${APS}="$(echo "$1" | tr '[:lower:]' '[:upper:]')"
		eval CHAN_${APS}="$2"
		quality=${4%:*}
		shift ; shift ; shift ; shift ; shift
		eval CAPS_${APS}="\"$*\""

		# Add 1000 for managed nodes as we prefer them to adhoc
		set -- $*
		case "$1" in
			*E*) eval QUAL_${APS}=$(( quality + 1000 )) ;;
			*)   eval QUAL_${APS}=\$quality ;;
		esac
	done

	if [ -z "${MAC_0}" ]; then
		ewarn "no access points found"
		eoutdent
		return 1
	fi

	# Sort based on quality
	local i=0 k=1 a= b= x= t=
	while [ ${i} -lt ${APS} ] ; do
		: $(( k = i + 1 ))
		while [ ${k} -le ${APS} ] ; do
			eval a=\$QUALITY_${i}
			[ -z "${a}" ] && break
			eval b=\$QUALITY_${k}
			if [ -n "${b}" -a "${a}" -lt "${b}" ] ; then
				for x in MAC SSID CHAN QUALITY CAPS ; do
					eval t=\$${x}_${i}
					eval ${x}_${i}=\$${x}_${k}
					eval ${x}_${k}=\$t
				done
			fi
			: $(( k += 1 ))
		done
		: $(( i += 1 ))
	done

	# Strip any duplicates
	local i=0 k=1 a= b=
	while [ ${i} -lt ${APS} ] ; do
		: $(( k = i + 1 ))
		while [ ${k} -le ${APS} ] ; do
			eval a=\$MAC_${i}
			eval b=\$MAC_${k}
			if [ "${a}" = "${b}" ] ; then
				eval a=\$QUALITY_${i}
				eval b=\$QUALITY_${k}
				if [ -n "${a}" -a -n "${b}" ] ; then
					if [ ${a} -ge ${b} ] ; then
						unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k}
					else
						unset MAC_${i} SSID_${i} CHAN_${i} QUALITY_${i} CAPS_${i}
					fi
				else
					unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k}
				fi
			fi
			: $(( k += 1 ))
		done
		: $(( i += 1 ))
	done

	local i=0 e= m= s=

	while [ ${i} -le ${APS} ] ; do
		eval x=\$MAC_${i}
		if [ -z "${x}" ] ; then
			: $(( i += 1 ))
			continue
		fi

		eval m=\$MODE_${i}
		[ -n "${m}" ] && m=", ${m}"
		eval s=\$SSID_${i}
		eval q=\$QUALITY_${i}
		eval e=\$CAPS_${i}
		case "${e}" in
			[EI]P*) e=", encrypted" ;;
			*) e="" ;;
		esac
		if [ -z "${s}" ] ; then
			einfo "Found ${x}${m}${e}"
		else
			einfo "Found \"${s}\" at ${x}${m}${e}"
		fi

		x="$(echo "${x}" | sed -e 's/://g')"
		eval x=\$mac_ssid_${x}
		if [ -n "${x}" ] ; then
			eval SSID_${i}=\$x
			s=${x}
			eindent
			einfo "mapping to \"${x}\""
			eoutdent
		fi

		eval set -- $(_flatten_array "blacklist_aps_${IFVAR}")
		[ $# = 0 ] && eval set -- $(_flatten_array "blacklist_aps")
		for x; do
			if [ "${x}" = "${s}" ] ; then
				ewarn "${s} has been blacklisted - not connecting"
				unset SSID_${i} MAC_${i} CHAN_${i} QUALITY_${i} CAPS_${i}
			fi
		done
		: $(( i += 1 ))
	done
	eoutdent
	return 0
}

iwconfig_force_preferred()
{
	eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
	[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
	[ $# = 0 ] && return 1

	ewarn "Trying to force preferred in case they are hidden"
	local ssid=
	for ssid; do
		local found_AP=false i=0 e=
		while [ ${i} -le ${APS:--1} ] ; do
			eval e=\$SSID_${i}
			if [ "${e}" = "${ssid}" ] ; then
				found_AP=true
				break
			fi
			: $(( i += 1 ))
		done
		if ! ${found_AP} ; then
			SSID=${ssid}
			iwconfig_associate && return 0
		fi
	done

	ewarn "Failed to associate with any preferred access points on ${IFACE}"
	return 1
}

iwconfig_connect_preferred()
{
	eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
	[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
	[ $# = 0 ] && return 1

	local ssid= i= mode= mac= caps= freq= chan=
	for ssid; do
		i=0
		while [ ${i} -le ${APS} ]  ; do
			eval e=\$SSID_${i}
			if [ "${e}" = "${ssid}" ] ; then
				SSID=${e}
				eval mac=\$MAC_${i}
				eval caps=\$CAPS_${i}
				eval freq=\$FREQ_${i}
				eval chan=\$CHAN_${i}
				iwconfig_associate "${mac}" \
				"${chan}" "${caps}" && return 0
			fi
			: $(( i += 1 ))
		done
	done

	return 1
}

iwconfig_connect_not_preferred()
{
	local ssid= i=0 mode= mac= caps= freq= chan= pref=

	while [ ${i} -le ${APS} ] ; do
		eval e=\$SSID_${i}
		if [ -z "${e}" ] ; then
			: $(( i += 1 ))
			continue
		fi

		eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
		[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
		pref=false
		for ssid; do
			if [ "${e}" = "${ssid}" ] ; then
				pref=true
				break
			fi
		done

		if ! ${pref} ; then
			SSID=${e}
			eval mac=\$MAC_${i}
			eval caps=\$CAPS_${i}
			eval freq=\$FREQ_${i}
			eval chan=\$CHAN_${i}
			iwconfig_associate "${mac}"  \
			"${chan}" "${caps}" && return 0
		fi
		: $(( i += 1 ))
	done

	return 1
}

iwconfig_defaults()
{
	# Set some defaults
	#ifconfig "${iface}" txpower 100 2>/dev/null
	ifconfig "${IFACE}" bssid -
	ifconfig "${IFACE}" ssid -
	ifconfig "${IFACE}" wepkey 1:- wepkey 2:- wepkey 3:- wepkey 4:-
	ifconfig "${IFACE}" authmode open
	ifconfig "${IFACE}" -mediaopt adhoc
	ifconfig "${IFACE}" -mediaopt hostap
}

iwconfig_configure()
{
	local x= APS=-1
	eval SSID=\$ssid_${IFVAR}

	# Setup ad-hoc mode?
	eval x=\$mode_${IFVAR}
	x=${x:-managed}
	case "${x}" in
		ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;;
	esac

	if [ "${x}" != "managed" -a "${x}" != "auto" -a "${x}" != "ad-hoc" -a "${x}" != "adhoc" -a ${x} != "master" ] ; then
		eerror "Only managed, ad-hoc, master and auto modes are supported"
		return 1
	fi

	# Has an SSID been forced?
	if [ -n "${SSID}" ]; then
		iwconfig_set_mode "${x}"
		iwconfig_associate && return 0
		[ "${SSID}" = "any" ] && iwconfig_force_preferred && return 0

		eval SSID=\$adhoc_ssid_${IFVAR}
		if [ -n "${SSID}" ]; then
			iwconfig_setup_specific adhoc
			return $?
		fi
		return 1
	fi

	# Are we forcing preferred only?
	eval x=\$associate_order_${IFVAR}
	[ -n "${x}" ] && associate_order=${x}
	associate_order=${associate_order:-any}
	if [ "${associate_order}" = "forcepreferredonly" ]; then
		iwconfig_force_preferred && return 0
	else
		iwconfig_scan || return 1
		iwconfig_connect_preferred && return 0
		[ "${associate_order}" = "forcepreferred" ] || \
		[ "${associate_order}" = "forceany" ] && \
		iwconfig_force_preferred && return 0
		[ "${associate_order}" = "any" ] || \
		[ "${associate_order}" = "forceany" ] && \
		iwconfig_connect_not_preferred && return 0
	fi

	e="associate with"
	[ -z "${MAC_0}" ] && e="find"
	[ "${preferred_aps}" = "force" ] || \
	[ "${preferred_aps}" = "forceonly" ] && \
	e="force"
	e="Couldn't ${e} any access points on ${IFACE}"

	eval SSID=\$adhoc_ssid_${IFVAR}
	if [ -n "${SSID}" ]; then
		ewarn "${e}"
		iwconfig_setup_specific adhoc
		return $?
	fi

	eerror "${e}"
	return 1
}

iwconfig_pre_start()
{
	# We don't configure wireless if we're being called from
	# the background
	yesno ${IN_BACKGROUND} && return 0

	service_set_value "SSID" ""
	_exists || return 0

	if ! _is_wireless ; then
		veinfo "${IFACE} is not wireless"
		return 0
	fi

	iwconfig_defaults
	iwconfig_user_config

	# Set the base metric to be 2000
	metric=2000

	einfo "Configuring wireless network for ${IFACE}"

	if iwconfig_configure ; then
		service_set_value "SSID" "${SSID}"
		return 0
	fi

	eerror "Failed to configure wireless for ${IFACE}"
	iwconfig_defaults
	#iwconfig "${IFACE}" txpower 0 2>/dev/null
	unset SSID SSIDVAR
	_down
	return 1
}

iwconfig_post_stop()
{
	yesno ${IN_BACKGROUND} && return 0
	_is_wireless || return 0
	iwconfig_defaults
	#iwconfig "${IFACE}" txpower 0 2>/dev/null
}