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
10 DEVNAME=${CONFIG##*/ifcfg-}
12 if [[ "$CONFIG" = /* ]]; then
13 if [ -f "$CONFIG" ] ; then
16 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
17 . "/etc/sysconfig/interfaces/$CONFIG"
20 # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
21 # and forgot to alter DEVICE= line you won't accidentally bring down eth0
22 # while executing ifdown eth1.
23 if [ -n "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
24 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
28 if [ -n "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
29 IPADDR="$IPADDR/$PREFIX"
35 # Notify programs that have requested notification
36 ( cd /var/run/netreport || exit
38 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
39 rm -f $i >/dev/null 2>&1
46 local hostname=$(hostname)
47 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o \
48 "$hostname" = "localhost.localdomain" ]; then
57 typeset hostname domain
59 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
60 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
62 if [ -n "$hostname" ] ; then
63 echo "$hostname" > /etc/HOSTNAME
67 if [ -n "$domain" ] ; then
69 if ! grep -q "search $domain" /etc/resolv.conf ; then
70 echo "search $domain" >> /etc/resolv.conf.new
72 echo "domain $domain" >> /etc/resolv.conf.new
73 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
74 mv -f /etc/resolv.conf.new /etc/resolv.conf
78 # Setup Network Address Translation (NAT)
83 if [ -r /etc/sysconfig/static-nat ]; then
84 if [ "$1" = "on" ]; then
85 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
86 /sbin/ip route add nat $dst via $src
87 /sbin/ip rule add from $src nat $dst
89 elif [ "$1" = "off" ]; then
90 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
91 /sbin/ip rule del from $src nat $dst
92 /sbin/ip route del nat $dst via $src
101 typeset rc arpdev host hwaddr flags neflags
103 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
104 if [ -x /sbin/arp ]; then
105 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
107 show "Setting static ARP entries"; busy
109 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
110 if [ -z "$arpdev" ]; then
113 # ip supports only ip addresses
114 grep "^[0-9]" /etc/ethers | \
115 while read host hwaddr flags; do
121 neflags="nud permanent"
124 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
129 if [ "$rc" -gt 0 ]; then
138 if is_yes "$STATIC_RARP"; then
139 if [ ! -e /proc/net/rarp ]; then
140 _modprobe single -k rarp
142 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
143 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
150 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && `echo $DEVICE | grep -q ^eth`; then
151 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
158 # Set up all IP && IP parameter variables
161 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
162 if [ -z "$DEVICETYPE" ]; then
163 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
166 # Setup DEVICETYPE for special cases.
167 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
171 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
172 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
174 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
175 if echo "${IP4ADDR}" | grep -q ":" ; then
179 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
180 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
182 if [ -n "${IP6_PRIM_IF}" ] ; then
183 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
184 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
186 if [ -n "${IP4_SRC_IF}" ] ; then
187 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
188 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
190 if [ -n "${IP6_SRC_IF}" ] ; then
191 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
192 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
195 # new rc-scripts 0.4.x option
196 if [ -z "$HANDLING" ]; then
200 # set handling for bridge
201 case "$DEVICETYPE" in
202 br|atm|lec|irda|vlan)
207 # Multicast ready devices
208 if is_yes "$MULTICAST"; then
211 case "$DEVICETYPE" in
222 if [ -n "$ARP" ] ; then
223 if is_yes "$ARP"; then
233 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
243 if is_yes "$IPV4_NETWORKING"; then
244 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
245 # set up default gateway
246 if [ -n "${GATEWAY}" ]; then
247 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
251 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
252 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
253 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
255 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
256 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
261 # IPv6 gateway && IPv6 globalroute
262 if is_yes "$IPV6_NETWORKING"; then
263 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
264 # set up default gateway
265 if [ -n "${GATEWAY6}" ]; then
266 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
268 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
269 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
272 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
273 # set up default route
274 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
275 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
276 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
277 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
284 # following function sets up routes not associated to any device
285 # eg. unreachable or blackhole routes
286 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
292 if [ -f /etc/sysconfig/static-routes ]; then
293 if [ "$1" = "on" -o "$1" = "yes" ] ; then
294 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
295 /sbin/ip route add $args
298 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
299 /sbin/ip route del $args 2>/dev/null
305 # Add ONLY IPv4 address (IPv6 address is added automaticly)
308 if is_yes "$IPV4_NETWORKING"; then
309 ip addr add 127.0.0.1/8 dev lo
311 ip link set dev lo up
316 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
317 ip link set dev lo down
322 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
323 _modprobe single -k ipv6
326 if is_yes "$IPX" && is_module "ipx"; then
327 _modprobe single -k ipx
332 # folowing function calculates network prefix from given network mask
333 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
337 typeset len olen bad prefix
344 j=$(echo "$1" | awk -F"." "{ print \$${i} }")
357 [ "$len" -gt "$olen" ] && bad=1
358 prefix=$(($prefix + $len))
362 if [ "$bad" = "0" ]; then
371 # folowing function calculates network mask from given prefix
372 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
376 typeset prefix a MASK
379 if [ "`echo "${1}" | grep "^[0-9]"`" = "" ]; then
394 [ $prefix -ge 8 ] && a=255
395 [ $prefix -le 0 ] && a=0
399 prefix="$(( $prefix - 8))"
400 if [ -z "$MASK" ]; then
411 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
412 get_ppp_device_and_pid ()
414 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
416 read PID ; echo "PID='$PID'"
417 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
418 } < "/var/run/ppp-$DEVNAME.pid"`
421 if [ -z "$REALDEVICE" ] ; then
426 # following function setups advanced routing rules
427 # Olgierd Pieczul <wojrus@pld-linux.org>
430 typeset args prio from src
432 if [ -f /etc/sysconfig/static-routes ] ; then
434 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
435 /sbin/ip rule add $args
437 elif is_no "$1"; then
438 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
439 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
445 is_wireless_device ()
447 if [ -x /sbin/iwconfig ]; then
448 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
456 if [ "$2" = "br" ]; then
462 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
463 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
465 WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
468 # wlan mode needs to be first
469 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
470 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
471 iwconfig ${device} nick ${WLAN_NICKNAME}
472 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
473 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
474 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
475 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
476 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
477 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
479 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
480 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
481 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
482 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
483 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
484 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
485 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
486 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
487 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
488 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
489 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
490 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
491 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
492 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
494 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
495 iwconfig ${device} key off
497 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
498 iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
499 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
500 iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
501 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
502 iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
503 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
504 nwid "$(eval echo \$${prefix}WLAN_NWID)"
506 # essid should be last due to network rescanning by wlan devices
507 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
508 essid "$(eval echo \$${prefix}WLAN_ESSID)"
510 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
511 iwconfig ${device} commit
517 [ -x /sbin/mii-tool ] || return 2
518 output=$(LC_ALL=C mii-tool $1 2>&1)
519 echo $output | LC_ALL=C grep -q "link ok" && return 1
520 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
525 [ -x /sbin/ethtool ] || return 2
526 output=$(LC_ALL=C ethtool $1 2>&1)
527 echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
528 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
533 [ -x /sbin/iwconfig ] || return 2
534 output=$(LC_ALL=C iwconfig $1 2>&1)
535 echo $output | LC_ALL=C grep -q "radio off" && return 0 || return 2
540 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
541 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
542 ip link set dev $1 up >/dev/null 2>&1
544 while [ $timeout -le 10 ]; do
551 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
554 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
558 timeout=$((timeout+1))
566 # This must be last line !