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