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