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
30 kill -SIGIO $i >/dev/null 2>&1 || \
31 rm -f $i >/dev/null 2>&1
38 if [ "`hostname`" = "(none)" -o "`hostname`" = "localhost" -o \
39 "`hostname`" = "localhost.localdomain" ]; then
48 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)
72 if [ -r /etc/sysconfig/static-nat ]; then
73 if [ "$1" = "on" ]; then
74 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
75 /sbin/ip route add nat $dst via $src
76 /sbin/ip rule add from $src nat $dst
78 elif [ "$1" = "off" ]; then
79 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
80 /sbin/ip rule del from $src nat $dst
81 /sbin/ip route del nat $dst via $src
90 typeset rc arpdev host hwaddr flags neflags
91 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
92 if [ -x /sbin/arp ]; then
93 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
95 show "Setting static ARP entries"; busy
97 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
98 if [ -z "$arpdev" ]; then
101 # ip supports only ip addresses
102 grep "^[0-9]" /etc/ethers | \
103 while read host hwaddr flags; do
109 neflags="nud permanent"
112 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
117 if [ "$rc" -gt 0 ]; then
126 if is_yes "$STATIC_RARP"; then
127 if [ ! -e /proc/net/rarp ]; then
128 _modprobe single -k rarp
130 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
131 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
138 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && `echo $DEVICE | grep -q ^eth`; then
139 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
146 # Set up all IP && IP parameter variables
149 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
150 if [ -z "$DEVICETYPE" ]; then
151 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
154 # Setup DEVICETYPE for special cases.
155 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
159 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
160 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
162 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
163 if echo "${IP4ADDR}" | grep -q ":" ; then
167 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
168 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
170 if [ -n "${IP6_PRIM_IF}" ] ; then
171 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
172 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
174 if [ -n "${IP4_SRC_IF}" ] ; then
175 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
176 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
178 if [ -n "${IP6_SRC_IF}" ] ; then
179 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
180 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
183 # new rc-scripts 0.4.x option
184 if [ -z "$HANDLING" ]; then
188 # set handling for bridge
189 case "$DEVICETYPE" in
190 br|atm|lec|irda|vlan)
195 # Multicast ready devices
196 if is_yes "$MULTICAST"; then
199 case "$DEVICETYPE" in
210 if [ -n "$ARP" ] ; then
211 if is_yes "$ARP"; then
222 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
232 if is_yes "$IPV4_NETWORKING"; then
233 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
234 # set up default gateway
235 if [ -n "${GATEWAY}" ]; then
236 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink
238 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
239 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
244 # IPv6 gateway && IPv6 globalroute
245 if is_yes "$IPV6_NETWORKING"; then
246 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
247 # set up default gateway
248 if [ -n "${GATEWAY6}" ]; then
249 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
251 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
252 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
255 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
256 # set up default route
257 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
258 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
259 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
260 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
267 # following function sets up routes not associated to any device
268 # eg. unreachable or blackhole routes
269 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
274 if [ -f /etc/sysconfig/static-routes ]; then
275 if [ "$1" = "on" -o "$1" = "yes" ] ; then
276 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
277 /sbin/ip route add $args
280 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
281 /sbin/ip route del $args 2>/dev/null
287 # Add ONLY IPv4 address (IPv6 address is added automaticly)
290 if is_yes "$IPV4_NETWORKING"; then
291 ip addr add 127.0.0.1/8 dev lo
293 ip link set dev lo up
298 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
299 ip link set dev lo down
304 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
305 _modprobe single -k ipv6
308 if is_yes "$IPX" && is_module "ipx"; then
309 _modprobe single -k ipx
314 # folowing function calculates network prefix from given network mask
315 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
319 typeset OISF len olen bad prefix
341 [ "$len" -gt "$olen" ] && bad=1
342 prefix=$(($prefix + $len))
347 if [ "$bad" = "0" ]; then
356 # folowing function calculates network mask from given prefix
357 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
361 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>
413 typeset args prio from src
414 if [ -f /etc/sysconfig/static-routes ] ; then
416 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
417 /sbin/ip rule add $args
419 elif is_no "$1"; then
420 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
421 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
429 is_wireless_device () {
430 if [ -x /sbin/iwconfig ]; then
431 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
439 if [ "$2" = "br" ]; then
445 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
446 WLAN_NICKNAME="$(hostname -f)"
448 WLAN_NICKANME="$(eval echo \$${prefix}WLAN_NICKNAME)"
451 # wlan mode needs to be first
452 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
453 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
454 iwconfig ${device} nickname ${WLAN_NICKNAME}
455 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && \
456 iwconfig ${device} essid "$(eval echo \$${prefix}WLAN_ESSID)"
457 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
458 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
459 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
460 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
461 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
462 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
464 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
465 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
466 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
467 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
468 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
469 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
470 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
471 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
472 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
473 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
474 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
475 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
476 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
477 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
479 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
480 iwconfig ${device} key off
482 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
483 iwconfig ${device} "$(eval echo \$${prefix}WLAN_POWER)"
484 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
485 iwconfig ${device} "$(eval echo \$${prefix}WLAN_TXPOWER)"
486 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
487 iwconfig ${device} "$(eval echo \$${prefix}WLAN_RETRY)"
488 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
489 iwconfig ${device} commit
491 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
492 nwid "$(eval echo \$${prefix}WLAN_NWID)"
494 #essid should be last due to netowork rescanning by wlan devices
495 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
496 essid "$(eval echo \$${prefix}WLAN_ESSID)"
502 [ -x /sbin/mii-tool ] || return 2
503 output=$(LC_ALL=C mii-tool $1 2>&1)
504 echo $output | LC_ALL=C grep -q "link ok" && return 1
505 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
510 [ -x /sbin/ethtool ] || return 2
511 output=$(LC_ALL=C ethtool $1 2>&1)
512 echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
513 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
519 if [ -x /sbin/mii-tool -o -x /sbin/ethtool ]; then
520 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
521 ip link set dev $1 up >/dev/null 2>&1
523 while [ $timeout -le 10 ]; do
528 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
531 if [ $m -eq 2 ] && [ $e -eq 2 ] ; then
535 timeout=$((timeout+1))
543 # This must be last line !
544 # vi:syntax=sh:ts=8:sw=4