blob: e21ec4a6f62d1ac1dcafb0f8bab7da3212528cbd [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 Weltea2e6a7a2015-08-29 21:47:39 +020075
Harald Weltec4338de2015-12-24 00:40:52 +010076static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020077
Harald Weltec4338de2015-12-24 00:40:52 +010078static struct hnb_gw *hnb_gw_create(void *ctx)
79{
80 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
81
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +020082 /* strdup so we can easily talloc_free in the VTY code */
83 gw->config.iuh_local_ip = talloc_strdup(gw, HNBGW_LOCAL_IP_DEFAULT);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +020084 gw->config.iuh_local_port = IUH_DEFAULT_SCTP_PORT;
Harald Weltec6d93452020-12-30 13:36:20 +010085 gw->config.log_prefix_hnb_id = true;
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 Weltef05c5bf2020-12-30 14:17:34 +0100237 LOGHNB(hnb, 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 Weltef05c5bf2020-12-30 14:17:34 +0100262 LOGHNB(hnb, DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%lu received\n", msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200263 rc = 0;
264 break;
265 default:
Harald Weltef05c5bf2020-12-30 14:17:34 +0100266 LOGHNB(hnb, DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%lu received\n", msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200267 rc = 0;
268 break;
269 }
270
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100271out:
Harald Weltef2f30002015-09-08 00:09:23 +0200272 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200273 return rc;
274}
275
Daniel Willmann6480cad2016-01-06 18:06:26 +0100276struct 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 +0100277{
278 struct hnb_context *ctx;
279
280 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
281 if (!ctx)
282 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100283 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100284
285 ctx->gw = gw;
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200286 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, NULL, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100287 if (!ctx->conn) {
288 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100289 talloc_free(ctx);
290 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100291 }
292
Harald Welte90256ba2015-12-23 20:16:36 +0100293 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100294 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100295}
296
Harald Weltec6d93452020-12-30 13:36:20 +0100297static const char *umts_cell_id_name(const struct umts_cell_id *ucid)
298{
299 static __thread char buf[40];
300
301 snprintf(buf, sizeof(buf), "%u-%u-L%u-R%u-S%u", ucid->mcc, ucid->mnc, ucid->lac, ucid->rac, ucid->sac);
302 return buf;
303}
304
305const char *hnb_context_name(struct hnb_context *ctx)
306{
307 if (!ctx)
308 return "NULL";
309
310 if (ctx->gw->config.log_prefix_hnb_id)
311 return ctx->identity_info;
312 else
313 return umts_cell_id_name(&ctx->id);
314}
315
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200316void hnb_context_release(struct hnb_context *ctx)
Harald Welte90256ba2015-12-23 20:16:36 +0100317{
318 struct hnbgw_context_map *map, *map2;
319
320 /* remove from the list of HNB contexts */
321 llist_del(&ctx->list);
322
323 /* deactivate all context maps */
324 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
325 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200326 * gone. Let's hope the second osmo_llist_del in the
327 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100328 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200329 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100330 context_map_deactivate(map);
331 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100332 ue_context_free_by_hnb(ctx->gw, ctx);
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100333
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200334 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100335
336 talloc_free(ctx);
337}
338
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200339/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100340static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200341{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100342 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200343 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200344
Daniel Willmann6480cad2016-01-06 18:06:26 +0100345 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200346 if (!ctx)
347 return -ENOMEM;
348
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200349 return 0;
350}
351
Harald Welteb3dae302015-08-30 12:20:09 +0200352static const struct log_info_cat log_cat[] = {
353 [DMAIN] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100354 .name = "DMAIN", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200355 .color = "",
356 .description = "Main program",
357 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100358 [DHNBAP] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100359 .name = "DHNBAP", .loglevel = LOGL_NOTICE, .enabled = 1,
Daniel Willmannbded9842015-12-17 11:51:17 +0100360 .color = "",
361 .description = "Home Node B Application Part",
362 },
Harald Weltef42317b2015-12-23 15:36:31 +0100363 [DRUA] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100364 .name = "DRUA", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Weltef42317b2015-12-23 15:36:31 +0100365 .color = "",
366 .description = "RANAP User Adaptation",
367 },
368 [DRANAP] = {
Philipp Maier554dbf42020-03-25 12:12:55 +0100369 .name = "DRANAP", .loglevel = LOGL_NOTICE, .enabled = 1,
Harald Weltef42317b2015-12-23 15:36:31 +0100370 .color = "",
371 .description = "RAN Application Part",
372 },
Harald Welteb3dae302015-08-30 12:20:09 +0200373};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200374
Harald Welteb3dae302015-08-30 12:20:09 +0200375static const struct log_info hnbgw_log_info = {
376 .cat = log_cat,
377 .num_cat = ARRAY_SIZE(log_cat),
378};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200379
Harald Welteb3dae302015-08-30 12:20:09 +0200380static struct vty_app_info vty_info = {
381 .name = "OsmoHNBGW",
Max1e57ba52017-12-28 12:27:41 +0100382 .version = PACKAGE_VERSION,
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200383 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200384};
385
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200386static struct {
387 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200388 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200389 bool log_disable_color;
390 bool log_enable_timestamp;
391 int log_level;
392 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200393} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200394 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200395 "osmo-hnbgw.cfg",
Neels Hofmeyr3c839962019-03-04 22:30:01 +0100396 false,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200397 false,
398 0,
399 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200400};
401
402static void print_usage()
403{
404 printf("Usage: osmo-hnbgw\n");
405}
406
407static void print_help()
408{
409 printf(" -h --help This text.\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200410 printf(" -d option --debug=DHNBAP:DRUA:DRANAP:DMAIN Enable debugging.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200411 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200412 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200413 printf(" -s --disable-color\n");
414 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
415 printf(" -V --version Print the version of OsmoHNBGW.\n");
416 printf(" -e --log-level number Set a global loglevel.\n");
Harald Welted24839f2021-02-23 17:31:20 +0100417
418 printf("\nVTY reference generation:\n");
419 printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n");
420 printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n");
421}
422
423static void handle_long_options(const char *prog_name, const int long_option)
424{
425 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
426
427 switch (long_option) {
428 case 1:
429 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
430 if (vty_ref_mode < 0) {
431 fprintf(stderr, "%s: Unknown VTY reference generation "
432 "mode '%s'\n", prog_name, optarg);
433 exit(2);
434 }
435 break;
436 case 2:
437 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
438 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
439 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
440 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
441 exit(0);
442 default:
443 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
444 exit(2);
445 }
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200446}
447
448static void handle_options(int argc, char **argv)
449{
450 while (1) {
451 int option_index = 0, c;
Harald Welted24839f2021-02-23 17:31:20 +0100452 static int long_option = 0;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200453 static struct option long_options[] = {
454 {"help", 0, 0, 'h'},
455 {"debug", 1, 0, 'd'},
456 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200457 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200458 {"disable-color", 0, 0, 's'},
459 {"timestamp", 0, 0, 'T'},
460 {"version", 0, 0, 'V' },
461 {"log-level", 1, 0, 'e'},
Harald Welted24839f2021-02-23 17:31:20 +0100462 {"vty-ref-mode", 1, &long_option, 1},
463 {"vty-ref-xml", 0, &long_option, 2},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200464 {0, 0, 0, 0}
465 };
466
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200467 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200468 long_options, &option_index);
469 if (c == -1)
470 break;
471
472 switch (c) {
Harald Welted24839f2021-02-23 17:31:20 +0100473 case 0:
474 handle_long_options(argv[0], long_option);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200475 case 'h':
476 print_usage();
477 print_help();
478 exit(0);
479 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200480 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200481 break;
482 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200483 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200484 break;
485 case 'D':
486 hnbgw_cmdline_config.daemonize = 1;
487 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200488 case 'c':
489 hnbgw_cmdline_config.config_file = optarg;
490 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200491 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200492 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200493 break;
494 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200495 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200496 break;
497 case 'V':
498 print_version(1);
499 exit(0);
500 break;
501 default:
502 /* catch unknown options *as well as* missing arguments. */
503 fprintf(stderr, "Error in command line options. Exiting.\n");
504 exit(-1);
505 break;
506 }
507 }
Harald Weltecc0914d2019-12-03 21:52:49 +0100508
509 if (argc > optind) {
510 fprintf(stderr, "Unsupported positional arguments on command line\n");
511 exit(2);
512 }
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200513}
514
Maxbfeea672017-12-27 22:53:36 +0100515CTRL_CMD_DEFINE_RO(hnb_info, "info");
516static int get_hnb_info(struct ctrl_cmd *cmd, void *data)
517{
518 struct hnb_context *hnb = data;
519
520 cmd->reply = talloc_strdup(cmd, hnb->identity_info);
521
522 return CTRL_CMD_REPLY;
523}
524
525CTRL_CMD_DEFINE_RO(hnbs, "num-hnb");
526static int get_hnbs(struct ctrl_cmd *cmd, void *data)
527{
528 cmd->reply = talloc_asprintf(cmd, "%u", hnb_contexts(data));
529
530 return CTRL_CMD_REPLY;
531}
532
533int hnb_ctrl_cmds_install()
534{
535 int rc = 0;
536
537 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_hnbs);
538 rc |= ctrl_cmd_install(CTRL_NODE_HNB, &cmd_hnb_info);
539
540 return rc;
541}
542
543static int hnb_ctrl_node_lookup(void *data, vector vline, int *node_type, void **node_data, int *i)
544{
545 const char *token = vector_slot(vline, *i);
546 struct hnb_context *hnb;
547 long num;
548
549 switch (*node_type) {
550 case CTRL_NODE_ROOT:
551 if (strcmp(token, "hnb") != 0)
552 return 0;
553
554 (*i)++;
555
556 if (!ctrl_parse_get_num(vline, *i, &num))
557 return -ERANGE;
558
559 hnb = hnb_context_by_id(data, num);
560 if (!hnb)
561 return -ENODEV;
562
563 *node_data = hnb;
564 *node_type = CTRL_NODE_HNB;
565 break;
566 default:
567 return 0;
568 }
569
570 return 1;
571}
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200572
Harald Welteb3dae302015-08-30 12:20:09 +0200573int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200574{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100575 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200576 int rc;
577
578 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200579 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200580 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200581
Harald Weltec4338de2015-12-24 00:40:52 +0100582 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200583 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100584
Neels Hofmeyrf8d5f372019-03-04 22:30:20 +0100585 rc = osmo_init_logging2(tall_hnb_ctx, &hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200586 if (rc < 0)
587 exit(1);
588
Vadim Yanitskiy11a16392020-01-01 14:05:55 +0100589 rc = osmo_ss7_init();
590 if (rc < 0) {
591 LOGP(DMAIN, LOGL_FATAL, "osmo_ss7_init() failed with rc=%d\n", rc);
592 exit(1);
593 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200594
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200595 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200596 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200597
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200598 osmo_ss7_vty_init_asp(tall_hnb_ctx);
Neels Hofmeyrb876b422018-09-27 01:54:58 +0200599 osmo_sccp_vty_init();
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200600 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
Pau Espin Pedrol965d5332019-09-20 17:21:16 +0200601 ctrl_vty_init(tall_hnb_ctx);
Pau Espin Pedrol92ea15a2019-08-05 14:49:35 +0200602 logging_vty_add_cmds();
Harald Welteb3dae302015-08-30 12:20:09 +0200603
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200604 /* Handle options after vty_init(), for --version */
605 handle_options(argc, argv);
606
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200607 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
608 if (rc < 0) {
609 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
610 hnbgw_cmdline_config.config_file);
611 return 1;
612 }
613
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200614 /*
615 * cmdline options take precedence over config file, but if no options
616 * were passed we must not override the config file.
617 */
618 if (hnbgw_cmdline_config.log_disable_color)
619 log_set_use_color(osmo_stderr_target, 0);
620 if (hnbgw_cmdline_config.log_category_mask)
621 log_parse_category_mask(osmo_stderr_target,
622 hnbgw_cmdline_config.log_category_mask);
623 if (hnbgw_cmdline_config.log_enable_timestamp)
624 log_set_print_timestamp(osmo_stderr_target, 1);
625 if (hnbgw_cmdline_config.log_level)
626 log_set_log_level(osmo_stderr_target,
627 hnbgw_cmdline_config.log_level);
628
Neels Hofmeyrade7e8b2017-12-20 02:33:44 +0100629 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 +0200630 if (rc < 0) {
631 perror("Error binding VTY port");
632 exit(1);
633 }
634
Maxbfeea672017-12-27 22:53:36 +0100635 g_hnb_gw->ctrl = ctrl_interface_setup_dynip2(g_hnb_gw, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW,
636 hnb_ctrl_node_lookup, _LAST_CTRL_NODE_HNB);
Maxef727412017-12-26 19:27:43 +0100637 if (!g_hnb_gw->ctrl) {
638 LOGP(DMAIN, LOGL_ERROR, "Failed to create CTRL interface on %s:%u\n",
639 ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW);
640 exit(1);
Maxbfeea672017-12-27 22:53:36 +0100641 } else {
642 rc = hnb_ctrl_cmds_install();
643 if (rc) {
644 LOGP(DMAIN, LOGL_ERROR, "Failed to install CTRL interface commands\n");
645 return 2;
646 }
Maxef727412017-12-26 19:27:43 +0100647 }
648
Harald Weltebdf3fd12016-01-03 17:04:09 +0100649 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100650
Pau Espin Pedrol9074d912020-08-20 13:45:40 +0200651 rc = hnbgw_cnlink_init(g_hnb_gw, "localhost", M3UA_PORT, "localhost");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200652 if (rc < 0) {
653 LOGP(DMAIN, LOGL_ERROR, "Failed to initialize SCCP link to CN\n");
654 exit(1);
655 }
656
Neels Hofmeyr4e2681d2017-12-24 23:54:07 +0100657 LOGP(DHNBAP, LOGL_NOTICE, "Using RNC-Id %u\n", g_hnb_gw->config.rnc_id);
658
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200659 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200660 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200661 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200662 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100663 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
664 if (!srv) {
665 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200666 exit(1);
667 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100668 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
669 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Pau Espin Pedrol4875f092020-10-12 16:04:02 +0200670 osmo_stream_srv_link_set_nodelay(srv, true);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200671 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200672 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100673 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
674
675 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100676 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100677 exit(1);
678 }
679 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200680
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200681 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200682 rc = osmo_daemonize();
683 if (rc < 0) {
684 perror("Error during daemonize");
685 exit(1);
686 }
687 }
688
689 while (1) {
690 rc = osmo_select_main(0);
691 if (rc < 0)
692 exit(3);
693 }
694
695 /* not reached */
696 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200697}