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 "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
31 echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
35 if [ "${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 "$hostname" = "localhost.localdomain" ]; then
65 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
66 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
68 if [ "$hostname" ]; then
69 echo "$hostname" > /etc/HOSTNAME
73 if [ "$domain" ]; then
74 # XXX umask and possible /etc/resolv.conf symlink breakage
75 > /etc/resolv.conf.new
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 LC_ALL=C /sbin/ip route show table all | grep -E "^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 local 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
177 # Set up all IP && IP parameter variables
180 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
181 if [ -z "$DEVICETYPE" ]; then
182 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[\.:]?[0-9]*[\.:]?[0-9]*$/,NUL); print $0 } ')
185 # Setup DEVICETYPE for special cases.
186 if echo ${DEVICE} | LC_ALL=C grep -qE '^[a-z0-9]+\.[0-9]+$'; then
190 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
191 SUBDEVICE=$(echo "$DEVICE" | grep -E "([0-9]+:[0-9]+)")
192 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
194 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
195 # check if ipaddr doesn't contain network length -- use $NETMASK then
196 if [[ "$IP4ADDR" != */* ]] && [ "$NETMASK" ]; then
197 IP4ADDR=$IP4ADDR/$(calcprefix $NETMASK)
200 # check if we have ipv6 or ipv4 address
201 if [[ "${IP4ADDR}" = *:* ]]; then
205 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
206 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
208 if [ "${IP6_PRIM_IF}" ] ; then
209 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
210 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
211 eval IP6ADDRLABEL="\$IP_LABEL${IP6_PRIM_IF:-}"
213 if [ "${IP4_SRC_IF}" ] ; then
214 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
215 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
218 # new rc-scripts 0.4.x option
219 if [ -z "$HANDLING" ]; then
223 # set handling for bridge
224 case "$DEVICETYPE" in
225 br|atm|lec|irda|vlan)
230 # Multicast ready devices
231 if is_yes "$MULTICAST"; then
234 case "$DEVICETYPE" in
246 if is_yes "$ARP"; then
256 if LC_ALL=C ip link show dev ${DEVICE} 2> /dev/null | grep -q UP; then
266 if is_yes "$IPV4_NETWORKING"; then
267 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
268 # set up default gateway
269 if [ "${GATEWAY}" ]; then
270 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
274 # The default gateway could be set via eth0, while bringing up eth1 we shouldn't set default gateway again.
275 # To prevent this message just set GATEWAYDEV=eth0 in /etc/sysconfig/network.
276 nls "Warning: Default gateway already set proably via other interface. Do you need to setup GATEWAYDEV?"
278 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
279 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
284 # IPv6 gateway && IPv6 globalroute
285 if is_yes "$IPV6_NETWORKING"; then
286 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
287 # set up default gateway
288 if [ "${GATEWAY6}" ]; then
289 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
291 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
292 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
295 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
296 # set up default route
297 if [ "${IPV6_GLOBALROUTEGW}" ]; then
298 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
299 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
300 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
307 # following function sets up routes not associated to any device
308 # eg. unreachable or blackhole routes
309 # (c) 1999 Jacek Konieczny <jajcus@pld-linux.org>
315 if [ -f /etc/sysconfig/static-routes ]; then
316 if [ "$1" = "on" -o "$1" = "yes" ] ; then
317 grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
318 /sbin/ip route add $args
321 grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
322 /sbin/ip route del $args 2>/dev/null
328 # Add ONLY IPv4 address (IPv6 address is added automaticly)
331 if is_yes "$IPV4_NETWORKING"; then
332 ip addr add 127.0.0.1/8 dev lo
334 ip link set dev lo up
339 LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
340 ip link set dev lo down
345 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
346 _modprobe single -k ipv6
349 if is_yes "$IPX" && is_module "ipx"; then
350 _modprobe single -k ipx
354 # calculate network prefix from given network mask
357 local old_IFS netmask prefix endp
366 for n in $netmask ; do
367 for i in 128 64 32 16 8 4 2 1 ; do
368 if [ $(($n & $i)) -ne 0 ]; then
369 if [ $endp -eq 0 ]; then
370 prefix=$(($prefix + 1))
383 # calculate network mask from given prefix
384 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
390 if [ "${1##[0-9]*}" ]; then
405 [ $prefix -ge 8 ] && a=255
406 [ $prefix -le 0 ] && a=0
410 prefix="$(( $prefix - 8))"
411 if [ -z "$MASK" ]; then
422 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
423 get_ppp_device_and_pid ()
425 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 local args prio from src
444 if [ -f /etc/sysconfig/static-routes ] ; then
446 grep -E "^(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 LC_ALL=C /sbin/ip rule show | grep -vE -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
476 if val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ -z "$val" ]; then
477 WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
482 # wlan mode needs to be first
483 wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
484 iwconfig ${device} mode "$wlan_mode"
486 iwconfig ${device} nick ${WLAN_NICKNAME}
488 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
489 iwconfig ${device} nwid "$val"
491 if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
492 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
493 iwconfig ${device} freq "$val"
494 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
495 iwconfig ${device} channel "$val"
499 val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
500 iwconfig ${device} sens "$val"
501 val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
502 iwconfig ${device} ap "$val"
503 val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
504 iwconfig ${device} rate "$val"
505 val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
506 iwconfig ${device} rts "$val"
507 val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
508 iwconfig ${device} frag "$val"
510 local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
511 local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
512 # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
513 if [ "$wlan_key" ]; then
514 # wlan key can contain index of key to set
515 # to set other than current keys suffix with 1-4:
517 for idx in 1 2 3 4; do
518 val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
519 [ "$val" ] || continue
520 iwconfig ${device} key "[$idx]" "$val"
522 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
523 iwconfig ${device} key "[$val]"
525 iwconfig ${device} key "$wlan_key"
528 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
531 if is_no "$wlan_encryption"; then
532 iwconfig ${device} key off
534 val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
535 iwconfig ${device} power "$val"
536 val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
537 iwconfig ${device} txpower "$val"
538 val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
539 iwconfig ${device} retry "$val"
540 val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
541 iwconfig ${device} nwid "$val"
543 # essid should be last due to network rescanning by wlan devices
544 val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
545 iwconfig ${device} essid "$val"
547 if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
548 iwconfig ${device} commit
555 # 2 - unsupported, unknown return
558 [ -x /sbin/mii-tool ] || return 2
559 local output=$(LC_ALL=C mii-tool $1 2>&1)
560 echo $output | grep -q "link ok" && return 1
561 echo $output | grep -q "no link" && return 0
568 # 2 - unsupported, unknown return
571 [ -x /sbin/ethtool ] || return 2
572 local output=$(LC_ALL=C ethtool $1 2>&1)
573 echo $output | grep -q "Link detected: yes" && return 1
574 echo $output | grep -q "Link detected: no" && return 0
584 [ -x /sbin/iwconfig ] || return 2
585 local output=$(LC_ALL=C iwconfig "$1" 2>&1)
586 echo "$output" | grep -q "radio off" && return 0
587 # XXX why no 'radio on' check?
596 if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
597 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
598 ip link set dev $1 up >/dev/null 2>&1
600 while [ $timeout -le 10 ]; do
607 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
610 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
614 timeout=$((timeout+1))