hotplug: Remove multiple copies of the same function
[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
d389e96b
MT
24HOOK_SETTINGS=(
25 "ADDRESS"
26 "ADVERTISED_LINK_SPEEDS"
27 "DEVICE"
28 "OFFLOADING"
29 "MTU"
30)
711ffac1 31
1c6a4e30 32hook_check_settings() {
64b7bffd
MT
33 assert ismac DEVICE
34
5e4629f6
MT
35 # Invalid MAC addresses are not allowed
36 assert not isoneof DEVICE 00:00:00:00:00:00 ff:ff:ff:ff:ff:ff
37
64b7bffd
MT
38 if isset ADDRESS; then
39 assert ismac ADDRESS
40 fi
ef53293c
MT
41
42 if isset MTU; then
43 assert mtu_is_valid "ethernet" "${MTU}"
44 fi
3a0e1dda 45
5b1fd814
MT
46 if isset MODES; then
47 local mode
48 for mode in ${MODES}; do
49 assert [ -n "${DEVICE_LINK_SPEEDS[${mode}]}" ]
50 done
3a0e1dda 51 fi
711ffac1
MT
52}
53
c1c6a024
MT
54hook_parse_cmdline() {
55 while [ $# -gt 0 ]; do
56 case "${1}" in
57 --address=*)
58 ADDRESS="$(cli_get_val "${1}")"
59
60 if ! mac_is_valid "${ADDRESS}"; then
61 error "Invalid MAC address: ${ADDRESS}"
62 return ${EXIT_ERROR}
63 fi
64 ;;
ef53293c 65
5b1fd814
MT
66 --advertised-link-speeds=*)
67 ADVERTISED_LINK_SPEEDS="$(cli_get_val "${1}")"
178aabc0 68
5b1fd814
MT
69 local speed
70 for speed in ${ADVERTISED_LINK_SPEEDS}; do
71 if [ -z "${DEVICE_LINK_SPEEDS[${speed}]}" ]; then
72 error "Unsupported link speed: ${speed}"
73 return ${EXIT_ERROR}
74 fi
75 done
178aabc0
MT
76 ;;
77
ef53293c
MT
78 --mtu=*)
79 MTU="$(cli_get_val "${1}")"
80
81 if ! mtu_is_valid "ethernet" "${MTU}"; then
82 error "Invalid MTU: ${MTU}"
83 return ${EXIT_ERROR}
84 fi
85 ;;
3a0e1dda 86
085a89dc
MT
87 --offloading=*)
88 OFFLOADING="$(cli_get_val "${1}")"
89
90 if enabled OFFLOADING; then
91 OFFLOADING="on"
0842edcc 92 elif disabled OFFLOADING; then
085a89dc 93 OFFLOADING="off"
0842edcc
MT
94 else
95 error "Invalid value for offloading: ${OFFLOADING}"
96 return ${EXIT_ERROR}
085a89dc
MT
97 fi
98 ;;
99
c1c6a024
MT
100 *)
101 error "Unknown argument: ${1}"
102 return ${EXIT_ERROR}
103 ;;
104 esac
105 shift
106 done
107}
108
39aae674
MT
109# This function is only called automatically by hotplug to create
110# a new ethernet port.
111hook_new() {
112 local port="${1}"
113 assert isset port
114
ec87f5ce
MT
115 local device="${2}"
116 assert isset device
117
118 local DEVICE="$(device_get_address "${device}")"
39aae674 119
eba9fa9c 120 if ! port_settings_write "${port}"; then
39aae674
MT
121 log ERROR "Could not write settings for port ${port}"
122 return ${EXIT_ERROR}
123 fi
124
125 return ${EXIT_OK}
126}
127
3d007f4c
MT
128hook_create() {
129 return ${EXIT_OK}
130}
131
ef53293c 132hook_up() {
79271f35
MT
133 local port="${1}"
134
d389e96b 135 local ${HOOK_SETTINGS[*]}
eba9fa9c 136 if ! port_settings_read "${port}"; then
ef53293c
MT
137 log ERROR "Could not read settings for port ${port}"
138 return ${EXIT_ERROR}
139 fi
140
79271f35
MT
141 # Set MAC address, if needed
142 if isset ADDRESS; then
143 device_set_address "${port}" "${ADDRESS}"
144 fi
145
ef53293c
MT
146 # Set MTU
147 if isset MTU; then
148 device_set_mtu "${port}" "${MTU}"
149 else
150 device_set_mtu "${port}" "${DEFAULT_MTU}"
151 fi
152
5b1fd814
MT
153 # Set link speeds
154 if isset ADVERTISED_LINK_SPEEDS; then
155 device_advertise_link_speeds "${port}" ${ADVERTISED_LINK_SPEEDS}
3a0e1dda
MT
156 fi
157
085a89dc
MT
158 # Auto-enable or disable hardware offloading
159 if ! isset OFFLOADING || enabled OFFLOADING; then
160 offloading_auto "${port}"
161 else
162 offloading_disable_all "${port}"
163 fi
c3db1412 164
ef53293c
MT
165 # Bring up the device
166 device_set_up "${port}"
167
711ffac1
MT
168 exit ${EXIT_OK}
169}
170
1c6a4e30 171hook_remove() {
711ffac1
MT
172 exit ${EXIT_OK}
173}
174
1c6a4e30 175hook_hotplug_rename() {
12f9c8d2 176 hook_hotplug_rename_by_address "$@"
8895cf8f 177}