]> git.ipfire.org Git - network.git/blob - src/hooks/ports/vlan
vlan: Validate ID
[network.git] / src / hooks / ports / vlan
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
22 . /usr/lib/network/header-port
23
24 HOOK_SETTINGS=(
25 "ADDRESS"
26 "ID"
27 "PARENT_PORT"
28 )
29
30 PORT_PARENTS_VAR="PARENT_PORT"
31
32 hook_check_settings() {
33 assert ismac ADDRESS
34 assert isset PARENT_PORT
35
36 assert isinteger ID
37 assert vlan_valid_id "${ID}"
38 }
39
40 hook_find_port_name() {
41 assert isset ID
42 assert isset PARENT_PORT
43
44 print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${ID}"
45 }
46
47 hook_parse_cmdline() {
48 while [ $# -gt 0 ]; do
49 case "${1}" in
50 --address=*)
51 ADDRESS=$(cli_get_val "${1}")
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
58 ;;
59 --id=*)
60 ID=$(cli_get_val "${1}")
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
67 ;;
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 ;;
77 *)
78 error "Unknown argument '${1}'"
79 return ${EXIT_CONF_ERROR}
80 ;;
81 esac
82 shift
83 done
84
85 # Generate a random MAC address if none given
86 if ! isset ADDRESS; then
87 ADDRESS="$(mac_generate)"
88 fi
89 }
90
91 hook_create() {
92 local port="${1}"
93 assert isset port
94
95 device_exists "${port}" && exit ${EXIT_OK}
96
97 # Read configruation
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
107
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
114 # Create the VLAN device
115 if ! vlan_create "${port}" \
116 --address="${ADDRESS}" \
117 --id="${id}" \
118 --parent="${PARENT_PORT}"; then
119 error "Could not create port: ${port}"
120 return ${EXIT_ERROR}
121 fi
122
123 return ${EXIT_OK}
124 }
125
126 hook_remove() {
127 local port="${1}"
128 assert isset port
129
130 if device_exists "${port}"; then
131 vlan_remove "${port}"
132 fi
133
134 exit ${EXIT_OK}
135 }