]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/network
cca3795f721daaa87a06a2058f7b9bee13a6b69b
[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
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         emit pld.network-starting
227         emit starting JOB=network
228
229         rc_splash "bootnetwork start"
230         network_init
231
232         for i in $interfaces_boot $interfaces_vlan_boot $interfaces_sit_boot; do
233                 run_cmd -a "$(nls 'Bringing up interface %s' "$i")" /sbin/ifup $i boot
234         done
235
236         for i in $interfaces_br_boot ; do
237                 run_cmd -a "$(nls 'Bringing up bridge interface %s' "$i")" /sbin/ifup $i boot
238         done
239
240         for i in $interfaces_virt_boot ; do
241                 run_cmd -a "$(nls 'Bringing up virtual interface %s' "$i")" /sbin/ifup $i boot
242         done
243
244         for i in $tunnels; do
245                 run_cmd -a "$(nls 'Setting tunnel %s' "$i")" /sbin/tnlup $i boot
246                 run_cmd -a "$(nls 'Bringing up tunnel interface %s' "$i")" /sbin/ifup tnlcfg-$i boot
247         done
248
249         network_postinit
250
251         touch /var/lock/subsys/network
252         emit --no-wait pld.network-started
253         emit --no-wait started JOB=network
254 }
255
256 stop() {
257         emit pld.network-stopping
258         emit stopping JOB=network
259         # If we go to runlevel 0, 1 or 6 then umount all network fs
260         if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
261                 if [ -x /etc/rc.d/init.d/netfs -a -f /var/lock/subsys/netfs ];
262                 then
263                         /etc/rc.d/init.d/netfs stop
264                 fi
265         fi
266
267         for i in $tunnels; do
268                 run_cmd -a "$(nls 'Shutting down tunnel interface %s' "$i")" /sbin/ifdown tnlcfg-$i boot
269                 run_cmd -a "$(nls 'Removing tunnel %s' "$i")" /sbin/tnldown $i boot
270         done
271
272         for i in $interfaces_virt_boot ; do
273                 run_cmd -a "$(nls 'Shutting down virtual interface %s' "$i")" /sbin/ifup $i boot
274         done
275
276         for i in $interfaces_br_boot ; do
277                 run_cmd -a "$(nls 'Shutting down bridge interface %s' "$i")" /sbin/ifdown $i boot
278         done
279
280         for i in $interfaces_sit_boot $interfaces_vlan_boot $interfaces_boot ; do
281                 run_cmd -a "$(nls 'Shutting down interface %s' "$i")" /sbin/ifdown $i boot
282         done
283
284         network_deinit
285
286         rm -f /var/lock/subsys/network >/dev/null 2>&1
287         emit --no-wait pld.network-stopped
288         emit --no-wait stopped JOB=network
289 }
290
291 # Reload all active interfaces
292 reload() {
293         if [ ! -f /var/lock/subsys/network ]; then
294                 msg_not_running network
295                 RETVAL=7
296                 return
297         fi
298
299         set_dhcpclient
300
301         # if no DHCP client found we can't reload anything
302         if [ -z "$DHCP_CLIENT" ]; then
303                 return
304         fi
305
306         local DHCP_ARGS
307         case ${DHCP_CLIENT##*/} in
308 #         pump)
309 #               DHCP_ARGS=""
310 #               ;;
311           dhcpcd)
312                 DHCP_ARGS="-n"
313                 ;;
314 #         dhcpxd)
315 #               DHCP_ARGS=""
316 #               ;;
317 #         dhclient)
318 #               DHCP_ARGS=""
319 #               ;;
320           *)
321                 echo "Reloading using $DHCP_CLIENT DHCP Client is not implmemented in rc-scripts"
322                 RETVAL=1
323                 return
324                 ;;
325         esac
326
327         # for IPv4 DHCP interfaces send signal to refresh interface
328         local dev devs=${*:-$(/sbin/ip link show | awk -F: '/UP/{print $2}')}
329         for dev in $devs; do
330                 if [ ! -f /etc/sysconfig/interfaces/ifcfg-$dev ]; then
331                         continue
332                 fi
333                 . /etc/sysconfig/interfaces/ifcfg-$dev
334
335                 if [ -n "$BOOTPROTO" -a "$BOOTPROTO" != "none" -a "$BOOTPROTO" != "static" ] && is_yes "$IPV4_NETWORKING"; then
336                         case ${DHCP_CLIENT##*/} in
337                           pump)
338                                 DHCP_ARGS="$DHCP_ARGS -i $DEVICE"
339                                 ;;
340                           dhcpcd)
341                                 DHCP_ARGS="$DHCP_ARGS $DEVICE"
342                                 ;;
343                           dhcpxd)
344                                 DHCP_ARGS="$DHCP_ARGS $DEVICE"
345                                 ;;
346                           dhclient)
347                                 DHCP_ARGS="$DHCP_ARGS $DEVICE"
348                                 ;;
349                         esac
350                         DHCP_ARGS="$DHCP_OPTIONS $DHCP_ARGS"
351
352                         show 'Reloading interface %s' $dev
353                         if $DHCP_CLIENT $DHCP_ARGS; then
354                                 ok
355                         else
356                                 fail
357                         fi
358                 fi
359         done
360
361 }
362
363 find_boot_interfaces
364
365 # See how we were called.
366 case "$1" in
367   start)
368         if is_yes "$VSERVER_ISOLATION_NET"; then
369                 emit pld.network-starting
370                 emit starting JOB=network
371                 touch /var/lock/subsys/network
372                 emit pld.network-started
373                 emit --no-wait started JOB=network
374                 exit 0
375         else
376                 start
377         fi
378         ;;
379   start_init)
380         network_init
381         ;;
382   start_postinit)
383         network_postinit
384         touch /var/lock/subsys/network
385         ;;
386   stop_deinit)
387         network_deinit
388         rm -f /var/lock/subsys/network
389         ;;
390   stop)
391         if is_yes "$VSERVER_ISOLATION_NET"; then
392                 emit pld.network-stopping
393                 emit stopping JOB=network
394                 rm -f /var/lock/subsys/network >/dev/null 2>&1
395                 emit pld.network-stopped
396                 emit --no-wait stopped JOB=network
397                 exit 0
398         else
399                 stop
400         fi
401         ;;
402
403   status)
404         nls "Configured devices:"
405         echo "lo $interfaces"
406         nls "Configured tunnels:"
407         echo "$tunnels"
408         echo
409         nls "Currently inactive devices and tunnels:"
410         /sbin/ip link show | awk -F":" '(/^[0-90-90-9]:/) && ! (/UP/) { print $2 }' | xargs
411         nls "Currently active devices and tunnels:"
412         /sbin/ip link show | awk -F":" ' (/UP/) { print $2 }' | xargs
413         ;;
414
415   reload)
416         if is_yes "$VSERVER_ISOLATION_NET"; then
417                 exit 0
418         fi
419         shift
420         reload ${1:+"$@"}
421         ;;
422
423   restart)
424         if is_yes "$VSERVER_ISOLATION_NET"; then
425                 exit 0
426         fi
427
428         stop
429         start
430         ;;
431
432   *)
433         msg_usage "$0 {start|stop|reload|restart|status}"
434         exit 3
435 esac
436
437 exit 0
This page took 0.124763 seconds and 2 git commands to generate.