blob: 672fd5128b42c57b6395e29aa34845d7dc2fbda3 [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{
Pau Espin Pedrola3a92ec2019-03-15 20:39:02 +010064 char tmp[128], buf[128];
Max9a852f22019-01-31 15:58:57 +010065 char *tok;
66 unsigned int i = 0;
67 uint8_t *m = msgb_data(msg);
Pau Espin Pedrolb9101322019-03-15 20:09:26 +010068 unsigned int truncated_len = OSMO_MIN(sizeof(tmp) - 1, msgb_length(msg));
Max9a852f22019-01-31 15:58:57 +010069
Pau Espin Pedrolb9101322019-03-15 20:09:26 +010070 if (msgb_length(msg) > truncated_len)
71 OVPN_LOG(msg, vpn, "received message too long (%d >= %u), truncating...\n", msgb_length(msg), truncated_len);
Max9a852f22019-01-31 15:58:57 +010072
73 if (msgb_length(msg) > 0) {
74 if (!isdigit(m[0])) /* skip OpenVPN greetings and alike */
75 return NULL;
76 } else {
77 OVPN_LOG(msg, vpn, "received message is empty, ignoring...\n");
78 return NULL;
79 }
80
Pau Espin Pedrolb9101322019-03-15 20:09:26 +010081 memcpy(tmp, m, truncated_len);
82 tmp[truncated_len] = '\0';
Max9a852f22019-01-31 15:58:57 +010083
84 for (tok = strtok(tmp, ","); tok && i < MAX_RESP_COMPONENTS; tok = strtok(NULL, ",")) {
85 /* The string format is documented in https://openvpn.net/community-resources/management-interface/ */
86 if (tok) { /* Parse csv string and pick interesting tokens while ignoring the rest. */
87 switch (i++) {
Pau Espin Pedrola3a92ec2019-03-15 20:39:02 +010088 /* case 0: unix/date time, not needed */
Max9a852f22019-01-31 15:58:57 +010089 case 1:
90 update_name(vpn->rem_cfg, tok);
91 break;
Pau Espin Pedrola3a92ec2019-03-15 20:39:02 +010092 case 2:
93 snprintf(buf, sizeof(buf), "%s (%s)", vpn->rem_cfg->name, tok);
94 update_name(vpn->rem_cfg, buf);
Max9a852f22019-01-31 15:58:57 +010095 case 3:
96 osmo_talloc_replace_string(vpn->rem_cfg, &vpn->tun_ip, tok);
97 break;
98 case 4:
99 update_host(vpn->rem_cfg, tok);
100 break;
101 case 5:
102 vpn->rem_cfg->remote_port = atoi(tok);
103 break;
104 }
105 }
106 }
107 return NULL;
108}
109
110static struct openvpn_client *openvpn_client_find_or_make(const struct osysmon_state *os,
111 const char *host, uint16_t port)
112{
113 struct openvpn_client *vpn;
114 llist_for_each_entry(vpn, &os->openvpn_clients, list) {
115 if (match_config(vpn->cfg, host, MATCH_HOST) && vpn->cfg->remote_port == port)
116 return vpn;
117 }
118
119 return NULL;
120}
121
Max6cbdcaf2019-02-21 10:06:19 +0100122static int disconnect_cb(struct osmo_stream_cli *conn)
123{
124 struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
125
Pau Espin Pedrol53f50732019-03-15 19:18:18 +0100126 update_name(vpn->rem_cfg, "disconnected from openvpn management socket");
Max6cbdcaf2019-02-21 10:06:19 +0100127 vpn->connected = false;
128 talloc_free(vpn->tun_ip);
129 talloc_free(vpn->rem_cfg->remote_host);
130 vpn->tun_ip = NULL;
131 vpn->rem_cfg->remote_host = NULL;
132
133 return 0;
134}
135
Max9a852f22019-01-31 15:58:57 +0100136static int connect_cb(struct osmo_stream_cli *conn)
137{
138 struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
139
Pau Espin Pedrol53f50732019-03-15 19:18:18 +0100140 update_name(vpn->rem_cfg, "connected to openvpn management socket");
Max6cbdcaf2019-02-21 10:06:19 +0100141 vpn->connected = true;
Max9a852f22019-01-31 15:58:57 +0100142
143 return 0;
144}
145
146static int read_cb(struct osmo_stream_cli *conn)
147{
148 int bytes;
149 struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
150 struct msgb *msg = msgb_alloc(1024, "OpenVPN");
151 if (!msg) {
152 OVPN_LOG(conn, vpn, "unable to allocate message in callback\n");
153 return 0;
154 }
155
156 bytes = osmo_stream_cli_recv(conn, msg);
157 if (bytes < 0)
Pau Espin Pedrol53f50732019-03-15 19:18:18 +0100158 OVPN_LOG(msg, vpn, "read on openvpn management socket failed (%d)\n", bytes);
Max9a852f22019-01-31 15:58:57 +0100159 else
160 parse_state(msg, vpn);
161
162 msgb_free(msg);
163
164 return 0;
165}
166
167static bool openvpn_client_create(struct osysmon_state *os, const char *name, const char *host, uint16_t port)
168{
169 struct openvpn_client *vpn = openvpn_client_find_or_make(os, host, port);
170 if (vpn)
171 return true;
172
173 vpn = talloc_zero(os, struct openvpn_client);
174 if (!vpn)
175 return false;
176
177 vpn->connected = false;
178
179 vpn->cfg = host_cfg_alloc(vpn, name, host, port);
180 if (!vpn->cfg)
181 goto dealloc;
182
Pau Espin Pedrol53f50732019-03-15 19:18:18 +0100183 vpn->rem_cfg = host_cfg_alloc(vpn, "connecting to openvpn management socket", NULL, 0);
Max9a852f22019-01-31 15:58:57 +0100184 if (!vpn->rem_cfg)
185 goto dealloc;
186
187 vpn->mgmt = make_tcp_client(vpn->cfg);
188 if (!vpn->mgmt) {
189 OVPN_LOG(vpn->rem_cfg, vpn, "failed to create TCP client\n");
190 goto dealloc;
191 }
192
193 /* Wait for 1 minute before attempting to reconnect to management interface */
Pau Espin Pedrol0813db32019-03-15 19:27:04 +0100194 osmo_stream_cli_set_reconnect_timeout(vpn->mgmt, 5);
Max9a852f22019-01-31 15:58:57 +0100195 osmo_stream_cli_set_read_cb(vpn->mgmt, read_cb);
196 osmo_stream_cli_set_connect_cb(vpn->mgmt, connect_cb);
Max6cbdcaf2019-02-21 10:06:19 +0100197 osmo_stream_cli_set_disconnect_cb(vpn->mgmt, disconnect_cb);
Max9a852f22019-01-31 15:58:57 +0100198
199 if (osmo_stream_cli_open(vpn->mgmt) < 0) {
Pau Espin Pedrol53f50732019-03-15 19:18:18 +0100200 OVPN_LOG(vpn->rem_cfg, vpn, "failed to connect to management socket\n");
Max9a852f22019-01-31 15:58:57 +0100201 goto dealloc;
202 }
203
204 osmo_stream_cli_set_data(vpn->mgmt, vpn);
205 llist_add_tail(&vpn->list, &os->openvpn_clients);
206
207 return true;
208
209dealloc:
210 talloc_free(vpn);
211 return false;
212}
213
214static void openvpn_client_destroy(struct openvpn_client *vpn)
215{
216 if (!vpn)
217 return;
218
219 osmo_stream_cli_destroy(vpn->mgmt);
220 llist_del(&vpn->list);
221 talloc_free(vpn);
222}
223
224
225/***********************************************************************
226 * VTY
227 ***********************************************************************/
228
229#define OPENVPN_STR "Configure OpenVPN management interface address\n"
230
231DEFUN(cfg_openvpn, cfg_openvpn_cmd,
232 "openvpn HOST <1-65535>",
233 OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n")
234{
235 uint16_t port = atoi(argv[1]);
236
237 if (!openvpn_client_create(g_oss, "OpenVPN", argv[0], port)) {
238 vty_out(vty, "Failed to create OpenVPN client for %s:%u%s", argv[0], port, VTY_NEWLINE);
239 return CMD_WARNING;
240 }
241
242 return CMD_SUCCESS;
243}
244
245DEFUN(cfg_no_openvpn, cfg_no_openvpn_cmd,
246 "no openvpn HOST <1-65535>",
247 NO_STR OPENVPN_STR "Name of the host to connect to\n" "Management interface port\n")
248{
249 uint16_t port = atoi(argv[1]);
250 struct openvpn_client *vpn = openvpn_client_find_or_make(g_oss, argv[0], port);
251 if (!vpn) {
252 vty_out(vty, "OpenVPN client %s:%u doesn't exist%s", argv[0], port, VTY_NEWLINE);
253 return CMD_WARNING;
254 }
255
256 openvpn_client_destroy(vpn);
257
258 return CMD_SUCCESS;
259}
260
261
262/***********************************************************************
263 * Runtime Code
264 ***********************************************************************/
265
266static int openvpn_client_poll(struct openvpn_client *vpn, struct value_node *parent)
267{
268 char *remote = make_authority(parent, vpn->rem_cfg);
269 struct value_node *vn_host = value_node_find_or_add(parent, make_authority(parent, vpn->cfg));
270 struct msgb *msg = msgb_alloc(128, "state");
271 if (!msg) {
272 value_node_add(vn_host, "msgb", "memory allocation failure");
273 return 0;
274 }
275
276 if (vpn->rem_cfg->name)
277 value_node_add(vn_host, "status", vpn->rem_cfg->name);
278
279 if (vpn->tun_ip)
280 value_node_add(vn_host, "tunnel", vpn->tun_ip);
281
282 if (remote)
283 value_node_add(vn_host, "remote", remote);
284
Max9a852f22019-01-31 15:58:57 +0100285 if (vpn->connected) { /* re-trigger state command */
286 msgb_printf(msg, "state\n");
287 osmo_stream_cli_send(vpn->mgmt, msg);
288 }
289
290 return 0;
291}
292
293/* called once on startup before config file parsing */
294int osysmon_openvpn_init()
295{
296 install_element(CONFIG_NODE, &cfg_openvpn_cmd);
297 install_element(CONFIG_NODE, &cfg_no_openvpn_cmd);
298
299 return 0;
300}
301
302/* called periodically */
303int osysmon_openvpn_poll(struct value_node *parent)
304{
305 int num_vpns = llist_count(&g_oss->openvpn_clients);
306 if (num_vpns) {
307 struct value_node *vn_vpn = value_node_add(parent, "OpenVPN", NULL);
308 struct openvpn_client *vpn;
309 llist_for_each_entry(vpn, &g_oss->openvpn_clients, list)
310 openvpn_client_poll(vpn, vn_vpn);
311 }
312
313 return num_vpns;
314}