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=$(basename "$CONFIG" | awk ' { gsub(/^ifcfg-/,NIL); print $0; } ')
12 if echo "$CONFIG"|egrep "^/" >/dev/null 2>&1 ; then
13 if [ -f "$CONFIG" ] ; then
16 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
17 . "/etc/sysconfig/interfaces/$CONFIG"
19 if [ -n "${PREFIX}" ] && ! $(echo $IPADDR | grep "/" 2>&1 >/dev/null) ; then
20 IPADDR="$IPADDR/$PREFIX"
26 # Notify programs that have requested notification
27 ( cd /var/run/netreport || exit
29 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
30 rm -f $i >/dev/null 2>&1
37 if [ "`hostname`" = "(none)" -o "`hostname`" = "localhost" -o \
38 "`hostname`" = "localhost.localdomain" ]; then
47 typeset hostname domain
49 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
50 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
52 if [ -n "$hostname" ] ; then
53 echo "$hostname" > /etc/HOSTNAME
57 if [ -n "$domain" ] ; then
59 if ! grep -q "search $domain" /etc/resolv.conf ; then
60 echo "search $domain" >> /etc/resolv.conf.new
62 echo "domain $domain" >> /etc/resolv.conf.new
63 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
64 mv -f /etc/resolv.conf.new /etc/resolv.conf
68 # Setup Network Address Translation (NAT)
73 if [ -r /etc/sysconfig/static-nat ]; then
74 if [ "$1" = "on" ]; then
75 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
76 /sbin/ip route add nat $dst via $src
77 /sbin/ip rule add from $src nat $dst
79 elif [ "$1" = "off" ]; then
80 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
81 /sbin/ip rule del from $src nat $dst
82 /sbin/ip route del nat $dst via $src
91 typeset rc arpdev host hwaddr flags neflags
93 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
94 if [ -x /sbin/arp ]; then
95 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
97 show "Setting static ARP entries"; busy
99 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
100 if [ -z "$arpdev" ]; then
103 # ip supports only ip addresses
104 grep "^[0-9]" /etc/ethers | \
105 while read host hwaddr flags; do
111 neflags="nud permanent"
114 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
119 if [ "$rc" -gt 0 ]; then
128 if is_yes "$STATIC_RARP"; then
129 if [ ! -e /proc/net/rarp ]; then
130 _modprobe single -k rarp
132 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
133 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
140 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && `echo $DEVICE | grep -q ^eth`; then
141 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
148 # Set up all IP && IP parameter variables
151 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
152 if [ -z "$DEVICETYPE" ]; then
153 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
156 # Setup DEVICETYPE for special cases.
157 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
161 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
162 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
164 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
165 if echo "${IP4ADDR}" | grep -q ":" ; then
169 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
170 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
172 if [ -n "${IP6_PRIM_IF}" ] ; then
173 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
174 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
176 if [ -n "${IP4_SRC_IF}" ] ; then
177 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
178 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
180 if [ -n "${IP6_SRC_IF}" ] ; then
181 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
182 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
185 # new rc-scripts 0.4.x option
186 if [ -z "$HANDLING" ]; then
190 # set handling for bridge
191 case "$DEVICETYPE" in
192 br|atm|lec|irda|vlan)
197 # Multicast ready devices
198 if is_yes "$MULTICAST"; then
201 case "$DEVICETYPE" in
212 if [ -n "$ARP" ] ; then
213 if is_yes "$ARP"; then
223 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
233 if is_yes "$IPV4_NETWORKING"; then
234 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
235 # set up default gateway
236 if [ -n "${GATEWAY}" ]; then
237 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink
239 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
240 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
245 # IPv6 gateway && IPv6 globalroute
246 if is_yes "$IPV6_NETWORKING"; then
247 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
248 # set up default gateway
249 if [ -n "${GATEWAY6}" ]; then
250 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
252 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
253 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
256 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
257 # set up default route
258 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
259 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
260 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
261 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
268 # following function sets up routes not associated to any device
269 # eg. unreachable or blackhole routes
270 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
276 if [ -f /etc/sysconfig/static-routes ]; then
277 if [ "$1" = "on" -o "$1" = "yes" ] ; then
278 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
279 /sbin/ip route add $args
282 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
283 /sbin/ip route del $args 2>/dev/null
289 # Add ONLY IPv4 address (IPv6 address is added automaticly)
292 if is_yes "$IPV4_NETWORKING"; then
293 ip addr add 127.0.0.1/8 dev lo
295 ip link set dev lo up
300 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
301 ip link set dev lo down
306 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
307 _modprobe single -k ipv6
310 if is_yes "$IPX" && is_module "ipx"; then
311 _modprobe single -k ipx
316 # folowing function calculates network prefix from given network mask
317 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
321 typeset len olen bad prefix
328 j=$(echo "$1" | awk -F"." "{ print \$${i} }")
341 [ "$len" -gt "$olen" ] && bad=1
342 prefix=$(($prefix + $len))
346 if [ "$bad" = "0" ]; then
355 # folowing function calculates network mask from given prefix
356 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
360 typeset prefix a MASK
363 if [ "`echo "${1}" | grep "^[0-9]"`" = "" ]; then
378 [ $prefix -ge 8 ] && a=255
379 [ $prefix -le 0 ] && a=0
383 prefix="$(( $prefix - 8))"
384 if [ -z "$MASK" ]; then
395 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
396 get_ppp_device_and_pid ()
398 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
400 read PID ; echo "PID='$PID'"
401 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
402 } < "/var/run/ppp-$DEVNAME.pid"`
405 if [ -z "$REALDEVICE" ] ; then
410 # following function setups advanced routing rules
411 # Olgierd Pieczul <wojrus@pld-linux.org>
414 typeset args prio from src
416 if [ -f /etc/sysconfig/static-routes ] ; then
418 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
419 /sbin/ip rule add $args
421 elif is_no "$1"; then
422 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
423 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
429 is_wireless_device ()
431 if [ -x /sbin/iwconfig ]; then
432 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
440 if [ "$2" = "br" ]; then
446 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
447 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
449 WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
452 # wlan mode needs to be first
453 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
454 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
455 iwconfig ${device} nickname ${WLAN_NICKNAME}
456 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
457 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
458 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
459 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
460 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
461 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
463 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
464 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
465 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
466 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
467 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
468 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
469 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
470 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
471 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
472 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
473 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
474 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
475 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
476 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
478 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
479 iwconfig ${device} key off
481 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
482 iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
483 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
484 iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
485 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
486 iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
487 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
488 nwid "$(eval echo \$${prefix}WLAN_NWID)"
490 # essid should be last due to network rescanning by wlan devices
491 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
492 essid "$(eval echo \$${prefix}WLAN_ESSID)"
494 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
495 iwconfig ${device} commit
501 [ -x /sbin/mii-tool ] || return 2
502 output=$(LC_ALL=C mii-tool $1 2>&1)
503 echo $output | LC_ALL=C grep -q "link ok" && return 1
504 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
509 [ -x /sbin/ethtool ] || return 2
510 output=$(LC_ALL=C ethtool $1 2>&1)
511 echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
512 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
517 [ -x /sbin/iwconfig ] || return 2
518 output=$(LC_ALL=C iwconfig $1 2>&1)
519 echo $output | LC_ALL=C grep -q "radio off" && return 0 || return 2
524 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
525 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
526 ip link set dev $1 up >/dev/null 2>&1
528 while [ $timeout -le 10 ]; do
535 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
538 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
542 timeout=$((timeout+1))
550 # This must be last line !