blob: 9cffb71d9a86cd4a84a2318cb9d1f6de944a9526 [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
93 llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) {
94 if (!nsvc->persistent)
95 continue;
96 vty_out(vty, " nse %u nsvci %u%s",
97 nsvc->nsei, nsvc->nsvci, VTY_NEWLINE);
98 vty_out(vty, " nse %u remote-role %s%s",
99 nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss",
100 VTY_NEWLINE);
Harald Welteb3ee2652010-05-19 14:38:50 +0200101 switch (nsvc->ll) {
102 case GPRS_NS_LL_UDP:
103 vty_out(vty, " nse %u encapsulation udp%s", nsvc->nsei,
104 VTY_NEWLINE);
Harald Welte144e0292010-05-13 11:45:07 +0200105 vty_out(vty, " nse %u remote-ip %s%s",
106 nsvc->nsei,
107 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
108 VTY_NEWLINE);
109 vty_out(vty, " nse %u remote-port %u%s",
Harald Weltebfe62e52017-05-15 12:48:30 +0200110 nsvc->nsei, osmo_ntohs(nsvc->ip.bts_addr.sin_port),
Harald Welte144e0292010-05-13 11:45:07 +0200111 VTY_NEWLINE);
Harald Welteb3ee2652010-05-19 14:38:50 +0200112 break;
113 case GPRS_NS_LL_FR_GRE:
114 vty_out(vty, " nse %u encapsulation framerelay-gre%s",
115 nsvc->nsei, VTY_NEWLINE);
116 vty_out(vty, " nse %u remote-ip %s%s",
117 nsvc->nsei,
118 inet_ntoa(nsvc->frgre.bts_addr.sin_addr),
119 VTY_NEWLINE);
120 vty_out(vty, " nse %u fr-dlci %u%s",
Harald Weltebfe62e52017-05-15 12:48:30 +0200121 nsvc->nsei, osmo_ntohs(nsvc->frgre.bts_addr.sin_port),
Harald Welteb3ee2652010-05-19 14:38:50 +0200122 VTY_NEWLINE);
123 default:
124 break;
Harald Welte144e0292010-05-13 11:45:07 +0200125 }
126 }
127
128 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
129 vty_out(vty, " timer %s %u%s",
130 get_value_string(gprs_ns_timer_strs, i),
131 vty_nsi->timeout[i], VTY_NEWLINE);
132
Harald Welte7fb05232010-05-19 15:09:09 +0200133 if (vty_nsi->nsip.local_ip) {
Harald Weltebfe62e52017-05-15 12:48:30 +0200134 ia.s_addr = osmo_htonl(vty_nsi->nsip.local_ip);
Harald Welte7fb05232010-05-19 15:09:09 +0200135 vty_out(vty, " encapsulation udp local-ip %s%s",
136 inet_ntoa(ia), VTY_NEWLINE);
Harald Welte7fb05232010-05-19 15:09:09 +0200137 }
Harald Weltece4ccbc2010-05-19 17:02:57 +0200138 if (vty_nsi->nsip.local_port)
139 vty_out(vty, " encapsulation udp local-port %u%s",
140 vty_nsi->nsip.local_port, VTY_NEWLINE);
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100141 if (vty_nsi->nsip.dscp)
142 vty_out(vty, " encapsulation udp dscp %d%s",
143 vty_nsi->nsip.dscp, VTY_NEWLINE);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200144
Harald Welte7fb05232010-05-19 15:09:09 +0200145 vty_out(vty, " encapsulation framerelay-gre enabled %u%s",
146 vty_nsi->frgre.enabled ? 1 : 0, VTY_NEWLINE);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200147 if (vty_nsi->frgre.local_ip) {
Harald Weltebfe62e52017-05-15 12:48:30 +0200148 ia.s_addr = osmo_htonl(vty_nsi->frgre.local_ip);
Harald Welte7fb05232010-05-19 15:09:09 +0200149 vty_out(vty, " encapsulation framerelay-gre local-ip %s%s",
150 inet_ntoa(ia), VTY_NEWLINE);
151 }
152
Harald Welte144e0292010-05-13 11:45:07 +0200153 return CMD_SUCCESS;
154}
155
156DEFUN(cfg_ns, cfg_ns_cmd,
157 "ns",
158 "Configure the GPRS Network Service")
159{
Harald Welte4f5883b2012-06-16 16:54:06 +0800160 vty->node = L_NS_NODE;
Harald Welte144e0292010-05-13 11:45:07 +0200161 return CMD_SUCCESS;
162}
163
Maxcbf5cdf2017-10-24 18:18:55 +0200164static void dump_nse(struct vty *vty, const struct gprs_nsvc *nsvc, bool stats, bool persistent_only)
Harald Welte92883342010-05-15 23:04:03 +0200165{
Max296511b2017-10-24 18:05:35 +0200166 if (persistent_only)
167 if (!nsvc->persistent)
168 return;
169
Max37f465e2017-10-24 16:04:55 +0200170 vty_out(vty, "NSEI %5u, NS-VC %5u, %5s %9s, ",
Harald Welte92883342010-05-15 23:04:03 +0200171 nsvc->nsei, nsvc->nsvci,
Max4ce24c42017-10-23 15:09:23 +0200172 NS_DESC_A(nsvc->state),
Max37f465e2017-10-24 16:04:55 +0200173 NS_DESC_B(nsvc->state));
174
175 if (nsvc->ll == GPRS_NS_LL_UDP) {
176 char local[INET6_ADDRSTRLEN + 1];
177 int rc = osmo_sock_local_ip((char *)&local, inet_ntoa(nsvc->ip.bts_addr.sin_addr));
178 vty_out(vty, "%s:%u ", (rc < 0) ? "unknown" : local, nsvc->nsi->nsip.local_port);
179 }
180
181 vty_out(vty, "Remote: %-4s, %5s %9s, %s ",
Max32f99712017-10-20 12:27:49 +0200182 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
Max4ce24c42017-10-23 15:09:23 +0200183 NS_DESC_A(nsvc->remote_state),
Max37f465e2017-10-24 16:04:55 +0200184 NS_DESC_B(nsvc->remote_state), gprs_ns_ll_str(nsvc));
Max95308592017-10-24 15:54:28 +0200185
Max37f465e2017-10-24 16:04:55 +0200186 vty_out(vty, "%s%s", nsvc->ll == GPRS_NS_LL_UDP ? "UDP" : "FR-GRE", VTY_NEWLINE);
Max95308592017-10-24 15:54:28 +0200187
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200188 if (stats) {
Harald Welte92883342010-05-15 23:04:03 +0200189 vty_out_rate_ctr_group(vty, " ", nsvc->ctrg);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200190 vty_out_stat_item_group(vty, " ", nsvc->statg);
191 }
Harald Welte92883342010-05-15 23:04:03 +0200192}
193
Maxcbf5cdf2017-10-24 18:18:55 +0200194static void dump_ns(struct vty *vty, const struct gprs_ns_inst *nsi, bool stats, bool persistent_only)
Harald Welte144e0292010-05-13 11:45:07 +0200195{
Harald Welte144e0292010-05-13 11:45:07 +0200196 struct gprs_nsvc *nsvc;
Harald Welte7fb05232010-05-19 15:09:09 +0200197 struct in_addr ia;
198
Harald Weltebfe62e52017-05-15 12:48:30 +0200199 ia.s_addr = osmo_htonl(vty_nsi->nsip.local_ip);
Harald Weltece4ccbc2010-05-19 17:02:57 +0200200 vty_out(vty, "Encapsulation NS-UDP-IP Local IP: %s, UDP Port: %u%s",
Harald Welte7fb05232010-05-19 15:09:09 +0200201 inet_ntoa(ia), vty_nsi->nsip.local_port, VTY_NEWLINE);
202
Harald Weltedc802812019-02-23 11:10:08 +0100203 if (nsi->frgre.enabled) {
204 ia.s_addr = osmo_htonl(vty_nsi->frgre.local_ip);
205 vty_out(vty, "Encapsulation NS-FR-GRE-IP Local IP: %s%s", inet_ntoa(ia), VTY_NEWLINE);
206 }
Harald Welte144e0292010-05-13 11:45:07 +0200207
208 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200209 if (nsvc == nsi->unknown_nsvc)
210 continue;
Max296511b2017-10-24 18:05:35 +0200211 dump_nse(vty, nsvc, stats, persistent_only);
Harald Welte144e0292010-05-13 11:45:07 +0200212 }
Harald Welte047f3872018-07-01 21:04:45 +0200213
214 gprs_sns_dump_vty(vty, nsi, stats);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200215}
Harald Welte144e0292010-05-13 11:45:07 +0200216
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200217DEFUN(show_ns, show_ns_cmd, "show ns",
218 SHOW_STR "Display information about the NS protocol")
219{
220 struct gprs_ns_inst *nsi = vty_nsi;
Max296511b2017-10-24 18:05:35 +0200221 dump_ns(vty, nsi, false, false);
Harald Welte144e0292010-05-13 11:45:07 +0200222 return CMD_SUCCESS;
223}
224
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200225DEFUN(show_ns_stats, show_ns_stats_cmd, "show ns stats",
226 SHOW_STR
227 "Display information about the NS protocol\n"
228 "Include statistics\n")
229{
230 struct gprs_ns_inst *nsi = vty_nsi;
Max296511b2017-10-24 18:05:35 +0200231 dump_ns(vty, nsi, true, false);
232 return CMD_SUCCESS;
233}
234
235DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent",
236 SHOW_STR
237 "Display information about the NS protocol\n"
238 "Show only persistent NS\n")
239{
240 struct gprs_ns_inst *nsi = vty_nsi;
241 dump_ns(vty, nsi, true, true);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200242 return CMD_SUCCESS;
243}
Harald Welte144e0292010-05-13 11:45:07 +0200244
Harald Welte92883342010-05-15 23:04:03 +0200245DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]",
246 SHOW_STR "Display information about the NS protocol\n"
247 "Select one NSE by its NSE Identifier\n"
248 "Select one NSE by its NS-VC Identifier\n"
249 "The Identifier of selected type\n"
250 "Include Statistics\n")
251{
252 struct gprs_ns_inst *nsi = vty_nsi;
253 struct gprs_nsvc *nsvc;
254 uint16_t id = atoi(argv[1]);
Max296511b2017-10-24 18:05:35 +0200255 bool show_stats = false;
Harald Welte92883342010-05-15 23:04:03 +0200256
257 if (!strcmp(argv[0], "nsei"))
Harald Weltef5430362012-06-17 12:25:53 +0800258 nsvc = gprs_nsvc_by_nsei(nsi, id);
Harald Welte92883342010-05-15 23:04:03 +0200259 else
Harald Weltef5430362012-06-17 12:25:53 +0800260 nsvc = gprs_nsvc_by_nsvci(nsi, id);
Harald Welte92883342010-05-15 23:04:03 +0200261
262 if (!nsvc) {
263 vty_out(vty, "No such NS Entity%s", VTY_NEWLINE);
264 return CMD_WARNING;
265 }
266
267 if (argc >= 3)
Max296511b2017-10-24 18:05:35 +0200268 show_stats = true;
Harald Welte92883342010-05-15 23:04:03 +0200269
Max296511b2017-10-24 18:05:35 +0200270 dump_nse(vty, nsvc, show_stats, false);
Harald Welte92883342010-05-15 23:04:03 +0200271 return CMD_SUCCESS;
272}
273
Harald Welte24133c32010-05-15 23:06:26 +0200274#define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n"
Harald Welte144e0292010-05-13 11:45:07 +0200275
276DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
277 "nse <0-65535> nsvci <0-65534>",
278 NSE_CMD_STR
279 "NS Virtual Connection\n"
280 "NS Virtual Connection ID (NSVCI)\n"
281 )
282{
283 uint16_t nsei = atoi(argv[0]);
284 uint16_t nsvci = atoi(argv[1]);
285 struct gprs_nsvc *nsvc;
286
Harald Weltef5430362012-06-17 12:25:53 +0800287 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200288 if (!nsvc) {
Harald Weltef5430362012-06-17 12:25:53 +0800289 nsvc = gprs_nsvc_create(vty_nsi, nsvci);
Harald Welte144e0292010-05-13 11:45:07 +0200290 nsvc->nsei = nsei;
291 }
292 nsvc->nsvci = nsvci;
293 /* All NSVCs that are explicitly configured by VTY are
294 * marked as persistent so we can write them to the config
295 * file at some later point */
296 nsvc->persistent = 1;
297
298 return CMD_SUCCESS;
299}
300
301DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
302 "nse <0-65535> remote-ip A.B.C.D",
303 NSE_CMD_STR
304 "Remote IP Address\n"
305 "Remote IP Address\n")
306{
307 uint16_t nsei = atoi(argv[0]);
308 struct gprs_nsvc *nsvc;
309
Harald Weltef5430362012-06-17 12:25:53 +0800310 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200311 if (!nsvc) {
312 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
313 return CMD_WARNING;
314 }
315 inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr);
316
317 return CMD_SUCCESS;
318
319}
320
321DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
322 "nse <0-65535> remote-port <0-65535>",
323 NSE_CMD_STR
324 "Remote UDP Port\n"
325 "Remote UDP Port Number\n")
326{
327 uint16_t nsei = atoi(argv[0]);
328 uint16_t port = atoi(argv[1]);
329 struct gprs_nsvc *nsvc;
330
Harald Weltef5430362012-06-17 12:25:53 +0800331 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200332 if (!nsvc) {
333 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
334 return CMD_WARNING;
335 }
336
Harald Welteb3ee2652010-05-19 14:38:50 +0200337 if (nsvc->ll != GPRS_NS_LL_UDP) {
338 vty_out(vty, "Cannot set UDP Port on non-UDP NSE%s",
339 VTY_NEWLINE);
340 return CMD_WARNING;
341 }
342
Harald Weltebfe62e52017-05-15 12:48:30 +0200343 nsvc->ip.bts_addr.sin_port = osmo_htons(port);
Harald Welte144e0292010-05-13 11:45:07 +0200344
345 return CMD_SUCCESS;
346}
347
Harald Welteb3ee2652010-05-19 14:38:50 +0200348DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd,
Harald Welte188bda62010-05-28 14:11:49 +0200349 "nse <0-65535> fr-dlci <16-1007>",
Harald Welteb3ee2652010-05-19 14:38:50 +0200350 NSE_CMD_STR
351 "Frame Relay DLCI\n"
352 "Frame Relay DLCI Number\n")
353{
354 uint16_t nsei = atoi(argv[0]);
355 uint16_t dlci = atoi(argv[1]);
356 struct gprs_nsvc *nsvc;
357
Harald Weltef5430362012-06-17 12:25:53 +0800358 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welteb3ee2652010-05-19 14:38:50 +0200359 if (!nsvc) {
360 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
361 return CMD_WARNING;
362 }
363
364 if (nsvc->ll != GPRS_NS_LL_FR_GRE) {
365 vty_out(vty, "Cannot set FR DLCI on non-FR NSE%s",
366 VTY_NEWLINE);
367 return CMD_WARNING;
368 }
369
Harald Weltebfe62e52017-05-15 12:48:30 +0200370 nsvc->frgre.bts_addr.sin_port = osmo_htons(dlci);
Harald Welteb3ee2652010-05-19 14:38:50 +0200371
372 return CMD_SUCCESS;
373}
374
375DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd,
376 "nse <0-65535> encapsulation (udp|framerelay-gre)",
377 NSE_CMD_STR
378 "Encapsulation for NS\n"
379 "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n")
380{
381 uint16_t nsei = atoi(argv[0]);
382 struct gprs_nsvc *nsvc;
383
Harald Weltef5430362012-06-17 12:25:53 +0800384 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welteb3ee2652010-05-19 14:38:50 +0200385 if (!nsvc) {
386 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
387 return CMD_WARNING;
388 }
389
390 if (!strcmp(argv[1], "udp"))
391 nsvc->ll = GPRS_NS_LL_UDP;
392 else
393 nsvc->ll = GPRS_NS_LL_FR_GRE;
394
395 return CMD_SUCCESS;
396}
397
398
Harald Welte144e0292010-05-13 11:45:07 +0200399DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
400 "nse <0-65535> remote-role (sgsn|bss)",
401 NSE_CMD_STR
402 "Remote NSE Role\n"
403 "Remote Peer is SGSN\n"
404 "Remote Peer is BSS\n")
405{
406 uint16_t nsei = atoi(argv[0]);
407 struct gprs_nsvc *nsvc;
408
Harald Weltef5430362012-06-17 12:25:53 +0800409 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200410 if (!nsvc) {
411 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
412 return CMD_WARNING;
413 }
414
415 if (!strcmp(argv[1], "sgsn"))
416 nsvc->remote_end_is_sgsn = 1;
417 else
418 nsvc->remote_end_is_sgsn = 0;
419
420 return CMD_SUCCESS;
421}
422
423DEFUN(cfg_no_nse, cfg_no_nse_cmd,
424 "no nse <0-65535>",
Harald Welte24133c32010-05-15 23:06:26 +0200425 "Delete Persistent NS Entity\n"
Harald Welte144e0292010-05-13 11:45:07 +0200426 "Delete " NSE_CMD_STR)
427{
428 uint16_t nsei = atoi(argv[0]);
429 struct gprs_nsvc *nsvc;
430
Harald Weltef5430362012-06-17 12:25:53 +0800431 nsvc = gprs_nsvc_by_nsei(vty_nsi, nsei);
Harald Welte144e0292010-05-13 11:45:07 +0200432 if (!nsvc) {
433 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
434 return CMD_WARNING;
435 }
436
Harald Welte24133c32010-05-15 23:06:26 +0200437 if (!nsvc->persistent) {
438 vty_out(vty, "NSEI %u is not a persistent NSE%s",
439 nsei, VTY_NEWLINE);
440 return CMD_WARNING;
441 }
442
443 nsvc->persistent = 0;
Harald Welte144e0292010-05-13 11:45:07 +0200444
445 return CMD_SUCCESS;
446}
447
448DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
449 "timer " NS_TIMERS " <0-65535>",
450 "Network Service Timer\n"
451 NS_TIMERS_HELP "Timer Value\n")
452{
453 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
454 int val = atoi(argv[1]);
455
456 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
457 return CMD_WARNING;
458
459 vty_nsi->timeout[idx] = val;
460
461 return CMD_SUCCESS;
462}
463
Harald Welte7fb05232010-05-19 15:09:09 +0200464#define ENCAPS_STR "NS encapsulation options\n"
465
466DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd,
467 "encapsulation udp local-ip A.B.C.D",
468 ENCAPS_STR "NS over UDP Encapsulation\n"
469 "Set the IP address on which we listen for NS/UDP\n"
470 "IP Address\n")
471{
472 struct in_addr ia;
473
474 inet_aton(argv[0], &ia);
Harald Weltebfe62e52017-05-15 12:48:30 +0200475 vty_nsi->nsip.local_ip = osmo_ntohl(ia.s_addr);
Harald Welte7fb05232010-05-19 15:09:09 +0200476
477 return CMD_SUCCESS;
478}
479
480DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd,
481 "encapsulation udp local-port <0-65535>",
482 ENCAPS_STR "NS over UDP Encapsulation\n"
483 "Set the UDP port on which we listen for NS/UDP\n"
484 "UDP port number\n")
485{
486 unsigned int port = atoi(argv[0]);
487
488 vty_nsi->nsip.local_port = port;
489
490 return CMD_SUCCESS;
491}
492
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100493DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd,
494 "encapsulation udp dscp <0-255>",
495 ENCAPS_STR "NS over UDP Encapsulation\n"
496 "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n")
497{
498 int dscp = atoi(argv[0]);
499 vty_nsi->nsip.dscp = dscp;
500 return CMD_SUCCESS;
501}
502
Harald Welte7fb05232010-05-19 15:09:09 +0200503DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd,
504 "encapsulation framerelay-gre local-ip A.B.C.D",
505 ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n"
506 "Set the IP address on which we listen for NS/FR/GRE\n"
507 "IP Address\n")
508{
509 struct in_addr ia;
510
Harald Welte7fb05232010-05-19 15:09:09 +0200511 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
Harald Welte047f3872018-07-01 21:04:45 +0200559 if (nsvc->nsi->bss_sns_fi) {
560 vty_out(vty, "A NS Instance using the IP Sub-Network doesn't use BLOCK/UNBLOCK/RESET%s",
561 VTY_NEWLINE);
562 return CMD_WARNING;
563 }
564
Harald Welte43f3b692010-05-14 19:36:59 +0200565 if (!strcmp(operation, "block"))
566 gprs_ns_tx_block(nsvc, NS_CAUSE_OM_INTERVENTION);
567 else if (!strcmp(operation, "unblock"))
568 gprs_ns_tx_unblock(nsvc);
569 else if (!strcmp(operation, "reset"))
570 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
571 else
572 return CMD_WARNING;
573
574 return CMD_SUCCESS;
575}
576
Harald Welte91f7f4b2010-05-15 23:52:02 +0200577DEFUN(logging_fltr_nsvc,
578 logging_fltr_nsvc_cmd,
579 "logging filter nsvc (nsei|nsvci) <0-65535>",
Harald Welte6703bf72010-05-16 00:00:04 +0200580 LOGGING_STR FILTER_STR
Harald Welte91f7f4b2010-05-15 23:52:02 +0200581 "Filter based on NS Virtual Connection\n"
582 "Identify NS-VC by NSEI\n"
583 "Identify NS-VC by NSVCI\n"
584 "Numeric identifier\n")
585{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200586 struct log_target *tgt;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200587 struct gprs_nsvc *nsvc;
588 uint16_t id = atoi(argv[1]);
589
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200590 log_tgt_mutex_lock();
591 tgt = osmo_log_vty2tgt(vty);
592 if (!tgt) {
593 log_tgt_mutex_unlock();
Harald Welte91f7f4b2010-05-15 23:52:02 +0200594 return CMD_WARNING;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200595 }
Harald Welte91f7f4b2010-05-15 23:52:02 +0200596
597 if (!strcmp(argv[0], "nsei"))
Harald Weltef5430362012-06-17 12:25:53 +0800598 nsvc = gprs_nsvc_by_nsei(vty_nsi, id);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200599 else
Harald Weltef5430362012-06-17 12:25:53 +0800600 nsvc = gprs_nsvc_by_nsvci(vty_nsi, id);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200601
602 if (!nsvc) {
603 vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200604 log_tgt_mutex_unlock();
Harald Welte91f7f4b2010-05-15 23:52:02 +0200605 return CMD_WARNING;
606 }
607
Harald Welte43ae94e2011-02-18 21:10:05 +0100608 log_set_nsvc_filter(tgt, nsvc);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200609 log_tgt_mutex_unlock();
Harald Welte91f7f4b2010-05-15 23:52:02 +0200610 return CMD_SUCCESS;
611}
Harald Welte43f3b692010-05-14 19:36:59 +0200612
Harald Welte144e0292010-05-13 11:45:07 +0200613int gprs_ns_vty_init(struct gprs_ns_inst *nsi)
614{
Neels Hofmeyrc32bfd52017-01-12 22:32:19 +0100615 static bool vty_elements_installed = false;
616
Harald Welte144e0292010-05-13 11:45:07 +0200617 vty_nsi = nsi;
618
Neels Hofmeyrc32bfd52017-01-12 22:32:19 +0100619 /* Regression test code may call this function repeatedly, so make sure
620 * that VTY elements are not duplicated, which would assert. */
621 if (vty_elements_installed)
622 return 0;
623 vty_elements_installed = true;
624
Harald Welte144e0292010-05-13 11:45:07 +0200625 install_element_ve(&show_ns_cmd);
Harald Welteaf1d4cb2010-05-13 12:32:30 +0200626 install_element_ve(&show_ns_stats_cmd);
Max296511b2017-10-24 18:05:35 +0200627 install_element_ve(&show_ns_pers_cmd);
Harald Welte92883342010-05-15 23:04:03 +0200628 install_element_ve(&show_nse_cmd);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200629 install_element_ve(&logging_fltr_nsvc_cmd);
Harald Welte144e0292010-05-13 11:45:07 +0200630
Harald Welte43ae94e2011-02-18 21:10:05 +0100631 install_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd);
632
Harald Welte144e0292010-05-13 11:45:07 +0200633 install_element(CONFIG_NODE, &cfg_ns_cmd);
634 install_node(&ns_node, config_write_ns);
Harald Welte4f5883b2012-06-16 16:54:06 +0800635 install_element(L_NS_NODE, &cfg_nse_nsvci_cmd);
636 install_element(L_NS_NODE, &cfg_nse_remoteip_cmd);
637 install_element(L_NS_NODE, &cfg_nse_remoteport_cmd);
638 install_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd);
639 install_element(L_NS_NODE, &cfg_nse_encaps_cmd);
640 install_element(L_NS_NODE, &cfg_nse_remoterole_cmd);
641 install_element(L_NS_NODE, &cfg_no_nse_cmd);
642 install_element(L_NS_NODE, &cfg_ns_timer_cmd);
643 install_element(L_NS_NODE, &cfg_nsip_local_ip_cmd);
644 install_element(L_NS_NODE, &cfg_nsip_local_port_cmd);
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +0100645 install_element(L_NS_NODE, &cfg_nsip_dscp_cmd);
Harald Welte4f5883b2012-06-16 16:54:06 +0800646 install_element(L_NS_NODE, &cfg_frgre_enable_cmd);
647 install_element(L_NS_NODE, &cfg_frgre_local_ip_cmd);
Harald Welte144e0292010-05-13 11:45:07 +0200648
Harald Welte43f3b692010-05-14 19:36:59 +0200649 install_element(ENABLE_NODE, &nsvc_nsei_cmd);
650
Harald Welte144e0292010-05-13 11:45:07 +0200651 return 0;
652}