]>
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 | ||
25 | VLAN_PORT_INTERFIX="v" | |
26 | ||
fc1e91cc MT |
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 | ||
1c6a4e30 | 44 | vlan_create() { |
d3a0f73d MT |
45 | local device="${1}" |
46 | shift | |
7951525a | 47 | |
d3a0f73d | 48 | assert isset device |
7951525a | 49 | |
d3a0f73d | 50 | local address |
fc1e91cc | 51 | local id=1 |
d3a0f73d | 52 | local parent |
d3a0f73d MT |
53 | |
54 | # Parse command line arguments | |
55 | while [ $# -gt 0 ]; do | |
56 | case "${1}" in | |
57 | --address=*) | |
58 | address=$(cli_get_val "${1}") | |
59 | ;; | |
f24529e4 MT |
60 | --id=*) |
61 | id=$(cli_get_val "${1}") | |
62 | ;; | |
d3a0f73d MT |
63 | --parent=*) |
64 | parent=$(cli_get_val "${1}") | |
65 | ;; | |
d3a0f73d MT |
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 | |
7951525a | 78 | |
d3a0f73d MT |
79 | # Check if address is valid |
80 | if ! ismac address; then | |
81 | log ERROR "Invalid mac address: ${address}" | |
82 | return ${EXIT_ERROR} | |
7951525a MT |
83 | fi |
84 | ||
fc1e91cc MT |
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 | ||
d3a0f73d MT |
91 | # Check if a device with the name does already exist |
92 | if device_exists "${device}"; then | |
93 | log ERROR "Device '${device}' already exists" | |
7951525a MT |
94 | return ${EXIT_ERROR} |
95 | fi | |
96 | ||
d3a0f73d MT |
97 | # Check if the parent device exists |
98 | if ! device_exists "${parent}"; then | |
99 | log ERROR "Parent device '${parent}' does not exist" | |
7951525a MT |
100 | return ${EXIT_ERROR} |
101 | fi | |
102 | ||
d3a0f73d MT |
103 | # Make the command |
104 | local command=( | |
105 | ip link add link "${parent}" name "${device}" | |
f24529e4 | 106 | address "${address}" type vlan id "${id}" |
d3a0f73d | 107 | ) |
7951525a | 108 | |
d3a0f73d MT |
109 | # Run the command |
110 | if ! cmd_quiet "${command[*]}"; then | |
111 | log ERROR "Could not create VLAN device ${device}: $?" | |
112 | return ${EXIT_ERROR} | |
7951525a MT |
113 | fi |
114 | ||
f24529e4 | 115 | log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})" |
7951525a | 116 | |
d3a0f73d | 117 | return ${EXIT_OK} |
7951525a MT |
118 | } |
119 | ||
1c6a4e30 | 120 | vlan_remove() { |
23ddd376 | 121 | device_delete "$@" |
7951525a MT |
122 | } |
123 | ||
1c6a4e30 | 124 | vlan_get_parent() { |
7951525a MT |
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 | ||
1c6a4e30 | 142 | vlan_get_id() { |
7951525a MT |
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 | ||
1c6a4e30 | 160 | vlan_get_by_parent_and_vid() { |
7951525a MT |
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 | } |