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
11 DEVNAME=${DEVNAME##ifcfg-}
12 DEVNAME=${DEVNAME##tnlcfg-}
14 if [[ "$CONFIG" = /* ]]; then
15 if [ -f "$CONFIG" ] ; then
18 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
19 . "/etc/sysconfig/interfaces/$CONFIG"
22 # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
23 # and forgot to alter DEVICE= line you won't accidentally bring down eth0
24 # while executing ifdown eth1.
25 if [ -n "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
26 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
30 if [ -n "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
31 IPADDR="$IPADDR/$PREFIX"
37 # Notify programs that have requested notification
38 ( cd /var/run/netreport || exit
40 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
41 rm -f $i >/dev/null 2>&1
48 local hostname=$(hostname)
49 if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o \
50 "$hostname" = "localhost.localdomain" ]; then
59 typeset hostname domain
61 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
62 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
64 if [ -n "$hostname" ] ; then
65 echo "$hostname" > /etc/HOSTNAME
69 if [ -n "$domain" ] ; then
71 if ! grep -q "search $domain" /etc/resolv.conf ; then
72 echo "search $domain" >> /etc/resolv.conf.new
74 echo "domain $domain" >> /etc/resolv.conf.new
75 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
76 mv -f /etc/resolv.conf.new /etc/resolv.conf
82 if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
83 DHCP_CLIENT=/sbin/pump
86 if [ -z "$DHCP_CLIENT" ]; then
87 if [ -x /sbin/dhcpcd ]; then
88 DHCP_CLIENT=/sbin/dhcpcd
89 elif [ -x /sbin/dhclient ]; then
90 DHCP_CLIENT=/sbin/dhclient
91 elif [ -x /sbin/dhcpxd ]; then
92 DHCP_CLIENT=/sbin/dhcpxd
93 elif [ -x /sbin/pump ]; then
94 DHCP_CLIENT=/sbin/pump
96 nls "Can't find a dhcp client."
102 # Setup Network Address Translation (NAT)
107 if [ -r /etc/sysconfig/static-nat ]; then
108 if [ "$1" = "on" ]; then
109 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
110 /sbin/ip route add nat $dst via $src
111 /sbin/ip rule add from $src nat $dst
113 elif [ "$1" = "off" ]; then
114 /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
115 /sbin/ip rule del from $src nat $dst
116 /sbin/ip route del nat $dst via $src
125 typeset rc arpdev host hwaddr flags neflags
127 if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
128 if [ -x /sbin/arp ]; then
129 run_cmd "Setting static ARP entries" /sbin/arp -f /etc/ethers
131 show "Setting static ARP entries"; busy
133 arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
134 if [ -z "$arpdev" ]; then
137 # ip supports only ip addresses
138 grep "^[0-9]" /etc/ethers | \
139 while read host hwaddr flags; do
145 neflags="nud permanent"
148 if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
153 if [ "$rc" -gt 0 ]; then
162 if is_yes "$STATIC_RARP"; then
163 if [ ! -e /proc/net/rarp ]; then
164 _modprobe single -k rarp
166 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
167 run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
174 if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && `echo $DEVICE | grep -q ^eth`; then
175 /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
182 # Set up all IP && IP parameter variables
185 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
186 if [ -z "$DEVICETYPE" ]; then
187 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
190 # Setup DEVICETYPE for special cases.
191 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
195 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
196 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
198 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
199 if echo "${IP4ADDR}" | grep -q ":" ; then
203 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
204 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
206 if [ -n "${IP6_PRIM_IF}" ] ; then
207 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
208 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
210 if [ -n "${IP4_SRC_IF}" ] ; then
211 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
212 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
214 if [ -n "${IP6_SRC_IF}" ] ; then
215 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
216 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
219 # new rc-scripts 0.4.x option
220 if [ -z "$HANDLING" ]; then
224 # set handling for bridge
225 case "$DEVICETYPE" in
226 br|atm|lec|irda|vlan)
231 # Multicast ready devices
232 if is_yes "$MULTICAST"; then
235 case "$DEVICETYPE" in
246 if [ -n "$ARP" ] ; then
247 if is_yes "$ARP"; then
257 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
267 if is_yes "$IPV4_NETWORKING"; then
268 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
269 # set up default gateway
270 if [ -n "${GATEWAY}" ]; then
271 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
275 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
276 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
277 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
279 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
280 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
285 # IPv6 gateway && IPv6 globalroute
286 if is_yes "$IPV6_NETWORKING"; then
287 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
288 # set up default gateway
289 if [ -n "${GATEWAY6}" ]; then
290 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
292 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
293 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
296 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
297 # set up default route
298 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
299 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
300 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
301 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
308 # following function sets up routes not associated to any device
309 # eg. unreachable or blackhole routes
310 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
316 if [ -f /etc/sysconfig/static-routes ]; then
317 if [ "$1" = "on" -o "$1" = "yes" ] ; then
318 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
319 /sbin/ip route add $args
322 egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
323 /sbin/ip route del $args 2>/dev/null
329 # Add ONLY IPv4 address (IPv6 address is added automaticly)
332 if is_yes "$IPV4_NETWORKING"; then
333 ip addr add 127.0.0.1/8 dev lo
335 ip link set dev lo up
340 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
341 ip link set dev lo down
346 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
347 _modprobe single -k ipv6
350 if is_yes "$IPX" && is_module "ipx"; then
351 _modprobe single -k ipx
355 # calculate network prefix from given network mask
358 typeset old_IFS netmask prefix endp
367 for n in $netmask ; do
368 for i in 128 64 32 16 8 4 2 1 ; do
369 if [ $(($n & $i)) -ne 0 ]; then
370 if [ $endp -eq 0 ]; then
371 prefix=$(($prefix + 1))
384 # calculate network mask from given prefix
385 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
388 typeset prefix a MASK
391 if [ -n "${1##[0-9]*}" ]; then
406 [ $prefix -ge 8 ] && a=255
407 [ $prefix -le 0 ] && a=0
411 prefix="$(( $prefix - 8))"
412 if [ -z "$MASK" ]; then
423 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
424 get_ppp_device_and_pid ()
426 if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
428 read PID ; echo "PID='$PID'"
429 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
430 } < "/var/run/ppp-$DEVNAME.pid"`
433 if [ -z "$REALDEVICE" ] ; then
438 # following function setups advanced routing rules
439 # Olgierd Pieczul <wojrus@pld-linux.org>
442 typeset args prio from src
444 if [ -f /etc/sysconfig/static-routes ] ; then
446 egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
447 /sbin/ip rule add $args
449 elif is_no "$1"; then
450 /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
451 [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
457 is_wireless_device ()
459 if [ -x /sbin/iwconfig ]; then
460 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
468 if [ "$2" = "br" ]; then
474 if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
475 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
477 WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
480 # wlan mode needs to be first
481 [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
482 iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
483 iwconfig ${device} nick ${WLAN_NICKNAME}
484 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
485 iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
486 if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
487 iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
488 elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
489 iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
491 [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
492 iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
493 [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
494 iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
495 [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
496 iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
497 [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
498 iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
499 [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
500 iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
501 if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
502 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
503 [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
504 iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
506 if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
507 iwconfig ${device} key off
509 [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
510 iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
511 [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
512 iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
513 [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
514 iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
515 [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
516 nwid "$(eval echo \$${prefix}WLAN_NWID)"
518 # essid should be last due to network rescanning by wlan devices
519 [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
520 essid "$(eval echo \$${prefix}WLAN_ESSID)"
522 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
523 iwconfig ${device} commit
529 [ -x /sbin/mii-tool ] || return 2
530 output=$(LC_ALL=C mii-tool $1 2>&1)
531 echo $output | LC_ALL=C grep -q "link ok" && return 1
532 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
537 [ -x /sbin/ethtool ] || return 2
538 output=$(LC_ALL=C ethtool $1 2>&1)
539 echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
540 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
545 [ -x /sbin/iwconfig ] || return 2
546 output=$(LC_ALL=C iwconfig $1 2>&1)
547 echo $output | LC_ALL=C grep -q "radio off" && return 0 || return 2
552 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
553 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
554 ip link set dev $1 up >/dev/null 2>&1
556 while [ $timeout -le 10 ]; do
563 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
566 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
570 timeout=$((timeout+1))
578 # This must be last line !