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