]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/ethernet
hooks: Import zone default settings, too
[people/ms/network.git] / src / hooks / ports / ethernet
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 # DEVICE equals the actual MAC address of the device.
25 # If ADDRESS is set, the device will get ADDRESS set for its MAC address.
26
27 HOOK_SETTINGS="ADDRESS ADVERTISED_LINK_SPEEDS DEVICE OFFLOADING MTU"
28
29 hook_check_settings() {
30 assert ismac DEVICE
31
32 # Invalid MAC addresses are not allowed
33 assert not isoneof DEVICE 00:00:00:00:00:00 ff:ff:ff:ff:ff:ff
34
35 if isset ADDRESS; then
36 assert ismac ADDRESS
37 fi
38
39 if isset MTU; then
40 assert mtu_is_valid "ethernet" "${MTU}"
41 fi
42
43 if isset MODES; then
44 local mode
45 for mode in ${MODES}; do
46 assert [ -n "${DEVICE_LINK_SPEEDS[${mode}]}" ]
47 done
48 fi
49 }
50
51 hook_parse_cmdline() {
52 while [ $# -gt 0 ]; do
53 case "${1}" in
54 --address=*)
55 ADDRESS="$(cli_get_val "${1}")"
56
57 if ! mac_is_valid "${ADDRESS}"; then
58 error "Invalid MAC address: ${ADDRESS}"
59 return ${EXIT_ERROR}
60 fi
61 ;;
62
63 --advertised-link-speeds=*)
64 ADVERTISED_LINK_SPEEDS="$(cli_get_val "${1}")"
65
66 local speed
67 for speed in ${ADVERTISED_LINK_SPEEDS}; do
68 if [ -z "${DEVICE_LINK_SPEEDS[${speed}]}" ]; then
69 error "Unsupported link speed: ${speed}"
70 return ${EXIT_ERROR}
71 fi
72 done
73 ;;
74
75 --mtu=*)
76 MTU="$(cli_get_val "${1}")"
77
78 if ! mtu_is_valid "ethernet" "${MTU}"; then
79 error "Invalid MTU: ${MTU}"
80 return ${EXIT_ERROR}
81 fi
82 ;;
83
84 --offloading=*)
85 OFFLOADING="$(cli_get_val "${1}")"
86
87 if enabled OFFLOADING; then
88 OFFLOADING="on"
89 elif disabled OFFLOADING; then
90 OFFLOADING="off"
91 else
92 error "Invalid value for offloading: ${OFFLOADING}"
93 return ${EXIT_ERROR}
94 fi
95 ;;
96
97 *)
98 error "Unknown argument: ${1}"
99 return ${EXIT_ERROR}
100 ;;
101 esac
102 shift
103 done
104 }
105
106 # This function is only called automatically by hotplug to create
107 # a new ethernet port.
108 hook_new() {
109 local port="${1}"
110 assert isset port
111
112 local device="${2}"
113 assert isset device
114
115 local DEVICE="$(device_get_address "${device}")"
116
117 if ! port_settings_write "${port}" ${HOOK_SETTINGS}; then
118 log ERROR "Could not write settings for port ${port}"
119 return ${EXIT_ERROR}
120 fi
121
122 return ${EXIT_OK}
123 }
124
125 hook_create() {
126 return ${EXIT_OK}
127 }
128
129 hook_up() {
130 local port="${1}"
131
132 local ${HOOK_SETTINGS}
133 if ! port_settings_read "${port}" ${HOOK_SETTINGS}; then
134 log ERROR "Could not read settings for port ${port}"
135 return ${EXIT_ERROR}
136 fi
137
138 # Set MAC address, if needed
139 if isset ADDRESS; then
140 device_set_address "${port}" "${ADDRESS}"
141 fi
142
143 # Set MTU
144 if isset MTU; then
145 device_set_mtu "${port}" "${MTU}"
146 else
147 device_set_mtu "${port}" "${DEFAULT_MTU}"
148 fi
149
150 # Set link speeds
151 if isset ADVERTISED_LINK_SPEEDS; then
152 device_advertise_link_speeds "${port}" ${ADVERTISED_LINK_SPEEDS}
153 fi
154
155 # Auto-enable or disable hardware offloading
156 if ! isset OFFLOADING || enabled OFFLOADING; then
157 offloading_auto "${port}"
158 else
159 offloading_disable_all "${port}"
160 fi
161
162 # Bring up the device
163 device_set_up "${port}"
164
165 exit ${EXIT_OK}
166 }
167
168 hook_remove() {
169 exit ${EXIT_OK}
170 }
171
172 hook_hotplug_rename() {
173 local port=${1}
174 assert isset port
175
176 local device=${2}
177 assert isset device
178
179 # Read in the conifguration file.
180 port_settings_read "${port}" ${HOOK_SETTINGS}
181
182 # Get the current MAC address of the device.
183 local address=$(device_get_address ${device})
184 assert isset address
185
186 # Check if the address matches with the configuration.
187 if list_match "${address}" ${DEVICE} ${ADDRESS}; then
188 log DEBUG "Device '${device}' equals port '${port}'."
189 exit ${EXIT_OK}
190 fi
191
192 log DEBUG "Device '${device}' does not equal port '${port}'."
193 exit ${EXIT_ERROR}
194 }