blob: d274cbebed04c8bceda49b37dd2c47c78cf929f5 [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);
312 break;
313 }
314
315}
316
317DEFUN(show_ns, show_ns_cmd, "show ns",
318 SHOW_STR "Display information about the NS protocol")
319{
320 dump_ns(vty, vty_nsi, false, false);
321 return CMD_SUCCESS;
322}
323
324DEFUN(show_ns_stats, show_ns_stats_cmd, "show ns stats",
325 SHOW_STR
326 "Display information about the NS protocol\n"
327 "Include statistics\n")
328{
329 dump_ns(vty, vty_nsi, true, false);
330 return CMD_SUCCESS;
331}
332
333DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
334 SHOW_STR
335 "Display information about the NS protocol\n"
336 "Show only persistent NS\n")
337{
338 dump_ns(vty, vty_nsi, true, true);
339 return CMD_SUCCESS;
340}
341
342DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
343 SHOW_STR "Display information about the NS protocol\n"
344 "Select one NSE by its NSE Identifier\n"
345 "Select one NSE by its NS-VC Identifier\n"
346 "The Identifier of selected type\n"
347 "Include Statistics\n")
348{
349 struct gprs_ns2_inst *nsi = vty_nsi;
350 struct gprs_ns2_nse *nse;
351 struct gprs_ns2_vc *nsvc;
352 uint16_t id = atoi(argv[1]);
353 bool show_stats = false;
354
355 if (argc >= 3)
356 show_stats = true;
357
358 if (!strcmp(argv[0], "nsei")) {
359 nse = gprs_ns2_nse_by_nsei(nsi, id);
360 if (!nse) {
361 return CMD_WARNING;
362 }
363
364 dump_nse(vty, nse, show_stats, false);
365 } else {
366 nsvc = gprs_ns2_nsvc_by_nsvci(nsi, id);
367
368 if (!nsvc) {
369 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
370 return CMD_WARNING;
371 }
372
373 dump_nsvc(vty, nsvc, show_stats);
374 }
375
376 return CMD_SUCCESS;
377}
378
379#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
380
381DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
382 "nse <0-65535> nsvci <0-65535>",
383 NSE_CMD_STR
384 "NS Virtual Connection\n"
385 "NS Virtual Connection ID (NSVCI)\n"
386 )
387{
388 struct ns2_vty_vc *vtyvc;
389
390 uint16_t nsei = atoi(argv[0]);
391 uint16_t nsvci = atoi(argv[1]);
392
393 vtyvc = vtyvc_by_nsei(nsei, true);
394 if (!vtyvc) {
395 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
396 return CMD_WARNING;
397 }
398
399 vtyvc->nsvci = nsvci;
400
401 return CMD_SUCCESS;
402}
403
404DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
405 "nse <0-65535> remote-ip " VTY_IPV46_CMD,
406 NSE_CMD_STR
407 "Remote IP Address\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200408 "Remote IPv4 Address\n"
409 "Remote IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200410{
411 uint16_t nsei = atoi(argv[0]);
412 struct ns2_vty_vc *vtyvc;
413
414 vtyvc = vtyvc_by_nsei(nsei, true);
415 if (!vtyvc) {
416 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
417 return CMD_WARNING;
418 }
419
420 osmo_sockaddr_str_from_str2(&vtyvc->remote, argv[1]);
421
422 return CMD_SUCCESS;
423}
424
425DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
426 "nse <0-65535> remote-port <0-65535>",
427 NSE_CMD_STR
428 "Remote UDP Port\n"
429 "Remote UDP Port Number\n")
430{
431 uint16_t nsei = atoi(argv[0]);
432 uint16_t port = atoi(argv[1]);
433 struct ns2_vty_vc *vtyvc;
434
435 vtyvc = vtyvc_by_nsei(nsei, true);
436 if (!vtyvc) {
437 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
438 return CMD_WARNING;
439 }
440
441 vtyvc->remote.port = port;
442
443 return CMD_SUCCESS;
444}
445
446DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
447 "nse <0-65535> fr-dlci <16-1007>",
448 NSE_CMD_STR
449 "Frame Relay DLCI\n"
450 "Frame Relay DLCI Number\n")
451{
452 uint16_t nsei = atoi(argv[0]);
453 uint16_t dlci = atoi(argv[1]);
454 struct ns2_vty_vc *vtyvc;
455
456 vtyvc = vtyvc_by_nsei(nsei, true);
457 if (!vtyvc) {
458 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
459 return CMD_WARNING;
460 }
461
462 if (vtyvc->ll != GPRS_NS_LL_FR_GRE) {
463 vty_out(vty, "Warning: seting FR DLCI on non-FR NSE%s",
464 VTY_NEWLINE);
465 }
466
467 vtyvc->frdlci = dlci;
468
469 return CMD_SUCCESS;
470}
471
472DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
473 "nse <0-65535> encapsulation (udp|framerelay-gre)",
474 NSE_CMD_STR
475 "Encapsulation for NS\n"
476 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
477{
478 uint16_t nsei = atoi(argv[0]);
479 struct ns2_vty_vc *vtyvc;
480
481 vtyvc = vtyvc_by_nsei(nsei, true);
482 if (!vtyvc) {
483 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
484 return CMD_WARNING;
485 }
486
487 if (!strcmp(argv[1], "udp"))
488 vtyvc->ll = GPRS_NS_LL_UDP;
489 else
490 vtyvc->ll = GPRS_NS_LL_FR_GRE;
491
492 return CMD_SUCCESS;
493}
494
495DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
496 "nse <0-65535> remote-role (sgsn|bss)",
497 NSE_CMD_STR
498 "Remote NSE Role\n"
499 "Remote Peer is SGSN\n"
500 "Remote Peer is BSS\n")
501{
502 uint16_t nsei = atoi(argv[0]);
503 struct ns2_vty_vc *vtyvc;
504
505 vtyvc = vtyvc_by_nsei(nsei, true);
506 if (!vtyvc) {
507 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
508 return CMD_WARNING;
509 }
510
511 if (!strcmp(argv[1], "sgsn"))
512 vtyvc->remote_end_is_sgsn = 1;
513 else
514 vtyvc->remote_end_is_sgsn = 0;
515
516 return CMD_SUCCESS;
517}
518
519DEFUN(cfg_no_nse, cfg_no_nse_cmd,
520 "no nse <0-65535>",
521 "Delete Persistent NS Entity\n"
522 "Delete " NSE_CMD_STR)
523{
524 uint16_t nsei = atoi(argv[0]);
525 struct ns2_vty_vc *vtyvc;
526
527 vtyvc = vtyvc_by_nsei(nsei, false);
528 if (!vtyvc) {
529 vty_out(vty, "The NSE %d does not exists.%s", nsei, VTY_NEWLINE);
530 return CMD_WARNING;
531 }
532
533 ns2_vc_free(vtyvc);
534
535 return CMD_SUCCESS;
536}
537
538DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
539 "timer " NS_TIMERS " <0-65535>",
540 "Network Service Timer\n"
541 NS_TIMERS_HELP "Timer Value\n")
542{
543 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
544 int val = atoi(argv[1]);
545
546 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
547 return CMD_WARNING;
548
549 vty_nsi->timeout[idx] = val;
550
551 return CMD_SUCCESS;
552}
553
554#define ENCAPS_STR "NS encapsulation options\n"
555
556DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
557 "encapsulation udp local-ip " VTY_IPV46_CMD,
558 ENCAPS_STR "NS over UDP Encapsulation\n"
559 "Set the IP address on which we listen for NS/UDP\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200560 "IPv4 Address\n"
561 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200562{
563 osmo_sockaddr_str_from_str2(&priv.udp, argv[0]);
564
565 return CMD_SUCCESS;
566}
567
568DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
569 "encapsulation udp local-port <0-65535>",
570 ENCAPS_STR "NS over UDP Encapsulation\n"
571 "Set the UDP port on which we listen for NS/UDP\n"
572 "UDP port number\n")
573{
574 unsigned int port = atoi(argv[0]);
575
576 priv.udp.port = port;
577
578 return CMD_SUCCESS;
579}
580
581DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
582 "encapsulation udp dscp <0-255>",
583 ENCAPS_STR "NS over UDP Encapsulation\n"
584 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
585{
586 int dscp = atoi(argv[0]);
587 struct gprs_ns2_vc_bind *bind;
588
589 priv.dscp = dscp;
590
591 llist_for_each_entry(bind, &vty_nsi->binding, list) {
592 if (gprs_ns2_is_ip_bind(bind))
593 gprs_ns2_ip_bind_set_dscp(bind, dscp);
594 }
595
596 return CMD_SUCCESS;
597}
598
599DEFUN(cfg_nsip_res_block_unblock, cfg_nsip_res_block_unblock_cmd,
600 "encapsulation udp use-reset-block-unblock (enabled|disabled)",
601 ENCAPS_STR "NS over UDP Encapsulation\n"
602 "Use NS-{RESET,BLOCK,UNBLOCK} procedures in violation of 3GPP TS 48.016\n"
603 "Enable NS-{RESET,BLOCK,UNBLOCK}\n"
604 "Disable NS-{RESET,BLOCK,UNBLOCK}\n")
605{
606 enum gprs_ns2_vc_mode vc_mode;
607 struct gprs_ns2_vc_bind *bind;
608
609 if (!strcmp(argv[0], "enabled"))
610 vc_mode = NS2_VC_MODE_BLOCKRESET;
611 else
612 vc_mode = NS2_VC_MODE_ALIVE;
613
614 if (priv.force_vc_mode) {
615 if (priv.vc_mode != vc_mode)
616 {
617 vty_out(vty, "Ignoring use-reset-block because it's already set by %s.%s",
618 priv.force_vc_mode_reason, VTY_NEWLINE);
619 return CMD_WARNING;
620 }
621
622 return CMD_SUCCESS;
623 }
624
625 priv.vc_mode = vc_mode;
626
627 llist_for_each_entry(bind, &vty_nsi->binding, list) {
628 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
629 }
630
631 return CMD_SUCCESS;
632}
633
634DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
635 "encapsulation framerelay-gre local-ip " VTY_IPV46_CMD,
636 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
637 "Set the IP address on which we listen for NS/FR/GRE\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200638 "IPv4 Address\n"
639 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200640{
641 osmo_sockaddr_str_from_str2(&priv.frgreaddr, argv[0]);
642
643 return CMD_SUCCESS;
644}
645
646DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd,
647 "encapsulation framerelay-gre enabled (1|0)",
648 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
649 "Enable or disable Frame Relay over GRE\n"
650 "Enable\n" "Disable\n")
651{
652 int enabled = atoi(argv[0]);
653
654 priv.frgre = enabled;
655
656 return CMD_SUCCESS;
657}
658
659/* TODO: allow vty to reset/block/unblock nsvc/nsei */
660
661/* TODO: add filter for NSEI as ns1 code does */
662/* TODO: add filter for single connection by description */
663DEFUN(logging_fltr_nsvc,
664 logging_fltr_nsvc_cmd,
665 "logging filter nsvc nsvci <0-65535>",
666 LOGGING_STR FILTER_STR
667 "Filter based on NS Virtual Connection\n"
668 "Identify NS-VC by NSVCI\n"
669 "Numeric identifier\n")
670{
671 struct log_target *tgt;
672 struct gprs_ns2_vc *nsvc;
673 uint16_t id = atoi(argv[1]);
674
675 log_tgt_mutex_lock();
676 tgt = osmo_log_vty2tgt(vty);
677 if (!tgt) {
678 log_tgt_mutex_unlock();
679 return CMD_WARNING;
680 }
681
682 nsvc = gprs_ns2_nsvc_by_nsvci(vty_nsi, id);
683 if (!nsvc) {
684 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
685 log_tgt_mutex_unlock();
686 return CMD_WARNING;
687 }
688
689 log_set_nsvc_filter(tgt, nsvc);
690 log_tgt_mutex_unlock();
691 return CMD_SUCCESS;
692}
693
694int gprs_ns2_vty_init(struct gprs_ns2_inst *nsi)
695{
696 static bool vty_elements_installed = false;
697
698 vty_nsi = nsi;
699 memset(&priv, 0, sizeof(struct ns2_vty_priv));
700 INIT_LLIST_HEAD(&priv.vtyvc);
701 priv.vc_mode = NS2_VC_MODE_BLOCKRESET;
702
703 /* Regression test code may call this function repeatedly, so make sure
704 * that VTY elements are not duplicated, which would assert. */
705 if (vty_elements_installed)
706 return 0;
707 vty_elements_installed = true;
708
709 install_element_ve(&show_ns_cmd);
710 install_element_ve(&show_ns_stats_cmd);
711 install_element_ve(&show_ns_pers_cmd);
712 install_element_ve(&show_nse_cmd);
713 install_element_ve(&logging_fltr_nsvc_cmd);
714
715 install_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
716
717 install_element(CONFIG_NODE, &cfg_ns_cmd);
718 install_node(&ns_node, config_write_ns);
719 install_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
720 install_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
721 install_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
722 install_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
723 install_element(L_NS_NODE, &cfg_nse_encaps_cmd);
724 install_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
725 install_element(L_NS_NODE, &cfg_no_nse_cmd);
726 install_element(L_NS_NODE, &cfg_ns_timer_cmd);
727 install_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
728 install_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
729 install_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
730 install_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd);
731 install_element(L_NS_NODE, &cfg_frgre_enable_cmd);
732 install_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
733
734 /* TODO: nsvc/nsei command to reset states or reset/block/unblock nsei/nsvcs */
735
736 return 0;
737}
738
739/*!
740 * \brief gprs_ns2_vty_create parse the vty tree into ns nodes
741 * It has to be in different steps to ensure the bind is created before creating VCs.
742 * \return 0 on success
743 */
744int gprs_ns2_vty_create() {
745 struct ns2_vty_vc *vtyvc;
746 struct gprs_ns2_vc_bind *bind;
747 struct gprs_ns2_nse *nse;
748 struct gprs_ns2_vc *nsvc;
749 struct osmo_sockaddr sockaddr;
750
751 if (!vty_nsi)
752 return -1;
753
754 /* create binds, only support a single bind. either FR or UDP */
755 if (priv.frgre) {
756 /* TODO not yet supported !*/
757 return -1;
758 } else {
759 /* UDP */
760 osmo_sockaddr_str_to_sockaddr(&priv.udp, &sockaddr.u.sas);
Alexander Couzens477ffb02020-10-01 19:05:28 +0200761 if (gprs_ns2_ip_bind(vty_nsi, &sockaddr, priv.dscp, &bind)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200762 /* 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 */
Daniel Willmann4fb27a82020-09-25 15:39:46 +0200828void gprs_ns2_vty_force_vc_mode(bool force, enum gprs_ns2_vc_mode mode, const char *reason)
Alexander Couzens6a161492020-07-12 13:45:50 +0200829{
830 priv.force_vc_mode = force;
831
832 if (force) {
833 priv.vc_mode = mode;
834 priv.force_vc_mode_reason = reason;
835 }
836}