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