lynxis lazus has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/21487 )


Change subject: WIP: gprs_ns2: add new vty2
......................................................................

WIP: gprs_ns2: add new vty2

Change-Id: I163279cf57e84198dc8c53e1c109f5a9474670e9
---
M include/osmocom/gprs/gprs_ns2.h
M include/osmocom/vty/command.h
M src/gb/Makefile.am
M src/gb/gprs_ns2_internal.h
A src/gb/gprs_ns2_vty2.c
5 files changed, 1,061 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/87/21487/1

diff --git a/include/osmocom/gprs/gprs_ns2.h b/include/osmocom/gprs/gprs_ns2.h
index 942cb87..d73fd82 100644
--- a/include/osmocom/gprs/gprs_ns2.h
+++ b/include/osmocom/gprs/gprs_ns2.h
@@ -37,6 +37,7 @@
        NS2_DIALECT_STATIC_RESETBLOCK,
        NS2_DIALECT_IPACCESS,
        NS2_DIALECT_SNS,
+       NS2_DIALECT_UNDEF,
 };

 /*! Osmocom NS link layer types */
@@ -44,6 +45,7 @@
        GPRS_NS2_LL_UDP,        /*!< NS/UDP/IP */
        GPRS_NS2_LL_FR,         /*!< NS/FR */
        GPRS_NS2_LL_FR_GRE,     /*!< NS/FR/GRE/IP */
+       GPRS_NS2_LL_UNDEF,      /*!< undefined used by vty */
 };

 /*! Osmocom NS primitives according to 48.016 5.2 Service primitves */
diff --git a/include/osmocom/vty/command.h b/include/osmocom/vty/command.h
index add57e2..431e20b 100644
--- a/include/osmocom/vty/command.h
+++ b/include/osmocom/vty/command.h
@@ -100,6 +100,9 @@
        L_CS7_SCCPADDR_GT_NODE, /*!< SS7 SCCP Global Title */

        L_CPU_SCHED_NODE,               /*!< CPU Sched related options node */
+       L_FR_NODE,
+       L_NS_BIND_NODE,
+       L_NS_NSE_NODE,
        /*
         * When adding new nodes to the libosmocore project, these nodes can be
         * used to avoid ABI changes for unrelated projects.
diff --git a/src/gb/Makefile.am b/src/gb/Makefile.am
index 41b6c6d..e393a44 100644
--- a/src/gb/Makefile.am
+++ b/src/gb/Makefile.am
@@ -22,7 +22,7 @@
                  gprs_bssgp.c gprs_bssgp_util.c gprs_bssgp_vty.c \
                  gprs_bssgp_bss.c \
                  gprs_ns2.c gprs_ns2_udp.c gprs_ns2_frgre.c gprs_ns2_fr.c 
gprs_ns2_vc_fsm.c gprs_ns2_sns.c \
-                 gprs_ns2_message.c gprs_ns2_vty.c \
+                 gprs_ns2_message.c gprs_ns2_vty.c gprs_ns2_vty2.c \
                  common_vty.c frame_relay.c
 endif

diff --git a/src/gb/gprs_ns2_internal.h b/src/gb/gprs_ns2_internal.h
index a8a4321..6d08c17 100644
--- a/src/gb/gprs_ns2_internal.h
+++ b/src/gb/gprs_ns2_internal.h
@@ -181,12 +181,18 @@

 /*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
 struct gprs_ns2_vc_bind {
+       /*! unique name */
+       const char *name;
+
        /*! list entry in nsi */
        struct llist_head list;
+
        /*! list of all VC */
        struct llist_head nsvc;
+
        /*! driver private structure */
        void *priv;
+
        /*! a pointer back to the nsi */
        struct gprs_ns2_inst *nsi;
        struct gprs_ns2_vc_driver *driver;
diff --git a/src/gb/gprs_ns2_vty2.c b/src/gb/gprs_ns2_vty2.c
new file mode 100644
index 0000000..e53b589
--- /dev/null
+++ b/src/gb/gprs_ns2_vty2.c
@@ -0,0 +1,1049 @@
+/*! \file gprs_ns2_vty.c
+ * VTY interface for our GPRS Networks Service (NS) implementation. */
+
+/* (C) 2020 by sysmocom - s.f.m.c. GmbH <i...@sysmocom.de>
+ * Author: Alexander Couzens <lyn...@fe80.eu>
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdint.h>
+
+#include <arpa/inet.h>
+#include <net/if.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/byteswap.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/rate_ctr.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/sockaddr_str.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/gprs/frame_relay.h>
+#include <osmocom/gprs/gprs_ns2.h>
+#include <osmocom/gsm/tlv.h>
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+#include <osmocom/vty/logging.h>
+#include <osmocom/vty/telnet_interface.h>
+#include <osmocom/vty/misc.h>
+
+#include "gprs_ns2_internal.h"
+
+static struct gprs_ns2_inst *vty_nsi = NULL;
+static struct osmo_fr_network *vty_fr_network = NULL;
+static struct llist_head binds;
+
+struct vty_bind {
+       struct llist_head list;
+       const char *name;
+       enum gprs_ns2_ll ll;
+       int dscp;
+       bool accept_ipaccess;
+       bool accept_sns;
+};
+
+static struct vty_bind *vty_bind_by_name(const char *name)
+{
+       struct vty_bind *vbind;
+       llist_for_each_entry(vbind, &binds, list) {
+               if (!strncmp(vbind->name, name, strlen(vbind->name)))
+                       return vbind;
+       }
+       return NULL;
+}
+
+static struct vty_bind *vty_bind_alloc(const char *name)
+{
+       struct vty_bind *vbind = talloc(vty_nsi, struct vty_bind);
+       if (!vbind)
+               return NULL;
+
+       vbind->name = talloc_strdup(vty_nsi, name);
+       if (!vbind->name) {
+               talloc_free(vbind);
+               return NULL;
+       }
+
+       llist_add(&vbind->list, &binds);
+       return vbind;
+}
+
+static void vty_bind_free(struct vty_bind *vbind)
+{
+       if (!vbind)
+               return;
+
+       llist_del(&vbind->list);
+}
+
+static struct cmd_node fr_node = {
+       L_FR_NODE,
+       "%s(config-fr)# ",
+       1,
+};
+
+DEFUN(cfg_fr, cfg_fr_cmd,
+      "fr",
+      "Configure frame relay interface\n"
+      )
+{
+       vty->node = L_FR_NODE;
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_fr_link, cfg_fr_link_cmd,
+      "link NETIF (fr|frnet)",
+      "link\n"
+      IFNAME_STR
+      "framrelay role. fr is used by BSS, frnet is used by frnet\n")
+{
+       struct gprs_ns2_vc_bind *fr;
+       const char *netif = argv[0];
+       const char *role = argv[1];
+
+       int rc = 0;
+       enum osmo_fr_role frrole;
+
+       if (!strcmp(role, "fr"))
+               frrole = FR_ROLE_USER_EQUIPMENT;
+       else if (!strcmp(role, "frnet"))
+               frrole = FR_ROLE_NETWORK_EQUIPMENT;
+
+       fr = gprs_ns2_fr_bind_by_netif(vty_nsi, netif);
+       if (fr) {
+               vty_out(vty, "Interface %s already used.\n", VTY_NEWLINE);
+               return CMD_ERR_NOTHING_TODO;
+       }
+
+       rc = gprs_ns2_fr_bind(vty_nsi, netif, vty_fr_network, frrole, &fr);
+       if (rc < 0) {
+               LOGP(DLNS, LOGL_ERROR, "Failed to bind interface %s on fr. Err: 
%d\n", netif, rc);
+               return CMD_WARNING;
+       }
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_fr_link, cfg_no_fr_link_cmd,
+      "no link NETIF",
+      "Delete a framerelay link\n"
+      "Delete a framerelay link\n"
+      IFNAME_STR
+      )
+{
+       struct gprs_ns2_vc_bind *fr;
+       const char *netif = argv[0];
+
+       fr = gprs_ns2_fr_bind_by_netif(vty_nsi, netif);
+       if (!fr) {
+               vty_out(vty, "Interface not found.%s", VTY_NEWLINE);
+               return CMD_ERR_NOTHING_TODO;
+       }
+
+       fr->driver->free_bind(fr);
+       return CMD_SUCCESS;
+}
+
+static int config_write_fr(struct vty *vty)
+{
+       return 0;
+}
+
+static struct cmd_node ns_node = {
+       L_NS_NODE,
+       "%s(config-ns)# ",
+       1,
+};
+
+DEFUN(cfg_ns, cfg_ns_cmd,
+      "ns",
+      "Configure the GPRS Network Service")
+{
+       vty->node = L_NS_NODE;
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_ns_nsei, cfg_ns_nsei_cmd,
+      "nse <0-65535>",
+      "Persistent NS Entity\n"
+      "NS Entity ID (NSEI)\n"
+      "NS-E type\n"
+      )
+{
+       struct gprs_ns2_nse *nse;
+       uint16_t nsei = atoi(argv[0]);
+
+       nse = gprs_ns2_nse_by_nsei(vty_nsi, nsei);
+       if (!nse) {
+               nse = gprs_ns2_create_nse(vty_nsi, nsei, GPRS_NS2_LL_UNDEF, 
NS2_DIALECT_UNDEF);
+               if (!nse) {
+                       vty_out(vty, "Failed to create NSE!%s", VTY_NEWLINE);
+                       return CMD_ERR_INCOMPLETE;
+               }
+               nse->persistent = true;
+       }
+
+       if (!nse->persistent) {
+               vty_out(vty, "A dynamic NSE already exists%s", VTY_NEWLINE);
+               return CMD_ERR_INCOMPLETE;
+       }
+
+       vty->node = L_NS_NSE_NODE;
+       vty->index = nse;
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_ns_nsei, cfg_no_ns_nsei_cmd,
+      "no nse <0-65535>",
+      "Delete a Persistent NS Entity\n"
+      "Delete a Persistent NS Entity\n"
+      "NS Entity ID (NSEI)\n"
+      "NS-E type\n"
+      )
+{
+       struct gprs_ns2_nse *nse;
+       uint16_t nsei = atoi(argv[0]);
+
+       nse = gprs_ns2_nse_by_nsei(vty_nsi, nsei);
+       if (!nse) {
+               vty_out(vty, "Can not find NS Entity %s%s", argv[0], 
VTY_NEWLINE);
+               return CMD_ERR_NOTHING_TODO;
+       }
+
+       if (!nse->persistent) {
+               vty_out(vty, "Ignoring non-persistent NS Entity%s", 
VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       vty_out(vty, "Deleting NS Entity %d%s", nse->nsei, VTY_NEWLINE);
+       gprs_ns2_free_nse(nse);
+       return CMD_SUCCESS;
+}
+
+/* TODO: add fr/gre */
+DEFUN(cfg_ns_bind, cfg_ns_bind_cmd,
+      "bind (frgre|udp) ID",
+      "Binding\n"
+      "UDP or FR over GRE\n"
+      "a unique identifier for this bind to reference NS-VCs\n"
+      )
+{
+       const char *nstype = argv[0];
+       const char *name = argv[1];
+       struct vty_bind *vbind;
+
+       if (!strcmp("frgre", nstype)) {
+               vty_out(vty, "fr/gre is unsupported%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       } else if (!strcmp("udp", nstype)) {
+               vbind = vty_bind_by_name(name);
+               if (!vbind) {
+                       vbind = vty_bind_alloc(name);
+                       if (!vbind) {
+                               vty_out(vty, "Can not create bind - out of 
memory%s", VTY_NEWLINE);
+                               return CMD_WARNING;
+                       }
+                       vbind->ll = GPRS_NS2_LL_UDP;
+               } else {
+                       if (vbind->ll != GPRS_NS2_LL_UDP) {
+                               vty_out(vty, "A bind with the ID already exists 
with a different type (frgre|udp)!%s",
+                                       VTY_NEWLINE);
+                               return CMD_WARNING;
+                       }
+               }
+               vty->index = vbind;
+               vty->node = L_NS_BIND_NODE;
+               return CMD_SUCCESS;
+       }
+
+       return CMD_WARNING;
+}
+
+DEFUN(cfg_no_ns_bind, cfg_no_ns_bind_cmd,
+      "no bind (frgre|udp) ID",
+      "Delete a binding\n"
+      "Delete a binding\n"
+      "UDP or FR over GRE\n"
+      "a unique identifier for this bind to reference NS-VCs\n"
+      )
+{
+       struct vty_bind *vbind;
+       struct gprs_ns2_vc_bind *bind;
+       const char *name = argv[1];
+
+       vbind = vty_bind_by_name(name);
+       if (!vbind) {
+               vty_out(vty, "bind %s does not exist!%s", name, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+       vty_bind_free(vbind);
+       bind = gprs_ns2_bind_by_name(vty_nsi, name);
+       if (bind)
+               bind->driver->free_bind(bind);
+       return CMD_SUCCESS;
+}
+
+static int config_write_ns(struct vty *vty)
+{
+       return 0;
+}
+
+static struct cmd_node ns_bind_node = {
+       L_NS_BIND_NODE,
+       "%s(config-ns-bind)# ",
+       1,
+};
+
+DEFUN(cfg_ns_bind_listen, cfg_ns_bind_listen_cmd,
+      "listen " VTY_IPV46_CMD " <1-65535>",
+      "Binding\n"
+      "IPv4 Address\n" "IPv6 Address\n"
+      "Port\n"
+      )
+{
+       struct vty_bind *vbind = vty->index;
+       struct gprs_ns2_vc_bind *bind;
+
+       const char *addr_str = argv[0];
+       unsigned int port = atoi(argv[1]);
+       struct osmo_sockaddr_str sockaddr_str;
+       struct osmo_sockaddr sockaddr;
+
+       if (osmo_sockaddr_str_from_str(&sockaddr_str, addr_str, port)) {
+               vty_out(vty, "Can not parse the Address %s %s%s",
+                       argv[0], argv[1], VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+       osmo_sockaddr_str_to_sockaddr(&sockaddr_str, &sockaddr.u.sas);
+       /* TODO: how to react with a changed bind ip?
+        * a bind can only have a single listen! */
+
+       if (gprs_ns2_ip_bind_by_sockaddr(vty_nsi, &sockaddr)) {
+               vty_out(vty, "A bind with the given address already exists!%s", 
VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (gprs_ns2_ip_bind(vty_nsi, &sockaddr, vbind->dscp, &bind) != 0) {
+               vty_out(vty, "Failed to create the bind!%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       bind->accept_ipaccess = vbind->accept_ipaccess;
+       bind->accept_sns = vbind->accept_sns;
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_ns_bind_listen, cfg_no_ns_bind_listen_cmd,
+      "no listen",
+      "Delete a IP/Port assignment\n"
+      "Delete a IP/Port assignment\n"
+      "IPv4 Address\n" "IPv6 Address\n"
+      "Port\n"
+      )
+{
+       struct vty_bind *vbind = vty->index;
+       struct gprs_ns2_vc_bind *bind;
+
+       bind = gprs_ns2_bind_by_name(vty_nsi, vbind->name);
+       if (!bind)
+               return CMD_ERR_NOTHING_TODO;
+
+       bind->driver->free_bind(bind);
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_ns_bind_ipaccess, cfg_ns_bind_ipaccess_cmd,
+      "accept ipaccess",
+      "Allow to create dynamic NS Entity by NS Reset PDU on UDP (ip.access 
style)\n"
+      )
+{
+       struct vty_bind *vbind = vty->index;
+       struct gprs_ns2_vc_bind *bind;
+
+       vbind->accept_ipaccess = true;
+       bind = gprs_ns2_bind_by_name(vty_nsi, vbind->name);
+       if (bind)
+               bind->accept_ipaccess = true;
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_ns_ipaccess, cfg_no_ns_ipaccess_cmd,
+      "no accept ipaccess",
+      "Reject NS Reset PDU on UDP (ip.access style)\n"
+      "Reject NS Reset PDU on UDP (ip.access style)\n"
+      )
+{
+       struct vty_bind *vbind = vty->index;
+       struct gprs_ns2_vc_bind *bind;
+
+       vbind->accept_ipaccess = false;
+       bind = gprs_ns2_bind_by_name(vty_nsi, vbind->name);
+       if (bind)
+               bind->accept_ipaccess = false;
+
+       return CMD_SUCCESS;
+}
+
+static int config_write_ns_bind(struct vty *vty)
+{
+       return 0;
+}
+
+static struct cmd_node ns_nse_node = {
+       L_NS_NSE_NODE,
+       "%s(config-ns-nse)# ",
+       1,
+};
+
+DEFUN(cfg_ns_nse_nsvc_fr, cfg_ns_nse_nsvc_fr_cmd,
+      "nsvc fr NETIF nsvci <0-65535> dlci <0-1023>",
+      "NS Virtual Connection\n"
+      "framerelay\n"
+      "frame relay interface. Must be registered via fr vty\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "Data Link connection identifier\n"
+      "Data Link connection identifier\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       const char *netif = argv[0];
+       uint16_t nsvci = atoi(argv[1]);
+       uint16_t dlci = atoi(argv[2]);
+       bool dialect_modified = false;
+       bool ll_modified = false;
+
+       if (nse->ll != GPRS_NS2_LL_FR && nse->ll != GPRS_NS2_LL_UNDEF) {
+               vty_out(vty, "Can not mix NSVC with different link layer%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (nse->dialect != NS2_DIALECT_STATIC_RESETBLOCK && nse->dialect != 
NS2_DIALECT_UNDEF) {
+               vty_out(vty, "Can not mix NS VC with different dailects%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (nse->ll == GPRS_NS2_LL_UNDEF) {
+               nse->ll = GPRS_NS2_LL_FR;
+               ll_modified = true;
+       }
+
+       if (nse->dialect == NS2_DIALECT_UNDEF) {
+               nse->dialect = NS2_DIALECT_STATIC_RESETBLOCK;
+               dialect_modified = true;
+       }
+
+
+       bind = gprs_ns2_fr_bind_by_netif(vty_nsi, netif);
+       if (!bind) {
+               vty_out(vty, "Can not find fr interface \"%s\". Please 
configure it via fr vty.%s",
+                       netif, VTY_NEWLINE);
+               goto err;
+       }
+
+       if (gprs_ns2_fr_nsvc_by_dlci(bind, dlci)) {
+               vty_out(vty, "A NS VC with the DLCI already exist!%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (gprs_ns2_nsvc_by_nsvci(vty_nsi, nsvci)) {
+               vty_out(vty, "A NS VC with the NSVCI already exist!%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       nsvc = gprs_ns2_fr_connect(bind, nse, nsvci, dlci);
+       if (!nsvc) {
+               /* Could not create NSVC, connect failed */
+               vty_out(vty, "Failed to create the NSVC%s", VTY_NEWLINE);
+               goto err;
+       }
+       nsvc->persistent = true;
+       return CMD_SUCCESS;
+
+err:
+       if (ll_modified)
+               nse->ll = GPRS_NS2_LL_UNDEF;
+       if (dialect_modified)
+               nse->dialect = NS2_DIALECT_UNDEF;
+
+       return CMD_WARNING;
+}
+
+DEFUN(cfg_no_ns_nse_nsvc_fr, cfg_no_ns_nse_nsvc_fr_cmd,
+      "no nsvc fr NETIF nsvci <0-65535> dlci <0-1023>",
+      "Delete framerelay NS VC\n"
+      "Delete framerelay NS VC\n"
+      "framerelay\n"
+      "frame relay interface. Must be registered via fr vty\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "Data Link connection identifier\n"
+      "Data Link connection identifier\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       const char *netif = argv[0];
+       uint16_t nsvci = atoi(argv[1]);
+       uint16_t vty_dlci = atoi(argv[2]);
+       uint16_t dlci;
+
+       bind = gprs_ns2_fr_bind_by_netif(vty_nsi, netif);
+       if (!bind) {
+               vty_out(vty, "Can not find fr interface \"%s\"%s",
+                       netif, VTY_NEWLINE);
+               return CMD_ERR_NOTHING_TODO;
+       }
+
+       nsvc = gprs_ns2_nsvc_by_nsvci(vty_nsi, nsvci);
+       if (!nsvc) {
+               vty_out(vty, "Can not find NS VC with NSVCI %d%s", nsvci, 
VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nsvc->bind != bind) {
+               vty_out(vty, "NS VC with NSVCI %d is not bound to fr interface 
%s%s",
+                       nsvci, netif, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       dlci = gprs_ns2_fr_nsvc_dlci(nsvc);
+       if (dlci != vty_dlci) {
+               vty_out(vty, "NS VC with NSVCI %d have the wrong dlci %d. 
Expected %d%s",
+                       nsvci, dlci, vty_dlci, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nse != nsvc->nse) {
+               vty_out(vty, "NSVC with NSVCI %d dlci %d is not part of this 
NSE!%s",
+                       nsvci, dlci, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       gprs_ns2_free_nsvc(nsvc);
+       if (llist_empty(&nse->nsvc)) {
+               nse->ll = GPRS_NS2_LL_UNDEF;
+               nse->dialect = NS2_DIALECT_UNDEF;
+       }
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_ns_nse_nsvc_udp, cfg_ns_nse_nsvc_udp_cmd,
+      "nsvc udp BIND " VTY_IPV46_CMD " <1-65535>",
+      "NS Virtual Connection\n"
+      "NS over UDP\n"
+      "A unique bind identifier created by ns bind\n"
+      "Remote IPv4 Address\n" "Remote IPv6 Address\n"
+      "Remote UDP Port\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       bool dialect_modified = false;
+       bool ll_modified = false;
+
+       const char *bind_name = argv[0];
+       struct osmo_sockaddr_str remote_str;
+       struct osmo_sockaddr remote;
+       uint16_t port = atoi(argv[2]);
+
+       if (nse->ll == GPRS_NS2_LL_UNDEF) {
+               nse->ll = GPRS_NS2_LL_UDP;
+               ll_modified = true;
+       }
+
+       if (nse->dialect == NS2_DIALECT_UNDEF) {
+               nse->dialect = NS2_DIALECT_STATIC_ALIVE;
+               dialect_modified = true;
+       }
+
+       if (nse->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Can not mix NS VC with different link layer%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (nse->dialect != NS2_DIALECT_STATIC_ALIVE) {
+               vty_out(vty, "Can not mix NS VC with different dailects%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (osmo_sockaddr_str_from_str(&remote_str, argv[1], port)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               goto err;
+       }
+
+       if (osmo_sockaddr_str_to_sockaddr(&remote_str, &remote.u.sas)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               goto err;
+       }
+
+       bind = gprs_ns2_bind_by_name(vty_nsi, bind_name);
+       if (!bind) {
+               vty_out(vty, "Can not find bind with name %s%s",
+                       bind_name, VTY_NEWLINE);
+               goto err;
+       }
+
+       if (bind->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Bind %s is not an UDP bind.%s",
+                       bind_name, VTY_NEWLINE);
+               goto err;
+       }
+
+       nsvc = gprs_ns2_ip_connect(bind, &remote, nse, 0);
+       if (!nsvc) {
+               vty_out(vty, "Can not create NS VC.%s", VTY_NEWLINE);
+               goto err;
+       }
+       nsvc->persistent = true;
+
+       return CMD_SUCCESS;
+
+err:
+       if (ll_modified)
+               nse->ll = GPRS_NS2_LL_UNDEF;
+       if (dialect_modified)
+               nse->dialect = NS2_DIALECT_UNDEF;
+       return CMD_WARNING;
+}
+
+DEFUN(cfg_no_ns_nse_nsvc_udp, cfg_no_ns_nse_nsvc_udp_cmd,
+      "no nsvc udp BIND " VTY_IPV46_CMD " <1-65535>",
+      "Delete a NS Virtual Connection\n"
+      "Delete a NS Virtual Connection\n"
+      "NS over UDP\n"
+      "A unique bind identifier created by ns bind\n"
+      "Remote IPv4 Address\n" "Remote IPv6 Address\n"
+      "Remote UDP Port\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       const char *bind_name = argv[0];
+       struct osmo_sockaddr_str remote_str;
+       struct osmo_sockaddr remote;
+       uint16_t port = atoi(argv[2]);
+
+       if (nse->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "NSE is not an UDP NSE%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nse->dialect != NS2_DIALECT_STATIC_ALIVE) {
+               vty_out(vty, "Can not mix NS VC with different dailects%s", 
VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       bind = gprs_ns2_bind_by_name(vty_nsi, bind_name);
+       if (!bind) {
+               vty_out(vty, "Can not find bind with name %s%s",
+                       bind_name, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (bind->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Bind %s is not an UDP bind.%s",
+                       bind_name, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (osmo_sockaddr_str_from_str(&remote_str, argv[1], port)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (osmo_sockaddr_str_to_sockaddr(&remote_str, &remote.u.sas)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, &remote);
+       if (!nsvc) {
+               vty_out(vty, "Can not find NS VC with remote %s:%d%s",
+                       remote_str.ip, remote_str.port, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (!nsvc->persistent) {
+               vty_out(vty, "NSVC with remote %s:%d is a dynamic NSVC. Not 
configured by vty.%s",
+                       remote_str.ip, remote_str.port, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nsvc->nse != nse) {
+               vty_out(vty, "NSVC is not part of this NSE!%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       gprs_ns2_free_nsvc(nsvc);
+       if (llist_empty(&nse->nsvc)) {
+               nse->ll = GPRS_NS2_LL_UNDEF;
+               nse->dialect = NS2_DIALECT_UNDEF;
+       }
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_ns_nse_nsvc_ipa, cfg_ns_nse_nsvc_ipa_cmd,
+      "nsvc ipa BIND nsvci <0-65535> " VTY_IPV46_CMD " <1-65535>",
+      "NS Virtual Connection\n"
+      "NS over UDP ip.access style (uses RESET/BLOCK)\n"
+      "A unique bind identifier created by ns bind\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "Remote IPv4 Address\n" "Remote IPv6 Address\n"
+      "Remote UDP Port\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       bool dialect_modified = false;
+       bool ll_modified = false;
+
+       const char *bind_name = argv[0];
+       uint16_t nsvci = atoi(argv[1]);
+       struct osmo_sockaddr_str remote_str;
+       struct osmo_sockaddr remote;
+       uint16_t port = atoi(argv[3]);
+
+       if (nse->ll == GPRS_NS2_LL_UNDEF) {
+               nse->ll = GPRS_NS2_LL_UDP;
+               ll_modified = true;
+       }
+
+       if (nse->dialect == NS2_DIALECT_UNDEF) {
+               nse->dialect = NS2_DIALECT_IPACCESS;
+               dialect_modified = true;
+       }
+
+       if (nse->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Can not mix NS VC with different link layer%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (nse->dialect != NS2_DIALECT_IPACCESS) {
+               vty_out(vty, "Can not mix NS VC with different dailects%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (osmo_sockaddr_str_from_str(&remote_str, argv[2], port)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               goto err;
+       }
+
+       if (osmo_sockaddr_str_to_sockaddr(&remote_str, &remote.u.sas)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               goto err;
+       }
+
+       bind = gprs_ns2_bind_by_name(vty_nsi, bind_name);
+       if (!bind) {
+               vty_out(vty, "Can not find bind with name %s%s",
+                       bind_name, VTY_NEWLINE);
+               goto err;
+       }
+
+       if (bind->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Bind %s is not an UDP bind.%s",
+                       bind_name, VTY_NEWLINE);
+               goto err;
+       }
+
+       nsvc = gprs_ns2_ip_connect(bind, &remote, nse, nsvci);
+       if (!nsvc) {
+               vty_out(vty, "Can not create NS VC.%s", VTY_NEWLINE);
+               goto err;
+       }
+       nsvc->persistent = true;
+
+       return CMD_SUCCESS;
+
+err:
+       if (ll_modified)
+               nse->ll = GPRS_NS2_LL_UNDEF;
+       if (dialect_modified)
+               nse->dialect = NS2_DIALECT_UNDEF;
+       return CMD_WARNING;
+}
+
+DEFUN(cfg_no_ns_nse_nsvc_ipa, cfg_no_ns_nse_nsvc_ipa_cmd,
+      "no nsvc ipa BIND nsvci <0-65535> " VTY_IPV46_CMD " <1-65535>",
+      "Delete a NS Virtual Connection\n"
+      "Delete a NS Virtual Connection\n"
+      "NS over UDP\n"
+      "A unique bind identifier created by ns bind\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "NS Virtual Connection ID (NSVCI)\n"
+      "Remote IPv4 Address\n" "Remote IPv6 Address\n"
+      "Remote UDP Port\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       const char *bind_name = argv[0];
+       uint16_t nsvci = atoi(argv[1]);
+       struct osmo_sockaddr_str remote_str;
+       struct osmo_sockaddr remote;
+       uint16_t port = atoi(argv[3]);
+
+       if (nse->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "NSE is not an UDP NSE%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nse->dialect != NS2_DIALECT_IPACCESS) {
+               vty_out(vty, "NSE is not an ipaccess NSE%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       bind = gprs_ns2_bind_by_name(vty_nsi, bind_name);
+       if (!bind) {
+               vty_out(vty, "Can not find bind with name %s%s",
+                       bind_name, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (bind->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Bind %s is not an UDP bind.%s",
+                       bind_name, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (osmo_sockaddr_str_from_str(&remote_str, argv[2], port)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (osmo_sockaddr_str_to_sockaddr(&remote_str, &remote.u.sas)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, &remote);
+       if (!nsvc) {
+               vty_out(vty, "Can not find NSVC with remote %s:%d%s",
+                       remote_str.ip, remote_str.port, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (!nsvc->persistent) {
+               vty_out(vty, "NSVC with remote %s:%d is a dynamic NSVC. Not 
configured by vty.%s",
+                       remote_str.ip, remote_str.port, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nsvc->nse != nse) {
+               vty_out(vty, "NSVC is not part of this NSE!%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (!nsvc->nsvci_is_valid) {
+               vty_out(vty, "NSVC doesn't have a nsvci!%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nsvc->nsvci != nsvci) {
+               vty_out(vty, "NSVC has a different nsvci (%d)!%s",
+                       nsvc->nsvci, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       gprs_ns2_free_nsvc(nsvc);
+       if (llist_empty(&nse->nsvc)) {
+               nse->ll = GPRS_NS2_LL_UNDEF;
+               nse->dialect = NS2_DIALECT_UNDEF;
+       }
+
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_ns_nse_sns, cfg_ns_nse_sns_cmd,
+      "sns BINDGROUP " VTY_IPV46_CMD " <1-65535>",
+      "SNS Initial Endpoint\n"
+      "A bind group. Use \"all\" an alias for all UDP binds.\n"
+      "SGSN IPv4 Address\n" "SGSN IPv6 Address\n"
+      "SGSN UDP Port\n"
+      )
+{
+       struct gprs_ns2_nse *nse = vty->index;
+       bool dialect_modified = false;
+       bool ll_modified = false;
+
+       const char *bindgroup = argv[0];
+       struct osmo_sockaddr_str remote_str;
+       struct osmo_sockaddr remote;
+       uint16_t port = atoi(argv[2]);
+
+       if (nse->ll == GPRS_NS2_LL_UNDEF) {
+               nse->ll = GPRS_NS2_LL_UDP;
+               ll_modified = true;
+       }
+
+       if (nse->dialect == NS2_DIALECT_UNDEF) {
+               nse->dialect = NS2_DIALECT_SNS;
+               dialect_modified = true;
+       }
+
+       if (nse->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "Can not mix NS VC with different link layer%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (nse->dialect != NS2_DIALECT_SNS) {
+               vty_out(vty, "Can not mix NS VC with different dailects%s", 
VTY_NEWLINE);
+               goto err;
+       }
+
+       if (osmo_sockaddr_str_from_str(&remote_str, argv[2], port)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               goto err;
+       }
+
+       if (osmo_sockaddr_str_to_sockaddr(&remote_str, &remote.u.sas)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               goto err;
+       }
+
+       /* TODO add */
+
+       return CMD_SUCCESS;
+
+err:
+       if (ll_modified)
+               nse->ll = GPRS_NS2_LL_UNDEF;
+       if (dialect_modified)
+               nse->dialect = NS2_DIALECT_UNDEF;
+       return CMD_WARNING;
+}
+
+DEFUN(cfg_no_ns_nse_sns, cfg_no_ns_nse_sns_cmd,
+      "no sns BINDGROUP " VTY_IPV46_CMD " <1-65535>",
+      "Delete a SNS Initial Endpoint\n"
+      "Delete a SNS Initial Endpoint\n"
+      "A bind group. Use \"all\" an alias for all UDP binds.\n"
+      "SGSN IPv4 Address\n" "SGSN IPv6 Address\n"
+      "SGSN UDP Port\n"
+      )
+{
+       struct gprs_ns2_vc_bind *bind;
+       struct gprs_ns2_vc *nsvc;
+       struct gprs_ns2_nse *nse = vty->index;
+       const char *bind_name = argv[0];
+       uint16_t nsvci = atoi(argv[1]);
+       struct osmo_sockaddr_str remote_str; /* argv[2] */
+       struct osmo_sockaddr remote;
+       uint16_t port = atoi(argv[3]);
+
+       if (nse->ll != GPRS_NS2_LL_UDP) {
+               vty_out(vty, "NSE is not an UDP NSE%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (nse->dialect != NS2_DIALECT_IPACCESS) {
+               vty_out(vty, "NSE is not an ipaccess NSE%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       bind = gprs_ns2_bind_by_name(vty_nsi, bind_name);
+       if (!bind) {
+               vty_out(vty, "Can not find bind with name %s%s",
+                       bind_name, VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (osmo_sockaddr_str_from_str(&remote_str, argv[2], port)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (osmo_sockaddr_str_to_sockaddr(&remote_str, &remote.u.sas)) {
+               vty_out(vty, "Can not parse IPv4/IPv6 or port.%s", VTY_NEWLINE);
+               return CMD_WARNING;
+       }
+
+       if (llist_empty(&nse->nsvc)) {
+               nse->ll = GPRS_NS2_LL_UNDEF;
+               nse->dialect = NS2_DIALECT_UNDEF;
+       }
+
+       return CMD_SUCCESS;
+}
+
+static int config_write_ns_nse(struct vty *vty)
+{
+       return 0;
+}
+
+int gprs_ns2_vty2_init(struct gprs_ns2_inst *nsi)
+{
+       vty_nsi = nsi;
+       INIT_LLIST_HEAD(&binds);
+
+       /* TODO: show xyz command */
+
+       install_node(&fr_node, config_write_fr);
+       install_lib_element(CONFIG_NODE, &cfg_fr_cmd);
+       install_lib_element(L_FR_NODE, &cfg_fr_link_cmd);
+       install_lib_element(L_FR_NODE, &cfg_no_fr_link_cmd);
+
+       install_lib_element(CONFIG_NODE, &cfg_ns_cmd);
+       install_node(&ns_node, config_write_ns);
+
+       install_lib_element(L_NS_NODE, &cfg_ns_nsei_cmd);
+       install_lib_element(L_NS_NODE, &cfg_no_ns_nsei_cmd);
+       install_lib_element(L_NS_NODE, &cfg_ns_bind_cmd);
+       install_lib_element(L_NS_NODE, &cfg_no_ns_bind_cmd);
+
+       install_node(&ns_bind_node, config_write_ns_bind);
+       install_lib_element(L_NS_BIND_NODE, &cfg_ns_bind_listen_cmd);
+       install_lib_element(L_NS_BIND_NODE, &cfg_no_ns_bind_listen_cmd);
+       install_lib_element(L_NS_BIND_NODE, &cfg_ns_bind_ipaccess_cmd);
+       install_lib_element(L_NS_BIND_NODE, &cfg_no_ns_ipaccess_cmd);
+       /* TODO: accept sns group IDENTIFIER */
+
+       install_node(&ns_nse_node, config_write_ns_nse);
+       install_lib_element(L_NS_NODE, &cfg_ns_nse_nsvc_fr_cmd);
+       install_lib_element(L_NS_NODE, &cfg_no_ns_nse_nsvc_fr_cmd);
+       install_lib_element(L_NS_NODE, &cfg_ns_nse_nsvc_udp_cmd);
+       install_lib_element(L_NS_NODE, &cfg_no_ns_nse_nsvc_udp_cmd);
+       install_lib_element(L_NS_NODE, &cfg_ns_nse_nsvc_ipa_cmd);
+       install_lib_element(L_NS_NODE, &cfg_no_ns_nse_nsvc_ipa_cmd);
+       install_lib_element(L_NS_NODE, &cfg_ns_nse_sns_cmd);
+       install_lib_element(L_NS_NODE, &cfg_no_ns_nse_sns_cmd);
+
+
+       return 0;
+}

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/21487
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I163279cf57e84198dc8c53e1c109f5a9474670e9
Gerrit-Change-Number: 21487
Gerrit-PatchSet: 1
Gerrit-Owner: lynxis lazus <lyn...@fe80.eu>
Gerrit-MessageType: newchange

Reply via email to