blob: 38d43930ba8ec53ae4e76566fe80cfa727a248d5 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_ns_vty.c
2 * VTY interface for our GPRS Networks Service (NS) implementation. */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2009-2014 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
Harald Welte144e0292010-05-13 11:45:07 +02006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Harald Welte144e0292010-05-13 11:45:07 +020011 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010012 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
Harald Welte144e0292010-05-13 11:45:07 +020014 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010019 * GNU General Public License for more details.
Harald Welte144e0292010-05-13 11:45:07 +020020 *
Harald Welte7fa89c22014-10-26 20:33:09 +010021 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010022 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte144e0292010-05-13 11:45:07 +020023 *
24 */
25
26#include <stdlib.h>
27#include <unistd.h>
28#include <errno.h>
29#include <stdint.h>
30
31#include <arpa/inet.h>
32
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010033#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020034#include <osmocom/core/byteswap.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010035#include <osmocom/gsm/tlv.h>
36#include <osmocom/core/talloc.h>
37#include <osmocom/core/select.h>
38#include <osmocom/core/rate_ctr.h>
Harald Welte73952e32012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_ns.h>
40#include <osmocom/gprs/gprs_bssgp.h>
Max37f465e2017-10-24 16:04:55 +020041#include <osmocom/core/socket.h>
Harald Welteac1a7152010-05-19 19:45:32 +020042#include <osmocom/vty/vty.h>
43#include <osmocom/vty/command.h>
44#include <osmocom/vty/logging.h>
45#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso167281e2011-03-28 19:35:00 +020046#include <osmocom/vty/misc.h>
Harald Welte144e0292010-05-13 11:45:07 +020047
Harald Welte4f5883b2012-06-16 16:54:06 +080048#include "common_vty.h"
Harald Welte047f3872018-07-01 21:04:45 +020049#include "gb_internal.h"
Harald Welte73952e32012-06-16 14:59:56 +080050
Harald Welte144e0292010-05-13 11:45:07 +020051static struct gprs_ns_inst *vty_nsi = NULL;
52
53/* FIXME: this should go to some common file as it is copied
54 * in vty_interface.c of the BSC */
55static const struct value_string gprs_ns_timer_strs[] = {
56 { 0, "tns-block" },
57 { 1, "tns-block-retries" },
58 { 2, "tns-reset" },
59 { 3, "tns-reset-retries" },
60 { 4, "tns-test" },
61 { 5, "tns-alive" },
62 { 6, "tns-alive-retries" },
Harald Welte047f3872018-07-01 21:04:45 +020063 { 7, "tsns-prov" },
Harald Welte144e0292010-05-13 11:45:07 +020064 { 0, NULL }
65};
66
Harald Weltef5430362012-06-17 12:25:53 +080067static void log_set_nsvc_filter(struct log_target *target,
68 struct gprs_nsvc *nsvc)
69{
70 if (nsvc) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010071 target->filter_map |= (1 << LOG_FLT_GB_NSVC);
72 target->filter_data[LOG_FLT_GB_NSVC] = nsvc;
73 } else if (target->filter_data[LOG_FLT_GB_NSVC]) {
74 target->filter_map = ~(1 << LOG_FLT_GB_NSVC);
75 target->filter_data[LOG_FLT_GB_NSVC] = NULL;
Harald Weltef5430362012-06-17 12:25:53 +080076 }
77}
78
Harald Welte144e0292010-05-13 11:45:07 +020079static struct cmd_node ns_node = {
Harald Welte4f5883b2012-06-16 16:54:06 +080080 L_NS_NODE,
Jacob Erlbeck35fe87c2013-10-24 01:33:20 +020081 "%s(config-ns)# ",
Harald Welte144e0292010-05-13 11:45:07 +020082 1,
83};
84
85static int config_write_ns(struct vty *vty)
86{
87 struct gprs_nsvc *nsvc;
88 unsigned int i;
Harald Welte7fb05232010-05-19 15:09:09 +020089 struct in_addr ia;
Harald Welte144e0292010-05-13 11:45:07 +020090
91 vty_out(vty, "ns%s", VTY_NEWLINE);
92
Harald Welte057122e2020-06-06 22:04:20 +020093 /* global configuration must be written first, as some of it may be
94 * relevant when creating the NSE/NSVC later below */
95
96 if (vty_nsi->nsip.local_ip) {
97 ia.s_addr = osmo_htonl(vty_nsi->nsip.local_ip);
98 vty_out(vty, " encapsulation udp local-ip %s%s",
99 inet_ntoa(ia), VTY_NEWLINE);
100 }
101 if (vty_nsi->nsip.local_port)
102 vty_out(vty, " encapsulation udp local-port %u%s",
103 vty_nsi->nsip.local_port, VTY_NEWLINE);
104 if (vty_nsi->nsip.dscp)
105 vty_out(vty, " encapsulation udp dscp %d%s",
106 vty_nsi->nsip.dscp, VTY_NEWLINE);
107
108 vty_out(vty, " encapsulation udp use-reset-block-unblock %s%s",
109 vty_nsi->nsip.use_reset_block_unblock ? "enabled" : "disabled", VTY_NEWLINE);
110
111 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
112 vty_nsi->frgre.enabled ? 1 : 0, VTY_NEWLINE);
113 if (vty_nsi->frgre.local_ip) {
114 ia.s_addr = osmo_htonl(vty_nsi->frgre.local_ip);
115 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
116 inet_ntoa(ia), VTY_NEWLINE);
117 }
118
Harald Welte144e0292010-05-13 11:45:07 +0200119 llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) {
120 if (!nsvc->persistent)
121 continue;
122 vty_out(vty, " nse %u nsvci %u%s",
123 nsvc->nsei, nsvc->nsvci, VTY_NEWLINE);
124 vty_out(vty, " nse %u remote-role %s%s",
125 nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss",
126 VTY_NEWLINE);
Harald Welteb3ee2652010-05-19 14:38:50 +0200127 switch (nsvc->ll) {
128 case GPRS_NS_LL_UDP:
129 vty_out(vty, " nse %u encapsulation udp%s", nsvc->nsei,
130 VTY_NEWLINE);
Harald Welte144e0292010-05-13 11:45:07 +0200131 vty_out(vty, " nse %u remote-ip %s%s",
132 nsvc->nsei,
133 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
134 VTY_NEWLINE);
135 vty_out(vty, " nse %u remote-port %u%s",
Harald Weltebfe62e52017-05-15 12:48:30 +0200136 nsvc->nsei, osmo_ntohs(nsvc->ip.bts_addr.sin_port),
Harald Welte144e0292010-05-13 11:45:07 +0200137 VTY_NEWLINE);
Harald Welteb3ee2652010-05-19 14:38:50 +0200138 break;
139 case GPRS_NS_LL_FR_GRE:
140 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
141 nsvc->nsei, VTY_NEWLINE);
142 vty_out(vty, " nse %u remote-ip %s%s",
143 nsvc->nsei,
144 inet_ntoa(nsvc->frgre.bts_addr.sin_addr),
145 VTY_NEWLINE);
146 vty_out(vty, " nse %u fr-dlci %u%s",
Harald Weltebfe62e52017-05-15 12:48:30 +0200147 nsvc->nsei, osmo_ntohs(nsvc->frgre.bts_addr.sin_port),
Harald Welteb3ee2652010-05-19 14:38:50 +0200148 VTY_NEWLINE);
149 default:
150 break;
Harald Welte144e0292010-05-13 11:45:07 +0200151 }
152 }
153
154 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
155 vty_out(vty, " timer %s %u%s",
156 get_value_string(gprs_ns_timer_strs, i),
157 vty_nsi->timeout[i], VTY_NEWLINE);
158
159 return CMD_SUCCESS;
160}
161
162DEFUN(cfg_ns, cfg_ns_cmd,
163 "ns",
164 "Configure the GPRS Network Service")
165{
Harald Welte4f5883b2012-06-16 16:54:06 +0800166 vty->node = L_NS_NODE;
Harald Welte144e0292010-05-13 11:45:07 +0200167 return CMD_SUCCESS;
168}
169
Maxcbf5cdf2017-10-24 18:18:55 +0200170static void dump_nse(struct vty *vty, const struct gprs_nsvc *nsvc, bool stats, bool persistent_only)
Harald Welte92883342010-05-15 23:04:03 +0200171{
Max296511b2017-10-24 18:05:35 +0200172 if (persistent_only)
173 if (!nsvc->persistent)
174 return;
175
Max37f465e2017-10-24 16:04:55 +0200176 vty_out(vty, "NSEI %5u, NS-VC %5u, %5s %9s, ",
Harald Welte92883342010-05-15 23:04:03 +0200177 nsvc->nsei, nsvc->nsvci,
Max4ce24c42017-10-23 15:09:23 +0200178 NS_DESC_A(nsvc->state),
Max37f465e2017-10-24 16:04:55 +0200179 NS_DESC_B(nsvc->state));
180
181 if (nsvc->ll == GPRS_NS_LL_UDP) {
182 char local[INET6_ADDRSTRLEN + 1];
183 int rc = osmo_sock_local_ip((char *)&local, inet_ntoa(nsvc->ip.bts_addr.sin_addr));
184 vty_out(vty, "%s:%u ", (rc < 0) ? "unknown" : local, nsvc->nsi->nsip.local_port);
185 }
186
187 vty_out(vty, "Remote: %-4s, %5s %9s, %s ",
Max32f99712017-10-20 12:27:49 +0200188 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
Max4ce24c42017-10-23 15:09:23 +0200189 NS_DESC_A(nsvc->remote_state),
Max37f465e2017-10-24 16:04:55 +0200190 NS_DESC_B(nsvc->remote_state), gprs_ns_ll_str(nsvc));
Max95308592017-10-24 15:54:28 +0200191
Max37f465e2017-10-24 16:04:55 +0200192 vty_out(vty, "%s%s", nsvc->ll == GPRS_NS_LL_UDP ? "UDP" : "FR-GRE", VTY_NEWLINE);
Max95308592017-10-24 15:54:28 +0200193
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200194 if (stats) {
Harald Welte92883342010-05-15 23:04:03 +0200195 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200196 vty_out_stat_item_group(vty, " ", nsvc->statg);
197 }
Harald Welte92883342010-05-15 23:04:03 +0200198}
199
Maxcbf5cdf2017-10-24 18:18:55 +0200200static void dump_ns(struct vty *vty, const struct gprs_ns_inst *nsi, bool stats, bool persistent_only)
Harald Welte144e0292010-05-13 11:45:07 +0200201{
Harald Welte144e0292010-05-13 11:45:07 +0200202 struct gprs_nsvc *nsvc;
Harald Welte7fb05232010-05-19 15:09:09 +0200203 struct in_addr ia;
204
Harald Weltebfe62e52017-05-15 12:48:30 +0200205 ia.s_addr = osmo_htonl(vty_nsi->nsip.local_ip);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200206 vty_out(vty, "Encapsulation NS-UDP-IP Local IP: %s, UDP Port: %u%s",
Harald Welte7fb05232010-05-19 15:09:09 +0200207 inet_ntoa(ia), vty_nsi->nsip.local_port, VTY_NEWLINE);
208
Harald Weltedc802812019-02-23 11:10:08 +0100209 if (nsi->frgre.enabled) {
210 ia.s_addr = osmo_htonl(vty_nsi->frgre.local_ip);
211 vty_out(vty, "Encapsulation NS-FR-GRE-IP Local IP: %s%s", inet_ntoa(ia), VTY_NEWLINE);
212 }
Harald Welte144e0292010-05-13 11:45:07 +0200213
214 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200215 if (nsvc == nsi->unknown_nsvc)
216 continue;
Max296511b2017-10-24 18:05:35 +0200217 dump_nse(vty, nsvc, stats, persistent_only);
Harald Welte144e0292010-05-13 11:45:07 +0200218 }
Harald Welte047f3872018-07-01 21:04:45 +0200219
220 gprs_sns_dump_vty(vty, nsi, stats);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200221}
Harald Welte144e0292010-05-13 11:45:07 +0200222
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200223DEFUN(show_ns, show_ns_cmd, "show ns",
224 SHOW_STR "Display information about the NS protocol")
225{
226 struct gprs_ns_inst *nsi = vty_nsi;
Max296511b2017-10-24 18:05:35 +0200227 dump_ns(vty, nsi, false, false);
Harald Welte144e0292010-05-13 11:45:07 +0200228 return CMD_SUCCESS;
229}
230
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200231DEFUN(show_ns_stats, show_ns_stats_cmd, "show ns stats",
232 SHOW_STR
233 "Display information about the NS protocol\n"
234 "Include statistics\n")
235{
236 struct gprs_ns_inst *nsi = vty_nsi;
Max296511b2017-10-24 18:05:35 +0200237 dump_ns(vty, nsi, true, false);
238 return CMD_SUCCESS;
239}
240
241DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
242 SHOW_STR
243 "Display information about the NS protocol\n"
244 "Show only persistent NS\n")
245{
246 struct gprs_ns_inst *nsi = vty_nsi;
247 dump_ns(vty, nsi, true, true);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200248 return CMD_SUCCESS;
249}
Harald Welte144e0292010-05-13 11:45:07 +0200250
Harald Welte92883342010-05-15 23:04:03 +0200251DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
252 SHOW_STR "Display information about the NS protocol\n"
253 "Select one NSE by its NSE Identifier\n"
254 "Select one NSE by its NS-VC Identifier\n"
255 "The Identifier of selected type\n"
256 "Include Statistics\n")
257{
258 struct gprs_ns_inst *nsi = vty_nsi;
259 struct gprs_nsvc *nsvc;
260 uint16_t id = atoi(argv[1]);
Max296511b2017-10-24 18:05:35 +0200261 bool show_stats = false;
Harald Welte92883342010-05-15 23:04:03 +0200262
263 if (!strcmp(argv[0], "nsei"))
Harald Weltef5430362012-06-17 12:25:53 +0800264 nsvc = gprs_nsvc_by_nsei(nsi, id);
Harald Welte92883342010-05-15 23:04:03 +0200265 else
Harald Weltef5430362012-06-17 12:25:53 +0800266 nsvc = gprs_nsvc_by_nsvci(nsi, id);
Harald Welte92883342010-05-15 23:04:03 +0200267
268 if (!nsvc) {
269 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
270 return CMD_WARNING;
271 }
272
273 if (argc >= 3)
Max296511b2017-10-24 18:05:35 +0200274 show_stats = true;
Harald Welte92883342010-05-15 23:04:03 +0200275
Max296511b2017-10-24 18:05:35 +0200276 dump_nse(vty, nsvc, show_stats, false);
Harald Welte92883342010-05-15 23:04:03 +0200277 return CMD_SUCCESS;
278}
279
Harald Welte24133c32010-05-15 23:06:26 +0200280#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
Harald Welte144e0292010-05-13 11:45:07 +0200281
282DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
283 "nse <0-65535> nsvci <0-65534>",
284 NSE_CMD_STR
285 "NS Virtual Connection\n"
286 "NS Virtual Connection ID (NSVCI)\n"
287 )
288{
289 uint16_t nsei = atoi(argv[0]);
290 uint16_t nsvci = atoi(argv[1]);
291 struct gprs_nsvc *nsvc;
292
Harald Weltef5430362012-06-17 12:25:53 +0800293 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200294 if (!nsvc) {
Harald Welte68944962020-06-07 10:31:48 +0200295 nsvc = gprs_nsvc_create2(vty_nsi, nsvci, 1, 1);
Harald Welte144e0292010-05-13 11:45:07 +0200296 nsvc->nsei = nsei;
297 }
298 nsvc->nsvci = nsvci;
299 /* All NSVCs that are explicitly configured by VTY are
300 * marked as persistent so we can write them to the config
301 * file at some later point */
302 nsvc->persistent = 1;
303
304 return CMD_SUCCESS;
305}
306
307DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
308 "nse <0-65535> remote-ip A.B.C.D",
309 NSE_CMD_STR
310 "Remote IP Address\n"
311 "Remote IP Address\n")
312{
313 uint16_t nsei = atoi(argv[0]);
314 struct gprs_nsvc *nsvc;
315
Harald Weltef5430362012-06-17 12:25:53 +0800316 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200317 if (!nsvc) {
318 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
319 return CMD_WARNING;
320 }
321 inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr);
322
323 return CMD_SUCCESS;
324
325}
326
327DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
328 "nse <0-65535> remote-port <0-65535>",
329 NSE_CMD_STR
330 "Remote UDP Port\n"
331 "Remote UDP Port Number\n")
332{
333 uint16_t nsei = atoi(argv[0]);
334 uint16_t port = atoi(argv[1]);
335 struct gprs_nsvc *nsvc;
336
Harald Weltef5430362012-06-17 12:25:53 +0800337 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200338 if (!nsvc) {
339 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
340 return CMD_WARNING;
341 }
342
Harald Welteb3ee2652010-05-19 14:38:50 +0200343 if (nsvc->ll != GPRS_NS_LL_UDP) {
344 vty_out(vty, "Cannot set UDP Port on non-UDP NSE%s",
345 VTY_NEWLINE);
346 return CMD_WARNING;
347 }
348
Harald Weltebfe62e52017-05-15 12:48:30 +0200349 nsvc->ip.bts_addr.sin_port = osmo_htons(port);
Harald Welte144e0292010-05-13 11:45:07 +0200350
351 return CMD_SUCCESS;
352}
353
Harald Welteb3ee2652010-05-19 14:38:50 +0200354DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
Harald Welte188bda62010-05-28 14:11:49 +0200355 "nse <0-65535> fr-dlci <16-1007>",
Harald Welteb3ee2652010-05-19 14:38:50 +0200356 NSE_CMD_STR
357 "Frame Relay DLCI\n"
358 "Frame Relay DLCI Number\n")
359{
360 uint16_t nsei = atoi(argv[0]);
361 uint16_t dlci = atoi(argv[1]);
362 struct gprs_nsvc *nsvc;
363
Harald Weltef5430362012-06-17 12:25:53 +0800364 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welteb3ee2652010-05-19 14:38:50 +0200365 if (!nsvc) {
366 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
367 return CMD_WARNING;
368 }
369
370 if (nsvc->ll != GPRS_NS_LL_FR_GRE) {
371 vty_out(vty, "Cannot set FR DLCI on non-FR NSE%s",
372 VTY_NEWLINE);
373 return CMD_WARNING;
374 }
375
Harald Weltebfe62e52017-05-15 12:48:30 +0200376 nsvc->frgre.bts_addr.sin_port = osmo_htons(dlci);
Harald Welteb3ee2652010-05-19 14:38:50 +0200377
378 return CMD_SUCCESS;
379}
380
381DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
382 "nse <0-65535> encapsulation (udp|framerelay-gre)",
383 NSE_CMD_STR
384 "Encapsulation for NS\n"
385 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
386{
387 uint16_t nsei = atoi(argv[0]);
388 struct gprs_nsvc *nsvc;
389
Harald Weltef5430362012-06-17 12:25:53 +0800390 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welteb3ee2652010-05-19 14:38:50 +0200391 if (!nsvc) {
392 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
393 return CMD_WARNING;
394 }
395
396 if (!strcmp(argv[1], "udp"))
397 nsvc->ll = GPRS_NS_LL_UDP;
398 else
399 nsvc->ll = GPRS_NS_LL_FR_GRE;
400
401 return CMD_SUCCESS;
402}
403
404
Harald Welte144e0292010-05-13 11:45:07 +0200405DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
406 "nse <0-65535> remote-role (sgsn|bss)",
407 NSE_CMD_STR
408 "Remote NSE Role\n"
409 "Remote Peer is SGSN\n"
410 "Remote Peer is BSS\n")
411{
412 uint16_t nsei = atoi(argv[0]);
413 struct gprs_nsvc *nsvc;
414
Harald Weltef5430362012-06-17 12:25:53 +0800415 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200416 if (!nsvc) {
417 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
418 return CMD_WARNING;
419 }
420
421 if (!strcmp(argv[1], "sgsn"))
422 nsvc->remote_end_is_sgsn = 1;
423 else
424 nsvc->remote_end_is_sgsn = 0;
425
426 return CMD_SUCCESS;
427}
428
429DEFUN(cfg_no_nse, cfg_no_nse_cmd,
430 "no nse <0-65535>",
Harald Welte24133c32010-05-15 23:06:26 +0200431 "Delete Persistent NS Entity\n"
Harald Welte144e0292010-05-13 11:45:07 +0200432 "Delete " NSE_CMD_STR)
433{
434 uint16_t nsei = atoi(argv[0]);
435 struct gprs_nsvc *nsvc;
436
Harald Weltef5430362012-06-17 12:25:53 +0800437 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200438 if (!nsvc) {
439 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
440 return CMD_WARNING;
441 }
442
Harald Welte24133c32010-05-15 23:06:26 +0200443 if (!nsvc->persistent) {
444 vty_out(vty, "NSEI %u is not a persistent NSE%s",
445 nsei, VTY_NEWLINE);
446 return CMD_WARNING;
447 }
448
449 nsvc->persistent = 0;
Harald Welte144e0292010-05-13 11:45:07 +0200450
451 return CMD_SUCCESS;
452}
453
454DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
455 "timer " NS_TIMERS " <0-65535>",
456 "Network Service Timer\n"
457 NS_TIMERS_HELP "Timer Value\n")
458{
459 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
460 int val = atoi(argv[1]);
461
462 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
463 return CMD_WARNING;
464
465 vty_nsi->timeout[idx] = val;
466
467 return CMD_SUCCESS;
468}
469
Harald Welte7fb05232010-05-19 15:09:09 +0200470#define ENCAPS_STR "NS encapsulation options\n"
471
472DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
473 "encapsulation udp local-ip A.B.C.D",
474 ENCAPS_STR "NS over UDP Encapsulation\n"
475 "Set the IP address on which we listen for NS/UDP\n"
476 "IP Address\n")
477{
478 struct in_addr ia;
479
480 inet_aton(argv[0], &ia);
Harald Weltebfe62e52017-05-15 12:48:30 +0200481 vty_nsi->nsip.local_ip = osmo_ntohl(ia.s_addr);
Harald Welte7fb05232010-05-19 15:09:09 +0200482
483 return CMD_SUCCESS;
484}
485
486DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
487 "encapsulation udp local-port <0-65535>",
488 ENCAPS_STR "NS over UDP Encapsulation\n"
489 "Set the UDP port on which we listen for NS/UDP\n"
490 "UDP port number\n")
491{
492 unsigned int port = atoi(argv[0]);
493
494 vty_nsi->nsip.local_port = port;
495
496 return CMD_SUCCESS;
497}
498
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100499DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
500 "encapsulation udp dscp <0-255>",
501 ENCAPS_STR "NS over UDP Encapsulation\n"
502 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
503{
504 int dscp = atoi(argv[0]);
505 vty_nsi->nsip.dscp = dscp;
506 return CMD_SUCCESS;
507}
508
Harald Welte057122e2020-06-06 22:04:20 +0200509DEFUN(cfg_nsip_res_block_unblock, cfg_nsip_res_block_unblock_cmd,
510 "encapsulation udp use-reset-block-unblock (enabled|disabled)",
511 ENCAPS_STR "NS over UDP Encapsulation\n"
512 "Use NS-{RESET,BLOCK,UNBLOCK} procedures in violation of 3GPP TS 48.016\n"
513 "Enable NS-{RESET,BLOCK,UNBLOCK}\n"
514 "Disable NS-{RESET,BLOCK,UNBLOCK}\n")
515{
516 if (!strcmp(argv[0], "enabled"))
517 vty_nsi->nsip.use_reset_block_unblock = true;
518 else
519 vty_nsi->nsip.use_reset_block_unblock = false;
520
521 return CMD_SUCCESS;
522}
523
Harald Welte7fb05232010-05-19 15:09:09 +0200524DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
525 "encapsulation framerelay-gre local-ip A.B.C.D",
526 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
527 "Set the IP address on which we listen for NS/FR/GRE\n"
528 "IP Address\n")
529{
530 struct in_addr ia;
531
Harald Welte7fb05232010-05-19 15:09:09 +0200532 inet_aton(argv[0], &ia);
Harald Weltebfe62e52017-05-15 12:48:30 +0200533 vty_nsi->frgre.local_ip = osmo_ntohl(ia.s_addr);
Harald Welte7fb05232010-05-19 15:09:09 +0200534
535 return CMD_SUCCESS;
536}
537
538DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd,
539 "encapsulation framerelay-gre enabled (1|0)",
540 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
541 "Enable or disable Frame Relay over GRE\n"
542 "Enable\n" "Disable\n")
543{
544 int enabled = atoi(argv[0]);
545
546 vty_nsi->frgre.enabled = enabled;
547
548 return CMD_SUCCESS;
549}
550
Harald Welte43f3b692010-05-14 19:36:59 +0200551DEFUN(nsvc_nsei, nsvc_nsei_cmd,
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200552 "nsvc (nsei|nsvci) <0-65535> (block|unblock|reset)",
Harald Welte43f3b692010-05-14 19:36:59 +0200553 "Perform an operation on a NSVC\n"
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +0100554 "NSEI to identify NS-VC Identifier (NS-VCI)\n"
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200555 "NS-VC Identifier (NS-VCI)\n"
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +0100556 "The NSEI\n"
Harald Welte43f3b692010-05-14 19:36:59 +0200557 "Initiate BLOCK procedure\n"
558 "Initiate UNBLOCK procedure\n"
559 "Initiate RESET procedure\n")
560{
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200561 const char *id_type = argv[0];
562 uint16_t id = atoi(argv[1]);
563 const char *operation = argv[2];
Harald Welte43f3b692010-05-14 19:36:59 +0200564 struct gprs_nsvc *nsvc;
565
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200566 if (!strcmp(id_type, "nsei"))
567 nsvc = gprs_nsvc_by_nsei(vty_nsi, id);
568 else if (!strcmp(id_type, "nsvci"))
569 nsvc = gprs_nsvc_by_nsvci(vty_nsi, id);
570 else {
571 vty_out(vty, "%%No such id_type '%s'%s", id_type, VTY_NEWLINE);
572 return CMD_WARNING;
573 }
574
Harald Welte43f3b692010-05-14 19:36:59 +0200575 if (!nsvc) {
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200576 vty_out(vty, "No such %s (%u)%s", id_type, id, VTY_NEWLINE);
Harald Welte43f3b692010-05-14 19:36:59 +0200577 return CMD_WARNING;
578 }
579
Harald Welte047f3872018-07-01 21:04:45 +0200580 if (nsvc->nsi->bss_sns_fi) {
581 vty_out(vty, "A NS Instance using the IP Sub-Network doesn't use BLOCK/UNBLOCK/RESET%s",
582 VTY_NEWLINE);
583 return CMD_WARNING;
584 }
585
Harald Welte43f3b692010-05-14 19:36:59 +0200586 if (!strcmp(operation, "block"))
587 gprs_ns_tx_block(nsvc, NS_CAUSE_OM_INTERVENTION);
588 else if (!strcmp(operation, "unblock"))
589 gprs_ns_tx_unblock(nsvc);
590 else if (!strcmp(operation, "reset"))
591 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
592 else
593 return CMD_WARNING;
594
595 return CMD_SUCCESS;
596}
597
Harald Welte91f7f4b2010-05-15 23:52:02 +0200598DEFUN(logging_fltr_nsvc,
599 logging_fltr_nsvc_cmd,
600 "logging filter nsvc (nsei|nsvci) <0-65535>",
Harald Welte6703bf72010-05-16 00:00:04 +0200601 LOGGING_STR FILTER_STR
Harald Welte91f7f4b2010-05-15 23:52:02 +0200602 "Filter based on NS Virtual Connection\n"
603 "Identify NS-VC by NSEI\n"
604 "Identify NS-VC by NSVCI\n"
605 "Numeric identifier\n")
606{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200607 struct log_target *tgt;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200608 struct gprs_nsvc *nsvc;
609 uint16_t id = atoi(argv[1]);
610
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200611 log_tgt_mutex_lock();
612 tgt = osmo_log_vty2tgt(vty);
613 if (!tgt) {
614 log_tgt_mutex_unlock();
Harald Welte91f7f4b2010-05-15 23:52:02 +0200615 return CMD_WARNING;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200616 }
Harald Welte91f7f4b2010-05-15 23:52:02 +0200617
618 if (!strcmp(argv[0], "nsei"))
Harald Weltef5430362012-06-17 12:25:53 +0800619 nsvc = gprs_nsvc_by_nsei(vty_nsi, id);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200620 else
Harald Weltef5430362012-06-17 12:25:53 +0800621 nsvc = gprs_nsvc_by_nsvci(vty_nsi, id);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200622
623 if (!nsvc) {
624 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200625 log_tgt_mutex_unlock();
Harald Welte91f7f4b2010-05-15 23:52:02 +0200626 return CMD_WARNING;
627 }
628
Harald Welte43ae94e2011-02-18 21:10:05 +0100629 log_set_nsvc_filter(tgt, nsvc);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200630 log_tgt_mutex_unlock();
Harald Welte91f7f4b2010-05-15 23:52:02 +0200631 return CMD_SUCCESS;
632}
Harald Welte43f3b692010-05-14 19:36:59 +0200633
Harald Welte144e0292010-05-13 11:45:07 +0200634int gprs_ns_vty_init(struct gprs_ns_inst *nsi)
635{
Neels Hofmeyrc32bfd52017-01-12 22:32:19 +0100636 static bool vty_elements_installed = false;
637
Harald Welte144e0292010-05-13 11:45:07 +0200638 vty_nsi = nsi;
639
Neels Hofmeyrc32bfd52017-01-12 22:32:19 +0100640 /* Regression test code may call this function repeatedly, so make sure
641 * that VTY elements are not duplicated, which would assert. */
642 if (vty_elements_installed)
643 return 0;
644 vty_elements_installed = true;
645
Harald Welte144e0292010-05-13 11:45:07 +0200646 install_element_ve(&show_ns_cmd);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200647 install_element_ve(&show_ns_stats_cmd);
Max296511b2017-10-24 18:05:35 +0200648 install_element_ve(&show_ns_pers_cmd);
Harald Welte92883342010-05-15 23:04:03 +0200649 install_element_ve(&show_nse_cmd);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200650 install_element_ve(&logging_fltr_nsvc_cmd);
Harald Welte144e0292010-05-13 11:45:07 +0200651
Harald Welte43ae94e2011-02-18 21:10:05 +0100652 install_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
653
Harald Welte144e0292010-05-13 11:45:07 +0200654 install_element(CONFIG_NODE, &cfg_ns_cmd);
655 install_node(&ns_node, config_write_ns);
Harald Welte4f5883b2012-06-16 16:54:06 +0800656 install_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
657 install_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
658 install_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
659 install_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
660 install_element(L_NS_NODE, &cfg_nse_encaps_cmd);
661 install_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
662 install_element(L_NS_NODE, &cfg_no_nse_cmd);
663 install_element(L_NS_NODE, &cfg_ns_timer_cmd);
664 install_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
665 install_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100666 install_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
Harald Welte057122e2020-06-06 22:04:20 +0200667 install_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd);
Harald Welte4f5883b2012-06-16 16:54:06 +0800668 install_element(L_NS_NODE, &cfg_frgre_enable_cmd);
669 install_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
Harald Welte144e0292010-05-13 11:45:07 +0200670
Harald Welte43f3b692010-05-14 19:36:59 +0200671 install_element(ENABLE_NODE, &nsvc_nsei_cmd);
672
Harald Welte144e0292010-05-13 11:45:07 +0200673 return 0;
674}