Magellan Linux

Contents of /trunk/magellan-initscripts/etc/rc.d/init.d/network

Parent Directory Parent Directory | Revision Log Revision Log


Revision 506 - (show annotations) (download)
Sat Jul 21 19:31:11 2007 UTC (16 years, 9 months ago) by niro
File size: 10267 byte(s)
-added support for bridges
-added support for custom routes

1 #!/bin/bash
2 # $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.12 2007-07-21 19:31:11 niro Exp $
3
4 #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k
5 #%start: 20
6 #%stop: 80
7
8 #deps
9 #%needs:
10 #%before:
11 #%after:
12
13 source /etc/sysconfig/rc
14 source ${rc_functions}
15
16 # read values from files
17 read_value()
18 {
19 local var="$1"
20 local file="$2"
21 local value
22
23 # local all possible vars
24 # global
25 local ONBOOT
26 local NETWORKING
27
28 # static
29 local IP
30 local NETMASK
31 local BROADCAST
32 local NETWORKING
33 local FORCE_MAC_TO
34
35 # dhcp
36 local DHCP_PROG
37 local DHCP_START
38 local DHCP_STOP
39
40 # default gw
41 local GATEWAY
42 local GATEWAY_IF
43
44 # wireless extensions
45 local WIRELESS_AP
46 local WIRELESS_AUTH_MODE
47 local WIRELESS_BITRATE
48 local WIRELESS_CHANNEL
49 local WIRELESS_DEFAULT_KEY
50 local WIRELESS_ESSID
51 local WIRELESS_FREQUENCY
52 local WIRELESS_KEY
53 local WIRELESS_KEY_ASCII
54 local WIRELESS_KEY_0
55 local WIRELESS_KEY_1
56 local WIRELESS_KEY_2
57 local WIRELESS_KEY_3
58 local WIRELESS_KEY_LENGTH
59 local WIRELESS_MODE
60 local WIRELESS_NICK
61 local WIRELESS_NWID
62 local WIRELESS_POWER
63
64 source ${file}
65 eval value=\$$(echo ${var})
66 echo "${value}"
67 }
68
69 checkconfig()
70 {
71 if [ -z "${NETWORKING}" ]
72 then
73 echo "NETWORKING missing in net.${interface}, aborted"
74 exit 1
75 fi
76
77 case "${NETWORKING}" in
78 static)
79 if [ -z "${IP}" ]
80 then
81 echo "IP missing in net.${interface}, aborted"
82 exit 1
83 fi
84
85 if [ -z "${NETMASK}" ]
86 then
87 echo -n "NETMASK missing in net.${interface}, "
88 echo "using 255.255.255.0"
89 NETMASK=255.255.255.0
90 fi
91
92 if [ -z "${BROADCAST}" ]
93 then
94 echo -n "BROADCAST missing in net.${interface}, "
95 echo "using default address"
96 fi
97 ;;
98
99 dhcp)
100 if [ -z "${DHCP_PROG}" ]
101 then
102 echo "DHCP_PROG missing in net.${interface}, aborted"
103 exit 1
104 fi
105 ;;
106
107 esac
108 }
109
110 # onboot_interface_list /path/to/files*
111 onboot_interface_list()
112 {
113 local file
114 local devices
115 local iface
116
117 # get list of all devices
118 for file in $@
119 do
120 if [[ $(read_value ONBOOT ${file}) = yes ]]
121 then
122 iface="$(basename ${file} | sed s/net.//)"
123 # exclude backup files and exclude net.routes too
124 case "${iface}" in
125 *~) ;;
126 */net.routes) ;;
127 *) devices="${devices} $(basename ${file} | sed s/net.//)" ;;
128 esac
129 fi
130 done
131
132 echo "${devices}"
133 }
134
135 config_wireless_wep()
136 {
137 local iface="$1"
138
139 if [[ -z ${iface} ]]
140 then
141 echo "WEP: no \$iface given. Aborting setup."
142 return 1
143 fi
144
145 ${CURS_UP}
146 ${SET_WWCOL}
147 echo "[AUTH: WEP]"
148
149 iwconfig "${iface}" enc on
150 [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}"
151 [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}"
152 [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}"
153 }
154
155 config_wireless_wpa()
156 {
157 local iface="$1"
158
159 if [[ -z ${iface} ]]
160 then
161 echo "WPA: no \$iface given. Aborting setup."
162 return 1
163 fi
164
165 if [ ! -x /sbin/wpa_supplicant ]
166 then
167 echo "WPA: wpa_supplicant not installed. Aborting setup."
168 return 1
169 fi
170
171 ${CURS_UP}
172 ${SET_WWCOL}
173 echo "[AUTH: WPA]"
174
175 # get default settings
176 [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant
177
178 # check the configuration
179 [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto
180 [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel
181 if [[ -z ${WIRELESS_WPA_DRIVER} ]]
182 then
183 echo "WPA: WIRELESS_WPA_DRIVER given. Aborting setup."
184 return 1
185 fi
186
187 # write a config with the settings from net.${iface}
188 # only wpa-psk ! all other needs manual setup
189 if [[ ${WIRELESS_WPA_AUTOCONF} = true ]]
190 then
191 # write default cfg from skeleton
192 cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG}
193
194 # setup the network entry
195 sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \
196 -e "s:@WIRELESS_KEY@:${WIRELESS_KEY}:g" \
197 ${WIRELESS_WPA_CONFIG}
198 fi
199
200 # remove old state dir
201 [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant
202
203 # now run the wpa_supplicant dameon
204 wpa_supplicant -B \
205 -D"${WIRELESS_WPA_DRIVER}" \
206 -c"${WIRELESS_WPA_CONFIG}" \
207 -i"${iface}" \
208 ${WIRELESS_WPA_OPTS}
209
210 # echo wait 5 seconds
211 echo " Waiting 5 seconds to retrieve authentification reply ... "
212 sleep 5
213 }
214
215 setup_wireless_extensions()
216 {
217 local iface="$1"
218
219 if [[ -z ${iface} ]]
220 then
221 echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup."
222 return 1
223 fi
224
225 if [[ -n ${WIRELESS_BITRATE} ]] ||
226 [[ -n ${WIRELESS_CHANNEL} ]] ||
227 [[ -n ${WIRELESS_ESSID} ]] ||
228 [[ -n ${WIRELESS_FREQUENCY} ]] ||
229 [[ -n ${WIRELESS_MODE} ]] ||
230 [[ -n ${WIRELESS_NICK} ]] ||
231 [[ -n ${WIRELESS_AUTH_MODE} ]]
232 then
233 echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... "
234 fi
235
236 [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}"
237 [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}"
238 [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}"
239 [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}"
240 [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}"
241 [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}"
242
243 case "${WIRELESS_AUTH_MODE}" in
244 wpa) config_wireless_wpa "${iface}" ;;
245 wep|on) config_wireless_wep "${iface}" ;;
246 off) iwconfig "${iface}" enc off ;;
247 esac
248 }
249
250 config_bridge_devices()
251 {
252 local iface="$1"
253 local method="$2"
254
255 if [[ -z ${iface} ]]
256 then
257 echo "BRIDGE: no \$iface given. Aborting setup."
258 return 1
259 fi
260
261 if [[ -z ${method} ]]
262 then
263 echo "BRIDGE: no \$method given. Aborting setup."
264 return 1
265 fi
266
267 # first check for brctl
268 if [[ -z $(which brctl) ]]
269 then
270 echo "brctl not found! Please install 'net-misc/bridge-utils'."
271 return 1
272 fi
273
274 # check the config
275 if [[ -z ${BRIDGE_INTERFACE} ]]
276 then
277 echo "BRIDGE: no \$BRIDGE_INTERFACE given. Aborting setup."
278 return 1
279 fi
280
281 case ${method} in
282 add)
283 # setup the bridge device
284 brctl addbr ${iface}
285 # enter promiscous mode
286 ifconfig ${BRIDGE_INTERFACE} 0.0.0.0 promisc
287 # now setup the bridge
288 brctl addif ${iface} ${BRIDGE_INTERFACE}
289 ;;
290 remove)
291 # bring the interface down
292 ifconfig ${BRIDGE_INTERFACE} down
293 # remove the interface from the bridge
294 brctl delif ${iface} ${BRIDGE_INTERFACE}
295 # bring the bridge down
296 brctl delbr ${iface}
297 ;;
298 esac
299
300 # unset the bridge variable to be safe
301 unset BRIDGE_INTERFACE
302 # continue to setup generic networking
303 }
304
305 config_routes()
306 {
307 local method="$1"
308 local message
309
310 # only add and del are allowed
311 case ${method} in
312 add) message="Adding route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
313 del) message="Removing route ${COLBLUE}${route}${COLDEFAULT} ..." ;;
314 *)
315 echo "config_routes: unsupported \$method '${method}'."
316 exit 1
317 ;;
318 esac
319
320 # adds/delete user routes
321 if [[ -f /etc/conf.d/net.routes ]]
322 then
323 ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end
324 while read route
325 do
326 case "${route}" in
327 \#*|"") continue ;;
328 esac
329 echo -e ${COLOREDSTAR}"${message}"
330 route ${method} ${route}
331 evaluate_retval
332 done
333 fi
334 }
335
336 networking_start()
337 {
338 local iface dns routes
339
340 # get list of all devices
341 for iface in $(onboot_interface_list ${network_settings}/net.*)
342 do
343 # checkconfig
344 source ${network_settings}/net.${iface} || exit 1
345 checkconfig
346
347 # setup mac
348 if [ -n "${FORCE_MAC_TO}" ]
349 then
350 echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... "
351 ifconfig "${iface}" hw ether "${FORCE_MAC_TO}"
352 evaluate_retval
353 fi
354
355 # setup bridges
356 if [[ ${iface} = br[0-9]* ]]
357 then
358 config_bridge_devices ${iface} add
359 fi
360
361 # activate the interface
362 ifconfig "${iface}" up
363
364 # now configure wireless_extensions
365 [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}"
366
367 echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..."
368
369 # setup static or dhcp
370 case ${NETWORKING} in
371 dhcp|DHCP)
372 ${CURS_UP}
373 ${SET_WWCOL}
374 echo "[DHCP]"
375 loadproc ${DHCP_PROG} ${DHCP_START} "${iface}"
376 ;;
377 static|STATIC)
378 ${CURS_UP}
379 ${SET_WWCOL}
380 echo "[STATIC]"
381 ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}"
382 evaluate_retval
383 ;;
384 esac
385
386 # setup def gw
387 if [[ -n ${GATEWAY} ]]
388 then
389 echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..."
390 route add default gateway ${GATEWAY} metric 1 dev ${iface}
391 evaluate_retval
392 fi
393
394 # setup /etc/resolv.conf
395 if [[ -n ${NAMESERVER} ]]
396 then
397 echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..."
398
399 # whipe out the old one
400 echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf
401 for dns in ${NAMESERVER}
402 do
403 echo "nameserver ${dns}" >> /etc/resolv.conf
404 done
405 fi
406 done
407
408 # setup user routes
409 config_routes add
410 }
411
412 networking_stop()
413 {
414 # get list of all devices
415 for iface in $(onboot_interface_list ${network_settings}/net.*)
416 do
417 source ${network_settings}/net.${iface} || exit 1
418 checkconfig
419
420 if [[ -n ${GATEWAY} ]]
421 then
422 echo -e ${COLOREDSTAR}"Removing default gateway ..."
423 route del -net default
424 evaluate_retval
425 fi
426
427 echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..."
428 ifconfig ${iface} down
429 evaluate_retval
430
431 # remove bridges
432 if [[ ${iface} = br[0-9]* ]]
433 then
434 config_bridge_devices ${iface} remove
435 fi
436
437 # shutdown dhcp-daemon
438 if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]]
439 then
440 echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..."
441 ${CURS_UP}
442 ${SET_WWCOL}
443 echo "[$(basename ${DHCP_PROG})]"
444 ${DHCP_PROG} ${DHCP_STOP} "${iface}"
445 evaluate_retval
446 fi
447
448 # shutdown wpa_supplicant daemon
449 if [[ -n $(pidof wpa_supplicant) ]]
450 then
451 killall wpa_supplicant
452 fi
453 done
454
455 # remove state dir
456 if [ -d /var/run/wpa_supplicant ]
457 then
458 rm -rf /var/run/wpa_supplicant
459 fi
460
461 # delete user routes
462 config_routes del
463 }
464
465 case $1 in
466 start)
467 networking_start
468 update_svcstatus $1
469 splash svc_started "$(basename $0)" 0
470 ;;
471
472 stop)
473 networking_stop
474 update_svcstatus $1
475 splash svc_stopped "$(basename $0)" 0
476 ;;
477
478 restart)
479 $0 stop
480 sleep 1
481 $0 start
482 ;;
483
484 *)
485 echo "Usage: $0 {start|stop|restart}"
486 exit 1
487 ;;
488 esac

Properties

Name Value
svn:executable *