]> TLD Linux GIT Repositories - rc-scripts.git/blob - rc.d/init.d/network
3592a2b476f104eb83fb047236aa5df875882c21
[rc-scripts.git] / rc.d / init.d / network
1 #!/bin/sh
2 #
3 # network       Bring up/down networking
4 #
5 # chkconfig:    2345 10 90
6 # description:  Activates/Deactivates all network interfaces configured to \
7 #               start at boot time.
8 #
9 # probe:        true
10
11 if [ ! -f /etc/sysconfig/network ]; then
12         . /etc/rc.d/init.d/functions
13         nls "%s is missing. Can't continue." "/etc/sysconfig/network"
14         exit 1
15 fi
16
17 . /etc/sysconfig/network
18
19 # Source function library.
20 . /etc/rc.d/init.d/functions
21 . /lib/rc-scripts/functions.network
22
23 # Will be removed in the future
24 if [ -n "$NETWORKING" ] && is_yes "$NETWORKING"; then
25         if [ -z "$IPV4_NETWORKING" ]; then
26                 echo "NETWORKING is set to YES, but IPV4_NETWORKING is empty!"
27                 echo "Please upgrade your config"
28                 echo "Assuming you want IPv4 networking"
29                 IPV4_NETWORKING=yes
30         fi
31 fi
32
33 ######
34 # initialize networking:
35 # - check IPv4, IPv6, IPX can be handled by system
36 # - setup default IPv{4,6} interfaces policy like:
37 #   - spoofig protection,
38 #   - icmp echo ignore broadcasts,
39 # - setup lo interface
40 network_init() {
41         if [ ! -x /sbin/ip ]; then
42                 nls "%s is missing. Can't continue." "/sbin/ip"
43                 exit 1
44         fi
45
46         # Modprobe needed devices
47         modprobe_net
48
49         # load sysctl params
50         apply_sysctl
51
52         # Setup interfaces names
53         if ! is_empty_file /etc/mactab && [ -x /sbin/nameif ]; then
54                 run_cmd "Setting interfaces names (nameif)" /sbin/nameif
55         fi
56
57         # Kernel network parameters
58         sysctl -e -p /etc/sysctl.conf > /dev/null 2>&1
59
60         # Set UP loopback interface
61         set_up_loopback
62
63         # Setup configuration
64         setup_nat on
65         setup_routes on
66         setup_ip_rules on
67         # Setup IPX
68         if is_yes "$IPX"; then
69                 if [ -n $IPXAUTOPRIMARY ] ; then
70                         if is_yes "$IPXAUTOPRIMARY"; then
71                                 IPXAUTOPRIMARY="on"
72                         else
73                                 IPXAUTOPRIMARY="off"
74                         fi
75                         /sbin/ipx_configure --auto_primary=$IPXAUTOPRIMARY
76                 fi
77                 if [ -n $IPXAUTOFRAME ] ; then
78                         if is_yes "$IPXAUTOFRAME"; then
79                                 IPXAUTOFRAME="on"
80                         else
81                                 IPXAUTOFRAME="off"
82                         fi
83                         /sbin/ipx_configure --auto_interface=$IPXAUTOFRAME
84                 fi
85                 if [ -n "$IPXINTERNALNETNUM" -a "$IPXINTERNALNETNUM" != "0" ]; then
86                         /sbin/ipx_internal_net add $IPXINTERNALNETNUM $IPXINTERNALNODENUM
87                 fi
88         fi
89 }
90
91 network_postinit() {
92         # Run this again to catch any interface-specific actions
93         apply_sysctl
94
95         # Set static RARP table
96         static_rarp
97
98         # Set static ARP table
99         static_arp
100 }
101
102 ######
103 # deinitialize networking
104 # - down lo interface.
105 network_deinit() {
106         setup_routes off
107         setup_ip_rules off
108
109         # Set down NAT rules
110         setup_nat off
111         # Set DOWN loopback interface
112         set_down_loopback
113 }
114
115 # Get list of interface configs
116 # ignores editor backup files and rpm backups
117 network_interface_configs() {
118         local match="$1"
119         for a in /etc/sysconfig/interfaces/$match; do
120                 case "$a" in
121                 *.rpmorig|*.rpmnew|*.rpmsave|*~|*.orig)
122                         continue
123                         ;;
124                 *)
125                         echo $a
126                 ;;
127                 esac
128         done
129 }
130
131 find_boot_interfaces() {
132         ifcfg_files="$(network_interface_configs 'ifcfg-*')"
133         bootprio=$(grep '^BOOTPRIO=' $ifcfg_files)
134
135         if [ -n "$bootprio" ]; then
136                 # find all the interfaces besides loopback.
137                 interfaces_boot=`
138                         for a in $(echo "$bootprio" | sort -t= -s -n -k2,2); do
139                                 i="${a%:BOOTPRIO*}"
140                                 case $i in
141                                         *ifcfg-lo) continue ;;
142                                 esac
143                                 DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
144                                 [ "${DEVICE:+set}" != "set" ] && continue
145                                 [ ${USERS:-no} != no ] && continue
146                                 [ ${ONBOOT:-no} = yes ] && echo "${i##*/ifcfg-}"
147                         done
148                 `
149         else
150                 interfaces_boot=`
151                         for i in $ifcfg_files; do
152                                 case ${i##*/} in
153                                         ifcfg-lo|ifcfg-sit*|ifcfg-atm*|ifcfg-lec*|ifcfg-nas*|ifcfg-br*|ifcfg-macvlan*|ifcfg-macvtap*|ifcfg-*.*) continue ;;
154                                 esac
155                                 DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
156                                 [ "${DEVICE:+set}" != "set" ] && continue
157                                 [ ${USERS:-no} != no ] && continue
158                                 [ ${ONBOOT:-no} = yes ] && echo "${i##*/ifcfg-}"
159                         done
160                 `
161
162                 interfaces_vlan_boot=`
163                         for i in $ifcfg_files; do
164                                 case ${i##*/} in
165                                         ifcfg-*.*) ;;
166                                         *) continue ;;
167                                 esac
168                                 DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
169                                 [ "${DEVICE:+set}" != "set" ] && continue
170                                 [ ${USERS:-no} != no ] && continue
171                                 [ ${ONBOOT:-no} = yes ] && echo "${i##*/ifcfg-}"
172                         done
173                 `
174
175                 interfaces_br_boot=`
176                         for i in $ifcfg_files; do
177                                 case ${i##*/} in
178                                         ifcfg-br*) ;;
179                                         *) continue ;;
180                                 esac
181                                 DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
182                                 [ "${DEVICE:+set}" != "set" ] && continue
183                                 [ ${USERS:-no} != no ] && continue
184                                 [ ${ONBOOT:-no} = yes ] && echo "${i##*/ifcfg-}"
185                         done
186                 `
187
188                 interfaces_virt_boot=`
189                         for i in $ifcfg_files; do
190                                 case ${i##*/} in
191                                         ifcfg-macvtap*|ifcfg-macvlan*) ;;
192                                         *) continue ;;
193                                 esac
194                                 DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
195                                 [ "${DEVICE:+set}" != "set" ] && continue
196                                 [ ${USERS:-no} != no ] && continue
197                                 [ ${ONBOOT:-no} = yes ] && echo "${i##*/ifcfg-}"
198                         done
199                 `
200
201                 interfaces_sit_boot=`
202                         for i in $ifcfg_files; do
203                                 case ${i##*/} in
204                                         ifcfg-sit*) ;;
205                                         *) continue ;;
206                                 esac
207                                 DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
208                                 [ "${DEVICE:+set}" != "set" ] && continue
209                                 [ ${USERS:-no} != no ] && continue
210                                 [ ${ONBOOT:-no} = yes ] && echo "${i##*/ifcfg-}"
211                         done
212                 `
213         fi
214
215         tunnels=$(
216                 for i in $(network_interface_configs 'tnlcfg-*'); do
217                         DEVICE=""; ONBOOT=""; USERS=""; . "$i" 2>/dev/null
218                         [ "${DEVICE:+set}" != "set" ] && continue
219                         [ ${USERS:-no} != no ] && continue
220                         [ ${ONBOOT:-no} = yes ] && echo "${i##*/tnlcfg-}"
221                 done
222         )
223 }
224
225 start() {
226         rc_splash "bootnetwork start"
227         network_init
228
229         for i in $interfaces_boot $interfaces_vlan_boot $interfaces_sit_boot; do
230                 run_cmd -a "$(nls 'Bringing up interface %s' "$i")" /sbin/ifup $i boot
231         done
232
233         for i in $interfaces_br_boot ; do
234                 run_cmd -a "$(nls 'Bringing up bridge interface %s' "$i")" /sbin/ifup $i boot
235         done
236
237         for i in $interfaces_virt_boot ; do
238                 run_cmd -a "$(nls 'Bringing up virtual interface %s' "$i")" /sbin/ifup $i boot
239         done
240
241         for i in $tunnels; do
242                 run_cmd -a "$(nls 'Setting tunnel %s' "$i")" /sbin/tnlup $i boot
243                 run_cmd -a "$(nls 'Bringing up tunnel interface %s' "$i")" /sbin/ifup tnlcfg-$i boot
244         done
245
246         network_postinit
247
248         touch /var/lock/subsys/network
249 }
250
251 stop() {
252         # If we go to runlevel 0, 1 or 6 then umount all network fs
253         if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
254                 if [ -x /etc/rc.d/init.d/netfs -a -f /var/lock/subsys/netfs ];
255                 then
256                         /etc/rc.d/init.d/netfs stop
257                 fi
258         fi
259
260         for i in $tunnels; do
261                 run_cmd -a "$(nls 'Shutting down tunnel interface %s' "$i")" /sbin/ifdown tnlcfg-$i boot
262                 run_cmd -a "$(nls 'Removing tunnel %s' "$i")" /sbin/tnldown $i boot
263         done
264
265         for i in $interfaces_virt_boot ; do
266                 run_cmd -a "$(nls 'Shutting down virtual interface %s' "$i")" /sbin/ifup $i boot
267         done
268
269         for i in $interfaces_br_boot ; do
270                 run_cmd -a "$(nls 'Shutting down bridge interface %s' "$i")" /sbin/ifdown $i boot
271         done
272
273         for i in $interfaces_sit_boot $interfaces_vlan_boot $interfaces_boot ; do
274                 run_cmd -a "$(nls 'Shutting down interface %s' "$i")" /sbin/ifdown $i boot
275         done
276
277         network_deinit
278
279         rm -f /var/lock/subsys/network >/dev/null 2>&1
280 }
281
282 # Reload all active interfaces
283 reload() {
284         if [ ! -f /var/lock/subsys/network ]; then
285                 msg_not_running network
286                 RETVAL=7
287                 return
288         fi
289
290         set_dhcpclient
291
292         # if no DHCP client found we can't reload anything
293         if [ -z "$DHCP_CLIENT" ]; then
294                 return
295         fi
296
297         local DHCP_ARGS
298         case ${DHCP_CLIENT##*/} in
299 #         pump)
300 #               DHCP_ARGS=""
301 #               ;;
302           dhcpcd)
303                 DHCP_ARGS="-n"
304                 ;;
305 #         dhcpxd)
306 #               DHCP_ARGS=""
307 #               ;;
308 #         dhclient)
309 #               DHCP_ARGS=""
310 #               ;;
311           *)
312                 echo "Reloading using $DHCP_CLIENT DHCP Client is not implmemented in rc-scripts"
313                 RETVAL=1
314                 return
315                 ;;
316         esac
317
318         # for IPv4 DHCP interfaces send signal to refresh interface
319         local dev devs=${*:-$(/sbin/ip link show | awk -F: '/UP/{print $2}')}
320         for dev in $devs; do
321                 if [ ! -f /etc/sysconfig/interfaces/ifcfg-$dev ]; then
322                         continue
323                 fi
324                 . /etc/sysconfig/interfaces/ifcfg-$dev
325
326                 if [ -n "$BOOTPROTO" -a "$BOOTPROTO" != "none" -a "$BOOTPROTO" != "static" ] && is_yes "$IPV4_NETWORKING"; then
327                         case ${DHCP_CLIENT##*/} in
328                           pump)
329                                 DHCP_ARGS="$DHCP_ARGS -i $DEVICE"
330                                 ;;
331                           dhcpcd)
332                                 DHCP_ARGS="$DHCP_ARGS $DEVICE"
333                                 ;;
334                           dhcpxd)
335                                 DHCP_ARGS="$DHCP_ARGS $DEVICE"
336                                 ;;
337                           dhclient)
338                                 DHCP_ARGS="$DHCP_ARGS $DEVICE"
339                                 ;;
340                         esac
341                         DHCP_ARGS="$DHCP_OPTIONS $DHCP_ARGS"
342
343                         show 'Reloading interface %s' $dev
344                         if $DHCP_CLIENT $DHCP_ARGS; then
345                                 ok
346                         else
347                                 fail
348                         fi
349                 fi
350         done
351
352 }
353
354 find_boot_interfaces
355
356 # See how we were called.
357 case "$1" in
358   start)
359         if is_yes "$VSERVER_ISOLATION_NET"; then
360                 touch /var/lock/subsys/network
361         else
362                 start
363         fi
364         ;;
365   start_init)
366         network_init
367         ;;
368   start_postinit)
369         network_postinit
370         touch /var/lock/subsys/network
371         ;;
372   stop_deinit)
373         network_deinit
374         rm -f /var/lock/subsys/network
375         ;;
376   stop)
377         if is_yes "$VSERVER_ISOLATION_NET"; then
378                 rm -f /var/lock/subsys/network >/dev/null 2>&1
379         else
380                 stop
381         fi
382         ;;
383
384   status)
385         nls "Configured devices:"
386         echo "lo $interfaces"
387         nls "Configured tunnels:"
388         echo "$tunnels"
389         echo
390         nls "Currently inactive devices and tunnels:"
391         /sbin/ip link show | awk -F":" '(/^[0-90-90-9]:/) && ! (/UP/) { print $2 }' | xargs
392         nls "Currently active devices and tunnels:"
393         /sbin/ip link show | awk -F":" ' (/UP/) { print $2 }' | xargs
394         ;;
395
396   reload)
397         if is_yes "$VSERVER_ISOLATION_NET"; then
398                 exit 0
399         fi
400         shift
401         reload ${1:+"$@"}
402         ;;
403
404   restart)
405         if is_yes "$VSERVER_ISOLATION_NET"; then
406                 exit 0
407         fi
408
409         stop
410         start
411         ;;
412
413   *)
414         msg_usage "$0 {start|stop|reload|restart|status}"
415         exit 3
416 esac
417
418 exit 0