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
11 DEVNAME=${DEVNAME##ifcfg-}
12 DEVNAME=${DEVNAME##tnlcfg-}
14 if [[ "$CONFIG" = /* ]]; then
15 if [ -f "$CONFIG" ] ; then
18 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
19 . "/etc/sysconfig/interfaces/$CONFIG"
22 # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
23 # and forgot to alter DEVICE= line you won't accidentally bring down eth0
24 # while executing ifdown eth1.
25 if [ -n "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
26 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
30 if [ -n "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
31 IPADDR="$IPADDR/$PREFIX"
37 # Notify programs that have requested notification
38 ( cd /var/run/netreport || exit
40 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
41 rm -f $i >/dev/null 2>&1
48 local hostname=$(hostname)
49 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o \
50 "$hostname" = "localhost.localdomain" ]; then
59 typeset hostname domain
61 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
62 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
64 if [ -n "$hostname" ] ; then
65 echo "$hostname" > /etc/HOSTNAME
69 if [ -n "$domain" ] ; then
71 if ! grep -q "search $domain" /etc/resolv.conf ; then
72 echo "search $domain" >> /etc/resolv.conf.new
74 echo "domain $domain" >> /etc/resolv.conf.new
75 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
76 mv -f /etc/resolv.conf.new /etc/resolv.conf
82 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
83 DHCP_CLIENT=/sbin/pump
86 if [ -z "$DHCP_CLIENT" ]; then
87 if [ -x /sbin/dhcpcd ]; then
88 DHCP_CLIENT=/sbin/dhcpcd
89 elif [ -x /sbin/dhclient ]; then
90 DHCP_CLIENT=/sbin/dhclient
91 elif [ -x /sbin/dhcpxd ]; then
92 DHCP_CLIENT=/sbin/dhcpxd
93 elif [ -x /sbin/pump ]; then
94 DHCP_CLIENT=/sbin/pump
96 nls "Can't find a dhcp client."
102 # Setup Network Address Translation (NAT)
107 if [ -r /etc/sysconfig/static-nat ]; then
108 if [ "$1" = "on" ]; then
109 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
110 /sbin/ip route add nat $dst via $src
111 /sbin/ip rule add from $src nat $dst
113 elif [ "$1" = "off" ]; then
114 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
115 /sbin/ip rule del from $src nat $dst
116 /sbin/ip route del nat $dst via $src
125 typeset rc arpdev host hwaddr flags neflags
127 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
128 if [ -x /sbin/arp ]; then
129 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
131 show "Setting static ARP entries"; busy
133 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
134 if [ -z "$arpdev" ]; then
137 # ip supports only ip addresses
138 grep "^[0-9]" /etc/ethers | \
139 while read host hwaddr flags; do
145 neflags="nud permanent"
148 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
153 if [ "$rc" -gt 0 ]; then
162 if is_yes "$STATIC_RARP"; then
163 if [ ! -e /proc/net/rarp ]; then
164 _modprobe single -k rarp
166 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
167 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
174 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && $(echo $DEVICE | grep -q ^eth); then
175 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
182 # Set up all IP && IP parameter variables
185 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
186 if [ -z "$DEVICETYPE" ]; then
187 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
190 # Setup DEVICETYPE for special cases.
191 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
195 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
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 [ -n "${IP6_PRIM_IF}" ] ; then
213 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
214 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
216 if [ -n "${IP4_SRC_IF}" ] ; then
217 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
218 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
220 if [ -n "${IP6_SRC_IF}" ] ; then
221 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
222 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
225 # new rc-scripts 0.4.x option
226 if [ -z "$HANDLING" ]; then
230 # set handling for bridge
231 case "$DEVICETYPE" in
232 br|atm|lec|irda|vlan)
237 # Multicast ready devices
238 if is_yes "$MULTICAST"; then
241 case "$DEVICETYPE" in
252 if [ -n "$ARP" ] ; then
253 if is_yes "$ARP"; then
263 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
273 if is_yes "$IPV4_NETWORKING"; then
274 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
275 # set up default gateway
276 if [ -n "${GATEWAY}" ]; then
277 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
281 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
282 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
283 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
285 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
286 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
291 # IPv6 gateway && IPv6 globalroute
292 if is_yes "$IPV6_NETWORKING"; then
293 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
294 # set up default gateway
295 if [ -n "${GATEWAY6}" ]; then
296 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
298 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
299 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
302 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
303 # set up default route
304 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
305 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
306 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
307 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
314 # following function sets up routes not associated to any device
315 # eg. unreachable or blackhole routes
316 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
322 if [ -f /etc/sysconfig/static-routes ]; then
323 if [ "$1" = "on" -o "$1" = "yes" ] ; then
324 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
325 /sbin/ip route add $args
328 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
329 /sbin/ip route del $args 2>/dev/null
335 # Add ONLY IPv4 address (IPv6 address is added automaticly)
338 if is_yes "$IPV4_NETWORKING"; then
339 ip addr add 127.0.0.1/8 dev lo
341 ip link set dev lo up
346 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
347 ip link set dev lo down
352 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
353 _modprobe single -k ipv6
356 if is_yes "$IPX" && is_module "ipx"; then
357 _modprobe single -k ipx
361 # calculate network prefix from given network mask
364 typeset old_IFS netmask prefix endp
373 for n in $netmask ; do
374 for i in 128 64 32 16 8 4 2 1 ; do
375 if [ $(($n & $i)) -ne 0 ]; then
376 if [ $endp -eq 0 ]; then
377 prefix=$(($prefix + 1))
390 # calculate network mask from given prefix
391 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
394 typeset prefix a MASK
397 if [ -n "${1##[0-9]*}" ]; then
412 [ $prefix -ge 8 ] && a=255
413 [ $prefix -le 0 ] && a=0
417 prefix="$(( $prefix - 8))"
418 if [ -z "$MASK" ]; then
429 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
430 get_ppp_device_and_pid ()
432 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
435 read PID ; echo "PID='$PID'"
436 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
437 } < "/var/run/ppp-$DEVNAME.pid")
440 if [ -z "$REALDEVICE" ] ; then
445 # following function setups advanced routing rules
446 # Olgierd Pieczul <wojrus@pld-linux.org>
449 typeset args prio from src
451 if [ -f /etc/sysconfig/static-routes ] ; then
453 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
454 /sbin/ip rule add $args
456 elif is_no "$1"; then
457 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
458 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
464 is_wireless_device ()
466 if [ -x /sbin/iwconfig ]; then
467 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
475 if [ "$2" = "br" ]; then
481 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
482 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
484 WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
487 # wlan mode needs to be first
488 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
489 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
490 iwconfig ${device} nick ${WLAN_NICKNAME}
491 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
492 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
493 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
494 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
495 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
496 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
498 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
499 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
500 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
501 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
502 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
503 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
504 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
505 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
506 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
507 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
508 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
509 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
510 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
511 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
513 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
514 iwconfig ${device} key off
516 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
517 iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
518 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
519 iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
520 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
521 iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
522 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
523 nwid "$(eval echo \$${prefix}WLAN_NWID)"
525 # essid should be last due to network rescanning by wlan devices
526 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
527 essid "$(eval echo \$${prefix}WLAN_ESSID)"
529 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
530 iwconfig ${device} commit
536 [ -x /sbin/mii-tool ] || return 2
537 output=$(LC_ALL=C mii-tool $1 2>&1)
538 echo $output | LC_ALL=C grep -q "link ok" && return 1
539 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
544 [ -x /sbin/ethtool ] || return 2
545 output=$(LC_ALL=C ethtool $1 2>&1)
546 echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
547 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
552 [ -x /sbin/iwconfig ] || return 2
553 output=$(LC_ALL=C iwconfig $1 2>&1)
554 echo $output | LC_ALL=C grep -q "radio off" && return 0 || return 2
559 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
560 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
561 ip link set dev $1 up >/dev/null 2>&1
563 while [ $timeout -le 10 ]; do
570 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
573 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
577 timeout=$((timeout+1))
585 # This must be last line !