]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/ports/wireless-mesh
wpa_supplicant: Drop config helper
[people/stevee/network.git] / src / hooks / ports / wireless-mesh
CommitLineData
727248f0
MT
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
24HOOK_PORT_PATTERN="${PORT_PATTERN_MESH}"
25
5ffbe0d5 26HOOK_SETTINGS="HOOK ADDRESS MESH_ID CHANNEL PHY PSK"
727248f0
MT
27
28hook_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
36hook_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 ;;
5ffbe0d5
MT
51 --pre-shared-key=*)
52 PSK=$(cli_get_val "${1}")
53 ;;
727248f0
MT
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
f8b7f135
MT
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
727248f0
MT
80}
81
82hook_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
1d736e21 105 # Write WPA supplicant configuration
02807ad2 106 wpa_supplicant_config_write "${port}" \
1d736e21
MT
107 --channel="${CHANNEL}" \
108 --key="${PSK}" \
109 --mode="802.11s" \
110 --ssid="${MESH_ID}" || return $?
111
727248f0
MT
112 return ${EXIT_OK}
113}
114
115hook_remove() {
116 local port="${1}"
117 assert isset port
118
02807ad2
MT
119 # Remove WPA supplicant configuration
120 wpa_supplicant_config_destroy "${port}"
121
727248f0
MT
122 if device_exists "${port}"; then
123 wireless_remove "${port}"
124 fi
125
126 exit ${EXIT_OK}
127}
128
1d736e21
MT
129hook_up() {
130 local port="${1}"
131
132 # Start WPA supplicant
133 wpa_supplicant_start "${port}"
134}
135
136hook_down() {
137 local port="${1}"
138
139 wpa_supplicant_stop "${port}"
140}
141
727248f0
MT
142hook_hotplug() {
143 local port="${1}"
144 assert isset port
145
146 port_settings_read "${port}" ${HOOK_SETTINGS}
147
148 case "$(hotplug_action)" in
149 add)
150 # Bring up the port when the phy is plugged in
151 if hotplug_event_port_uses_phy "${port}"; then
152 hook_create "${port}" || exit $?
153 fi
154 ;;
155
156 *)
157 exit ${EXIT_NOT_HANDLED}
158 ;;
159 esac
160
161 exit ${EXIT_OK}
162}