]>
Commit | Line | Data |
---|---|---|
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 | # Selectable speeds in MBit/s | |
28 | VALID_SPEEDS="10000 1000 100 10" | |
29 | ||
30 | # We can run in full or half duplex | |
31 | VALID_DUPLEXES="full half" | |
32 | ||
33 | # DEVICE equals the actual MAC address of the device. | |
34 | # If ADDRESS is set, the device will get ADDRESS set for its MAC address. | |
35 | ||
36 | HOOK_SETTINGS="HOOK ADDRESS DEVICE DUPLEX MTU SPEED" | |
37 | ||
38 | hook_check_settings() { | |
39 | assert ismac DEVICE | |
40 | ||
41 | if isset ADDRESS; then | |
42 | assert ismac ADDRESS | |
43 | fi | |
44 | ||
45 | if isset MTU; then | |
46 | assert mtu_is_valid "ethernet" "${MTU}" | |
47 | fi | |
48 | ||
49 | if isset DUPLEX; then | |
50 | assert isoneof DUPLEX ${VALID_DUPLEXES} | |
51 | fi | |
52 | ||
53 | if isset SPEED; then | |
54 | assert isoneof SPEED ${VALID_SPEEDS} | |
55 | fi | |
56 | } | |
57 | ||
58 | hook_parse_cmdline() { | |
59 | while [ $# -gt 0 ]; do | |
60 | case "${1}" in | |
61 | --address=*) | |
62 | ADDRESS="$(cli_get_val "${1}")" | |
63 | ||
64 | if ! mac_is_valid "${ADDRESS}"; then | |
65 | error "Invalid MAC address: ${ADDRESS}" | |
66 | return ${EXIT_ERROR} | |
67 | fi | |
68 | ;; | |
69 | ||
70 | --duplex=*) | |
71 | DUPLEX="$(cli_get_val "${1}")" | |
72 | ||
73 | if ! isoneof DUPLEX ${VALID_DUPLEXES}; then | |
74 | error "Invalid duplex mode: ${DUPLEX}" | |
75 | return ${EXIT_ERROR} | |
76 | fi | |
77 | ;; | |
78 | ||
79 | --mtu=*) | |
80 | MTU="$(cli_get_val "${1}")" | |
81 | ||
82 | if ! mtu_is_valid "ethernet" "${MTU}"; then | |
83 | error "Invalid MTU: ${MTU}" | |
84 | return ${EXIT_ERROR} | |
85 | fi | |
86 | ;; | |
87 | ||
88 | --speed=*) | |
89 | SPEED="$(cli_get_val "${1}")" | |
90 | ||
91 | if ! isoneof SPEED ${VALID_SPEEDS}; then | |
92 | error "Invalid speed: ${SPEED}" | |
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 | hook_create() { | |
107 | return ${EXIT_OK} | |
108 | } | |
109 | ||
110 | hook_up() { | |
111 | local port="${1}" | |
112 | ||
113 | local ${HOOK_SETTINGS} | |
114 | if ! port_settings_read "${port}" ${HOOK_SETTINGS}; then | |
115 | log ERROR "Could not read settings for port ${port}" | |
116 | return ${EXIT_ERROR} | |
117 | fi | |
118 | ||
119 | # Set MAC address, if needed | |
120 | if isset ADDRESS; then | |
121 | device_set_address "${port}" "${ADDRESS}" | |
122 | fi | |
123 | ||
124 | # Set MTU | |
125 | if isset MTU; then | |
126 | device_set_mtu "${port}" "${MTU}" | |
127 | else | |
128 | device_set_mtu "${port}" "${DEFAULT_MTU}" | |
129 | fi | |
130 | ||
131 | # Set duplex mode | |
132 | if isset DUPLEX; then | |
133 | device_set_duplex "${port}" "${DUPLEX}" | |
134 | fi | |
135 | ||
136 | # Set speed | |
137 | if isset SPEED; then | |
138 | device_set_speed "${port}" "${SPEED}" | |
139 | fi | |
140 | ||
141 | # Bring up the device | |
142 | device_set_up "${port}" | |
143 | ||
144 | exit ${EXIT_OK} | |
145 | } | |
146 | ||
147 | hook_remove() { | |
148 | exit ${EXIT_OK} | |
149 | } | |
150 | ||
151 | hook_hotplug_rename() { | |
152 | local port=${1} | |
153 | assert isset port | |
154 | ||
155 | local device=${2} | |
156 | assert isset device | |
157 | ||
158 | # Read in the conifguration file. | |
159 | port_settings_read "${port}" ${HOOK_SETTINGS} | |
160 | ||
161 | # Get the current MAC address of the device. | |
162 | local address=$(device_get_address ${device}) | |
163 | assert isset address | |
164 | ||
165 | # Check if the address matches with the configuration. | |
166 | if list_match "${address}" ${DEVICE} ${ADDRESS}; then | |
167 | log DEBUG "Device '${device}' equals port '${port}'." | |
168 | exit ${EXIT_OK} | |
169 | fi | |
170 | ||
171 | log DEBUG "Device '${device}' does not equal port '${port}'." | |
172 | exit ${EXIT_ERROR} | |
173 | } |