]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/dummy
1c4b3c9fdd21260472f7c68b043645b9ea094532
[people/ms/network.git] / src / hooks / ports / dummy
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 HOOK_SETTINGS=(
25 "ADDRESS"
26 )
27
28 hook_check_settings() {
29 assert ismac ADDRESS
30 }
31
32 hook_parse_cmdline() {
33 while [ $# -gt 0 ]; do
34 case "${1}" in
35 --address=*)
36 ADDRESS=$(cli_get_val "${1}")
37 ;;
38 *)
39 warning "Unknown argument '${1}'"
40 ;;
41 esac
42 shift
43 done
44
45 if isset ADDRESS; then
46 if ! ismac ADDRESS; then
47 error "Invalid MAC address given: ${ADDRESS}"
48 return ${EXIT_ERROR}
49 fi
50
51 # Generate a random but static MAC address if none was set
52 else
53 ADDRESS=$(mac_generate)
54 fi
55 }
56
57 hook_new() {
58 if ! hook_parse_cmdline "$@"; then
59 return ${EXIT_ERROR}
60 fi
61
62 local port=$(port_find_free ${DUMMY_PORT_PATTERN})
63 assert isset port
64
65 if port_settings_write "${port}" ${HOOK_SETTINGS[*]}; then
66 log INFO "New dummy port '${port}' has been created"
67 fi
68
69 exit ${EXIT_OK}
70 }
71
72 hook_create() {
73 local port="${1}"
74 assert isset port
75
76 # Read configuration
77 port_settings_read "${port}" ${HOOK_SETTINGS[*]}
78
79 # Create the dummy device
80 dummy_create "${port}" "${ADDRESS}"
81
82 exit ${EXIT_OK}
83 }
84
85 hook_remove() {
86 local port="${1}"
87 assert isset port
88
89 # Remove the dummy device
90 dummy_remove "${port}"
91 }
92
93 hook_up() {
94 local port="${1}"
95 assert isset port
96
97 # Bring up the port.
98 device_set_up ${port}
99
100 exit ${EXIT_OK}
101 }
102
103 hook_down() {
104 local port="${1}"
105 assert isset port
106
107 # Tear down the port.
108 device_set_down ${port}
109
110 exit ${EXIT_OK}
111 }
112
113 hook_hotplug_rename() {
114 local port=${1}
115 assert isset port
116
117 local device=${2}
118 assert isset device
119
120 port_settings_read "${port}" ${HOOK_SETTINGS[*]}
121
122 if [ "${ADDRESS}" = "$(device_get_address ${device})" ]; then
123 log DEBUG "Device '${device}' equals port '${port}'."
124 exit ${EXIT_OK}
125 fi
126
127 log DEBUG "Device '${device}' does not equal port '${port}'."
128 exit ${EXIT_ERROR}
129 }