]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/vlan
Makefile: Fix typo in localstatedir
[people/ms/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 "PROTOCOL"
29 )
30
31 # Set the default to 802.1Q
32 DEFAULT_PROTOCOL="${VLAN_SUPPORTED_PROTOCOLS[0]}"
33
34 PORT_PARENTS_VAR="PARENT_PORT"
35
36 hook_check_settings() {
37 assert ismac ADDRESS
38 assert isset PARENT_PORT
39
40 assert isset PROTOCOL
41 assert vlan_supported_protocol "${PROTOCOL}"
42
43 assert isinteger ID
44 assert vlan_valid_id "${ID}"
45 }
46
47 hook_find_port_name() {
48 assert isset ID
49 assert isset PARENT_PORT
50
51 print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${ID}"
52 }
53
54 hook_parse_cmdline() {
55 while [ $# -gt 0 ]; do
56 case "${1}" in
57 --address=*)
58 ADDRESS=$(cli_get_val "${1}")
59
60 # Validate address
61 if ! mac_is_valid "${ADDRESS}"; then
62 error "Invalid MAC address given: ${ADDRESS}"
63 return ${EXIT_CONF_ERROR}
64 fi
65 ;;
66 --id=*)
67 ID=$(cli_get_val "${1}")
68
69 # Validate VLAN ID
70 if ! vlan_valid_id "${ID}"; then
71 error "Invalid VLAN ID: ${ID}"
72 return ${EXIT_CONF_ERROR}
73 fi
74 ;;
75 --port=*)
76 PARENT_PORT=$(cli_get_val "${1}")
77
78 # Check if PARENT_PORT exists
79 if ! port_exists "${PARENT_PORT}"; then
80 error "Port '${PARENT_PORT}' does not exist"
81 return ${EXIT_CONF_ERROR}
82 fi
83 ;;
84 --protocol=*)
85 PROTOCOL="$(cli_get_val "${1}")"
86
87 # Check if PROTOCOL is supported
88 if ! vlan_supported_protocol "${PROTOCOL}"; then
89 error "Protocol '${PROTOCOL}' is not supported"
90 error "Choose one of ${VLAN_SUPPORTED_PROTOCOLS[*]}"
91 return ${EXIT_CONF_ERROR}
92 fi
93 ;;
94 *)
95 error "Unknown argument '${1}'"
96 return ${EXIT_CONF_ERROR}
97 ;;
98 esac
99 shift
100 done
101
102 # Generate a random MAC address if none given
103 if ! isset ADDRESS; then
104 ADDRESS="$(mac_generate)"
105 fi
106 }
107
108 hook_create() {
109 local port="${1}"
110 assert isset port
111
112 device_exists "${port}" && exit ${EXIT_OK}
113
114 # Read configruation
115 if ! port_settings_read "${port}"; then
116 return ${EXIT_ERROR}
117 fi
118
119 # Check if the parent port exists
120 if ! port_exists "${PARENT_PORT}"; then
121 error "Port '${PARENT_PORT}' does not exist"
122 return ${EXIT_ERROR}
123 fi
124
125 # Create the partent port first
126 if ! port_create "${PARENT_PORT}"; then
127 error "Could not bring up parent port: ${PARENT_PORT}"
128 return ${EXIT_ERROR}
129 fi
130
131 # Create the VLAN device
132 if ! vlan_create "${port}" \
133 --address="${ADDRESS}" \
134 --id="${ID}" \
135 --parent="${PARENT_PORT}" \
136 --protocol="${PROTOCOL}"; then
137 error "Could not create port: ${port}"
138 return ${EXIT_ERROR}
139 fi
140
141 return ${EXIT_OK}
142 }
143
144 hook_remove() {
145 local port="${1}"
146 assert isset port
147
148 if device_exists "${port}"; then
149 vlan_remove "${port}"
150 fi
151
152 exit ${EXIT_OK}
153 }