blob: 824466ba5d54ff0ece40c346284b07a184fb9d49 [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_vty.c
2 * VTY interface for our GPRS Networks Service (NS) implementation. */
3
4/* (C) 2009-2014 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
6 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
7 * Author: Alexander Couzens <lynxis@fe80.eu>
8 *
9 * All Rights Reserved
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27
28#include <stdlib.h>
29#include <unistd.h>
30#include <errno.h>
31#include <stdint.h>
32
33#include <arpa/inet.h>
34
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/byteswap.h>
37#include <osmocom/core/talloc.h>
38#include <osmocom/core/select.h>
39#include <osmocom/core/rate_ctr.h>
40#include <osmocom/core/socket.h>
41#include <osmocom/core/sockaddr_str.h>
42#include <osmocom/core/linuxlist.h>
43#include <osmocom/core/socket.h>
44#include <osmocom/gprs/gprs_ns2.h>
45#include <osmocom/gsm/tlv.h>
46#include <osmocom/vty/vty.h>
47#include <osmocom/vty/command.h>
48#include <osmocom/vty/logging.h>
49#include <osmocom/vty/telnet_interface.h>
50#include <osmocom/vty/misc.h>
51
52#include "gprs_ns2_internal.h"
53
54struct ns2_vty_priv {
55 /* global listen */
56 struct osmo_sockaddr_str udp;
57 struct osmo_sockaddr_str frgreaddr;
58 int dscp;
59 enum gprs_ns2_vc_mode vc_mode;
60 /* force vc mode if another configuration forces
61 * the vc mode. E.g. SNS configuration */
62 bool force_vc_mode;
Daniel Willmann4fb27a82020-09-25 15:39:46 +020063 const char *force_vc_mode_reason;
Alexander Couzens6a161492020-07-12 13:45:50 +020064 bool frgre;
65
66 struct llist_head vtyvc;
67};
68
69struct ns2_vty_vc {
70 struct llist_head list;
71
72 struct osmo_sockaddr_str remote;
73 enum gprs_ns_ll ll;
74
75 /* old vty code doesnt support multiple NSVCI per NSEI */
76 uint16_t nsei;
77 uint16_t nsvci;
78 uint16_t frdlci;
79
80 bool remote_end_is_sgsn;
81 bool configured;
82};
83
84static struct gprs_ns2_inst *vty_nsi = NULL;
85static struct ns2_vty_priv priv;
86
87/* FIXME: this should go to some common file as it is copied
88 * in vty_interface.c of the BSC */
89static const struct value_string gprs_ns_timer_strs[] = {
90 { 0, "tns-block" },
91 { 1, "tns-block-retries" },
92 { 2, "tns-reset" },
93 { 3, "tns-reset-retries" },
94 { 4, "tns-test" },
95 { 5, "tns-alive" },
96 { 6, "tns-alive-retries" },
97 { 7, "tsns-prov" },
98 { 0, NULL }
99};
100
101static void log_set_nsvc_filter(struct log_target *target,
102 struct gprs_ns2_vc *nsvc)
103{
104 if (nsvc) {
105 target->filter_map |= (1 << LOG_FLT_GB_NSVC);
106 target->filter_data[LOG_FLT_GB_NSVC] = nsvc;
107 } else if (target->filter_data[LOG_FLT_GB_NSVC]) {
108 target->filter_map = ~(1 << LOG_FLT_GB_NSVC);
109 target->filter_data[LOG_FLT_GB_NSVC] = NULL;
110 }
111}
112
113static struct cmd_node ns_node = {
114 L_NS_NODE,
115 "%s(config-ns)# ",
116 1,
117};
118
119static struct ns2_vty_vc *vtyvc_alloc(uint16_t nsei) {
120 struct ns2_vty_vc *vtyvc = talloc_zero(vty_nsi, struct ns2_vty_vc);
121 if (!vtyvc)
122 return vtyvc;
123
124 vtyvc->nsei = nsei;
125
126 llist_add(&vtyvc->list, &priv.vtyvc);
127
128 return vtyvc;
129}
130
131static void ns2_vc_free(struct ns2_vty_vc *vtyvc) {
132 if (!vtyvc)
133 return;
134
135 llist_del(&vtyvc->list);
136 talloc_free(vtyvc);
137}
138
139static struct ns2_vty_vc *vtyvc_by_nsei(uint16_t nsei, bool alloc_missing) {
140 struct ns2_vty_vc *vtyvc;
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200141
Alexander Couzens6a161492020-07-12 13:45:50 +0200142 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
143 if (vtyvc->nsei == nsei)
144 return vtyvc;
145 }
146
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200147 if (!alloc_missing)
148 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200149
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200150 vtyvc = vtyvc_alloc(nsei);
151 if (!vtyvc)
152 return vtyvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200153
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200154 vtyvc->nsei = nsei;
155 return vtyvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200156}
157
158static int config_write_ns(struct vty *vty)
159{
160 struct ns2_vty_vc *vtyvc;
161 unsigned int i;
162 struct osmo_sockaddr_str sockstr;
163
164 vty_out(vty, "ns%s", VTY_NEWLINE);
165
166 /* global configuration must be written first, as some of it may be
167 * relevant when creating the NSE/NSVC later below */
168
169 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
170 priv.frgre ? 1 : 0, VTY_NEWLINE);
171
172 if (priv.frgre) {
173 if (strlen(priv.frgreaddr.ip)) {
174 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
175 sockstr.ip, VTY_NEWLINE);
176 }
177 } else {
178 if (strlen(priv.udp.ip)) {
179 vty_out(vty, " encapsulation udp local-ip %s%s",
180 priv.udp.ip, VTY_NEWLINE);
181 }
182
183 if (priv.udp.port)
184 vty_out(vty, " encapsulation udp local-port %u%s",
185 priv.udp.port, VTY_NEWLINE);
186 }
187
188 if (priv.dscp)
189 vty_out(vty, " encapsulation udp dscp %d%s",
190 priv.dscp, VTY_NEWLINE);
191
192 vty_out(vty, " encapsulation udp use-reset-block-unblock %s%s",
193 priv.vc_mode == NS2_VC_MODE_BLOCKRESET ? "enabled" : "disabled", VTY_NEWLINE);
194
195 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
196 vty_out(vty, " nse %u nsvci %u%s",
197 vtyvc->nsei, vtyvc->nsvci, VTY_NEWLINE);
198
199 vty_out(vty, " nse %u remote-role %s%s",
200 vtyvc->nsei, vtyvc->remote_end_is_sgsn ? "sgsn" : "bss",
201 VTY_NEWLINE);
202
203 switch (vtyvc->ll) {
204 case GPRS_NS_LL_UDP:
205 vty_out(vty, " nse %u encapsulation udp%s", vtyvc->nsei, VTY_NEWLINE);
206 vty_out(vty, " nse %u remote-ip %s%s",
207 vtyvc->nsei,
208 vtyvc->remote.ip,
209 VTY_NEWLINE);
210 vty_out(vty, " nse %u remote-port %u%s",
211 vtyvc->nsei, vtyvc->remote.port,
212 VTY_NEWLINE);
213 break;
214 case GPRS_NS_LL_FR_GRE:
215 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
216 vtyvc->nsei, VTY_NEWLINE);
217 vty_out(vty, " nse %u remote-ip %s%s",
218 vtyvc->nsei,
219 vtyvc->remote.ip,
220 VTY_NEWLINE);
221 vty_out(vty, " nse %u fr-dlci %u%s",
222 vtyvc->nsei, vtyvc->frdlci,
223 VTY_NEWLINE);
224 break;
225 default:
226 break;
227 }
228 }
229
230 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
231 vty_out(vty, " timer %s %u%s",
232 get_value_string(gprs_ns_timer_strs, i),
233 vty_nsi->timeout[i], VTY_NEWLINE);
234
235 return CMD_SUCCESS;
236}
237
238DEFUN(cfg_ns, cfg_ns_cmd,
239 "ns",
240 "Configure the GPRS Network Service")
241{
242 vty->node = L_NS_NODE;
243 return CMD_SUCCESS;
244}
245
246static void dump_nsvc(struct vty *vty, struct gprs_ns2_vc *nsvc, bool stats)
247{
248 struct osmo_sockaddr_str remote;
249 struct osmo_sockaddr_str local;
250 struct osmo_sockaddr *sockaddr;
251
252 switch (nsvc->ll) {
253 case GPRS_NS_LL_UDP: {
254 sockaddr = gprs_ns2_ip_vc_sockaddr(nsvc);
255 if (!sockaddr) {
256 vty_out(vty, "unknown");
257 break;
258 }
259
260 if (osmo_sockaddr_str_from_sockaddr(
261 &remote,
262 &sockaddr->u.sas)) {
263 vty_out(vty, "unknown");
264 break;
265 }
266
267 vty_out(vty, "%s:%u <> %s:%u", local.ip, local.port, remote.ip, remote.port);
268 break;
269 }
270 case GPRS_NS_LL_FR_GRE:
271 /* TODO: implement dump_nse for FR GRE */
272 case GPRS_NS_LL_E1:
273 /* TODO: implement dump_nse for E1 */
274 break;
275 }
276
277 vty_out(vty, "Remote: %s ",
278 gprs_ns2_ll_str(nsvc));
279
280 vty_out(vty, "%s%s", nsvc->ll == GPRS_NS_LL_UDP ? "UDP" : "FR-GRE", VTY_NEWLINE);
281
282 if (stats) {
283 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
284 vty_out_stat_item_group(vty, " ", nsvc->statg);
285 }
286}
287
288static void dump_nse(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats, bool persistent_only)
289{
290 struct gprs_ns2_vc *nsvc;
291
292 vty_out(vty, "NSEI %5u%s",
293 nse->nsei, VTY_NEWLINE);
294
295 gprs_ns2_sns_dump_vty(vty, nse, stats);
296 llist_for_each_entry(nsvc, &nse->nsvc, list) {
297 if (persistent_only) {
298 if (nsvc->persistent)
299 dump_nsvc(vty, nsvc, stats);
300 } else {
301 dump_nsvc(vty, nsvc, stats);
302 }
303 }
304}
305
306static void dump_ns(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats, bool persistent_only)
307{
308 struct gprs_ns2_nse *nse;
309
310 llist_for_each_entry(nse, &nsi->nse, list) {
311 dump_nse(vty, nse, stats, persistent_only);
Alexander Couzens6a161492020-07-12 13:45:50 +0200312 }
313
314}
315
316DEFUN(show_ns, show_ns_cmd, "show ns",
317 SHOW_STR "Display information about the NS protocol")
318{
319 dump_ns(vty, vty_nsi, false, false);
320 return CMD_SUCCESS;
321}
322
323DEFUN(show_ns_stats, show_ns_stats_cmd, "show ns stats",
324 SHOW_STR
325 "Display information about the NS protocol\n"
326 "Include statistics\n")
327{
328 dump_ns(vty, vty_nsi, true, false);
329 return CMD_SUCCESS;
330}
331
332DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
333 SHOW_STR
334 "Display information about the NS protocol\n"
335 "Show only persistent NS\n")
336{
337 dump_ns(vty, vty_nsi, true, true);
338 return CMD_SUCCESS;
339}
340
341DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
342 SHOW_STR "Display information about the NS protocol\n"
343 "Select one NSE by its NSE Identifier\n"
344 "Select one NSE by its NS-VC Identifier\n"
345 "The Identifier of selected type\n"
346 "Include Statistics\n")
347{
348 struct gprs_ns2_inst *nsi = vty_nsi;
349 struct gprs_ns2_nse *nse;
350 struct gprs_ns2_vc *nsvc;
351 uint16_t id = atoi(argv[1]);
352 bool show_stats = false;
353
354 if (argc >= 3)
355 show_stats = true;
356
357 if (!strcmp(argv[0], "nsei")) {
358 nse = gprs_ns2_nse_by_nsei(nsi, id);
359 if (!nse) {
360 return CMD_WARNING;
361 }
362
363 dump_nse(vty, nse, show_stats, false);
364 } else {
365 nsvc = gprs_ns2_nsvc_by_nsvci(nsi, id);
366
367 if (!nsvc) {
368 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
369 return CMD_WARNING;
370 }
371
372 dump_nsvc(vty, nsvc, show_stats);
373 }
374
375 return CMD_SUCCESS;
376}
377
378#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
379
380DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
381 "nse <0-65535> nsvci <0-65535>",
382 NSE_CMD_STR
383 "NS Virtual Connection\n"
384 "NS Virtual Connection ID (NSVCI)\n"
385 )
386{
387 struct ns2_vty_vc *vtyvc;
388
389 uint16_t nsei = atoi(argv[0]);
390 uint16_t nsvci = atoi(argv[1]);
391
392 vtyvc = vtyvc_by_nsei(nsei, true);
393 if (!vtyvc) {
394 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
395 return CMD_WARNING;
396 }
397
398 vtyvc->nsvci = nsvci;
399
400 return CMD_SUCCESS;
401}
402
403DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
404 "nse <0-65535> remote-ip " VTY_IPV46_CMD,
405 NSE_CMD_STR
406 "Remote IP Address\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200407 "Remote IPv4 Address\n"
408 "Remote IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200409{
410 uint16_t nsei = atoi(argv[0]);
411 struct ns2_vty_vc *vtyvc;
412
413 vtyvc = vtyvc_by_nsei(nsei, true);
414 if (!vtyvc) {
415 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
416 return CMD_WARNING;
417 }
418
419 osmo_sockaddr_str_from_str2(&vtyvc->remote, argv[1]);
420
421 return CMD_SUCCESS;
422}
423
424DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
425 "nse <0-65535> remote-port <0-65535>",
426 NSE_CMD_STR
427 "Remote UDP Port\n"
428 "Remote UDP Port Number\n")
429{
430 uint16_t nsei = atoi(argv[0]);
431 uint16_t port = atoi(argv[1]);
432 struct ns2_vty_vc *vtyvc;
433
434 vtyvc = vtyvc_by_nsei(nsei, true);
435 if (!vtyvc) {
436 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
437 return CMD_WARNING;
438 }
439
440 vtyvc->remote.port = port;
441
442 return CMD_SUCCESS;
443}
444
445DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
446 "nse <0-65535> fr-dlci <16-1007>",
447 NSE_CMD_STR
448 "Frame Relay DLCI\n"
449 "Frame Relay DLCI Number\n")
450{
451 uint16_t nsei = atoi(argv[0]);
452 uint16_t dlci = atoi(argv[1]);
453 struct ns2_vty_vc *vtyvc;
454
455 vtyvc = vtyvc_by_nsei(nsei, true);
456 if (!vtyvc) {
457 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
458 return CMD_WARNING;
459 }
460
461 if (vtyvc->ll != GPRS_NS_LL_FR_GRE) {
462 vty_out(vty, "Warning: seting FR DLCI on non-FR NSE%s",
463 VTY_NEWLINE);
464 }
465
466 vtyvc->frdlci = dlci;
467
468 return CMD_SUCCESS;
469}
470
471DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
472 "nse <0-65535> encapsulation (udp|framerelay-gre)",
473 NSE_CMD_STR
474 "Encapsulation for NS\n"
475 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
476{
477 uint16_t nsei = atoi(argv[0]);
478 struct ns2_vty_vc *vtyvc;
479
480 vtyvc = vtyvc_by_nsei(nsei, true);
481 if (!vtyvc) {
482 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
483 return CMD_WARNING;
484 }
485
486 if (!strcmp(argv[1], "udp"))
487 vtyvc->ll = GPRS_NS_LL_UDP;
488 else
489 vtyvc->ll = GPRS_NS_LL_FR_GRE;
490
491 return CMD_SUCCESS;
492}
493
494DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
495 "nse <0-65535> remote-role (sgsn|bss)",
496 NSE_CMD_STR
497 "Remote NSE Role\n"
498 "Remote Peer is SGSN\n"
499 "Remote Peer is BSS\n")
500{
501 uint16_t nsei = atoi(argv[0]);
502 struct ns2_vty_vc *vtyvc;
503
504 vtyvc = vtyvc_by_nsei(nsei, true);
505 if (!vtyvc) {
506 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
507 return CMD_WARNING;
508 }
509
510 if (!strcmp(argv[1], "sgsn"))
511 vtyvc->remote_end_is_sgsn = 1;
512 else
513 vtyvc->remote_end_is_sgsn = 0;
514
515 return CMD_SUCCESS;
516}
517
518DEFUN(cfg_no_nse, cfg_no_nse_cmd,
519 "no nse <0-65535>",
520 "Delete Persistent NS Entity\n"
521 "Delete " NSE_CMD_STR)
522{
523 uint16_t nsei = atoi(argv[0]);
524 struct ns2_vty_vc *vtyvc;
525
526 vtyvc = vtyvc_by_nsei(nsei, false);
527 if (!vtyvc) {
528 vty_out(vty, "The NSE %d does not exists.%s", nsei, VTY_NEWLINE);
529 return CMD_WARNING;
530 }
531
532 ns2_vc_free(vtyvc);
533
534 return CMD_SUCCESS;
535}
536
537DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
538 "timer " NS_TIMERS " <0-65535>",
539 "Network Service Timer\n"
540 NS_TIMERS_HELP "Timer Value\n")
541{
542 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
543 int val = atoi(argv[1]);
544
545 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
546 return CMD_WARNING;
547
548 vty_nsi->timeout[idx] = val;
549
550 return CMD_SUCCESS;
551}
552
553#define ENCAPS_STR "NS encapsulation options\n"
554
555DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
556 "encapsulation udp local-ip " VTY_IPV46_CMD,
557 ENCAPS_STR "NS over UDP Encapsulation\n"
558 "Set the IP address on which we listen for NS/UDP\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200559 "IPv4 Address\n"
560 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200561{
562 osmo_sockaddr_str_from_str2(&priv.udp, argv[0]);
563
564 return CMD_SUCCESS;
565}
566
567DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
568 "encapsulation udp local-port <0-65535>",
569 ENCAPS_STR "NS over UDP Encapsulation\n"
570 "Set the UDP port on which we listen for NS/UDP\n"
571 "UDP port number\n")
572{
573 unsigned int port = atoi(argv[0]);
574
575 priv.udp.port = port;
576
577 return CMD_SUCCESS;
578}
579
580DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
581 "encapsulation udp dscp <0-255>",
582 ENCAPS_STR "NS over UDP Encapsulation\n"
583 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
584{
585 int dscp = atoi(argv[0]);
586 struct gprs_ns2_vc_bind *bind;
587
588 priv.dscp = dscp;
589
590 llist_for_each_entry(bind, &vty_nsi->binding, list) {
591 if (gprs_ns2_is_ip_bind(bind))
592 gprs_ns2_ip_bind_set_dscp(bind, dscp);
593 }
594
595 return CMD_SUCCESS;
596}
597
598DEFUN(cfg_nsip_res_block_unblock, cfg_nsip_res_block_unblock_cmd,
599 "encapsulation udp use-reset-block-unblock (enabled|disabled)",
600 ENCAPS_STR "NS over UDP Encapsulation\n"
601 "Use NS-{RESET,BLOCK,UNBLOCK} procedures in violation of 3GPP TS 48.016\n"
602 "Enable NS-{RESET,BLOCK,UNBLOCK}\n"
603 "Disable NS-{RESET,BLOCK,UNBLOCK}\n")
604{
605 enum gprs_ns2_vc_mode vc_mode;
606 struct gprs_ns2_vc_bind *bind;
607
608 if (!strcmp(argv[0], "enabled"))
609 vc_mode = NS2_VC_MODE_BLOCKRESET;
610 else
611 vc_mode = NS2_VC_MODE_ALIVE;
612
613 if (priv.force_vc_mode) {
614 if (priv.vc_mode != vc_mode)
615 {
616 vty_out(vty, "Ignoring use-reset-block because it's already set by %s.%s",
617 priv.force_vc_mode_reason, VTY_NEWLINE);
618 return CMD_WARNING;
619 }
620
621 return CMD_SUCCESS;
622 }
623
624 priv.vc_mode = vc_mode;
625
626 llist_for_each_entry(bind, &vty_nsi->binding, list) {
627 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
628 }
629
630 return CMD_SUCCESS;
631}
632
633DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
634 "encapsulation framerelay-gre local-ip " VTY_IPV46_CMD,
635 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
636 "Set the IP address on which we listen for NS/FR/GRE\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200637 "IPv4 Address\n"
638 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200639{
640 osmo_sockaddr_str_from_str2(&priv.frgreaddr, argv[0]);
641
642 return CMD_SUCCESS;
643}
644
645DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd,
646 "encapsulation framerelay-gre enabled (1|0)",
647 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
648 "Enable or disable Frame Relay over GRE\n"
649 "Enable\n" "Disable\n")
650{
651 int enabled = atoi(argv[0]);
652
653 priv.frgre = enabled;
654
655 return CMD_SUCCESS;
656}
657
658/* TODO: allow vty to reset/block/unblock nsvc/nsei */
659
660/* TODO: add filter for NSEI as ns1 code does */
661/* TODO: add filter for single connection by description */
662DEFUN(logging_fltr_nsvc,
663 logging_fltr_nsvc_cmd,
664 "logging filter nsvc nsvci <0-65535>",
665 LOGGING_STR FILTER_STR
666 "Filter based on NS Virtual Connection\n"
667 "Identify NS-VC by NSVCI\n"
668 "Numeric identifier\n")
669{
670 struct log_target *tgt;
671 struct gprs_ns2_vc *nsvc;
672 uint16_t id = atoi(argv[1]);
673
674 log_tgt_mutex_lock();
675 tgt = osmo_log_vty2tgt(vty);
676 if (!tgt) {
677 log_tgt_mutex_unlock();
678 return CMD_WARNING;
679 }
680
681 nsvc = gprs_ns2_nsvc_by_nsvci(vty_nsi, id);
682 if (!nsvc) {
683 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
684 log_tgt_mutex_unlock();
685 return CMD_WARNING;
686 }
687
688 log_set_nsvc_filter(tgt, nsvc);
689 log_tgt_mutex_unlock();
690 return CMD_SUCCESS;
691}
692
Alexander Couzens1fac6f72020-10-01 19:08:38 +0200693/**
694 * gprs_ns2_vty_init initialize the vty
695 * \param[inout] nsi
696 * \param[in] default_bind set the default address to bind to. Can be NULL.
697 * \return 0 on success
698 */
699int gprs_ns2_vty_init(struct gprs_ns2_inst *nsi, struct osmo_sockaddr_str *default_bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200700{
701 static bool vty_elements_installed = false;
702
703 vty_nsi = nsi;
704 memset(&priv, 0, sizeof(struct ns2_vty_priv));
705 INIT_LLIST_HEAD(&priv.vtyvc);
706 priv.vc_mode = NS2_VC_MODE_BLOCKRESET;
Alexander Couzens1fac6f72020-10-01 19:08:38 +0200707 if (default_bind)
708 memcpy(&priv.udp, default_bind, sizeof(*default_bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200709
710 /* Regression test code may call this function repeatedly, so make sure
711 * that VTY elements are not duplicated, which would assert. */
712 if (vty_elements_installed)
713 return 0;
714 vty_elements_installed = true;
715
716 install_element_ve(&show_ns_cmd);
717 install_element_ve(&show_ns_stats_cmd);
718 install_element_ve(&show_ns_pers_cmd);
719 install_element_ve(&show_nse_cmd);
720 install_element_ve(&logging_fltr_nsvc_cmd);
721
722 install_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
723
724 install_element(CONFIG_NODE, &cfg_ns_cmd);
725 install_node(&ns_node, config_write_ns);
726 install_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
727 install_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
728 install_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
729 install_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
730 install_element(L_NS_NODE, &cfg_nse_encaps_cmd);
731 install_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
732 install_element(L_NS_NODE, &cfg_no_nse_cmd);
733 install_element(L_NS_NODE, &cfg_ns_timer_cmd);
734 install_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
735 install_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
736 install_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
737 install_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd);
738 install_element(L_NS_NODE, &cfg_frgre_enable_cmd);
739 install_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
740
741 /* TODO: nsvc/nsei command to reset states or reset/block/unblock nsei/nsvcs */
742
743 return 0;
744}
745
746/*!
747 * \brief gprs_ns2_vty_create parse the vty tree into ns nodes
748 * It has to be in different steps to ensure the bind is created before creating VCs.
749 * \return 0 on success
750 */
751int gprs_ns2_vty_create() {
752 struct ns2_vty_vc *vtyvc;
753 struct gprs_ns2_vc_bind *bind;
754 struct gprs_ns2_nse *nse;
755 struct gprs_ns2_vc *nsvc;
756 struct osmo_sockaddr sockaddr;
757
758 if (!vty_nsi)
759 return -1;
760
761 /* create binds, only support a single bind. either FR or UDP */
762 if (priv.frgre) {
763 /* TODO not yet supported !*/
764 return -1;
765 } else {
766 /* UDP */
767 osmo_sockaddr_str_to_sockaddr(&priv.udp, &sockaddr.u.sas);
Alexander Couzens477ffb02020-10-01 19:05:28 +0200768 if (gprs_ns2_ip_bind(vty_nsi, &sockaddr, priv.dscp, &bind)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200769 /* TODO: could not bind on the specific address */
770 return -1;
771 }
772 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
773 }
774
775 /* create vcs */
776 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
777 if (strlen(vtyvc->remote.ip) == 0) {
778 /* Invalid IP for VC */
779 continue;
780 }
781
782 if (!vtyvc->remote.port) {
783 /* Invalid port for VC */
784 continue;
785 }
786
787 if (osmo_sockaddr_str_to_sockaddr(&vtyvc->remote, &sockaddr.u.sas)) {
788 /* Invalid sockaddr for VC */
789 continue;
790 }
791
792 nse = gprs_ns2_nse_by_nsei(vty_nsi, vtyvc->nsei);
793 if (!nse) {
794 nse = gprs_ns2_create_nse(vty_nsi, vtyvc->nsei);
795 if (!nse) {
796 /* Could not create NSE for VTY */
797 continue;
798 }
799 }
800 nse->persistent = true;
801
802 if (bind) {
803 nsvc = gprs_ns2_ip_connect(bind,
804 &sockaddr,
805 nse,
806 vtyvc->nsvci);
807 if (!nsvc) {
808 /* Could not create NSVC, connect failed */
809 continue;
810 }
811 nsvc->persistent = true;
812 }
813 }
814
815
816 return 0;
817}
818
819/*!
820 * \brief ns2_vty_bind_apply will be called when a new bind is created to apply vty settings
821 * \param bind
822 * \return
823 */
824void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind)
825{
826 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
827}
828
829/*!
830 * \brief ns2_vty_force_vc_mode force a mode and prevents the vty from overwriting it.
831 * \param force if true mode and reason will be set. false to allow modification via vty.
832 * \param mode
833 * \param reason A description shown to the user when a vty command wants to change the mode.
834 */
Daniel Willmann4fb27a82020-09-25 15:39:46 +0200835void gprs_ns2_vty_force_vc_mode(bool force, enum gprs_ns2_vc_mode mode, const char *reason)
Alexander Couzens6a161492020-07-12 13:45:50 +0200836{
837 priv.force_vc_mode = force;
838
839 if (force) {
840 priv.vc_mode = mode;
841 priv.force_vc_mode_reason = reason;
842 }
843}