1 #!/bin/sh - keep it for file(1) to get bourne shell script result
5 # This is not a shell script; it provides functions to network scripts
13 DEVNAME=${DEVNAME##ifcfg-}
14 DEVNAME=${DEVNAME##tnlcfg-}
16 if [[ "$CONFIG" = /* ]]; then
17 if [ -f "$CONFIG" ]; then
21 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ]; then
22 . "/etc/sysconfig/interfaces/$CONFIG"
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!"
35 if [ "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
36 IPADDR="$IPADDR/$PREFIX"
42 # Notify programs that have requested notification
43 ( cd /var/run/netreport || exit
45 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
46 rm -f $i >/dev/null 2>&1
53 local hostname=$(hostname)
54 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o "$hostname" = "localhost.localdomain" ]; then
65 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
66 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
68 if [ "$hostname" ]; then
69 echo "$hostname" > /etc/HOSTNAME
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
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
87 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
88 DHCP_CLIENT=/sbin/pump
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
105 nls "Can't find a DHCP client."
111 # Setup Network Address Translation (NAT)
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
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
134 local rc arpdev host hwaddr flags neflags
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
140 show "Setting static ARP entries"; busy
142 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
143 if [ -z "$arpdev" ]; then
146 # ip supports only ip addresses
147 grep "^[0-9]" /etc/ethers | \
148 while read host hwaddr flags; do
154 neflags="nud permanent"
157 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
162 if [ "$rc" -gt 0 ]; then
171 if is_yes "$STATIC_RARP"; then
172 if [ ! -e /proc/net/rarp ]; then
173 _modprobe single rarp
175 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
176 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
181 # Set up all IP && IP parameter variables
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 } ')
189 # Setup DEVICETYPE for special cases.
190 if echo ${DEVICE} | LC_ALL=C grep -qE '^[a-z0-9]+\.[0-9]+$'; then
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 -q 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
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
355 is_no "$IPV4_NETWORKING" && continue
357 /sbin/ip route add $args dev lo
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
367 LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
368 ip link set dev lo down
373 if is_yes "$IPV6_NETWORKING"
374 if is_module "ipv6"; then
375 _modprobe single ipv6
377 elif [ -d /proc/sys/net/ipv6 ]; then
378 sysctl -w net.ipv6.conf.all.disable_ipv6=0
381 if is_yes "$IPX" && is_module "ipx"; then
386 # calculate network prefix from given network mask
389 local old_IFS netmask prefix endp
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))
415 # calculate network mask from given prefix
416 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
422 if [ "${1##[0-9]*}" ]; then
437 [ $prefix -ge 8 ] && a=255
438 [ $prefix -le 0 ] && a=0
442 prefix="$(( $prefix - 8))"
443 if [ -z "$MASK" ]; then
454 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
455 get_ppp_device_and_pid ()
457 if [ -f "/var/run/ppp-$DEVNAME.pid" ]; then
460 read PID; echo "PID='$PID'"
461 read REALDEVICE; echo "REALDEVICE=$REALDEVICE"
462 } < "/var/run/ppp-$DEVNAME.pid")
465 if [ -z "$REALDEVICE" ]; then
470 # following function setups advanced routing rules
471 # Olgierd Pieczul <wojrus@pld-linux.org>
474 local args prio from src
476 if [ -f /etc/sysconfig/static-routes ]; 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
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
487 is_no "$IPV6_NETWORKING" && return
488 if [ -f /etc/sysconfig/static-routes6 ]; 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
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
501 is_wireless_device ()
503 if [ -x /sbin/iwconfig ]; then
504 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
512 if [ "$2" = "br" ]; then
520 # wlan mode needs to be first
521 wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
522 iwconfig ${device} mode "$wlan_mode"
524 val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ "$val" ] && \
525 iwconfig ${device} nick "$val"
527 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
528 iwconfig ${device} nwid "$val"
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"
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"
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:
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"
561 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
562 iwconfig ${device} key "[$val]"
564 iwconfig ${device} key "$wlan_key"
567 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
570 if is_no "$wlan_encryption"; then
571 iwconfig ${device} key off
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"
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"
584 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
585 iwconfig ${device} commit
592 # 2 - unsupported, unknown return
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
605 # 2 - unsupported, unknown return
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
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
638 # 2 - unknown/unsupported
641 local e i m timeout device max_timeout
645 if [ ! -x /sbin/mii-tool -a ! -x /sbin/ethtool -a ! -x /sbin/iwconfig ]; then
649 [ -z "$max_timeout" ] && max_timeout=6
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
655 while [ $timeout -le $max_timeout ]; do
656 check_ethtool $device
658 check_iwconfig $device
660 # trust ethtool and iwconfig
661 if [ $i -eq 1 ] || [ $e -eq 1 ]; then
664 # use mii check only if all other check are unsupported
665 # (mii check lies too often)
666 check_mii_tool $device
668 if [ $m -eq 1 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
672 timeout=$((timeout+1))
674 # do not abort dhclient if all the checks are unsupported
675 if [ $m -eq 2 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then