network.subr revision 100280
1#!/bin/sh -x 2# 3# $FreeBSD: head/etc/network.subr 100280 2002-07-18 05:00:17Z gordon $ 4# 5 6# PROVIDE: network1 7# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty 8# KEYWORD: FreeBSD 9 10. /etc/rc.subr 11 12name="network1" 13start_cmd="network_start" 14stop_cmd="network_stop" 15 16convert_host_conf() 17{ 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 31} 32 33generate_host_conf() 34{ 35 nsswitch_conf=$1; shift; 36 host_conf=$1; shift; 37 38 awk ' 39BEGIN { 40 xlat["files"] = "hosts"; 41 xlat["dns"] = "bind"; 42 xlat["nis"] = "nis"; 43 cont = 0; 44} 45sub(/^[\t ]*hosts:/, "") || cont { 46 if (!cont) 47 srcs = "" 48 sub(/#.*/, "") 49 gsub(/[][]/, " & ") 50 cont = sub(/\\$/, "") 51 srcs = srcs " " $0 52} 53END { 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 62} 63 64network_start() 65{ 66 # set hostname, turn on network 67 # 68 echo -n "Doing initial network setup:" 69 70 # Generate host.conf for compatibility 71 # 72 if [ -f "/etc/nsswitch.conf" ]; then 73 echo -n ' host.conf' 74 generate_host_conf /etc/nsswitch.conf /etc/host.conf 75 fi 76 77 # Convert host.conf to nsswitch.conf if necessary 78 # 79 if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then 80 echo '' 81 echo 'Warning: /etc/host.conf is no longer used' 82 echo ' /etc/nsswitch.conf will be created for you' 83 convert_host_conf /etc/host.conf /etc/nsswitch.conf 84 fi 85 86 # Set the host name if it is not already set 87 # 88 if [ -z "`hostname -s`" ]; then 89 hostname ${hostname} 90 echo -n ' hostname' 91 fi 92 93 # Set the domainname if we're using NIS 94 # 95 if checkyesno nisdomainname ; then 96 domainname ${nisdomainname} 97 echo -n ' domain' 98 fi 99 100 # Attempt to create cloned interfaces. 101 for ifn in ${cloned_interfaces}; do 102 ifconfig ${ifn} create 103 done 104 105 # Set up all the network interfaces, calling startup scripts if needed 106 # 107 case ${network_interfaces} in 108 [Aa][Uu][Tt][Oo]) 109 network_interfaces="`ifconfig -l`" 110 ;; 111 *) 112 network_interfaces="${network_interfaces} ${cloned_interfaces}" 113 ;; 114 esac 115 116 dhcp_interfaces="" 117 for ifn in ${network_interfaces}; do 118 if [ -r /etc/start_if.${ifn} ]; then 119 . /etc/start_if.${ifn} 120 eval showstat_$ifn=1 121 fi 122 123 # Do the primary ifconfig if specified 124 # 125 eval ifconfig_args=\$ifconfig_${ifn} 126 127 case ${ifconfig_args} in 128 '') 129 ;; 130 [Dd][Hh][Cc][Pp]) 131 # DHCP inits are done all in one go below 132 dhcp_interfaces="$dhcp_interfaces $ifn" 133 eval showstat_$ifn=1 134 ;; 135 *) 136 ifconfig ${ifn} ${ifconfig_args} 137 eval showstat_$ifn=1 138 ;; 139 esac 140 done 141 142 if [ ! -z "${dhcp_interfaces}" ]; then 143 ${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces} fi 144 145 for ifn in ${network_interfaces}; do 146 # Check to see if aliases need to be added 147 # 148 alias=0 149 while : ; do 150 eval ifconfig_args=\$ifconfig_${ifn}_alias${alias} 151 if [ -n "${ifconfig_args}" ]; then 152 ifconfig ${ifn} ${ifconfig_args} alias 153 eval showstat_$ifn=1 154 alias=$((${alias} + 1)) 155 else 156 break; 157 fi 158 done 159 160 # Do ipx address if specified 161 # 162 eval ifconfig_args=\$ifconfig_${ifn}_ipx 163 if [ -n "${ifconfig_args}" ]; then 164 ifconfig ${ifn} ${ifconfig_args} 165 eval showstat_$ifn=1 166 fi 167 done 168 169 # catch-all for interfaces that haven't been 'ifconfig'ed so far 170 for ifn in ${network_interfaces}; do 171 eval showstat=\$showstat_${ifn} 172 if [ ! -z ${showstat} ]; then 173 ifconfig ${ifn} 174 fi 175 done 176 177 178 179 # Check $defaultroute, then /etc/mygate, for the name of my gateway 180 # host. That name must be in /etc/hosts. 181 # 182 if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then 183 defaultroute=`cat /etc/mygate` 184 fi 185 if [ -n "$defaultroute" ]; then 186 route add default $defaultroute 187 fi 188 189 # Check if each configured interface xxN has an $ifaliases_xxN variable 190 # associated, then configure additional IP addresses for that interface. 191 # The variable contains a list of "address netmask" pairs, with 192 # "netmask" set to "-" if the interface default netmask is to be used. 193 # 194 # Note that $ifaliases_xxN works only with certain configurations and 195 # considered not recommended. Use /etc/ifconfig.xxN if possible. 196 # 197 # 198 if [ -n "$configured_interfaces" ]; then 199 echo "Adding interface aliases:" 200 done_aliases_message=yes 201 fi 202 for int in $configured_interfaces; do 203 eval args=\$ifaliases_$int 204 if [ -n "$args" ]; then 205 set -- $args 206 while [ $# -ge 2 ]; do 207 addr=$1 ; net=$2 ; shift 2 208 if [ "$net" = "-" ]; then 209 # for compatibility only, obsolete 210 ifconfig $int inet alias $addr 211 else 212 ifconfig $int inet alias $addr \ 213 netmask $net 214 fi 215 # Use loopback, not the wire 216 route add $addr 127.0.0.1 217 done 218 fi 219 done 220 221 # /etc/ifaliases, if it exists, contains the names of additional IP 222 # addresses for each interface. It is formatted as a series of lines 223 # that contain 224 # address interface netmask 225 # 226 # Note that /etc/ifaliases works only with certain cases only and its 227 # use is not recommended. Use /etc/ifconfig.xxN instead. 228 # 229 # 230 if [ -f /etc/ifaliases ]; then 231 if [ "$done_aliases_message" != yes ]; then 232 echo "Adding interface aliases:" 233 fi 234 while read addr int net; do 235 if [ -z "$net" ]; then 236 # for compatibility only, obsolete 237 ifconfig $int inet alias $addr 238 else 239 ifconfig $int inet alias $addr netmask $net 240 fi 241 # use loopback, not the wire 242 route add $addr 127.0.0.1 243 done < /etc/ifaliases 244 fi 245 246 # IPv6 interface autoconfiguration. 247 # 248 if ifconfig lo0 inet6 >/dev/null 2>&1; then 249 # wait till DAD is completed. always invoke it in case 250 # if are configured manually by ifconfig 251 # 252 dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null` 253 sleep $dadcount 254 sleep 1 255 256 if checkyesno rtsol; then 257 if [ "$ip6mode" = "autohost" ]; then 258 echo 'Sending router solicitation...' 259 rtsol $rtsol_flags 260 else 261 echo 262 warn \ 263 "ip6mode must be set to 'autohost' to use rtsol." 264 fi 265 266 # wait till DAD is completed, for global addresses 267 # configured by router advert message. 268 # 269 sleep $dadcount 270 sleep 1 271 fi 272 fi 273 274 # XXX this must die 275 if [ -s /etc/netstart.local ]; then 276 sh /etc/netstart.local start 277 fi 278 fi 279 echo '.' 280 281 # Resync ipfilter 282 /etc/rc.d/ipfilter resync 283} 284 285network_stop() 286{ 287 echo "Stopping network." 288 289 # XXX this must die 290 if [ -s /etc/netstart.local ]; then 291 sh /etc/netstart.local stop 292 fi 293 294 echo "Deleting aliases." 295 if [ -f /etc/ifaliases ]; then 296 while read addr int net; do 297 ifconfig $int inet delete $addr 298 done < /etc/ifaliases 299 fi 300 301 for int in `ifconfig -lu`; do 302 eval args=\$ifaliases_$int 303 if [ -n "$args" ]; then 304 set -- $args 305 while [ $# -ge 2 ]; do 306 addr=$1 ; net=$2 ; shift 2 307 ifconfig $int inet delete $addr 308 done 309 fi 310 done 311 312 # down interfaces 313 # 314 echo -n 'Downing network interfaces:' 315 if [ "$net_interfaces" != NO ]; then 316 if checkyesno auto_ifconfig; then 317 tmp=`ifconfig -l` 318 else 319 tmp="$net_interfaces" 320 fi 321 for int in $tmp; do 322 eval args=\$ifconfig_$int 323 if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then 324 echo -n " $int" 325 ifconfig $int down 326 fi 327 done 328 echo "." 329 fi 330 331 # flush routes 332 # 333 route -n flush 334 335} 336 337load_rc_config $name 338run_rc_command "$1" 339