| @@ -1,28 +1,31 @@ | | | @@ -1,28 +1,31 @@ |
1 | #!/bin/sh | | 1 | #!/bin/sh |
2 | # | | 2 | # |
3 | # $NetBSD: network,v 1.53 2008/07/24 19:48:19 reed Exp $ | | 3 | # $NetBSD: network,v 1.54 2008/09/20 12:12:38 apb Exp $ |
4 | # | | 4 | # |
5 | | | 5 | |
6 | # PROVIDE: network | | 6 | # PROVIDE: network |
7 | # REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl | | 7 | # REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl |
8 | # BEFORE: NETWORKING | | 8 | # BEFORE: NETWORKING |
9 | | | 9 | |
10 | $_rc_subr_loaded . /etc/rc.subr | | 10 | $_rc_subr_loaded . /etc/rc.subr |
11 | | | 11 | |
12 | name="network" | | 12 | name="network" |
13 | start_cmd="network_start" | | 13 | start_cmd="network_start" |
14 | stop_cmd="network_stop" | | 14 | stop_cmd="network_stop" |
15 | | | 15 | |
| | | 16 | nl=' |
| | | 17 | ' # a newline |
| | | 18 | |
16 | network_start() | | 19 | network_start() |
17 | { | | 20 | { |
18 | # set hostname, turn on network | | 21 | # set hostname, turn on network |
19 | # | | 22 | # |
20 | echo "Starting network." | | 23 | echo "Starting network." |
21 | | | 24 | |
22 | # If $hostname is set, use it for my Internet name, | | 25 | # If $hostname is set, use it for my Internet name, |
23 | # otherwise use /etc/myname | | 26 | # otherwise use /etc/myname |
24 | # | | 27 | # |
25 | if [ -z "$hostname" ] && [ -f /etc/myname ]; then | | 28 | if [ -z "$hostname" ] && [ -f /etc/myname ]; then |
26 | hostname=$(cat /etc/myname) | | 29 | hostname=$(cat /etc/myname) |
27 | fi | | 30 | fi |
28 | if [ -n "$hostname" ]; then | | 31 | if [ -n "$hostname" ]; then |
| @@ -145,32 +148,38 @@ network_start() | | | @@ -145,32 +148,38 @@ network_start() |
145 | host) | | 148 | host) |
146 | echo 'IPv6 mode: host' | | 149 | echo 'IPv6 mode: host' |
147 | ;; | | 150 | ;; |
148 | | | 151 | |
149 | *) warn "invalid \$ip6mode value "\"$ip6mode\" | | 152 | *) warn "invalid \$ip6mode value "\"$ip6mode\" |
150 | ;; | | 153 | ;; |
151 | | | 154 | |
152 | esac | | 155 | esac |
153 | fi | | 156 | fi |
154 | | | 157 | |
155 | # Configure all of the network interfaces listed in $net_interfaces; | | 158 | # Configure all of the network interfaces listed in $net_interfaces; |
156 | # if $auto_ifconfig is YES, grab all interfaces from ifconfig. | | 159 | # if $auto_ifconfig is YES, grab all interfaces from ifconfig. |
157 | # In the following, "xxN" stands in for interface names, like "le0". | | 160 | # In the following, "xxN" stands in for interface names, like "le0". |
158 | # For any interfaces that has an $ifconfig_xxN variable associated, | | 161 | # |
159 | # we do "ifconfig xxN $ifconfig_xxN". | | 162 | # For any interfaces that has an $ifconfig_xxN variable |
160 | # If there is no such variable, we take the contents of the file | | 163 | # associated, we break it into lines using ';' as a separator, |
161 | # /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each | | 164 | # then process it just like the contents of an /etc/ifconfig.xxN |
162 | # line of the file as the arguments for a separate "ifconfig" | | 165 | # file. |
163 | # invocation. | | 166 | # |
| | | 167 | # For each line from the $ifconfig_xxN variable or the |
| | | 168 | # /etc/ifconfig.xxN file, we ignore comments and blank lines, |
| | | 169 | # treat lines beginning with "!" as commands to execute, treat |
| | | 170 | # "dhcp" as a special case to invoke dhcpcd, and for any other |
| | | 171 | # line we run "ifconfig xxN", using each line of the file as the |
| | | 172 | # arguments for a separate "ifconfig" invocation. |
164 | # | | 173 | # |
165 | # In order to configure an interface reasonably, you at the very least | | 174 | # In order to configure an interface reasonably, you at the very least |
166 | # need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"), | | 175 | # need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"), |
167 | # and probably a netmask (as in "netmask 0xffffffe0"). You will | | 176 | # and probably a netmask (as in "netmask 0xffffffe0"). You will |
168 | # frequently need to specify a media type, as in "media UTP", for | | 177 | # frequently need to specify a media type, as in "media UTP", for |
169 | # interface cards with multiple media connections that do not | | 178 | # interface cards with multiple media connections that do not |
170 | # autoconfigure. See the ifconfig manual page for details. | | 179 | # autoconfigure. See the ifconfig manual page for details. |
171 | # | | 180 | # |
172 | # Note that /etc/ifconfig.xxN takes multiple lines. The following | | 181 | # Note that /etc/ifconfig.xxN takes multiple lines. The following |
173 | # configuration is possible: | | 182 | # configuration is possible: |
174 | # inet 10.1.1.1 netmask 0xffffff00 | | 183 | # inet 10.1.1.1 netmask 0xffffff00 |
175 | # inet 10.1.1.2 netmask 0xffffff00 alias | | 184 | # inet 10.1.1.2 netmask 0xffffff00 alias |
176 | # inet6 2001:db8::1 prefixlen 64 alias | | 185 | # inet6 2001:db8::1 prefixlen 64 alias |
| @@ -182,68 +191,107 @@ network_start() | | | @@ -182,68 +191,107 @@ network_start() |
182 | if checkyesno auto_ifconfig; then | | 191 | if checkyesno auto_ifconfig; then |
183 | tmp=$(/sbin/ifconfig -l) | | 192 | tmp=$(/sbin/ifconfig -l) |
184 | for cloner in $(/sbin/ifconfig -C 2>/dev/null); do | | 193 | for cloner in $(/sbin/ifconfig -C 2>/dev/null); do |
185 | for int in /etc/ifconfig.${cloner}[0-9]*; do | | 194 | for int in /etc/ifconfig.${cloner}[0-9]*; do |
186 | [ ! -f $int ] && break | | 195 | [ ! -f $int ] && break |
187 | tmp="$tmp ${int##*.}" | | 196 | tmp="$tmp ${int##*.}" |
188 | done | | 197 | done |
189 | done | | 198 | done |
190 | else | | 199 | else |
191 | tmp="$net_interfaces" | | 200 | tmp="$net_interfaces" |
192 | fi | | 201 | fi |
193 | echo -n 'Configuring network interfaces:' | | 202 | echo -n 'Configuring network interfaces:' |
194 | for int in $tmp; do | | 203 | for int in $tmp; do |
195 | eval args=\$ifconfig_$int | | 204 | eval argslist=\$ifconfig_$int |
196 | if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then | | 205 | |
197 | if /sbin/ifconfig $int create 2>/dev/null && \ | | 206 | # Skip interfaces that do not have explicit |
198 | checkyesno ipfilter; then | | 207 | # configuration information. If auto_ifconfig is |
199 | # resync ipf(4) | | 208 | # false then also warn about such interfaces. |
200 | /sbin/ipf -y >/dev/null | | 209 | # |
201 | fi | | 210 | if [ -z "$argslist" ] && ! [ -f /etc/ifconfig.$int ] |
202 | fi | | 211 | then |
203 | if [ "$args" = "dhcp" ]; then | | | |
204 | echo -n " $int" | | | |
205 | /sbin/dhcpcd -n ${dhcpcd_flags} $int | | | |
206 | elif [ -n "$args" ]; then | | | |
207 | echo -n " $int" | | | |
208 | /sbin/ifconfig $int $args | | | |
209 | elif [ -f /etc/ifconfig.$int ]; then | | | |
210 | echo -n " $int" | | | |
211 | while read args; do | | | |
212 | [ -z "$args" ] && continue | | | |
213 | case "$args" in | | | |
214 | "#"*|create) | | | |
215 | ;; | | | |
216 | "!"*) | | | |
217 | eval ${args#*!} | | | |
218 | ;; | | | |
219 | dhcp) | | | |
220 | /sbin/dhcpcd -n ${dhcpcd_flags} \ | | | |
221 | $int | | | |
222 | ;; | | | |
223 | *) | | | |
224 | eval /sbin/ifconfig $int $args | | | |
225 | ;; | | | |
226 | esac | | | |
227 | done < /etc/ifconfig.$int | | | |
228 | else | | | |
229 | if ! checkyesno auto_ifconfig; then | | 212 | if ! checkyesno auto_ifconfig; then |
230 | echo | | 213 | echo |
231 | warn \ | | 214 | warn \ |
232 | "/etc/ifconfig.$int missing and ifconfig_$int not set;" | | 215 | "/etc/ifconfig.$int missing and ifconfig_$int not set;" |
233 | warn "interface $int not configured." | | 216 | warn "interface $int not configured." |
234 | fi | | 217 | fi |
235 | continue | | 218 | continue |
236 | fi | | 219 | fi |
| | | 220 | |
| | | 221 | echo -n " $int" |
| | | 222 | |
| | | 223 | # Create the interface if necessary. |
| | | 224 | # If the interface did not exist before, |
| | | 225 | # then also resync ipf(4). |
| | | 226 | # |
| | | 227 | if /sbin/ifconfig $int create 2>/dev/null && \ |
| | | 228 | checkyesno ipfilter; then |
| | | 229 | /sbin/ipf -y >/dev/null |
| | | 230 | fi |
| | | 231 | |
| | | 232 | # If $ifconfig_xxN is empty, then use |
| | | 233 | # /etc/ifconfig.xxN, which we know exists due to |
| | | 234 | # an earlier test. |
| | | 235 | # |
| | | 236 | # If $ifconfig_xxN is non-empty and contains a |
| | | 237 | # newline, then just use it as is. (This allows |
| | | 238 | # semicolons through unmolested.) |
| | | 239 | # |
| | | 240 | # If $ifconfig_xxN is non-empty and does not |
| | | 241 | # contain a newline, then convert all semicolons |
| | | 242 | # to newlines. |
| | | 243 | # |
| | | 244 | case "$argslist" in |
| | | 245 | '') |
| | | 246 | cat /etc/ifconfig.$int |
| | | 247 | ;; |
| | | 248 | *"${nl}"*) |
| | | 249 | echo "$argslist" |
| | | 250 | ;; |
| | | 251 | *) |
| | | 252 | ( |
| | | 253 | set -o noglob |
| | | 254 | IFS=';'; set -- $argslist |
| | | 255 | #echo >&2 "[$#] [$1] [$2] [$3] [$4]" |
| | | 256 | IFS="$nl"; echo "$*" |
| | | 257 | ) |
| | | 258 | ;; |
| | | 259 | esac | |
| | | 260 | while read -r args; do |
| | | 261 | case "$args" in |
| | | 262 | ''|"#"*|create) |
| | | 263 | ;; |
| | | 264 | "!"*) |
| | | 265 | # Run arbitrary command in a subshell. |
| | | 266 | ( eval "${args#*!}" ) |
| | | 267 | ;; |
| | | 268 | dhcp) |
| | | 269 | /sbin/dhcpcd -n ${dhcpcd_flags} $int |
| | | 270 | ;; |
| | | 271 | *) |
| | | 272 | # Pass args to ifconfig. Note |
| | | 273 | # that args may contain embedded |
| | | 274 | # shell metacharacters, such as |
| | | 275 | # "ssid 'foo;*>bar'". |
| | | 276 | ( |
| | | 277 | set -o noglob |
| | | 278 | eval set -- "$args" |
| | | 279 | #echo >&2 "[$#] [$1] [$2] [$3]" |
| | | 280 | /sbin/ifconfig $int "$@" |
| | | 281 | ) |
| | | 282 | ;; |
| | | 283 | esac |
| | | 284 | done |
237 | configured_interfaces="$configured_interfaces $int" | | 285 | configured_interfaces="$configured_interfaces $int" |
238 | done | | 286 | done |
239 | echo "." | | 287 | echo "." |
240 | fi | | 288 | fi |
241 | | | 289 | |
242 | # Check $defaultroute, then /etc/mygate, for the name or address | | 290 | # Check $defaultroute, then /etc/mygate, for the name or address |
243 | # of my IPv4 gateway host. If using a name, that name must be in | | 291 | # of my IPv4 gateway host. If using a name, that name must be in |
244 | # /etc/hosts. | | 292 | # /etc/hosts. |
245 | # | | 293 | # |
246 | if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then | | 294 | if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then |
247 | defaultroute=$(cat /etc/mygate) | | 295 | defaultroute=$(cat /etc/mygate) |
248 | fi | | 296 | fi |
249 | if [ -n "$defaultroute" ]; then | | 297 | if [ -n "$defaultroute" ]; then |
| @@ -264,56 +312,56 @@ network_start() | | | @@ -264,56 +312,56 @@ network_start() |
264 | warn \ | | 312 | warn \ |
265 | "ip6mode is set to 'autohost' and a v6 default route is also set." | | 313 | "ip6mode is set to 'autohost' and a v6 default route is also set." |
266 | fi | | 314 | fi |
267 | /sbin/route add -inet6 default $defaultroute6 | | 315 | /sbin/route add -inet6 default $defaultroute6 |
268 | fi | | 316 | fi |
269 | | | 317 | |
270 | echo -n "Adding interface aliases:" | | 318 | echo -n "Adding interface aliases:" |
271 | | | 319 | |
272 | # Check if each configured interface xxN has an $ifaliases_xxN variable | | 320 | # Check if each configured interface xxN has an $ifaliases_xxN variable |
273 | # associated, then configure additional IP addresses for that interface. | | 321 | # associated, then configure additional IP addresses for that interface. |
274 | # The variable contains a list of "address netmask" pairs, with | | 322 | # The variable contains a list of "address netmask" pairs, with |
275 | # "netmask" set to "-" if the interface default netmask is to be used. | | 323 | # "netmask" set to "-" if the interface default netmask is to be used. |
276 | # | | 324 | # |
277 | # Note that $ifaliases_xxN works only with certain configurations and | | 325 | # Note that $ifaliases_xxN works only in certain cases and its |
278 | # considered not recommended. Use /etc/ifconfig.xxN if possible. | | 326 | # use is not recommended. Use /etc/ifconfig.xxN or multiple |
279 | # | | 327 | # commands in $ifconfig_xxN instead. |
280 | # | | 328 | # |
281 | for int in lo0 $configured_interfaces; do | | 329 | for int in lo0 $configured_interfaces; do |
282 | eval args=\$ifaliases_$int | | 330 | eval args=\$ifaliases_$int |
283 | if [ -n "$args" ]; then | | 331 | if [ -n "$args" ]; then |
284 | set -- $args | | 332 | set -- $args |
285 | while [ $# -ge 2 ]; do | | 333 | while [ $# -ge 2 ]; do |
286 | addr=$1 ; net=$2 ; shift 2 | | 334 | addr=$1 ; net=$2 ; shift 2 |
287 | if [ "$net" = "-" ]; then | | 335 | if [ "$net" = "-" ]; then |
288 | # for compatibility only, obsolete | | 336 | # for compatibility only, obsolete |
289 | /sbin/ifconfig $int inet alias $addr | | 337 | /sbin/ifconfig $int inet alias $addr |
290 | else | | 338 | else |
291 | /sbin/ifconfig $int inet alias $addr \ | | 339 | /sbin/ifconfig $int inet alias $addr \ |
292 | netmask $net | | 340 | netmask $net |
293 | fi | | 341 | fi |
294 | echo -n " $int:$addr" | | 342 | echo -n " $int:$addr" |
295 | done | | 343 | done |
296 | fi | | 344 | fi |
297 | done | | 345 | done |
298 | | | 346 | |
299 | # /etc/ifaliases, if it exists, contains the names of additional IP | | 347 | # /etc/ifaliases, if it exists, contains the names of additional IP |
300 | # addresses for each interface. It is formatted as a series of lines | | 348 | # addresses for each interface. It is formatted as a series of lines |
301 | # that contain | | 349 | # that contain |
302 | # address interface netmask | | 350 | # address interface netmask |
303 | # | | 351 | # |
304 | # Note that /etc/ifaliases works only with certain cases only and its | | 352 | # Note that /etc/ifaliases works only in certain cases and its |
305 | # use is not recommended. Use /etc/ifconfig.xxN instead. | | 353 | # use is not recommended. Use /etc/ifconfig.xxN or multiple |
306 | # | | 354 | # commands in $ifconfig_xxN instead. |
307 | # | | 355 | # |
308 | if [ -f /etc/ifaliases ]; then | | 356 | if [ -f /etc/ifaliases ]; then |
309 | while read addr int net; do | | 357 | while read addr int net; do |
310 | if [ -z "$net" ]; then | | 358 | if [ -z "$net" ]; then |
311 | # for compatibility only, obsolete | | 359 | # for compatibility only, obsolete |
312 | /sbin/ifconfig $int inet alias $addr | | 360 | /sbin/ifconfig $int inet alias $addr |
313 | else | | 361 | else |
314 | /sbin/ifconfig $int inet alias $addr netmask $net | | 362 | /sbin/ifconfig $int inet alias $addr netmask $net |
315 | fi | | 363 | fi |
316 | done < /etc/ifaliases | | 364 | done < /etc/ifaliases |
317 | fi | | 365 | fi |
318 | | | 366 | |
319 | echo | | 367 | echo |