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
80 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
81 DHCP_CLIENT=/sbin/pump
84 if [ -z "$DHCP_CLIENT" ]; then
85 if [ -x /sbin/dhcpcd ]; then
86 DHCP_CLIENT=/sbin/dhcpcd
87 elif [ -x /sbin/dhclient ]; then
88 DHCP_CLIENT=/sbin/dhclient
89 elif [ -x /sbin/dhcpxd ]; then
90 DHCP_CLIENT=/sbin/dhcpxd
91 elif [ -x /sbin/pump ]; then
92 DHCP_CLIENT=/sbin/pump
94 nls "Can't find a dhcp client."
100 # Setup Network Address Translation (NAT)
105 if [ -r /etc/sysconfig/static-nat ]; then
106 if [ "$1" = "on" ]; then
107 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
108 /sbin/ip route add nat $dst via $src
109 /sbin/ip rule add from $src nat $dst
111 elif [ "$1" = "off" ]; then
112 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
113 /sbin/ip rule del from $src nat $dst
114 /sbin/ip route del nat $dst via $src
123 typeset rc arpdev host hwaddr flags neflags
125 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
126 if [ -x /sbin/arp ]; then
127 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
129 show "Setting static ARP entries"; busy
131 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
132 if [ -z "$arpdev" ]; then
135 # ip supports only ip addresses
136 grep "^[0-9]" /etc/ethers | \
137 while read host hwaddr flags; do
143 neflags="nud permanent"
146 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
151 if [ "$rc" -gt 0 ]; then
160 if is_yes "$STATIC_RARP"; then
161 if [ ! -e /proc/net/rarp ]; then
162 _modprobe single -k rarp
164 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
165 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
172 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && `echo $DEVICE | grep -q ^eth`; then
173 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
180 # Set up all IP && IP parameter variables
183 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
184 if [ -z "$DEVICETYPE" ]; then
185 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
188 # Setup DEVICETYPE for special cases.
189 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
193 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
194 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
196 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
197 if echo "${IP4ADDR}" | grep -q ":" ; then
201 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
202 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
204 if [ -n "${IP6_PRIM_IF}" ] ; then
205 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
206 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
208 if [ -n "${IP4_SRC_IF}" ] ; then
209 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
210 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
212 if [ -n "${IP6_SRC_IF}" ] ; then
213 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
214 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
217 # new rc-scripts 0.4.x option
218 if [ -z "$HANDLING" ]; then
222 # set handling for bridge
223 case "$DEVICETYPE" in
224 br|atm|lec|irda|vlan)
229 # Multicast ready devices
230 if is_yes "$MULTICAST"; then
233 case "$DEVICETYPE" in
244 if [ -n "$ARP" ] ; then
245 if is_yes "$ARP"; then
255 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
265 if is_yes "$IPV4_NETWORKING"; then
266 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
267 # set up default gateway
268 if [ -n "${GATEWAY}" ]; then
269 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
273 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
274 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
275 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
277 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
278 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
283 # IPv6 gateway && IPv6 globalroute
284 if is_yes "$IPV6_NETWORKING"; then
285 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
286 # set up default gateway
287 if [ -n "${GATEWAY6}" ]; then
288 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
290 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
291 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
294 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
295 # set up default route
296 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
297 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
298 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
299 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
306 # following function sets up routes not associated to any device
307 # eg. unreachable or blackhole routes
308 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
314 if [ -f /etc/sysconfig/static-routes ]; then
315 if [ "$1" = "on" -o "$1" = "yes" ] ; then
316 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
317 /sbin/ip route add $args
320 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
321 /sbin/ip route del $args 2>/dev/null
327 # Add ONLY IPv4 address (IPv6 address is added automaticly)
330 if is_yes "$IPV4_NETWORKING"; then
331 ip addr add 127.0.0.1/8 dev lo
333 ip link set dev lo up
338 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
339 ip link set dev lo down
344 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
345 _modprobe single -k ipv6
348 if is_yes "$IPX" && is_module "ipx"; then
349 _modprobe single -k ipx
354 # folowing function calculates network prefix from given network mask
355 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
359 typeset len olen bad prefix
366 j=$(echo "$1" | awk -F"." "{ print \$${i} }")
379 [ "$len" -gt "$olen" ] && bad=1
380 prefix=$(($prefix + $len))
384 if [ "$bad" = "0" ]; then
393 # folowing function calculates network mask from given prefix
394 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
398 typeset prefix a MASK
401 if [ "`echo "${1}" | grep "^[0-9]"`" = "" ]; then
416 [ $prefix -ge 8 ] && a=255
417 [ $prefix -le 0 ] && a=0
421 prefix="$(( $prefix - 8))"
422 if [ -z "$MASK" ]; then
433 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
434 get_ppp_device_and_pid ()
436 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
438 read PID ; echo "PID='$PID'"
439 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
440 } < "/var/run/ppp-$DEVNAME.pid"`
443 if [ -z "$REALDEVICE" ] ; then
448 # following function setups advanced routing rules
449 # Olgierd Pieczul <wojrus@pld-linux.org>
452 typeset args prio from src
454 if [ -f /etc/sysconfig/static-routes ] ; then
456 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
457 /sbin/ip rule add $args
459 elif is_no "$1"; then
460 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
461 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
467 is_wireless_device ()
469 if [ -x /sbin/iwconfig ]; then
470 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
478 if [ "$2" = "br" ]; then
484 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
485 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
487 WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
490 # wlan mode needs to be first
491 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
492 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
493 iwconfig ${device} nick ${WLAN_NICKNAME}
494 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
495 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
496 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
497 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
498 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
499 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
501 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
502 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
503 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
504 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
505 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
506 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
507 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
508 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
509 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
510 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
511 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
512 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
513 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
514 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
516 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
517 iwconfig ${device} key off
519 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
520 iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
521 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
522 iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
523 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
524 iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
525 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
526 nwid "$(eval echo \$${prefix}WLAN_NWID)"
528 # essid should be last due to network rescanning by wlan devices
529 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
530 essid "$(eval echo \$${prefix}WLAN_ESSID)"
532 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
533 iwconfig ${device} commit
539 [ -x /sbin/mii-tool ] || return 2
540 output=$(LC_ALL=C mii-tool $1 2>&1)
541 echo $output | LC_ALL=C grep -q "link ok" && return 1
542 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
547 [ -x /sbin/ethtool ] || return 2
548 output=$(LC_ALL=C ethtool $1 2>&1)
549 echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
550 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
555 [ -x /sbin/iwconfig ] || return 2
556 output=$(LC_ALL=C iwconfig $1 2>&1)
557 echo $output | LC_ALL=C grep -q "radio off" && return 0 || return 2
562 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
563 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
564 ip link set dev $1 up >/dev/null 2>&1
566 while [ $timeout -le 10 ]; do
573 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
576 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
580 timeout=$((timeout+1))
588 # This must be last line !