]> git.ipfire.org Git - people/stevee/network.git/blame_incremental - src/functions/functions.vlan
Fix creating new configs
[people/stevee/network.git] / src / functions / functions.vlan
... / ...
CommitLineData
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2012 IPFire Network Development Team #
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
22PROC_NET_VLAN="/proc/net/vlan"
23PROC_NET_VLAN_CONFIG="${PROC_NET_VLAN}/config"
24
25VLAN_SUPPORTED_PROTOCOLS=(
26 "802.1Q" # default
27 "802.1ad"
28)
29
30VLAN_PORT_INTERFIX="v"
31
32vlan_valid_id() {
33 local id="${1}"
34
35 # Must be an integer
36 if ! isinteger id; then
37 return ${EXIT_FALSE}
38 fi
39
40 # Must be between 1 and 4095
41 if [ ${id} -ge 1 ] && [ ${id} -le 4096 ]; then
42 return ${EXIT_TRUE}
43 fi
44
45 # Otherwise this is invalid
46 return ${EXIT_FALSE}
47}
48
49vlan_supported_protocol() {
50 local proto="${1}"
51 assert isset proto
52
53 list_match "${proto}" "${VLAN_SUPPORTED_PROTOCOLS[@]}"
54}
55
56vlan_create() {
57 local device="${1}"
58 shift
59
60 assert isset device
61
62 local address
63 local id=1
64 local parent
65 local protocol="${VLAN_SUPPORTED_PROTOCOLS[0]}"
66
67 # Parse command line arguments
68 while [ $# -gt 0 ]; do
69 case "${1}" in
70 --address=*)
71 address=$(cli_get_val "${1}")
72 ;;
73 --id=*)
74 id=$(cli_get_val "${1}")
75 ;;
76 --parent=*)
77 parent=$(cli_get_val "${1}")
78 ;;
79 --protocol=*)
80 protocol=$(cli_get_val "${1}")
81 ;;
82 *)
83 error "Unrecognized argument: ${1}"
84 return ${EXIT_ERROR}
85 ;;
86 esac
87 shift
88 done
89
90 # Generate a random MAC address if none was passed
91 if ! isset address; then
92 address="$(mac_generate)"
93 fi
94
95 # Check if address is valid
96 if ! ismac address; then
97 log ERROR "Invalid mac address: ${address}"
98 return ${EXIT_ERROR}
99 fi
100
101 # Check protocol
102 if ! vlan_supported_protocol "${protocol}"; then
103 log ERROR "Invalid protocol: ${protocol}"
104 return ${EXIT_ERROR}
105 fi
106
107 # Check VLAN ID
108 if ! vlan_valid_id "${id}"; then
109 log ERROR "Invalid VLAN ID: ${id}"
110 return ${EXIT_ERROR}
111 fi
112
113 # Check if a device with the name does already exist
114 if device_exists "${device}"; then
115 log ERROR "Device '${device}' already exists"
116 return ${EXIT_ERROR}
117 fi
118
119 # Check if the parent device exists
120 if ! device_exists "${parent}"; then
121 log ERROR "Parent device '${parent}' does not exist"
122 return ${EXIT_ERROR}
123 fi
124
125 # Make the command
126 local command=(
127 ip link add link "${parent}" name "${device}"
128 address "${address}" type vlan proto "${protocol}" id "${id}"
129 )
130
131 # Run the command
132 if ! cmd_quiet "${command[*]}"; then
133 log ERROR "Could not create VLAN device ${device}: $?"
134 return ${EXIT_ERROR}
135 fi
136
137 log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})"
138
139 return ${EXIT_OK}
140}
141
142vlan_remove() {
143 device_delete "$@"
144}
145
146vlan_get_parent() {
147 local device=${1}
148 assert isset device
149
150 # Nothing to do, if 8021q module is not loaded.
151 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
152
153 local dev spacer1 id spacer2 parent
154 while read dev spacer1 id spacer2 parent; do
155 [ "${device}" = "${dev}" ] || continue
156
157 print "${parent}"
158 return ${EXIT_OK}
159 done < ${PROC_NET_VLAN_CONFIG}
160
161 return ${EXIT_ERROR}
162}
163
164vlan_get_id() {
165 local device=${1}
166 assert isset device
167
168 # Nothing to do, if 8021q module is not loaded.
169 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
170
171 local dev spacer1 id spacer2 parent
172 while read dev spacer1 id spacer2 parent; do
173 [ "${device}" = "${dev}" ] || continue
174
175 print "${id}"
176 return ${EXIT_OK}
177 done < ${PROC_NET_VLAN_CONFIG}
178
179 return ${EXIT_ERROR}
180}
181
182vlan_get_by_parent_and_vid() {
183 local parent=${1}
184 assert isset parent
185
186 local vid=${2}
187 assert isset vid
188
189 # Nothing to do, if 8021q module is not loaded.
190 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
191
192 local dev spacer1 id spacer2 par
193 while read dev spacer1 id spacer2 par; do
194 [ "${parent}" = "${par}" ] || continue
195 [ "${vid}" = "${id}" ] || continue
196
197 print "${dev}"
198 return ${EXIT_OK}
199 done < ${PROC_NET_VLAN_CONFIG}
200
201 return ${EXIT_ERROR}
202}