2 # $Id: functions.network,v 1.2 2002/05/11 09:50:53 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 # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
176 DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
178 eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
179 if echo "${IP4ADDR}" | grep -q ":" ; then
183 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
184 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
186 if [ -n "${IP6_PRIM_IF}" ] ; then
187 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
188 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
190 if [ -n "${IP4_SRC_IF}" ] ; then
191 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
192 IP4SRCADDR="src $IP4SRCADDR"
194 if [ -n "${IP6_SRC_IF}" ] ; then
195 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
196 IP6SRCADDR="src $IP6SRCADDR"
199 # new rc-scripts 0.4.x option
200 if [ -z "$HANDLING" ]; then
204 # set handling for bridge
205 case "$DEVICETYPE" in
211 # Multicast ready devices
212 if is_yes "$MULTICAST"; then
215 case "$DEVICETYPE" in
226 if [ -n "$ARP" ] ; then
227 if is_yes "$ARP"; then
238 if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
248 if is_yes "$IPV4_NETWORKING"; then
249 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
250 # set up default gateway
251 if [ -n "${GATEWAY}" ]; then
252 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink
254 elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
255 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
260 # IPv6 gateway && IPv6 globalroute
261 if is_yes "$IPV6_NETWORKING"; then
262 if is_no "${IPV6_FORWARDING}" ; then
263 if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
264 # set up default gateway
265 if [ -n "${GATEWAY6}" ]; then
266 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
268 elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
269 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
273 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
274 # set up default route
275 if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
276 ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
277 elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
278 ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
286 # following function sets up routes not associated to any device
287 # eg. unreachable or blackhole routes
288 # (c) 1999 Jacek Konieczny <jajcus@pld.org.pl>
293 if [ -f /etc/sysconfig/static-routes ]; then
294 if [ "$1" = "on" -o "$1" = "yes" ] ; then
295 egrep "^(none)|(any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
296 /sbin/ip route add $args
299 egrep "^(none)|(any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
300 /sbin/ip route del $args 2>/dev/null
306 # Add ONLY IPv4 address (IPv6 address is added automaticly)
309 if is_yes "$IPV4_NETWORKING"; then
310 ip addr add 127.0.0.1/8 dev lo
312 ip link set dev lo up
317 ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
318 ip link set dev lo down
323 if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
324 _modprobe single -k ipv6
327 if is_yes "$IPX" && is_module "ipx"; then
328 _modprobe single -k ipx
333 # folowing function calculates network prefix from given network mask
334 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
338 typeset OISF len olen bad prefix
360 if [ "$len" -gt "$olen" ] ; then bad=1; fi
361 prefix=$(($prefix + $len))
366 if [ "$bad" = "0" ]; then
375 # folowing function calculates network mask from given prefix
376 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
380 typeset prefix a MASK
382 if [ "`echo "${1}" | grep "^[0-9]"`" = "" ]; then
397 if [ $prefix -ge 8 ]; then a=255; fi
398 if [ $prefix -le 0 ]; then a=0; fi
402 prefix="$(( $prefix - 8))"
403 if [ -z "$MASK" ]; then
414 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
415 get_ppp_device_and_pid () {
417 if [ -f "/var/run/ppp-$DEVICE.pid" ] ; then
419 read PID ; echo "PID='$PID'"
420 read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
421 } < "/var/run/ppp-$DEVICE.pid"`
423 if [ -z "$REALDEVICE" ] ; then
427 # following function setups advanced routing rules
428 # Olgierd Pieczul <wojrus@pld.org.pl>
431 if [ -f /etc/sysconfig/ip-rules ] ; then
432 if [ "$1" = "on" -o "$1" = "yes" ] ; then
433 egrep "^(from|to|iif|tos|fwmark|dev|pref)[[:blank:]]" /etc/sysconfig/ip-rules | while read args; do
434 /sbin/ip rule add $args
437 egrep "^(from|to|iif|tos|fwmark|dev|pref)[[:blank:]]" /etc/sysconfig/ip-rules | while read args; do
438 /sbin/ip rule del $args
444 # This must be last line !
445 # vi:syntax=sh:tw=78:ts=8:sw=4