blob: a409cf4c7cfeb319a1b6f28259f404ea344890a9 [file] [log] [blame]
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +02001/*
2 * osmo-pcap-client code
3 *
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +02004 * (C) 2011-2016 by Holger Hans Peter Freyther <holger@moiji-mobile.com>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +02005 * (C) 2011 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <osmo-pcap/osmo_pcap_client.h>
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020024#include <osmo-pcap/common.h>
25
26#include <osmocom/core/talloc.h>
27
28#include <stdlib.h>
29
30
31#define PCAP_STRING "PCAP related functions\n"
32#define SERVER_STRING "Server string\n"
33
34static struct cmd_node client_node = {
35 CLIENT_NODE,
36 "%s(client)#",
37 1,
38};
39
40DEFUN(cfg_client,
41 cfg_client_cmd,
42 "client",
43 "Enter the client configuration\n")
44{
45 vty->node = CLIENT_NODE;
46 return CMD_SUCCESS;
47}
48
49static int config_write_client(struct vty *vty)
50{
51 vty_out(vty, "client%s", VTY_NEWLINE);
Holger Hans Peter Freytherbac0c982011-05-31 17:52:08 +020052
53 if (pcap_client->device)
54 vty_out(vty, " pcap device %s%s",
55 pcap_client->device, VTY_NEWLINE);
56
57 if (pcap_client->filter_string)
58 vty_out(vty, " pcap filter %s%s",
59 pcap_client->filter_string, VTY_NEWLINE);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020060 vty_out(vty, " pcap detect-loop %d%s",
61 pcap_client->filter_itself, VTY_NEWLINE);
Holger Hans Peter Freytherb7a834b2015-09-10 16:45:45 +020062 if (pcap_client->gprs_filtering)
63 vty_out(vty, " pcap add-filter gprs%s", VTY_NEWLINE);
Holger Hans Peter Freytherbac0c982011-05-31 17:52:08 +020064
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +020065 if (pcap_client->tls_on) {
66 vty_out(vty, " enable tls%s", VTY_NEWLINE);
67 vty_out(vty, " tls hostname %s%s", pcap_client->tls_hostname, VTY_NEWLINE);
68 vty_out(vty, " %stls verify-cert%s",
69 pcap_client->tls_verify ? "" : "no ", VTY_NEWLINE);
70 if (pcap_client->tls_capath)
71 vty_out(vty, " tls capath %s%s", pcap_client->tls_capath, VTY_NEWLINE);
72 if (pcap_client->tls_client_cert)
73 vty_out(vty, " tls client-cert %s%s",
74 pcap_client->tls_client_cert, VTY_NEWLINE);
75 if (pcap_client->tls_client_key)
76 vty_out(vty, " tls client-key %s%s",
77 pcap_client->tls_client_key, VTY_NEWLINE);
78 if (pcap_client->tls_priority)
79 vty_out(vty, " tls priority %s%s",
80 pcap_client->tls_priority, VTY_NEWLINE);
81 vty_out(vty, " tls log-level %d%s",
82 pcap_client->tls_log_level, VTY_NEWLINE);
83 }
84
Holger Hans Peter Freytherbac0c982011-05-31 17:52:08 +020085 if (pcap_client->srv_ip)
86 vty_out(vty, " server ip %s%s",
87 pcap_client->srv_ip, VTY_NEWLINE);
88
89 if (pcap_client->srv_port > 0)
90 vty_out(vty, " server port %d%s",
91 pcap_client->srv_port, VTY_NEWLINE);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020092
93 return CMD_SUCCESS;
94}
95
96DEFUN(cfg_client_device,
97 cfg_client_device_cmd,
98 "pcap device NAME",
99 PCAP_STRING "the device to filter\n" "device name\n")
100{
101 osmo_client_capture(pcap_client, argv[0]);
102 return CMD_SUCCESS;
103}
104
Holger Hans Peter Freytherb7a834b2015-09-10 16:45:45 +0200105DEFUN(cfg_client_add_gprs,
106 cfg_client_add_gprs_cmd,
107 "pcap add-filter gprs",
108 PCAP_STRING "Add-filter\n" "Custom filtering for GPRS\n")
109{
110 pcap_client->gprs_filtering = 1;
111 return CMD_SUCCESS;
112}
113
114DEFUN(cfg_client_del_gprs,
115 cfg_client_del_gprs_cmd,
116 "no pcap add-filter gprs",
117 NO_STR PCAP_STRING "Add-filter\n" "Custom filter for GPRS\n")
118{
119 pcap_client->gprs_filtering = 0;
120 return CMD_SUCCESS;
121}
122
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200123DEFUN(cfg_client_filter,
124 cfg_client_filter_cmd,
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200125 "pcap filter .NAME",
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200126 PCAP_STRING "filter string in pcap syntax\n" "filter\n")
127{
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200128 char *filter = argv_concat(argv, argc, 0);
129 if (!filter) {
130 vty_out(vty, "Failed to allocate buffer.%s", VTY_NEWLINE);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200131 return CMD_WARNING;
132 }
133
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200134
135 if (osmo_client_filter(pcap_client, filter) != 0) {
136 vty_out(vty, "Failed to set the device.%s", VTY_NEWLINE);
137 talloc_free(filter);
138 return CMD_WARNING;
139 }
140
141 talloc_free(filter);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200142 return CMD_SUCCESS;
143}
144
145DEFUN(cfg_client_loop,
146 cfg_client_loop_cmd,
147 "pcap detect-loop (0|1)",
148 PCAP_STRING "detect loop and drop\n" "No detection\n" "Detection\n")
149{
150 pcap_client->filter_itself = atoi(argv[0]);
151 return CMD_SUCCESS;
152}
153
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +0200154
155#define TLS_STR "Transport Layer Security\n"
156
157DEFUN(cfg_enable_tls,
158 cfg_enable_tls_cmd,
159 "enable tls",
160 "Enable\n" "Transport Layer Security\n")
161{
162 if (!pcap_client->tls_on) {
163 if (pcap_client->wqueue.bfd.fd >= 0)
164 osmo_client_reconnect(pcap_client);
165 }
166
167 pcap_client->tls_on = true;
168 return CMD_SUCCESS;
169}
170
171DEFUN(cfg_disable_tls,
172 cfg_disable_tls_cmd,
173 "disable tls",
174 "Disable\n" "Transport Layer Security\n")
175{
176 if (pcap_client->tls_on)
177 osmo_client_reconnect(pcap_client);
178
179 pcap_client->tls_on = false;
180 return CMD_SUCCESS;
181}
182
183DEFUN(cfg_tls_hostname,
184 cfg_tls_hostname_cmd,
185 "tls hostname NAME",
186 TLS_STR "hostname for certificate validation\n" "name\n")
187{
188 talloc_free(pcap_client->tls_hostname);
189 pcap_client->tls_hostname = talloc_strdup(pcap_client, argv[0]);
190 return CMD_SUCCESS;
191}
192
193DEFUN(cfg_no_tls_hostname,
194 cfg_no_tls_hostname_cmd,
195 "no tls hostname",
196 NO_STR TLS_STR "hostname for certificate validation\n")
197{
198 talloc_free(pcap_client->tls_hostname);
199 pcap_client->tls_hostname = NULL;
200 return CMD_SUCCESS;
201}
202
203DEFUN(cfg_tls_verify,
204 cfg_tls_verify_cmd,
205 "tls verify-cert",
206 TLS_STR "Verify certificates\n")
207{
208 pcap_client->tls_verify = true;
209 return CMD_SUCCESS;
210}
211
212DEFUN(cfg_no_tls_verify,
213 cfg_no_tls_verify_cmd,
214 "no tls verify-cert",
215 NO_STR TLS_STR "Verify certificates\n")
216{
217 pcap_client->tls_verify = false;
218 return CMD_SUCCESS;
219}
220
221DEFUN(cfg_tls_capath,
222 cfg_tls_capath_cmd,
223 "tls capath .PATH",
224 TLS_STR "Trusted root certificates\n" "Filename\n")
225{
226 talloc_free(pcap_client->tls_capath);
227 pcap_client->tls_capath = talloc_strdup(pcap_client, argv[0]);
228 return CMD_SUCCESS;
229}
230
231DEFUN(cfg_no_tls_capath,
232 cfg_no_tls_capath_cmd,
233 "no tls capath",
234 NO_STR TLS_STR "Trusted root certificates\n")
235{
236 talloc_free(pcap_client->tls_capath);
237 pcap_client->tls_capath = NULL;
238 return CMD_SUCCESS;
239}
240
241DEFUN(cfg_tls_client_cert,
242 cfg_tls_client_cert_cmd,
243 "tls client-cert .PATH",
244 TLS_STR "Client certificate for authentication\n" "Filename\n")
245{
246 talloc_free(pcap_client->tls_client_cert);
247 pcap_client->tls_client_cert = talloc_strdup(pcap_client, argv[0]);
248 return CMD_SUCCESS;
249}
250
251DEFUN(cfg_no_tls_client_cert,
252 cfg_no_tls_client_cert_cmd,
253 "no tls client-cert",
254 NO_STR TLS_STR "Client certificate for authentication\n")
255{
256 talloc_free(pcap_client->tls_client_cert);
257 pcap_client->tls_client_cert = NULL;
258 return CMD_SUCCESS;
259}
260
261DEFUN(cfg_tls_client_key,
262 cfg_tls_client_key_cmd,
263 "tls client-key .PATH",
264 TLS_STR "Client private key\n" "Filename\n")
265{
266 talloc_free(pcap_client->tls_client_key);
267 pcap_client->tls_client_key = talloc_strdup(pcap_client, argv[0]);
268 return CMD_SUCCESS;
269}
270
271DEFUN(cfg_no_tls_client_key,
272 cfg_no_tls_client_key_cmd,
273 "no tls client-key",
274 NO_STR TLS_STR "Client private key\n")
275{
276 talloc_free(pcap_client->tls_client_key);
277 pcap_client->tls_client_key = NULL;
278 return CMD_SUCCESS;
279}
280
281DEFUN(cfg_tls_priority,
282 cfg_tls_priority_cmd,
283 "tls priority STR",
284 TLS_STR "Priority string for GNUtls\n" "Priority string\n")
285{
286 talloc_free(pcap_client->tls_priority);
287 pcap_client->tls_priority = talloc_strdup(pcap_client, argv[0]);
288 return CMD_SUCCESS;
289}
290
291DEFUN(cfg_no_tls_priority,
292 cfg_no_tls_priority_cmd,
293 "no tls priority",
294 NO_STR TLS_STR "Priority string for GNUtls\n")
295{
296 talloc_free(pcap_client->tls_priority);
297 pcap_client->tls_priority = NULL;
298 return CMD_SUCCESS;
299}
300
301DEFUN(cfg_tls_log_level,
302 cfg_tls_log_level_cmd,
303 "tls log-level <0-255>",
304 TLS_STR "Log-level\n" "GNUtls debug level\n")
305{
306 pcap_client->tls_log_level = atoi(argv[0]);
307 return CMD_SUCCESS;
308}
309
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200310DEFUN(cfg_server_ip,
311 cfg_server_ip_cmd,
312 "server ip A.B.C.D",
313 SERVER_STRING "IP Address of the server\n" "IP\n")
314{
315 talloc_free(pcap_client->srv_ip);
316 pcap_client->srv_ip = talloc_strdup(pcap_client, argv[0]);
317 return CMD_SUCCESS;
318}
319
320DEFUN(cfg_server_port,
321 cfg_server_port_cmd,
322 "server port <1-65535>",
323 SERVER_STRING "Port\n" "Number\n")
324{
325 pcap_client->srv_port = atoi(argv[0]);
326 return CMD_SUCCESS;
327}
328
329
330int vty_client_init(struct osmo_pcap_client *pcap)
331{
332 install_element(CONFIG_NODE, &cfg_client_cmd);
333 install_node(&client_node, config_write_client);
334 install_default(CLIENT_NODE);
335
336 install_element(CLIENT_NODE, &cfg_client_device_cmd);
337 install_element(CLIENT_NODE, &cfg_client_filter_cmd);
338 install_element(CLIENT_NODE, &cfg_client_loop_cmd);
339
340 install_element(CLIENT_NODE, &cfg_server_ip_cmd);
341 install_element(CLIENT_NODE, &cfg_server_port_cmd);
342
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +0200343 install_element(CLIENT_NODE, &cfg_enable_tls_cmd);
344 install_element(CLIENT_NODE, &cfg_disable_tls_cmd);
345 install_element(CLIENT_NODE, &cfg_tls_hostname_cmd);
346 install_element(CLIENT_NODE, &cfg_no_tls_hostname_cmd);
347 install_element(CLIENT_NODE, &cfg_tls_verify_cmd);
348 install_element(CLIENT_NODE, &cfg_no_tls_verify_cmd);
349 install_element(CLIENT_NODE, &cfg_tls_capath_cmd);
350 install_element(CLIENT_NODE, &cfg_no_tls_capath_cmd);
351 install_element(CLIENT_NODE, &cfg_tls_client_cert_cmd);
352 install_element(CLIENT_NODE, &cfg_no_tls_client_cert_cmd);
353 install_element(CLIENT_NODE, &cfg_tls_client_key_cmd);
354 install_element(CLIENT_NODE, &cfg_no_tls_client_key_cmd);
355 install_element(CLIENT_NODE, &cfg_tls_priority_cmd);
356 install_element(CLIENT_NODE, &cfg_no_tls_priority_cmd);
357 install_element(CLIENT_NODE, &cfg_tls_log_level_cmd);
358
Holger Hans Peter Freytherb7a834b2015-09-10 16:45:45 +0200359 install_element(CLIENT_NODE, &cfg_client_add_gprs_cmd);
360 install_element(CLIENT_NODE, &cfg_client_del_gprs_cmd);
361
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200362 return 0;
363}