]> git.pld-linux.org Git - projects/rc-scripts.git/blob - sysconfig/network-scripts/functions.network
Really disable IPv6.
[projects/rc-scripts.git] / sysconfig / network-scripts / functions.network
1 #!/bin/sh - keep it for file(1) to get bourne shell script result
2 #
3 # $Id$
4 #
5 # This is not a shell script; it provides functions to network scripts
6 # that source it.
7
8 source_config ()
9 {
10         local foundconfig=0
11
12         DEVNAME=${CONFIG##*/}
13         DEVNAME=${DEVNAME##ifcfg-}
14         DEVNAME=${DEVNAME##tnlcfg-}
15
16         if [[ "$CONFIG" = /* ]]; then
17                 if [ -f "$CONFIG" ]; then
18                         . "$CONFIG"
19                         foundconfig=1
20                 fi
21         elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ]; then
22                 . "/etc/sysconfig/interfaces/$CONFIG"
23                 foundconfig=1
24         fi
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
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)
30         if [ "$foundconfig" -eq "1" -a "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
31                 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
32                 exit 1
33         fi
34
35         if [ "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
36                 IPADDR="$IPADDR/$PREFIX"
37         fi
38 }
39
40 do_netreport ()
41 {
42         # Notify programs that have requested notification
43         ( cd /var/run/netreport || exit
44         for i in *; do
45                 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
46                         rm -f $i >/dev/null 2>&1
47         done
48         )
49 }
50
51 need_hostname()
52 {
53         local hostname=$(hostname)
54         if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o "$hostname" = "localhost.localdomain" ]; then
55                 NEEDHOSTNAME=yes
56         else
57                 unset NEEDHOSTNAME
58         fi
59 }
60
61 set_hostname()
62 {
63         local hostname domain
64
65         hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
66         domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
67
68         if [ "$hostname" ]; then
69                 echo "$hostname" > /etc/HOSTNAME
70                 hostname $hostname
71         fi
72
73         if [ "$domain" ]; then
74                 # XXX umask and possible /etc/resolv.conf symlink breakage
75                 > /etc/resolv.conf.new
76                 if ! grep -q "search $domain" /etc/resolv.conf; then
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
83 }
84
85 set_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 [ "$BOOTPROTO" = "zeroconf" -a -x /usr/sbin/avahi-autoipd ]; then
93                         DHCP_CLIENT=/usr/sbin/avahi-autoipd
94                 elif [ -x /sbin/dhcpcd ]; then
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
102                 elif [ "$BOOTPROTO" = "auto" -a -x /usr/sbin/avahi-autoipd ]; then
103                         DHCP_CLIENT=/usr/sbin/avahi-autoipd
104                 else
105                         nls "Can't find a DHCP client."
106                         exit 1
107                 fi
108         fi
109 }
110
111 # Setup Network Address Translation (NAT)
112 setup_nat()
113 {
114         local src via dst
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
123                         LC_ALL=C /sbin/ip route show table all | grep -E "^nat[[:blank:]]" | while read nat dst via src args; do
124                                 /sbin/ip rule del from $src nat $dst
125                                 /sbin/ip route del nat $dst via $src
126                         done
127                 fi
128         fi
129 }
130
131 # Setup static ARP
132 static_arp()
133 {
134         local rc arpdev host hwaddr flags neflags
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
165                 fi
166         fi
167 }
168
169 static_rarp()
170 {
171         if is_yes "$STATIC_RARP"; then
172                 if [ ! -e /proc/net/rarp ]; then
173                         _modprobe single rarp
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
179 }
180
181 # Set up all IP && IP parameter variables
182 setup_ip_param ()
183 {
184         # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
185         if [ -z "$DEVICETYPE" ]; then
186                 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[\.:]?[0-9]*[\.:]?[0-9]*$/,NUL); print $0 } ')
187         fi
188
189         # Setup DEVICETYPE for special cases.
190         if echo ${DEVICE} | LC_ALL=C grep -qE '^[a-z0-9]+\.[0-9]+$'; then
191                 DEVICETYPE=vlan
192         fi
193
194         # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
195         SUBDEVICE=$(echo "$DEVICE" | grep -E "([0-9]+:[0-9]+)")
196         DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
197
198         eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
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
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
212         if [ "${IP6_PRIM_IF}" ]; then
213                 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
214                 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
215                 eval IP6ADDRLABEL="\$IP_LABEL${IP6_PRIM_IF:-}"
216         fi
217         if [ "${IP4_SRC_IF}" ]; then
218                 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
219                 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
220         fi
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
233
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
249         if [ "$ARP" ]; then
250                 if is_yes "$ARP"; then
251                         ARP="arp on"
252                 else
253                         ARP="arp off"
254                 fi
255         fi
256 }
257
258 check_device_down ()
259 {
260         if LC_ALL=C ip link show dev ${DEVICE} 2> /dev/null | grep -q UP; then
261                 return 1
262         else
263                 return 0
264         fi
265 }
266
267 setup_ip_gw_ro ()
268 {
269         # IPv4 gateway
270         if is_yes "$IPV4_NETWORKING"; then
271                 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
272                         # set up default gateway
273                         if [ "${GATEWAY}" ]; then
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
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
292                         if [ "${GATEWAY6}" ]; then
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
301                         if [ "${IPV6_GLOBALROUTEGW}" ]; then
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
308 }
309
310 #
311 # following function sets up routes not associated to any device
312 # eg. unreachable or blackhole routes
313 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
314 #
315 setup_routes()
316 {
317         local args
318
319         if [ -f /etc/sysconfig/static-routes ]; then
320                 if [ "$1" = "on" -o "$1" = "yes" ]; then
321                         grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
322                                 /sbin/ip route add $args
323                         done
324                 else
325                         grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
326                                 /sbin/ip route del $args 2>/dev/null
327                         done
328                 fi
329         fi
330         is_no "$IPV6_NETWORKING" && return
331         if [ -f /etc/sysconfig/static-routes6 ]; then
332                 if [ "$1" = "on" -o "$1" = "yes" ]; then
333                         grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
334                                 /sbin/ip -6 route add $args
335                         done
336                 else
337                         grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
338                                 /sbin/ip -6 route del $args 2>/dev/null
339                         done
340                 fi
341         fi
342 }
343
344 # Add ONLY IPv4 address (IPv6 address is added automaticly)
345 set_up_loopback()
346 {
347         if is_yes "$IPV4_NETWORKING"; then
348                 ip addr add 127.0.0.1/8 dev lo
349         fi
350         ip link set dev lo up
351         grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
352                 if [[ "$args" = *:* ]]; then
353                         is_no "$IPV6_NETWORKING" && continue
354                 else
355                         is_no "$IPV4_NETWORKING" && continue
356                 fi
357                 /sbin/ip route add $args dev lo
358         done
359         is_no "$IPV6_NETWORKING" && return
360         grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
361                 /sbin/ip -6 route add $args dev lo
362         done
363 }
364
365 set_down_loopback()
366 {
367         LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
368         ip link set dev lo down
369 }
370
371 modprobe_net()
372 {
373         if is_yes "$IPV6_NETWORKING"
374                 if is_module "ipv6"; then
375                         _modprobe single ipv6
376                 fi
377         elif [ -d /proc/sys/net/ipv6 ]; then
378                 sysctl -w net.ipv6.conf.all.disable_ipv6=1
379         fi
380
381         if is_yes "$IPX" && is_module "ipx"; then
382                 _modprobe single ipx
383         fi
384 }
385
386 # calculate network prefix from given network mask
387 calcprefix()
388 {
389         local old_IFS netmask prefix endp
390
391         old_IFS=$IFS
392         IFS='.'
393         netmask=$(echo $1)
394         IFS=$old_IFS
395
396         prefix=0
397         endp=0
398         for n in $netmask; do
399                 for i in 128 64 32 16 8 4 2 1; do
400                         if [ $(($n & $i)) -ne 0 ]; then
401                                 if [ $endp -eq 0 ]; then
402                                         prefix=$(($prefix + 1))
403                                 else
404                                         echo "32"
405                                         return
406                                 fi
407                         else
408                                 endp=1
409                         fi
410                 done
411         done
412         echo $prefix
413 }
414
415 # calculate network mask from given prefix
416 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
417 calcnetmask()
418 {
419         local prefix a MASK
420
421         MASK=""
422         if [ "${1##[0-9]*}" ]; then
423                 prefix=32
424         else
425                 prefix=$1
426         fi
427         for i in 1 2 3 4; do
428                 case $prefix in
429                   7)    a=254;;
430                   6)    a=252;;
431                   5)    a=248;;
432                   4)    a=240;;
433                   3)    a=224;;
434                   2)    a=192;;
435                   1)    a=128;;
436                   *)
437                         [ $prefix -ge 8 ] && a=255
438                         [ $prefix -le 0 ] && a=0
439                         ;;
440                 esac
441
442                 prefix="$(( $prefix - 8))"
443                 if [ -z "$MASK" ]; then
444                         MASK=$a
445                 else
446                         MASK=$MASK.$a
447                 fi
448         done
449
450         echo $MASK
451         return
452 }
453
454 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
455 get_ppp_device_and_pid ()
456 {
457         if [ -f "/var/run/ppp-$DEVNAME.pid" ]; then
458                 eval $(
459                 {
460                         read PID; echo "PID='$PID'"
461                         read REALDEVICE; echo "REALDEVICE=$REALDEVICE"
462                 } < "/var/run/ppp-$DEVNAME.pid")
463         fi
464
465         if [ -z "$REALDEVICE" ]; then
466                 REALDEVICE=$DEVICE
467         fi
468 }
469
470 # following function setups advanced routing rules
471 # Olgierd Pieczul <wojrus@pld-linux.org>
472 setup_ip_rules ()
473 {
474         local args prio from src
475
476         if [ -f /etc/sysconfig/static-routes ]; then
477                 if is_yes "$1"; then
478                         grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
479                                 /sbin/ip rule add $args
480                         done
481                 elif is_no "$1"; then
482                         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
483                                 [ "$src" = "all" ] && /sbin/ip rule delete $args || /sbin/ip rule delete $from $src $args
484                         done
485                 fi
486         fi
487         is_no "$IPV6_NETWORKING" && return
488         if [ -f /etc/sysconfig/static-routes6 ]; then
489                 if is_yes "$1"; then
490                         grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes6 | while read args; do
491                                 /sbin/ip -6 rule add $args
492                         done
493                 elif is_no "$1"; then
494                         LC_ALL=C /sbin/ip -6 rule show | grep -vE -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
495                                 [ "$src" = "all" ] && /sbin/ip -6 rule delete $args || /sbin/ip -6 rule delete $from $src $args
496                         done
497                 fi
498         fi
499 }
500
501 is_wireless_device ()
502 {
503         if [ -x /sbin/iwconfig ]; then
504                 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
505         fi
506         return 1
507 }
508
509 wireless_param()
510 {
511         device="$1"
512         if [ "$2" = "br" ]; then
513                 prefix="${device}_"
514         else
515                 prefix=""
516         fi
517
518         local val wlan_mode
519
520         # wlan mode needs to be first
521         wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
522                 iwconfig ${device} mode "$wlan_mode"
523
524         val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ "$val" ] && \
525                 iwconfig ${device} nick "$val"
526
527         val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
528                 iwconfig ${device} nwid "$val"
529
530         if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
531                 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
532                         iwconfig ${device} freq "$val"
533                 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
534                         iwconfig ${device} channel "$val"
535                 fi
536         fi
537
538         val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
539                 iwconfig ${device} sens "$val"
540         val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
541                 iwconfig ${device} ap "$val"
542         val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
543                 iwconfig ${device} rate "$val"
544         val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
545                 iwconfig ${device} rts "$val"
546         val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
547                 iwconfig ${device} frag "$val"
548
549         local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
550         local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
551         # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
552         if [ "$wlan_key" ]; then
553                 # wlan key can contain index of key to set
554                 # to set other than current keys suffix with 1-4:
555                 local idx
556                 for idx in 1 2 3 4; do
557                         val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
558                         [ "$val" ] || continue
559                         iwconfig ${device} key "[$idx]" "$val"
560                 done
561                 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
562                         iwconfig ${device} key "[$val]"
563                 else
564                         iwconfig ${device} key "$wlan_key"
565                 fi
566
567                 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
568         fi
569
570         if is_no "$wlan_encryption"; then
571                 iwconfig ${device} key off
572         fi
573         val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
574                 iwconfig ${device} power "$val"
575         val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
576                 iwconfig ${device} txpower "$val"
577         val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
578                 iwconfig ${device} retry "$val"
579
580         # essid should be last due to network rescanning by wlan devices
581         val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
582                 iwconfig ${device} essid "$val"
583
584         if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
585                 iwconfig ${device} commit
586         fi
587 }
588
589 # returns
590 # 0 - no link
591 # 1 - link ok
592 # 2 - unsupported, unknown return
593 check_mii_tool ()
594 {
595         [ -x /sbin/mii-tool ] || return 2
596         local output=$(LC_ALL=C mii-tool $1 2>&1)
597         echo $output | grep -q "link ok" && return 1
598         echo $output | grep -q "no link" && return 0
599         return 2
600 }
601
602 # returns
603 # 0 - link off
604 # 1 - link on
605 # 2 - unsupported, unknown return
606 check_ethtool ()
607 {
608         [ -x /sbin/ethtool ] || return 2
609         local output=$(LC_ALL=C ethtool $1 2>&1)
610         echo $output | grep -q "Link detected: yes" && return 1
611         echo $output | grep -q "Link detected: no" && return 0
612         return 2
613 }
614
615 # returns
616 # 0 - radio off
617 # 1 - radio on
618 # 2 - unsupported
619 check_iwconfig ()
620 {
621         local output
622         # rfkill state (are there devices with multiple rfkill buttons?)
623         output=$(cat /sys/class/net/${1}/device/rfkill*/state 2> /dev/null)
624         # 1 is rfkill not active
625         [ "$output" = "1" ] && return 1
626         [ "$output" = "0" -o "$output" = "2" ] && return 0
627         [ -x /sbin/iwconfig ] || return 2
628         output=$(LC_ALL=C iwconfig "$1" 2>&1)
629         # "radio off" is ipwxxx only "feature" (and there is no "radio on")
630         echo "$output" | grep -q "radio off" && return 0
631         # XXX: need more generic checks for wifi
632         return 2
633 }
634
635 # returns
636 # 0 - link down
637 # 1 - link up
638 # 2 - unknown/unsupported
639 check_link_down ()
640 {
641         local e i m timeout device max_timeout
642         device="$1"
643         max_timeout="$2"
644
645         if [ ! -x /sbin/mii-tool -a ! -x /sbin/ethtool -a ! -x /sbin/iwconfig ]; then
646                 return 2
647         fi
648
649         [ -z "$max_timeout" ] && max_timeout=6
650
651         if ! LC_ALL=C ip link show dev $device 2>/dev/null | grep -q UP; then
652                 ip link set dev $device up >/dev/null 2>&1
653         fi
654         timeout=0
655         while [ $timeout -le $max_timeout ]; do
656                 check_ethtool $device
657                 e=$?
658                 check_iwconfig $device
659                 i=$?
660                 # trust ethtool and iwconfig
661                 if [ $i -eq 1 ] || [ $e -eq 1 ]; then
662                         return 1
663                 fi
664                 # use mii check only if all other check are unsupported
665                 # (mii check lies too often)
666                 check_mii_tool $device
667                 m=$?
668                 if [ $m -eq 1 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
669                         return 1
670                 fi
671                 usleep 500000
672                 timeout=$((timeout+1))
673         done
674         # do not abort dhclient if all the checks are unsupported
675         if [ $m -eq 2 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
676                 return 2
677         fi
678         return 0
679 }
This page took 0.107137 seconds and 4 git commands to generate.