]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/wireless-mesh
802.11s: Write WPA supplicant configuration
[people/stevee/network.git] / src / hooks / ports / wireless-mesh
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 Michael Tremer #
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_PORT_PATTERN="${PORT_PATTERN_MESH}"
25
26 HOOK_SETTINGS="HOOK ADDRESS MESH_ID CHANNEL PHY PSK"
27
28 hook_check_settings() {
29 assert ismac ADDRESS
30 assert isset MESH_ID
31 assert isset CHANNEL
32 assert isset PHY
33 assert ismac PHY
34 }
35
36 hook_parse_cmdline() {
37 while [ $# -gt 0 ]; do
38 case "${1}" in
39 --address=*)
40 ADDRESS=$(cli_get_val "${1}")
41 ;;
42 --channel=*)
43 CHANNEL=$(cli_get_val "${1}")
44 ;;
45 --mesh-id=*)
46 MESH_ID=$(cli_get_val "${1}")
47 ;;
48 --phy=*)
49 PHY=$(cli_get_val "${1}")
50 ;;
51 --pre-shared-key=*)
52 PSK=$(cli_get_val "${1}")
53 ;;
54 *)
55 warning "Ignoring unknown argument '${1}'"
56 ;;
57 esac
58 shift
59 done
60
61 if ! isset MESH_ID; then
62 error "Please pass a mesh ID"
63 return ${EXIT_ERROR}
64 fi
65
66 # Save address of phy do identify it again
67 PHY=$(phy_get ${PHY})
68 PHY=$(phy_get_address ${PHY})
69
70 # Generate a MAC address if none given
71 if ! isset ADDRESS; then
72 ADDRESS=$(mac_generate)
73 fi
74
75 # Check if channel is valid
76 if ! wireless_channel_is_valid "${CHANNEL}"; then
77 log ERROR "Channel is invalid: ${CHANNEL}"
78 return ${EXIT_ERROR}
79 fi
80 }
81
82 hook_create() {
83 local port="${1}"
84 assert isset port
85
86 # Read settings
87 port_settings_read "${port}" ${HOOK_SETTINGS}
88
89 # Check if the PHY is present.
90 local phy="$(phy_get "${PHY}")"
91 if ! isset phy; then
92 log DEBUG "phy '${PHY}' is not present"
93 return ${EXIT_ERROR}
94 fi
95
96 # Create the wireless device, if it does not exist, yet.
97 if ! device_exists "${port}"; then
98 wireless_create "${port}" \
99 --address="${ADDRESS}" \
100 --channel="${CHANNEL}" \
101 --phy="${phy}" \
102 --type="mesh-point" || return $?
103 fi
104
105 # Write WPA supplicant configuration
106 local config_file="$(wpa_supplicant_config_dir "${port}")/wpa_supplicant.conf"
107
108 wpa_supplicant_config_write \
109 "${port}" \
110 "${config_file}" \
111 --channel="${CHANNEL}" \
112 --key="${PSK}" \
113 --mode="802.11s" \
114 --ssid="${MESH_ID}" || return $?
115
116 return ${EXIT_OK}
117 }
118
119 hook_remove() {
120 local port="${1}"
121 assert isset port
122
123 if device_exists "${port}"; then
124 wireless_remove "${port}"
125 fi
126
127 exit ${EXIT_OK}
128 }
129
130 hook_up() {
131 local port="${1}"
132
133 # Start WPA supplicant
134 wpa_supplicant_start "${port}"
135 }
136
137 hook_down() {
138 local port="${1}"
139
140 wpa_supplicant_stop "${port}"
141 }
142
143 hook_hotplug() {
144 local port="${1}"
145 assert isset port
146
147 port_settings_read "${port}" ${HOOK_SETTINGS}
148
149 case "$(hotplug_action)" in
150 add)
151 # Bring up the port when the phy is plugged in
152 if hotplug_event_port_uses_phy "${port}"; then
153 hook_create "${port}" || exit $?
154 fi
155 ;;
156
157 *)
158 exit ${EXIT_NOT_HANDLED}
159 ;;
160 esac
161
162 exit ${EXIT_OK}
163 }