2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 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 ###############################################################################
22 function virtual_init
() {
25 ebtables-restore
<<EOF
33 -A BROUTING -p 802_1Q -j DROP
37 init_register virtual_init
39 function device_create_virtual
() {
40 log WARN
"Called deprecated function device_create_virtual"
41 device_virtual_create $@
44 function virtual_create
() {
45 local port
=$
(devicify
${1})
48 local newport
=${port}v
${vid}
50 if [ -z "${mac}" ]; then
54 log INFO
"Creating virtual device '${newport}' with address '${mac}'."
56 local oldport
=$
(virtual_get_by_parent_and_vid
${port} ${vid})
58 if device_exists
${oldport}; then
61 if [ "${oldport}" != "${newport}" ]; then
62 differences
="${differences} name"
64 if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
65 differences
="${differences} address"
68 echo "differences: $differences"
70 if [ -n "${differences}" ]; then
71 if device_is_used
${oldport}; then
72 error_log
"There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on."
75 log DEBUG
"There is a device '${oldport}' but it not used, so we grab it to ourselves."
78 log DEBUG
"Device '${newport}' already exists and reflects our configuration. Go on."
80 device_set_up
${oldport}
85 log DEBUG
"Virtual device '${newport}' does not exist, yet."
87 vconfig set_name_type DEV_PLUS_VID_NO_PAD
>/dev
/null
88 vconfig add
${port} ${vid} >/dev
/null
90 if [ $?
-ne ${EXIT_OK} ]; then
91 error_log
"Could not create virtual device '${newport}'."
95 oldport
=$
(virtual_get_by_parent_and_vid
${port} ${vid})
99 assert device_exists
${oldport}
101 if ! device_exists
${oldport}; then
102 error
"Could not determine the created virtual device '${newport}'."
106 # The device is expected to be named like ${port}.${vid}
107 # and will be renamed to the virtual schema
108 device_set_name
${oldport} ${newport}
110 if [ $?
-ne ${EXIT_OK} ]; then
111 error_log
"Could not set name of virtual device '${newport}'."
115 assert device_exists
${newport}
117 # Setting new mac address
118 device_set_address
${newport} ${mac}
120 if [ $?
-ne ${EXIT_OK} ]; then
121 error_log
"Could not set address '${mac}' to virtual device '${newport}'."
125 # Bring up the new device
126 device_set_up
${newport}
131 function virtual_remove
() {
132 local device
=$
(devicify
${1})
134 log INFO
"Removing virtual device '${device}' with address '$(macify ${device})'."
136 device_set_down
${device}
138 vconfig rem
${device} >/dev
/null
140 if [ $?
-ne ${EXIT_OK} ]; then
141 error_log
"Could not remote virtual device '${newport}'."
148 function virtual_get_parent
() {
151 local parent
=$
(grep "^${device}" < /proc
/net
/vlan
/config |
awk '{ print $NF }')
153 if device_exists
${parent}; then
161 function virtual_get_by_parent_and_vid
() {
172 assert
[ -e "/proc/net/vlan/config" ]
174 fgrep
'|' < /proc
/net
/vlan
/config |
tr -d '|' | \
175 while read v_port v_id v_parent
; do
176 if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
185 function device_virtual_create
() {
186 log WARN
"Called deprecated function device_virtual_create"
190 function device_virtual_remove
() {
191 log WARN
"Called deprecated function device_virtual_remove"
195 function device_virtual_get_parent
() {
196 log WARN
"Called deprecated function device_virtual_get_parent"
197 virtual_get_parent $@
200 function device_virtual_get_by_parent_and_vid
() {
201 log WARN
"Called deprecated function device_virtual_get_by_parent_and_vid"
202 virtual_get_by_parent_and_vid $@