blob: 97becf642e041561b08933dd32f04a8695a11350 [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>
Alexander Couzens841817e2020-11-19 00:41:29 +010034#include <net/if.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020035
36#include <osmocom/core/msgb.h>
37#include <osmocom/core/byteswap.h>
Daniel Willmanndbab7142020-11-18 14:19:56 +010038#include <osmocom/core/fsm.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020039#include <osmocom/core/talloc.h>
40#include <osmocom/core/select.h>
41#include <osmocom/core/rate_ctr.h>
42#include <osmocom/core/socket.h>
43#include <osmocom/core/sockaddr_str.h>
44#include <osmocom/core/linuxlist.h>
45#include <osmocom/core/socket.h>
Alexander Couzens841817e2020-11-19 00:41:29 +010046#include <osmocom/gprs/frame_relay.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020047#include <osmocom/gprs/gprs_ns2.h>
48#include <osmocom/gsm/tlv.h>
49#include <osmocom/vty/vty.h>
50#include <osmocom/vty/command.h>
51#include <osmocom/vty/logging.h>
52#include <osmocom/vty/telnet_interface.h>
53#include <osmocom/vty/misc.h>
54
55#include "gprs_ns2_internal.h"
56
Daniel Willmanncb3e9b52020-12-02 15:50:22 +010057#define SHOW_NS_STR "Display information about the NS protocol\n"
58
Alexander Couzens6a161492020-07-12 13:45:50 +020059struct ns2_vty_priv {
60 /* global listen */
61 struct osmo_sockaddr_str udp;
62 struct osmo_sockaddr_str frgreaddr;
63 int dscp;
64 enum gprs_ns2_vc_mode vc_mode;
65 /* force vc mode if another configuration forces
66 * the vc mode. E.g. SNS configuration */
67 bool force_vc_mode;
Daniel Willmann4fb27a82020-09-25 15:39:46 +020068 const char *force_vc_mode_reason;
Alexander Couzens6a161492020-07-12 13:45:50 +020069 bool frgre;
70
71 struct llist_head vtyvc;
72};
73
74struct ns2_vty_vc {
75 struct llist_head list;
76
77 struct osmo_sockaddr_str remote;
Alexander Couzens24a14ac2020-11-19 02:34:49 +010078 enum gprs_ns2_ll ll;
Alexander Couzens6a161492020-07-12 13:45:50 +020079
80 /* old vty code doesnt support multiple NSVCI per NSEI */
81 uint16_t nsei;
82 uint16_t nsvci;
83 uint16_t frdlci;
84
Alexander Couzens841817e2020-11-19 00:41:29 +010085 struct {
86 enum osmo_fr_role role;
87 } fr;
88
89 char netif[IF_NAMESIZE];
90
Alexander Couzens6a161492020-07-12 13:45:50 +020091 bool remote_end_is_sgsn;
92 bool configured;
93};
94
95static struct gprs_ns2_inst *vty_nsi = NULL;
96static struct ns2_vty_priv priv;
Alexander Couzens841817e2020-11-19 00:41:29 +010097static struct osmo_fr_network *vty_fr_network = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +020098
99/* FIXME: this should go to some common file as it is copied
100 * in vty_interface.c of the BSC */
101static const struct value_string gprs_ns_timer_strs[] = {
102 { 0, "tns-block" },
103 { 1, "tns-block-retries" },
104 { 2, "tns-reset" },
105 { 3, "tns-reset-retries" },
106 { 4, "tns-test" },
107 { 5, "tns-alive" },
108 { 6, "tns-alive-retries" },
109 { 7, "tsns-prov" },
110 { 0, NULL }
111};
112
113static void log_set_nsvc_filter(struct log_target *target,
114 struct gprs_ns2_vc *nsvc)
115{
116 if (nsvc) {
117 target->filter_map |= (1 << LOG_FLT_GB_NSVC);
118 target->filter_data[LOG_FLT_GB_NSVC] = nsvc;
119 } else if (target->filter_data[LOG_FLT_GB_NSVC]) {
120 target->filter_map = ~(1 << LOG_FLT_GB_NSVC);
121 target->filter_data[LOG_FLT_GB_NSVC] = NULL;
122 }
123}
124
125static struct cmd_node ns_node = {
126 L_NS_NODE,
127 "%s(config-ns)# ",
128 1,
129};
130
Harald Welte6d4db232020-11-25 19:33:42 +0100131static struct ns2_vty_vc *vtyvc_alloc(uint16_t nsei) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200132 struct ns2_vty_vc *vtyvc = talloc_zero(vty_nsi, struct ns2_vty_vc);
133 if (!vtyvc)
134 return vtyvc;
135
136 vtyvc->nsei = nsei;
137
138 llist_add(&vtyvc->list, &priv.vtyvc);
139
140 return vtyvc;
141}
142
143static void ns2_vc_free(struct ns2_vty_vc *vtyvc) {
144 if (!vtyvc)
145 return;
146
147 llist_del(&vtyvc->list);
148 talloc_free(vtyvc);
149}
150
Harald Welte6d4db232020-11-25 19:33:42 +0100151static struct ns2_vty_vc *vtyvc_by_nsei(uint16_t nsei, bool alloc_missing) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200152 struct ns2_vty_vc *vtyvc;
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200153
Alexander Couzens6a161492020-07-12 13:45:50 +0200154 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
Harald Welte6d4db232020-11-25 19:33:42 +0100155 if (vtyvc->nsei == nsei)
Alexander Couzens6a161492020-07-12 13:45:50 +0200156 return vtyvc;
157 }
158
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200159 if (!alloc_missing)
160 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200161
Harald Welte6d4db232020-11-25 19:33:42 +0100162 vtyvc = vtyvc_alloc(nsei);
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200163 if (!vtyvc)
164 return vtyvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200165
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200166 vtyvc->nsei = nsei;
167 return vtyvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200168}
169
170static int config_write_ns(struct vty *vty)
171{
172 struct ns2_vty_vc *vtyvc;
173 unsigned int i;
174 struct osmo_sockaddr_str sockstr;
175
176 vty_out(vty, "ns%s", VTY_NEWLINE);
177
178 /* global configuration must be written first, as some of it may be
179 * relevant when creating the NSE/NSVC later below */
180
181 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
182 priv.frgre ? 1 : 0, VTY_NEWLINE);
183
184 if (priv.frgre) {
185 if (strlen(priv.frgreaddr.ip)) {
186 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
187 sockstr.ip, VTY_NEWLINE);
188 }
189 } else {
190 if (strlen(priv.udp.ip)) {
191 vty_out(vty, " encapsulation udp local-ip %s%s",
192 priv.udp.ip, VTY_NEWLINE);
193 }
194
195 if (priv.udp.port)
196 vty_out(vty, " encapsulation udp local-port %u%s",
197 priv.udp.port, VTY_NEWLINE);
198 }
199
200 if (priv.dscp)
201 vty_out(vty, " encapsulation udp dscp %d%s",
202 priv.dscp, VTY_NEWLINE);
203
204 vty_out(vty, " encapsulation udp use-reset-block-unblock %s%s",
205 priv.vc_mode == NS2_VC_MODE_BLOCKRESET ? "enabled" : "disabled", VTY_NEWLINE);
206
207 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
208 vty_out(vty, " nse %u nsvci %u%s",
209 vtyvc->nsei, vtyvc->nsvci, VTY_NEWLINE);
210
Harald Welte6d4db232020-11-25 19:33:42 +0100211 vty_out(vty, " nse %u remote-role %s%s",
212 vtyvc->nsei, vtyvc->remote_end_is_sgsn ? "sgsn" : "bss",
213 VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200214
215 switch (vtyvc->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100216 case GPRS_NS2_LL_UDP:
Harald Welte6d4db232020-11-25 19:33:42 +0100217 vty_out(vty, " nse %u encapsulation udp%s", vtyvc->nsei, VTY_NEWLINE);
218 vty_out(vty, " nse %u remote-ip %s%s",
219 vtyvc->nsei,
220 vtyvc->remote.ip,
Alexander Couzens6a161492020-07-12 13:45:50 +0200221 VTY_NEWLINE);
Harald Welte6d4db232020-11-25 19:33:42 +0100222 vty_out(vty, " nse %u remote-port %u%s",
223 vtyvc->nsei, vtyvc->remote.port,
224 VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200225 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100226 case GPRS_NS2_LL_FR_GRE:
Harald Welte6d4db232020-11-25 19:33:42 +0100227 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
228 vtyvc->nsei, VTY_NEWLINE);
229 vty_out(vty, " nse %u remote-ip %s%s",
230 vtyvc->nsei,
231 vtyvc->remote.ip,
232 VTY_NEWLINE);
233 vty_out(vty, " nse %u fr-dlci %u%s",
234 vtyvc->nsei, vtyvc->frdlci,
235 VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200236 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100237 case GPRS_NS2_LL_FR:
Harald Welte6d4db232020-11-25 19:33:42 +0100238 vty_out(vty, " nse %u fr %s dlci %u%s",
239 vtyvc->nsei, vtyvc->netif, vtyvc->frdlci,
240 VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100241 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200242 default:
243 break;
244 }
245 }
246
247 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
248 vty_out(vty, " timer %s %u%s",
249 get_value_string(gprs_ns_timer_strs, i),
250 vty_nsi->timeout[i], VTY_NEWLINE);
251
252 return CMD_SUCCESS;
253}
254
255DEFUN(cfg_ns, cfg_ns_cmd,
256 "ns",
257 "Configure the GPRS Network Service")
258{
259 vty->node = L_NS_NODE;
260 return CMD_SUCCESS;
261}
262
263static void dump_nsvc(struct vty *vty, struct gprs_ns2_vc *nsvc, bool stats)
264{
Harald Weltedc2d0802020-12-01 18:17:28 +0100265 char nsvci_str[32];
266
267 if (nsvc->nsvci_is_valid)
268 snprintf(nsvci_str, sizeof(nsvci_str), "%05u", nsvc->nsvci);
269 else
270 snprintf(nsvci_str, sizeof(nsvci_str), "none");
271
272 vty_out(vty, " NSVCI %s: %s %s data_weight=%u sig_weight=%u %s%s", nsvci_str,
273 osmo_fsm_inst_state_name(nsvc->fi),
274 nsvc->persistent ? "PERSIST" : "DYNAMIC",
275 nsvc->data_weight, nsvc->sig_weight,
276 gprs_ns2_ll_str(nsvc), VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200277
278 if (stats) {
Harald Welte7aa60992020-12-01 17:53:17 +0100279 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
280 vty_out_stat_item_group(vty, " ", nsvc->statg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 }
282}
283
284static void dump_nse(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats, bool persistent_only)
285{
286 struct gprs_ns2_vc *nsvc;
287
Harald Welte0ff12ad2020-12-01 17:51:07 +0100288 vty_out(vty, "NSEI %05u: %s, %s%s", nse->nsei, gprs_ns2_lltype_str(nse->ll),
289 nse->alive ? "ALIVE" : "DEAD", VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200290
291 gprs_ns2_sns_dump_vty(vty, nse, stats);
292 llist_for_each_entry(nsvc, &nse->nsvc, list) {
293 if (persistent_only) {
294 if (nsvc->persistent)
295 dump_nsvc(vty, nsvc, stats);
296 } else {
297 dump_nsvc(vty, nsvc, stats);
298 }
299 }
300}
301
Alexander Couzens22f34712020-10-02 02:34:39 +0200302static void dump_bind(struct vty *vty, const struct gprs_ns2_vc_bind *bind, bool stats)
303{
304 if (bind->dump_vty)
305 bind->dump_vty(bind, vty, stats);
306}
307
Harald Welte2fce19a2020-12-01 17:52:55 +0100308static void dump_ns_bind(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats)
Alexander Couzens6a161492020-07-12 13:45:50 +0200309{
Alexander Couzens22f34712020-10-02 02:34:39 +0200310 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200311
Alexander Couzens22f34712020-10-02 02:34:39 +0200312 llist_for_each_entry(bind, &nsi->binding, list) {
313 dump_bind(vty, bind, stats);
314 }
Harald Welte2fce19a2020-12-01 17:52:55 +0100315}
316
317
318static void dump_ns_entities(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats, bool persistent_only)
319{
320 struct gprs_ns2_nse *nse;
Alexander Couzens22f34712020-10-02 02:34:39 +0200321
Alexander Couzens6a161492020-07-12 13:45:50 +0200322 llist_for_each_entry(nse, &nsi->nse, list) {
323 dump_nse(vty, nse, stats, persistent_only);
Alexander Couzens6a161492020-07-12 13:45:50 +0200324 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200325}
326
Harald Welte2fce19a2020-12-01 17:52:55 +0100327DEFUN(show_ns_binds, show_ns_binds_cmd, "show ns binds [stats]",
Daniel Willmanncb3e9b52020-12-02 15:50:22 +0100328 SHOW_STR SHOW_NS_STR
Harald Welte2fce19a2020-12-01 17:52:55 +0100329 "Display information about the NS protocol binds\n"
330 "Include statistic\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200331{
Harald Welte2fce19a2020-12-01 17:52:55 +0100332 bool stats = false;
333 if (argc > 0)
334 stats = true;
335
336 dump_ns_bind(vty, vty_nsi, stats);
Alexander Couzens6a161492020-07-12 13:45:50 +0200337 return CMD_SUCCESS;
338}
339
Harald Welte2fce19a2020-12-01 17:52:55 +0100340DEFUN(show_ns_entities, show_ns_entities_cmd, "show ns entities [stats]",
Daniel Willmanncb3e9b52020-12-02 15:50:22 +0100341 SHOW_STR SHOW_NS_STR
Harald Welte2fce19a2020-12-01 17:52:55 +0100342 "Display information about the NS protocol entities (NSEs)\n"
Alexander Couzens6a161492020-07-12 13:45:50 +0200343 "Include statistics\n")
344{
Harald Welte2fce19a2020-12-01 17:52:55 +0100345 bool stats = false;
346 if (argc > 0)
347 stats = true;
348
349 dump_ns_entities(vty, vty_nsi, stats, false);
Alexander Couzens6a161492020-07-12 13:45:50 +0200350 return CMD_SUCCESS;
351}
352
353DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
Daniel Willmanncb3e9b52020-12-02 15:50:22 +0100354 SHOW_STR SHOW_NS_STR
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 "Show only persistent NS\n")
356{
Harald Welte2fce19a2020-12-01 17:52:55 +0100357 dump_ns_entities(vty, vty_nsi, true, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200358 return CMD_SUCCESS;
359}
360
361DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
Daniel Willmanncb3e9b52020-12-02 15:50:22 +0100362 SHOW_STR SHOW_NS_STR
Alexander Couzens6a161492020-07-12 13:45:50 +0200363 "Select one NSE by its NSE Identifier\n"
364 "Select one NSE by its NS-VC Identifier\n"
365 "The Identifier of selected type\n"
366 "Include Statistics\n")
367{
368 struct gprs_ns2_inst *nsi = vty_nsi;
369 struct gprs_ns2_nse *nse;
370 struct gprs_ns2_vc *nsvc;
371 uint16_t id = atoi(argv[1]);
372 bool show_stats = false;
373
374 if (argc >= 3)
375 show_stats = true;
376
377 if (!strcmp(argv[0], "nsei")) {
378 nse = gprs_ns2_nse_by_nsei(nsi, id);
379 if (!nse) {
380 return CMD_WARNING;
381 }
382
383 dump_nse(vty, nse, show_stats, false);
384 } else {
385 nsvc = gprs_ns2_nsvc_by_nsvci(nsi, id);
386
387 if (!nsvc) {
388 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
389 return CMD_WARNING;
390 }
391
392 dump_nsvc(vty, nsvc, show_stats);
393 }
394
395 return CMD_SUCCESS;
396}
397
Daniel Willmanndbab7142020-11-18 14:19:56 +0100398static int nsvc_force_unconf_cb(struct gprs_ns2_vc *nsvc, void *ctx)
399{
400 gprs_ns2_vc_force_unconfigured(nsvc);
401 return 0;
402}
403
404DEFUN_HIDDEN(nsvc_force_unconf, nsvc_force_unconf_cmd,
405 "nsvc nsei <0-65535> force-unconfigured",
406 "NS Virtual Connection\n"
407 "The NSEI\n"
408 "Reset the NSVCs back to initial state\n"
409 )
410{
411 struct gprs_ns2_inst *nsi = vty_nsi;
412 struct gprs_ns2_nse *nse;
413
414 uint16_t id = atoi(argv[0]);
415
416 nse = gprs_ns2_nse_by_nsei(nsi, id);
417 if (!nse) {
418 vty_out(vty, "Could not find NSE for NSEI %u%s", id, VTY_NEWLINE);
419 return CMD_WARNING;
420 }
421
422 /* Perform the operation for all nsvc */
423 gprs_ns2_nse_foreach_nsvc(nse, nsvc_force_unconf_cb, NULL);
424
425 return CMD_SUCCESS;
426}
427
Alexander Couzens6a161492020-07-12 13:45:50 +0200428#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
429
Alexander Couzens841817e2020-11-19 00:41:29 +0100430DEFUN(cfg_nse_fr, cfg_nse_fr_cmd,
431 "nse <0-65535> nsvci <0-65535> (fr|frnet) NETIF dlci <0-1023>",
432 NSE_CMD_STR
433 "NS Virtual Connection\n"
434 "NS Virtual Connection ID (NSVCI)\n"
Harald Welte92049192020-11-25 20:56:06 +0100435 "Frame Relay User-Side\n"
436 "Frame Relay Network-Side\n"
Alexander Couzens841817e2020-11-19 00:41:29 +0100437 IFNAME_STR
438 "Data Link connection identifier\n"
439 "Data Link connection identifier\n"
440 )
441{
442 struct ns2_vty_vc *vtyvc;
443
444 uint16_t nsei = atoi(argv[0]);
445 uint16_t nsvci = atoi(argv[1]);
446 const char *role = argv[2];
447 const char *name = argv[3];
448 uint16_t dlci = atoi(argv[4]);
449
Harald Welte6d4db232020-11-25 19:33:42 +0100450 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens841817e2020-11-19 00:41:29 +0100451 if (!vtyvc) {
452 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
453 return CMD_WARNING;
454 }
455
456 if (!strcmp(role, "fr"))
457 vtyvc->fr.role = FR_ROLE_USER_EQUIPMENT;
458 else if (!strcmp(role, "frnet"))
459 vtyvc->fr.role = FR_ROLE_NETWORK_EQUIPMENT;
460
461 osmo_strlcpy(vtyvc->netif, name, sizeof(vtyvc->netif));
462 vtyvc->frdlci = dlci;
463 vtyvc->nsvci = nsvci;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100464 vtyvc->ll = GPRS_NS2_LL_FR;
Alexander Couzens841817e2020-11-19 00:41:29 +0100465
466 return CMD_SUCCESS;
467}
468
Alexander Couzens6a161492020-07-12 13:45:50 +0200469DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
470 "nse <0-65535> nsvci <0-65535>",
471 NSE_CMD_STR
472 "NS Virtual Connection\n"
473 "NS Virtual Connection ID (NSVCI)\n"
474 )
475{
476 struct ns2_vty_vc *vtyvc;
477
478 uint16_t nsei = atoi(argv[0]);
479 uint16_t nsvci = atoi(argv[1]);
480
Harald Welte6d4db232020-11-25 19:33:42 +0100481 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200482 if (!vtyvc) {
483 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
484 return CMD_WARNING;
485 }
486
487 vtyvc->nsvci = nsvci;
488
489 return CMD_SUCCESS;
490}
491
492DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100493 "nse <0-65535> remote-ip " VTY_IPV46_CMD,
Alexander Couzens6a161492020-07-12 13:45:50 +0200494 NSE_CMD_STR
495 "Remote IP Address\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200496 "Remote IPv4 Address\n"
497 "Remote IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200498{
499 uint16_t nsei = atoi(argv[0]);
500 struct ns2_vty_vc *vtyvc;
501
Harald Welte6d4db232020-11-25 19:33:42 +0100502 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200503 if (!vtyvc) {
504 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
505 return CMD_WARNING;
506 }
507
Harald Welte6d4db232020-11-25 19:33:42 +0100508 osmo_sockaddr_str_from_str2(&vtyvc->remote, argv[1]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200509
510 return CMD_SUCCESS;
511}
512
513DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100514 "nse <0-65535> remote-port <0-65535>",
Alexander Couzens6a161492020-07-12 13:45:50 +0200515 NSE_CMD_STR
516 "Remote UDP Port\n"
517 "Remote UDP Port Number\n")
518{
519 uint16_t nsei = atoi(argv[0]);
Harald Welte6d4db232020-11-25 19:33:42 +0100520 uint16_t port = atoi(argv[1]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200521 struct ns2_vty_vc *vtyvc;
522
Harald Welte6d4db232020-11-25 19:33:42 +0100523 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200524 if (!vtyvc) {
525 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
526 return CMD_WARNING;
527 }
528
529 vtyvc->remote.port = port;
530
531 return CMD_SUCCESS;
532}
533
534DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
Alexander Couzens841817e2020-11-19 00:41:29 +0100535 "nse <0-65535> nsvci <0-65535> fr-dlci <16-1007>",
Alexander Couzens6a161492020-07-12 13:45:50 +0200536 NSE_CMD_STR
Harald Welte92049192020-11-25 20:56:06 +0100537 "NS Virtual Connection\n"
538 "NS Virtual Connection ID (NSVCI)\n"
Alexander Couzens6a161492020-07-12 13:45:50 +0200539 "Frame Relay DLCI\n"
540 "Frame Relay DLCI Number\n")
541{
542 uint16_t nsei = atoi(argv[0]);
Alexander Couzens841817e2020-11-19 00:41:29 +0100543 uint16_t nsvci = atoi(argv[1]);
544 uint16_t dlci = atoi(argv[2]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200545 struct ns2_vty_vc *vtyvc;
546
Harald Welte6d4db232020-11-25 19:33:42 +0100547 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200548 if (!vtyvc) {
549 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
550 return CMD_WARNING;
551 }
552
Alexander Couzens6a161492020-07-12 13:45:50 +0200553 vtyvc->frdlci = dlci;
Harald Welte6d4db232020-11-25 19:33:42 +0100554 vtyvc->nsvci = nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200555
556 return CMD_SUCCESS;
557}
558
559DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100560 "nse <0-65535> encapsulation (udp|framerelay-gre)",
Alexander Couzens6a161492020-07-12 13:45:50 +0200561 NSE_CMD_STR
562 "Encapsulation for NS\n"
563 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
564{
565 uint16_t nsei = atoi(argv[0]);
566 struct ns2_vty_vc *vtyvc;
567
Harald Welte6d4db232020-11-25 19:33:42 +0100568 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200569 if (!vtyvc) {
570 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
571 return CMD_WARNING;
572 }
573
Harald Welte6d4db232020-11-25 19:33:42 +0100574 if (!strcmp(argv[1], "udp"))
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100575 vtyvc->ll = GPRS_NS2_LL_UDP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200576 else
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100577 vtyvc->ll = GPRS_NS2_LL_FR_GRE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200578
579 return CMD_SUCCESS;
580}
581
582DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100583 "nse <0-65535> remote-role (sgsn|bss)",
Alexander Couzens6a161492020-07-12 13:45:50 +0200584 NSE_CMD_STR
585 "Remote NSE Role\n"
586 "Remote Peer is SGSN\n"
587 "Remote Peer is BSS\n")
588{
589 uint16_t nsei = atoi(argv[0]);
590 struct ns2_vty_vc *vtyvc;
591
Harald Welte6d4db232020-11-25 19:33:42 +0100592 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200593 if (!vtyvc) {
594 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
595 return CMD_WARNING;
596 }
597
Harald Welte6d4db232020-11-25 19:33:42 +0100598 if (!strcmp(argv[1], "sgsn"))
Alexander Couzens6a161492020-07-12 13:45:50 +0200599 vtyvc->remote_end_is_sgsn = 1;
600 else
601 vtyvc->remote_end_is_sgsn = 0;
602
603 return CMD_SUCCESS;
604}
605
606DEFUN(cfg_no_nse, cfg_no_nse_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100607 "no nse <0-65535>",
Alexander Couzens6a161492020-07-12 13:45:50 +0200608 "Delete Persistent NS Entity\n"
609 "Delete " NSE_CMD_STR)
610{
611 uint16_t nsei = atoi(argv[0]);
612 struct ns2_vty_vc *vtyvc;
613
Harald Welte6d4db232020-11-25 19:33:42 +0100614 vtyvc = vtyvc_by_nsei(nsei, false);
Alexander Couzens6a161492020-07-12 13:45:50 +0200615 if (!vtyvc) {
616 vty_out(vty, "The NSE %d does not exists.%s", nsei, VTY_NEWLINE);
617 return CMD_WARNING;
618 }
619
620 ns2_vc_free(vtyvc);
621
622 return CMD_SUCCESS;
623}
624
625DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
626 "timer " NS_TIMERS " <0-65535>",
627 "Network Service Timer\n"
628 NS_TIMERS_HELP "Timer Value\n")
629{
630 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
631 int val = atoi(argv[1]);
632
633 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
634 return CMD_WARNING;
635
636 vty_nsi->timeout[idx] = val;
637
638 return CMD_SUCCESS;
639}
640
641#define ENCAPS_STR "NS encapsulation options\n"
642
643DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
644 "encapsulation udp local-ip " VTY_IPV46_CMD,
645 ENCAPS_STR "NS over UDP Encapsulation\n"
646 "Set the IP address on which we listen for NS/UDP\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200647 "IPv4 Address\n"
648 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200649{
650 osmo_sockaddr_str_from_str2(&priv.udp, argv[0]);
651
652 return CMD_SUCCESS;
653}
654
655DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
656 "encapsulation udp local-port <0-65535>",
657 ENCAPS_STR "NS over UDP Encapsulation\n"
658 "Set the UDP port on which we listen for NS/UDP\n"
659 "UDP port number\n")
660{
661 unsigned int port = atoi(argv[0]);
662
663 priv.udp.port = port;
664
665 return CMD_SUCCESS;
666}
667
668DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
669 "encapsulation udp dscp <0-255>",
670 ENCAPS_STR "NS over UDP Encapsulation\n"
671 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
672{
673 int dscp = atoi(argv[0]);
674 struct gprs_ns2_vc_bind *bind;
675
676 priv.dscp = dscp;
677
678 llist_for_each_entry(bind, &vty_nsi->binding, list) {
679 if (gprs_ns2_is_ip_bind(bind))
680 gprs_ns2_ip_bind_set_dscp(bind, dscp);
681 }
682
683 return CMD_SUCCESS;
684}
685
686DEFUN(cfg_nsip_res_block_unblock, cfg_nsip_res_block_unblock_cmd,
687 "encapsulation udp use-reset-block-unblock (enabled|disabled)",
688 ENCAPS_STR "NS over UDP Encapsulation\n"
689 "Use NS-{RESET,BLOCK,UNBLOCK} procedures in violation of 3GPP TS 48.016\n"
690 "Enable NS-{RESET,BLOCK,UNBLOCK}\n"
691 "Disable NS-{RESET,BLOCK,UNBLOCK}\n")
692{
693 enum gprs_ns2_vc_mode vc_mode;
694 struct gprs_ns2_vc_bind *bind;
695
696 if (!strcmp(argv[0], "enabled"))
697 vc_mode = NS2_VC_MODE_BLOCKRESET;
698 else
699 vc_mode = NS2_VC_MODE_ALIVE;
700
701 if (priv.force_vc_mode) {
702 if (priv.vc_mode != vc_mode)
703 {
704 vty_out(vty, "Ignoring use-reset-block because it's already set by %s.%s",
705 priv.force_vc_mode_reason, VTY_NEWLINE);
706 return CMD_WARNING;
707 }
708
709 return CMD_SUCCESS;
710 }
711
712 priv.vc_mode = vc_mode;
713
714 llist_for_each_entry(bind, &vty_nsi->binding, list) {
715 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
716 }
717
718 return CMD_SUCCESS;
719}
720
721DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
722 "encapsulation framerelay-gre local-ip " VTY_IPV46_CMD,
723 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
724 "Set the IP address on which we listen for NS/FR/GRE\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200725 "IPv4 Address\n"
726 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200727{
728 osmo_sockaddr_str_from_str2(&priv.frgreaddr, argv[0]);
729
730 return CMD_SUCCESS;
731}
732
733DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd,
734 "encapsulation framerelay-gre enabled (1|0)",
735 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
736 "Enable or disable Frame Relay over GRE\n"
737 "Enable\n" "Disable\n")
738{
739 int enabled = atoi(argv[0]);
740
741 priv.frgre = enabled;
742
743 return CMD_SUCCESS;
744}
745
746/* TODO: allow vty to reset/block/unblock nsvc/nsei */
747
748/* TODO: add filter for NSEI as ns1 code does */
749/* TODO: add filter for single connection by description */
750DEFUN(logging_fltr_nsvc,
751 logging_fltr_nsvc_cmd,
752 "logging filter nsvc nsvci <0-65535>",
753 LOGGING_STR FILTER_STR
754 "Filter based on NS Virtual Connection\n"
755 "Identify NS-VC by NSVCI\n"
756 "Numeric identifier\n")
757{
758 struct log_target *tgt;
759 struct gprs_ns2_vc *nsvc;
760 uint16_t id = atoi(argv[1]);
761
762 log_tgt_mutex_lock();
763 tgt = osmo_log_vty2tgt(vty);
764 if (!tgt) {
765 log_tgt_mutex_unlock();
766 return CMD_WARNING;
767 }
768
769 nsvc = gprs_ns2_nsvc_by_nsvci(vty_nsi, id);
770 if (!nsvc) {
771 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
772 log_tgt_mutex_unlock();
773 return CMD_WARNING;
774 }
775
776 log_set_nsvc_filter(tgt, nsvc);
777 log_tgt_mutex_unlock();
778 return CMD_SUCCESS;
779}
780
Alexander Couzens1fac6f72020-10-01 19:08:38 +0200781/**
782 * gprs_ns2_vty_init initialize the vty
783 * \param[inout] nsi
784 * \param[in] default_bind set the default address to bind to. Can be NULL.
785 * \return 0 on success
786 */
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700787int gprs_ns2_vty_init(struct gprs_ns2_inst *nsi,
788 const struct osmo_sockaddr_str *default_bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200789{
790 static bool vty_elements_installed = false;
791
792 vty_nsi = nsi;
793 memset(&priv, 0, sizeof(struct ns2_vty_priv));
794 INIT_LLIST_HEAD(&priv.vtyvc);
795 priv.vc_mode = NS2_VC_MODE_BLOCKRESET;
Alexander Couzens1fac6f72020-10-01 19:08:38 +0200796 if (default_bind)
797 memcpy(&priv.udp, default_bind, sizeof(*default_bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200798
799 /* Regression test code may call this function repeatedly, so make sure
800 * that VTY elements are not duplicated, which would assert. */
801 if (vty_elements_installed)
802 return 0;
803 vty_elements_installed = true;
804
Harald Welte2fce19a2020-12-01 17:52:55 +0100805 install_lib_element_ve(&show_ns_binds_cmd);
806 install_lib_element_ve(&show_ns_entities_cmd);
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700807 install_lib_element_ve(&show_ns_pers_cmd);
808 install_lib_element_ve(&show_nse_cmd);
809 install_lib_element_ve(&logging_fltr_nsvc_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200810
Daniel Willmanndbab7142020-11-18 14:19:56 +0100811 install_lib_element(ENABLE_NODE, &nsvc_force_unconf_cmd);
812
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700813 install_lib_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200814
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700815 install_lib_element(CONFIG_NODE, &cfg_ns_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200816 install_node(&ns_node, config_write_ns);
Alexander Couzens841817e2020-11-19 00:41:29 +0100817 install_lib_element(L_NS_NODE, &cfg_nse_fr_cmd);
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700818 install_lib_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
819 install_lib_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
820 install_lib_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
821 install_lib_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
822 install_lib_element(L_NS_NODE, &cfg_nse_encaps_cmd);
823 install_lib_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
824 install_lib_element(L_NS_NODE, &cfg_no_nse_cmd);
825 install_lib_element(L_NS_NODE, &cfg_ns_timer_cmd);
826 install_lib_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
827 install_lib_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
828 install_lib_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
829 install_lib_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd);
830 install_lib_element(L_NS_NODE, &cfg_frgre_enable_cmd);
831 install_lib_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200832
833 /* TODO: nsvc/nsei command to reset states or reset/block/unblock nsei/nsvcs */
834
835 return 0;
836}
837
838/*!
839 * \brief gprs_ns2_vty_create parse the vty tree into ns nodes
840 * It has to be in different steps to ensure the bind is created before creating VCs.
841 * \return 0 on success
842 */
843int gprs_ns2_vty_create() {
844 struct ns2_vty_vc *vtyvc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100845 struct gprs_ns2_vc_bind *bind, *fr;
Alexander Couzens6a161492020-07-12 13:45:50 +0200846 struct gprs_ns2_nse *nse;
847 struct gprs_ns2_vc *nsvc;
848 struct osmo_sockaddr sockaddr;
Alexander Couzens841817e2020-11-19 00:41:29 +0100849 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200850
851 if (!vty_nsi)
852 return -1;
853
854 /* create binds, only support a single bind. either FR or UDP */
855 if (priv.frgre) {
856 /* TODO not yet supported !*/
857 return -1;
858 } else {
859 /* UDP */
860 osmo_sockaddr_str_to_sockaddr(&priv.udp, &sockaddr.u.sas);
Alexander Couzens477ffb02020-10-01 19:05:28 +0200861 if (gprs_ns2_ip_bind(vty_nsi, &sockaddr, priv.dscp, &bind)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200862 /* TODO: could not bind on the specific address */
863 return -1;
864 }
865 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
866 }
867
868 /* create vcs */
869 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100870 /* validate settings */
871 switch (vtyvc->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100872 case GPRS_NS2_LL_UDP:
Alexander Couzens841817e2020-11-19 00:41:29 +0100873 if (strlen(vtyvc->remote.ip) == 0) {
874 /* Invalid IP for VC */
875 continue;
876 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200877
Alexander Couzens841817e2020-11-19 00:41:29 +0100878 if (!vtyvc->remote.port) {
879 /* Invalid port for VC */
880 continue;
881 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200882
Alexander Couzens841817e2020-11-19 00:41:29 +0100883 if (osmo_sockaddr_str_to_sockaddr(&vtyvc->remote, &sockaddr.u.sas)) {
884 /* Invalid sockaddr for VC */
885 continue;
886 }
887 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100888 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100889 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100890 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200891 continue;
892 }
893
894 nse = gprs_ns2_nse_by_nsei(vty_nsi, vtyvc->nsei);
895 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100896 nse = gprs_ns2_create_nse(vty_nsi, vtyvc->nsei, vtyvc->ll);
Alexander Couzens6a161492020-07-12 13:45:50 +0200897 if (!nse) {
898 /* Could not create NSE for VTY */
899 continue;
900 }
901 }
902 nse->persistent = true;
903
Alexander Couzens841817e2020-11-19 00:41:29 +0100904 switch (vtyvc->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100905 case GPRS_NS2_LL_UDP:
Alexander Couzens841817e2020-11-19 00:41:29 +0100906 nsvc = gprs_ns2_ip_connect(bind,
907 &sockaddr,
908 nse,
909 vtyvc->nsvci);
910 if (!nsvc) {
911 /* Could not create NSVC, connect failed */
912 continue;
913 }
914 nsvc->persistent = true;
915 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100916 case GPRS_NS2_LL_FR: {
Alexander Couzens841817e2020-11-19 00:41:29 +0100917 if (vty_fr_network == NULL) {
918 /* TODO: add a switch for BSS/SGSN/gbproxy */
919 vty_fr_network = osmo_fr_network_alloc(vty_nsi);
920 }
921 fr = gprs_ns2_fr_bind_by_netif(
922 vty_nsi,
923 vtyvc->netif);
924 if (!fr) {
925 rc = gprs_ns2_fr_bind(vty_nsi, vtyvc->netif, vty_fr_network, vtyvc->fr.role, &fr);
926 if (rc < 0) {
927 LOGP(DLNS, LOGL_ERROR, "Can not create fr bind on device %s err: %d\n", vtyvc->netif, rc);
928 return rc;
929 }
930 }
931
932 nsvc = gprs_ns2_fr_connect(fr, vtyvc->nsei, vtyvc->nsvci, vtyvc->frdlci);
933 if (!nsvc) {
934 /* Could not create NSVC, connect failed */
935 continue;
936 }
937 nsvc->persistent = true;
938 break;
939 }
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100940 case GPRS_NS2_LL_FR_GRE:
Alexander Couzensd745a0e2020-10-07 00:50:00 +0200941 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200942 }
943 }
944
945
946 return 0;
947}
948
949/*!
950 * \brief ns2_vty_bind_apply will be called when a new bind is created to apply vty settings
951 * \param bind
952 * \return
953 */
954void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind)
955{
956 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
957}
958
959/*!
960 * \brief ns2_vty_force_vc_mode force a mode and prevents the vty from overwriting it.
961 * \param force if true mode and reason will be set. false to allow modification via vty.
962 * \param mode
963 * \param reason A description shown to the user when a vty command wants to change the mode.
964 */
Daniel Willmann4fb27a82020-09-25 15:39:46 +0200965void gprs_ns2_vty_force_vc_mode(bool force, enum gprs_ns2_vc_mode mode, const char *reason)
Alexander Couzens6a161492020-07-12 13:45:50 +0200966{
967 priv.force_vc_mode = force;
968
969 if (force) {
970 priv.vc_mode = mode;
971 priv.force_vc_mode_reason = reason;
972 }
973}