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