blob: ba8a06f7b07a9b72f1386b15a8931972508a6a80 [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 Weltec6d93452020-12-30 13:36:20 +010086 gw->config.log_prefix_hnb_id = true;
Harald Weltec4338de2015-12-24 00:40:52 +010087
Harald Weltec4338de2015-12-24 00:40:52 +010088 gw->next_ue_ctx_id = 23;
89 INIT_LLIST_HEAD(&gw->hnb_list);
90 INIT_LLIST_HEAD(&gw->ue_list);
Harald Weltec4338de2015-12-24 00:40:52 +010091
92 context_map_init(gw);
93
94 return gw;
95}
96
Maxbfeea672017-12-27 22:53:36 +010097struct hnb_context *hnb_context_by_id(struct hnb_gw *gw, uint32_t cid)
98{
99 struct hnb_context *hnb;
100
101 llist_for_each_entry(hnb, &gw->hnb_list, list) {
102 if (hnb->id.cid == cid)
103 return hnb;
104 }
105
106 return NULL;
107}
108
Stefan Sperling319c2852018-10-29 18:19:14 +0100109struct hnb_context *hnb_context_by_identity_info(struct hnb_gw *gw, const char *identity_info)
110{
111 struct hnb_context *hnb;
112
113 llist_for_each_entry(hnb, &gw->hnb_list, list) {
114 if (strcmp(identity_info, hnb->identity_info) == 0)
115 return hnb;
116 }
117
118 return NULL;
119}
120
121
Maxbfeea672017-12-27 22:53:36 +0100122unsigned hnb_contexts(const struct hnb_gw *gw)
123{
124 unsigned num_ctx = 0;
125 struct hnb_context *hnb;
126
127 llist_for_each_entry(hnb, &gw->hnb_list, list) {
128 num_ctx++;
129 }
130
131 return num_ctx;
132}
133
Harald Weltec4338de2015-12-24 00:40:52 +0100134struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +0200135{
136 struct ue_context *ue;
137
Harald Weltec4338de2015-12-24 00:40:52 +0100138 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200139 if (ue->context_id == id)
140 return ue;
141 }
142 return NULL;
143
144}
145
Harald Weltec4338de2015-12-24 00:40:52 +0100146struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200147{
148 struct ue_context *ue;
149
Harald Weltec4338de2015-12-24 00:40:52 +0100150 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200151 if (!strcmp(ue->imsi, imsi))
152 return ue;
153 }
154 return NULL;
155}
156
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200157struct ue_context *ue_context_by_tmsi(struct hnb_gw *gw, uint32_t tmsi)
158{
159 struct ue_context *ue;
160
161 llist_for_each_entry(ue, &gw->ue_list, list) {
162 if (ue->tmsi == tmsi)
163 return ue;
164 }
165 return NULL;
166}
167
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100168void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
169{
170 struct ue_context *ue, *tmp;
171
172 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
173 if (ue->hnb == hnb)
174 ue_context_free(ue);
175 }
176}
177
Harald Weltec4338de2015-12-24 00:40:52 +0100178static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200179{
180 uint32_t id;
181
182 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100183 id = gw->next_ue_ctx_id++;
184 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200185
186 return id;
187}
188
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200189struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi,
190 uint32_t tmsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200191{
192 struct ue_context *ue;
193
194 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
195 if (!ue)
196 return NULL;
197
198 ue->hnb = hnb;
Harald Weltedc529f32020-12-30 13:39:06 +0100199 if (imsi)
200 OSMO_STRLCPY_ARRAY(ue->imsi, imsi);
201 else
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200202 ue->imsi[0] = '\0';
203 ue->tmsi = tmsi;
Harald Weltec4338de2015-12-24 00:40:52 +0100204 ue->context_id = get_next_ue_ctx_id(hnb->gw);
205 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200206
Neels Hofmeyr0476e282016-09-22 19:37:29 +0200207 LOGP(DHNBAP, LOGL_INFO, "created UE context: id 0x%x, imsi %s, tmsi 0x%x\n",
208 ue->context_id, imsi? imsi : "-", tmsi);
209
Harald Welteb534e5c2015-09-11 00:15:16 +0200210 return ue;
211}
212
213void ue_context_free(struct ue_context *ue)
214{
215 llist_del(&ue->list);
216 talloc_free(ue);
217}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100218
219static int hnb_read_cb(struct osmo_stream_srv *conn)
220{
221 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200222 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200223 int rc;
224
225 if (!msg)
226 return -ENOMEM;
227
Harald Welte350814a2015-09-10 22:32:15 +0200228 /* we store a reference to the HomeNodeB in the msg->dest for the
229 * benefit of varoius downstream processing functions */
230 msg->dst = hnb;
231
Daniel Willmann6480cad2016-01-06 18:06:26 +0100232 rc = osmo_stream_srv_recv(conn, msg);
233 if (rc == -EAGAIN) {
234 /* Notification received */
235 msgb_free(msg);
236 return 0;
237 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200238 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200239 /* FIXME: clean up after disappeared HNB */
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200240 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100241 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100242 } else if (rc == 0) {
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200243 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100244 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100245
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100246 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100247 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200248 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100249 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200250
Daniel Willmann6480cad2016-01-06 18:06:26 +0100251 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200252 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100253 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200254 rc = hnbgw_hnbap_rx(hnb, msg);
255 break;
256 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100257 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200258 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200259 break;
260 case IUH_PPI_SABP:
261 case IUH_PPI_RNA:
262 case IUH_PPI_PUA:
Harald Welteda86fe52017-11-21 08:14:37 +0100263 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100264 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200265 rc = 0;
266 break;
267 default:
Harald Welteda86fe52017-11-21 08:14:37 +0100268 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100269 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200270 rc = 0;
271 break;
272 }
273
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100274out:
Harald Weltef2f30002015-09-08 00:09:23 +0200275 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200276 return rc;
277}
278
Daniel Willmann6480cad2016-01-06 18:06:26 +0100279struct 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 +0100280{
281 struct hnb_context *ctx;
282
283 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
284 if (!ctx)
285 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100286 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100287
288 ctx->gw = gw;
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200289 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, NULL, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100290 if (!ctx->conn) {
291 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100292 talloc_free(ctx);
293 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100294 }
295
Harald Welte90256ba2015-12-23 20:16:36 +0100296 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100297 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100298}
299
Harald Weltec6d93452020-12-30 13:36:20 +0100300static const char *umts_cell_id_name(const struct umts_cell_id *ucid)
301{
302 static __thread char buf[40];
303
304 snprintf(buf, sizeof(buf), "%u-%u-L%u-R%u-S%u", ucid->mcc, ucid->mnc, ucid->lac, ucid->rac, ucid->sac);
305 return buf;
306}
307
308const char *hnb_context_name(struct hnb_context *ctx)
309{
310 if (!ctx)
311 return "NULL";
312
313 if (ctx->gw->config.log_prefix_hnb_id)
314 return ctx->identity_info;
315 else
316 return umts_cell_id_name(&ctx->id);
317}
318
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200319void hnb_context_release(struct hnb_context *ctx)
Harald Welte90256ba2015-12-23 20:16:36 +0100320{
321 struct hnbgw_context_map *map, *map2;
322
323 /* remove from the list of HNB contexts */
324 llist_del(&ctx->list);
325
326 /* deactivate all context maps */
327 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
328 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200329 * gone. Let's hope the second osmo_llist_del in the
330 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100331 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200332 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100333 context_map_deactivate(map);
334 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100335 ue_context_free_by_hnb(ctx->gw, ctx);
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100336
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200337 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100338
339 talloc_free(ctx);
340}
341
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200342/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100343static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200344{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100345 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200346 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200347
Daniel Willmann6480cad2016-01-06 18:06:26 +0100348 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200349 if (!ctx)
350 return -ENOMEM;
351
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200352 return 0;
353}
354
Harald Welteb3dae302015-08-30 12:20:09 +0200355static const struct log_info_cat log_cat[] = {
356 [DMAIN] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100357 .name = "DMAIN", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200358 .color = "",
359 .description = "Main program",
360 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100361 [DHNBAP] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100362 .name = "DHNBAP", .loglevel = LOGL_NOTICE, .enabled = 1,
Daniel Willmannbded9842015-12-17 11:51:17 +0100363 .color = "",
364 .description = "Home Node B Application Part",
365 },
Harald Weltef42317b2015-12-23 15:36:31 +0100366 [DRUA] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100367 .name = "DRUA", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Weltef42317b2015-12-23 15:36:31 +0100368 .color = "",
369 .description = "RANAP User Adaptation",
370 },
371 [DRANAP] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100372 .name = "DRANAP", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Weltef42317b2015-12-23 15:36:31 +0100373 .color = "",
374 .description = "RAN Application Part",
375 },
Harald Welteb3dae302015-08-30 12:20:09 +0200376};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200377
Harald Welteb3dae302015-08-30 12:20:09 +0200378static const struct log_info hnbgw_log_info = {
379 .cat = log_cat,
380 .num_cat = ARRAY_SIZE(log_cat),
381};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200382
Harald Welteb3dae302015-08-30 12:20:09 +0200383static struct vty_app_info vty_info = {
384 .name = "OsmoHNBGW",
Max1e57ba52017-12-28 12:27:41 +0100385 .version = PACKAGE_VERSION,
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200386 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200387};
388
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200389static struct {
390 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200391 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200392 bool log_disable_color;
393 bool log_enable_timestamp;
394 int log_level;
395 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200396} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200397 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200398 "osmo-hnbgw.cfg",
Neels Hofmeyr3c839962019-03-04 22:30:01 +0100399 false,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200400 false,
401 0,
402 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200403};
404
405static void print_usage()
406{
407 printf("Usage: osmo-hnbgw\n");
408}
409
410static void print_help()
411{
412 printf(" -h --help This text.\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200413 printf(" -d option --debug=DHNBAP:DRUA:DRANAP:DMAIN Enable debugging.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200414 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200415 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200416 printf(" -s --disable-color\n");
417 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
418 printf(" -V --version Print the version of OsmoHNBGW.\n");
419 printf(" -e --log-level number Set a global loglevel.\n");
420}
421
422static void handle_options(int argc, char **argv)
423{
424 while (1) {
425 int option_index = 0, c;
426 static struct option long_options[] = {
427 {"help", 0, 0, 'h'},
428 {"debug", 1, 0, 'd'},
429 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200430 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200431 {"disable-color", 0, 0, 's'},
432 {"timestamp", 0, 0, 'T'},
433 {"version", 0, 0, 'V' },
434 {"log-level", 1, 0, 'e'},
435 {0, 0, 0, 0}
436 };
437
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200438 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200439 long_options, &option_index);
440 if (c == -1)
441 break;
442
443 switch (c) {
444 case 'h':
445 print_usage();
446 print_help();
447 exit(0);
448 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200449 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200450 break;
451 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200452 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200453 break;
454 case 'D':
455 hnbgw_cmdline_config.daemonize = 1;
456 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200457 case 'c':
458 hnbgw_cmdline_config.config_file = optarg;
459 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200460 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200461 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200462 break;
463 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200464 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200465 break;
466 case 'V':
467 print_version(1);
468 exit(0);
469 break;
470 default:
471 /* catch unknown options *as well as* missing arguments. */
472 fprintf(stderr, "Error in command line options. Exiting.\n");
473 exit(-1);
474 break;
475 }
476 }
Harald Weltecc0914d2019-12-03 21:52:49 +0100477
478 if (argc > optind) {
479 fprintf(stderr, "Unsupported positional arguments on command line\n");
480 exit(2);
481 }
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200482}
483
Maxbfeea672017-12-27 22:53:36 +0100484CTRL_CMD_DEFINE_RO(hnb_info, "info");
485static int get_hnb_info(struct ctrl_cmd *cmd, void *data)
486{
487 struct hnb_context *hnb = data;
488
489 cmd->reply = talloc_strdup(cmd, hnb->identity_info);
490
491 return CTRL_CMD_REPLY;
492}
493
494CTRL_CMD_DEFINE_RO(hnbs, "num-hnb");
495static int get_hnbs(struct ctrl_cmd *cmd, void *data)
496{
497 cmd->reply = talloc_asprintf(cmd, "%u", hnb_contexts(data));
498
499 return CTRL_CMD_REPLY;
500}
501
502int hnb_ctrl_cmds_install()
503{
504 int rc = 0;
505
506 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_hnbs);
507 rc |= ctrl_cmd_install(CTRL_NODE_HNB, &cmd_hnb_info);
508
509 return rc;
510}
511
512static int hnb_ctrl_node_lookup(void *data, vector vline, int *node_type, void **node_data, int *i)
513{
514 const char *token = vector_slot(vline, *i);
515 struct hnb_context *hnb;
516 long num;
517
518 switch (*node_type) {
519 case CTRL_NODE_ROOT:
520 if (strcmp(token, "hnb") != 0)
521 return 0;
522
523 (*i)++;
524
525 if (!ctrl_parse_get_num(vline, *i, &num))
526 return -ERANGE;
527
528 hnb = hnb_context_by_id(data, num);
529 if (!hnb)
530 return -ENODEV;
531
532 *node_data = hnb;
533 *node_type = CTRL_NODE_HNB;
534 break;
535 default:
536 return 0;
537 }
538
539 return 1;
540}
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200541
Harald Welteb3dae302015-08-30 12:20:09 +0200542int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200543{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100544 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200545 int rc;
546
547 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200548 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200549 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200550
Harald Weltec4338de2015-12-24 00:40:52 +0100551 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200552 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100553
Neels Hofmeyrf8d5f372019-03-04 22:30:20 +0100554 rc = osmo_init_logging2(tall_hnb_ctx, &hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200555 if (rc < 0)
556 exit(1);
557
Vadim Yanitskiy11a16392020-01-01 14:05:55 +0100558 rc = osmo_ss7_init();
559 if (rc < 0) {
560 LOGP(DMAIN, LOGL_FATAL, "osmo_ss7_init() failed with rc=%d\n", rc);
561 exit(1);
562 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200563
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200564 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200565 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200566
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200567 osmo_ss7_vty_init_asp(tall_hnb_ctx);
Neels Hofmeyrb876b422018-09-27 01:54:58 +0200568 osmo_sccp_vty_init();
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200569 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
Pau Espin Pedrol965d5332019-09-20 17:21:16 +0200570 ctrl_vty_init(tall_hnb_ctx);
Pau Espin Pedrol92ea15a2019-08-05 14:49:35 +0200571 logging_vty_add_cmds();
Harald Welteb3dae302015-08-30 12:20:09 +0200572
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200573 /* Handle options after vty_init(), for --version */
574 handle_options(argc, argv);
575
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200576 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
577 if (rc < 0) {
578 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
579 hnbgw_cmdline_config.config_file);
580 return 1;
581 }
582
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200583 /*
584 * cmdline options take precedence over config file, but if no options
585 * were passed we must not override the config file.
586 */
587 if (hnbgw_cmdline_config.log_disable_color)
588 log_set_use_color(osmo_stderr_target, 0);
589 if (hnbgw_cmdline_config.log_category_mask)
590 log_parse_category_mask(osmo_stderr_target,
591 hnbgw_cmdline_config.log_category_mask);
592 if (hnbgw_cmdline_config.log_enable_timestamp)
593 log_set_print_timestamp(osmo_stderr_target, 1);
594 if (hnbgw_cmdline_config.log_level)
595 log_set_log_level(osmo_stderr_target,
596 hnbgw_cmdline_config.log_level);
597
Neels Hofmeyrade7e8b2017-12-20 02:33:44 +0100598 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 +0200599 if (rc < 0) {
600 perror("Error binding VTY port");
601 exit(1);
602 }
603
Maxbfeea672017-12-27 22:53:36 +0100604 g_hnb_gw->ctrl = ctrl_interface_setup_dynip2(g_hnb_gw, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW,
605 hnb_ctrl_node_lookup, _LAST_CTRL_NODE_HNB);
Maxef727412017-12-26 19:27:43 +0100606 if (!g_hnb_gw->ctrl) {
607 LOGP(DMAIN, LOGL_ERROR, "Failed to create CTRL interface on %s:%u\n",
608 ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW);
609 exit(1);
Maxbfeea672017-12-27 22:53:36 +0100610 } else {
611 rc = hnb_ctrl_cmds_install();
612 if (rc) {
613 LOGP(DMAIN, LOGL_ERROR, "Failed to install CTRL interface commands\n");
614 return 2;
615 }
Maxef727412017-12-26 19:27:43 +0100616 }
617
Harald Weltebdf3fd12016-01-03 17:04:09 +0100618 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100619
Pau Espin Pedrol9074d912020-08-20 13:45:40 +0200620 rc = hnbgw_cnlink_init(g_hnb_gw, "localhost", M3UA_PORT, "localhost");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200621 if (rc < 0) {
622 LOGP(DMAIN, LOGL_ERROR, "Failed to initialize SCCP link to CN\n");
623 exit(1);
624 }
625
Neels Hofmeyr4e2681d2017-12-24 23:54:07 +0100626 LOGP(DHNBAP, LOGL_NOTICE, "Using RNC-Id %u\n", g_hnb_gw->config.rnc_id);
627
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200628 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200629 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200630 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200631 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100632 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
633 if (!srv) {
634 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200635 exit(1);
636 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100637 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
638 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Pau Espin Pedrol4875f092020-10-12 16:04:02 +0200639 osmo_stream_srv_link_set_nodelay(srv, true);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200640 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200641 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100642 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
643
644 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100645 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100646 exit(1);
647 }
648 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200649
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200650 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200651 rc = osmo_daemonize();
652 if (rc < 0) {
653 perror("Error during daemonize");
654 exit(1);
655 }
656 }
657
658 while (1) {
659 rc = osmo_select_main(0);
660 if (rc < 0)
661 exit(3);
662 }
663
664 /* not reached */
665 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200666}