From 8125fd0d959b4b622668d509e4d8934ef4cb4525 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 27 Oct 2021 18:14:52 +0200 Subject: [PATCH] Move existing vty code to its own vty.c/h file Change-Id: I88107e9ed03ff8f807b57a3ea0d189fd70864861 --- include/osmocom/hnodeb/Makefile.am | 1 + include/osmocom/hnodeb/hnodeb.h | 8 ++ include/osmocom/hnodeb/vty.h | 31 ++++++ src/osmo-hnodeb/Makefile.am | 1 + src/osmo-hnodeb/main.c | 129 ++----------------------- src/osmo-hnodeb/vty.c | 148 +++++++++++++++++++++++++++++ 6 files changed, 195 insertions(+), 123 deletions(-) create mode 100644 include/osmocom/hnodeb/vty.h create mode 100644 src/osmo-hnodeb/vty.c diff --git a/include/osmocom/hnodeb/Makefile.am b/include/osmocom/hnodeb/Makefile.am index 9f8963a..339f8a3 100644 --- a/include/osmocom/hnodeb/Makefile.am +++ b/include/osmocom/hnodeb/Makefile.am @@ -2,4 +2,5 @@ noinst_HEADERS = \ hnodeb.h \ ranap.h \ rua.h \ + vty.h \ $(NULL) diff --git a/include/osmocom/hnodeb/hnodeb.h b/include/osmocom/hnodeb/hnodeb.h index ef11ab7..e36311e 100644 --- a/include/osmocom/hnodeb/hnodeb.h +++ b/include/osmocom/hnodeb/hnodeb.h @@ -19,6 +19,8 @@ */ #pragma once +#include + #include #include #include @@ -101,4 +103,10 @@ void hnb_rx_paging(struct hnb *hnb, const char *imsi); void hnb_nas_rx_dtap(struct hnb *hnb, void *data, int len); void hnb_rx_secmode_cmd(struct hnb *hnb, long ip_alg); +int hnb_ue_register_tx(struct hnb *hnb, const char *imsi_str); +void hnb_send_register_req(struct hnb *hnb); +void hnb_send_deregister_req(struct hnb *hnb); +struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi); + +extern void *tall_hnb_ctx; extern struct hnb g_hnb; diff --git a/include/osmocom/hnodeb/vty.h b/include/osmocom/hnodeb/vty.h new file mode 100644 index 0000000..7e3f29f --- /dev/null +++ b/include/osmocom/hnodeb/vty.h @@ -0,0 +1,31 @@ +/* (C) 2015 by Daniel Willmann + * (C) 2021 by sysmocom - s.f.m.c. GmbH + * Author: Pau Espin Pedrol + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +#pragma once + +#include +#include +#include + +enum hnb_vty_nodes { + CHAN_NODE = _LAST_OSMOVTY_NODE, +}; + +void hnb_vty_init(void); diff --git a/src/osmo-hnodeb/Makefile.am b/src/osmo-hnodeb/Makefile.am index 9f09a0a..d1cd3ec 100644 --- a/src/osmo-hnodeb/Makefile.am +++ b/src/osmo-hnodeb/Makefile.am @@ -33,6 +33,7 @@ osmo_hnodeb_SOURCES = \ debug.c \ ranap.c \ rua.c \ + vty.c \ $(NULL) osmo_hnodeb_LDADD = \ diff --git a/src/osmo-hnodeb/main.c b/src/osmo-hnodeb/main.c index 9a2aaf9..ae9403d 100644 --- a/src/osmo-hnodeb/main.c +++ b/src/osmo-hnodeb/main.c @@ -54,7 +54,6 @@ #include #include #include -#include "asn1helpers.h" #include #include @@ -70,9 +69,10 @@ #include #include +#include #include -static void *tall_hnb_ctx; +void *tall_hnb_ctx; struct hnb g_hnb = { .gw_addr = "127.0.0.1", @@ -113,7 +113,7 @@ static int hnb_ue_de_register_tx(struct hnb *hnb) } #endif -static int hnb_ue_register_tx(struct hnb *hnb, const char *imsi_str) +int hnb_ue_register_tx(struct hnb *hnb, const char *imsi_str) { struct msgb *msg; int rc, imsi_len; @@ -649,7 +649,7 @@ static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) return rc; } -static void hnb_send_register_req(struct hnb *hnb) +void hnb_send_register_req(struct hnb *hnb) { HNBAP_HNBRegisterRequest_t request_out; struct msgb *msg; @@ -698,7 +698,7 @@ static void hnb_send_register_req(struct hnb *hnb) osmo_wqueue_enqueue(&hnb->wqueue, msg); } -static void hnb_send_deregister_req(struct hnb *hnb) +void hnb_send_deregister_req(struct hnb *hnb) { struct msgb *msg; int rc; @@ -744,79 +744,8 @@ static int sctp_sock_init(int fd) return rc; } -#define HNBAP_STR "HNBAP related commands\n" -#define HNB_STR "HomeNodeB commands\n" -#define UE_STR "User Equipment commands\n" -#define RANAP_STR "RANAP related commands\n" -#define CSPS_STR "Circuit Switched\n" "Packet Switched\n" -DEFUN(hnb_register, hnb_register_cmd, - "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST") -{ - hnb_send_register_req(&g_hnb); - - return CMD_SUCCESS; -} - -DEFUN(hnb_deregister, hnb_deregister_cmd, - "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST") -{ - hnb_send_deregister_req(&g_hnb); - - return CMD_SUCCESS; -} - -DEFUN(ue_register, ue_register_cmd, - "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST") -{ - hnb_ue_register_tx(&g_hnb, argv[0]); - - return CMD_SUCCESS; -} - -DEFUN(asn_dbg, asn_dbg_cmd, - "asn-debug (1|0)", "Enable or disable libasn1c debugging") -{ - asn_debug = atoi(argv[0]); - - return CMD_SUCCESS; -} - -DEFUN(ranap_reset, ranap_reset_cmd, - "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR) -{ - int is_ps = 0; - struct msgb *msg, *rua; - - RANAP_Cause_t cause = { - .present = RANAP_Cause_PR_transmissionNetwork, - .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure, - }; - - if (!strcmp(argv[0], "ps")) - is_ps = 1; - - msg = ranap_new_msg_reset(is_ps, &cause); - rua = rua_new_udt(msg); - //msgb_free(msg); - osmo_wqueue_enqueue(&g_hnb.wqueue, rua); - - return CMD_SUCCESS; -} - - -enum my_vty_nodes { - CHAN_NODE = _LAST_OSMOVTY_NODE, -}; - -static struct cmd_node chan_node = { - CHAN_NODE, - "%s(chan)> ", - 1, -}; - - -static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi) +struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi) { uint8_t lu[] = { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST, 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57, @@ -848,52 +777,6 @@ static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi) return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu)); } -DEFUN(chan, chan_cmd, - "channel (cs|ps) lu imsi IMSI", - "Open a new Signalling Connection\n" - "To Circuit-Switched CN\n" - "To Packet-Switched CN\n" - "Performing a Location Update\n" - ) -{ - struct hnb_chan *chan; - struct msgb *msg, *rua; - static uint16_t conn_id = 42; - - chan = talloc_zero(tall_hnb_ctx, struct hnb_chan); - if (!strcmp(argv[0], "ps")) - chan->is_ps = 1; - chan->imsi = talloc_strdup(chan, argv[1]); - chan->conn_id = conn_id; - conn_id++; - - msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi); - rua = rua_new_conn(chan->is_ps, chan->conn_id, msg); - - osmo_wqueue_enqueue(&g_hnb.wqueue, rua); - - vty->index = chan; - vty->node = CHAN_NODE; - - if (!chan->is_ps) - g_hnb.cs.chan = chan; - - - return CMD_SUCCESS; -} - -static void hnb_vty_init(void) -{ - install_element_ve(&asn_dbg_cmd); - install_element_ve(&hnb_register_cmd); - install_element_ve(&hnb_deregister_cmd); - install_element_ve(&ue_register_cmd); - install_element_ve(&ranap_reset_cmd); - install_element_ve(&chan_cmd); - - install_node(&chan_node, NULL); -} - static void handle_options(int argc, char **argv) { while (1) { diff --git a/src/osmo-hnodeb/vty.c b/src/osmo-hnodeb/vty.c new file mode 100644 index 0000000..ccca6d0 --- /dev/null +++ b/src/osmo-hnodeb/vty.c @@ -0,0 +1,148 @@ +/* (C) 2015 by Daniel Willmann + * (C) 2021 by sysmocom - s.f.m.c. GmbH + * Author: Pau Espin Pedrol + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +#include "config.h" + +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +static struct cmd_node chan_node = { + CHAN_NODE, + "%s(chan)> ", + 1, +}; + +#define HNBAP_STR "HNBAP related commands\n" +#define HNB_STR "HomeNodeB commands\n" +#define UE_STR "User Equipment commands\n" +#define RANAP_STR "RANAP related commands\n" +#define CSPS_STR "Circuit Switched\n" "Packet Switched\n" + +DEFUN(hnb_register, hnb_register_cmd, + "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST") +{ + hnb_send_register_req(&g_hnb); + + return CMD_SUCCESS; +} + +DEFUN(hnb_deregister, hnb_deregister_cmd, + "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST") +{ + hnb_send_deregister_req(&g_hnb); + + return CMD_SUCCESS; +} + +DEFUN(ue_register, ue_register_cmd, + "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST") +{ + hnb_ue_register_tx(&g_hnb, argv[0]); + + return CMD_SUCCESS; +} + +DEFUN(asn_dbg, asn_dbg_cmd, + "asn-debug (1|0)", "Enable or disable libasn1c debugging") +{ + asn_debug = atoi(argv[0]); + + return CMD_SUCCESS; +} + +DEFUN(ranap_reset, ranap_reset_cmd, + "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR) +{ + int is_ps = 0; + struct msgb *msg, *rua; + + RANAP_Cause_t cause = { + .present = RANAP_Cause_PR_transmissionNetwork, + .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure, + }; + + if (!strcmp(argv[0], "ps")) + is_ps = 1; + + msg = ranap_new_msg_reset(is_ps, &cause); + rua = rua_new_udt(msg); + //msgb_free(msg); + osmo_wqueue_enqueue(&g_hnb.wqueue, rua); + + return CMD_SUCCESS; +} + +DEFUN(chan, chan_cmd, + "channel (cs|ps) lu imsi IMSI", + "Open a new Signalling Connection\n" + "To Circuit-Switched CN\n" + "To Packet-Switched CN\n" + "Performing a Location Update\n" + ) +{ + struct hnb_chan *chan; + struct msgb *msg, *rua; + static uint16_t conn_id = 42; + + chan = talloc_zero(tall_hnb_ctx, struct hnb_chan); + if (!strcmp(argv[0], "ps")) + chan->is_ps = 1; + chan->imsi = talloc_strdup(chan, argv[1]); + chan->conn_id = conn_id; + conn_id++; + + msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi); + rua = rua_new_conn(chan->is_ps, chan->conn_id, msg); + + osmo_wqueue_enqueue(&g_hnb.wqueue, rua); + + vty->index = chan; + vty->node = CHAN_NODE; + + if (!chan->is_ps) + g_hnb.cs.chan = chan; + + + return CMD_SUCCESS; +} + +void hnb_vty_init(void) +{ + install_element_ve(&asn_dbg_cmd); + install_element_ve(&hnb_register_cmd); + install_element_ve(&hnb_deregister_cmd); + install_element_ve(&ue_register_cmd); + install_element_ve(&ranap_reset_cmd); + install_element_ve(&chan_cmd); + + install_node(&chan_node, NULL); +}