]> git.pld-linux.org Git - projects/rc-scripts.git/blob - lib/functions.network
bb7ed5ff4a726c36b99537906ff8e2a12d75ecc9
[projects/rc-scripts.git] / lib / 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/dhclient ]; then
95                         DHCP_CLIENT=/sbin/dhclient
96                 elif [ -x /sbin/dhcpcd ]; then
97                         DHCP_CLIENT=/sbin/dhcpcd
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         emit net-device-up IFACE=lo
352
353         grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
354                 if [[ "$args" = *:* ]]; then
355                         is_no "$IPV6_NETWORKING" && continue
356                 else
357                         is_no "$IPV4_NETWORKING" && continue
358                 fi
359                 /sbin/ip route add $args dev lo
360         done
361         is_no "$IPV6_NETWORKING" && return
362         grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
363                 /sbin/ip -6 route add $args dev lo
364         done
365 }
366
367 set_down_loopback()
368 {
369         LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
370         ip link set dev lo down
371 }
372
373 modprobe_net()
374 {
375         if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
376                 _modprobe single ipv6
377         fi
378
379         if is_yes "$IPX" && is_module "ipx"; then
380                 _modprobe single ipx
381         fi
382 }
383
384 # calculate network prefix from given network mask
385 calcprefix()
386 {
387         local old_IFS netmask prefix endp
388
389         old_IFS=$IFS
390         IFS='.'
391         netmask=$(echo $1)
392         IFS=$old_IFS
393
394         prefix=0
395         endp=0
396         for n in $netmask; do
397                 for i in 128 64 32 16 8 4 2 1; do
398                         if [ $(($n & $i)) -ne 0 ]; then
399                                 if [ $endp -eq 0 ]; then
400                                         prefix=$(($prefix + 1))
401                                 else
402                                         echo "32"
403                                         return
404                                 fi
405                         else
406                                 endp=1
407                         fi
408                 done
409         done
410         echo $prefix
411 }
412
413 # calculate network mask from given prefix
414 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
415 calcnetmask()
416 {
417         local prefix a MASK
418
419         MASK=""
420         if [ "${1##[0-9]*}" ]; then
421                 prefix=32
422         else
423                 prefix=$1
424         fi
425         for i in 1 2 3 4; do
426                 case $prefix in
427                   7)    a=254;;
428                   6)    a=252;;
429                   5)    a=248;;
430                   4)    a=240;;
431                   3)    a=224;;
432                   2)    a=192;;
433                   1)    a=128;;
434                   *)
435                         [ $prefix -ge 8 ] && a=255
436                         [ $prefix -le 0 ] && a=0
437                         ;;
438                 esac
439
440                 prefix="$(( $prefix - 8))"
441                 if [ -z "$MASK" ]; then
442                         MASK=$a
443                 else
444                         MASK=$MASK.$a
445                 fi
446         done
447
448         echo $MASK
449         return
450 }
451
452 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
453 get_ppp_device_and_pid ()
454 {
455         if [ -f "/var/run/ppp-$DEVNAME.pid" ]; then
456                 eval $(
457                 {
458                         read PID; echo "PID='$PID'"
459                         read REALDEVICE; echo "REALDEVICE=$REALDEVICE"
460                 } < "/var/run/ppp-$DEVNAME.pid")
461         fi
462
463         if [ -z "$REALDEVICE" ]; then
464                 REALDEVICE=$DEVICE
465         fi
466 }
467
468 # following function setups advanced routing rules
469 # Olgierd Pieczul <wojrus@pld-linux.org>
470 setup_ip_rules ()
471 {
472         local args prio from src i prio
473
474         if [ -f /etc/sysconfig/static-routes ]; then
475                 if is_yes "$1"; then
476                         i=10000
477                         grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
478                                 prio=""
479                                 if [ "${args##* prio }" = "$args" ]; then
480                                         # no "prio" used in rule, use own prio
481                                         prio="prio $i"
482                                         i=$((i + 5))
483                                 fi
484                                 /sbin/ip rule add $args $prio
485                         done
486                 elif is_no "$1"; then
487                         LC_ALL=C /sbin/ip rule show | grep -vE -e "from all lookup (main|default|local|253|254|255) \$" -e " map-to " | while read prio from src args; do
488                                 [ "$src" = "all" ] && /sbin/ip rule delete $args || /sbin/ip rule delete $from $src $args
489                         done
490                 fi
491         fi
492         is_no "$IPV6_NETWORKING" && return
493         if [ -f /etc/sysconfig/static-routes6 ]; then
494                 if is_yes "$1"; then
495                         i=10000
496                         grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes6 | while read args; do
497                                 prio=""
498                                 if [ "${args##* prio }" = "$args" ]; then
499                                         # no "prio" used in rule, use own prio
500                                         prio="prio $i"
501                                         i=$((i + 5))
502                                 fi
503                                 /sbin/ip -6 rule add $args $prio
504                         done
505                 elif is_no "$1"; then
506                         LC_ALL=C /sbin/ip -6 rule show | grep -vE -e "from all lookup (main|default|local|253|254|255) \$" -e " map-to " | while read prio from src args; do
507                                 [ "$src" = "all" ] && /sbin/ip -6 rule delete $args || /sbin/ip -6 rule delete $from $src $args
508                         done
509                 fi
510         fi
511 }
512
513 is_wireless_device ()
514 {
515         if [ -x /sbin/iwconfig ]; then
516                 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
517         fi
518         return 1
519 }
520
521 wireless_param()
522 {
523         device="$1"
524         if [ "$2" = "br" ]; then
525                 prefix="${device}_"
526         else
527                 prefix=""
528         fi
529
530         local val wlan_mode
531
532         # wlan mode needs to be first
533         wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
534                 iwconfig ${device} mode "$wlan_mode"
535
536         val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ "$val" ] && \
537                 iwconfig ${device} nick "$val"
538
539         val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
540                 iwconfig ${device} nwid "$val"
541
542         if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
543                 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
544                         iwconfig ${device} freq "$val"
545                 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
546                         iwconfig ${device} channel "$val"
547                 fi
548         fi
549
550         val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
551                 iwconfig ${device} sens "$val"
552         val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
553                 iwconfig ${device} ap "$val"
554         val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
555                 iwconfig ${device} rate "$val"
556         val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
557                 iwconfig ${device} rts "$val"
558         val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
559                 iwconfig ${device} frag "$val"
560
561         local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
562         local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
563         # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
564         if [ "$wlan_key" ]; then
565                 # wlan key can contain index of key to set
566                 # to set other than current keys suffix with 1-4:
567                 local idx
568                 for idx in 1 2 3 4; do
569                         val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
570                         [ "$val" ] || continue
571                         iwconfig ${device} key "[$idx]" "$val"
572                 done
573                 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
574                         iwconfig ${device} key "[$val]"
575                 else
576                         iwconfig ${device} key "$wlan_key"
577                 fi
578
579                 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
580         fi
581
582         if is_no "$wlan_encryption"; then
583                 iwconfig ${device} key off
584         fi
585         val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
586                 iwconfig ${device} power "$val"
587         val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
588                 iwconfig ${device} txpower "$val"
589         val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
590                 iwconfig ${device} retry "$val"
591
592         # essid should be last due to network rescanning by wlan devices
593         val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
594                 iwconfig ${device} essid "$val"
595
596         if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
597                 iwconfig ${device} commit
598         fi
599 }
600
601 # returns
602 # 0 - no link
603 # 1 - link ok
604 # 2 - unsupported, unknown return
605 check_mii_tool ()
606 {
607         [ -x /sbin/mii-tool ] || return 2
608         local output=$(LC_ALL=C mii-tool $1 2>&1)
609         echo $output | grep -q "link ok" && return 1
610         echo $output | grep -q "no link" && return 0
611         return 2
612 }
613
614 # returns
615 # 0 - link off
616 # 1 - link on
617 # 2 - unsupported, unknown return
618 check_ethtool ()
619 {
620         [ -x /sbin/ethtool ] || return 2
621         local output=$(LC_ALL=C ethtool $1 2>&1)
622         echo $output | grep -q "Link detected: yes" && return 1
623         echo $output | grep -q "Link detected: no" && return 0
624         return 2
625 }
626
627 # returns
628 # 0 - radio off
629 # 1 - radio on
630 # 2 - unsupported
631 check_iwconfig ()
632 {
633         local output
634         # rfkill state (are there devices with multiple rfkill buttons?)
635         output=$(cat /sys/class/net/${1}/device/rfkill*/state 2> /dev/null)
636         # 1 is rfkill not active
637         [ "$output" = "1" ] && return 1
638         [ "$output" = "0" -o "$output" = "2" ] && return 0
639         [ -x /sbin/iwconfig ] || return 2
640         output=$(LC_ALL=C iwconfig "$1" 2>&1)
641         # "radio off" is ipwxxx only "feature" (and there is no "radio on")
642         echo "$output" | grep -q "radio off" && return 0
643         # XXX: need more generic checks for wifi
644         return 2
645 }
646
647 # returns
648 # 0 - link down
649 # 1 - link up
650 # 2 - unknown/unsupported
651 check_link_down ()
652 {
653         local e i m timeout device max_timeout
654         device="$1"
655         max_timeout="$2"
656
657         if [ ! -x /sbin/mii-tool -a ! -x /sbin/ethtool -a ! -x /sbin/iwconfig ]; then
658                 return 2
659         fi
660
661         [ -z "$max_timeout" ] && max_timeout=6
662
663         if ! LC_ALL=C ip link show dev $device 2>/dev/null | grep -q UP; then
664                 ip link set dev $device up >/dev/null 2>&1
665                 emit net-device-up IFACE=$device
666         fi
667         timeout=0
668         while [ $timeout -le $max_timeout ]; do
669                 check_ethtool $device
670                 e=$?
671                 check_iwconfig $device
672                 i=$?
673                 # trust ethtool and iwconfig
674                 if [ $i -eq 1 ] || [ $e -eq 1 ]; then
675                         return 1
676                 fi
677                 # use mii check only if all other check are unsupported
678                 # (mii check lies too often)
679                 check_mii_tool $device
680                 m=$?
681                 if [ $m -eq 1 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
682                         return 1
683                 fi
684                 usleep 500000
685                 timeout=$((timeout+1))
686         done
687         # do not abort dhclient if all the checks are unsupported
688         if [ $m -eq 2 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
689                 return 2
690         fi
691         return 0
692 }
This page took 0.087333 seconds and 3 git commands to generate.