network.subr (110746) | network.subr (113674) |
---|---|
1#!/bin/sh -x | |
2# | 1# |
3# $FreeBSD: head/etc/network.subr 110746 2003-02-12 04:26:10Z imp $ | 2# Copyright (c) 2003 The FreeBSD Project. All rights reserved. |
4# | 3# |
4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions 6# are met: 7# 1. Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# 2. Redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution. 12# 13# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23# SUCH DAMAGE. 24# 25# $FreeBSD: head/etc/network.subr 113674 2003-04-18 17:51:54Z mtm $ 26# |
|
5 | 27 |
6# PROVIDE: network1 7# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty 8# KEYWORD: FreeBSD | 28# 29# Subroutines commonly used from network startup scripts. 30# Requires that rc.conf be loaded first. 31# |
9 | 32 |
10. /etc/rc.subr | 33# ifconfig_up if 34# Evaluate ifconfig(8) arguments for interface $if and 35# run ifconfig(8) with those arguments. It returns 0 if 36# arguments were found and executed or 1 if the interface 37# had no arguments. 38# 39ifconfig_up() 40{ 41 eval ifconfig_args=\$ifconfig_$1 42 if [ -n "${ifconfig_args}" ]; then 43 ifconfig $1 ${ifconfig_args} 44 return 0 45 fi 46 return 1 47} |
11 | 48 |
12name="network1" 13start_cmd="network_start" 14stop_cmd="network_stop" | 49# ifalias_up if 50# Configure aliases for network interface $if. 51# It returns 0 if at least one alias was configured or 52# 1 if there were none. 53# 54ifalias_up() 55{ 56 _ret=1 57 alias=0 58 while : ; do 59 eval ifconfig_args=\$ifconfig_$1_alias${alias} 60 if [ -n "${ifconfig_args}" ]; then 61 ifconfig $1 ${ifconfig_args} alias 62 alias=$((${alias} + 1)) 63 _ret=0 64 else 65 break 66 fi 67 done 68 return $_ret 69} |
15 | 70 |
16convert_host_conf() | 71# ifscript_up if 72# Evaluate a startup script for the $if interface. 73# It returns 0 if a script was found and processed or 74# 1 if no script was found. 75# 76ifscript_up() |
17{ | 77{ |
18 host_conf=$1; shift; 19 nsswitch_conf=$1; shift; 20 awk ' \ 21 /^[:blank:]*#/ { next } \ 22 /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next } \ 23 /(dns|bind)/ { nsswitch[c] = "dns"; c++; next } \ 24 /nis/ { nsswitch[c] = "nis"; c++; next } \ 25 { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" } \ 26 END { \ 27 printf "hosts: "; \ 28 for (i in nsswitch) printf "%s ", nsswitch[i]; \ 29 printf "\n"; \ 30 }' < $host_conf > $nsswitch_conf | 78 if [ -r /etc/start_if.$1 ]; then 79 . /etc/start_if.$1 80 return 0 81 fi 82 return 1 |
31} 32 | 83} 84 |
33generate_host_conf() | 85# Create cloneable interfaces. 86# 87clone_up() |
34{ | 88{ |
35 nsswitch_conf=$1; shift; 36 host_conf=$1; shift; | 89 _prefix= 90 _list= 91 for ifn in ${cloned_interfaces}; do 92 ifconfig ${ifn} create 93 if $? ; then 94 _list="${_list}${_prefix}${ifn}" 95 [ -z "$_prefix" ] && _prefix=' ' 96 fi 97 done 98 debug "Cloned: ${_list}" 99} |
37 | 100 |
38 awk ' 39 BEGIN { 40 xlat["files"] = "hosts"; 41 xlat["dns"] = "bind"; 42 xlat["nis"] = "nis"; 43 cont = 0; 44 } 45 sub(/^[\t ]*hosts:/, "") || cont { 46 if (!cont) 47 srcs = "" 48 sub(/#.*/, "") 49 gsub(/[][]/, " & ") 50 cont = sub(/\\$/, "") 51 srcs = srcs " " $0 52 } 53 END { 54 print "# Auto-generated from nsswitch.conf, do not edit" 55 ns = split(srcs, s) 56 for (n = 1; n <= ns; ++n) { 57 if (s[n] in xlat) 58 print xlat[s[n]] 59 } 60 } 61 ' <$nsswitch_conf >$host_conf | 101# Destroy cloned interfaces. Destroyed interfaces are echoed 102# to standard output. 103# 104clone_down() 105{ 106 _prefix= 107 _list= 108 for ifn in ${cloned_interfaces}; do 109 ifconfig ${ifn} destroy 110 if $? ; then 111 _list="${_list}${_prefix}${ifn}" 112 [ -z "$_prefix" ] && _prefix=' ' 113 fi 114 done 115 debug "Destroyed clones: ${_list}" |
62} 63 | 116} 117 |
64network_gif_setup() { | 118gif_up() { |
65 case ${gif_interfaces} in 66 [Nn][Oo] | '') 67 ;; 68 *) 69 for i in ${gif_interfaces}; do 70 eval peers=\$gifconfig_$i 71 case ${peers} in 72 '') --- 5 unchanged lines hidden (view full) --- 78 ifconfig $i up 79 ;; 80 esac 81 done 82 ;; 83 esac 84} 85 | 119 case ${gif_interfaces} in 120 [Nn][Oo] | '') 121 ;; 122 *) 123 for i in ${gif_interfaces}; do 124 eval peers=\$gifconfig_$i 125 case ${peers} in 126 '') --- 5 unchanged lines hidden (view full) --- 132 ifconfig $i up 133 ;; 134 esac 135 done 136 ;; 137 esac 138} 139 |
86network_start() | 140# 141# ipx_up ifn 142# Configure any IPX addresses for interface $ifn. Returns 0 if IPX 143# arguments were found and configured; returns 1 otherwise. 144# 145ipx_up() |
87{ | 146{ |
88 # set hostname, turn on network 89 # 90 echo -n "Doing initial network setup:" 91 92 # Generate host.conf for compatibility 93 # 94 if [ -f "/etc/nsswitch.conf" ]; then 95 echo -n ' host.conf' 96 generate_host_conf /etc/nsswitch.conf /etc/host.conf | 147 ifn="$1" 148 eval ifconfig_args=\$ifconfig_${ifn}_ipx 149 if [ -n "${ifconfig_args}" ]; then 150 ifconfig ${ifn} ${ifconfig_args} 151 return 0 |
97 fi | 152 fi |
153 return 1 154} |
|
98 | 155 |
99 # Convert host.conf to nsswitch.conf if necessary 100 # 101 if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then 102 echo '' 103 echo 'Warning: /etc/host.conf is no longer used' 104 echo ' /etc/nsswitch.conf will be created for you' 105 convert_host_conf /etc/host.conf /etc/nsswitch.conf 106 fi | 156# 157# list_net_interfaces type 158# List all network interfaces. The type of interface returned 159# can be controlled by the type argument. The type 160# argument can be any of the following: 161# nodhcp - all interfaces, excluding DHCP configured interfaces 162# dhcp - list only DHCP configured interfaces 163# If no argument is specified all network interfaces are output. 164# Note that the list always includes cloned interfaces. 165# 166list_net_interfaces() 167{ 168 type=$1 |
107 | 169 |
108 # Set the host name if it is not already set | 170 # Get a list of ALL the interfaces |
109 # | 171 # |
110 if [ -z "`hostname -s`" ]; then 111 hostname ${hostname} 112 echo -n ' hostname' 113 fi 114 115 # Set the domainname if we're using NIS 116 # 117 case ${nisdomainname} in 118 [Nn][Oo]|'') 119 ;; 120 *) 121 domainname ${nisdomainname} 122 echo -n ' domain' 123 ;; 124 esac 125 126 echo '.' 127 128 # Attempt to create cloned interfaces. 129 for ifn in ${cloned_interfaces}; do 130 ifconfig ${ifn} create 131 done 132 133 # gifconfig 134 network_gif_setup 135 136 # Set up all the network interfaces, calling startup scripts if needed 137 # | |
138 case ${network_interfaces} in 139 [Aa][Uu][Tt][Oo]) | 172 case ${network_interfaces} in 173 [Aa][Uu][Tt][Oo]) |
140 network_interfaces="`ifconfig -l`" | 174 _tmplist="`ifconfig -l`" |
141 ;; 142 *) | 175 ;; 176 *) |
143 network_interfaces="${network_interfaces} ${cloned_interfaces}" | 177 _tmplist="${network_interfaces}" |
144 ;; 145 esac | 178 ;; 179 esac |
180 _tmplist="${_tmplist} ${cloned_interfaces}" |
|
146 | 181 |
147 dhcp_interfaces="" 148 for ifn in ${network_interfaces}; do 149 if [ -r /etc/start_if.${ifn} ]; then 150 . /etc/start_if.${ifn} 151 eval showstat_$ifn=1 152 fi | 182 if [ -z "$type" ]; then 183 echo $_tmplist 184 return 0 185 fi |
153 | 186 |
154 # Do the primary ifconfig if specified 155 # 156 eval ifconfig_args=\$ifconfig_${ifn} 157 158 case ${ifconfig_args} in 159 '') 160 ;; | 187 # Separate out dhcp and non-dhcp intefraces 188 # 189 _aprefix= 190 _brefix= 191 for _if in ${_tmplist} ; do 192 eval _ifarg="\$ifconfig_${_if}" 193 case "$_ifarg" in |
161 [Dd][Hh][Cc][Pp]) | 194 [Dd][Hh][Cc][Pp]) |
162 # DHCP inits are done all in one go below 163 dhcp_interfaces="$dhcp_interfaces $ifn" 164 eval showstat_$ifn=1 | 195 _dhcplist="${_dhcplist}${_aprefix}${_if}" 196 [ -z "$_aprefix" ] && _aprefix=' ' |
165 ;; | 197 ;; |
166 *) 167 ifconfig ${ifn} ${ifconfig_args} 168 eval showstat_$ifn=1 | 198 ''|*) 199 _nodhcplist="${_nodhcplist}${_bprefix}${_if}" 200 [ -z "$_bprefix" ] && _bprefix=' ' |
169 ;; 170 esac 171 done 172 | 201 ;; 202 esac 203 done 204 |
173 if [ ! -z "${dhcp_interfaces}" ]; then 174 ${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces} 175 fi 176 177 for ifn in ${network_interfaces}; do 178 # Check to see if aliases need to be added 179 # 180 alias=0 181 while : ; do 182 eval ifconfig_args=\$ifconfig_${ifn}_alias${alias} 183 if [ -n "${ifconfig_args}" ]; then 184 ifconfig ${ifn} ${ifconfig_args} alias 185 eval showstat_$ifn=1 186 alias=$((${alias} + 1)) 187 else 188 break; 189 fi 190 done 191 192 # Do ipx address if specified 193 # 194 eval ifconfig_args=\$ifconfig_${ifn}_ipx 195 if [ -n "${ifconfig_args}" ]; then 196 ifconfig ${ifn} ${ifconfig_args} 197 eval showstat_$ifn=1 198 fi 199 done 200 201 # Display ifconfiged interfaces 202 for ifn in ${network_interfaces}; do 203 eval showstat=\$showstat_${ifn} 204 if [ ! -z ${showstat} ]; then 205 ifconfig ${ifn} 206 fi 207 done 208 209 # Resync ipfilter 210 /etc/rc.d/ipfilter resync | 205 case "$type" in 206 nodhcp) 207 echo $_nodhcplist 208 ;; 209 dhcp) 210 echo $_dhcplist 211 ;; 212 esac 213 return 0 |
211} | 214} |
212 213network_stop() 214{ 215 echo -n "Stopping network:" 216 217 # flush routes 218 # 219 echo -n " flush routes" 220 route -n flush 221 222 echo '.' 223} 224 225load_rc_config $name 226run_rc_command "$1" | |