blob: 135a5324abd46a2ef250bc91279c7b7d45dd2872 [file] [log] [blame]
Max9a852f22019-01-31 15:58:57 +01001/* Simple Osmocom System Monitor (osysmon): Support for OpenVPN monitoring */
2
3/* (C) 2019 by sysmocom - s.f.m.c. GmbH.
4 * Author: Max Suraev
5 * All Rights Reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 */
24
25#include <string.h>
26#include <stdbool.h>
27#include <ctype.h>
28#include <errno.h>
29
30#include <osmocom/core/utils.h>
31#include <osmocom/vty/vty.h>
32#include <osmocom/vty/command.h>
33#include <osmocom/core/msgb.h>
34#include <osmocom/netif/stream.h>
35
36#include "osysmon.h"
37#include "client.h"
38#include "value_node.h"
39
40/***********************************************************************
41 * Data model
42 ***********************************************************************/
43
44#define OVPN_LOG(ctx, vpn, fmt, args...) \
45 fprintf(stderr, "OpenVPN [%s]: " fmt, make_authority(ctx, vpn->cfg), ##args)
46
47/* max number of csv in response */
48#define MAX_RESP_COMPONENTS 6
49
50/* a single OpenVPN management interface client */
51struct openvpn_client {
52 /* links to osysmon.openvpn_clients */
53 struct llist_head list;
54 struct host_cfg *cfg;
55 struct osmo_stream_cli *mgmt;
56 /* fields below are parsed from response to 'state' command on mgmt interface */
57 struct host_cfg *rem_cfg;
58 char *tun_ip;
59 bool connected;
60};
61
62static char *parse_state(struct msgb *msg, struct openvpn_client *vpn)
63{
64 char tmp[128];
65 char *tok;
66 unsigned int i = 0;
67 uint8_t *m = msgb_data(msg);
68
69 if (msgb_length(msg) > 128)
70 OVPN_LOG(msg, vpn, "received message too long (%d > %u), truncating...\n", msgb_length(msg), 128);
71
72 if (msgb_length(msg) > 0) {
73 if (!isdigit(m[0])) /* skip OpenVPN greetings and alike */
74 return NULL;
75 } else {
76 OVPN_LOG(msg, vpn, "received message is empty, ignoring...\n");
77 return NULL;
78 }
79
80 OSMO_STRLCPY_ARRAY(tmp, (char *)m);
81
82 for (tok = strtok(tmp, ","); tok && i < MAX_RESP_COMPONENTS; tok = strtok(NULL, ",")) {
83 /* The string format is documented in https://openvpn.net/community-resources/management-interface/ */
84 if (tok) { /* Parse csv string and pick interesting tokens while ignoring the rest. */
85 switch (i++) {
86 case 1:
87 update_name(vpn->rem_cfg, tok);
88 break;
89 case 3:
90 osmo_talloc_replace_string(vpn->rem_cfg, &vpn->tun_ip, tok);
91 break;
92 case 4:
93 update_host(vpn->rem_cfg, tok);
94 break;
95 case 5:
96 vpn->rem_cfg->remote_port = atoi(tok);
97 break;
98 }
99 }
100 }
101 return NULL;
102}
103
104static struct openvpn_client *openvpn_client_find_or_make(const struct osysmon_state *os,
105 const char *host, uint16_t port)
106{
107 struct openvpn_client *vpn;
108 llist_for_each_entry(vpn, &os->openvpn_clients, list) {
109 if (match_config(vpn->cfg, host, MATCH_HOST) && vpn->cfg->remote_port == port)
110 return vpn;
111 }
112
113 return NULL;
114}
115
116static int connect_cb(struct osmo_stream_cli *conn)
117{
118 struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
119
120 update_name(vpn->rem_cfg, "management interface incompatible");
121 vpn->connected = true; /* FIXME: there's no callback for lost connection to drop this flag */
122
123 return 0;
124}
125
126static int read_cb(struct osmo_stream_cli *conn)
127{
128 int bytes;
129 struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
130 struct msgb *msg = msgb_alloc(1024, "OpenVPN");
131 if (!msg) {
132 OVPN_LOG(conn, vpn, "unable to allocate message in callback\n");
133 return 0;
134 }
135
136 bytes = osmo_stream_cli_recv(conn, msg);
137 if (bytes < 0)
138 OVPN_LOG(msg, vpn, "unable to receive message in callback\n");
139 else
140 parse_state(msg, vpn);
141
142 msgb_free(msg);
143
144 return 0;
145}
146
147static bool openvpn_client_create(struct osysmon_state *os, const char *name, const char *host, uint16_t port)
148{
149 struct openvpn_client *vpn = openvpn_client_find_or_make(os, host, port);
150 if (vpn)
151 return true;
152
153 vpn = talloc_zero(os, struct openvpn_client);
154 if (!vpn)
155 return false;
156
157 vpn->connected = false;
158
159 vpn->cfg = host_cfg_alloc(vpn, name, host, port);
160 if (!vpn->cfg)
161 goto dealloc;
162
163 vpn->rem_cfg = host_cfg_alloc(vpn, "management interface unavailable", NULL, 0);
164 if (!vpn->rem_cfg)
165 goto dealloc;
166
167 vpn->mgmt = make_tcp_client(vpn->cfg);
168 if (!vpn->mgmt) {
169 OVPN_LOG(vpn->rem_cfg, vpn, "failed to create TCP client\n");
170 goto dealloc;
171 }
172
173 /* Wait for 1 minute before attempting to reconnect to management interface */
174 osmo_stream_cli_set_reconnect_timeout(vpn->mgmt, 60);
175 osmo_stream_cli_set_read_cb(vpn->mgmt, read_cb);
176 osmo_stream_cli_set_connect_cb(vpn->mgmt, connect_cb);
177
178 if (osmo_stream_cli_open(vpn->mgmt) < 0) {
179 OVPN_LOG(vpn->rem_cfg, vpn, "failed to connect to management interface\n");
180 goto dealloc;
181 }
182
183 osmo_stream_cli_set_data(vpn->mgmt, vpn);
184 llist_add_tail(&vpn->list, &os->openvpn_clients);
185
186 return true;
187
188dealloc:
189 talloc_free(vpn);
190 return false;
191}
192
193static void openvpn_client_destroy(struct openvpn_client *vpn)
194{
195 if (!vpn)
196 return;
197
198 osmo_stream_cli_destroy(vpn->mgmt);
199 llist_del(&vpn->list);
200 talloc_free(vpn);
201}
202
203
204/***********************************************************************
205 * VTY
206 ***********************************************************************/
207
208#define OPENVPN_STR "Configure OpenVPN management interface address\n"
209
210DEFUN(cfg_openvpn, cfg_openvpn_cmd,
211 "openvpn HOST <1-65535>",
212 OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n")
213{
214 uint16_t port = atoi(argv[1]);
215
216 if (!openvpn_client_create(g_oss, "OpenVPN", argv[0], port)) {
217 vty_out(vty, "Failed to create OpenVPN client for %s:%u%s", argv[0], port, VTY_NEWLINE);
218 return CMD_WARNING;
219 }
220
221 return CMD_SUCCESS;
222}
223
224DEFUN(cfg_no_openvpn, cfg_no_openvpn_cmd,
225 "no openvpn HOST <1-65535>",
226 NO_STR OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n")
227{
228 uint16_t port = atoi(argv[1]);
229 struct openvpn_client *vpn = openvpn_client_find_or_make(g_oss, argv[0], port);
230 if (!vpn) {
231 vty_out(vty, "OpenVPN client %s:%u doesn't exist%s", argv[0], port, VTY_NEWLINE);
232 return CMD_WARNING;
233 }
234
235 openvpn_client_destroy(vpn);
236
237 return CMD_SUCCESS;
238}
239
240
241/***********************************************************************
242 * Runtime Code
243 ***********************************************************************/
244
245static int openvpn_client_poll(struct openvpn_client *vpn, struct value_node *parent)
246{
247 char *remote = make_authority(parent, vpn->rem_cfg);
248 struct value_node *vn_host = value_node_find_or_add(parent, make_authority(parent, vpn->cfg));
249 struct msgb *msg = msgb_alloc(128, "state");
250 if (!msg) {
251 value_node_add(vn_host, "msgb", "memory allocation failure");
252 return 0;
253 }
254
255 if (vpn->rem_cfg->name)
256 value_node_add(vn_host, "status", vpn->rem_cfg->name);
257
258 if (vpn->tun_ip)
259 value_node_add(vn_host, "tunnel", vpn->tun_ip);
260
261 if (remote)
262 value_node_add(vn_host, "remote", remote);
263
264 /* FIXME: there's no way to check client state so this might be triggered even while it's reconnecting */
265 if (vpn->connected) { /* re-trigger state command */
266 msgb_printf(msg, "state\n");
267 osmo_stream_cli_send(vpn->mgmt, msg);
268 }
269
270 return 0;
271}
272
273/* called once on startup before config file parsing */
274int osysmon_openvpn_init()
275{
276 install_element(CONFIG_NODE, &cfg_openvpn_cmd);
277 install_element(CONFIG_NODE, &cfg_no_openvpn_cmd);
278
279 return 0;
280}
281
282/* called periodically */
283int osysmon_openvpn_poll(struct value_node *parent)
284{
285 int num_vpns = llist_count(&g_oss->openvpn_clients);
286 if (num_vpns) {
287 struct value_node *vn_vpn = value_node_add(parent, "OpenVPN", NULL);
288 struct openvpn_client *vpn;
289 llist_for_each_entry(vpn, &g_oss->openvpn_clients, list)
290 openvpn_client_poll(vpn, vn_vpn);
291 }
292
293 return num_vpns;
294}