2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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 PROC_NET_VLAN
="/proc/net/vlan"
23 PROC_NET_VLAN_CONFIG
="${PROC_NET_VLAN}/config"
25 VLAN_SUPPORTED_PROTOCOLS
=(
30 VLAN_PORT_INTERFIX
="v"
36 if ! isinteger id
; then
40 # Must be between 1 and 4095
41 if [ ${id} -ge 1 ] && [ ${id} -le 4096 ]; then
45 # Otherwise this is invalid
49 vlan_supported_protocol
() {
53 list_match
"${proto}" "${VLAN_SUPPORTED_PROTOCOLS[@]}"
65 local protocol
="${VLAN_SUPPORTED_PROTOCOLS[0]}"
67 # Parse command line arguments
68 while [ $# -gt 0 ]; do
71 address
=$
(cli_get_val
"${1}")
74 id
=$
(cli_get_val
"${1}")
77 parent
=$
(cli_get_val
"${1}")
80 protocol
=$
(cli_get_val
"${1}")
83 error
"Unrecognized argument: ${1}"
90 # Generate a random MAC address if none was passed
91 if ! isset address
; then
92 address
="$(mac_generate)"
95 # Check if address is valid
96 if ! ismac address
; then
97 log ERROR
"Invalid mac address: ${address}"
102 if ! vlan_supported_protocol
"${protocol}"; then
103 log ERROR
"Invalid protocol: ${protocol}"
108 if ! vlan_valid_id
"${id}"; then
109 log ERROR
"Invalid VLAN ID: ${id}"
113 # Check if a device with the name does already exist
114 if device_exists
"${device}"; then
115 log ERROR
"Device '${device}' already exists"
119 # Check if the parent device exists
120 if ! device_exists
"${parent}"; then
121 log ERROR
"Parent device '${parent}' does not exist"
127 ip link add link
"${parent}" name
"${device}"
128 address
"${address}" type vlan proto "${protocol}" id "${id}"
132 if ! cmd_quiet
"${command[*]}"; then
133 log ERROR
"Could not create VLAN device ${device}: $?"
137 log DEBUG
"Created VLAN device ${device} (parent = ${parent}, id = ${id})"
150 # Nothing to do, if 8021q module is not loaded.
151 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
153 local dev spacer1 id spacer2 parent
154 while read dev spacer1 id spacer2 parent
; do
155 [ "${device}" = "${dev}" ] ||
continue
159 done < ${PROC_NET_VLAN_CONFIG}
168 # Nothing to do, if 8021q module is not loaded.
169 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
171 local dev spacer1 id spacer2 parent
172 while read dev spacer1 id spacer2 parent
; do
173 [ "${device}" = "${dev}" ] ||
continue
177 done < ${PROC_NET_VLAN_CONFIG}
182 vlan_get_by_parent_and_vid
() {
189 # Nothing to do, if 8021q module is not loaded.
190 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
192 local dev spacer1 id spacer2 par
193 while read dev spacer1 id spacer2 par
; do
194 [ "${parent}" = "${par}" ] ||
continue
195 [ "${vid}" = "${id}" ] ||
continue
199 done < ${PROC_NET_VLAN_CONFIG}