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