blob: dd157d7675b51fd33126e6df571a69cdfe17c7d0 [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* main application for hnb-gw part of osmo-iuh */
2
3/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +02004 * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
Harald Welte77847ad2015-10-06 22:07:04 +02005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
Harald Welteb3dae302015-08-30 12:20:09 +020022#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <errno.h>
28#include <signal.h>
Neels Hofmeyr1d03f192016-08-18 03:15:14 +020029#include <stdbool.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020030
Harald Welteb3dae302015-08-30 12:20:09 +020031#include <sys/types.h>
32#include <sys/socket.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020033#include <netinet/in.h>
Harald Welteb3dae302015-08-30 12:20:09 +020034#include <netinet/sctp.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020035#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020036
37#include <osmocom/core/application.h>
38#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020039#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020040#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020041#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020042#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020043#include <osmocom/core/write_queue.h>
Maxef727412017-12-26 19:27:43 +010044#include <osmocom/ctrl/control_if.h>
45#include <osmocom/ctrl/control_cmd.h>
46#include <osmocom/ctrl/control_vty.h>
47#include <osmocom/ctrl/ports.h>
Harald Welteb3dae302015-08-30 12:20:09 +020048#include <osmocom/vty/telnet_interface.h>
49#include <osmocom/vty/logging.h>
Harald Welteda86fe52017-11-21 08:14:37 +010050#include <osmocom/vty/command.h>
Neels Hofmeyr2c56f6d2017-12-20 02:31:49 +010051#include <osmocom/vty/ports.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020052
Harald Welteffa7c0a2015-12-23 00:03:41 +010053#include <osmocom/netif/stream.h>
54
Harald Welteda86fe52017-11-21 08:14:37 +010055#include <osmocom/ranap/ranap_common.h>
56
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020057#include <osmocom/sigtran/protocol/m3ua.h>
Harald Welte75a4e652015-12-22 23:59:24 +010058#include <osmocom/sigtran/sccp_sap.h>
59
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020060#include <osmocom/iuh/hnbgw.h>
61#include <osmocom/iuh/hnbgw_hnbap.h>
62#include <osmocom/iuh/hnbgw_rua.h>
63#include <osmocom/iuh/hnbgw_cn.h>
64#include <osmocom/iuh/context_map.h>
Harald Welteb3dae302015-08-30 12:20:09 +020065
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +020066static const char * const osmo_hnbgw_copyright =
67 "OsmoHNBGW - Osmocom Home Node B Gateway implementation\r\n"
68 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
69 "Contributions by Daniel Willmann, Harald Welte, Neels Hofmeyr\r\n"
70 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
71 "This is free software: you are free to change and redistribute it.\r\n"
72 "There is NO WARRANTY, to the extent permitted by law.\r\n";
73
Harald Welteb3dae302015-08-30 12:20:09 +020074static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020075void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020076
Harald Weltec4338de2015-12-24 00:40:52 +010077static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020078
Harald Weltec4338de2015-12-24 00:40:52 +010079static struct hnb_gw *hnb_gw_create(void *ctx)
80{
81 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
82
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +020083 /* strdup so we can easily talloc_free in the VTY code */
84 gw->config.iuh_local_ip = talloc_strdup(gw, HNBGW_LOCAL_IP_DEFAULT);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +020085 gw->config.iuh_local_port = IUH_DEFAULT_SCTP_PORT;
Harald Weltec4338de2015-12-24 00:40:52 +010086
Harald Weltec4338de2015-12-24 00:40:52 +010087 gw->next_ue_ctx_id = 23;
88 INIT_LLIST_HEAD(&gw->hnb_list);
89 INIT_LLIST_HEAD(&gw->ue_list);
Harald Weltec4338de2015-12-24 00:40:52 +010090
91 context_map_init(gw);
92
93 return gw;
94}
95
Maxbfeea672017-12-27 22:53:36 +010096struct hnb_context *hnb_context_by_id(struct hnb_gw *gw, uint32_t cid)
97{
98 struct hnb_context *hnb;
99
100 llist_for_each_entry(hnb, &gw->hnb_list, list) {
101 if (hnb->id.cid == cid)
102 return hnb;
103 }
104
105 return NULL;
106}
107
Stefan Sperling319c2852018-10-29 18:19:14 +0100108struct hnb_context *hnb_context_by_identity_info(struct hnb_gw *gw, const char *identity_info)
109{
110 struct hnb_context *hnb;
111
112 llist_for_each_entry(hnb, &gw->hnb_list, list) {
113 if (strcmp(identity_info, hnb->identity_info) == 0)
114 return hnb;
115 }
116
117 return NULL;
118}
119
120
Maxbfeea672017-12-27 22:53:36 +0100121unsigned hnb_contexts(const struct hnb_gw *gw)
122{
123 unsigned num_ctx = 0;
124 struct hnb_context *hnb;
125
126 llist_for_each_entry(hnb, &gw->hnb_list, list) {
127 num_ctx++;
128 }
129
130 return num_ctx;
131}
132
Harald Weltec4338de2015-12-24 00:40:52 +0100133struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +0200134{
135 struct ue_context *ue;
136
Harald Weltec4338de2015-12-24 00:40:52 +0100137 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200138 if (ue->context_id == id)
139 return ue;
140 }
141 return NULL;
142
143}
144
Harald Weltec4338de2015-12-24 00:40:52 +0100145struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200146{
147 struct ue_context *ue;
148
Harald Weltec4338de2015-12-24 00:40:52 +0100149 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200150 if (!strcmp(ue->imsi, imsi))
151 return ue;
152 }
153 return NULL;
154}
155
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200156struct ue_context *ue_context_by_tmsi(struct hnb_gw *gw, uint32_t tmsi)
157{
158 struct ue_context *ue;
159
160 llist_for_each_entry(ue, &gw->ue_list, list) {
161 if (ue->tmsi == tmsi)
162 return ue;
163 }
164 return NULL;
165}
166
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100167void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
168{
169 struct ue_context *ue, *tmp;
170
171 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
172 if (ue->hnb == hnb)
173 ue_context_free(ue);
174 }
175}
176
Harald Weltec4338de2015-12-24 00:40:52 +0100177static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200178{
179 uint32_t id;
180
181 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100182 id = gw->next_ue_ctx_id++;
183 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200184
185 return id;
186}
187
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200188struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi,
189 uint32_t tmsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200190{
191 struct ue_context *ue;
192
193 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
194 if (!ue)
195 return NULL;
196
197 ue->hnb = hnb;
Harald Weltedc529f32020-12-30 13:39:06 +0100198 if (imsi)
199 OSMO_STRLCPY_ARRAY(ue->imsi, imsi);
200 else
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200201 ue->imsi[0] = '\0';
202 ue->tmsi = tmsi;
Harald Weltec4338de2015-12-24 00:40:52 +0100203 ue->context_id = get_next_ue_ctx_id(hnb->gw);
204 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200205
Neels Hofmeyr0476e282016-09-22 19:37:29 +0200206 LOGP(DHNBAP, LOGL_INFO, "created UE context: id 0x%x, imsi %s, tmsi 0x%x\n",
207 ue->context_id, imsi? imsi : "-", tmsi);
208
Harald Welteb534e5c2015-09-11 00:15:16 +0200209 return ue;
210}
211
212void ue_context_free(struct ue_context *ue)
213{
214 llist_del(&ue->list);
215 talloc_free(ue);
216}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100217
218static int hnb_read_cb(struct osmo_stream_srv *conn)
219{
220 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200221 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200222 int rc;
223
224 if (!msg)
225 return -ENOMEM;
226
Harald Welte350814a2015-09-10 22:32:15 +0200227 /* we store a reference to the HomeNodeB in the msg->dest for the
228 * benefit of varoius downstream processing functions */
229 msg->dst = hnb;
230
Daniel Willmann6480cad2016-01-06 18:06:26 +0100231 rc = osmo_stream_srv_recv(conn, msg);
232 if (rc == -EAGAIN) {
233 /* Notification received */
234 msgb_free(msg);
235 return 0;
236 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200237 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200238 /* FIXME: clean up after disappeared HNB */
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200239 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100240 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100241 } else if (rc == 0) {
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200242 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100243 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100244
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100245 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100246 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200247 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100248 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200249
Daniel Willmann6480cad2016-01-06 18:06:26 +0100250 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200251 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100252 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200253 rc = hnbgw_hnbap_rx(hnb, msg);
254 break;
255 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100256 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200257 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200258 break;
259 case IUH_PPI_SABP:
260 case IUH_PPI_RNA:
261 case IUH_PPI_PUA:
Harald Welteda86fe52017-11-21 08:14:37 +0100262 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100263 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200264 rc = 0;
265 break;
266 default:
Harald Welteda86fe52017-11-21 08:14:37 +0100267 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100268 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200269 rc = 0;
270 break;
271 }
272
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100273out:
Harald Weltef2f30002015-09-08 00:09:23 +0200274 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200275 return rc;
276}
277
Daniel Willmann6480cad2016-01-06 18:06:26 +0100278struct hnb_context *hnb_context_alloc(struct hnb_gw *gw, struct osmo_stream_srv_link *link, int new_fd)
Harald Welte90256ba2015-12-23 20:16:36 +0100279{
280 struct hnb_context *ctx;
281
282 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
283 if (!ctx)
284 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100285 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100286
287 ctx->gw = gw;
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200288 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, NULL, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100289 if (!ctx->conn) {
290 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100291 talloc_free(ctx);
292 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100293 }
294
Harald Welte90256ba2015-12-23 20:16:36 +0100295 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100296 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100297}
298
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200299void hnb_context_release(struct hnb_context *ctx)
Harald Welte90256ba2015-12-23 20:16:36 +0100300{
301 struct hnbgw_context_map *map, *map2;
302
303 /* remove from the list of HNB contexts */
304 llist_del(&ctx->list);
305
306 /* deactivate all context maps */
307 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
308 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200309 * gone. Let's hope the second osmo_llist_del in the
310 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100311 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200312 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100313 context_map_deactivate(map);
314 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100315 ue_context_free_by_hnb(ctx->gw, ctx);
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100316
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200317 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100318
319 talloc_free(ctx);
320}
321
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200322/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100323static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200324{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100325 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200326 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200327
Daniel Willmann6480cad2016-01-06 18:06:26 +0100328 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200329 if (!ctx)
330 return -ENOMEM;
331
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200332 return 0;
333}
334
Harald Welteb3dae302015-08-30 12:20:09 +0200335static const struct log_info_cat log_cat[] = {
336 [DMAIN] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100337 .name = "DMAIN", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200338 .color = "",
339 .description = "Main program",
340 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100341 [DHNBAP] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100342 .name = "DHNBAP", .loglevel = LOGL_NOTICE, .enabled = 1,
Daniel Willmannbded9842015-12-17 11:51:17 +0100343 .color = "",
344 .description = "Home Node B Application Part",
345 },
Harald Weltef42317b2015-12-23 15:36:31 +0100346 [DRUA] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100347 .name = "DRUA", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Weltef42317b2015-12-23 15:36:31 +0100348 .color = "",
349 .description = "RANAP User Adaptation",
350 },
351 [DRANAP] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100352 .name = "DRANAP", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Weltef42317b2015-12-23 15:36:31 +0100353 .color = "",
354 .description = "RAN Application Part",
355 },
Harald Welteb3dae302015-08-30 12:20:09 +0200356};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200357
Harald Welteb3dae302015-08-30 12:20:09 +0200358static const struct log_info hnbgw_log_info = {
359 .cat = log_cat,
360 .num_cat = ARRAY_SIZE(log_cat),
361};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200362
Harald Welteb3dae302015-08-30 12:20:09 +0200363static struct vty_app_info vty_info = {
364 .name = "OsmoHNBGW",
Max1e57ba52017-12-28 12:27:41 +0100365 .version = PACKAGE_VERSION,
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200366 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200367};
368
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200369static struct {
370 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200371 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200372 bool log_disable_color;
373 bool log_enable_timestamp;
374 int log_level;
375 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200376} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200377 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200378 "osmo-hnbgw.cfg",
Neels Hofmeyr3c839962019-03-04 22:30:01 +0100379 false,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200380 false,
381 0,
382 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200383};
384
385static void print_usage()
386{
387 printf("Usage: osmo-hnbgw\n");
388}
389
390static void print_help()
391{
392 printf(" -h --help This text.\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200393 printf(" -d option --debug=DHNBAP:DRUA:DRANAP:DMAIN Enable debugging.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200394 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200395 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200396 printf(" -s --disable-color\n");
397 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
398 printf(" -V --version Print the version of OsmoHNBGW.\n");
399 printf(" -e --log-level number Set a global loglevel.\n");
400}
401
402static void handle_options(int argc, char **argv)
403{
404 while (1) {
405 int option_index = 0, c;
406 static struct option long_options[] = {
407 {"help", 0, 0, 'h'},
408 {"debug", 1, 0, 'd'},
409 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200410 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200411 {"disable-color", 0, 0, 's'},
412 {"timestamp", 0, 0, 'T'},
413 {"version", 0, 0, 'V' },
414 {"log-level", 1, 0, 'e'},
415 {0, 0, 0, 0}
416 };
417
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200418 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200419 long_options, &option_index);
420 if (c == -1)
421 break;
422
423 switch (c) {
424 case 'h':
425 print_usage();
426 print_help();
427 exit(0);
428 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200429 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200430 break;
431 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200432 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200433 break;
434 case 'D':
435 hnbgw_cmdline_config.daemonize = 1;
436 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200437 case 'c':
438 hnbgw_cmdline_config.config_file = optarg;
439 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200440 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200441 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200442 break;
443 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200444 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200445 break;
446 case 'V':
447 print_version(1);
448 exit(0);
449 break;
450 default:
451 /* catch unknown options *as well as* missing arguments. */
452 fprintf(stderr, "Error in command line options. Exiting.\n");
453 exit(-1);
454 break;
455 }
456 }
Harald Weltecc0914d2019-12-03 21:52:49 +0100457
458 if (argc > optind) {
459 fprintf(stderr, "Unsupported positional arguments on command line\n");
460 exit(2);
461 }
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200462}
463
Maxbfeea672017-12-27 22:53:36 +0100464CTRL_CMD_DEFINE_RO(hnb_info, "info");
465static int get_hnb_info(struct ctrl_cmd *cmd, void *data)
466{
467 struct hnb_context *hnb = data;
468
469 cmd->reply = talloc_strdup(cmd, hnb->identity_info);
470
471 return CTRL_CMD_REPLY;
472}
473
474CTRL_CMD_DEFINE_RO(hnbs, "num-hnb");
475static int get_hnbs(struct ctrl_cmd *cmd, void *data)
476{
477 cmd->reply = talloc_asprintf(cmd, "%u", hnb_contexts(data));
478
479 return CTRL_CMD_REPLY;
480}
481
482int hnb_ctrl_cmds_install()
483{
484 int rc = 0;
485
486 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_hnbs);
487 rc |= ctrl_cmd_install(CTRL_NODE_HNB, &cmd_hnb_info);
488
489 return rc;
490}
491
492static int hnb_ctrl_node_lookup(void *data, vector vline, int *node_type, void **node_data, int *i)
493{
494 const char *token = vector_slot(vline, *i);
495 struct hnb_context *hnb;
496 long num;
497
498 switch (*node_type) {
499 case CTRL_NODE_ROOT:
500 if (strcmp(token, "hnb") != 0)
501 return 0;
502
503 (*i)++;
504
505 if (!ctrl_parse_get_num(vline, *i, &num))
506 return -ERANGE;
507
508 hnb = hnb_context_by_id(data, num);
509 if (!hnb)
510 return -ENODEV;
511
512 *node_data = hnb;
513 *node_type = CTRL_NODE_HNB;
514 break;
515 default:
516 return 0;
517 }
518
519 return 1;
520}
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200521
Harald Welteb3dae302015-08-30 12:20:09 +0200522int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200523{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100524 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200525 int rc;
526
527 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200528 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200529 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200530
Harald Weltec4338de2015-12-24 00:40:52 +0100531 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200532 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100533
Neels Hofmeyrf8d5f372019-03-04 22:30:20 +0100534 rc = osmo_init_logging2(tall_hnb_ctx, &hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200535 if (rc < 0)
536 exit(1);
537
Vadim Yanitskiy11a16392020-01-01 14:05:55 +0100538 rc = osmo_ss7_init();
539 if (rc < 0) {
540 LOGP(DMAIN, LOGL_FATAL, "osmo_ss7_init() failed with rc=%d\n", rc);
541 exit(1);
542 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200543
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200544 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200545 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200546
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200547 osmo_ss7_vty_init_asp(tall_hnb_ctx);
Neels Hofmeyrb876b422018-09-27 01:54:58 +0200548 osmo_sccp_vty_init();
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200549 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
Pau Espin Pedrol965d5332019-09-20 17:21:16 +0200550 ctrl_vty_init(tall_hnb_ctx);
Pau Espin Pedrol92ea15a2019-08-05 14:49:35 +0200551 logging_vty_add_cmds();
Harald Welteb3dae302015-08-30 12:20:09 +0200552
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200553 /* Handle options after vty_init(), for --version */
554 handle_options(argc, argv);
555
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200556 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
557 if (rc < 0) {
558 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
559 hnbgw_cmdline_config.config_file);
560 return 1;
561 }
562
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200563 /*
564 * cmdline options take precedence over config file, but if no options
565 * were passed we must not override the config file.
566 */
567 if (hnbgw_cmdline_config.log_disable_color)
568 log_set_use_color(osmo_stderr_target, 0);
569 if (hnbgw_cmdline_config.log_category_mask)
570 log_parse_category_mask(osmo_stderr_target,
571 hnbgw_cmdline_config.log_category_mask);
572 if (hnbgw_cmdline_config.log_enable_timestamp)
573 log_set_print_timestamp(osmo_stderr_target, 1);
574 if (hnbgw_cmdline_config.log_level)
575 log_set_log_level(osmo_stderr_target,
576 hnbgw_cmdline_config.log_level);
577
Neels Hofmeyrade7e8b2017-12-20 02:33:44 +0100578 rc = telnet_init_dynif(tall_hnb_ctx, g_hnb_gw, vty_get_bind_addr(), OSMO_VTY_PORT_HNBGW);
Harald Welteb3dae302015-08-30 12:20:09 +0200579 if (rc < 0) {
580 perror("Error binding VTY port");
581 exit(1);
582 }
583
Maxbfeea672017-12-27 22:53:36 +0100584 g_hnb_gw->ctrl = ctrl_interface_setup_dynip2(g_hnb_gw, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW,
585 hnb_ctrl_node_lookup, _LAST_CTRL_NODE_HNB);
Maxef727412017-12-26 19:27:43 +0100586 if (!g_hnb_gw->ctrl) {
587 LOGP(DMAIN, LOGL_ERROR, "Failed to create CTRL interface on %s:%u\n",
588 ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW);
589 exit(1);
Maxbfeea672017-12-27 22:53:36 +0100590 } else {
591 rc = hnb_ctrl_cmds_install();
592 if (rc) {
593 LOGP(DMAIN, LOGL_ERROR, "Failed to install CTRL interface commands\n");
594 return 2;
595 }
Maxef727412017-12-26 19:27:43 +0100596 }
597
Harald Weltebdf3fd12016-01-03 17:04:09 +0100598 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100599
Pau Espin Pedrol9074d912020-08-20 13:45:40 +0200600 rc = hnbgw_cnlink_init(g_hnb_gw, "localhost", M3UA_PORT, "localhost");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200601 if (rc < 0) {
602 LOGP(DMAIN, LOGL_ERROR, "Failed to initialize SCCP link to CN\n");
603 exit(1);
604 }
605
Neels Hofmeyr4e2681d2017-12-24 23:54:07 +0100606 LOGP(DHNBAP, LOGL_NOTICE, "Using RNC-Id %u\n", g_hnb_gw->config.rnc_id);
607
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200608 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200609 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200610 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200611 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100612 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
613 if (!srv) {
614 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200615 exit(1);
616 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100617 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
618 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Pau Espin Pedrol4875f092020-10-12 16:04:02 +0200619 osmo_stream_srv_link_set_nodelay(srv, true);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200620 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200621 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100622 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
623
624 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100625 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100626 exit(1);
627 }
628 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200629
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200630 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200631 rc = osmo_daemonize();
632 if (rc < 0) {
633 perror("Error during daemonize");
634 exit(1);
635 }
636 }
637
638 while (1) {
639 rc = osmo_select_main(0);
640 if (rc < 0)
641 exit(3);
642 }
643
644 /* not reached */
645 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200646}