]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.d/netif
Sync to HEAD@r271160.
[FreeBSD/FreeBSD.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$
26 #
27
28 # PROVIDE: netif
29 # REQUIRE: atm1 FILESYSTEMS serial sppp sysctl
30 # REQUIRE: ipfilter ipfs
31 # KEYWORD: nojailvnet
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 clear_cmd="doclear"
42 vnetup_cmd="vnet_up"
43 vnetdown_cmd="vnet_down"
44 extra_commands="cloneup clonedown clear vnetup vnetdown"
45 cmdifn=
46
47 set_rcvar_obsolete ipv6_enable ipv6_activate_all_interfaces
48 set_rcvar_obsolete ipv6_prefer
49
50 network_start()
51 {
52         local _if
53
54         # Set the list of interfaces to work on.
55         #
56         cmdifn=$*
57
58         if [ -z "$cmdifn" ]; then
59                 #
60                 # We're operating as a general network start routine.
61                 #
62
63                 # disable SIGINT (Ctrl-c) when running at startup
64                 trap : 2
65         fi
66
67         # Create cloned interfaces
68         clone_up $cmdifn
69
70         # Rename interfaces.
71         ifnet_rename $cmdifn
72
73         # Configure the interface(s).
74         network_common ifn_start $cmdifn
75
76         if [ -f /etc/rc.d/ipfilter ] ; then
77                 # Resync ipfilter
78                 /etc/rc.d/ipfilter quietresync
79         fi
80         if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
81                 /etc/rc.d/bridge start $cmdifn
82         fi
83         if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
84                 for _if in $cmdifn; do
85                         /etc/rc.d/routing start any $_if
86                 done
87         fi
88 }
89
90 network_stop()
91 {
92         _clone_down=1
93         network_stop0 $*
94 }
95
96 doclear()
97 {
98         _clone_down=
99         network_stop0 $*
100 }
101
102 network_stop0()
103 {
104         local _if
105
106         # Set the list of interfaces to work on.
107         #
108         cmdifn=$*
109
110         # Deconfigure the interface(s)
111         network_common ifn_stop $cmdifn
112
113         # Destroy cloned interfaces
114         if [ -n "$_clone_down" ]; then
115                 clone_down $cmdifn
116         fi
117
118         if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
119                 for _if in $cmdifn; do
120                         /etc/rc.d/routing stop any $_if
121                 done
122         fi
123 }
124
125 vnet_up()
126 {
127         cmdifn=$*
128
129         network_common ifn_vnetup $cmdifn
130 }
131
132 vnet_down()
133 {
134         cmdifn=$*
135
136         network_common ifn_vnetdown $cmdifn
137 }
138
139 # network_common routine
140 #       Common configuration subroutine for network interfaces. This
141 #       routine takes all the preparatory steps needed for configuriing
142 #       an interface and then calls $routine.
143 network_common()
144 {
145         local _cooked_list _tmp_list _fail _func _ok _str _cmdifn
146
147         _func=
148
149         if [ -z "$1" ]; then
150                 err 1 "network_common(): No function name specified."
151         else
152                 _func="$1"
153                 shift
154         fi
155
156         # Set the scope of the command (all interfaces or just one).
157         #
158         _cooked_list=
159         _tmp_list=
160         _cmdifn=$*
161         if [ -n "$_cmdifn" ]; then
162                 # Don't check that the interface(s) exist.  We need to run
163                 # the down code even when the interface doesn't exist to
164                 # kill off wpa_supplicant.
165                 # XXXBED: is this really true or does wpa_supplicant die?
166                 # if so, we should get rid of the devd entry
167                 _cooked_list="$_cmdifn"
168         else
169                 _cooked_list="`list_net_interfaces`"
170         fi
171
172         # Expand epair[0-9] to epair[0-9][ab].
173         for ifn in $_cooked_list; do
174         case ${ifn#epair} in
175         [0-9]*[ab])     ;;      # Skip epair[0-9]*[ab].
176         [0-9]*)
177                 for _str in $_cooked_list; do
178                 case $_str in
179                 $ifn)   _tmp_list="$_tmp_list ${ifn}a ${ifn}b" ;;
180                 *)      _tmp_list="$_tmp_list ${ifn}" ;;
181                 esac
182                 done
183                 _cooked_list=${_tmp_list# }
184         ;;
185         esac
186         done
187
188         _dadwait=
189         _fail=
190         _ok=
191         for ifn in ${_cooked_list# }; do
192                 # Skip if ifn does not exist.
193                 case $_func in
194                 ifn_stop)
195                         if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
196                                 warn "$ifn does not exist.  Skipped."
197                                 _fail="${_fail} ${ifn}"
198                                 continue
199                         fi
200                 ;;
201                 esac
202                 if ${_func} ${ifn} $2; then
203                         _ok="${_ok} ${ifn}"
204                         if ipv6if ${ifn}; then
205                                 _dadwait=1
206                         fi
207                 else
208                         _fail="${_fail} ${ifn}"
209                 fi
210         done
211
212         # inet6 address configuration needs sleep for DAD.
213         case ${_func}:${_dadwait} in
214         ifn_start:1|ifn_vnetup:1|ifn_vnetdown:1)
215                 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
216                 sleep 1
217         ;;
218         esac
219
220         _str=
221         if [ -n "${_ok}" ]; then
222                 case ${_func} in
223                 ifn_start)
224                         _str='Starting'
225                 ;;
226                 ifn_stop)
227                         _str='Stopping'
228                 ;;
229                 ifn_vnetup)
230                         _str='Moving'
231                 ;;
232                 ifn_vnetdown)
233                         _str='Reclaiming'
234                 ;;
235                 esac
236                 echo "${_str} Network:${_ok}."
237                 case ${_func} in
238                 ifn_vnetup)
239                         # Clear _ok not to do "ifconfig $ifn"
240                         # because $ifn is no longer in the current vnet.
241                         _ok=
242                 ;;
243                 esac
244                 if check_startmsgs; then
245                         for ifn in ${_ok}; do
246                                 /sbin/ifconfig ${ifn}
247                         done
248                 fi
249         fi
250
251         debug "The following interfaces were not configured: $_fail"
252 }
253
254 load_rc_config $name
255 run_rc_command $*