]> git.pld-linux.org Git - projects/rc-scripts.git/blame - sysconfig/network-scripts/functions.network
Revert previous commit. Fix is_module instead to do not match partial module names.
[projects/rc-scripts.git] / sysconfig / network-scripts / functions.network
CommitLineData
356d834b 1#!/bin/sh - keep it for file(1) to get bourne shell script result
b0443108 2#
ec8b15cb 3# $Id$
5e6dfc29 4#
7e04fe0e 5# This is not a shell script; it provides functions to network scripts
6# that source it.
7
8source_config ()
9{
944d6d80 10 local foundconfig=0
d30d98d4 11
01e7134e
ER
12 DEVNAME=${CONFIG##*/}
13 DEVNAME=${DEVNAME##ifcfg-}
14 DEVNAME=${DEVNAME##tnlcfg-}
5e6dfc29 15
1377630a 16 if [[ "$CONFIG" = /* ]]; then
d36ee0fb 17 if [ -f "$CONFIG" ]; then
5e6dfc29 18 . "$CONFIG"
d30d98d4 19 foundconfig=1
5e6dfc29 20 fi
d36ee0fb 21 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ]; then
5e6dfc29 22 . "/etc/sysconfig/interfaces/$CONFIG"
d30d98d4 23 foundconfig=1
5e6dfc29 24 fi
d100991e
ER
25
26 # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
27 # and forgot to alter DEVICE= line you won't accidentally bring down eth0
d30d98d4
AM
28 # while executing ifdown eth1. We do that only if configuration file exists
29 # (sometimes ifcfg-xyz isn't needed at all like server-side pppoe pppX interfaces)
8413b103 30 if [ "$foundconfig" -eq "1" -a "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
d100991e
ER
31 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
32 exit 1
33 fi
34
8d92f4e5 35 if [ "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
5e6dfc29
JR
36 IPADDR="$IPADDR/$PREFIX"
37 fi
7e04fe0e 38}
39
40do_netreport ()
41{
5e6dfc29
JR
42 # Notify programs that have requested notification
43 ( cd /var/run/netreport || exit
ac2433c4 44 for i in *; do
5e6dfc29
JR
45 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
46 rm -f $i >/dev/null 2>&1
47 done
48 )
7e04fe0e 49}
50
51need_hostname()
52{
5411f833 53 local hostname=$(hostname)
ac2433c4 54 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o "$hostname" = "localhost.localdomain" ]; then
5e6dfc29
JR
55 NEEDHOSTNAME=yes
56 else
57 unset NEEDHOSTNAME
58 fi
7e04fe0e 59}
60
61set_hostname()
62{
944d6d80 63 local hostname domain
5e6dfc29
JR
64
65 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
66 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
67
8d92f4e5 68 if [ "$hostname" ]; then
5e6dfc29
JR
69 echo "$hostname" > /etc/HOSTNAME
70 hostname $hostname
71 fi
72
8d92f4e5 73 if [ "$domain" ]; then
ac2433c4
ER
74 # XXX umask and possible /etc/resolv.conf symlink breakage
75 > /etc/resolv.conf.new
5937edff 76 if ! grep -q "search $domain" /etc/resolv.conf; then
5e6dfc29
JR
77 echo "search $domain" >> /etc/resolv.conf.new
78 fi
79 echo "domain $domain" >> /etc/resolv.conf.new
80 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
81 mv -f /etc/resolv.conf.new /etc/resolv.conf
82 fi
7e04fe0e 83}
84
f1dba8bc
ER
85set_dhcpclient()
86{
87 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
88 DHCP_CLIENT=/sbin/pump
89 fi
90
91 if [ -z "$DHCP_CLIENT" ]; then
f3df266c
JK
92 if [ "$BOOTPROTO" = "zeroconf" -a -x /usr/sbin/avahi-autoipd ]; then
93 DHCP_CLIENT=/usr/sbin/avahi-autoipd
94 elif [ -x /sbin/dhcpcd ]; then
f1dba8bc
ER
95 DHCP_CLIENT=/sbin/dhcpcd
96 elif [ -x /sbin/dhclient ]; then
97 DHCP_CLIENT=/sbin/dhclient
98 elif [ -x /sbin/dhcpxd ]; then
99 DHCP_CLIENT=/sbin/dhcpxd
100 elif [ -x /sbin/pump ]; then
101 DHCP_CLIENT=/sbin/pump
f3df266c
JK
102 elif [ "$BOOTPROTO" = "auto" -a -x /usr/sbin/avahi-autoipd ]; then
103 DHCP_CLIENT=/usr/sbin/avahi-autoipd
f1dba8bc 104 else
ac7078fd 105 nls "Can't find a DHCP client."
f1dba8bc
ER
106 exit 1
107 fi
108 fi
109}
110
767c8707
AM
111# Setup Network Address Translation (NAT)
112setup_nat()
113{
944d6d80 114 local src via dst
5e6dfc29
JR
115
116 if [ -r /etc/sysconfig/static-nat ]; then
117 if [ "$1" = "on" ]; then
118 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
119 /sbin/ip route add nat $dst via $src
120 /sbin/ip rule add from $src nat $dst
121 done
122 elif [ "$1" = "off" ]; then
a51d5138 123 LC_ALL=C /sbin/ip route show table all | grep -E "^nat[[:blank:]]" | while read nat dst via src args; do
5e6dfc29
JR
124 /sbin/ip rule del from $src nat $dst
125 /sbin/ip route del nat $dst via $src
126 done
127 fi
128 fi
767c8707
AM
129}
130
fa8aca70
JR
131# Setup static ARP
132static_arp()
7e04fe0e 133{
944d6d80 134 local rc arpdev host hwaddr flags neflags
5e6dfc29
JR
135
136 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
137 if [ -x /sbin/arp ]; then
138 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
139 else
140 show "Setting static ARP entries"; busy
141 rc=0
142 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
143 if [ -z "$arpdev" ]; then
144 rc=1
145 else
146 # ip supports only ip addresses
147 grep "^[0-9]" /etc/ethers | \
148 while read host hwaddr flags; do
149 case "$flags" in
150 *temp*)
151 neflags="nud stale"
152 ;;
153 *)
154 neflags="nud permanent"
155 ;;
156 esac
157 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
158 rc=1
159 fi
160 done
161 fi
162 if [ "$rc" -gt 0 ]; then
163 fail
164 fi
0a6161f2 165 fi
74a7f2e4 166 fi
7e04fe0e 167}
168
fa8aca70 169static_rarp()
7e04fe0e 170{
5e6dfc29
JR
171 if is_yes "$STATIC_RARP"; then
172 if [ ! -e /proc/net/rarp ]; then
170103c8 173 _modprobe single rarp
5e6dfc29
JR
174 fi
175 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
176 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
177 fi
178 fi
e9e86175
AM
179}
180
fa8aca70
JR
181# Set up all IP && IP parameter variables
182setup_ip_param ()
38198f50 183{
5e6dfc29
JR
184 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
185 if [ -z "$DEVICETYPE" ]; then
14a0605b 186 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[\.:]?[0-9]*[\.:]?[0-9]*$/,NUL); print $0 } ')
5e6dfc29
JR
187 fi
188
189 # Setup DEVICETYPE for special cases.
a51d5138 190 if echo ${DEVICE} | LC_ALL=C grep -qE '^[a-z0-9]+\.[0-9]+$'; then
5e6dfc29
JR
191 DEVICETYPE=vlan
192 fi
193
194 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
a51d5138 195 SUBDEVICE=$(echo "$DEVICE" | grep -E "([0-9]+:[0-9]+)")
5e6dfc29
JR
196 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
197
198 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
19196eca
ER
199 # check if ipaddr doesn't contain network length -- use $NETMASK then
200 if [[ "$IP4ADDR" != */* ]] && [ "$NETMASK" ]; then
201 IP4ADDR=$IP4ADDR/$(calcprefix $NETMASK)
202 fi
203
204 # check if we have ipv6 or ipv4 address
205 if [[ "${IP4ADDR}" = *:* ]]; then
5e6dfc29
JR
206 IP6ADDR=${IP4ADDR}
207 IP4ADDR=""
208 else
209 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
210 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
211 fi
d36ee0fb 212 if [ "${IP6_PRIM_IF}" ]; then
5e6dfc29
JR
213 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
214 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
440a9ecf 215 eval IP6ADDRLABEL="\$IP_LABEL${IP6_PRIM_IF:-}"
5e6dfc29 216 fi
d36ee0fb 217 if [ "${IP4_SRC_IF}" ]; then
5e6dfc29
JR
218 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
219 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
220 fi
5e6dfc29
JR
221
222 # new rc-scripts 0.4.x option
223 if [ -z "$HANDLING" ]; then
224 HANDLING=0
225 fi
226
227 # set handling for bridge
228 case "$DEVICETYPE" in
229 br|atm|lec|irda|vlan)
230 HANDLING=1
231 ;;
232 esac
74a7f2e4 233
5e6dfc29
JR
234 # Multicast ready devices
235 if is_yes "$MULTICAST"; then
236 MULTICAST="on"
237 else
238 case "$DEVICETYPE" in
239 eth|br)
240 MULTICAST="on"
241 ;;
242 *)
243 MULTICAST="off"
244 ;;
245 esac
246 fi
247
248 # ARP ready devices
d36ee0fb 249 if [ "$ARP" ]; then
5e6dfc29
JR
250 if is_yes "$ARP"; then
251 ARP="arp on"
252 else
253 ARP="arp off"
254 fi
255 fi
38198f50
AM
256}
257
fa8aca70 258check_device_down ()
38198f50 259{
ac7078fd 260 if LC_ALL=C ip link show dev ${DEVICE} 2> /dev/null | grep -q UP; then
5e6dfc29
JR
261 return 1
262 else
263 return 0
264 fi
38198f50
AM
265}
266
5e6dfc29 267setup_ip_gw_ro ()
38198f50 268{
5e6dfc29
JR
269 # IPv4 gateway
270 if is_yes "$IPV4_NETWORKING"; then
271 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
272 # set up default gateway
8d92f4e5 273 if [ "${GATEWAY}" ]; then
b6fb522d
ER
274 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
275 if [ $? = 0 ]; then
276 DEFGW=${GATEWAY}
277 else
278 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
279 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
280 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
281 fi
5e6dfc29
JR
282 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
283 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
284 fi
285 fi
286 fi
287
288 # IPv6 gateway && IPv6 globalroute
289 if is_yes "$IPV6_NETWORKING"; then
290 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
291 # set up default gateway
8d92f4e5 292 if [ "${GATEWAY6}" ]; then
5e6dfc29
JR
293 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
294 DEFGW6=${GATEWAY6}
295 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
296 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
297 fi
298 fi
299 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
300 # set up default route
8d92f4e5 301 if [ "${IPV6_GLOBALROUTEGW}" ]; then
5e6dfc29
JR
302 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
303 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
304 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
305 fi
306 fi
307 fi
0a6161f2 308}
309
fa8aca70
JR
310#
311# following function sets up routes not associated to any device
312# eg. unreachable or blackhole routes
ec8b15cb 313# (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
fa8aca70
JR
314#
315setup_routes()
0a6161f2 316{
944d6d80 317 local args
5e6dfc29
JR
318
319 if [ -f /etc/sysconfig/static-routes ]; then
d36ee0fb 320 if [ "$1" = "on" -o "$1" = "yes" ]; then
a51d5138 321 grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
5e6dfc29
JR
322 /sbin/ip route add $args
323 done
324 else
a51d5138 325 grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
5e6dfc29
JR
326 /sbin/ip route del $args 2>/dev/null
327 done
328 fi
329 fi
0a6161f2 330}
38198f50 331
fa8aca70
JR
332# Add ONLY IPv4 address (IPv6 address is added automaticly)
333set_up_loopback()
0a6161f2 334{
5e6dfc29
JR
335 if is_yes "$IPV4_NETWORKING"; then
336 ip addr add 127.0.0.1/8 dev lo
337 fi
338 ip link set dev lo up
fa8aca70
JR
339}
340
341set_down_loopback()
342{
ac7078fd 343 LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
5e6dfc29 344 ip link set dev lo down
0a6161f2 345}
346
347modprobe_net()
348{
63d30ecf
AM
349 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
350 _modprobe single ipv6
5e6dfc29 351 fi
fa8aca70 352
63d30ecf
AM
353 if is_yes "$IPX" && is_module "ipx"; then
354 _modprobe single ipx
5e6dfc29 355 fi
e016cdae 356}
7e04fe0e 357
50cd434b 358# calculate network prefix from given network mask
fa8aca70
JR
359calcprefix()
360{
944d6d80 361 local old_IFS netmask prefix endp
50cd434b
JR
362
363 old_IFS=$IFS
364 IFS='.'
365 netmask=$(echo $1)
366 IFS=$old_IFS
5e6dfc29
JR
367
368 prefix=0
50cd434b 369 endp=0
5937edff
TP
370 for n in $netmask; do
371 for i in 128 64 32 16 8 4 2 1; do
50cd434b
JR
372 if [ $(($n & $i)) -ne 0 ]; then
373 if [ $endp -eq 0 ]; then
374 prefix=$(($prefix + 1))
375 else
376 echo "32"
377 return
378 fi
379 else
380 endp=1
381 fi
382 done
5e6dfc29 383 done
50cd434b 384 echo $prefix
490f6163 385}
85ebccea 386
08ad08db 387# calculate network mask from given prefix
5379c9d4 388# (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
fa8aca70
JR
389calcnetmask()
390{
944d6d80 391 local prefix a MASK
5e6dfc29
JR
392
393 MASK=""
8d92f4e5 394 if [ "${1##[0-9]*}" ]; then
5e6dfc29
JR
395 prefix=32
396 else
944d6d80 397 prefix=$1
5e6dfc29
JR
398 fi
399 for i in 1 2 3 4; do
400 case $prefix in
5937edff
TP
401 7) a=254;;
402 6) a=252;;
403 5) a=248;;
404 4) a=240;;
405 3) a=224;;
406 2) a=192;;
407 1) a=128;;
5e6dfc29
JR
408 *)
409 [ $prefix -ge 8 ] && a=255
410 [ $prefix -le 0 ] && a=0
411 ;;
412 esac
413
414 prefix="$(( $prefix - 8))"
415 if [ -z "$MASK" ]; then
416 MASK=$a
417 else
418 MASK=$MASK.$a
419 fi
420 done
421
422 echo $MASK
423 return
5379c9d4 424}
47dbf68e 425
dd8b6525 426# Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
5e6dfc29
JR
427get_ppp_device_and_pid ()
428{
d36ee0fb 429 if [ -f "/var/run/ppp-$DEVNAME.pid" ]; then
bf14fcab
JR
430 eval $(
431 {
5937edff
TP
432 read PID; echo "PID='$PID'"
433 read REALDEVICE; echo "REALDEVICE=$REALDEVICE"
bf14fcab 434 } < "/var/run/ppp-$DEVNAME.pid")
5e6dfc29
JR
435 fi
436
d36ee0fb 437 if [ -z "$REALDEVICE" ]; then
5e6dfc29
JR
438 REALDEVICE=$DEVICE
439 fi
dd8b6525 440}
c3bd5d9f 441
e686ef49 442# following function setups advanced routing rules
ec8b15cb 443# Olgierd Pieczul <wojrus@pld-linux.org>
5e6dfc29
JR
444setup_ip_rules ()
445{
944d6d80 446 local args prio from src
5e6dfc29 447
d36ee0fb 448 if [ -f /etc/sysconfig/static-routes ]; then
5e6dfc29 449 if is_yes "$1"; then
a51d5138 450 grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
5e6dfc29
JR
451 /sbin/ip rule add $args
452 done
453 elif is_no "$1"; then
a51d5138 454 LC_ALL=C /sbin/ip rule show | grep -vE -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
2eb86030 455 [ "$src" = "all" ] && ip rule delete $args || ip rule delete $from $src $args
5e6dfc29
JR
456 done
457 fi
458 fi
e686ef49 459}
6d145c04 460
5e6dfc29
JR
461is_wireless_device ()
462{
463 if [ -x /sbin/iwconfig ]; then
464 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
465 fi
466 return 1
a6c22d3d
AM
467}
468
7a4d5330 469wireless_param()
5e6dfc29
JR
470{
471 device="$1"
472 if [ "$2" = "br" ]; then
473 prefix="${device}_"
474 else
475 prefix=""
476 fi
477
7a4d5330
ER
478 local val wlan_mode
479
5e6dfc29 480 # wlan mode needs to be first
8d92f4e5 481 wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
7a4d5330
ER
482 iwconfig ${device} mode "$wlan_mode"
483
df3e1a2c
AM
484 val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ "$val" ] && \
485 iwconfig ${device} nick "$val"
7a4d5330 486
8d92f4e5 487 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
7a4d5330
ER
488 iwconfig ${device} nwid "$val"
489
009113c6 490 if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
8d92f4e5 491 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
7a4d5330 492 iwconfig ${device} freq "$val"
8d92f4e5 493 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
7a4d5330
ER
494 iwconfig ${device} channel "$val"
495 fi
5e6dfc29 496 fi
7a4d5330 497
8d92f4e5 498 val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
7a4d5330 499 iwconfig ${device} sens "$val"
8d92f4e5 500 val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
7a4d5330 501 iwconfig ${device} ap "$val"
8d92f4e5 502 val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
7a4d5330 503 iwconfig ${device} rate "$val"
8d92f4e5 504 val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
7a4d5330 505 iwconfig ${device} rts "$val"
8d92f4e5 506 val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
7a4d5330
ER
507 iwconfig ${device} frag "$val"
508
b9d6440a 509 local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
334325e1
ER
510 local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
511 # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
8d92f4e5 512 if [ "$wlan_key" ]; then
334325e1
ER
513 # wlan key can contain index of key to set
514 # to set other than current keys suffix with 1-4:
515 local idx
516 for idx in 1 2 3 4; do
517 val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
518 [ "$val" ] || continue
f0f3583f 519 iwconfig ${device} key "[$idx]" "$val"
334325e1
ER
520 done
521 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
522 iwconfig ${device} key "[$val]"
523 else
524 iwconfig ${device} key "$wlan_key"
525 fi
526
8d92f4e5 527 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
7a4d5330
ER
528 fi
529
b9d6440a 530 if is_no "$wlan_encryption"; then
5e6dfc29
JR
531 iwconfig ${device} key off
532 fi
8d92f4e5 533 val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
7a4d5330 534 iwconfig ${device} power "$val"
8d92f4e5 535 val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
7a4d5330 536 iwconfig ${device} txpower "$val"
8d92f4e5 537 val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
7a4d5330 538 iwconfig ${device} retry "$val"
2ae29436 539
9a0f7d04 540 # essid should be last due to network rescanning by wlan devices
8d92f4e5 541 val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
7a4d5330 542 iwconfig ${device} essid "$val"
a1ac348c
AM
543
544 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
545 iwconfig ${device} commit
9a0f7d04 546 fi
2ae29436
AM
547}
548
ac2433c4
ER
549# returns
550# 0 - no link
551# 1 - link ok
552# 2 - unsupported, unknown return
0336ea13
AM
553check_mii_tool ()
554{
5e6dfc29 555 [ -x /sbin/mii-tool ] || return 2
1e23492f 556 local output=$(LC_ALL=C mii-tool $1 2>&1)
917c3c39 557 echo $output | grep -q "link ok" && return 1
ac2433c4
ER
558 echo $output | grep -q "no link" && return 0
559 return 2
0336ea13
AM
560}
561
ac2433c4
ER
562# returns
563# 0 - link off
564# 1 - link on
565# 2 - unsupported, unknown return
0336ea13
AM
566check_ethtool ()
567{
5e6dfc29 568 [ -x /sbin/ethtool ] || return 2
1e23492f 569 local output=$(LC_ALL=C ethtool $1 2>&1)
917c3c39 570 echo $output | grep -q "Link detected: yes" && return 1
ac2433c4
ER
571 echo $output | grep -q "Link detected: no" && return 0
572 return 2
0336ea13
AM
573}
574
ac2433c4
ER
575# returns
576# 0 - radio off
577# 1 - radio on
578# 2 - unsupported
1b85f830
AM
579check_iwconfig ()
580{
141b5edb 581 local output
0f55a779 582 # rfkill state (are there devices with multiple rfkill buttons?)
141b5edb 583 output=$(cat /sys/class/net/${1}/device/rfkill*/state 2> /dev/null)
0f55a779
AM
584 # 1 is rfkill not active
585 [ "$output" = "1" ] && return 1
586 [ "$output" = "0" -o "$output" = "2" ] && return 0
b343992d
TP
587 [ -x /sbin/iwconfig ] || return 2
588 output=$(LC_ALL=C iwconfig "$1" 2>&1)
589 # "radio off" is ipwxxx only "feature" (and there is no "radio on")
590 echo "$output" | grep -q "radio off" && return 0
2864e829 591 # XXX: need more generic checks for wifi
ac2433c4 592 return 2
1b85f830 593}
0336ea13 594
ac2433c4
ER
595# returns
596# 0 - link down
597# 1 - link up
5937edff 598# 2 - unknown/unsupported
0336ea13
AM
599check_link_down ()
600{
9306925b
AM
601 local e i m timeout device max_timeout
602 device="$1"
603 max_timeout="$2"
604
00d406d2
ER
605 if [ ! -x /sbin/mii-tool -a ! -x /sbin/ethtool -a ! -x /sbin/iwconfig ]; then
606 return 2
607 fi
608
9306925b
AM
609 [ -z "$max_timeout" ] && max_timeout=6
610
611 if ! LC_ALL=C ip link show dev $device 2>/dev/null | grep -q UP; then
612 ip link set dev $device up >/dev/null 2>&1
c8ac1dc0
AM
613 fi
614 timeout=0
9306925b
AM
615 while [ $timeout -le $max_timeout ]; do
616 check_ethtool $device
c8ac1dc0 617 e=$?
9306925b 618 check_iwconfig $device
c8ac1dc0
AM
619 i=$?
620 # trust ethtool and iwconfig
621 if [ $i -eq 1 ] || [ $e -eq 1 ]; then
622 return 1
74a7f2e4 623 fi
c8ac1dc0
AM
624 # use mii check only if all other check are unsupported
625 # (mii check lies too often)
9306925b 626 check_mii_tool $device
c8ac1dc0
AM
627 m=$?
628 if [ $m -eq 1 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
629 return 1
630 fi
631 usleep 500000
632 timeout=$((timeout+1))
633 done
634 # do not abort dhclient if all the checks are unsupported
635 if [ $m -eq 2 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
636 return 2
74a7f2e4 637 fi
c8ac1dc0 638 return 0
0336ea13 639}
This page took 0.179858 seconds and 4 git commands to generate.