blob: 15247639b39fcce239bb48b0155d37ba76836fc4 [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 Welte73952e32012-06-16 14:59:56 +080049
Harald Welte144e0292010-05-13 11:45:07 +020050static struct gprs_ns_inst *vty_nsi = NULL;
51
52/* FIXME: this should go to some common file as it is copied
53 * in vty_interface.c of the BSC */
54static const struct value_string gprs_ns_timer_strs[] = {
55 { 0, "tns-block" },
56 { 1, "tns-block-retries" },
57 { 2, "tns-reset" },
58 { 3, "tns-reset-retries" },
59 { 4, "tns-test" },
60 { 5, "tns-alive" },
61 { 6, "tns-alive-retries" },
62 { 0, NULL }
63};
64
Harald Weltef5430362012-06-17 12:25:53 +080065static void log_set_nsvc_filter(struct log_target *target,
66 struct gprs_nsvc *nsvc)
67{
68 if (nsvc) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010069 target->filter_map |= (1 << LOG_FLT_GB_NSVC);
70 target->filter_data[LOG_FLT_GB_NSVC] = nsvc;
71 } else if (target->filter_data[LOG_FLT_GB_NSVC]) {
72 target->filter_map = ~(1 << LOG_FLT_GB_NSVC);
73 target->filter_data[LOG_FLT_GB_NSVC] = NULL;
Harald Weltef5430362012-06-17 12:25:53 +080074 }
75}
76
Harald Welte144e0292010-05-13 11:45:07 +020077static struct cmd_node ns_node = {
Harald Welte4f5883b2012-06-16 16:54:06 +080078 L_NS_NODE,
Jacob Erlbeck35fe87c2013-10-24 01:33:20 +020079 "%s(config-ns)# ",
Harald Welte144e0292010-05-13 11:45:07 +020080 1,
81};
82
83static int config_write_ns(struct vty *vty)
84{
85 struct gprs_nsvc *nsvc;
86 unsigned int i;
Harald Welte7fb05232010-05-19 15:09:09 +020087 struct in_addr ia;
Harald Welte144e0292010-05-13 11:45:07 +020088
89 vty_out(vty, "ns%s", VTY_NEWLINE);
90
91 llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) {
92 if (!nsvc->persistent)
93 continue;
94 vty_out(vty, " nse %u nsvci %u%s",
95 nsvc->nsei, nsvc->nsvci, VTY_NEWLINE);
96 vty_out(vty, " nse %u remote-role %s%s",
97 nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss",
98 VTY_NEWLINE);
Harald Welteb3ee2652010-05-19 14:38:50 +020099 switch (nsvc->ll) {
100 case GPRS_NS_LL_UDP:
101 vty_out(vty, " nse %u encapsulation udp%s", nsvc->nsei,
102 VTY_NEWLINE);
Harald Welte144e0292010-05-13 11:45:07 +0200103 vty_out(vty, " nse %u remote-ip %s%s",
104 nsvc->nsei,
105 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
106 VTY_NEWLINE);
107 vty_out(vty, " nse %u remote-port %u%s",
Harald Weltebfe62e52017-05-15 12:48:30 +0200108 nsvc->nsei, osmo_ntohs(nsvc->ip.bts_addr.sin_port),
Harald Welte144e0292010-05-13 11:45:07 +0200109 VTY_NEWLINE);
Harald Welteb3ee2652010-05-19 14:38:50 +0200110 break;
111 case GPRS_NS_LL_FR_GRE:
112 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
113 nsvc->nsei, VTY_NEWLINE);
114 vty_out(vty, " nse %u remote-ip %s%s",
115 nsvc->nsei,
116 inet_ntoa(nsvc->frgre.bts_addr.sin_addr),
117 VTY_NEWLINE);
118 vty_out(vty, " nse %u fr-dlci %u%s",
Harald Weltebfe62e52017-05-15 12:48:30 +0200119 nsvc->nsei, osmo_ntohs(nsvc->frgre.bts_addr.sin_port),
Harald Welteb3ee2652010-05-19 14:38:50 +0200120 VTY_NEWLINE);
121 default:
122 break;
Harald Welte144e0292010-05-13 11:45:07 +0200123 }
124 }
125
126 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
127 vty_out(vty, " timer %s %u%s",
128 get_value_string(gprs_ns_timer_strs, i),
129 vty_nsi->timeout[i], VTY_NEWLINE);
130
Harald Welte7fb05232010-05-19 15:09:09 +0200131 if (vty_nsi->nsip.local_ip) {
Harald Weltebfe62e52017-05-15 12:48:30 +0200132 ia.s_addr = osmo_htonl(vty_nsi->nsip.local_ip);
Harald Welte7fb05232010-05-19 15:09:09 +0200133 vty_out(vty, " encapsulation udp local-ip %s%s",
134 inet_ntoa(ia), VTY_NEWLINE);
Harald Welte7fb05232010-05-19 15:09:09 +0200135 }
Harald Weltece4ccbc2010-05-19 17:02:57 +0200136 if (vty_nsi->nsip.local_port)
137 vty_out(vty, " encapsulation udp local-port %u%s",
138 vty_nsi->nsip.local_port, VTY_NEWLINE);
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100139 if (vty_nsi->nsip.dscp)
140 vty_out(vty, " encapsulation udp dscp %d%s",
141 vty_nsi->nsip.dscp, VTY_NEWLINE);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200142
Harald Welte7fb05232010-05-19 15:09:09 +0200143 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
144 vty_nsi->frgre.enabled ? 1 : 0, VTY_NEWLINE);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200145 if (vty_nsi->frgre.local_ip) {
Harald Weltebfe62e52017-05-15 12:48:30 +0200146 ia.s_addr = osmo_htonl(vty_nsi->frgre.local_ip);
Harald Welte7fb05232010-05-19 15:09:09 +0200147 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
148 inet_ntoa(ia), VTY_NEWLINE);
149 }
150
Harald Welte144e0292010-05-13 11:45:07 +0200151 return CMD_SUCCESS;
152}
153
154DEFUN(cfg_ns, cfg_ns_cmd,
155 "ns",
156 "Configure the GPRS Network Service")
157{
Harald Welte4f5883b2012-06-16 16:54:06 +0800158 vty->node = L_NS_NODE;
Harald Welte144e0292010-05-13 11:45:07 +0200159 return CMD_SUCCESS;
160}
161
Maxcbf5cdf2017-10-24 18:18:55 +0200162static void dump_nse(struct vty *vty, const struct gprs_nsvc *nsvc, bool stats, bool persistent_only)
Harald Welte92883342010-05-15 23:04:03 +0200163{
Max296511b2017-10-24 18:05:35 +0200164 if (persistent_only)
165 if (!nsvc->persistent)
166 return;
167
Max37f465e2017-10-24 16:04:55 +0200168 vty_out(vty, "NSEI %5u, NS-VC %5u, %5s %9s, ",
Harald Welte92883342010-05-15 23:04:03 +0200169 nsvc->nsei, nsvc->nsvci,
Max4ce24c42017-10-23 15:09:23 +0200170 NS_DESC_A(nsvc->state),
Max37f465e2017-10-24 16:04:55 +0200171 NS_DESC_B(nsvc->state));
172
173 if (nsvc->ll == GPRS_NS_LL_UDP) {
174 char local[INET6_ADDRSTRLEN + 1];
175 int rc = osmo_sock_local_ip((char *)&local, inet_ntoa(nsvc->ip.bts_addr.sin_addr));
176 vty_out(vty, "%s:%u ", (rc < 0) ? "unknown" : local, nsvc->nsi->nsip.local_port);
177 }
178
179 vty_out(vty, "Remote: %-4s, %5s %9s, %s ",
Max32f99712017-10-20 12:27:49 +0200180 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
Max4ce24c42017-10-23 15:09:23 +0200181 NS_DESC_A(nsvc->remote_state),
Max37f465e2017-10-24 16:04:55 +0200182 NS_DESC_B(nsvc->remote_state), gprs_ns_ll_str(nsvc));
Max95308592017-10-24 15:54:28 +0200183
Max37f465e2017-10-24 16:04:55 +0200184 vty_out(vty, "%s%s", nsvc->ll == GPRS_NS_LL_UDP ? "UDP" : "FR-GRE", VTY_NEWLINE);
Max95308592017-10-24 15:54:28 +0200185
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200186 if (stats) {
Harald Welte92883342010-05-15 23:04:03 +0200187 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200188 vty_out_stat_item_group(vty, " ", nsvc->statg);
189 }
Harald Welte92883342010-05-15 23:04:03 +0200190}
191
Maxcbf5cdf2017-10-24 18:18:55 +0200192static void dump_ns(struct vty *vty, const struct gprs_ns_inst *nsi, bool stats, bool persistent_only)
Harald Welte144e0292010-05-13 11:45:07 +0200193{
Harald Welte144e0292010-05-13 11:45:07 +0200194 struct gprs_nsvc *nsvc;
Harald Welte7fb05232010-05-19 15:09:09 +0200195 struct in_addr ia;
196
Harald Weltebfe62e52017-05-15 12:48:30 +0200197 ia.s_addr = osmo_htonl(vty_nsi->nsip.local_ip);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200198 vty_out(vty, "Encapsulation NS-UDP-IP Local IP: %s, UDP Port: %u%s",
Harald Welte7fb05232010-05-19 15:09:09 +0200199 inet_ntoa(ia), vty_nsi->nsip.local_port, VTY_NEWLINE);
200
Harald Weltedc802812019-02-23 11:10:08 +0100201 if (nsi->frgre.enabled) {
202 ia.s_addr = osmo_htonl(vty_nsi->frgre.local_ip);
203 vty_out(vty, "Encapsulation NS-FR-GRE-IP Local IP: %s%s", inet_ntoa(ia), VTY_NEWLINE);
204 }
Harald Welte144e0292010-05-13 11:45:07 +0200205
206 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200207 if (nsvc == nsi->unknown_nsvc)
208 continue;
Max296511b2017-10-24 18:05:35 +0200209 dump_nse(vty, nsvc, stats, persistent_only);
Harald Welte144e0292010-05-13 11:45:07 +0200210 }
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200211}
Harald Welte144e0292010-05-13 11:45:07 +0200212
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200213DEFUN(show_ns, show_ns_cmd, "show ns",
214 SHOW_STR "Display information about the NS protocol")
215{
216 struct gprs_ns_inst *nsi = vty_nsi;
Max296511b2017-10-24 18:05:35 +0200217 dump_ns(vty, nsi, false, false);
Harald Welte144e0292010-05-13 11:45:07 +0200218 return CMD_SUCCESS;
219}
220
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200221DEFUN(show_ns_stats, show_ns_stats_cmd, "show ns stats",
222 SHOW_STR
223 "Display information about the NS protocol\n"
224 "Include statistics\n")
225{
226 struct gprs_ns_inst *nsi = vty_nsi;
Max296511b2017-10-24 18:05:35 +0200227 dump_ns(vty, nsi, true, false);
228 return CMD_SUCCESS;
229}
230
231DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
232 SHOW_STR
233 "Display information about the NS protocol\n"
234 "Show only persistent NS\n")
235{
236 struct gprs_ns_inst *nsi = vty_nsi;
237 dump_ns(vty, nsi, true, true);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200238 return CMD_SUCCESS;
239}
Harald Welte144e0292010-05-13 11:45:07 +0200240
Harald Welte92883342010-05-15 23:04:03 +0200241DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
242 SHOW_STR "Display information about the NS protocol\n"
243 "Select one NSE by its NSE Identifier\n"
244 "Select one NSE by its NS-VC Identifier\n"
245 "The Identifier of selected type\n"
246 "Include Statistics\n")
247{
248 struct gprs_ns_inst *nsi = vty_nsi;
249 struct gprs_nsvc *nsvc;
250 uint16_t id = atoi(argv[1]);
Max296511b2017-10-24 18:05:35 +0200251 bool show_stats = false;
Harald Welte92883342010-05-15 23:04:03 +0200252
253 if (!strcmp(argv[0], "nsei"))
Harald Weltef5430362012-06-17 12:25:53 +0800254 nsvc = gprs_nsvc_by_nsei(nsi, id);
Harald Welte92883342010-05-15 23:04:03 +0200255 else
Harald Weltef5430362012-06-17 12:25:53 +0800256 nsvc = gprs_nsvc_by_nsvci(nsi, id);
Harald Welte92883342010-05-15 23:04:03 +0200257
258 if (!nsvc) {
259 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
260 return CMD_WARNING;
261 }
262
263 if (argc >= 3)
Max296511b2017-10-24 18:05:35 +0200264 show_stats = true;
Harald Welte92883342010-05-15 23:04:03 +0200265
Max296511b2017-10-24 18:05:35 +0200266 dump_nse(vty, nsvc, show_stats, false);
Harald Welte92883342010-05-15 23:04:03 +0200267 return CMD_SUCCESS;
268}
269
Harald Welte24133c32010-05-15 23:06:26 +0200270#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
Harald Welte144e0292010-05-13 11:45:07 +0200271
272DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
273 "nse <0-65535> nsvci <0-65534>",
274 NSE_CMD_STR
275 "NS Virtual Connection\n"
276 "NS Virtual Connection ID (NSVCI)\n"
277 )
278{
279 uint16_t nsei = atoi(argv[0]);
280 uint16_t nsvci = atoi(argv[1]);
281 struct gprs_nsvc *nsvc;
282
Harald Weltef5430362012-06-17 12:25:53 +0800283 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200284 if (!nsvc) {
Harald Weltef5430362012-06-17 12:25:53 +0800285 nsvc = gprs_nsvc_create(vty_nsi, nsvci);
Harald Welte144e0292010-05-13 11:45:07 +0200286 nsvc->nsei = nsei;
287 }
288 nsvc->nsvci = nsvci;
289 /* All NSVCs that are explicitly configured by VTY are
290 * marked as persistent so we can write them to the config
291 * file at some later point */
292 nsvc->persistent = 1;
293
294 return CMD_SUCCESS;
295}
296
297DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
298 "nse <0-65535> remote-ip A.B.C.D",
299 NSE_CMD_STR
300 "Remote IP Address\n"
301 "Remote IP Address\n")
302{
303 uint16_t nsei = atoi(argv[0]);
304 struct gprs_nsvc *nsvc;
305
Harald Weltef5430362012-06-17 12:25:53 +0800306 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200307 if (!nsvc) {
308 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
309 return CMD_WARNING;
310 }
311 inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr);
312
313 return CMD_SUCCESS;
314
315}
316
317DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
318 "nse <0-65535> remote-port <0-65535>",
319 NSE_CMD_STR
320 "Remote UDP Port\n"
321 "Remote UDP Port Number\n")
322{
323 uint16_t nsei = atoi(argv[0]);
324 uint16_t port = atoi(argv[1]);
325 struct gprs_nsvc *nsvc;
326
Harald Weltef5430362012-06-17 12:25:53 +0800327 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200328 if (!nsvc) {
329 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
330 return CMD_WARNING;
331 }
332
Harald Welteb3ee2652010-05-19 14:38:50 +0200333 if (nsvc->ll != GPRS_NS_LL_UDP) {
334 vty_out(vty, "Cannot set UDP Port on non-UDP NSE%s",
335 VTY_NEWLINE);
336 return CMD_WARNING;
337 }
338
Harald Weltebfe62e52017-05-15 12:48:30 +0200339 nsvc->ip.bts_addr.sin_port = osmo_htons(port);
Harald Welte144e0292010-05-13 11:45:07 +0200340
341 return CMD_SUCCESS;
342}
343
Harald Welteb3ee2652010-05-19 14:38:50 +0200344DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
Harald Welte188bda62010-05-28 14:11:49 +0200345 "nse <0-65535> fr-dlci <16-1007>",
Harald Welteb3ee2652010-05-19 14:38:50 +0200346 NSE_CMD_STR
347 "Frame Relay DLCI\n"
348 "Frame Relay DLCI Number\n")
349{
350 uint16_t nsei = atoi(argv[0]);
351 uint16_t dlci = atoi(argv[1]);
352 struct gprs_nsvc *nsvc;
353
Harald Weltef5430362012-06-17 12:25:53 +0800354 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welteb3ee2652010-05-19 14:38:50 +0200355 if (!nsvc) {
356 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
357 return CMD_WARNING;
358 }
359
360 if (nsvc->ll != GPRS_NS_LL_FR_GRE) {
361 vty_out(vty, "Cannot set FR DLCI on non-FR NSE%s",
362 VTY_NEWLINE);
363 return CMD_WARNING;
364 }
365
Harald Weltebfe62e52017-05-15 12:48:30 +0200366 nsvc->frgre.bts_addr.sin_port = osmo_htons(dlci);
Harald Welteb3ee2652010-05-19 14:38:50 +0200367
368 return CMD_SUCCESS;
369}
370
371DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
372 "nse <0-65535> encapsulation (udp|framerelay-gre)",
373 NSE_CMD_STR
374 "Encapsulation for NS\n"
375 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
376{
377 uint16_t nsei = atoi(argv[0]);
378 struct gprs_nsvc *nsvc;
379
Harald Weltef5430362012-06-17 12:25:53 +0800380 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welteb3ee2652010-05-19 14:38:50 +0200381 if (!nsvc) {
382 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
383 return CMD_WARNING;
384 }
385
386 if (!strcmp(argv[1], "udp"))
387 nsvc->ll = GPRS_NS_LL_UDP;
388 else
389 nsvc->ll = GPRS_NS_LL_FR_GRE;
390
391 return CMD_SUCCESS;
392}
393
394
Harald Welte144e0292010-05-13 11:45:07 +0200395DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
396 "nse <0-65535> remote-role (sgsn|bss)",
397 NSE_CMD_STR
398 "Remote NSE Role\n"
399 "Remote Peer is SGSN\n"
400 "Remote Peer is BSS\n")
401{
402 uint16_t nsei = atoi(argv[0]);
403 struct gprs_nsvc *nsvc;
404
Harald Weltef5430362012-06-17 12:25:53 +0800405 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200406 if (!nsvc) {
407 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
408 return CMD_WARNING;
409 }
410
411 if (!strcmp(argv[1], "sgsn"))
412 nsvc->remote_end_is_sgsn = 1;
413 else
414 nsvc->remote_end_is_sgsn = 0;
415
416 return CMD_SUCCESS;
417}
418
419DEFUN(cfg_no_nse, cfg_no_nse_cmd,
420 "no nse <0-65535>",
Harald Welte24133c32010-05-15 23:06:26 +0200421 "Delete Persistent NS Entity\n"
Harald Welte144e0292010-05-13 11:45:07 +0200422 "Delete " NSE_CMD_STR)
423{
424 uint16_t nsei = atoi(argv[0]);
425 struct gprs_nsvc *nsvc;
426
Harald Weltef5430362012-06-17 12:25:53 +0800427 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200428 if (!nsvc) {
429 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
430 return CMD_WARNING;
431 }
432
Harald Welte24133c32010-05-15 23:06:26 +0200433 if (!nsvc->persistent) {
434 vty_out(vty, "NSEI %u is not a persistent NSE%s",
435 nsei, VTY_NEWLINE);
436 return CMD_WARNING;
437 }
438
439 nsvc->persistent = 0;
Harald Welte144e0292010-05-13 11:45:07 +0200440
441 return CMD_SUCCESS;
442}
443
444DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
445 "timer " NS_TIMERS " <0-65535>",
446 "Network Service Timer\n"
447 NS_TIMERS_HELP "Timer Value\n")
448{
449 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
450 int val = atoi(argv[1]);
451
452 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
453 return CMD_WARNING;
454
455 vty_nsi->timeout[idx] = val;
456
457 return CMD_SUCCESS;
458}
459
Harald Welte7fb05232010-05-19 15:09:09 +0200460#define ENCAPS_STR "NS encapsulation options\n"
461
462DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
463 "encapsulation udp local-ip A.B.C.D",
464 ENCAPS_STR "NS over UDP Encapsulation\n"
465 "Set the IP address on which we listen for NS/UDP\n"
466 "IP Address\n")
467{
468 struct in_addr ia;
469
470 inet_aton(argv[0], &ia);
Harald Weltebfe62e52017-05-15 12:48:30 +0200471 vty_nsi->nsip.local_ip = osmo_ntohl(ia.s_addr);
Harald Welte7fb05232010-05-19 15:09:09 +0200472
473 return CMD_SUCCESS;
474}
475
476DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
477 "encapsulation udp local-port <0-65535>",
478 ENCAPS_STR "NS over UDP Encapsulation\n"
479 "Set the UDP port on which we listen for NS/UDP\n"
480 "UDP port number\n")
481{
482 unsigned int port = atoi(argv[0]);
483
484 vty_nsi->nsip.local_port = port;
485
486 return CMD_SUCCESS;
487}
488
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100489DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
490 "encapsulation udp dscp <0-255>",
491 ENCAPS_STR "NS over UDP Encapsulation\n"
492 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
493{
494 int dscp = atoi(argv[0]);
495 vty_nsi->nsip.dscp = dscp;
496 return CMD_SUCCESS;
497}
498
Harald Welte7fb05232010-05-19 15:09:09 +0200499DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
500 "encapsulation framerelay-gre local-ip A.B.C.D",
501 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
502 "Set the IP address on which we listen for NS/FR/GRE\n"
503 "IP Address\n")
504{
505 struct in_addr ia;
506
507 if (!vty_nsi->frgre.enabled) {
508 vty_out(vty, "FR/GRE is not enabled%s", VTY_NEWLINE);
509 return CMD_WARNING;
510 }
511 inet_aton(argv[0], &ia);
Harald Weltebfe62e52017-05-15 12:48:30 +0200512 vty_nsi->frgre.local_ip = osmo_ntohl(ia.s_addr);
Harald Welte7fb05232010-05-19 15:09:09 +0200513
514 return CMD_SUCCESS;
515}
516
517DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd,
518 "encapsulation framerelay-gre enabled (1|0)",
519 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
520 "Enable or disable Frame Relay over GRE\n"
521 "Enable\n" "Disable\n")
522{
523 int enabled = atoi(argv[0]);
524
525 vty_nsi->frgre.enabled = enabled;
526
527 return CMD_SUCCESS;
528}
529
Harald Welte43f3b692010-05-14 19:36:59 +0200530DEFUN(nsvc_nsei, nsvc_nsei_cmd,
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200531 "nsvc (nsei|nsvci) <0-65535> (block|unblock|reset)",
Harald Welte43f3b692010-05-14 19:36:59 +0200532 "Perform an operation on a NSVC\n"
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +0100533 "NSEI to identify NS-VC Identifier (NS-VCI)\n"
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200534 "NS-VC Identifier (NS-VCI)\n"
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +0100535 "The NSEI\n"
Harald Welte43f3b692010-05-14 19:36:59 +0200536 "Initiate BLOCK procedure\n"
537 "Initiate UNBLOCK procedure\n"
538 "Initiate RESET procedure\n")
539{
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200540 const char *id_type = argv[0];
541 uint16_t id = atoi(argv[1]);
542 const char *operation = argv[2];
Harald Welte43f3b692010-05-14 19:36:59 +0200543 struct gprs_nsvc *nsvc;
544
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200545 if (!strcmp(id_type, "nsei"))
546 nsvc = gprs_nsvc_by_nsei(vty_nsi, id);
547 else if (!strcmp(id_type, "nsvci"))
548 nsvc = gprs_nsvc_by_nsvci(vty_nsi, id);
549 else {
550 vty_out(vty, "%%No such id_type '%s'%s", id_type, VTY_NEWLINE);
551 return CMD_WARNING;
552 }
553
Harald Welte43f3b692010-05-14 19:36:59 +0200554 if (!nsvc) {
Jacob Erlbeck687b6902013-10-24 01:33:19 +0200555 vty_out(vty, "No such %s (%u)%s", id_type, id, VTY_NEWLINE);
Harald Welte43f3b692010-05-14 19:36:59 +0200556 return CMD_WARNING;
557 }
558
559 if (!strcmp(operation, "block"))
560 gprs_ns_tx_block(nsvc, NS_CAUSE_OM_INTERVENTION);
561 else if (!strcmp(operation, "unblock"))
562 gprs_ns_tx_unblock(nsvc);
563 else if (!strcmp(operation, "reset"))
564 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
565 else
566 return CMD_WARNING;
567
568 return CMD_SUCCESS;
569}
570
Harald Welte91f7f4b2010-05-15 23:52:02 +0200571DEFUN(logging_fltr_nsvc,
572 logging_fltr_nsvc_cmd,
573 "logging filter nsvc (nsei|nsvci) <0-65535>",
Harald Welte6703bf72010-05-16 00:00:04 +0200574 LOGGING_STR FILTER_STR
Harald Welte91f7f4b2010-05-15 23:52:02 +0200575 "Filter based on NS Virtual Connection\n"
576 "Identify NS-VC by NSEI\n"
577 "Identify NS-VC by NSVCI\n"
578 "Numeric identifier\n")
579{
Harald Welte43ae94e2011-02-18 21:10:05 +0100580 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200581 struct gprs_nsvc *nsvc;
582 uint16_t id = atoi(argv[1]);
583
Harald Welte43ae94e2011-02-18 21:10:05 +0100584 if (!tgt)
Harald Welte91f7f4b2010-05-15 23:52:02 +0200585 return CMD_WARNING;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200586
587 if (!strcmp(argv[0], "nsei"))
Harald Weltef5430362012-06-17 12:25:53 +0800588 nsvc = gprs_nsvc_by_nsei(vty_nsi, id);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200589 else
Harald Weltef5430362012-06-17 12:25:53 +0800590 nsvc = gprs_nsvc_by_nsvci(vty_nsi, id);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200591
592 if (!nsvc) {
593 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
594 return CMD_WARNING;
595 }
596
Harald Welte43ae94e2011-02-18 21:10:05 +0100597 log_set_nsvc_filter(tgt, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200598 return CMD_SUCCESS;
599}
Harald Welte43f3b692010-05-14 19:36:59 +0200600
Harald Welte144e0292010-05-13 11:45:07 +0200601int gprs_ns_vty_init(struct gprs_ns_inst *nsi)
602{
Neels Hofmeyrc32bfd52017-01-12 22:32:19 +0100603 static bool vty_elements_installed = false;
604
Harald Welte144e0292010-05-13 11:45:07 +0200605 vty_nsi = nsi;
606
Neels Hofmeyrc32bfd52017-01-12 22:32:19 +0100607 /* Regression test code may call this function repeatedly, so make sure
608 * that VTY elements are not duplicated, which would assert. */
609 if (vty_elements_installed)
610 return 0;
611 vty_elements_installed = true;
612
Harald Welte144e0292010-05-13 11:45:07 +0200613 install_element_ve(&show_ns_cmd);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200614 install_element_ve(&show_ns_stats_cmd);
Max296511b2017-10-24 18:05:35 +0200615 install_element_ve(&show_ns_pers_cmd);
Harald Welte92883342010-05-15 23:04:03 +0200616 install_element_ve(&show_nse_cmd);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200617 install_element_ve(&logging_fltr_nsvc_cmd);
Harald Welte144e0292010-05-13 11:45:07 +0200618
Harald Welte43ae94e2011-02-18 21:10:05 +0100619 install_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
620
Harald Welte144e0292010-05-13 11:45:07 +0200621 install_element(CONFIG_NODE, &cfg_ns_cmd);
622 install_node(&ns_node, config_write_ns);
Harald Welte4f5883b2012-06-16 16:54:06 +0800623 install_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
624 install_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
625 install_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
626 install_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
627 install_element(L_NS_NODE, &cfg_nse_encaps_cmd);
628 install_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
629 install_element(L_NS_NODE, &cfg_no_nse_cmd);
630 install_element(L_NS_NODE, &cfg_ns_timer_cmd);
631 install_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
632 install_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100633 install_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
Harald Welte4f5883b2012-06-16 16:54:06 +0800634 install_element(L_NS_NODE, &cfg_frgre_enable_cmd);
635 install_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
Harald Welte144e0292010-05-13 11:45:07 +0200636
Harald Welte43f3b692010-05-14 19:36:59 +0200637 install_element(ENABLE_NODE, &nsvc_nsei_cmd);
638
Harald Welte144e0292010-05-13 11:45:07 +0200639 return 0;
640}