Merge branch 'vendor/GDTOA'
[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.8 2008/07/06 23:56:32 thomas Exp $
27 #
28
29 # PROVIDE: netif
30 # REQUIRE: atm1 ipfilter mountcritlocal serial sppp sysctl tty
31
32 . /etc/rc.subr
33 . /etc/network.subr
34
35 name="network"
36 start_cmd="network_start"
37 stop_cmd="network_stop"
38 cloneup_cmd="clone_up"
39 clonedown_cmd="clone_down"
40 extra_commands="cloneup clonedown"
41 _cmdifn=
42
43 network_start()
44 {
45         # Set the list of interfaces to work on.
46         #
47         _cmdifn=$*
48
49         if [ -z "$_cmdifn" ]; then
50                 #
51                 # We're operating as a general network start routine.
52                 #
53
54                 # Create cloned interfaces
55                 clone_up
56
57                 # Create IPv6<-->IPv4 tunnels
58                 gif_up
59
60                 # Rename interfaces.
61                 ifnet_rename
62         fi
63
64         # Configure the interface(s).
65         network_common ifn_start verbose
66
67         # Give our interfaces a little time to come up
68         # before we start pounding them.  In particular, dhclient
69         # and named can get mightily confused.
70         sleep 2
71
72         # Resync ipfilter
73         /etc/rc.d/ipfilter resync
74 }
75
76 network_stop()
77 {
78         # Set the list of interfaces to work on.
79         #
80         _cmdifn=$*
81
82         echo -n "Stopping network:"
83
84         # Deconfigure the interface(s)
85         network_common ifn_stop
86         echo '.'
87 }
88
89 # network_common routine verbose
90 #       Common configuration subroutine for network interfaces. This
91 #       routine takes all the preparatory steps needed for configuring
92 #       an interface and then calls $routine. If $verbose is specified,
93 #       it will call ifconfig(8) to show, in long format, the configured
94 #       interfaces. If $verbose is not given, it will simply output the
95 #       configured interface(s).
96 network_common()
97 {
98         _func=
99         _verbose=
100
101         if [ -z "$1" ]; then
102                 err 1 "network_common(): No function name specified."
103         else
104                 _func="$1"
105         fi
106         [ -n "$2" ] && _verbose=yes
107
108         # Get a list of network interfaces.
109         _ifn_list="`list_net_interfaces`"
110
111         # Set the scope of the command (all interfaces or just one).
112         #
113         _cooked_list=
114         if [ -n "$_cmdifn" ]; then
115                 for i in $_cmdifn ; do
116                         eval _if=\"`expr "$_ifn_list" : ".*\(${i}\).*"`\"
117                         if [ -z "$_if" ]; then
118                                 err 1 "No such network interface: $i"
119                         fi
120                         _cooked_list="$_cooked_list $_if"
121                 done
122         else
123                 _cooked_list="$_ifn_list"
124         fi
125
126         for ifn in ${_cooked_list}; do
127                 if ${_func} ${ifn} ; then
128                         eval showstat_$ifn=1
129                 else
130                         _fail="$_fail $ifn"
131                 fi
132         done
133
134         # Display interfaces configured by this script
135         #
136         for ifn in ${_cooked_list}; do
137                 eval showstat=\$showstat_${ifn}
138                 if [ ! -z ${showstat} ]; then
139                         if [ -n "$_verbose" ]; then
140                                 ifconfig ${ifn}
141                         else
142                                 echo -n " ${ifn}"
143                         fi
144                 fi
145         done
146         debug "The following interfaces were not configured: $_fail"
147 }
148
149 ifn_start()
150 {
151         local ifn cfg
152         ifn="$1"
153         cfg=1
154
155         [ -z "$ifn" ] && return 1
156
157         ifscript_up ${ifn} && cfg=0
158         ifconfig_up ${ifn} && cfg=0
159         ifalias_up ${ifn} && cfg=0
160         ipx_up ${ifn} && cfg=0
161         childif_create ${ifn} && cfg=0
162
163         return $cfg
164 }
165
166 ifn_stop()
167 {
168         local ifn cfg
169         ifn="$1"
170         cfg=1
171
172         [ -z "$ifn" ] && return 1
173
174         ipx_down ${ifn} && cfg=0
175         ifalias_down ${ifn} && cfg=0
176         ifconfig_down ${ifn} && cfg=0
177         ifscript_down ${ifn} && cfg=0
178         childif_destroy ${ifn} && cfg=0
179
180         return $cfg
181 }
182
183 # get_if_var if var [default]
184 #       Return the value of the pseudo-hash corresponding to $if where
185 #       $var is a string containg the sub-string "IF" which will be
186 #       replaced with $if after the characters defined in _punct are
187 #       replaced with '_'. If the variable is unset, replace it with
188 #       $default if given.
189 get_if_var()
190 {
191         local _if _punct _var _default prefix suffix
192
193         if [ $# -ne 2 -a $# -ne 3 ]; then
194                 err 3 'USAGE: get_if_var name var [default]'
195         fi
196
197         _if=$1
198         _punct=". - / +"
199         for _punct_c in $_punct; do
200                 _if=`ltr ${_if} ${_punct_c} '_'`
201         done
202         _var=$2
203         _default=$3
204
205         prefix=${_var%%IF*}
206         suffix=${_var##*IF}
207         eval echo \${${prefix}${_if}${suffix}-${_default}}
208 }
209
210 # childif_create
211 #       Create and configure child interfaces.  Return 0 if child
212 #       interfaces are created.
213 #
214 childif_create()
215 {
216         local cfg child child_vlans child_wlans create_args debug_flags ifn i
217         cfg=1
218         ifn=$1
219
220         # Create wireless interfaces
221         child_wlans=`get_if_var $ifn wlans_IF`
222
223         for child in ${child_wlans}; do
224                 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
225                 debug_flags="`get_if_var $child wlandebug_IF`"
226
227                 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
228                         ifconfig $child create ${create_args} && cfg=0
229                         if [ -n "${debug_flags}" ]; then
230                                 wlandebug -i $child ${debug_flags}
231                         fi
232                 else
233                         i=`ifconfig wlan create ${create_args}`
234                         if [ -n "${debug_flags}" ]; then
235                                 wlandebug -i $i ${debug_flags}
236                         fi
237                         ifconfig $i name $child && cfg=0
238                 fi
239                 ifn_start $child
240         done
241
242         return ${cfg}
243 }
244
245 # childif_destroy
246 #       Destroy child interfaces.
247 #
248 childif_destroy()
249 {
250         local cfg child child_vlans child_wlans ifn
251         cfg=1
252
253         child_wlans=`get_if_var $ifn wlans_IF`
254         for child in ${child_wlans}; do
255                 if ! `ifconfig -n $child > /dev/null 2>&1`; then
256                         continue
257                 fi
258                 ifn_stop $child
259                 ifconfig $child destroy && cfg=0
260         done
261
262         return ${cfg}
263 }
264
265 load_rc_config $name
266 run_rc_command $*