blob: 8514704304068d5e7f872c8ae0fe09c491564886 [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
57struct ns2_vty_priv {
58 /* global listen */
59 struct osmo_sockaddr_str udp;
60 struct osmo_sockaddr_str frgreaddr;
61 int dscp;
62 enum gprs_ns2_vc_mode vc_mode;
63 /* force vc mode if another configuration forces
64 * the vc mode. E.g. SNS configuration */
65 bool force_vc_mode;
Daniel Willmann4fb27a82020-09-25 15:39:46 +020066 const char *force_vc_mode_reason;
Alexander Couzens6a161492020-07-12 13:45:50 +020067 bool frgre;
68
69 struct llist_head vtyvc;
70};
71
72struct ns2_vty_vc {
73 struct llist_head list;
74
75 struct osmo_sockaddr_str remote;
Alexander Couzens24a14ac2020-11-19 02:34:49 +010076 enum gprs_ns2_ll ll;
Alexander Couzens6a161492020-07-12 13:45:50 +020077
78 /* old vty code doesnt support multiple NSVCI per NSEI */
79 uint16_t nsei;
80 uint16_t nsvci;
81 uint16_t frdlci;
82
Alexander Couzens841817e2020-11-19 00:41:29 +010083 struct {
84 enum osmo_fr_role role;
85 } fr;
86
87 char netif[IF_NAMESIZE];
88
Alexander Couzens6a161492020-07-12 13:45:50 +020089 bool remote_end_is_sgsn;
90 bool configured;
91};
92
93static struct gprs_ns2_inst *vty_nsi = NULL;
94static struct ns2_vty_priv priv;
Alexander Couzens841817e2020-11-19 00:41:29 +010095static struct osmo_fr_network *vty_fr_network = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +020096
97/* FIXME: this should go to some common file as it is copied
98 * in vty_interface.c of the BSC */
99static const struct value_string gprs_ns_timer_strs[] = {
100 { 0, "tns-block" },
101 { 1, "tns-block-retries" },
102 { 2, "tns-reset" },
103 { 3, "tns-reset-retries" },
104 { 4, "tns-test" },
105 { 5, "tns-alive" },
106 { 6, "tns-alive-retries" },
107 { 7, "tsns-prov" },
108 { 0, NULL }
109};
110
111static void log_set_nsvc_filter(struct log_target *target,
112 struct gprs_ns2_vc *nsvc)
113{
114 if (nsvc) {
115 target->filter_map |= (1 << LOG_FLT_GB_NSVC);
116 target->filter_data[LOG_FLT_GB_NSVC] = nsvc;
117 } else if (target->filter_data[LOG_FLT_GB_NSVC]) {
118 target->filter_map = ~(1 << LOG_FLT_GB_NSVC);
119 target->filter_data[LOG_FLT_GB_NSVC] = NULL;
120 }
121}
122
123static struct cmd_node ns_node = {
124 L_NS_NODE,
125 "%s(config-ns)# ",
126 1,
127};
128
Harald Welte6d4db232020-11-25 19:33:42 +0100129static struct ns2_vty_vc *vtyvc_alloc(uint16_t nsei) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200130 struct ns2_vty_vc *vtyvc = talloc_zero(vty_nsi, struct ns2_vty_vc);
131 if (!vtyvc)
132 return vtyvc;
133
134 vtyvc->nsei = nsei;
135
136 llist_add(&vtyvc->list, &priv.vtyvc);
137
138 return vtyvc;
139}
140
141static void ns2_vc_free(struct ns2_vty_vc *vtyvc) {
142 if (!vtyvc)
143 return;
144
145 llist_del(&vtyvc->list);
146 talloc_free(vtyvc);
147}
148
Harald Welte6d4db232020-11-25 19:33:42 +0100149static struct ns2_vty_vc *vtyvc_by_nsei(uint16_t nsei, bool alloc_missing) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200150 struct ns2_vty_vc *vtyvc;
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200151
Alexander Couzens6a161492020-07-12 13:45:50 +0200152 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
Harald Welte6d4db232020-11-25 19:33:42 +0100153 if (vtyvc->nsei == nsei)
Alexander Couzens6a161492020-07-12 13:45:50 +0200154 return vtyvc;
155 }
156
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200157 if (!alloc_missing)
158 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200159
Harald Welte6d4db232020-11-25 19:33:42 +0100160 vtyvc = vtyvc_alloc(nsei);
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200161 if (!vtyvc)
162 return vtyvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200163
Alexander Couzens1f0625f2020-09-23 18:22:20 +0200164 vtyvc->nsei = nsei;
165 return vtyvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200166}
167
168static int config_write_ns(struct vty *vty)
169{
170 struct ns2_vty_vc *vtyvc;
171 unsigned int i;
172 struct osmo_sockaddr_str sockstr;
173
174 vty_out(vty, "ns%s", VTY_NEWLINE);
175
176 /* global configuration must be written first, as some of it may be
177 * relevant when creating the NSE/NSVC later below */
178
179 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
180 priv.frgre ? 1 : 0, VTY_NEWLINE);
181
182 if (priv.frgre) {
183 if (strlen(priv.frgreaddr.ip)) {
184 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
185 sockstr.ip, VTY_NEWLINE);
186 }
187 } else {
188 if (strlen(priv.udp.ip)) {
189 vty_out(vty, " encapsulation udp local-ip %s%s",
190 priv.udp.ip, VTY_NEWLINE);
191 }
192
193 if (priv.udp.port)
194 vty_out(vty, " encapsulation udp local-port %u%s",
195 priv.udp.port, VTY_NEWLINE);
196 }
197
198 if (priv.dscp)
199 vty_out(vty, " encapsulation udp dscp %d%s",
200 priv.dscp, VTY_NEWLINE);
201
202 vty_out(vty, " encapsulation udp use-reset-block-unblock %s%s",
203 priv.vc_mode == NS2_VC_MODE_BLOCKRESET ? "enabled" : "disabled", VTY_NEWLINE);
204
205 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
206 vty_out(vty, " nse %u nsvci %u%s",
207 vtyvc->nsei, vtyvc->nsvci, VTY_NEWLINE);
208
Harald Welte6d4db232020-11-25 19:33:42 +0100209 vty_out(vty, " nse %u remote-role %s%s",
210 vtyvc->nsei, vtyvc->remote_end_is_sgsn ? "sgsn" : "bss",
211 VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200212
213 switch (vtyvc->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100214 case GPRS_NS2_LL_UDP:
Harald Welte6d4db232020-11-25 19:33:42 +0100215 vty_out(vty, " nse %u encapsulation udp%s", vtyvc->nsei, VTY_NEWLINE);
216 vty_out(vty, " nse %u remote-ip %s%s",
217 vtyvc->nsei,
218 vtyvc->remote.ip,
Alexander Couzens6a161492020-07-12 13:45:50 +0200219 VTY_NEWLINE);
Harald Welte6d4db232020-11-25 19:33:42 +0100220 vty_out(vty, " nse %u remote-port %u%s",
221 vtyvc->nsei, vtyvc->remote.port,
222 VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200223 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100224 case GPRS_NS2_LL_FR_GRE:
Harald Welte6d4db232020-11-25 19:33:42 +0100225 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
226 vtyvc->nsei, VTY_NEWLINE);
227 vty_out(vty, " nse %u remote-ip %s%s",
228 vtyvc->nsei,
229 vtyvc->remote.ip,
230 VTY_NEWLINE);
231 vty_out(vty, " nse %u fr-dlci %u%s",
232 vtyvc->nsei, vtyvc->frdlci,
233 VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200234 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100235 case GPRS_NS2_LL_FR:
Harald Welte6d4db232020-11-25 19:33:42 +0100236 vty_out(vty, " nse %u fr %s dlci %u%s",
237 vtyvc->nsei, vtyvc->netif, vtyvc->frdlci,
238 VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100239 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200240 default:
241 break;
242 }
243 }
244
245 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
246 vty_out(vty, " timer %s %u%s",
247 get_value_string(gprs_ns_timer_strs, i),
248 vty_nsi->timeout[i], VTY_NEWLINE);
249
250 return CMD_SUCCESS;
251}
252
253DEFUN(cfg_ns, cfg_ns_cmd,
254 "ns",
255 "Configure the GPRS Network Service")
256{
257 vty->node = L_NS_NODE;
258 return CMD_SUCCESS;
259}
260
261static void dump_nsvc(struct vty *vty, struct gprs_ns2_vc *nsvc, bool stats)
262{
Daniel Willmanned1fa012020-11-06 15:40:27 +0100263 vty_out(vty, " %s%s", gprs_ns2_ll_str(nsvc), VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200264
265 if (stats) {
266 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
267 vty_out_stat_item_group(vty, " ", nsvc->statg);
268 }
269}
270
271static void dump_nse(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats, bool persistent_only)
272{
273 struct gprs_ns2_vc *nsvc;
274
Harald Welte0ff12ad2020-12-01 17:51:07 +0100275 vty_out(vty, "NSEI %05u: %s, %s%s", nse->nsei, gprs_ns2_lltype_str(nse->ll),
276 nse->alive ? "ALIVE" : "DEAD", VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200277
278 gprs_ns2_sns_dump_vty(vty, nse, stats);
279 llist_for_each_entry(nsvc, &nse->nsvc, list) {
280 if (persistent_only) {
281 if (nsvc->persistent)
282 dump_nsvc(vty, nsvc, stats);
283 } else {
284 dump_nsvc(vty, nsvc, stats);
285 }
286 }
287}
288
Alexander Couzens22f34712020-10-02 02:34:39 +0200289static void dump_bind(struct vty *vty, const struct gprs_ns2_vc_bind *bind, bool stats)
290{
291 if (bind->dump_vty)
292 bind->dump_vty(bind, vty, stats);
293}
294
Harald Welte2fce19a2020-12-01 17:52:55 +0100295static void dump_ns_bind(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats)
Alexander Couzens6a161492020-07-12 13:45:50 +0200296{
Alexander Couzens22f34712020-10-02 02:34:39 +0200297 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200298
Alexander Couzens22f34712020-10-02 02:34:39 +0200299 llist_for_each_entry(bind, &nsi->binding, list) {
300 dump_bind(vty, bind, stats);
301 }
Harald Welte2fce19a2020-12-01 17:52:55 +0100302}
303
304
305static void dump_ns_entities(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats, bool persistent_only)
306{
307 struct gprs_ns2_nse *nse;
Alexander Couzens22f34712020-10-02 02:34:39 +0200308
Alexander Couzens6a161492020-07-12 13:45:50 +0200309 llist_for_each_entry(nse, &nsi->nse, list) {
310 dump_nse(vty, nse, stats, persistent_only);
Alexander Couzens6a161492020-07-12 13:45:50 +0200311 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200312}
313
Harald Welte2fce19a2020-12-01 17:52:55 +0100314DEFUN(show_ns_binds, show_ns_binds_cmd, "show ns binds [stats]",
315 SHOW_STR
316 "Display information about the NS protocol binds\n"
317 "Include statistic\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200318{
Harald Welte2fce19a2020-12-01 17:52:55 +0100319 bool stats = false;
320 if (argc > 0)
321 stats = true;
322
323 dump_ns_bind(vty, vty_nsi, stats);
Alexander Couzens6a161492020-07-12 13:45:50 +0200324 return CMD_SUCCESS;
325}
326
Harald Welte2fce19a2020-12-01 17:52:55 +0100327DEFUN(show_ns_entities, show_ns_entities_cmd, "show ns entities [stats]",
Alexander Couzens6a161492020-07-12 13:45:50 +0200328 SHOW_STR
Harald Welte2fce19a2020-12-01 17:52:55 +0100329 "Display information about the NS protocol entities (NSEs)\n"
Alexander Couzens6a161492020-07-12 13:45:50 +0200330 "Include statistics\n")
331{
Harald Welte2fce19a2020-12-01 17:52:55 +0100332 bool stats = false;
333 if (argc > 0)
334 stats = true;
335
336 dump_ns_entities(vty, vty_nsi, stats, false);
Alexander Couzens6a161492020-07-12 13:45:50 +0200337 return CMD_SUCCESS;
338}
339
340DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
341 SHOW_STR
342 "Display information about the NS protocol\n"
343 "Show only persistent NS\n")
344{
Harald Welte2fce19a2020-12-01 17:52:55 +0100345 dump_ns_entities(vty, vty_nsi, true, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200346 return CMD_SUCCESS;
347}
348
349DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
350 SHOW_STR "Display information about the NS protocol\n"
351 "Select one NSE by its NSE Identifier\n"
352 "Select one NSE by its NS-VC Identifier\n"
353 "The Identifier of selected type\n"
354 "Include Statistics\n")
355{
356 struct gprs_ns2_inst *nsi = vty_nsi;
357 struct gprs_ns2_nse *nse;
358 struct gprs_ns2_vc *nsvc;
359 uint16_t id = atoi(argv[1]);
360 bool show_stats = false;
361
362 if (argc >= 3)
363 show_stats = true;
364
365 if (!strcmp(argv[0], "nsei")) {
366 nse = gprs_ns2_nse_by_nsei(nsi, id);
367 if (!nse) {
368 return CMD_WARNING;
369 }
370
371 dump_nse(vty, nse, show_stats, false);
372 } else {
373 nsvc = gprs_ns2_nsvc_by_nsvci(nsi, id);
374
375 if (!nsvc) {
376 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
377 return CMD_WARNING;
378 }
379
380 dump_nsvc(vty, nsvc, show_stats);
381 }
382
383 return CMD_SUCCESS;
384}
385
Daniel Willmanndbab7142020-11-18 14:19:56 +0100386static int nsvc_force_unconf_cb(struct gprs_ns2_vc *nsvc, void *ctx)
387{
388 gprs_ns2_vc_force_unconfigured(nsvc);
389 return 0;
390}
391
392DEFUN_HIDDEN(nsvc_force_unconf, nsvc_force_unconf_cmd,
393 "nsvc nsei <0-65535> force-unconfigured",
394 "NS Virtual Connection\n"
395 "The NSEI\n"
396 "Reset the NSVCs back to initial state\n"
397 )
398{
399 struct gprs_ns2_inst *nsi = vty_nsi;
400 struct gprs_ns2_nse *nse;
401
402 uint16_t id = atoi(argv[0]);
403
404 nse = gprs_ns2_nse_by_nsei(nsi, id);
405 if (!nse) {
406 vty_out(vty, "Could not find NSE for NSEI %u%s", id, VTY_NEWLINE);
407 return CMD_WARNING;
408 }
409
410 /* Perform the operation for all nsvc */
411 gprs_ns2_nse_foreach_nsvc(nse, nsvc_force_unconf_cb, NULL);
412
413 return CMD_SUCCESS;
414}
415
Alexander Couzens6a161492020-07-12 13:45:50 +0200416#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
417
Alexander Couzens841817e2020-11-19 00:41:29 +0100418DEFUN(cfg_nse_fr, cfg_nse_fr_cmd,
419 "nse <0-65535> nsvci <0-65535> (fr|frnet) NETIF dlci <0-1023>",
420 NSE_CMD_STR
421 "NS Virtual Connection\n"
422 "NS Virtual Connection ID (NSVCI)\n"
Harald Welte92049192020-11-25 20:56:06 +0100423 "Frame Relay User-Side\n"
424 "Frame Relay Network-Side\n"
Alexander Couzens841817e2020-11-19 00:41:29 +0100425 IFNAME_STR
426 "Data Link connection identifier\n"
427 "Data Link connection identifier\n"
428 )
429{
430 struct ns2_vty_vc *vtyvc;
431
432 uint16_t nsei = atoi(argv[0]);
433 uint16_t nsvci = atoi(argv[1]);
434 const char *role = argv[2];
435 const char *name = argv[3];
436 uint16_t dlci = atoi(argv[4]);
437
Harald Welte6d4db232020-11-25 19:33:42 +0100438 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens841817e2020-11-19 00:41:29 +0100439 if (!vtyvc) {
440 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
441 return CMD_WARNING;
442 }
443
444 if (!strcmp(role, "fr"))
445 vtyvc->fr.role = FR_ROLE_USER_EQUIPMENT;
446 else if (!strcmp(role, "frnet"))
447 vtyvc->fr.role = FR_ROLE_NETWORK_EQUIPMENT;
448
449 osmo_strlcpy(vtyvc->netif, name, sizeof(vtyvc->netif));
450 vtyvc->frdlci = dlci;
451 vtyvc->nsvci = nsvci;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100452 vtyvc->ll = GPRS_NS2_LL_FR;
Alexander Couzens841817e2020-11-19 00:41:29 +0100453
454 return CMD_SUCCESS;
455}
456
Alexander Couzens6a161492020-07-12 13:45:50 +0200457DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
458 "nse <0-65535> nsvci <0-65535>",
459 NSE_CMD_STR
460 "NS Virtual Connection\n"
461 "NS Virtual Connection ID (NSVCI)\n"
462 )
463{
464 struct ns2_vty_vc *vtyvc;
465
466 uint16_t nsei = atoi(argv[0]);
467 uint16_t nsvci = atoi(argv[1]);
468
Harald Welte6d4db232020-11-25 19:33:42 +0100469 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200470 if (!vtyvc) {
471 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
472 return CMD_WARNING;
473 }
474
475 vtyvc->nsvci = nsvci;
476
477 return CMD_SUCCESS;
478}
479
480DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100481 "nse <0-65535> remote-ip " VTY_IPV46_CMD,
Alexander Couzens6a161492020-07-12 13:45:50 +0200482 NSE_CMD_STR
483 "Remote IP Address\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200484 "Remote IPv4 Address\n"
485 "Remote IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200486{
487 uint16_t nsei = atoi(argv[0]);
488 struct ns2_vty_vc *vtyvc;
489
Harald Welte6d4db232020-11-25 19:33:42 +0100490 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200491 if (!vtyvc) {
492 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
493 return CMD_WARNING;
494 }
495
Harald Welte6d4db232020-11-25 19:33:42 +0100496 osmo_sockaddr_str_from_str2(&vtyvc->remote, argv[1]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200497
498 return CMD_SUCCESS;
499}
500
501DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100502 "nse <0-65535> remote-port <0-65535>",
Alexander Couzens6a161492020-07-12 13:45:50 +0200503 NSE_CMD_STR
504 "Remote UDP Port\n"
505 "Remote UDP Port Number\n")
506{
507 uint16_t nsei = atoi(argv[0]);
Harald Welte6d4db232020-11-25 19:33:42 +0100508 uint16_t port = atoi(argv[1]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200509 struct ns2_vty_vc *vtyvc;
510
Harald Welte6d4db232020-11-25 19:33:42 +0100511 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200512 if (!vtyvc) {
513 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
514 return CMD_WARNING;
515 }
516
517 vtyvc->remote.port = port;
518
519 return CMD_SUCCESS;
520}
521
522DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
Alexander Couzens841817e2020-11-19 00:41:29 +0100523 "nse <0-65535> nsvci <0-65535> fr-dlci <16-1007>",
Alexander Couzens6a161492020-07-12 13:45:50 +0200524 NSE_CMD_STR
Harald Welte92049192020-11-25 20:56:06 +0100525 "NS Virtual Connection\n"
526 "NS Virtual Connection ID (NSVCI)\n"
Alexander Couzens6a161492020-07-12 13:45:50 +0200527 "Frame Relay DLCI\n"
528 "Frame Relay DLCI Number\n")
529{
530 uint16_t nsei = atoi(argv[0]);
Alexander Couzens841817e2020-11-19 00:41:29 +0100531 uint16_t nsvci = atoi(argv[1]);
532 uint16_t dlci = atoi(argv[2]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200533 struct ns2_vty_vc *vtyvc;
534
Harald Welte6d4db232020-11-25 19:33:42 +0100535 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200536 if (!vtyvc) {
537 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
538 return CMD_WARNING;
539 }
540
Alexander Couzens6a161492020-07-12 13:45:50 +0200541 vtyvc->frdlci = dlci;
Harald Welte6d4db232020-11-25 19:33:42 +0100542 vtyvc->nsvci = nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200543
544 return CMD_SUCCESS;
545}
546
547DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100548 "nse <0-65535> encapsulation (udp|framerelay-gre)",
Alexander Couzens6a161492020-07-12 13:45:50 +0200549 NSE_CMD_STR
550 "Encapsulation for NS\n"
551 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
552{
553 uint16_t nsei = atoi(argv[0]);
554 struct ns2_vty_vc *vtyvc;
555
Harald Welte6d4db232020-11-25 19:33:42 +0100556 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200557 if (!vtyvc) {
558 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
559 return CMD_WARNING;
560 }
561
Harald Welte6d4db232020-11-25 19:33:42 +0100562 if (!strcmp(argv[1], "udp"))
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100563 vtyvc->ll = GPRS_NS2_LL_UDP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200564 else
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100565 vtyvc->ll = GPRS_NS2_LL_FR_GRE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200566
567 return CMD_SUCCESS;
568}
569
570DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100571 "nse <0-65535> remote-role (sgsn|bss)",
Alexander Couzens6a161492020-07-12 13:45:50 +0200572 NSE_CMD_STR
573 "Remote NSE Role\n"
574 "Remote Peer is SGSN\n"
575 "Remote Peer is BSS\n")
576{
577 uint16_t nsei = atoi(argv[0]);
578 struct ns2_vty_vc *vtyvc;
579
Harald Welte6d4db232020-11-25 19:33:42 +0100580 vtyvc = vtyvc_by_nsei(nsei, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200581 if (!vtyvc) {
582 vty_out(vty, "Can not allocate space %s", VTY_NEWLINE);
583 return CMD_WARNING;
584 }
585
Harald Welte6d4db232020-11-25 19:33:42 +0100586 if (!strcmp(argv[1], "sgsn"))
Alexander Couzens6a161492020-07-12 13:45:50 +0200587 vtyvc->remote_end_is_sgsn = 1;
588 else
589 vtyvc->remote_end_is_sgsn = 0;
590
591 return CMD_SUCCESS;
592}
593
594DEFUN(cfg_no_nse, cfg_no_nse_cmd,
Harald Welte6d4db232020-11-25 19:33:42 +0100595 "no nse <0-65535>",
Alexander Couzens6a161492020-07-12 13:45:50 +0200596 "Delete Persistent NS Entity\n"
597 "Delete " NSE_CMD_STR)
598{
599 uint16_t nsei = atoi(argv[0]);
600 struct ns2_vty_vc *vtyvc;
601
Harald Welte6d4db232020-11-25 19:33:42 +0100602 vtyvc = vtyvc_by_nsei(nsei, false);
Alexander Couzens6a161492020-07-12 13:45:50 +0200603 if (!vtyvc) {
604 vty_out(vty, "The NSE %d does not exists.%s", nsei, VTY_NEWLINE);
605 return CMD_WARNING;
606 }
607
608 ns2_vc_free(vtyvc);
609
610 return CMD_SUCCESS;
611}
612
613DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
614 "timer " NS_TIMERS " <0-65535>",
615 "Network Service Timer\n"
616 NS_TIMERS_HELP "Timer Value\n")
617{
618 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
619 int val = atoi(argv[1]);
620
621 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
622 return CMD_WARNING;
623
624 vty_nsi->timeout[idx] = val;
625
626 return CMD_SUCCESS;
627}
628
629#define ENCAPS_STR "NS encapsulation options\n"
630
631DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
632 "encapsulation udp local-ip " VTY_IPV46_CMD,
633 ENCAPS_STR "NS over UDP Encapsulation\n"
634 "Set the IP address on which we listen for NS/UDP\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200635 "IPv4 Address\n"
636 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200637{
638 osmo_sockaddr_str_from_str2(&priv.udp, argv[0]);
639
640 return CMD_SUCCESS;
641}
642
643DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
644 "encapsulation udp local-port <0-65535>",
645 ENCAPS_STR "NS over UDP Encapsulation\n"
646 "Set the UDP port on which we listen for NS/UDP\n"
647 "UDP port number\n")
648{
649 unsigned int port = atoi(argv[0]);
650
651 priv.udp.port = port;
652
653 return CMD_SUCCESS;
654}
655
656DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
657 "encapsulation udp dscp <0-255>",
658 ENCAPS_STR "NS over UDP Encapsulation\n"
659 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
660{
661 int dscp = atoi(argv[0]);
662 struct gprs_ns2_vc_bind *bind;
663
664 priv.dscp = dscp;
665
666 llist_for_each_entry(bind, &vty_nsi->binding, list) {
667 if (gprs_ns2_is_ip_bind(bind))
668 gprs_ns2_ip_bind_set_dscp(bind, dscp);
669 }
670
671 return CMD_SUCCESS;
672}
673
674DEFUN(cfg_nsip_res_block_unblock, cfg_nsip_res_block_unblock_cmd,
675 "encapsulation udp use-reset-block-unblock (enabled|disabled)",
676 ENCAPS_STR "NS over UDP Encapsulation\n"
677 "Use NS-{RESET,BLOCK,UNBLOCK} procedures in violation of 3GPP TS 48.016\n"
678 "Enable NS-{RESET,BLOCK,UNBLOCK}\n"
679 "Disable NS-{RESET,BLOCK,UNBLOCK}\n")
680{
681 enum gprs_ns2_vc_mode vc_mode;
682 struct gprs_ns2_vc_bind *bind;
683
684 if (!strcmp(argv[0], "enabled"))
685 vc_mode = NS2_VC_MODE_BLOCKRESET;
686 else
687 vc_mode = NS2_VC_MODE_ALIVE;
688
689 if (priv.force_vc_mode) {
690 if (priv.vc_mode != vc_mode)
691 {
692 vty_out(vty, "Ignoring use-reset-block because it's already set by %s.%s",
693 priv.force_vc_mode_reason, VTY_NEWLINE);
694 return CMD_WARNING;
695 }
696
697 return CMD_SUCCESS;
698 }
699
700 priv.vc_mode = vc_mode;
701
702 llist_for_each_entry(bind, &vty_nsi->binding, list) {
703 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
704 }
705
706 return CMD_SUCCESS;
707}
708
709DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
710 "encapsulation framerelay-gre local-ip " VTY_IPV46_CMD,
711 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
712 "Set the IP address on which we listen for NS/FR/GRE\n"
Alexander Couzensc82c40a2020-09-24 05:55:48 +0200713 "IPv4 Address\n"
714 "IPv6 Address\n")
Alexander Couzens6a161492020-07-12 13:45:50 +0200715{
716 osmo_sockaddr_str_from_str2(&priv.frgreaddr, argv[0]);
717
718 return CMD_SUCCESS;
719}
720
721DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd,
722 "encapsulation framerelay-gre enabled (1|0)",
723 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
724 "Enable or disable Frame Relay over GRE\n"
725 "Enable\n" "Disable\n")
726{
727 int enabled = atoi(argv[0]);
728
729 priv.frgre = enabled;
730
731 return CMD_SUCCESS;
732}
733
734/* TODO: allow vty to reset/block/unblock nsvc/nsei */
735
736/* TODO: add filter for NSEI as ns1 code does */
737/* TODO: add filter for single connection by description */
738DEFUN(logging_fltr_nsvc,
739 logging_fltr_nsvc_cmd,
740 "logging filter nsvc nsvci <0-65535>",
741 LOGGING_STR FILTER_STR
742 "Filter based on NS Virtual Connection\n"
743 "Identify NS-VC by NSVCI\n"
744 "Numeric identifier\n")
745{
746 struct log_target *tgt;
747 struct gprs_ns2_vc *nsvc;
748 uint16_t id = atoi(argv[1]);
749
750 log_tgt_mutex_lock();
751 tgt = osmo_log_vty2tgt(vty);
752 if (!tgt) {
753 log_tgt_mutex_unlock();
754 return CMD_WARNING;
755 }
756
757 nsvc = gprs_ns2_nsvc_by_nsvci(vty_nsi, id);
758 if (!nsvc) {
759 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
760 log_tgt_mutex_unlock();
761 return CMD_WARNING;
762 }
763
764 log_set_nsvc_filter(tgt, nsvc);
765 log_tgt_mutex_unlock();
766 return CMD_SUCCESS;
767}
768
Alexander Couzens1fac6f72020-10-01 19:08:38 +0200769/**
770 * gprs_ns2_vty_init initialize the vty
771 * \param[inout] nsi
772 * \param[in] default_bind set the default address to bind to. Can be NULL.
773 * \return 0 on success
774 */
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700775int gprs_ns2_vty_init(struct gprs_ns2_inst *nsi,
776 const struct osmo_sockaddr_str *default_bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200777{
778 static bool vty_elements_installed = false;
779
780 vty_nsi = nsi;
781 memset(&priv, 0, sizeof(struct ns2_vty_priv));
782 INIT_LLIST_HEAD(&priv.vtyvc);
783 priv.vc_mode = NS2_VC_MODE_BLOCKRESET;
Alexander Couzens1fac6f72020-10-01 19:08:38 +0200784 if (default_bind)
785 memcpy(&priv.udp, default_bind, sizeof(*default_bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200786
787 /* Regression test code may call this function repeatedly, so make sure
788 * that VTY elements are not duplicated, which would assert. */
789 if (vty_elements_installed)
790 return 0;
791 vty_elements_installed = true;
792
Harald Welte2fce19a2020-12-01 17:52:55 +0100793 install_lib_element_ve(&show_ns_binds_cmd);
794 install_lib_element_ve(&show_ns_entities_cmd);
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700795 install_lib_element_ve(&show_ns_pers_cmd);
796 install_lib_element_ve(&show_nse_cmd);
797 install_lib_element_ve(&logging_fltr_nsvc_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200798
Daniel Willmanndbab7142020-11-18 14:19:56 +0100799 install_lib_element(ENABLE_NODE, &nsvc_force_unconf_cmd);
800
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700801 install_lib_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200802
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700803 install_lib_element(CONFIG_NODE, &cfg_ns_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200804 install_node(&ns_node, config_write_ns);
Alexander Couzens841817e2020-11-19 00:41:29 +0100805 install_lib_element(L_NS_NODE, &cfg_nse_fr_cmd);
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +0700806 install_lib_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
807 install_lib_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
808 install_lib_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
809 install_lib_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
810 install_lib_element(L_NS_NODE, &cfg_nse_encaps_cmd);
811 install_lib_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
812 install_lib_element(L_NS_NODE, &cfg_no_nse_cmd);
813 install_lib_element(L_NS_NODE, &cfg_ns_timer_cmd);
814 install_lib_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
815 install_lib_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
816 install_lib_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
817 install_lib_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd);
818 install_lib_element(L_NS_NODE, &cfg_frgre_enable_cmd);
819 install_lib_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
Alexander Couzens6a161492020-07-12 13:45:50 +0200820
821 /* TODO: nsvc/nsei command to reset states or reset/block/unblock nsei/nsvcs */
822
823 return 0;
824}
825
826/*!
827 * \brief gprs_ns2_vty_create parse the vty tree into ns nodes
828 * It has to be in different steps to ensure the bind is created before creating VCs.
829 * \return 0 on success
830 */
831int gprs_ns2_vty_create() {
832 struct ns2_vty_vc *vtyvc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100833 struct gprs_ns2_vc_bind *bind, *fr;
Alexander Couzens6a161492020-07-12 13:45:50 +0200834 struct gprs_ns2_nse *nse;
835 struct gprs_ns2_vc *nsvc;
836 struct osmo_sockaddr sockaddr;
Alexander Couzens841817e2020-11-19 00:41:29 +0100837 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200838
839 if (!vty_nsi)
840 return -1;
841
842 /* create binds, only support a single bind. either FR or UDP */
843 if (priv.frgre) {
844 /* TODO not yet supported !*/
845 return -1;
846 } else {
847 /* UDP */
848 osmo_sockaddr_str_to_sockaddr(&priv.udp, &sockaddr.u.sas);
Alexander Couzens477ffb02020-10-01 19:05:28 +0200849 if (gprs_ns2_ip_bind(vty_nsi, &sockaddr, priv.dscp, &bind)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200850 /* TODO: could not bind on the specific address */
851 return -1;
852 }
853 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
854 }
855
856 /* create vcs */
857 llist_for_each_entry(vtyvc, &priv.vtyvc, list) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100858 /* validate settings */
859 switch (vtyvc->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100860 case GPRS_NS2_LL_UDP:
Alexander Couzens841817e2020-11-19 00:41:29 +0100861 if (strlen(vtyvc->remote.ip) == 0) {
862 /* Invalid IP for VC */
863 continue;
864 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200865
Alexander Couzens841817e2020-11-19 00:41:29 +0100866 if (!vtyvc->remote.port) {
867 /* Invalid port for VC */
868 continue;
869 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200870
Alexander Couzens841817e2020-11-19 00:41:29 +0100871 if (osmo_sockaddr_str_to_sockaddr(&vtyvc->remote, &sockaddr.u.sas)) {
872 /* Invalid sockaddr for VC */
873 continue;
874 }
875 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100876 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100877 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100878 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200879 continue;
880 }
881
882 nse = gprs_ns2_nse_by_nsei(vty_nsi, vtyvc->nsei);
883 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100884 nse = gprs_ns2_create_nse(vty_nsi, vtyvc->nsei, vtyvc->ll);
Alexander Couzens6a161492020-07-12 13:45:50 +0200885 if (!nse) {
886 /* Could not create NSE for VTY */
887 continue;
888 }
889 }
890 nse->persistent = true;
891
Alexander Couzens841817e2020-11-19 00:41:29 +0100892 switch (vtyvc->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100893 case GPRS_NS2_LL_UDP:
Alexander Couzens841817e2020-11-19 00:41:29 +0100894 nsvc = gprs_ns2_ip_connect(bind,
895 &sockaddr,
896 nse,
897 vtyvc->nsvci);
898 if (!nsvc) {
899 /* Could not create NSVC, connect failed */
900 continue;
901 }
902 nsvc->persistent = true;
903 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100904 case GPRS_NS2_LL_FR: {
Alexander Couzens841817e2020-11-19 00:41:29 +0100905 if (vty_fr_network == NULL) {
906 /* TODO: add a switch for BSS/SGSN/gbproxy */
907 vty_fr_network = osmo_fr_network_alloc(vty_nsi);
908 }
909 fr = gprs_ns2_fr_bind_by_netif(
910 vty_nsi,
911 vtyvc->netif);
912 if (!fr) {
913 rc = gprs_ns2_fr_bind(vty_nsi, vtyvc->netif, vty_fr_network, vtyvc->fr.role, &fr);
914 if (rc < 0) {
915 LOGP(DLNS, LOGL_ERROR, "Can not create fr bind on device %s err: %d\n", vtyvc->netif, rc);
916 return rc;
917 }
918 }
919
920 nsvc = gprs_ns2_fr_connect(fr, vtyvc->nsei, vtyvc->nsvci, vtyvc->frdlci);
921 if (!nsvc) {
922 /* Could not create NSVC, connect failed */
923 continue;
924 }
925 nsvc->persistent = true;
926 break;
927 }
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100928 case GPRS_NS2_LL_FR_GRE:
Alexander Couzensd745a0e2020-10-07 00:50:00 +0200929 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200930 }
931 }
932
933
934 return 0;
935}
936
937/*!
938 * \brief ns2_vty_bind_apply will be called when a new bind is created to apply vty settings
939 * \param bind
940 * \return
941 */
942void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind)
943{
944 gprs_ns2_bind_set_mode(bind, priv.vc_mode);
945}
946
947/*!
948 * \brief ns2_vty_force_vc_mode force a mode and prevents the vty from overwriting it.
949 * \param force if true mode and reason will be set. false to allow modification via vty.
950 * \param mode
951 * \param reason A description shown to the user when a vty command wants to change the mode.
952 */
Daniel Willmann4fb27a82020-09-25 15:39:46 +0200953void gprs_ns2_vty_force_vc_mode(bool force, enum gprs_ns2_vc_mode mode, const char *reason)
Alexander Couzens6a161492020-07-12 13:45:50 +0200954{
955 priv.force_vc_mode = force;
956
957 if (force) {
958 priv.vc_mode = mode;
959 priv.force_vc_mode_reason = reason;
960 }
961}