Merge from vendor branch CVS:
[dragonfly.git] / etc / rc.d / netif
1 #!/bin/sh
2 #
3 # Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
15 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 # IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
18 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 #
25 # $FreeBSD: src/etc/rc.d/netif,v 1.2 2003/06/29 05:34:41 mtm Exp $
26 # $DragonFly: src/etc/rc.d/netif,v 1.3 2005/04/15 18:40:52 joerg Exp $
27 #
28
29 # PROVIDE: netif
30 # REQUIRE: atm1 ipfilter mountcritlocal serial sppp sysctl tty
31 # KEYWORD: DragonFly 
32
33 . /etc/rc.subr
34 . /etc/network.subr
35
36 name="network"
37 start_cmd="network_start"
38 stop_cmd="network_stop"
39 cloneup_cmd="clone_up"
40 clonedown_cmd="clone_down"
41 extra_commands="cloneup clonedown"
42 _cmdifn=
43
44 network_start()
45 {
46         if [ -z "$_cmdifn" ]; then
47                 #
48                 # We're operating as a general network start routine.
49                 #
50
51                 # Create cloned interfaces
52                 clone_up
53
54                 # Create IPv6<-->IPv4 tunnels
55                 gif_up
56         fi
57
58         # Configure the interface(s).
59         network_common ifn_start verbose
60
61         # Resync ipfilter
62         /etc/rc.d/ipfilter resync
63 }
64
65 network_stop()
66 {
67         echo -n "Stopping network:"
68
69         # Deconfigure the interface(s)
70         network_common ifn_stop
71         echo '.'
72 }
73
74 # network_common routine verbose
75 #       Common configuration subroutine for network interfaces. This
76 #       routine takes all the preparatory steps needed for configuriing
77 #       an interface and then calls $routine. If $verbose is specified,
78 #       it will call ifconfig(8) to show, in long format, the configured
79 #       interfaces. If $verbose is not given, it will simply output the
80 #       configured interface(s).
81 network_common()
82 {
83         _func=
84         _verbose=
85
86         if [ -z "$1" ]; then
87                 err "network_common(): No function name specified."
88         else
89                 _func="$1"
90         fi
91         [ -n "$2" ] && _verbose=yes
92
93         # Get a list of network interfaces. Do not include dhcp interfaces.
94         _ifn_list="`list_net_interfaces nodhcp`"
95
96         # Set the scope of the command (all interfaces or just one).
97         #
98         _cooked_list="$_ifn_list"
99         if [ -n "$_cmdifn" ]; then
100                 eval _cooked_list=\"`expr "$_ifn_list" : ".*\($_cmdifn\).*"`\"
101                 if [ -z "$_cooked_list" ]; then
102                         err "No such network interface: $_cmdifn"
103                         return 1
104                 fi
105         fi
106
107         for ifn in ${_cooked_list}; do
108                 if ${_func} ${ifn} ; then
109                         eval showstat_$ifn=1
110                 else
111                         _fail="$_fail $ifn"
112                 fi
113         done
114
115         # Display interfaces configured by this script
116         #
117         for ifn in ${_cooked_list}; do
118                 eval showstat=\$showstat_${ifn}
119                 if [ ! -z ${showstat} ]; then
120                         if [ -n "$_verbose" ]; then
121                                 ifconfig ${ifn}
122                         else
123                                 echo -n " ${ifn}"
124                         fi
125                 fi
126         done
127         debug "The following interfaces were not configured: $_fail"
128 }
129
130 ifn_start()
131 {
132         local ifn cfg
133         ifn="$1"
134         cfg=1
135
136         [ -z "$ifn" ] && return 1
137
138         ifscript_up ${ifn} && cfg=0
139         ifconfig_up ${ifn} && cfg=0
140         ifalias_up ${ifn} && cfg=0
141         ipx_up ${ifn} && cfg=0
142
143         return $cfg
144 }
145
146 ifn_stop()
147 {
148         local ifn cfg
149         ifn="$1"
150         cfg=1
151
152         [ -z "$ifn" ] && return 1
153
154         ipx_down ${ifn} && cfg=0
155         ifalias_down ${ifn} && cfg=0
156         ifconfig_down ${ifn} && cfg=0
157         ifscript_down ${ifn} && cfg=0
158
159         return $cfg
160 }
161
162 if [ -n "$2" ]; then
163         _cmdifn="$2"
164 fi
165
166 load_rc_config $name
167 run_rc_command "$1"