]> git.ipfire.org Git - people/ms/network.git/blame - hooks/bridge
network: New function beautify_time.
[people/ms/network.git] / hooks / bridge
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 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
22. /lib/network/header-zone
23
d82cf370
MT
24HOOK_SETTINGS="HOOK STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE STP_PRIORITY"
25HOOK_SETTINGS="${HOOK_SETTINGS} MAC MTU"
1848564d
MT
26
27# Default values
28MAC=$(mac_generate)
29MTU=1500
30STP="on"
31STP_FORWARD_DELAY=0
32STP_HELLO=2
33STP_MAXAGE=20
d82cf370 34STP_PRIORITY=512 # XXX check out better value
1848564d
MT
35
36function _check() {
37 assert ismac MAC
38 assert isbool STP
39 assert isinteger STP_HELLO
40 assert isinteger STP_FORWARD_DELAY
d82cf370 41 assert isinteger STP_PRIORITY
1848564d
MT
42 assert isinteger MTU
43}
44
45function _parse_cmdline() {
46 while [ $# -gt 0 ]; do
47 case "${1}" in
48 --stp=*)
49 STP=${1#--stp=}
50 ;;
51 --stp-hello=*)
52 STP_HELLO=${1#--stp-hello=}
53 ;;
54 --stp-forward-delay=*)
55 STP_FORWARD_DELAY=${1#--stp-forward-delay=}
56 ;;
d82cf370
MT
57 --stp-priority=*)
58 STP_PRIORITY=${1#--stp-priority=}
59 ;;
1848564d
MT
60 --mtu=*)
61 MTU=${1#--mtu=}
62 ;;
63 --mac=*)
64 MAC=${1#--mac=}
65 ;;
66 *)
67 warning "Ignoring unknown option '${1}'"
68 ;;
69 esac
70 shift
71 done
72}
73
74function _up() {
75 local zone=${1}
76 shift
77
78 config_read ${ZONE_DIR}/${zone}/settings
79
80 if ! device_exists ${zone}; then
81 brctl addbr ${zone}
82 fi
83
7cbea20d 84 [ -n "${MAC}" ] && device_set_address ${zone} ${MAC}
1848564d
MT
85 [ -n "${MTU}" ] && device_set_mtu ${zone} ${MTU}
86
87 # Enable STP
88 if enabled STP; then
89 brctl stp ${zone} on
90
91 if [ -n "${STP_FORWARD_DELAY}" ]; then
92 brctl setfd ${zone} ${STP_FORWARD_DELAY}
93 fi
94
95 if [ -n "${STP_HELLO}" ]; then
96 brctl sethello ${zone} ${STP_HELLO}
97 fi
98
99 if [ -n "${STP_MAXAGE}" ]; then
100 brctl setmaxage ${zone} ${STP_MAXAGE}
101 fi
d82cf370
MT
102
103 if [ -n "${STP_PRIORITY}" ]; then
104 brctl setbridgeprio ${zone} ${STP_PRIORITY}
105 fi
1848564d
MT
106 else
107 brctl stp ${zone} off
108 fi
109
110 device_set_up ${zone}
111
112 # Bring all ports up
113 zone_ports_up ${zone}
114
115 # XXX Do we need this?
116 # Wait until bridge is forwarding
117 # which is needed by dhcp client
118 #if enabled STP; then
119 # bridge_is_forwarding ${zone}
120 #fi
121
122 zone_configs_up ${zone}
123
124 event_interface_up ${zone}
125
126 exit $?
127}
128
129function _down() {
130 local zone=${1}
131 shift
132
133 if ! device_is_up ${zone}; then
134 warning "Zone '${zone}' is not up"
135 exit ${EXIT_OK}
136 fi
137
138 event_interface_down ${zone}
139
140 zone_configs_down ${zone}
141 zone_ports_down ${zone}
142
143 device_set_down ${zone}
144 brctl delbr ${zone}
145
146 exit $?
147}
148
e84e4e76
MT
149function _status() {
150 local zone=${1}
151
152 cli_status_headline ${zone}
153
154 # Exit if zone is down
155 if ! zone_is_up ${zone}; then
156 echo # Empty line
157 exit ${EXIT_ERROR}
158 fi
159
d82cf370
MT
160 # XXX Add bridge stp priority here
161 # brctl does not give any information about that
162
e84e4e76
MT
163 cli_headline " Spanning Tree Protocol information:"
164 echo " Bridge ID : $(stp_bridge_id ${zone})"
165 echo " Designated root : $(stp_designated_root ${zone})"
166 echo " Path cost : $(stp_pathcost ${zone})"
167 echo # Empty line
168
169 # Topology information
170 printf " Topology changing : %6s\n" $(stp_topology_change ${zone})
d82cf370 171 printf " Topology change time : %6s\n" $(beautify_time $(stp_topology_change_time ${zone}))
e84e4e76
MT
172 printf " Topology change count : %6s\n" $(stp_topology_change_count ${zone})
173
174 cli_headline " Ports:"
175 zone_ports_cmd status ${zone}
176
177 cli_headline " Configurations:"
178 zone_configs_cmd status ${zone}
179
180 echo # Empty line
181 exit ${EXIT_OK}
182}
183
1848564d
MT
184function _addport() {
185 local zone=${1}
186 local hook=${2}
187 shift 2
188
189 if ! hook_exists port ${hook}; then
190 error "Hook does not exist '${hook}'"
191 exit ${EXIT_ERROR}
192 fi
193
194 port_hook ${hook} add ${zone}
195}
196
197run $@