]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.vlan
vlan: Rename tag to id
[people/ms/network.git] / src / functions / functions.vlan
CommitLineData
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
22PROC_NET_VLAN="/proc/net/vlan"
23PROC_NET_VLAN_CONFIG="${PROC_NET_VLAN}/config"
24
25VLAN_PORT_INTERFIX="v"
26
1c6a4e30 27vlan_create() {
d3a0f73d
MT
28 local device="${1}"
29 shift
7951525a 30
d3a0f73d 31 assert isset device
7951525a 32
d3a0f73d 33 local address
f24529e4 34 local id
d3a0f73d 35 local parent
d3a0f73d
MT
36
37 # Parse command line arguments
38 while [ $# -gt 0 ]; do
39 case "${1}" in
40 --address=*)
41 address=$(cli_get_val "${1}")
42 ;;
f24529e4
MT
43 --id=*)
44 id=$(cli_get_val "${1}")
45 ;;
d3a0f73d
MT
46 --parent=*)
47 parent=$(cli_get_val "${1}")
48 ;;
d3a0f73d
MT
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
7951525a 61
d3a0f73d
MT
62 # Check if address is valid
63 if ! ismac address; then
64 log ERROR "Invalid mac address: ${address}"
65 return ${EXIT_ERROR}
7951525a
MT
66 fi
67
d3a0f73d
MT
68 # Check if a device with the name does already exist
69 if device_exists "${device}"; then
70 log ERROR "Device '${device}' already exists"
7951525a
MT
71 return ${EXIT_ERROR}
72 fi
73
d3a0f73d
MT
74 # Check if the parent device exists
75 if ! device_exists "${parent}"; then
76 log ERROR "Parent device '${parent}' does not exist"
7951525a
MT
77 return ${EXIT_ERROR}
78 fi
79
d3a0f73d
MT
80 # Make the command
81 local command=(
82 ip link add link "${parent}" name "${device}"
f24529e4 83 address "${address}" type vlan id "${id}"
d3a0f73d 84 )
7951525a 85
d3a0f73d
MT
86 # Run the command
87 if ! cmd_quiet "${command[*]}"; then
88 log ERROR "Could not create VLAN device ${device}: $?"
89 return ${EXIT_ERROR}
7951525a
MT
90 fi
91
f24529e4 92 log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})"
7951525a 93
d3a0f73d 94 return ${EXIT_OK}
7951525a
MT
95}
96
1c6a4e30 97vlan_remove() {
23ddd376 98 device_delete "$@"
7951525a
MT
99}
100
1c6a4e30 101vlan_get_parent() {
7951525a
MT
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
1c6a4e30 119vlan_get_id() {
7951525a
MT
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
1c6a4e30 137vlan_get_by_parent_and_vid() {
7951525a
MT
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}