]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/ports/vlan
vlan: Validate ID
[people/stevee/network.git] / src / hooks / ports / vlan
CommitLineData
711ffac1
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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
987dfeb4 22. /usr/lib/network/header-port
711ffac1 23
d389e96b
MT
24HOOK_SETTINGS=(
25 "ADDRESS"
f24529e4 26 "ID"
a2f35a67 27 "PARENT_PORT"
d389e96b 28)
711ffac1 29
a2f35a67 30PORT_PARENTS_VAR="PARENT_PORT"
98f4dae6 31
1c6a4e30 32hook_check_settings() {
fc1e91cc 33 assert ismac ADDRESS
a2f35a67 34 assert isset PARENT_PORT
7951525a 35
fc1e91cc
MT
36 assert isinteger ID
37 assert vlan_valid_id "${ID}"
711ffac1
MT
38}
39
96045e9c 40hook_find_port_name() {
f24529e4 41 assert isset ID
a2f35a67 42 assert isset PARENT_PORT
96045e9c 43
f24529e4 44 print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${ID}"
96045e9c
MT
45}
46
47hook_parse_cmdline() {
711ffac1
MT
48 while [ $# -gt 0 ]; do
49 case "${1}" in
7951525a 50 --address=*)
2212045f 51 ADDRESS=$(cli_get_val "${1}")
abb65554
MT
52
53 # Validate address
54 if ! mac_is_valid "${ADDRESS}"; then
55 error "Invalid MAC address given: ${ADDRESS}"
56 return ${EXIT_CONF_ERROR}
57 fi
711ffac1 58 ;;
f24529e4
MT
59 --id=*)
60 ID=$(cli_get_val "${1}")
fc1e91cc
MT
61
62 # Validate VLAN ID
63 if ! vlan_valid_id "${ID}"; then
64 error "Invalid VLAN ID: ${ID}"
65 return ${EXIT_CONF_ERROR}
66 fi
f24529e4 67 ;;
a2f35a67
MT
68 --port=*)
69 PARENT_PORT=$(cli_get_val "${1}")
70
71 # Check if PARENT_PORT exists
72 if ! port_exists "${PARENT_PORT}"; then
73 error "Port '${PARENT_PORT}' does not exist"
74 return ${EXIT_CONF_ERROR}
75 fi
76 ;;
a2f35a67 77 *)
0cf39f2d
MT
78 error "Unknown argument '${1}'"
79 return ${EXIT_CONF_ERROR}
80 ;;
711ffac1
MT
81 esac
82 shift
83 done
abb65554
MT
84
85 # Generate a random MAC address if none given
86 if ! isset ADDRESS; then
87 ADDRESS="$(mac_generate)"
88 fi
711ffac1
MT
89}
90
1c6a4e30 91hook_create() {
1ba6a2bb 92 local port="${1}"
711ffac1
MT
93 assert isset port
94
1ba6a2bb 95 device_exists "${port}" && exit ${EXIT_OK}
7951525a 96
1ba6a2bb 97 # Read configruation
47767231
MT
98 if ! port_settings_read "${port}"; then
99 return ${EXIT_ERROR}
100 fi
101
102 # Check if the parent port exists
103 if ! port_exists "${PARENT_PORT}"; then
104 error "Port '${PARENT_PORT}' does not exist"
105 return ${EXIT_ERROR}
106 fi
711ffac1 107
68cacd23
MT
108 # Create the partent port first
109 if ! port_create "${PARENT_PORT}"; then
110 error "Could not bring up parent port: ${PARENT_PORT}"
111 return ${EXIT_ERROR}
112 fi
113
1ba6a2bb 114 # Create the VLAN device
d3a0f73d
MT
115 if ! vlan_create "${port}" \
116 --address="${ADDRESS}" \
f24529e4
MT
117 --id="${id}" \
118 --parent="${PARENT_PORT}"; then
d3a0f73d
MT
119 error "Could not create port: ${port}"
120 return ${EXIT_ERROR}
121 fi
7951525a 122
d3a0f73d 123 return ${EXIT_OK}
711ffac1
MT
124}
125
1c6a4e30 126hook_remove() {
1ba6a2bb 127 local port="${1}"
711ffac1
MT
128 assert isset port
129
1ba6a2bb
MT
130 if device_exists "${port}"; then
131 vlan_remove "${port}"
711ffac1
MT
132 fi
133
711ffac1
MT
134 exit ${EXIT_OK}
135}