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 \
55 "$hostname" = "localhost.localdomain" ]; then
64 typeset hostname domain
66 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
67 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
69 if [ "$hostname" ]; then
70 echo "$hostname" > /etc/HOSTNAME
74 if [ "$domain" ]; then
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 [ -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
101 nls "Can't find a dhcp client."
107 # Setup Network Address Translation (NAT)
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
118 elif [ "$1" = "off" ]; then
119 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
120 /sbin/ip rule del from $src nat $dst
121 /sbin/ip route del nat $dst via $src
130 typeset rc arpdev host hwaddr flags neflags
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
136 show "Setting static ARP entries"; busy
138 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
139 if [ -z "$arpdev" ]; then
142 # ip supports only ip addresses
143 grep "^[0-9]" /etc/ethers | \
144 while read host hwaddr flags; do
150 neflags="nud permanent"
153 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
158 if [ "$rc" -gt 0 ]; then
167 if is_yes "$STATIC_RARP"; then
168 if [ ! -e /proc/net/rarp ]; then
169 _modprobe single -k rarp
171 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
172 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
179 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && $(echo $DEVICE | grep -q ^eth); then
180 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
187 # Set up all IP && IP parameter variables
190 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
191 if [ -z "$DEVICETYPE" ]; then
192 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
195 # Setup DEVICETYPE for special cases.
196 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
200 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
201 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
203 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
204 # check if ipaddr doesn't contain network length -- use $NETMASK then
205 if [[ "$IP4ADDR" != */* ]] && [ "$NETMASK" ]; then
206 IP4ADDR=$IP4ADDR/$(calcprefix $NETMASK)
209 # check if we have ipv6 or ipv4 address
210 if [[ "${IP4ADDR}" = *:* ]]; then
214 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
215 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
217 if [ "${IP6_PRIM_IF}" ] ; then
218 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
219 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
221 if [ "${IP4_SRC_IF}" ] ; then
222 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
223 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
225 if [ "${IP6_SRC_IF}" ] ; then
226 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
227 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
230 # new rc-scripts 0.4.x option
231 if [ -z "$HANDLING" ]; then
235 # set handling for bridge
236 case "$DEVICETYPE" in
237 br|atm|lec|irda|vlan)
242 # Multicast ready devices
243 if is_yes "$MULTICAST"; then
246 case "$DEVICETYPE" in
258 if is_yes "$ARP"; then
268 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
278 if is_yes "$IPV4_NETWORKING"; then
279 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
280 # set up default gateway
281 if [ "${GATEWAY}" ]; then
282 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
286 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
287 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
288 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
290 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
291 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
296 # IPv6 gateway && IPv6 globalroute
297 if is_yes "$IPV6_NETWORKING"; then
298 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
299 # set up default gateway
300 if [ "${GATEWAY6}" ]; then
301 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
303 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
304 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
307 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
308 # set up default route
309 if [ "${IPV6_GLOBALROUTEGW}" ]; then
310 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
311 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
312 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
319 # following function sets up routes not associated to any device
320 # eg. unreachable or blackhole routes
321 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
327 if [ -f /etc/sysconfig/static-routes ]; then
328 if [ "$1" = "on" -o "$1" = "yes" ] ; then
329 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
330 /sbin/ip route add $args
333 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
334 /sbin/ip route del $args 2>/dev/null
340 # Add ONLY IPv4 address (IPv6 address is added automaticly)
343 if is_yes "$IPV4_NETWORKING"; then
344 ip addr add 127.0.0.1/8 dev lo
346 ip link set dev lo up
351 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
352 ip link set dev lo down
357 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
358 _modprobe single -k ipv6
361 if is_yes "$IPX" && is_module "ipx"; then
362 _modprobe single -k ipx
366 # calculate network prefix from given network mask
369 typeset old_IFS netmask prefix endp
378 for n in $netmask ; do
379 for i in 128 64 32 16 8 4 2 1 ; do
380 if [ $(($n & $i)) -ne 0 ]; then
381 if [ $endp -eq 0 ]; then
382 prefix=$(($prefix + 1))
395 # calculate network mask from given prefix
396 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
399 typeset prefix a MASK
402 if [ "${1##[0-9]*}" ]; then
417 [ $prefix -ge 8 ] && a=255
418 [ $prefix -le 0 ] && a=0
422 prefix="$(( $prefix - 8))"
423 if [ -z "$MASK" ]; then
434 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
435 get_ppp_device_and_pid ()
437 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
440 read PID ; echo "PID='$PID'"
441 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
442 } < "/var/run/ppp-$DEVNAME.pid")
445 if [ -z "$REALDEVICE" ] ; then
450 # following function setups advanced routing rules
451 # Olgierd Pieczul <wojrus@pld-linux.org>
454 typeset args prio from src
456 if [ -f /etc/sysconfig/static-routes ] ; then
458 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
459 /sbin/ip rule add $args
461 elif is_no "$1"; then
462 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
463 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
469 is_wireless_device ()
471 if [ -x /sbin/iwconfig ]; then
472 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
480 if [ "$2" = "br" ]; then
488 if val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ -z "$val" ]; then
489 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
494 # wlan mode needs to be first
495 wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
496 iwconfig ${device} mode "$wlan_mode"
498 iwconfig ${device} nick ${WLAN_NICKNAME}
500 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
501 iwconfig ${device} nwid "$val"
503 if [ "$wlan_mode" != "Managed" ]; then
504 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
505 iwconfig ${device} freq "$val"
506 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
507 iwconfig ${device} channel "$val"
511 val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
512 iwconfig ${device} sens "$val"
513 val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
514 iwconfig ${device} ap "$val"
515 val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
516 iwconfig ${device} rate "$val"
517 val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
518 iwconfig ${device} rts "$val"
519 val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
520 iwconfig ${device} frag "$val"
522 local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
523 local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
524 # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
525 if [ "$wlan_key" ]; then
526 # wlan key can contain index of key to set
527 # to set other than current keys suffix with 1-4:
529 for idx in 1 2 3 4; do
530 val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
531 [ "$val" ] || continue
532 iwconfig ${device} key "[$idx]" "$val"
534 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
535 iwconfig ${device} key "[$val]"
537 iwconfig ${device} key "$wlan_key"
540 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
543 if is_no "$wlan_encryption"; then
544 iwconfig ${device} key off
546 val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
547 iwconfig ${device} power "$val"
548 val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
549 iwconfig ${device} txpower "$val"
550 val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
551 iwconfig ${device} retry "$val"
552 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
553 iwconfig ${device} nwid "$val"
555 # essid should be last due to network rescanning by wlan devices
556 val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
557 iwconfig ${device} essid "$val"
559 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
560 iwconfig ${device} commit
566 [ -x /sbin/mii-tool ] || return 2
567 output=$(LC_ALL=C mii-tool $1 2>&1)
568 echo $output | grep -q "link ok" && return 1
569 echo $output | grep -q "no link" && return 0 || return 2
574 [ -x /sbin/ethtool ] || return 2
575 output=$(LC_ALL=C ethtool $1 2>&1)
576 echo $output | grep -q "Link detected: yes" && return 1
577 echo $output | grep -q "Link detected: no" && return 0 || return 2
582 [ -x /sbin/iwconfig ] || return 2
583 output=$(LC_ALL=C iwconfig $1 2>&1)
584 echo $output | grep -q "radio off" && return 0 || return 2
589 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
590 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
591 ip link set dev $1 up >/dev/null 2>&1
593 while [ $timeout -le 10 ]; do
600 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
603 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
607 timeout=$((timeout+1))
615 # This must be last line !