1 #!/bin/sh - keep it for file(1) to get bourne shell script result
4 # This is not a shell script; it provides functions to network scripts
12 DEVNAME=${DEVNAME##ifcfg-}
13 DEVNAME=${DEVNAME##tnlcfg-}
15 if [[ "$CONFIG" = /* ]]; then
16 if [ -f "$CONFIG" ]; then
20 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ]; then
21 . "/etc/sysconfig/interfaces/$CONFIG"
25 # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
26 # and forgot to alter DEVICE= line you won't accidentally bring down eth0
27 # while executing ifdown eth1. We do that only if configuration file exists
28 # (sometimes ifcfg-xyz isn't needed at all like server-side pppoe pppX interfaces)
29 if [ "$foundconfig" -eq "1" -a "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
30 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
34 if [ "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
35 IPADDR="$IPADDR/$PREFIX"
41 # Notify programs that have requested notification
42 ( cd /var/run/netreport || exit
44 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
45 rm -f $i >/dev/null 2>&1
52 local hostname=$(hostname)
53 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o "$hostname" = "localhost.localdomain" ]; then
64 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
65 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
67 if [ "$hostname" ]; then
68 echo "$hostname" > /etc/HOSTNAME
72 if [ "$domain" ]; then
73 # XXX umask and possible /etc/resolv.conf symlink breakage
74 > /etc/resolv.conf.new
75 if ! grep -Fq "search $domain" /etc/resolv.conf; then
76 echo "search $domain" >> /etc/resolv.conf.new
78 echo "domain $domain" >> /etc/resolv.conf.new
79 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
80 mv -f /etc/resolv.conf.new /etc/resolv.conf
86 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
87 DHCP_CLIENT=/sbin/pump
90 if [ -z "$DHCP_CLIENT" ]; then
91 if [ "$BOOTPROTO" = "zeroconf" -a -x /usr/sbin/avahi-autoipd ]; then
92 DHCP_CLIENT=/usr/sbin/avahi-autoipd
93 elif [ -x /sbin/dhcpcd ]; then
94 DHCP_CLIENT=/sbin/dhcpcd
95 elif [ -x /sbin/dhclient ]; then
96 DHCP_CLIENT=/sbin/dhclient
97 elif [ -x /sbin/dhcpxd ]; then
98 DHCP_CLIENT=/sbin/dhcpxd
99 elif [ -x /sbin/pump ]; then
100 DHCP_CLIENT=/sbin/pump
101 elif [ "$BOOTPROTO" = "auto" -a -x /usr/sbin/avahi-autoipd ]; then
102 DHCP_CLIENT=/usr/sbin/avahi-autoipd
104 nls "Can't find a DHCP client."
110 # Setup Network Address Translation (NAT)
115 if [ -r /etc/sysconfig/static-nat ]; then
116 if [ "$1" = "on" ]; then
117 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
118 /sbin/ip route add nat $dst via $src
119 /sbin/ip rule add from $src nat $dst
121 elif [ "$1" = "off" ]; then
122 LC_ALL=C /sbin/ip route show table all | grep -E "^nat[[:blank:]]" | while read nat dst via src args; do
123 /sbin/ip rule del from $src nat $dst
124 /sbin/ip route del nat $dst via $src
133 local rc arpdev host hwaddr flags neflags
135 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
136 if [ -x /sbin/arp ]; then
137 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
139 show "Setting static ARP entries"; busy
141 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
142 if [ -z "$arpdev" ]; then
145 # ip supports only ip addresses
146 grep "^[0-9]" /etc/ethers | \
147 while read host hwaddr flags; do
153 neflags="nud permanent"
156 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
161 if [ "$rc" -gt 0 ]; then
170 if is_yes "$STATIC_RARP"; then
171 if [ ! -e /proc/net/rarp ]; then
172 _modprobe single rarp
174 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
175 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
180 # Set up all IP && IP parameter variables
183 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
184 if [ -z "$DEVICETYPE" ]; then
185 # If there's a dot, it's a vlan
186 if echo ${DEVICE} | LC_ALL=C grep -qE '^[a-z0-9]+\.[0-9]+$'; then
189 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[\.:]?[0-9]*[\.:]?[0-9]*$/,NUL); print $0 } ')
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 } ')
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)
204 # check if we have ipv6 or ipv4 address
205 if [[ "${IP4ADDR}" = *:* ]]; then
209 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
210 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
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:-}"
217 if [ "${IP4_SRC_IF}" ]; then
218 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
219 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
222 # new rc-scripts 0.4.x option
223 if [ -z "$HANDLING" ]; then
227 # set handling for bridge
228 case "$DEVICETYPE" in
229 br|atm|lec|irda|vlan)
234 # Multicast ready devices
235 if is_yes "$MULTICAST"; then
238 case "$DEVICETYPE" in
250 if is_yes "$ARP"; then
260 if LC_ALL=C ip link show dev ${DEVICE} 2> /dev/null | grep -Fq UP; then
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
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?"
282 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
283 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
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
295 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
296 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
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}
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>
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
325 grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
326 /sbin/ip route del $args 2>/dev/null
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
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
344 # Add ONLY IPv4 address (IPv6 address is added automaticly)
347 if is_yes "$IPV4_NETWORKING"; then
348 ip addr add 127.0.0.1/8 dev lo
350 ip link set dev lo up
352 grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
353 if [[ "$args" = *:* ]]; then
354 is_no "$IPV6_NETWORKING" && continue
356 is_no "$IPV4_NETWORKING" && continue
358 /sbin/ip route add $args dev lo
360 is_no "$IPV6_NETWORKING" && return
361 grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
362 /sbin/ip -6 route add $args dev lo
368 LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
369 ip link set dev lo down
374 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
375 _modprobe single ipv6
378 if is_yes "$IPX" && is_module "ipx"; then
383 # calculate network prefix from given network mask
386 local old_IFS netmask prefix endp
395 for n in $netmask; do
396 for i in 128 64 32 16 8 4 2 1; do
397 if [ $(($n & $i)) -ne 0 ]; then
398 if [ $endp -eq 0 ]; then
399 prefix=$(($prefix + 1))
412 # calculate network mask from given prefix
413 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
419 if [ "${1##[0-9]*}" ]; then
434 [ $prefix -ge 8 ] && a=255
435 [ $prefix -le 0 ] && a=0
439 prefix="$(( $prefix - 8))"
440 if [ -z "$MASK" ]; then
451 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
452 get_ppp_device_and_pid ()
454 if [ -f "/var/run/ppp-$DEVNAME.pid" ]; then
457 read PID; echo "PID='$PID'"
458 read REALDEVICE; echo "REALDEVICE=$REALDEVICE"
459 } < "/var/run/ppp-$DEVNAME.pid")
462 if [ -z "$REALDEVICE" ]; then
467 # following function setups advanced routing rules
468 # Olgierd Pieczul <wojrus@pld-linux.org>
471 local args prio from src i prio
473 if [ -f /etc/sysconfig/static-routes ]; then
476 grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
478 if [ "${args##* prio }" = "$args" ]; then
479 # no "prio" used in rule, use own prio
483 /sbin/ip rule add $args $prio
485 elif is_no "$1"; then
486 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
487 [ "$src" = "all" ] && /sbin/ip rule delete $args || /sbin/ip rule delete $from $src $args
491 is_no "$IPV6_NETWORKING" && return
492 if [ -f /etc/sysconfig/static-routes6 ]; then
495 grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes6 | while read args; do
497 if [ "${args##* prio }" = "$args" ]; then
498 # no "prio" used in rule, use own prio
502 /sbin/ip -6 rule add $args $prio
504 elif is_no "$1"; then
505 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
506 [ "$src" = "all" ] && /sbin/ip -6 rule delete $args || /sbin/ip -6 rule delete $from $src $args
512 is_wireless_device ()
514 if [ -x /sbin/iwconfig ]; then
515 LC_ALL=C iwconfig "${1}" 2>&1 | grep -Fq "no wireless extensions" || return 0
523 if [ "$2" = "br" ]; then
531 # wlan mode needs to be first
532 wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
533 iwconfig ${device} mode "$wlan_mode"
535 val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ "$val" ] && \
536 iwconfig ${device} nick "$val"
538 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
539 iwconfig ${device} nwid "$val"
541 if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
542 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
543 iwconfig ${device} freq "$val"
544 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
545 iwconfig ${device} channel "$val"
549 val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
550 iwconfig ${device} sens "$val"
551 val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
552 iwconfig ${device} ap "$val"
553 val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
554 iwconfig ${device} rate "$val"
555 val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
556 iwconfig ${device} rts "$val"
557 val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
558 iwconfig ${device} frag "$val"
560 local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
561 local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
562 # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
563 if [ "$wlan_key" ]; then
564 # wlan key can contain index of key to set
565 # to set other than current keys suffix with 1-4:
567 for idx in 1 2 3 4; do
568 val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
569 [ "$val" ] || continue
570 iwconfig ${device} key "[$idx]" "$val"
572 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
573 iwconfig ${device} key "[$val]"
575 iwconfig ${device} key "$wlan_key"
578 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
581 if is_no "$wlan_encryption"; then
582 iwconfig ${device} key off
584 val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
585 iwconfig ${device} power "$val"
586 val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
587 iwconfig ${device} txpower "$val"
588 val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
589 iwconfig ${device} retry "$val"
591 # essid should be last due to network rescanning by wlan devices
592 val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
593 iwconfig ${device} essid "$val"
595 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
596 iwconfig ${device} commit
603 # 2 - unsupported, unknown return
606 [ -x /sbin/mii-tool ] || return 2
607 local output=$(LC_ALL=C mii-tool $1 2>&1)
608 echo $output | grep -Fq "link ok" && return 1
609 echo $output | grep -Fq "no link" && return 0
616 # 2 - unsupported, unknown return
619 [ -x /sbin/ethtool ] || return 2
620 local output=$(LC_ALL=C ethtool $1 2>&1)
621 echo $output | grep -Fq "Link detected: yes" && return 1
622 echo $output | grep -Fq "Link detected: no" && return 0
633 # rfkill state (are there devices with multiple rfkill buttons?)
634 output=$(cat /sys/class/net/${1}/device/rfkill*/state 2> /dev/null)
635 # 1 is rfkill not active
636 [ "$output" = "1" ] && return 1
637 [ "$output" = "0" -o "$output" = "2" ] && return 0
638 [ -x /sbin/iwconfig ] || return 2
639 output=$(LC_ALL=C iwconfig "$1" 2>&1)
640 # "radio off" is ipwxxx only "feature" (and there is no "radio on")
641 echo "$output" | grep -Fq "radio off" && return 0
642 # XXX: need more generic checks for wifi
649 # 2 - unknown/unsupported
652 local e i m timeout device max_timeout
656 if [ ! -x /sbin/mii-tool -a ! -x /sbin/ethtool -a ! -x /sbin/iwconfig ]; then
660 [ -z "$max_timeout" ] && max_timeout=10
662 if ! LC_ALL=C ip link show dev $device 2>/dev/null | grep -Fq UP; then
663 ip link set dev $device up >/dev/null 2>&1
666 while [ $timeout -le $max_timeout ]; do
667 check_ethtool $device
669 check_iwconfig $device
671 # trust ethtool and iwconfig
672 if [ $i -eq 1 ] || [ $e -eq 1 ]; then
675 # use mii check only if all other check are unsupported
676 # (mii check lies too often)
677 check_mii_tool $device
679 if [ $m -eq 1 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
683 timeout=$((timeout+1))
685 # do not abort dhclient if all the checks are unsupported
686 if [ $m -eq 2 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then