]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/ethernet
port: ethernet: Allow setting the MTU
[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 # Default MTU
25 DEFAULT_MTU=1500
26
27 # DEVICE equals the actual MAC address of the device.
28 # If ADDRESS is set, the device will get ADDRESS set for its MAC address.
29
30 HOOK_SETTINGS="HOOK ADDRESS DEVICE MTU"
31
32 hook_check_settings() {
33 assert ismac DEVICE
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
44 hook_parse_cmdline() {
45 while [ $# -gt 0 ]; do
46 case "${1}" in
47 --address=*)
48 ADDRESS="$(cli_get_val "${1}")"
49
50 if ! mac_is_valid "${ADDRESS}"; then
51 error "Invalid MAC address: ${ADDRESS}"
52 return ${EXIT_ERROR}
53 fi
54 ;;
55
56 --mtu=*)
57 MTU="$(cli_get_val "${1}")"
58
59 if ! mtu_is_valid "ethernet" "${MTU}"; then
60 error "Invalid MTU: ${MTU}"
61 return ${EXIT_ERROR}
62 fi
63 ;;
64 *)
65 error "Unknown argument: ${1}"
66 return ${EXIT_ERROR}
67 ;;
68 esac
69 shift
70 done
71 }
72
73 hook_new() {
74 local port=${1}
75 assert isset port
76 shift
77
78 ADDRESS=""
79 DEVICE=$(device_get_address ${port})
80
81 port_settings_write "${port}" ${HOOK_SETTINGS}
82
83 exit ${EXIT_OK}
84 }
85
86 hook_up() {
87 local port="${1}"
88
89 local ${HOOK_SETTINGS}
90 if ! port_settings_read "${port}" ${HOOK_SETTINGS}; then
91 log ERROR "Could not read settings for port ${port}"
92 return ${EXIT_ERROR}
93 fi
94
95 # Set MAC address, if needed
96 if isset ADDRESS; then
97 device_set_address "${port}" "${ADDRESS}"
98 fi
99
100 # Set MTU
101 if isset MTU; then
102 device_set_mtu "${port}" "${MTU}"
103 else
104 device_set_mtu "${port}" "${DEFAULT_MTU}"
105 fi
106
107 # Bring up the device
108 device_set_up "${port}"
109
110 exit ${EXIT_OK}
111 }
112
113 hook_remove() {
114 exit ${EXIT_OK}
115 }
116
117 hook_hotplug_rename() {
118 local port=${1}
119 assert isset port
120
121 local device=${2}
122 assert isset device
123
124 # Read in the conifguration file.
125 port_settings_read "${port}" ${HOOK_SETTINGS}
126
127 # Get the current MAC address of the device.
128 local address=$(device_get_address ${device})
129 assert isset address
130
131 # Check if the address matches with the configuration.
132 if list_match "${address}" ${DEVICE} ${ADDRESS}; then
133 log DEBUG "Device '${device}' equals port '${port}'."
134
135 hook_create "${device}"
136 fi
137
138 log DEBUG "Device '${device}' does not equal port '${port}'."
139 exit ${EXIT_ERROR}
140 }