]> git.pld-linux.org Git - projects/rc-scripts.git/blob - sysconfig/network-scripts/functions.network
- allow WLAN_MODE=managed in lowercase (as it's in doc)
[projects/rc-scripts.git] / sysconfig / network-scripts / functions.network
1 #!/bin/sh - keep it for file(1) to get bourne shell script result
2 #
3 # $Id$
4 #
5 # This is not a shell script; it provides functions to network scripts
6 # that source it.
7
8 source_config ()
9 {
10         typeset foundconfig=0
11
12         DEVNAME=${CONFIG##*/}
13         DEVNAME=${DEVNAME##ifcfg-}
14         DEVNAME=${DEVNAME##tnlcfg-}
15
16         if [[ "$CONFIG" = /* ]]; then
17                 if [ -f "$CONFIG" ] ; then
18                         . "$CONFIG"
19                         foundconfig=1
20                 fi
21         elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
22                 . "/etc/sysconfig/interfaces/$CONFIG"
23                 foundconfig=1
24         fi
25
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!"
32                 exit 1
33         fi
34
35         if [ "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
36                 IPADDR="$IPADDR/$PREFIX"
37         fi
38 }
39
40 do_netreport ()
41 {
42         # Notify programs that have requested notification
43         ( cd /var/run/netreport || exit
44         for i in * ; do
45                 [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
46                         rm -f $i >/dev/null 2>&1
47         done
48         )
49 }
50
51 need_hostname()
52 {
53         local hostname=$(hostname)
54         if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o \
55              "$hostname" = "localhost.localdomain" ]; then
56                 NEEDHOSTNAME=yes
57         else
58                 unset NEEDHOSTNAME
59         fi
60 }
61
62 set_hostname()
63 {
64         typeset hostname domain
65
66         hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
67         domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
68
69         if [ "$hostname" ]; then
70                 echo "$hostname" > /etc/HOSTNAME
71                 hostname $hostname
72         fi
73
74         if [ "$domain" ]; then
75                 >/etc/resolv.conf.new
76                 if ! grep -q "search $domain" /etc/resolv.conf ; then
77                         echo "search $domain" >> /etc/resolv.conf.new
78                 fi
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
82         fi
83 }
84
85 set_dhcpclient()
86 {
87         if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
88                 DHCP_CLIENT=/sbin/pump
89         fi
90
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
100                 else
101                         nls "Can't find a dhcp client."
102                         exit 1
103                 fi
104         fi
105 }
106
107 # Setup Network Address Translation (NAT)
108 setup_nat()
109 {
110         typeset src via dst
111
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
117                         done
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
122                         done
123                 fi
124         fi
125 }
126
127 # Setup static ARP
128 static_arp()
129 {
130         typeset rc arpdev host hwaddr flags neflags
131
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
135                 else
136                         show "Setting static ARP entries"; busy
137                         rc=0
138                         arpdev=$(ip link show | awk -F':' '(/UP/) && ! (/NOARP/) && ! (/lo:/) && ! (/NONE:/) { print $2; exit }')
139                         if [ -z "$arpdev" ]; then
140                                 rc=1
141                         else
142                                 # ip supports only ip addresses
143                                 grep "^[0-9]" /etc/ethers | \
144                                 while read host hwaddr flags; do
145                                         case "$flags" in
146                                           *temp*)
147                                                 neflags="nud stale"
148                                                 ;;
149                                           *)
150                                                 neflags="nud permanent"
151                                                 ;;
152                                         esac
153                                         if ! /sbin/ip neigh add $host lladdr $hwaddr $neflags dev $arpdev; then
154                                                 rc=1
155                                         fi
156                                 done
157                         fi
158                         if [ "$rc" -gt 0 ]; then
159                                 fail
160                         fi
161                 fi
162         fi
163 }
164
165 static_rarp()
166 {
167         if is_yes "$STATIC_RARP"; then
168                 if [ ! -e /proc/net/rarp ]; then
169                         _modprobe single -k rarp
170                 fi
171                 if [ -r /etc/ethers -a -x /sbin/rarp ]; then
172                         run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
173                 fi
174         fi
175 }
176
177 vlan_setup()
178 {
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
181                 ETH_VLANS="yes"
182         else
183                 ETH_VLANS="no"
184         fi
185 }
186
187 # Set up all IP && IP parameter variables
188 setup_ip_param ()
189 {
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 } ')
193         fi
194
195         # Setup DEVICETYPE for special cases.
196         if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
197                 DEVICETYPE=vlan
198         fi
199
200         # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
201         DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
202
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)
207         fi
208
209         # check if we have ipv6 or ipv4 address
210         if [[ "${IP4ADDR}" = *:* ]]; then
211                 IP6ADDR=${IP4ADDR}
212                 IP4ADDR=""
213         else
214                 eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
215                 eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
216         fi
217         if [ "${IP6_PRIM_IF}" ] ; then
218                 eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
219                 eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
220         fi
221         if [ "${IP4_SRC_IF}" ] ; then
222                 eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
223                 IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
224         fi
225         if [ "${IP6_SRC_IF}" ] ; then
226                 eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
227                 IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
228         fi
229
230         # new rc-scripts 0.4.x option
231         if [ -z "$HANDLING" ]; then
232                 HANDLING=0
233         fi
234
235         # set handling for bridge
236         case "$DEVICETYPE" in
237           br|atm|lec|irda|vlan)
238                 HANDLING=1
239                 ;;
240         esac
241
242         # Multicast ready devices
243         if is_yes "$MULTICAST"; then
244                 MULTICAST="on"
245         else
246                 case "$DEVICETYPE" in
247                   eth|br)
248                         MULTICAST="on"
249                         ;;
250                   *)
251                         MULTICAST="off"
252                         ;;
253                 esac
254         fi
255
256         # ARP ready devices
257         if [ "$ARP" ] ; then
258                 if is_yes "$ARP"; then
259                         ARP="arp on"
260                 else
261                         ARP="arp off"
262                 fi
263         fi
264 }
265
266 check_device_down ()
267 {
268         if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
269                 return 1
270         else
271                 return 0
272         fi
273 }
274
275 setup_ip_gw_ro ()
276 {
277         # IPv4 gateway
278         if is_yes "$IPV4_NETWORKING"; then
279                 if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
280                         # set up default gateway
281                         if [ "${GATEWAY}" ]; then
282                                 ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
283                                 if [ $? = 0 ]; then
284                                         DEFGW=${GATEWAY}
285                                 else
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?"
289                                 fi
290                         elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
291                                 ip -4 route add default dev ${DEVICE} ${IP4SRCADDR}
292                         fi
293                 fi
294         fi
295
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 [ "${GATEWAY6}" ]; then
301                                 ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
302                                 DEFGW6=${GATEWAY6}
303                         elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
304                                 ip -6 route add default dev ${DEVICE} ${IP6SRCADDR}
305                         fi
306                 fi
307                 if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
308                         # set up default route
309                         if [ "${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}
313                         fi
314                 fi
315         fi
316 }
317
318 #
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>
322 #
323 setup_routes()
324 {
325         typeset args
326
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
331                         done
332                 else
333                         egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
334                                 /sbin/ip route del $args 2>/dev/null
335                         done
336                 fi
337         fi
338 }
339
340 # Add ONLY IPv4 address (IPv6 address is added automaticly)
341 set_up_loopback()
342 {
343         if is_yes "$IPV4_NETWORKING"; then
344                 ip addr add 127.0.0.1/8 dev lo
345         fi
346         ip link set dev lo up
347 }
348
349 set_down_loopback()
350 {
351         ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
352         ip link set dev lo down
353 }
354
355 modprobe_net()
356 {
357         if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
358                 _modprobe single -k ipv6
359         fi
360
361         if is_yes "$IPX" && is_module "ipx"; then
362                 _modprobe single -k ipx
363         fi
364 }
365
366 # calculate network prefix from given network mask
367 calcprefix()
368 {
369         typeset old_IFS netmask prefix endp
370
371         old_IFS=$IFS
372         IFS='.'
373         netmask=$(echo $1)
374         IFS=$old_IFS
375
376         prefix=0
377         endp=0
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))
383                                 else
384                                         echo "32"
385                                         return
386                                 fi
387                         else
388                                 endp=1
389                         fi
390                 done
391         done
392         echo $prefix
393 }
394
395 # calculate network mask from given prefix
396 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
397 calcnetmask()
398 {
399         typeset prefix a MASK
400
401         MASK=""
402         if [ "${1##[0-9]*}" ]; then
403                 prefix=32
404         else
405                 typeset -i prefix=$1
406         fi
407         for i in 1 2 3 4; do
408                 case $prefix in
409                   7)    a=254 ;;
410                   6)    a=252 ;;
411                   5)    a=248 ;;
412                   4)    a=240 ;;
413                   3)    a=224 ;;
414                   2)    a=192 ;;
415                   1)    a=128 ;;
416                   *)
417                         [ $prefix -ge 8 ] && a=255
418                         [ $prefix -le 0 ] && a=0
419                         ;;
420                 esac
421
422                 prefix="$(( $prefix - 8))"
423                 if [ -z "$MASK" ]; then
424                         MASK=$a
425                 else
426                         MASK=$MASK.$a
427                 fi
428         done
429
430         echo $MASK
431         return
432 }
433
434 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
435 get_ppp_device_and_pid ()
436 {
437         if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
438                 eval $(
439                 {
440                         read PID ; echo "PID='$PID'"
441                         read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
442                 } < "/var/run/ppp-$DEVNAME.pid")
443         fi
444
445         if [ -z "$REALDEVICE" ] ; then
446                 REALDEVICE=$DEVICE
447         fi
448 }
449
450 # following function setups advanced routing rules
451 # Olgierd Pieczul <wojrus@pld-linux.org>
452 setup_ip_rules ()
453 {
454         typeset args prio from src
455
456         if [ -f /etc/sysconfig/static-routes ] ; then
457                 if is_yes "$1"; 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
460                         done
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
464                         done
465                 fi
466         fi
467 }
468
469 is_wireless_device ()
470 {
471         if [ -x /sbin/iwconfig ]; then
472                 LC_ALL=C iwconfig "${1}" 2>&1 | grep -q "no wireless extensions" || return 0
473         fi
474         return 1
475 }
476
477 wireless_param()
478 {
479         device="$1"
480         if [ "$2" = "br" ]; then
481                 prefix="${device}_"
482         else
483                 prefix=""
484         fi
485
486         local val wlan_mode
487
488         if val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ -z "$val" ]; then
489                  WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
490         else
491                  WLAN_NICKNAME="$val"
492         fi
493
494         # wlan mode needs to be first
495         wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
496                 iwconfig ${device} mode "$wlan_mode"
497
498         iwconfig ${device} nick ${WLAN_NICKNAME}
499
500         val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
501                 iwconfig ${device} nwid "$val"
502
503         if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
504                 if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
505                         iwconfig ${device} freq "$val"
506                 elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
507                         iwconfig ${device} channel "$val"
508                 fi
509         fi
510
511         val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
512                 iwconfig ${device} sens "$val"
513         val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
514                 iwconfig ${device} ap "$val"
515         val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
516                 iwconfig ${device} rate "$val"
517         val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
518                 iwconfig ${device} rts "$val"
519         val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
520                 iwconfig ${device} frag "$val"
521
522         local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
523         local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
524         # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
525         if [ "$wlan_key" ]; then
526                 # wlan key can contain index of key to set
527                 # to set other than current keys suffix with 1-4:
528                 local idx
529                 for idx in 1 2 3 4; do
530                         val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
531                         [ "$val" ] || continue
532                         iwconfig ${device} key "[$idx]" "$val"
533                 done
534                 if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
535                         iwconfig ${device} key "[$val]"
536                 else
537                         iwconfig ${device} key "$wlan_key"
538                 fi
539
540                 [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
541         fi
542
543         if is_no "$wlan_encryption"; then
544                 iwconfig ${device} key off
545         fi
546         val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
547                 iwconfig ${device} power "$val"
548         val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
549                 iwconfig ${device} txpower "$val"
550         val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
551                 iwconfig ${device} retry "$val"
552         val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
553                 iwconfig ${device} nwid "$val"
554
555         # essid should be last due to network rescanning by wlan devices
556         val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
557                 iwconfig ${device} essid "$val"
558
559         if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
560                 iwconfig ${device} commit
561         fi
562 }
563
564 check_mii_tool ()
565 {
566         [ -x /sbin/mii-tool ] || return 2
567         output=$(LC_ALL=C mii-tool $1 2>&1)
568         echo $output | grep -q "link ok" && return 1
569         echo $output | grep -q "no link" && return 0 || return 2
570 }
571
572 check_ethtool ()
573 {
574         [ -x /sbin/ethtool ] || return 2
575         output=$(LC_ALL=C ethtool $1 2>&1)
576         echo $output | grep -q "Link detected: yes" && return 1
577         echo $output | grep -q "Link detected: no" && return 0 || return 2
578 }
579
580 check_iwconfig ()
581 {
582         [ -x /sbin/iwconfig ] || return 2
583         output=$(LC_ALL=C iwconfig $1 2>&1)
584         echo $output | grep -q "radio off" && return 0 || return 2
585 }
586
587 check_link_down ()
588 {
589         if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
590                 if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
591                         ip link set dev $1 up >/dev/null 2>&1
592                         timeout=0
593                         while [ $timeout -le 10 ]; do
594                                 check_mii_tool $1
595                                 m=$?
596                                 check_ethtool $1
597                                 e=$?
598                                 check_iwconfig $1
599                                 i=$?
600                                 if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
601                                         return 1
602                                 fi
603                                 if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
604                                         return 1
605                                 fi
606                                 usleep 500000
607                                 timeout=$((timeout+1))
608                         done
609                         return 0
610                 fi
611         fi
612         return 1
613 }
614
615 # This must be last line !
616 # vi:syntax=sh
This page took 0.067341 seconds and 4 git commands to generate.