blob: 94d8fb941b446583135fa69856e3546029c1bf6c [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
108unsigned hnb_contexts(const struct hnb_gw *gw)
109{
110 unsigned num_ctx = 0;
111 struct hnb_context *hnb;
112
113 llist_for_each_entry(hnb, &gw->hnb_list, list) {
114 num_ctx++;
115 }
116
117 return num_ctx;
118}
119
Harald Weltec4338de2015-12-24 00:40:52 +0100120struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +0200121{
122 struct ue_context *ue;
123
Harald Weltec4338de2015-12-24 00:40:52 +0100124 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200125 if (ue->context_id == id)
126 return ue;
127 }
128 return NULL;
129
130}
131
Harald Weltec4338de2015-12-24 00:40:52 +0100132struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200133{
134 struct ue_context *ue;
135
Harald Weltec4338de2015-12-24 00:40:52 +0100136 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200137 if (!strcmp(ue->imsi, imsi))
138 return ue;
139 }
140 return NULL;
141}
142
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200143struct ue_context *ue_context_by_tmsi(struct hnb_gw *gw, uint32_t tmsi)
144{
145 struct ue_context *ue;
146
147 llist_for_each_entry(ue, &gw->ue_list, list) {
148 if (ue->tmsi == tmsi)
149 return ue;
150 }
151 return NULL;
152}
153
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100154void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
155{
156 struct ue_context *ue, *tmp;
157
158 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
159 if (ue->hnb == hnb)
160 ue_context_free(ue);
161 }
162}
163
Harald Weltec4338de2015-12-24 00:40:52 +0100164static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200165{
166 uint32_t id;
167
168 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100169 id = gw->next_ue_ctx_id++;
170 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200171
172 return id;
173}
174
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200175struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi,
176 uint32_t tmsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200177{
178 struct ue_context *ue;
179
180 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
181 if (!ue)
182 return NULL;
183
184 ue->hnb = hnb;
Harald Weltee08cdd92016-11-26 00:01:53 +0100185 if (imsi) {
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200186 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltee08cdd92016-11-26 00:01:53 +0100187 ue->imsi[sizeof(ue->imsi)-1] = '\0';
188 } else
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200189 ue->imsi[0] = '\0';
190 ue->tmsi = tmsi;
Harald Weltec4338de2015-12-24 00:40:52 +0100191 ue->context_id = get_next_ue_ctx_id(hnb->gw);
192 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200193
Neels Hofmeyr0476e282016-09-22 19:37:29 +0200194 LOGP(DHNBAP, LOGL_INFO, "created UE context: id 0x%x, imsi %s, tmsi 0x%x\n",
195 ue->context_id, imsi? imsi : "-", tmsi);
196
Harald Welteb534e5c2015-09-11 00:15:16 +0200197 return ue;
198}
199
200void ue_context_free(struct ue_context *ue)
201{
202 llist_del(&ue->list);
203 talloc_free(ue);
204}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100205static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200206{
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100207 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
208
209 /* This connection is about to be closed. Destroy the HNB context now. */
210 if (hnb)
211 hnb_context_release(hnb, false);
212
Harald Welteda86fe52017-11-21 08:14:37 +0100213 return 0;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100214}
215
216static int hnb_read_cb(struct osmo_stream_srv *conn)
217{
218 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200219 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200220 int rc;
221
222 if (!msg)
223 return -ENOMEM;
224
Harald Welte350814a2015-09-10 22:32:15 +0200225 /* we store a reference to the HomeNodeB in the msg->dest for the
226 * benefit of varoius downstream processing functions */
227 msg->dst = hnb;
228
Daniel Willmann6480cad2016-01-06 18:06:26 +0100229 rc = osmo_stream_srv_recv(conn, msg);
230 if (rc == -EAGAIN) {
231 /* Notification received */
232 msgb_free(msg);
233 return 0;
234 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200235 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200236 /* FIXME: clean up after disappeared HNB */
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100237 hnb_context_release(hnb, true);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100238 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100239 } else if (rc == 0) {
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100240 hnb_context_release(hnb, true);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100241 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100242
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100243 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100244 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200245 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100246 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200247
Daniel Willmann6480cad2016-01-06 18:06:26 +0100248 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200249 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100250 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200251 rc = hnbgw_hnbap_rx(hnb, msg);
252 break;
253 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100254 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200255 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200256 break;
257 case IUH_PPI_SABP:
258 case IUH_PPI_RNA:
259 case IUH_PPI_PUA:
Harald Welteda86fe52017-11-21 08:14:37 +0100260 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100261 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200262 rc = 0;
263 break;
264 default:
Harald Welteda86fe52017-11-21 08:14:37 +0100265 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100266 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;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100286 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
287 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
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100297void hnb_context_release(struct hnb_context *ctx, bool destroy_conn)
Harald Welte90256ba2015-12-23 20:16:36 +0100298{
299 struct hnbgw_context_map *map, *map2;
300
301 /* remove from the list of HNB contexts */
302 llist_del(&ctx->list);
303
304 /* deactivate all context maps */
305 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
306 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200307 * gone. Let's hope the second osmo_llist_del in the
308 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100309 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200310 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100311 context_map_deactivate(map);
312 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100313 ue_context_free_by_hnb(ctx->gw, ctx);
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100314
315 if (destroy_conn)
316 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100317
318 talloc_free(ctx);
319}
320
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200321/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100322static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200323{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100324 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200325 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200326
Daniel Willmann6480cad2016-01-06 18:06:26 +0100327 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200328 if (!ctx)
329 return -ENOMEM;
330
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200331 return 0;
332}
333
Harald Welteb3dae302015-08-30 12:20:09 +0200334static const struct log_info_cat log_cat[] = {
335 [DMAIN] = {
Neels Hofmeyr6341f4d2016-04-04 18:02:49 +0200336 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200337 .color = "",
338 .description = "Main program",
339 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100340 [DHNBAP] = {
341 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
342 .color = "",
343 .description = "Home Node B Application Part",
344 },
Harald Weltef42317b2015-12-23 15:36:31 +0100345 [DRUA] = {
346 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
347 .color = "",
348 .description = "RANAP User Adaptation",
349 },
350 [DRANAP] = {
351 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
352 .color = "",
353 .description = "RAN Application Part",
354 },
Harald Welteb3dae302015-08-30 12:20:09 +0200355};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200356
Harald Welteb3dae302015-08-30 12:20:09 +0200357static const struct log_info hnbgw_log_info = {
358 .cat = log_cat,
359 .num_cat = ARRAY_SIZE(log_cat),
360};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200361
Harald Welteb3dae302015-08-30 12:20:09 +0200362static struct vty_app_info vty_info = {
363 .name = "OsmoHNBGW",
Max1e57ba52017-12-28 12:27:41 +0100364 .version = PACKAGE_VERSION,
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200365 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200366};
367
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200368static struct {
369 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200370 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200371 bool log_disable_color;
372 bool log_enable_timestamp;
373 int log_level;
374 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200375} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200376 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200377 "osmo-hnbgw.cfg",
378 true,
379 false,
380 0,
381 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200382};
383
384static void print_usage()
385{
386 printf("Usage: osmo-hnbgw\n");
387}
388
389static void print_help()
390{
391 printf(" -h --help This text.\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200392 printf(" -d option --debug=DHNBAP:DRUA:DRANAP:DMAIN Enable debugging.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200393 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200394 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200395 printf(" -s --disable-color\n");
396 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
397 printf(" -V --version Print the version of OsmoHNBGW.\n");
398 printf(" -e --log-level number Set a global loglevel.\n");
399}
400
401static void handle_options(int argc, char **argv)
402{
403 while (1) {
404 int option_index = 0, c;
405 static struct option long_options[] = {
406 {"help", 0, 0, 'h'},
407 {"debug", 1, 0, 'd'},
408 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200409 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200410 {"disable-color", 0, 0, 's'},
411 {"timestamp", 0, 0, 'T'},
412 {"version", 0, 0, 'V' },
413 {"log-level", 1, 0, 'e'},
414 {0, 0, 0, 0}
415 };
416
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200417 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200418 long_options, &option_index);
419 if (c == -1)
420 break;
421
422 switch (c) {
423 case 'h':
424 print_usage();
425 print_help();
426 exit(0);
427 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200428 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200429 break;
430 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200431 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200432 break;
433 case 'D':
434 hnbgw_cmdline_config.daemonize = 1;
435 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200436 case 'c':
437 hnbgw_cmdline_config.config_file = optarg;
438 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200439 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200440 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200441 break;
442 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200443 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200444 break;
445 case 'V':
446 print_version(1);
447 exit(0);
448 break;
449 default:
450 /* catch unknown options *as well as* missing arguments. */
451 fprintf(stderr, "Error in command line options. Exiting.\n");
452 exit(-1);
453 break;
454 }
455 }
456}
457
Maxbfeea672017-12-27 22:53:36 +0100458CTRL_CMD_DEFINE_RO(hnb_info, "info");
459static int get_hnb_info(struct ctrl_cmd *cmd, void *data)
460{
461 struct hnb_context *hnb = data;
462
463 cmd->reply = talloc_strdup(cmd, hnb->identity_info);
464
465 return CTRL_CMD_REPLY;
466}
467
468CTRL_CMD_DEFINE_RO(hnbs, "num-hnb");
469static int get_hnbs(struct ctrl_cmd *cmd, void *data)
470{
471 cmd->reply = talloc_asprintf(cmd, "%u", hnb_contexts(data));
472
473 return CTRL_CMD_REPLY;
474}
475
476int hnb_ctrl_cmds_install()
477{
478 int rc = 0;
479
480 rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_hnbs);
481 rc |= ctrl_cmd_install(CTRL_NODE_HNB, &cmd_hnb_info);
482
483 return rc;
484}
485
486static int hnb_ctrl_node_lookup(void *data, vector vline, int *node_type, void **node_data, int *i)
487{
488 const char *token = vector_slot(vline, *i);
489 struct hnb_context *hnb;
490 long num;
491
492 switch (*node_type) {
493 case CTRL_NODE_ROOT:
494 if (strcmp(token, "hnb") != 0)
495 return 0;
496
497 (*i)++;
498
499 if (!ctrl_parse_get_num(vline, *i, &num))
500 return -ERANGE;
501
502 hnb = hnb_context_by_id(data, num);
503 if (!hnb)
504 return -ENODEV;
505
506 *node_data = hnb;
507 *node_type = CTRL_NODE_HNB;
508 break;
509 default:
510 return 0;
511 }
512
513 return 1;
514}
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200515
Harald Welteb3dae302015-08-30 12:20:09 +0200516int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200517{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100518 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200519 int rc;
520
521 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200522 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200523 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200524
Harald Weltec4338de2015-12-24 00:40:52 +0100525 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200526 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100527
Harald Welteb3dae302015-08-30 12:20:09 +0200528 rc = osmo_init_logging(&hnbgw_log_info);
529 if (rc < 0)
530 exit(1);
531
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200532 osmo_ss7_init();
533
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200534 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200535 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200536
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200537 osmo_ss7_vty_init_asp(tall_hnb_ctx);
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200538 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
539 logging_vty_add_cmds(&hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200540
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200541 /* Handle options after vty_init(), for --version */
542 handle_options(argc, argv);
543
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200544 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
545 if (rc < 0) {
546 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
547 hnbgw_cmdline_config.config_file);
548 return 1;
549 }
550
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200551 /*
552 * cmdline options take precedence over config file, but if no options
553 * were passed we must not override the config file.
554 */
555 if (hnbgw_cmdline_config.log_disable_color)
556 log_set_use_color(osmo_stderr_target, 0);
557 if (hnbgw_cmdline_config.log_category_mask)
558 log_parse_category_mask(osmo_stderr_target,
559 hnbgw_cmdline_config.log_category_mask);
560 if (hnbgw_cmdline_config.log_enable_timestamp)
561 log_set_print_timestamp(osmo_stderr_target, 1);
562 if (hnbgw_cmdline_config.log_level)
563 log_set_log_level(osmo_stderr_target,
564 hnbgw_cmdline_config.log_level);
565
Neels Hofmeyrade7e8b2017-12-20 02:33:44 +0100566 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 +0200567 if (rc < 0) {
568 perror("Error binding VTY port");
569 exit(1);
570 }
571
Maxbfeea672017-12-27 22:53:36 +0100572 g_hnb_gw->ctrl = ctrl_interface_setup_dynip2(g_hnb_gw, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW,
573 hnb_ctrl_node_lookup, _LAST_CTRL_NODE_HNB);
Maxef727412017-12-26 19:27:43 +0100574 if (!g_hnb_gw->ctrl) {
575 LOGP(DMAIN, LOGL_ERROR, "Failed to create CTRL interface on %s:%u\n",
576 ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_HNBGW);
577 exit(1);
Maxbfeea672017-12-27 22:53:36 +0100578 } else {
579 rc = hnb_ctrl_cmds_install();
580 if (rc) {
581 LOGP(DMAIN, LOGL_ERROR, "Failed to install CTRL interface commands\n");
582 return 2;
583 }
Maxef727412017-12-26 19:27:43 +0100584 }
585
Harald Weltebdf3fd12016-01-03 17:04:09 +0100586 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100587
Neels Hofmeyraccb7802017-12-04 15:24:32 +0100588 rc = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", M3UA_PORT, NULL);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200589 if (rc < 0) {
590 LOGP(DMAIN, LOGL_ERROR, "Failed to initialize SCCP link to CN\n");
591 exit(1);
592 }
593
Neels Hofmeyr4e2681d2017-12-24 23:54:07 +0100594 LOGP(DHNBAP, LOGL_NOTICE, "Using RNC-Id %u\n", g_hnb_gw->config.rnc_id);
595
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200596 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200597 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200598 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200599 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100600 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
601 if (!srv) {
602 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200603 exit(1);
604 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100605 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
606 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200607 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200608 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100609 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
610
611 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100612 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100613 exit(1);
614 }
615 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200616
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200617 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200618 rc = osmo_daemonize();
619 if (rc < 0) {
620 perror("Error during daemonize");
621 exit(1);
622 }
623 }
624
625 while (1) {
626 rc = osmo_select_main(0);
627 if (rc < 0)
628 exit(3);
629 }
630
631 /* not reached */
632 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200633}