]>
git.ipfire.org Git - people/stevee/network.git/blob - network
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2009 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
28 if [ -e "/lib/network/functions" ]; then
29 .
/lib
/network
/functions
30 elif [ -e "lib/functions" ]; then
34 echo "Cannot find functions library. Exiting." >&2
39 echo -e "${BOLD}Usage $0${NORMAL}:\n"
42 echo "This script will help you configuring your network."
44 echo "You should know that there are three different things:"
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."
50 echo " $0 [global flags] <hook|port|zone> ... or"
51 echo " $0 [global flags] <cmd line options...>"
53 echo -e "${BOLD}Global flags:${NORMAL}"
54 echo " --verbose -v - Turn on verbose mode."
55 echo " --debug -d - Turn on debug mode."
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."
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."
70 echo -e "${BOLD}Hook configuration:${NORMAL}"
72 echo " ${0} [global options] hook <command>"
74 echo -e "${BOLD}1st level commands:${NORMAL}"
75 echo -e " ${BOLD}list:${NORMAL}"
76 echo " Returns a list of all available hooks."
79 echo " ${0} [global options] hook <hook> <command>"
81 echo -e "${BOLD}2nd level commands:${NORMAL}"
82 echo -e " ${BOLD}help:${NORMAL}"
83 echo " Displays some help about the given hook."
85 echo " Example: $0 hook ethernet help"
89 echo -e "${BOLD}Port Configuration:${NORMAL}"
91 echo " $0 [global options] port <command> ..."
93 echo -e "${BOLD}Commands:${NORMAL}"
94 echo -e " ${BOLD}show:${NORMAL}"
95 echo " Displays information about a given port."
97 echo " Requires a \"port\"."
98 echo " Example: $0 port show 00:11:22:33:44:55"
99 echo " $0 port show port0"
103 echo -e "${BOLD}Zone Configuration:${NORMAL}"
105 echo " $0 [global options] zone <command> ..."
107 echo -e "${BOLD}Commands:${NORMAL}"
108 echo -e " ${BOLD}show:${NORMAL}"
109 echo " Displays information about a given zone."
111 echo " Requires a \"zone\"."
112 echo " Example: $0 zone show green0"
114 echo -e " ${BOLD}add:${NORMAL}"
115 echo " Adds a new zone."
117 echo " Requires a \"zone\"."
118 echo " Example: $0 zone add green0"
120 echo -e " ${BOLD}del:${NORMAL}"
121 echo " Deletes a zone."
123 echo " Requires a \"zone\"."
124 echo " Example: $0 zone del green0"
126 echo -e " ${BOLD}addport:${NORMAL}"
127 echo " Adds a port to a zone."
129 echo " Requires a \"zone\" and \"port\"."
130 echo " Example: $0 zone addport green0 port0"
132 echo " You may also pass a hook and its parameters:"
133 echo " $0 zone addport green0 port0 vlan 10"
135 echo -e " ${BOLD}delport:${NORMAL}"
136 echo " Deletes a port from a zone."
138 echo " Requires a \"zone\" and \"port\"."
139 echo " Example: $0 zone delport green0"
141 echo " You may also pass a hook and its parameters:"
142 echo " $0 zone delport green0 port0 vlan 10"
154 if [ "$DEBUG" = "1" ]; then
167 if [ "$VERBOSE" = "1" ]; then
176 debug
&& echo -e "${ERROR}$@${NORMAL}" >&2
180 verbose
&& echo -e "$@" >&2
184 echo -e "${ERROR}ERROR${NORMAL}: $@" >&2
192 while [ $# -gt 0 ]; do
201 error
"Unrecognized argument: $1"
207 if [ "${reload}" = "1" ]; then
208 # Reloading network to apply changes immediately
209 vecho
"Reloading network settings..."
212 # Reload firewall, too
213 firewall
=$
(which firewall
2>/dev
/null
)
214 if [ -n "${firewall}" ]; then
215 vecho
"Reloading firewall..."
216 cmd
${firewall} reload
220 decho
"Exiting with code ${code}."
225 decho
"Running command: $@"
227 DEBUG
=${DEBUG} VERBOSE
=${VERBOSE} $@
229 DEBUG
=${DEBUG} VERBOSE
=${VERBOSE} $@
>/dev
/null
243 local count
=${#units}
244 while [ ${count} -gt 0 ]; do
245 if [ ${size} -lt 1024 ]; then
248 size
=$
((${size} / 1024))
249 count
=$
((${count} - 1))
251 printf "%4d %s\n" "${size}" "${units[$((${#units} - ${count}))]}"
254 function port_show
() {
256 if [ $# -eq 0 ]; then
257 for port
in /sys
/class
/net
/*; do
259 device_is_real
${port} ||
continue
267 echo "##################################################"
269 echo -e "# Port ${CLR_BOLD_BLU}${port}${NORMAL}"
270 echo "# ------------------------------------------------"
273 if device_is_up
${port}; then
274 echo -e "${CLR_BOLD_GRN}up${NORMAL}"
276 echo -e "${CLR_BOLD_RED}down${NORMAL}"
280 if device_has_carrier
${port}; then
281 echo -e "${CLR_BOLD_GRN}yes${NORMAL}"
283 echo -e "${CLR_BOLD_RED}no${NORMAL}"
286 if device_is_up
${port}; then
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)"
298 function port_raw
() {
300 if [ $# -eq 0 ]; then
301 for port
in /sys
/class
/net
/*; do
303 device_is_real
${port} ||
continue
313 type=$(device_type ${port})
314 mac=$(macify ${port})
315 carrier=$(device_has_carrier ${port} && echo "1" || echo "0")
316 up=$(device_is_up ${port} && echo "1" || echo "0")
321 function port_add
() {
326 if ! zone_exists
${zone}; then
327 error
"Zone ${BOLD}${zone}${NORMAL} does not exist."
331 mkdir
-p ${CONFIG_PORTS}/${port} 2>/dev
/null
332 if hook_exists
${hook}; then
333 /lib
/network
/hooks
/${hook} --zone=${zone} add $@
335 if [ "$RET" -eq "0" ]; then
336 vecho
"Successfully added port to ${BOLD}${zone}${NORMAL}."
338 error
"Hook ${BOLD}${hook}${NORMAL} exited with $RET."
342 error
"Hook ${BOLD}${hook}${NORMAL} does not exist or is not executeable."
347 function port_del
() {
355 if is_uuid
${1}; then
357 config
="${CONFIG_UUIDS}/${uuid}"
359 if [ -e "${config}" ]; then
360 hook
=$
(config_get_hook
${config})
362 error
"Given config file does not exist: ${config}."
367 hook_run
--config=${config} pre-down
368 hook_run
--config=${config} post-down
369 hook_run
--config=${config} rem
372 function zone_discover
() {
375 for hook
in $
(hook_list zone
); do
376 hook_run
${hook} --zone=${zone} discover
380 function zone_show
() {
384 if [ -z "$zone" ]; then
385 for zone
in ${CONFIG_ZONES}/*; do
386 zone_show $
(basename $zone)
391 if ! zone_exists
${zone}; then
392 error
"Zone ${BOLD}${zone}${NORMAL} does not exist."
396 echo "##################################################"
398 echo -e "# Zone ${CLR_BOLD_BLU}${zone}${NORMAL}"
399 echo "# ------------------------------------------------"
402 if zone_is_up
${zone}; then
403 echo -e "# Status: ${CLR_BOLD_GRN}up${NORMAL}"
405 echo -e "# Status: ${CLR_BOLD_RED}down${NORMAL}"
410 echo -e "# ${CLR_BOLD_BLU}Ports:${NORMAL}"
411 hooks_run_ports status
${CONFIG_ZONES}/${zone} --zone=${zone}
414 echo -e "# ${CLR_BOLD_BLU}Zone configurations:${NORMAL}"
415 hooks_run_zones status
${CONFIG_ZONES}/${zone} --zone=${zone}
420 function zone_raw
() {
422 if [ $# -eq 0 ]; then
423 for zone
in $
(zone_list
); do
433 up=$(zone_is_up ${zone} && echo "1" || echo "0")
438 function zone_add
() {
441 if zone_exists
${zone}; then
442 error
"Zone ${BOLD}${zone}${NORMAL} already exists."
446 if ! zone_valid_name
${zone}; then
447 error
"The given zone name is not valid."
451 mkdir
-p ${CONFIG_ZONES}/${zone}
452 vecho
"Successfully added zone ${BOLD}${zone}${NORMAL}."
455 function zone_del
() {
458 if ! zone_exists
${zone}; then
459 error
"Zone ${BOLD}${zone}${NORMAL} does not exist."
463 cmd
/lib
/network
/zone
--zone=${zone} down
464 rm -rf ${CONFIG_ZONES}/${zone}
465 vecho
"Successfully removed zone ${BOLD}${zone}${NORMAL}."
469 while [ "$#" -gt 0 ]; do
476 decho
"Debug mode enabled."
480 vecho
"${BOLD}Verbose mode enabled.${NORMAL}"
487 for zone
in $
(zone_list
); do
489 decho
"Running command: ${HOME_DIR}/zone --zone=${zone} ${action}"
490 DEBUG
=${DEBUG} VERBOSE=${VERBOSE} ${HOME_DIR}/zone --zone=${zone} ${action}
495 DEBUG
=${DEBUG} VERBOSE
=${VERBOSE} $0 stop $@
497 DEBUG
=${DEBUG} VERBOSE
=${VERBOSE} $0 start $@
507 if hook_exists
${1}; then
516 if hook_exists
${hook}; then
517 hook_run
${hook} ${1}
520 error
"Hook ${hook} does not exist or is not executeable."
559 zone
=$1; hook
=$2; shift 2
560 if [ -z "${zone}" ] ||
[ -z "${hook}" ]; then
563 hook_run
${hook} --zone=${zone} add $@
591 zone_run
--zone=${zone} ${arg} $@
610 error
"Option \"$arg\" is not known."