]> git.ipfire.org Git - people/arne_f/network.git/blob - hooks/zones/bridge.ports/virtual
network: Make two groups of hooks, again.
[people/arne_f/network.git] / hooks / zones / bridge.ports / virtual
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-port
23
24 HOOK_SETTINGS="HOOK DEVICE DEVICE_MAC DEVICE_VID"
25
26 DEVICE_MAC=$(mac_generate)
27
28 function _check() {
29 assert ismac DEVICE
30 assert ismac DEVICE_MAC
31
32 assert isinteger DEVICE_VID
33
34 if [ ${DEVICE_VID} -gt 4096 ]; then
35 error "DEVICE_VID is greater than 4096."
36 exit ${EXIT_ERROR}
37 fi
38
39 local reserved
40 for reserved in 0 4095; do
41 if [ "${DEVICE_VID}" = "${reserved}" ]; then
42 error "DEVICE_VID=${reserved} is reserved."
43 exit ${EXIT_ERROR}
44 fi
45 done
46 }
47
48 function _create() {
49 local zone=${1}
50 local device=${2}
51 shift 2
52
53 while [ $# -gt 0 ]; do
54 case "${1}" in
55 --mac=*)
56 DEVICE_MAC=${1#--mac=}
57 ;;
58 --id=*)
59 DEVICE_VID=${1#--id=}
60 ;;
61 *)
62 warning "Unknown argument '${1}'"
63 ;;
64 esac
65 shift
66 done
67
68 DEVICE=$(macify ${device})
69
70 _check
71
72 config_write $(zone_dir ${zone})/port.${HOOK}.$(device_hash ${device}).${DEVICE_VID} ${HOOK_SETTINGS}
73
74 exit ${EXIT_OK}
75 }
76
77 function _edit() {
78 local zone=${1}
79
80 error "TODO - edit mac addres"
81
82 exit ${EXIT_ERROR}
83 }
84
85 function _up() {
86 local zone=${1}
87 local port=${2}
88
89 config_read $(zone_dir ${zone})/${port}
90
91 if ! device_exists $(devicify ${DEVICE_MAC}); then
92 device_virtual_create ${DEVICE} ${DEVICE_VID} ${DEVICE_MAC}
93 fi
94
95 local device=$(devicify ${DEVICE_MAC})
96
97 # Set same MTU to device that the bridge has got
98 device_set_mtu ${device} $(device_get_mtu ${zone})
99
100 bridge_attach_device ${zone} ${device}
101
102 exit ${EXIT_OK}
103 }
104
105 function _down() {
106 local zone=${1}
107 local port=${2}
108
109 config_read $(zone_dir ${zone})/${port}
110
111 local device=$(devicify ${DEVICE_MAC})
112
113 if ! device_exists ${device}; then
114 error "Device '${DEVICE_MAC}' does not exist."
115 exit ${EXIT_ERROR}
116 fi
117
118 bridge_detach_device ${zone} ${device}
119
120 device_virtual_remove ${device}
121
122 exit ${EXIT_OK}
123 }
124
125 function _status() {
126 local zone=${1}
127 local port=${2}
128
129 config_read $(zone_dir ${zone})/${port}
130
131 local device=$(devicify ${DEVICE_MAC})
132
133 printf " %-10s - " "${device}"
134 if ! device_is_up ${device}; then
135 echo -ne "${COLOUR_DOWN} DOWN ${COLOUR_NORMAL}"
136 else
137 local state=$(stp_port_state ${zone} ${device})
138 local colour="COLOUR_STP_${state}"
139 printf "${!colour}%10s${COLOUR_NORMAL}" ${state}
140 fi
141
142 echo -n " - DSR: $(stp_port_designated_root ${zone} ${device})"
143 echo -n " - Cost: $(stp_port_pathcost ${zone} ${device})"
144 echo
145
146 exit ${EXIT_OK}
147 }
148
149 run $@