vlan: Rename tag to id
[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_create() {
28         local device="${1}"
29         shift
30
31         assert isset device
32
33         local address
34         local id
35         local parent
36
37         # Parse command line arguments
38         while [ $# -gt 0 ]; do
39                 case "${1}" in
40                         --address=*)
41                                 address=$(cli_get_val "${1}")
42                                 ;;
43                         --id=*)
44                                 id=$(cli_get_val "${1}")
45                                 ;;
46                         --parent=*)
47                                 parent=$(cli_get_val "${1}")
48                                 ;;
49                         *)
50                                 error "Unrecognized argument: ${1}"
51                                 return ${EXIT_ERROR}
52                                 ;;
53                 esac
54                 shift
55         done
56
57         # Generate a random MAC address if none was passed
58         if ! isset address; then
59                 address="$(mac_generate)"
60         fi
61
62         # Check if address is valid
63         if ! ismac address; then
64                 log ERROR "Invalid mac address: ${address}"
65                 return ${EXIT_ERROR}
66         fi
67
68         # Check if a device with the name does already exist
69         if device_exists "${device}"; then
70                 log ERROR "Device '${device}' already exists"
71                 return ${EXIT_ERROR}
72         fi
73
74         # Check if the parent device exists
75         if ! device_exists "${parent}"; then
76                 log ERROR "Parent device '${parent}' does not exist"
77                 return ${EXIT_ERROR}
78         fi
79
80         # Make the command
81         local command=(
82                 ip link add link "${parent}" name "${device}"
83                         address "${address}" type vlan id "${id}"
84         )
85
86         # Run the command
87         if ! cmd_quiet "${command[*]}"; then
88                 log ERROR "Could not create VLAN device ${device}: $?"
89                 return ${EXIT_ERROR}
90         fi
91
92         log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})"
93
94         return ${EXIT_OK}
95 }
96
97 vlan_remove() {
98         device_delete "$@"
99 }
100
101 vlan_get_parent() {
102         local device=${1}
103         assert isset device
104
105         # Nothing to do, if 8021q module is not loaded.
106         [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
107
108         local dev spacer1 id spacer2 parent
109         while read dev spacer1 id spacer2 parent; do
110                 [ "${device}" = "${dev}" ] || continue
111
112                 print "${parent}"
113                 return ${EXIT_OK}
114         done < ${PROC_NET_VLAN_CONFIG}
115
116         return ${EXIT_ERROR}
117 }
118
119 vlan_get_id() {
120         local device=${1}
121         assert isset device
122
123         # Nothing to do, if 8021q module is not loaded.
124         [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
125
126         local dev spacer1 id spacer2 parent
127         while read dev spacer1 id spacer2 parent; do
128                 [ "${device}" = "${dev}" ] || continue
129
130                 print "${id}"
131                 return ${EXIT_OK}
132         done < ${PROC_NET_VLAN_CONFIG}
133
134         return ${EXIT_ERROR}
135 }
136
137 vlan_get_by_parent_and_vid() {
138         local parent=${1}
139         assert isset parent
140
141         local vid=${2}
142         assert isset vid
143
144         # Nothing to do, if 8021q module is not loaded.
145         [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
146
147         local dev spacer1 id spacer2 par
148         while read dev spacer1 id spacer2 par; do
149                 [ "${parent}" = "${par}" ] || continue
150                 [ "${vid}" = "${id}" ] || continue
151
152                 print "${dev}"
153                 return ${EXIT_OK}
154         done < ${PROC_NET_VLAN_CONFIG}
155
156         return ${EXIT_ERROR}
157 }