header-port: Print errors if config could not be read/written
[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
27HOOK_SETTINGS="HOOK ADDRESS DEVICE"
711ffac1 28
1c6a4e30 29hook_check_settings() {
64b7bffd
MT
30 assert ismac DEVICE
31
32 if isset ADDRESS; then
33 assert ismac ADDRESS
34 fi
711ffac1
MT
35}
36
c1c6a024
MT
37hook_parse_cmdline() {
38 while [ $# -gt 0 ]; do
39 case "${1}" in
40 --address=*)
41 ADDRESS="$(cli_get_val "${1}")"
42
43 if ! mac_is_valid "${ADDRESS}"; then
44 error "Invalid MAC address: ${ADDRESS}"
45 return ${EXIT_ERROR}
46 fi
47 ;;
48 *)
49 error "Unknown argument: ${1}"
50 return ${EXIT_ERROR}
51 ;;
52 esac
53 shift
54 done
55}
56
1c6a4e30 57hook_new() {
711ffac1 58 local port=${1}
711ffac1 59 assert isset port
64b7bffd 60 shift
711ffac1 61
64b7bffd
MT
62 ADDRESS=""
63 DEVICE=$(device_get_address ${port})
711ffac1 64
e9df08ad 65 port_settings_write "${port}" ${HOOK_SETTINGS}
711ffac1
MT
66
67 exit ${EXIT_OK}
68}
69
1c6a4e30 70hook_create() {
1ba6a2bb
MT
71 # Nothing to do here. Real ethernet devices cannot
72 # be created. They are just there.
711ffac1
MT
73 exit ${EXIT_OK}
74}
75
1c6a4e30 76hook_remove() {
711ffac1
MT
77 exit ${EXIT_OK}
78}
79
1c6a4e30 80hook_hotplug_rename() {
8895cf8f 81 local port=${1}
8895cf8f 82 assert isset port
64b7bffd
MT
83
84 local device=${2}
8895cf8f
MT
85 assert isset device
86
64b7bffd 87 # Read in the conifguration file.
e9df08ad 88 port_settings_read "${port}" ${HOOK_SETTINGS}
64b7bffd
MT
89
90 # Get the current MAC address of the device.
91 local address=$(device_get_address ${device})
92 assert isset address
8895cf8f 93
64b7bffd
MT
94 # Check if the address matches with the configuration.
95 if list_match "${address}" ${DEVICE} ${ADDRESS}; then
8c63fa13 96 log DEBUG "Device '${device}' equals port '${port}'."
64b7bffd
MT
97
98 # Change the MAC address, if needed.
99 if isset ADDRESS; then
100 device_set_address "${device}" "${ADDRESS}"
101 fi
102
103 # Everything is done.
8895cf8f
MT
104 exit ${EXIT_OK}
105 fi
106
8c63fa13 107 log DEBUG "Device '${device}' does not equal port '${port}'."
8895cf8f
MT
108 exit ${EXIT_ERROR}
109}