]> git.ipfire.org Git - people/stevee/network.git/blame - network
network: Remove support for blue zone.
[people/stevee/network.git] / network
CommitLineData
5b20e43a
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2009 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22BOLD="\\033[1;39m"
23NORMAL="\\033[0;39m"
24ERROR="\\033[1;31m"
25
26. /etc/init/functions
27
28if [ -e "/lib/network/functions" ]; then
29 . /lib/network/functions
30elif [ -e "lib/functions" ]; then
31 HOME_DIR="lib"
32 . lib/functions
33else
34 echo "Cannot find functions library. Exiting." >&2
35 exit 1
36fi
37
38function usage() {
39 echo -e "${BOLD}Usage $0${NORMAL}:\n"
40 case "$1" in
41 main|"")
42 echo "This script will help you configuring your network."
43 echo
44 echo "You should know that there are three different things:"
45 echo
46 echo " hook: A script to control connections and protocols."
47 echo " port: A physical connection to somewhere."
48 echo " zone: A group of ports."
49 echo
50 echo " $0 [global flags] <hook|port|zone> ... or"
51 echo " $0 [global flags] <cmd line options...>"
52 echo
53 echo -e "${BOLD}Global flags:${NORMAL}"
54 echo " --verbose -v - Turn on verbose mode."
55 echo " --debug -d - Turn on debug mode."
56 echo
57 echo -e "${BOLD}Command line options:${NORMAL}"
58 echo " help - Prints this help message."
59 echo " start - Starts the whole network."
60 echo " stop - Stops the whole network."
61 echo " restart - Restarts the whole network."
62 echo " reload - Reloads the whole network."
63 echo
64 echo " hook - Run \"$0 hook help\" for more information."
65 echo " port - Run \"$0 port help\" for more information."
66 echo " zone - Run \"$0 zone help\" for more information."
67 echo
68 ;;
69 hook*)
70 echo -e "${BOLD}Hook configuration:${NORMAL}"
71 echo
72 echo " ${0} [global options] hook <command>"
73 echo
74 echo -e "${BOLD}1st level commands:${NORMAL}"
75 echo -e " ${BOLD}list:${NORMAL}"
76 echo " Returns a list of all available hooks."
77 echo
78 echo
79 echo " ${0} [global options] hook <hook> <command>"
80 echo
81 echo -e "${BOLD}2nd level commands:${NORMAL}"
82 echo -e " ${BOLD}help:${NORMAL}"
83 echo " Displays some help about the given hook."
84 echo
85 echo " Example: $0 hook ethernet help"
86 echo
87 ;;
88 port)
89 echo -e "${BOLD}Port Configuration:${NORMAL}"
90 echo
91 echo " $0 [global options] port <command> ..."
92 echo
93 echo -e "${BOLD}Commands:${NORMAL}"
94 echo -e " ${BOLD}show:${NORMAL}"
95 echo " Displays information about a given port."
96 echo
97 echo " Requires a \"port\"."
98 echo " Example: $0 port show 00:11:22:33:44:55"
99 echo " $0 port show port0"
100 echo
101 ;;
102 zone)
103 echo -e "${BOLD}Zone Configuration:${NORMAL}"
104 echo
105 echo " $0 [global options] zone <command> ..."
106 echo
107 echo -e "${BOLD}Commands:${NORMAL}"
108 echo -e " ${BOLD}show:${NORMAL}"
109 echo " Displays information about a given zone."
110 echo
111 echo " Requires a \"zone\"."
112 echo " Example: $0 zone show green0"
113 echo
114 echo -e " ${BOLD}add:${NORMAL}"
115 echo " Adds a new zone."
116 echo
117 echo " Requires a \"zone\"."
118 echo " Example: $0 zone add green0"
119 echo
120 echo -e " ${BOLD}del:${NORMAL}"
121 echo " Deletes a zone."
122 echo
123 echo " Requires a \"zone\"."
124 echo " Example: $0 zone del green0"
125 echo
126 echo -e " ${BOLD}addport:${NORMAL}"
127 echo " Adds a port to a zone."
128 echo
129 echo " Requires a \"zone\" and \"port\"."
130 echo " Example: $0 zone addport green0 port0"
131 echo
132 echo " You may also pass a hook and its parameters:"
133 echo " $0 zone addport green0 port0 vlan 10"
134 echo
135 echo -e " ${BOLD}delport:${NORMAL}"
136 echo " Deletes a port from a zone."
137 echo
138 echo " Requires a \"zone\" and \"port\"."
139 echo " Example: $0 zone delport green0"
140 echo
141 echo " You may also pass a hook and its parameters:"
142 echo " $0 zone delport green0 port0 vlan 10"
143 echo
144 esac
145 _exit ${2-1}
146}
147
148function debug() {
149 if [ -n "$1" ]; then
150 DEBUG=$1
151 verbose $1
152 return
153 else
154 if [ "$DEBUG" = "1" ]; then
155 return 0
156 else
157 return 1
158 fi
159 fi
160}
161
162function verbose() {
163 if [ -n "$1" ]; then
164 VERBOSE=$1
165 return
166 else
167 if [ "$VERBOSE" = "1" ]; then
168 return 0
169 else
170 return 1
171 fi
172 fi
173}
174
175function decho() {
176 debug && echo -e "${ERROR}$@${NORMAL}" >&2
177}
178
179function vecho() {
180 verbose && echo -e "$@" >&2
181}
182
183function error() {
184 echo -e "${ERROR}ERROR${NORMAL}: $@" >&2
185 _exit 1
186}
187
188function _exit() {
189 local code
190 local reload
191
192 while [ $# -gt 0 ]; do
193 case "$1" in
194 --reload)
195 reload=1
196 ;;
197 [0-9]*)
198 code=$1
199 ;;
200 *)
201 error "Unrecognized argument: $1"
202 ;;
203 esac
204 shift
205 done
206
207 if [ "${reload}" = "1" ]; then
208 # Reloading network to apply changes immediately
209 vecho "Reloading network settings..."
210 cmd $0 reload
211
212 # Reload firewall, too
213 firewall=$(which firewall 2>/dev/null)
214 if [ -n "${firewall}" ]; then
215 vecho "Reloading firewall..."
216 cmd ${firewall} reload
217 fi
218 fi
219
220 decho "Exiting with code ${code}."
221 exit ${code}
222}
223
224function cmd() {
225 decho "Running command: $@"
226 if debug; then
227 DEBUG=${DEBUG} VERBOSE=${VERBOSE} $@
228 else
229 DEBUG=${DEBUG} VERBOSE=${VERBOSE} $@ >/dev/null
230 fi
231}
232
233function size() {
234 local size=${1}
235
236 local units
237 units[0]="Bytes "
238 units[1]="kBytes"
239 units[2]="MBytes"
240 units[3]="GBytes"
241 units[4]="TBytes"
242
243 local count=${#units}
244 while [ ${count} -gt 0 ]; do
245 if [ ${size} -lt 1024 ]; then
246 break
247 fi
248 size=$((${size} / 1024))
249 count=$((${count} - 1))
250 done
251 printf "%4d %s\n" "${size}" "${units[$((${#units} - ${count}))]}"
252}
253
254function port_show() {
255 local port
256 if [ $# -eq 0 ]; then
257 for port in /sys/class/net/*; do
258 port=${port##*/}
259 device_is_real ${port} || continue
260 port_show ${port}
261 done
262 return
263 fi
264
265 port=$(devicify $1)
266
267 echo "##################################################"
268 echo "#"
269 echo -e "# Port ${CLR_BOLD_BLU}${port}${NORMAL}"
270 echo "# ------------------------------------------------"
271
272 echo -n "# State: "
273 if device_is_up ${port}; then
274 echo -e "${CLR_BOLD_GRN}up${NORMAL}"
275 else
276 echo -e "${CLR_BOLD_RED}down${NORMAL}"
277 fi
278
279 echo -n "# Link : "
280 if device_has_carrier ${port}; then
281 echo -e "${CLR_BOLD_GRN}yes${NORMAL}"
282 else
283 echo -e "${CLR_BOLD_RED}no${NORMAL}"
284 fi
285
286 if device_is_up ${port}; then
287 echo "#"
288 echo "# Statistics:"
289 echo -n "# RX: $(size $(</sys/class/net/${port}/statistics/rx_bytes))"
290 echo " ($(</sys/class/net/${port}/statistics/rx_packets) packets)"
291 echo -n "# TX: $(size $(</sys/class/net/${port}/statistics/tx_bytes))"
292 echo " ($(</sys/class/net/${port}/statistics/tx_packets) packets)"
293 fi
294
295 echo "#"
296}
297
298function port_raw() {
299 local port
300 if [ $# -eq 0 ]; then
301 for port in /sys/class/net/*; do
302 port=${port##*/}
303 device_is_real ${port} || continue
304 port_raw ${port}
305 done
306 return
307 fi
308
309 port=$(devicify $1)
310
311 cat <<EOF
312[${port}]
313type=$(device_type ${port})
314mac=$(macify ${port})
315carrier=$(device_has_carrier ${port} && echo "1" || echo "0")
316up=$(device_is_up ${port} && echo "1" || echo "0")
317
318EOF
319}
320
321function port_add() {
322 local zone=${1}
323 local hook=${2}
324 shift 2
325
326 if ! zone_exists ${zone}; then
327 error "Zone ${BOLD}${zone}${NORMAL} does not exist."
328 return 1
329 fi
330
331 mkdir -p ${CONFIG_PORTS}/${port} 2>/dev/null
332 if hook_exists ${hook}; then
333 /lib/network/hooks/${hook} --zone=${zone} add $@
334 RET=$?
335 if [ "$RET" -eq "0" ]; then
336 vecho "Successfully added port to ${BOLD}${zone}${NORMAL}."
337 else
338 error "Hook ${BOLD}${hook}${NORMAL} exited with $RET."
339 return $RET
340 fi
341 else
342 error "Hook ${BOLD}${hook}${NORMAL} does not exist or is not executeable."
343 return 1
344 fi
345}
346
347function port_del() {
348 local config
349 local hook
350 local uuid
351
352 local zone=${1}
353 shift
354
355 if is_uuid ${1}; then
356 uuid=${1}
357 config="${CONFIG_UUIDS}/${uuid}"
358
359 if [ -e "${config}" ]; then
360 hook=$(config_get_hook ${config})
361 else
362 error "Given config file does not exist: ${config}."
363 return 1
364 fi
365 fi
366
367 hook_run --config=${config} pre-down
368 hook_run --config=${config} post-down
369 hook_run --config=${config} rem
370}
371
372function zone_discover() {
373 local zone=${1}
374
375 for hook in $(hook_list zone); do
376 hook_run ${hook} --zone=${zone} discover
377 done
378}
379
380function zone_show() {
381 local zone
382 zone=$1
383
384 if [ -z "$zone" ]; then
385 for zone in ${CONFIG_ZONES}/*; do
386 zone_show $(basename $zone)
387 done
388 return
389 fi
390
391 if ! zone_exists ${zone}; then
392 error "Zone ${BOLD}${zone}${NORMAL} does not exist."
393 return 2
394 fi
395
396 echo "##################################################"
397 echo "#"
398 echo -e "# Zone ${CLR_BOLD_BLU}${zone}${NORMAL}"
399 echo "# ------------------------------------------------"
400
401 # Up or down?
402 if zone_is_up ${zone}; then
403 echo -e "# Status: ${CLR_BOLD_GRN}up${NORMAL}"
404 else
405 echo -e "# Status: ${CLR_BOLD_RED}down${NORMAL}"
406 fi
407 echo "#"
408
409 # Ports
410 echo -e "# ${CLR_BOLD_BLU}Ports:${NORMAL}"
411 hooks_run_ports status ${CONFIG_ZONES}/${zone} --zone=${zone}
412
413 echo "#"
414 echo -e "# ${CLR_BOLD_BLU}Zone configurations:${NORMAL}"
415 hooks_run_zones status ${CONFIG_ZONES}/${zone} --zone=${zone}
416 echo "#"
417
418}
419
420function zone_raw() {
421 local zone
422 if [ $# -eq 0 ]; then
423 for zone in $(zone_list); do
424 zone_raw ${zone##*/}
425 done
426 return
427 fi
428
429 zone=${1}
430
431cat <<EOF
432[${zone}]
433up=$(zone_is_up ${zone} && echo "1" || echo "0")
434
435EOF
436}
437
438function zone_add() {
439 local zone=$1
440
441 if zone_exists ${zone}; then
442 error "Zone ${BOLD}${zone}${NORMAL} already exists."
443 return 1
444 fi
445
446 if ! zone_valid_name ${zone}; then
447 error "The given zone name is not valid."
448 return 1
449 fi
450
451 mkdir -p ${CONFIG_ZONES}/${zone}
452 vecho "Successfully added zone ${BOLD}${zone}${NORMAL}."
453}
454
455function zone_del() {
456 local zone=$1
457
458 if ! zone_exists ${zone}; then
459 error "Zone ${BOLD}${zone}${NORMAL} does not exist."
460 return 1
461 fi
462
463 cmd /lib/network/zone --zone=${zone} down
464 rm -rf ${CONFIG_ZONES}/${zone}
465 vecho "Successfully removed zone ${BOLD}${zone}${NORMAL}."
466}
467
468# See what to do
469while [ "$#" -gt 0 ]; do
470 arg=$1
471 shift
472
473 case "$arg" in
474 --debug|-d)
475 debug 1
476 decho "Debug mode enabled."
477 ;;
478 --verbose|-v)
479 verbose 1
480 vecho "${BOLD}Verbose mode enabled.${NORMAL}"
481 ;;
482 help|-h|--help)
483 usage main 0
484 ;;
485 start|stop|reload)
486 action=${arg}
487 for zone in $(zone_list); do
488 zone=${zone##*/}
489 decho "Running command: ${HOME_DIR}/zone --zone=${zone} ${action}"
490 DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOME_DIR}/zone --zone=${zone} ${action}
491 done
492 _exit $?
493 ;;
494 restart)
495 DEBUG=${DEBUG} VERBOSE=${VERBOSE} $0 stop $@
496 sleep 1
497 DEBUG=${DEBUG} VERBOSE=${VERBOSE} $0 start $@
498 _exit $?
499 ;;
500 hook|hooks)
501 case "$1" in
502 list)
503 hook_list
504 _exit $?
505 ;;
506 *)
507 if hook_exists ${1}; then
508 hook=${1}
509 else
510 usage hook
511 fi
512 esac
513 shift
514 case "$1" in
515 help|info)
516 if hook_exists ${hook}; then
517 hook_run ${hook} ${1}
518 _exit $?
519 else
520 error "Hook ${hook} does not exist or is not executeable."
521 _exit 1
522 fi
523 ;;
524 *)
525 usage hook
526 ;;
527 esac
528 ;;
529 p*)
530 arg=$1
531 shift
532 case "$arg" in
533 help)
534 usage port 0
535 ;;
536 show)
537 port_show $@
538 _exit $?
539 ;;
540 _raw)
541 port_raw $@
542 _exit $?
543 ;;
544 esac
545 ;;
546 z*)
547 arg=$1
548 shift
549 case "$arg" in
550 add)
551 zone_add $@
552 _exit --reload $?
553 ;;
554 addport)
555 port_add $@
556 _exit --reload $?
557 ;;
558 config)
559 zone=$1; hook=$2; shift 2
560 if [ -z "${zone}" ] || [ -z "${hook}" ]; then
561 usage config
562 fi
563 hook_run ${hook} --zone=${zone} add $@
564 _exit --reload $?
565 ;;
566 del)
567 zone_del $@
568 _exit --reload $?
569 ;;
570 delport)
571 port_del $@
572 _exit --reload $?
573 ;;
574 discover)
575 zone_discover $@
576 _exit $?
577 ;;
578 help)
579 usage zone 0
580 ;;
581 list)
582 zone_list
583 _exit $?
584 ;;
585 show)
586 zone_show $@
587 _exit $?
588 ;;
589 start|stop)
590 zone=$1; shift
591 zone_run --zone=${zone} ${arg} $@
592 ;;
593 _raw)
594 zone_raw $@
595 _exit $?
596 ;;
597 esac
598 ;;
599 show)
600 arg=${1}
601 shift
602 case "${arg}" in
603 ports)
604 port_show $@
605 _exit 0
606 ;;
607 esac
608 ;;
609 -*)
610 error "Option \"$arg\" is not known."
611 ;;
612 esac
613done
614
615usage main