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.4 2005/11/19 21:47:32 swildner 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         if [ -z "$_cmdifn" ]; then
46                 #
47                 # We're operating as a general network start routine.
48                 #
49
50                 # Create cloned interfaces
51                 clone_up
52
53                 # Create IPv6<-->IPv4 tunnels
54                 gif_up
55         fi
56
57         # Configure the interface(s).
58         network_common ifn_start verbose
59
60         # Resync ipfilter
61         /etc/rc.d/ipfilter resync
62 }
63
64 network_stop()
65 {
66         echo -n "Stopping network:"
67
68         # Deconfigure the interface(s)
69         network_common ifn_stop
70         echo '.'
71 }
72
73 # network_common routine verbose
74 #       Common configuration subroutine for network interfaces. This
75 #       routine takes all the preparatory steps needed for configuriing
76 #       an interface and then calls $routine. If $verbose is specified,
77 #       it will call ifconfig(8) to show, in long format, the configured
78 #       interfaces. If $verbose is not given, it will simply output the
79 #       configured interface(s).
80 network_common()
81 {
82         _func=
83         _verbose=
84
85         if [ -z "$1" ]; then
86                 err "network_common(): No function name specified."
87         else
88                 _func="$1"
89         fi
90         [ -n "$2" ] && _verbose=yes
91
92         # Get a list of network interfaces. Do not include dhcp interfaces.
93         _ifn_list="`list_net_interfaces nodhcp`"
94
95         # Set the scope of the command (all interfaces or just one).
96         #
97         _cooked_list="$_ifn_list"
98         if [ -n "$_cmdifn" ]; then
99                 eval _cooked_list=\"`expr "$_ifn_list" : ".*\($_cmdifn\).*"`\"
100                 if [ -z "$_cooked_list" ]; then
101                         err "No such network interface: $_cmdifn"
102                         return 1
103                 fi
104         fi
105
106         for ifn in ${_cooked_list}; do
107                 if ${_func} ${ifn} ; then
108                         eval showstat_$ifn=1
109                 else
110                         _fail="$_fail $ifn"
111                 fi
112         done
113
114         # Display interfaces configured by this script
115         #
116         for ifn in ${_cooked_list}; do
117                 eval showstat=\$showstat_${ifn}
118                 if [ ! -z ${showstat} ]; then
119                         if [ -n "$_verbose" ]; then
120                                 ifconfig ${ifn}
121                         else
122                                 echo -n " ${ifn}"
123                         fi
124                 fi
125         done
126         debug "The following interfaces were not configured: $_fail"
127 }
128
129 ifn_start()
130 {
131         local ifn cfg
132         ifn="$1"
133         cfg=1
134
135         [ -z "$ifn" ] && return 1
136
137         ifscript_up ${ifn} && cfg=0
138         ifconfig_up ${ifn} && cfg=0
139         ifalias_up ${ifn} && cfg=0
140         ipx_up ${ifn} && cfg=0
141
142         return $cfg
143 }
144
145 ifn_stop()
146 {
147         local ifn cfg
148         ifn="$1"
149         cfg=1
150
151         [ -z "$ifn" ] && return 1
152
153         ipx_down ${ifn} && cfg=0
154         ifalias_down ${ifn} && cfg=0
155         ifconfig_down ${ifn} && cfg=0
156         ifscript_down ${ifn} && cfg=0
157
158         return $cfg
159 }
160
161 if [ -n "$2" ]; then
162         _cmdifn="$2"
163 fi
164
165 load_rc_config $name
166 run_rc_command "$1"