]> git.ipfire.org Git - people/ms/network.git/blob - src/header-port
d75fdd86240b7bc344202c1ab794288951c56cf9
[people/ms/network.git] / src / header-port
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 INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN"
23
24 HOOK_PORT_PATTERN="${PORT_PATTERN}"
25
26 # This function is called after a device has been plugged
27 # into the system and got its correct name.
28 # The function is intended to create child ports and things
29 # like that.
30 hook_hotplug() {
31 # If the hook does not handle the hotplug event, it
32 # must return EXIT_NOT_HANDLED.
33 exit ${EXIT_NOT_HANDLED}
34 }
35
36 # This function gets called when a device is plugged in
37 # to determine the right name.
38 # The first argument is the port which should be tested
39 # against the second argument which is the device that
40 # has been plugged in.
41 hook_hotplug_rename() {
42 exit ${EXIT_FALSE}
43 }
44
45 hook_hotplug_rename_by_address() {
46 local port="${1}"
47 assert isset port
48
49 local device="${2}"
50 assert isset device
51
52 # Read in the conifguration file.
53 if ! port_settings_read "${port}"; then
54 return ${EXIT_ERROR}
55 fi
56
57 # Get the current MAC address of the device.
58 local address="$(device_get_address "${device}")"
59 assert isset address
60
61 # Check if the address matches with the configuration.
62 if list_match "${address}" "${ADDRESS}" "${DEVICE}"; then
63 log DEBUG "Device '${device}' is port '${port}'"
64 return ${EXIT_OK}
65 fi
66
67 log DEBUG "Device '${device}' is not port '${port}'"
68 return ${EXIT_ERROR}
69 }
70
71 hook_default_new() {
72 local ${HOOK_SETTINGS[*]}
73
74 # Import all default variables
75 hook_set_defaults
76
77 if ! hook_parse_cmdline "$@"; then
78 return ${EXIT_ERROR}
79 fi
80
81 assert isset HOOK_PORT_PATTERN
82
83 local port=$(port_find_free ${HOOK_PORT_PATTERN})
84 assert isset port
85
86 port_settings_write "${port}" ${HOOK_SETTINGS[*]}
87
88 exit ${EXIT_OK}
89 }
90
91 hook_new() {
92 hook_default_new "$@"
93 }
94
95 hook_default_edit() {
96 local port=${1}
97 assert isset port
98 shift
99
100 # Read settings
101 if ! port_settings_read "${port}" ${HOOK_SETTINGS[*]}; then
102 error "Could not read settings for port ${port}"
103 return ${EXIT_ERROR}
104 fi
105
106 # Parse command line arguments
107 if ! hook_parse_cmdline "$@"; then
108 return ${EXIT_ERROR}
109 fi
110
111 # Save settings
112 if ! port_settings_write "${port}" ${HOOK_SETTINGS[*]}; then
113 error "Could not write settings for port ${port}"
114 return ${EXIT_ERROR}
115 fi
116
117 # Apply settings
118 port_restart "${port}"
119
120 return ${EXIT_OK}
121 }
122
123 hook_edit() {
124 hook_default_edit "$@"
125 }
126
127 # Returns a list of all children of this port
128 hook_children() {
129 local port="${1}"
130
131 if ! port_settings_read "${port}" ${HOOK_SETTINGS[*]}; then
132 log ERROR "Could not read port settings: ${port}"
133 return ${EXIT_OK}
134 fi
135
136 print "${SLAVES}"
137 }
138
139 hook_status() {
140 local port="${1}"
141 assert isset port
142
143 cli_device_headline "${port}" --long
144 exit ${EXIT_OK}
145 }
146
147 # Create any virtual devices, but don't bring them up
148 # Must tolerate that the device may already exist
149 hook_create() {
150 cmd_not_implemented
151 }
152
153 # Must tolerate that the device may not exist
154 hook_remove() {
155 cmd_not_implemented
156 }
157
158 # Just bring up the device
159 hook_default_up() {
160 local port="${1}"
161 assert isset port
162
163 if ! device_exists "${port}"; then
164 log ERROR "Port '${port}' does not exist and cannot be brought up"
165 exit ${EXIT_ERROR}
166 fi
167
168 # Bring up the port
169 device_set_up "${port}"
170
171 # Bring up all slaves if the port has any
172 local slave
173 for slave in $(port_get_slaves "${port}"); do
174 port_up "${slave}"
175 done
176 }
177
178 # Depends on the port existing
179 hook_up() {
180 hook_default_up "$@"
181 }
182
183 hook_default_down() {
184 local port="${1}"
185 assert isset port
186
187 if device_exists "${port}"; then
188 device_set_down "${port}"
189 fi
190
191 # Bring down all slaves if the port has any
192 local slave
193 for slave in $(port_get_slaves "${port}"); do
194 port_down "${slave}"
195 done
196 }
197
198 hook_down() {
199 hook_default_down "$@"
200 }