]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/network.subr
Catch up with sam's changes to network interfaces.
[FreeBSD/FreeBSD.git] / etc / network.subr
1 #!/bin/sh -x
2 #
3 # $FreeBSD$
4 #
5
6 # PROVIDE: network1
7 # REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
8 # KEYWORD: FreeBSD
9
10 . /etc/rc.subr
11
12 name="network1"
13 start_cmd="network_start"
14 stop_cmd="network_stop"
15
16 convert_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
33 generate_host_conf()
34 {
35         nsswitch_conf=$1; shift;
36         host_conf=$1; shift;
37
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
62 }
63
64 network_gif_setup() {
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                         '')
73                                 continue
74                                 ;;
75                         *)
76                                 ifconfig $i create >/dev/null 2>&1
77                                 ifconfig $i tunnel ${peers}
78                                 ifconfig $i up
79                                 ;;
80                         esac
81                 done
82                 ;;
83         esac
84 }
85
86 network_start()
87 {
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
97         fi
98
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
107
108         # Set the host name if it is not already set
109         #
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])
140                 network_interfaces="`ifconfig -l`"
141                 ;;
142         *)
143                 network_interfaces="${network_interfaces} ${cloned_interfaces}"
144                 ;;
145         esac
146
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
153
154                 # Do the primary ifconfig if specified
155                 #
156                 eval ifconfig_args=\$ifconfig_${ifn}
157
158                 case ${ifconfig_args} in
159                 '')
160                         ;;
161                 [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
165                         ;;
166                 *)
167                         ifconfig ${ifn} ${ifconfig_args}
168                         eval showstat_$ifn=1
169                         ;;
170                 esac
171         done
172
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
211 }
212
213 network_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
225 load_rc_config $name
226 run_rc_command "$1"