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