network-hotplug: Try to use fewer checks when deleting a device
[people/ms/network.git] / src / hooks / ports / ethernet
CommitLineData
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
8c63fa13 22. /usr/lib/network/header-port
711ffac1 23
64b7bffd
MT
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
8578e61d 27HOOK_SETTINGS="ADDRESS ADVERTISED_LINK_SPEEDS DEVICE OFFLOADING MTU"
711ffac1 28
1c6a4e30 29hook_check_settings() {
64b7bffd
MT
30 assert ismac DEVICE
31
32 if isset ADDRESS; then
33 assert ismac ADDRESS
34 fi
ef53293c
MT
35
36 if isset MTU; then
37 assert mtu_is_valid "ethernet" "${MTU}"
38 fi
3a0e1dda 39
5b1fd814
MT
40 if isset MODES; then
41 local mode
42 for mode in ${MODES}; do
43 assert [ -n "${DEVICE_LINK_SPEEDS[${mode}]}" ]
44 done
3a0e1dda 45 fi
711ffac1
MT
46}
47
c1c6a024
MT
48hook_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 ;;
ef53293c 59
5b1fd814
MT
60 --advertised-link-speeds=*)
61 ADVERTISED_LINK_SPEEDS="$(cli_get_val "${1}")"
178aabc0 62
5b1fd814
MT
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
178aabc0
MT
70 ;;
71
ef53293c
MT
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 ;;
3a0e1dda 80
085a89dc
MT
81 --offloading=*)
82 OFFLOADING="$(cli_get_val "${1}")"
83
84 if enabled OFFLOADING; then
85 OFFLOADING="on"
0842edcc 86 elif disabled OFFLOADING; then
085a89dc 87 OFFLOADING="off"
0842edcc
MT
88 else
89 error "Invalid value for offloading: ${OFFLOADING}"
90 return ${EXIT_ERROR}
085a89dc
MT
91 fi
92 ;;
93
c1c6a024
MT
94 *)
95 error "Unknown argument: ${1}"
96 return ${EXIT_ERROR}
97 ;;
98 esac
99 shift
100 done
101}
102
39aae674
MT
103# This function is only called automatically by hotplug to create
104# a new ethernet port.
105hook_new() {
106 local port="${1}"
107 assert isset port
108
ec87f5ce
MT
109 local device="${2}"
110 assert isset device
111
112 local DEVICE="$(device_get_address "${device}")"
39aae674
MT
113
114 if ! port_settings_write "${port}" ${HOOK_SETTINGS}; then
115 log ERROR "Could not write settings for port ${port}"
116 return ${EXIT_ERROR}
117 fi
118
119 return ${EXIT_OK}
120}
121
3d007f4c
MT
122hook_create() {
123 return ${EXIT_OK}
124}
125
ef53293c 126hook_up() {
79271f35
MT
127 local port="${1}"
128
ef53293c
MT
129 local ${HOOK_SETTINGS}
130 if ! port_settings_read "${port}" ${HOOK_SETTINGS}; then
131 log ERROR "Could not read settings for port ${port}"
132 return ${EXIT_ERROR}
133 fi
134
79271f35
MT
135 # Set MAC address, if needed
136 if isset ADDRESS; then
137 device_set_address "${port}" "${ADDRESS}"
138 fi
139
ef53293c
MT
140 # Set MTU
141 if isset MTU; then
142 device_set_mtu "${port}" "${MTU}"
143 else
144 device_set_mtu "${port}" "${DEFAULT_MTU}"
145 fi
146
5b1fd814
MT
147 # Set link speeds
148 if isset ADVERTISED_LINK_SPEEDS; then
149 device_advertise_link_speeds "${port}" ${ADVERTISED_LINK_SPEEDS}
3a0e1dda
MT
150 fi
151
085a89dc
MT
152 # Auto-enable or disable hardware offloading
153 if ! isset OFFLOADING || enabled OFFLOADING; then
154 offloading_auto "${port}"
155 else
156 offloading_disable_all "${port}"
157 fi
c3db1412 158
ef53293c
MT
159 # Bring up the device
160 device_set_up "${port}"
161
711ffac1
MT
162 exit ${EXIT_OK}
163}
164
1c6a4e30 165hook_remove() {
711ffac1
MT
166 exit ${EXIT_OK}
167}
168
1c6a4e30 169hook_hotplug_rename() {
8895cf8f 170 local port=${1}
8895cf8f 171 assert isset port
64b7bffd
MT
172
173 local device=${2}
8895cf8f
MT
174 assert isset device
175
64b7bffd 176 # Read in the conifguration file.
e9df08ad 177 port_settings_read "${port}" ${HOOK_SETTINGS}
64b7bffd
MT
178
179 # Get the current MAC address of the device.
180 local address=$(device_get_address ${device})
181 assert isset address
8895cf8f 182
64b7bffd
MT
183 # Check if the address matches with the configuration.
184 if list_match "${address}" ${DEVICE} ${ADDRESS}; then
8c63fa13 185 log DEBUG "Device '${device}' equals port '${port}'."
2f7adf2c 186 exit ${EXIT_OK}
8895cf8f
MT
187 fi
188
8c63fa13 189 log DEBUG "Device '${device}' does not equal port '${port}'."
8895cf8f
MT
190 exit ${EXIT_ERROR}
191}