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
() {
26 init_register virtual_init
28 function device_create_virtual
() {
29 log WARN
"Called deprecated function device_create_virtual"
30 device_virtual_create $@
33 function virtual_create
() {
34 local port
=$
(devicify
${1})
37 local newport
=${port}v
${vid}
39 if [ -z "${mac}" ]; then
43 log INFO
"Creating virtual device '${newport}' with address '${mac}'."
45 local oldport
=$
(virtual_get_by_parent_and_vid
${port} ${vid})
47 if device_exists
${oldport}; then
50 if [ "${oldport}" != "${newport}" ]; then
51 differences
="${differences} name"
53 if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
54 differences
="${differences} address"
57 echo "differences: $differences"
59 if [ -n "${differences}" ]; then
60 if device_is_used
${oldport}; then
61 error_log
"There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on."
64 log DEBUG
"There is a device '${oldport}' but it not used, so we grab it to ourselves."
67 log DEBUG
"Device '${newport}' already exists and reflects our configuration. Go on."
69 device_set_up
${oldport}
74 log DEBUG
"Virtual device '${newport}' does not exist, yet."
76 vconfig set_name_type DEV_PLUS_VID_NO_PAD
>/dev
/null
77 vconfig add
${port} ${vid} >/dev
/null
79 if [ $?
-ne ${EXIT_OK} ]; then
80 error_log
"Could not create virtual device '${newport}'."
84 oldport
=$
(virtual_get_by_parent_and_vid
${port} ${vid})
88 assert device_exists
${oldport}
90 if ! device_exists
${oldport}; then
91 error
"Could not determine the created virtual device '${newport}'."
95 # The device is expected to be named like ${port}.${vid}
96 # and will be renamed to the virtual schema
97 device_set_name
${oldport} ${newport}
99 if [ $?
-ne ${EXIT_OK} ]; then
100 error_log
"Could not set name of virtual device '${newport}'."
104 assert device_exists
${newport}
106 # Setting new mac address
107 device_set_address
${newport} ${mac}
109 if [ $?
-ne ${EXIT_OK} ]; then
110 error_log
"Could not set address '${mac}' to virtual device '${newport}'."
114 # Bring up the new device
115 device_set_up
${newport}
120 function virtual_remove
() {
121 local device
=$
(devicify
${1})
123 log INFO
"Removing virtual device '${device}' with address '$(macify ${device})'."
125 device_set_down
${device}
127 vconfig rem
${device} >/dev
/null
129 if [ $?
-ne ${EXIT_OK} ]; then
130 error_log
"Could not remote virtual device '${newport}'."
137 function virtual_get_parent
() {
140 local parent
=$
(grep "^${device}" < /proc
/net
/vlan
/config |
awk '{ print $NF }')
142 if device_exists
${parent}; then
150 function virtual_get_by_parent_and_vid
() {
161 assert
[ -e "/proc/net/vlan/config" ]
163 fgrep
'|' < /proc
/net
/vlan
/config |
tr -d '|' | \
164 while read v_port v_id v_parent
; do
165 if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
174 function device_virtual_create
() {
175 log WARN
"Called deprecated function device_virtual_create"
179 function device_virtual_remove
() {
180 log WARN
"Called deprecated function device_virtual_remove"
184 function device_virtual_get_parent
() {
185 log WARN
"Called deprecated function device_virtual_get_parent"
186 virtual_get_parent $@
189 function device_virtual_get_by_parent_and_vid
() {
190 log WARN
"Called deprecated function device_virtual_get_by_parent_and_vid"
191 virtual_get_by_parent_and_vid $@