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