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
13 DEVNAME=${DEVNAME##ifcfg-}
14 DEVNAME=${DEVNAME##tnlcfg-}
16 if [[ "$CONFIG" = /* ]]; then
17 if [ -f "$CONFIG" ] ; then
21 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
22 . "/etc/sysconfig/interfaces/$CONFIG"
26 # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
27 # and forgot to alter DEVICE= line you won't accidentally bring down eth0
28 # while executing ifdown eth1. We do that only if configuration file exists
29 # (sometimes ifcfg-xyz isn't needed at all like server-side pppoe pppX interfaces)
30 if [ "$foundconfig" -eq "1" -a -n "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
31 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
35 if [ -n "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
36 IPADDR="$IPADDR/$PREFIX"
42 # Notify programs that have requested notification
43 ( cd /var/run/netreport || exit
45 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
46 rm -f $i >/dev/null 2>&1
53 local hostname=$(hostname)
54 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o \
55 "$hostname" = "localhost.localdomain" ]; then
64 typeset hostname domain
66 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
67 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
69 if [ -n "$hostname" ] ; then
70 echo "$hostname" > /etc/HOSTNAME
74 if [ -n "$domain" ] ; then
76 if ! grep -q "search $domain" /etc/resolv.conf ; then
77 echo "search $domain" >> /etc/resolv.conf.new
79 echo "domain $domain" >> /etc/resolv.conf.new
80 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
81 mv -f /etc/resolv.conf.new /etc/resolv.conf
87 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
88 DHCP_CLIENT=/sbin/pump
91 if [ -z "$DHCP_CLIENT" ]; then
92 if [ -x /sbin/dhcpcd ]; then
93 DHCP_CLIENT=/sbin/dhcpcd
94 elif [ -x /sbin/dhclient ]; then
95 DHCP_CLIENT=/sbin/dhclient
96 elif [ -x /sbin/dhcpxd ]; then
97 DHCP_CLIENT=/sbin/dhcpxd
98 elif [ -x /sbin/pump ]; then
99 DHCP_CLIENT=/sbin/pump
101 nls "Can't find a dhcp client."
107 # Setup Network Address Translation (NAT)
112 if [ -r /etc/sysconfig/static-nat ]; then
113 if [ "$1" = "on" ]; then
114 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
115 /sbin/ip route add nat $dst via $src
116 /sbin/ip rule add from $src nat $dst
118 elif [ "$1" = "off" ]; then
119 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
120 /sbin/ip rule del from $src nat $dst
121 /sbin/ip route del nat $dst via $src
130 typeset rc arpdev host hwaddr flags neflags
132 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
133 if [ -x /sbin/arp ]; then
134 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
136 show "Setting static ARP entries"; busy
138 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
139 if [ -z "$arpdev" ]; then
142 # ip supports only ip addresses
143 grep "^[0-9]" /etc/ethers | \
144 while read host hwaddr flags; do
150 neflags="nud permanent"
153 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
158 if [ "$rc" -gt 0 ]; then
167 if is_yes "$STATIC_RARP"; then
168 if [ ! -e /proc/net/rarp ]; then
169 _modprobe single -k rarp
171 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
172 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
179 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && $(echo $DEVICE | grep -q ^eth); then
180 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
187 # Set up all IP && IP parameter variables
190 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
191 if [ -z "$DEVICETYPE" ]; then
192 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
195 # Setup DEVICETYPE for special cases.
196 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
200 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
201 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
203 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
204 # check if ipaddr doesn't contain network length -- use $NETMASK then
205 if [[ "$IP4ADDR" != */* ]] && [ "$NETMASK" ]; then
206 IP4ADDR=$IP4ADDR/$(calcprefix $NETMASK)
209 # check if we have ipv6 or ipv4 address
210 if [[ "${IP4ADDR}" = *:* ]]; then
214 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
215 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
217 if [ -n "${IP6_PRIM_IF}" ] ; then
218 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
219 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
221 if [ -n "${IP4_SRC_IF}" ] ; then
222 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
223 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
225 if [ -n "${IP6_SRC_IF}" ] ; then
226 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
227 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
230 # new rc-scripts 0.4.x option
231 if [ -z "$HANDLING" ]; then
235 # set handling for bridge
236 case "$DEVICETYPE" in
237 br|atm|lec|irda|vlan)
242 # Multicast ready devices
243 if is_yes "$MULTICAST"; then
246 case "$DEVICETYPE" in
257 if [ -n "$ARP" ] ; then
258 if is_yes "$ARP"; then
268 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
278 if is_yes "$IPV4_NETWORKING"; then
279 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
280 # set up default gateway
281 if [ -n "${GATEWAY}" ]; then
282 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
286 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
287 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
288 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
290 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
291 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
296 # IPv6 gateway && IPv6 globalroute
297 if is_yes "$IPV6_NETWORKING"; then
298 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
299 # set up default gateway
300 if [ -n "${GATEWAY6}" ]; then
301 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
303 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
304 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
307 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
308 # set up default route
309 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
310 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
311 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
312 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
319 # following function sets up routes not associated to any device
320 # eg. unreachable or blackhole routes
321 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
327 if [ -f /etc/sysconfig/static-routes ]; then
328 if [ "$1" = "on" -o "$1" = "yes" ] ; then
329 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
330 /sbin/ip route add $args
333 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
334 /sbin/ip route del $args 2>/dev/null
340 # Add ONLY IPv4 address (IPv6 address is added automaticly)
343 if is_yes "$IPV4_NETWORKING"; then
344 ip addr add 127.0.0.1/8 dev lo
346 ip link set dev lo up
351 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
352 ip link set dev lo down
357 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
358 _modprobe single -k ipv6
361 if is_yes "$IPX" && is_module "ipx"; then
362 _modprobe single -k ipx
366 # calculate network prefix from given network mask
369 typeset old_IFS netmask prefix endp
378 for n in $netmask ; do
379 for i in 128 64 32 16 8 4 2 1 ; do
380 if [ $(($n & $i)) -ne 0 ]; then
381 if [ $endp -eq 0 ]; then
382 prefix=$(($prefix + 1))
395 # calculate network mask from given prefix
396 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
399 typeset prefix a MASK
402 if [ -n "${1##[0-9]*}" ]; then
417 [ $prefix -ge 8 ] && a=255
418 [ $prefix -le 0 ] && a=0
422 prefix="$(( $prefix - 8))"
423 if [ -z "$MASK" ]; then
434 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
435 get_ppp_device_and_pid ()
437 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
440 read PID ; echo "PID='$PID'"
441 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
442 } < "/var/run/ppp-$DEVNAME.pid")
445 if [ -z "$REALDEVICE" ] ; then
450 # following function setups advanced routing rules
451 # Olgierd Pieczul <wojrus@pld-linux.org>
454 typeset args prio from src
456 if [ -f /etc/sysconfig/static-routes ] ; then
458 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
459 /sbin/ip rule add $args
461 elif is_no "$1"; then
462 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
463 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
469 is_wireless_device ()
471 if [ -x /sbin/iwconfig ]; then
472 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
480 if [ "$2" = "br" ]; then
486 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
487 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
489 WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
492 # wlan mode needs to be first
493 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
494 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
495 iwconfig ${device} nick ${WLAN_NICKNAME}
496 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
497 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
498 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
499 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
500 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
501 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
503 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
504 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
505 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
506 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
507 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
508 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
509 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
510 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
511 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
512 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
513 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
514 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
515 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
516 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
518 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
519 iwconfig ${device} key off
521 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
522 iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
523 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
524 iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
525 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
526 iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
527 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
528 nwid "$(eval echo \$${prefix}WLAN_NWID)"
530 # essid should be last due to network rescanning by wlan devices
531 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
532 essid "$(eval echo \$${prefix}WLAN_ESSID)"
534 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
535 iwconfig ${device} commit
541 [ -x /sbin/mii-tool ] || return 2
542 output=$(LC_ALL=C mii-tool $1 2>&1)
543 echo $output | grep -q "link ok" && return 1
544 echo $output | grep -q "no link" && return 0 || return 2
549 [ -x /sbin/ethtool ] || return 2
550 output=$(LC_ALL=C ethtool $1 2>&1)
551 echo $output | grep -q "Link detected: yes" && return 1
552 echo $output | grep -q "Link detected: no" && return 0 || return 2
557 [ -x /sbin/iwconfig ] || return 2
558 output=$(LC_ALL=C iwconfig $1 2>&1)
559 echo $output | grep -q "radio off" && return 0 || return 2
564 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
565 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
566 ip link set dev $1 up >/dev/null 2>&1
568 while [ $timeout -le 10 ]; do
575 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
578 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
582 timeout=$((timeout+1))
590 # This must be last line !