]>
Commit | Line | Data |
---|---|---|
7951525a MT |
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 | ||
2eb7011c MT |
25 | VLAN_SUPPORTED_PROTOCOLS=( |
26 | "802.1Q" # default | |
27 | "802.1ad" | |
28 | ) | |
29 | ||
7951525a MT |
30 | VLAN_PORT_INTERFIX="v" |
31 | ||
fc1e91cc MT |
32 | vlan_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 | ||
2eb7011c MT |
49 | vlan_supported_protocol() { |
50 | local proto="${1}" | |
51 | assert isset proto | |
52 | ||
53 | list_match "${proto}" "${VLAN_SUPPORTED_PROTOCOLS[@]}" | |
54 | } | |
55 | ||
1c6a4e30 | 56 | vlan_create() { |
d3a0f73d MT |
57 | local device="${1}" |
58 | shift | |
7951525a | 59 | |
d3a0f73d | 60 | assert isset device |
7951525a | 61 | |
d3a0f73d | 62 | local address |
fc1e91cc | 63 | local id=1 |
d3a0f73d | 64 | local parent |
2eb7011c | 65 | local protocol="${VLAN_SUPPORTED_PROTOCOLS[0]}" |
d3a0f73d MT |
66 | |
67 | # Parse command line arguments | |
68 | while [ $# -gt 0 ]; do | |
69 | case "${1}" in | |
70 | --address=*) | |
71 | address=$(cli_get_val "${1}") | |
72 | ;; | |
f24529e4 MT |
73 | --id=*) |
74 | id=$(cli_get_val "${1}") | |
75 | ;; | |
d3a0f73d MT |
76 | --parent=*) |
77 | parent=$(cli_get_val "${1}") | |
78 | ;; | |
2eb7011c MT |
79 | --protocol=*) |
80 | protocol=$(cli_get_val "${1}") | |
81 | ;; | |
d3a0f73d MT |
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 | |
7951525a | 94 | |
d3a0f73d MT |
95 | # Check if address is valid |
96 | if ! ismac address; then | |
97 | log ERROR "Invalid mac address: ${address}" | |
98 | return ${EXIT_ERROR} | |
7951525a MT |
99 | fi |
100 | ||
2eb7011c MT |
101 | # Check protocol |
102 | if ! vlan_supported_protocol "${protocol}"; then | |
103 | log ERROR "Invalid protocol: ${protocol}" | |
104 | return ${EXIT_ERROR} | |
105 | fi | |
106 | ||
fc1e91cc MT |
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 | ||
d3a0f73d MT |
113 | # Check if a device with the name does already exist |
114 | if device_exists "${device}"; then | |
115 | log ERROR "Device '${device}' already exists" | |
7951525a MT |
116 | return ${EXIT_ERROR} |
117 | fi | |
118 | ||
d3a0f73d MT |
119 | # Check if the parent device exists |
120 | if ! device_exists "${parent}"; then | |
121 | log ERROR "Parent device '${parent}' does not exist" | |
7951525a MT |
122 | return ${EXIT_ERROR} |
123 | fi | |
124 | ||
d3a0f73d MT |
125 | # Make the command |
126 | local command=( | |
127 | ip link add link "${parent}" name "${device}" | |
2eb7011c | 128 | address "${address}" type vlan proto "${protocol}" id "${id}" |
d3a0f73d | 129 | ) |
7951525a | 130 | |
d3a0f73d MT |
131 | # Run the command |
132 | if ! cmd_quiet "${command[*]}"; then | |
133 | log ERROR "Could not create VLAN device ${device}: $?" | |
134 | return ${EXIT_ERROR} | |
7951525a MT |
135 | fi |
136 | ||
f24529e4 | 137 | log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})" |
7951525a | 138 | |
d3a0f73d | 139 | return ${EXIT_OK} |
7951525a MT |
140 | } |
141 | ||
1c6a4e30 | 142 | vlan_remove() { |
23ddd376 | 143 | device_delete "$@" |
7951525a MT |
144 | } |
145 | ||
1c6a4e30 | 146 | vlan_get_parent() { |
7951525a MT |
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 | ||
1c6a4e30 | 164 | vlan_get_id() { |
7951525a MT |
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 | ||
1c6a4e30 | 182 | vlan_get_by_parent_and_vid() { |
7951525a MT |
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 | } |