2 # $Id: functions.network,v 1.5 2002/08/24 16:05:52 misiek Exp $
4 # This is not a shell script; it provides functions to network scripts
9 DEVNAME=$(basename "$CONFIG" | awk ' { gsub(/^ifcfg-/,NIL); print $0; } ')
11 if echo "$CONFIG"|egrep "^/" >/dev/null 2>&1 ; then
12 if [ -f "$CONFIG" ] ; then
15 elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
16 . "/etc/sysconfig/interfaces/$CONFIG"
18 if [ -n "${PREFIX}" ] && ! $(echo $IPADDR | grep "/" 2>&1 >/dev/null) ; then
19 IPADDR="$IPADDR/$PREFIX"
25 # Notify programs that have requested notification
26 ( cd /var/run/netreport || exit
29 kill -SIGIO $i >/dev/null 2>&1 || \
30 rm -f $i >/dev/null 2>&1
37 if [ "`hostname`" = "(none)" -o "`hostname`" = "localhost" -o \
38 "`hostname`" = "localhost.localdomain" ]; then
47 typeset hostname domain
48 hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
49 domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
51 if [ -n "$hostname" ] ; then
52 echo "$hostname" > /etc/HOSTNAME
56 if [ -n "$domain" ] ; then
58 if ! grep -q "search $domain" /etc/resolv.conf ; then
59 echo "search $domain" >> /etc/resolv.conf.new
61 echo "domain $domain" >> /etc/resolv.conf.new
62 grep -v "^domain " /etc/resolv.conf >> /etc/resolv.conf.new
63 mv -f /etc/resolv.conf.new /etc/resolv.conf
67 # PLD GNU/Linux network functions
68 # Copyright (C) 1999, 2000 Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
70 # Setup Network Address Translation (NAT)
74 if [ -r /etc/sysconfig/static-nat ]; then
75 grep "^[0-9]" /etc/sysconfig/static-nat | while read src via dst; do
76 if [ "$1" = "on" ]; then
77 /sbin/ip route add nat $src via $dst
78 /sbin/ip rule add from $dst nat $src
79 elif [ "$1" = "off" ]; then
80 /sbin/ip rule del from $dst nat $src
81 /sbin/ip route del nat $src via $dst
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
148 if is_yes "$IPV4_NETWORKING" && is_yes "$IPV4_FORWARDING" && [ -f /proc/sys/net/ipv4/ip_forward ];
150 if [ "$1" = "on" ]; then
151 run_cmd "Enabling IPv4 packet forwarding" sysctl -w net.ipv4.ip_forward=1
153 run_cmd "Disabling IPv4 packet forwarding" sysctl -w net.ipv4.ip_forward=0
157 if is_yes "$IPV6_NETWORKING" && is_yes "$IPV6_FORWARDING" && [ -f /proc/sys/net/ipv6/conf/all/forwarding ];
159 if [ "$1" = "on" ]; then
160 run_cmd "Enabling IPv6 packet forwarding" sysctl -w net.ipv6.conf.all.forwarding=1
162 run_cmd "Disabling IPv6 packet forwarding" sysctl -w net.ipv6.conf.all.forwarding=0
167 # Set up all IP && IP parameter variables
170 # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
171 if [ -z "$DEVICETYPE" ]; then
172 DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
175 # Setup DEVICETYPE for special cases.
176 if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
180 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
181 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
183 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
184 if echo "${IP4ADDR}" | grep -q ":" ; then
188 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
189 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
191 if [ -n "${IP6_PRIM_IF}" ] ; then
192 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
193 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
195 if [ -n "${IP4_SRC_IF}" ] ; then
196 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
197 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
199 if [ -n "${IP6_SRC_IF}" ] ; then
200 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
201 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
204 # new rc-scripts 0.4.x option
205 if [ -z "$HANDLING" ]; then
209 # set handling for bridge
210 case "$DEVICETYPE" in
211 br|atm|lec|irda|vlan)
216 # Multicast ready devices
217 if is_yes "$MULTICAST"; then
220 case "$DEVICETYPE" in
231 if [ -n "$ARP" ] ; then
232 if is_yes "$ARP"; then
243 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
253 if is_yes "$IPV4_NETWORKING"; then
254 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
255 # set up default gateway
256 if [ -n "${GATEWAY}" ]; then
257 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink
259 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
260 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
265 # IPv6 gateway && IPv6 globalroute
266 if is_yes "$IPV6_NETWORKING"; then
267 if is_no "${IPV6_FORWARDING}" ; then
268 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
269 # set up default gateway
270 if [ -n "${GATEWAY6}" ]; then
271 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
273 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
274 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
278 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
279 # set up default route
280 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
281 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
282 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
283 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
291 # following function sets up routes not associated to any device
292 # eg. unreachable or blackhole routes
293 # (c) 1999 Jacek Konieczny <jajcus@pld.org.pl>
298 if [ -f /etc/sysconfig/static-routes ]; then
299 if [ "$1" = "on" -o "$1" = "yes" ] ; then
300 egrep "^(none)|(any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
301 /sbin/ip route add $args
304 egrep "^(none)|(any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
305 /sbin/ip route del $args 2>/dev/null
311 # Add ONLY IPv4 address (IPv6 address is added automaticly)
314 if is_yes "$IPV4_NETWORKING"; then
315 ip addr add 127.0.0.1/8 dev lo
317 ip link set dev lo up
322 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
323 ip link set dev lo down
328 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
329 _modprobe single -k ipv6
332 if is_yes "$IPX" && is_module "ipx"; then
333 _modprobe single -k ipx
338 # folowing function calculates network prefix from given network mask
339 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
343 typeset OISF len olen bad prefix
365 if [ "$len" -gt "$olen" ] ; then bad=1; fi
366 prefix=$(($prefix + $len))
371 if [ "$bad" = "0" ]; then
380 # folowing function calculates network mask from given prefix
381 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
385 typeset prefix a MASK
387 if [ "`echo "${1}" | grep "^[0-9]"`" = "" ]; then
402 if [ $prefix -ge 8 ]; then a=255; fi
403 if [ $prefix -le 0 ]; then a=0; fi
407 prefix="$(( $prefix - 8))"
408 if [ -z "$MASK" ]; then
419 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
420 get_ppp_device_and_pid () {
422 if [ -f "/var/run/ppp-$DEVICE.pid" ] ; then
424 read PID ; echo "PID='$PID'"
425 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
426 } < "/var/run/ppp-$DEVICE.pid"`
429 if [ -z "$REALDEVICE" ] ; then
434 # following function setups advanced routing rules
435 # Olgierd Pieczul <wojrus@pld.org.pl>
438 if [ -f /etc/sysconfig/ip-rules ] ; then
439 if [ "$1" = "on" -o "$1" = "yes" ] ; then
440 egrep "^(from|to|iif|tos|fwmark|dev|pref)[[:blank:]]" /etc/sysconfig/ip-rules | while read args; do
441 /sbin/ip rule add $args
444 egrep "^(from|to|iif|tos|fwmark|dev|pref)[[:blank:]]" /etc/sysconfig/ip-rules | while read args; do
445 /sbin/ip rule del $args
453 is_wireless_device () {
454 if [ -x /sbin/iwconfig ]; then
455 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
460 # This must be last line !
461 # vi:syntax=sh:tw=78:ts=8:sw=4