]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.virtual
network: Add some initialization handlers.
[people/arne_f/network.git] / functions.virtual
CommitLineData
943e3f7e 1#!/bin/bash
9620ecef
MT
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###############################################################################
943e3f7e
MT
21
22function virtual_init() {
23 module_load 8021q
24}
25
26init_register virtual_init
9620ecef
MT
27
28function device_create_virtual() {
29 log WARN "Called deprecated function device_create_virtual"
30 device_virtual_create $@
31}
32
33function virtual_create() {
34 local port=$(devicify ${1})
35 local vid=${2}
36 local mac=${3}
37 local newport=${port}v${vid}
38
39 if [ -z "${mac}" ]; then
40 mac=$(mac_generate)
41 fi
42
43 log INFO "Creating virtual device '${newport}' with address '${mac}'."
44
45 local oldport=$(virtual_get_by_parent_and_vid ${port} ${vid})
46
47 if device_exists ${oldport}; then
48 local differences
49
50 if [ "${oldport}" != "${newport}" ]; then
51 differences="${differences} name"
52 fi
53 if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
54 differences="${differences} address"
55 fi
56
57 echo "differences: $differences"
58
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."
62 return ${EXIT_ERROR}
63 else
64 log DEBUG "There is a device '${oldport}' but it not used, so we grab it to ourselves."
65 fi
66 else
67 log DEBUG "Device '${newport}' already exists and reflects our configuration. Go on."
68
69 device_set_up ${oldport}
70 return ${EXIT_OK}
71 fi
72
73 else
74 log DEBUG "Virtual device '${newport}' does not exist, yet."
75
76 vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
77 vconfig add ${port} ${vid} >/dev/null
78
79 if [ $? -ne ${EXIT_OK} ]; then
80 error_log "Could not create virtual device '${newport}'."
81 return ${EXIT_ERROR}
82 fi
83
84 oldport=$(virtual_get_by_parent_and_vid ${port} ${vid})
85
86 fi
87
88 assert device_exists ${oldport}
89
90 if ! device_exists ${oldport}; then
91 error "Could not determine the created virtual device '${newport}'."
92 return ${EXIT_ERROR}
93 fi
94
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}
98
99 if [ $? -ne ${EXIT_OK} ]; then
100 error_log "Could not set name of virtual device '${newport}'."
101 return ${EXIT_ERROR}
102 fi
103
104 assert device_exists ${newport}
105
106 # Setting new mac address
107 device_set_address ${newport} ${mac}
108
109 if [ $? -ne ${EXIT_OK} ]; then
110 error_log "Could not set address '${mac}' to virtual device '${newport}'."
111 return ${EXIT_ERROR}
112 fi
113
114 # Bring up the new device
115 device_set_up ${newport}
116
117 return ${EXIT_OK}
118}
119
120function virtual_remove() {
121 local device=$(devicify ${1})
122
123 log INFO "Removing virtual device '${device}' with address '$(macify ${device})'."
124
125 device_set_down ${device}
126
127 vconfig rem ${device} >/dev/null
128
129 if [ $? -ne ${EXIT_OK} ]; then
130 error_log "Could not remote virtual device '${newport}'."
131 return ${EXIT_ERROR}
132 fi
133
134 return ${EXIT_OK}
135}
136
137function virtual_get_parent() {
138 local device=${1}
139
140 local parent=$(grep "^${device}" < /proc/net/vlan/config | awk '{ print $NF }')
141
142 if device_exists ${parent}; then
143 echo "${parent}"
144 return ${EXIT_OK}
145 fi
146
147 return ${EXIT_ERROR}
148}
149
150function virtual_get_by_parent_and_vid() {
151 local parent=${1}
152 local vid=${2}
153
154 assert isset parent
155 assert isset vid
156
157 local v_port
158 local v_id
159 local v_parent
160
161 assert [ -e "/proc/net/vlan/config" ]
162
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
166 echo "${v_port}"
167 return ${EXIT_OK}
168 fi
169 done
170
171 return ${EXIT_ERROR}
172}
173
174function device_virtual_create() {
175 log WARN "Called deprecated function device_virtual_create"
176 virtual_create $@
177}
178
179function device_virtual_remove() {
180 log WARN "Called deprecated function device_virtual_remove"
181 virtual_remove $@
182}
183
184function device_virtual_get_parent() {
185 log WARN "Called deprecated function device_virtual_get_parent"
186 virtual_get_parent $@
187}
188
189function 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 $@
192}