]>
Commit | Line | Data |
---|---|---|
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 |
24 | HOOK_SETTINGS=( |
25 | "ADDRESS" | |
f24529e4 | 26 | "ID" |
a2f35a67 | 27 | "PARENT_PORT" |
2eb7011c | 28 | "PROTOCOL" |
d389e96b | 29 | ) |
711ffac1 | 30 | |
2eb7011c MT |
31 | # Set the default to 802.1Q |
32 | DEFAULT_PROTOCOL="${VLAN_SUPPORTED_PROTOCOLS[0]}" | |
33 | ||
a2f35a67 | 34 | PORT_PARENTS_VAR="PARENT_PORT" |
98f4dae6 | 35 | |
1c6a4e30 | 36 | hook_check_settings() { |
fc1e91cc | 37 | assert ismac ADDRESS |
a2f35a67 | 38 | assert isset PARENT_PORT |
7951525a | 39 | |
2eb7011c MT |
40 | assert isset PROTOCOL |
41 | assert vlan_supported_protocol "${PROTOCOL}" | |
42 | ||
fc1e91cc MT |
43 | assert isinteger ID |
44 | assert vlan_valid_id "${ID}" | |
711ffac1 MT |
45 | } |
46 | ||
96045e9c | 47 | hook_find_port_name() { |
f24529e4 | 48 | assert isset ID |
a2f35a67 | 49 | assert isset PARENT_PORT |
96045e9c | 50 | |
f24529e4 | 51 | print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${ID}" |
96045e9c MT |
52 | } |
53 | ||
54 | hook_parse_cmdline() { | |
711ffac1 MT |
55 | while [ $# -gt 0 ]; do |
56 | case "${1}" in | |
7951525a | 57 | --address=*) |
2212045f | 58 | ADDRESS=$(cli_get_val "${1}") |
abb65554 MT |
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 | |
711ffac1 | 65 | ;; |
f24529e4 MT |
66 | --id=*) |
67 | ID=$(cli_get_val "${1}") | |
fc1e91cc MT |
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 | |
f24529e4 | 74 | ;; |
a2f35a67 MT |
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 | ;; | |
2eb7011c MT |
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 | ;; | |
a2f35a67 | 94 | *) |
0cf39f2d MT |
95 | error "Unknown argument '${1}'" |
96 | return ${EXIT_CONF_ERROR} | |
97 | ;; | |
711ffac1 MT |
98 | esac |
99 | shift | |
100 | done | |
abb65554 MT |
101 | |
102 | # Generate a random MAC address if none given | |
103 | if ! isset ADDRESS; then | |
104 | ADDRESS="$(mac_generate)" | |
105 | fi | |
711ffac1 MT |
106 | } |
107 | ||
1c6a4e30 | 108 | hook_create() { |
1ba6a2bb | 109 | local port="${1}" |
711ffac1 MT |
110 | assert isset port |
111 | ||
1ba6a2bb | 112 | device_exists "${port}" && exit ${EXIT_OK} |
7951525a | 113 | |
1ba6a2bb | 114 | # Read configruation |
47767231 MT |
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 | |
711ffac1 | 124 | |
68cacd23 MT |
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 | ||
1ba6a2bb | 131 | # Create the VLAN device |
d3a0f73d MT |
132 | if ! vlan_create "${port}" \ |
133 | --address="${ADDRESS}" \ | |
2eb7011c MT |
134 | --id="${ID}" \ |
135 | --parent="${PARENT_PORT}" \ | |
136 | --protocol="${PROTOCOL}"; then | |
d3a0f73d MT |
137 | error "Could not create port: ${port}" |
138 | return ${EXIT_ERROR} | |
139 | fi | |
7951525a | 140 | |
d3a0f73d | 141 | return ${EXIT_OK} |
711ffac1 MT |
142 | } |
143 | ||
1c6a4e30 | 144 | hook_remove() { |
1ba6a2bb | 145 | local port="${1}" |
711ffac1 MT |
146 | assert isset port |
147 | ||
1ba6a2bb MT |
148 | if device_exists "${port}"; then |
149 | vlan_remove "${port}" | |
711ffac1 MT |
150 | fi |
151 | ||
711ffac1 MT |
152 | exit ${EXIT_OK} |
153 | } |