blob: f204ff1696dc2ca9541b2fbc0ce14e0642b8f59d [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;
63 char *force_vc_mode_reason;
64 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;
141 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
142 if (vtyvc->nsei == nsei)
143 return vtyvc;
144 }
145
146 if (alloc_missing) {
147 vtyvc = vtyvc_alloc(nsei);
148 if (!vtyvc)
149 return vtyvc;
150
151 vtyvc->nsei = nsei;
152 }
153
154 return NULL;
155}
156
157static int config_write_ns(struct vty *vty)
158{
159 struct ns2_vty_vc *vtyvc;
160 unsigned int i;
161 struct osmo_sockaddr_str sockstr;
162
163 vty_out(vty, "ns%s", VTY_NEWLINE);
164
165 /* global configuration must be written first, as some of it may be
166 * relevant when creating the NSE/NSVC later below */
167
168 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
169 priv.frgre ? 1 : 0, VTY_NEWLINE);
170
171 if (priv.frgre) {
172 if (strlen(priv.frgreaddr.ip)) {
173 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
174 sockstr.ip, VTY_NEWLINE);
175 }
176 } else {
177 if (strlen(priv.udp.ip)) {
178 vty_out(vty, " encapsulation udp local-ip %s%s",
179 priv.udp.ip, VTY_NEWLINE);
180 }
181
182 if (priv.udp.port)
183 vty_out(vty, " encapsulation udp local-port %u%s",
184 priv.udp.port, VTY_NEWLINE);
185 }
186
187 if (priv.dscp)
188 vty_out(vty, " encapsulation udp dscp %d%s",
189 priv.dscp, VTY_NEWLINE);
190
191 vty_out(vty, " encapsulation udp use-reset-block-unblock %s%s",
192 priv.vc_mode == NS2_VC_MODE_BLOCKRESET ? "enabled" : "disabled", VTY_NEWLINE);
193
194 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
195 vty_out(vty, " nse %u nsvci %u%s",
196 vtyvc->nsei, vtyvc->nsvci, VTY_NEWLINE);
197
198 vty_out(vty, " nse %u remote-role %s%s",
199 vtyvc->nsei, vtyvc->remote_end_is_sgsn ? "sgsn" : "bss",
200 VTY_NEWLINE);
201
202 switch (vtyvc->ll) {
203 case GPRS_NS_LL_UDP:
204 vty_out(vty, " nse %u encapsulation udp%s", vtyvc->nsei, VTY_NEWLINE);
205 vty_out(vty, " nse %u remote-ip %s%s",
206 vtyvc->nsei,
207 vtyvc->remote.ip,
208 VTY_NEWLINE);
209 vty_out(vty, " nse %u remote-port %u%s",
210 vtyvc->nsei, vtyvc->remote.port,
211 VTY_NEWLINE);
212 break;
213 case GPRS_NS_LL_FR_GRE:
214 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
215 vtyvc->nsei, VTY_NEWLINE);
216 vty_out(vty, " nse %u remote-ip %s%s",
217 vtyvc->nsei,
218 vtyvc->remote.ip,
219 VTY_NEWLINE);
220 vty_out(vty, " nse %u fr-dlci %u%s",
221 vtyvc->nsei, vtyvc->frdlci,
222 VTY_NEWLINE);
223 break;
224 default:
225 break;
226 }
227 }
228
229 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
230 vty_out(vty, " timer %s %u%s",
231 get_value_string(gprs_ns_timer_strs, i),
232 vty_nsi->timeout[i], VTY_NEWLINE);
233
234 return CMD_SUCCESS;
235}
236
237DEFUN(cfg_ns, cfg_ns_cmd,
238 "ns",
239 "Configure the GPRS Network Service")
240{
241 vty->node = L_NS_NODE;
242 return CMD_SUCCESS;
243}
244
245static void dump_nsvc(struct vty *vty, struct gprs_ns2_vc *nsvc, bool stats)
246{
247 struct osmo_sockaddr_str remote;
248 struct osmo_sockaddr_str local;
249 struct osmo_sockaddr *sockaddr;
250
251 switch (nsvc->ll) {
252 case GPRS_NS_LL_UDP: {
253 sockaddr = gprs_ns2_ip_vc_sockaddr(nsvc);
254 if (!sockaddr) {
255 vty_out(vty, "unknown");
256 break;
257 }
258
259 if (osmo_sockaddr_str_from_sockaddr(
260 &remote,
261 &sockaddr->u.sas)) {
262 vty_out(vty, "unknown");
263 break;
264 }
265
266 vty_out(vty, "%s:%u <> %s:%u", local.ip, local.port, remote.ip, remote.port);
267 break;
268 }
269 case GPRS_NS_LL_FR_GRE:
270 /* TODO: implement dump_nse for FR GRE */
271 case GPRS_NS_LL_E1:
272 /* TODO: implement dump_nse for E1 */
273 break;
274 }
275
276 vty_out(vty, "Remote: %s ",
277 gprs_ns2_ll_str(nsvc));
278
279 vty_out(vty, "%s%s", nsvc->ll == GPRS_NS_LL_UDP ? "UDP" : "FR-GRE", VTY_NEWLINE);
280
281 if (stats) {
282 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
283 vty_out_stat_item_group(vty, " ", nsvc->statg);
284 }
285}
286
287static void dump_nse(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats, bool persistent_only)
288{
289 struct gprs_ns2_vc *nsvc;
290
291 vty_out(vty, "NSEI %5u%s",
292 nse->nsei, VTY_NEWLINE);
293
294 gprs_ns2_sns_dump_vty(vty, nse, stats);
295 llist_for_each_entry(nsvc, &nse->nsvc, list) {
296 if (persistent_only) {
297 if (nsvc->persistent)
298 dump_nsvc(vty, nsvc, stats);
299 } else {
300 dump_nsvc(vty, nsvc, stats);
301 }
302 }
303}
304
305static void dump_ns(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats, bool persistent_only)
306{
307 struct gprs_ns2_nse *nse;
308
309 llist_for_each_entry(nse, &nsi->nse, list) {
310 dump_nse(vty, nse, stats, persistent_only);
311 break;
312 }
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
693int gprs_ns2_vty_init(struct gprs_ns2_inst *nsi)
694{
695 static bool vty_elements_installed = false;
696
697 vty_nsi = nsi;
698 memset(&priv, 0, sizeof(struct ns2_vty_priv));
699 INIT_LLIST_HEAD(&priv.vtyvc);
700 priv.vc_mode = NS2_VC_MODE_BLOCKRESET;
701
702 /* Regression test code may call this function repeatedly, so make sure
703 * that VTY elements are not duplicated, which would assert. */
704 if (vty_elements_installed)
705 return 0;
706 vty_elements_installed = true;
707
708 install_element_ve(&show_ns_cmd);
709 install_element_ve(&show_ns_stats_cmd);
710 install_element_ve(&show_ns_pers_cmd);
711 install_element_ve(&show_nse_cmd);
712 install_element_ve(&logging_fltr_nsvc_cmd);
713
714 install_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
715
716 install_element(CONFIG_NODE, &cfg_ns_cmd);
717 install_node(&ns_node, config_write_ns);
718 install_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
719 install_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
720 install_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
721 install_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
722 install_element(L_NS_NODE, &cfg_nse_encaps_cmd);
723 install_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
724 install_element(L_NS_NODE, &cfg_no_nse_cmd);
725 install_element(L_NS_NODE, &cfg_ns_timer_cmd);
726 install_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
727 install_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
728 install_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
729 install_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd);
730 install_element(L_NS_NODE, &cfg_frgre_enable_cmd);
731 install_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
732
733 /* TODO: nsvc/nsei command to reset states or reset/block/unblock nsei/nsvcs */
734
735 return 0;
736}
737
738/*!
739 * \brief gprs_ns2_vty_create parse the vty tree into ns nodes
740 * It has to be in different steps to ensure the bind is created before creating VCs.
741 * \return 0 on success
742 */
743int gprs_ns2_vty_create() {
744 struct ns2_vty_vc *vtyvc;
745 struct gprs_ns2_vc_bind *bind;
746 struct gprs_ns2_nse *nse;
747 struct gprs_ns2_vc *nsvc;
748 struct osmo_sockaddr sockaddr;
749
750 if (!vty_nsi)
751 return -1;
752
753 /* create binds, only support a single bind. either FR or UDP */
754 if (priv.frgre) {
755 /* TODO not yet supported !*/
756 return -1;
757 } else {
758 /* UDP */
759 osmo_sockaddr_str_to_sockaddr(&priv.udp, &sockaddr.u.sas);
760 gprs_ns2_ip_bind(vty_nsi, &sockaddr, priv.dscp, &bind);
761 if (!bind) {
762 /* TODO: could not bind on the specific address */
763 return -1;
764 }
765 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
766 }
767
768 /* create vcs */
769 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
770 if (strlen(vtyvc->remote.ip) == 0) {
771 /* Invalid IP for VC */
772 continue;
773 }
774
775 if (!vtyvc->remote.port) {
776 /* Invalid port for VC */
777 continue;
778 }
779
780 if (osmo_sockaddr_str_to_sockaddr(&vtyvc->remote, &sockaddr.u.sas)) {
781 /* Invalid sockaddr for VC */
782 continue;
783 }
784
785 nse = gprs_ns2_nse_by_nsei(vty_nsi, vtyvc->nsei);
786 if (!nse) {
787 nse = gprs_ns2_create_nse(vty_nsi, vtyvc->nsei);
788 if (!nse) {
789 /* Could not create NSE for VTY */
790 continue;
791 }
792 }
793 nse->persistent = true;
794
795 if (bind) {
796 nsvc = gprs_ns2_ip_connect(bind,
797 &sockaddr,
798 nse,
799 vtyvc->nsvci);
800 if (!nsvc) {
801 /* Could not create NSVC, connect failed */
802 continue;
803 }
804 nsvc->persistent = true;
805 }
806 }
807
808
809 return 0;
810}
811
812/*!
813 * \brief ns2_vty_bind_apply will be called when a new bind is created to apply vty settings
814 * \param bind
815 * \return
816 */
817void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind)
818{
819 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
820}
821
822/*!
823 * \brief ns2_vty_force_vc_mode force a mode and prevents the vty from overwriting it.
824 * \param force if true mode and reason will be set. false to allow modification via vty.
825 * \param mode
826 * \param reason A description shown to the user when a vty command wants to change the mode.
827 */
828void gprs_ns2_vty_force_vc_mode(bool force, enum gprs_ns2_vc_mode mode, char *reason)
829{
830 priv.force_vc_mode = force;
831
832 if (force) {
833 priv.vc_mode = mode;
834 priv.force_vc_mode_reason = reason;
835 }
836}