blob: 01f64a725ee0326a635955571eba0b060c4a616c [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>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Harald Welteb3dae302015-08-30 12:20:09 +020021#include <unistd.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <getopt.h>
26#include <errno.h>
27#include <signal.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020028
Harald Welteb3dae302015-08-30 12:20:09 +020029#include <sys/types.h>
30#include <sys/socket.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020031#include <netinet/in.h>
Harald Welteb3dae302015-08-30 12:20:09 +020032#include <netinet/sctp.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020033#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020034
35#include <osmocom/core/application.h>
36#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020037#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020038#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020039#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020040#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020041#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020042
43#include <osmocom/vty/telnet_interface.h>
44#include <osmocom/vty/logging.h>
Daniel Willmann56f62732015-12-02 11:22:53 +010045#include <osmocom/vty/command.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020046
Harald Welteffa7c0a2015-12-23 00:03:41 +010047#include <osmocom/netif/stream.h>
48
Harald Welte75a4e652015-12-22 23:59:24 +010049#include <osmocom/sigtran/sua.h>
50#include <osmocom/sigtran/protocol/sua.h>
51#include <osmocom/sigtran/sccp_sap.h>
52
Harald Weltea2e6a7a2015-08-29 21:47:39 +020053#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020054#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020055#include "hnbgw_rua.h"
Harald Weltec4338de2015-12-24 00:40:52 +010056#include "hnbgw_cn.h"
Harald Welte90256ba2015-12-23 20:16:36 +010057#include "context_map.h"
Harald Welteb3dae302015-08-30 12:20:09 +020058
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +020059static const char * const osmo_hnbgw_copyright =
60 "OsmoHNBGW - Osmocom Home Node B Gateway implementation\r\n"
61 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
62 "Contributions by Daniel Willmann, Harald Welte, Neels Hofmeyr\r\n"
63 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
64 "This is free software: you are free to change and redistribute it.\r\n"
65 "There is NO WARRANTY, to the extent permitted by law.\r\n";
66
Harald Welteb3dae302015-08-30 12:20:09 +020067static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020068void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020069
Harald Weltec4338de2015-12-24 00:40:52 +010070static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020071
Harald Weltec4338de2015-12-24 00:40:52 +010072static int listen_fd_cb(struct osmo_fd *fd, unsigned int what);
73
74static struct hnb_gw *hnb_gw_create(void *ctx)
75{
76 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
77
78 gw->config.iuh_listen_port = IUH_DEFAULT_SCTP_PORT;
79
Harald Weltec4338de2015-12-24 00:40:52 +010080 gw->next_ue_ctx_id = 23;
81 INIT_LLIST_HEAD(&gw->hnb_list);
82 INIT_LLIST_HEAD(&gw->ue_list);
83 INIT_LLIST_HEAD(&gw->cn_list);
84
85 context_map_init(gw);
86
87 return gw;
88}
89
90struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +020091{
92 struct ue_context *ue;
93
Harald Weltec4338de2015-12-24 00:40:52 +010094 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +020095 if (ue->context_id == id)
96 return ue;
97 }
98 return NULL;
99
100}
101
Harald Weltec4338de2015-12-24 00:40:52 +0100102struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200103{
104 struct ue_context *ue;
105
Harald Weltec4338de2015-12-24 00:40:52 +0100106 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200107 if (!strcmp(ue->imsi, imsi))
108 return ue;
109 }
110 return NULL;
111}
112
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100113void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
114{
115 struct ue_context *ue, *tmp;
116
117 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
118 if (ue->hnb == hnb)
119 ue_context_free(ue);
120 }
121}
122
Harald Weltec4338de2015-12-24 00:40:52 +0100123static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200124{
125 uint32_t id;
126
127 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100128 id = gw->next_ue_ctx_id++;
129 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200130
131 return id;
132}
133
134struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
135{
136 struct ue_context *ue;
137
138 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
139 if (!ue)
140 return NULL;
141
142 ue->hnb = hnb;
143 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltec4338de2015-12-24 00:40:52 +0100144 ue->context_id = get_next_ue_ctx_id(hnb->gw);
145 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200146
147 return ue;
148}
149
150void ue_context_free(struct ue_context *ue)
151{
152 llist_del(&ue->list);
153 talloc_free(ue);
154}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100155static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200156{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100157}
158
159static int hnb_read_cb(struct osmo_stream_srv *conn)
160{
161 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200162 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200163 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200164 int rc;
165
166 if (!msg)
167 return -ENOMEM;
168
Harald Welte350814a2015-09-10 22:32:15 +0200169 /* we store a reference to the HomeNodeB in the msg->dest for the
170 * benefit of varoius downstream processing functions */
171 msg->dst = hnb;
172
Daniel Willmann6480cad2016-01-06 18:06:26 +0100173 rc = osmo_stream_srv_recv(conn, msg);
174 if (rc == -EAGAIN) {
175 /* Notification received */
176 msgb_free(msg);
177 return 0;
178 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200179 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200180 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100181 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100182 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100183 } else if (rc == 0) {
Daniel Willmann6480cad2016-01-06 18:06:26 +0100184 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100185 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100186
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100187 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100188 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200189 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100190 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200191
Daniel Willmann6480cad2016-01-06 18:06:26 +0100192 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100194 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200195 rc = hnbgw_hnbap_rx(hnb, msg);
196 break;
197 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100198 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200199 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200200 break;
201 case IUH_PPI_SABP:
202 case IUH_PPI_RNA:
203 case IUH_PPI_PUA:
204 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100205 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200206 rc = 0;
207 break;
208 default:
209 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100210 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200211 rc = 0;
212 break;
213 }
214
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100215out:
Harald Weltef2f30002015-09-08 00:09:23 +0200216 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200217 return rc;
218}
219
Harald Welte3f712562015-09-07 21:53:25 +0200220static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
221{
222 struct hnb_context *ctx = fd->data;
223 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100224 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200225 .sinfo_stream = ctx->hnbap_stream,
226 };
227 int rc;
228
229 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
230 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200231 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200232 return rc;
233}
234
Daniel Willmann6480cad2016-01-06 18:06:26 +0100235struct 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 +0100236{
237 struct hnb_context *ctx;
238
239 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
240 if (!ctx)
241 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100242 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100243
244 ctx->gw = gw;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100245 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
246 if (!ctx->conn) {
247 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100248 talloc_free(ctx);
249 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100250 }
251
Harald Welte90256ba2015-12-23 20:16:36 +0100252 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100253 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100254}
255
256void hnb_context_release(struct hnb_context *ctx)
257{
258 struct hnbgw_context_map *map, *map2;
259
260 /* remove from the list of HNB contexts */
261 llist_del(&ctx->list);
262
263 /* deactivate all context maps */
264 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
265 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200266 * gone. Let's hope the second osmo_llist_del in the
267 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100268 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200269 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100270 context_map_deactivate(map);
271 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100272 ue_context_free_by_hnb(ctx->gw, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100273 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100274
275 talloc_free(ctx);
276}
277
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200278/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100279static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200280{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100281 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200282 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200283
Daniel Willmann6480cad2016-01-06 18:06:26 +0100284 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200285 if (!ctx)
286 return -ENOMEM;
287
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200288 return 0;
289}
290
Harald Welteb3dae302015-08-30 12:20:09 +0200291static const struct log_info_cat log_cat[] = {
292 [DMAIN] = {
Neels Hofmeyr6341f4d2016-04-04 18:02:49 +0200293 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200294 .color = "",
295 .description = "Main program",
296 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100297 [DHNBAP] = {
298 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
299 .color = "",
300 .description = "Home Node B Application Part",
301 },
Harald Welte75a4e652015-12-22 23:59:24 +0100302 [DSUA] = {
303 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
304 .color = "",
305 .description = "SCCP User Adaptation",
306 },
Harald Weltef42317b2015-12-23 15:36:31 +0100307 [DRUA] = {
308 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
309 .color = "",
310 .description = "RANAP User Adaptation",
311 },
312 [DRANAP] = {
313 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
314 .color = "",
315 .description = "RAN Application Part",
316 },
Harald Welteb3dae302015-08-30 12:20:09 +0200317};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200318
Harald Welteb3dae302015-08-30 12:20:09 +0200319static const struct log_info hnbgw_log_info = {
320 .cat = log_cat,
321 .num_cat = ARRAY_SIZE(log_cat),
322};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200323
Harald Welteb3dae302015-08-30 12:20:09 +0200324static struct vty_app_info vty_info = {
325 .name = "OsmoHNBGW",
326 .version = "0",
327};
328
Daniel Willmann56f62732015-12-02 11:22:53 +0100329static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
330{
Daniel Willmann5f8c7182016-01-14 15:42:07 +0100331 struct hnbgw_context_map *map;
332
Daniel Willmann56f62732015-12-02 11:22:53 +0100333 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
334 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
335 VTY_NEWLINE);
336 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
Daniel Willmann5f8c7182016-01-14 15:42:07 +0100337
338 llist_for_each_entry(map, &hnb->map_list, hnb_list) {
339 vty_out(vty, " Map %u->%u (RUA->SUA) cnlink=%p state=%u%s", map->rua_ctx_id, map->scu_conn_id,
340 map->cn_link, map->state, VTY_NEWLINE);
341
342 }
Daniel Willmann56f62732015-12-02 11:22:53 +0100343}
344
345static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
346{
347 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
348}
349
350DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
351{
352 struct hnb_context *hnb;
353
Harald Weltec4338de2015-12-24 00:40:52 +0100354 llist_for_each_entry(hnb, &g_hnb_gw->hnb_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100355 vty_dump_hnb_info(vty, hnb);
356 }
357
358 return CMD_SUCCESS;
359}
360
361DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
362{
363 struct ue_context *ue;
364
Harald Weltec4338de2015-12-24 00:40:52 +0100365 llist_for_each_entry(ue, &g_hnb_gw->ue_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100366 vty_dump_ue_info(vty, ue);
367 }
368
369 return CMD_SUCCESS;
370}
371
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100372DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
373{
374 talloc_report_full(tall_hnb_ctx, stderr);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100375 talloc_report_full(talloc_asn1_ctx, stderr);
376
377 return CMD_SUCCESS;
378}
379
Daniel Willmann56f62732015-12-02 11:22:53 +0100380static void hnbgw_vty_init(void)
381{
382 install_element_ve(&show_hnb_cmd);
383 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100384 install_element_ve(&show_talloc_cmd);
Neels Hofmeyr110bb5c2016-02-11 17:51:11 +0100385
386 logging_vty_add_cmds(&hnbgw_log_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100387}
388
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200389
390static struct {
391 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200392 const char *config_file;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200393} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200394 0,
395 "osmo-hnbgw.cfg"
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200396};
397
398static void print_usage()
399{
400 printf("Usage: osmo-hnbgw\n");
401}
402
403static void print_help()
404{
405 printf(" -h --help This text.\n");
406 printf(" -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN Enable debugging.\n");
407 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200408 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200409 printf(" -s --disable-color\n");
410 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
411 printf(" -V --version Print the version of OsmoHNBGW.\n");
412 printf(" -e --log-level number Set a global loglevel.\n");
413}
414
415static void handle_options(int argc, char **argv)
416{
417 while (1) {
418 int option_index = 0, c;
419 static struct option long_options[] = {
420 {"help", 0, 0, 'h'},
421 {"debug", 1, 0, 'd'},
422 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200423 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200424 {"disable-color", 0, 0, 's'},
425 {"timestamp", 0, 0, 'T'},
426 {"version", 0, 0, 'V' },
427 {"log-level", 1, 0, 'e'},
428 {0, 0, 0, 0}
429 };
430
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200431 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200432 long_options, &option_index);
433 if (c == -1)
434 break;
435
436 switch (c) {
437 case 'h':
438 print_usage();
439 print_help();
440 exit(0);
441 case 's':
442 log_set_use_color(osmo_stderr_target, 0);
443 break;
444 case 'd':
445 log_parse_category_mask(osmo_stderr_target, optarg);
446 break;
447 case 'D':
448 hnbgw_cmdline_config.daemonize = 1;
449 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200450 case 'c':
451 hnbgw_cmdline_config.config_file = optarg;
452 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200453 case 'T':
454 log_set_print_timestamp(osmo_stderr_target, 1);
455 break;
456 case 'e':
457 log_set_log_level(osmo_stderr_target, atoi(optarg));
458 break;
459 case 'V':
460 print_version(1);
461 exit(0);
462 break;
463 default:
464 /* catch unknown options *as well as* missing arguments. */
465 fprintf(stderr, "Error in command line options. Exiting.\n");
466 exit(-1);
467 break;
468 }
469 }
470}
471
472
Harald Welteb3dae302015-08-30 12:20:09 +0200473int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200474{
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200475 struct osmo_sccp_user *sua_user;
476 struct osmo_sccp_link *sua_link;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100477 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200478 int rc;
479
480 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200481 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200482
Harald Weltec4338de2015-12-24 00:40:52 +0100483 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200484 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100485
Harald Welteb3dae302015-08-30 12:20:09 +0200486 rc = osmo_init_logging(&hnbgw_log_info);
487 if (rc < 0)
488 exit(1);
489
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200490 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200491 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200492
Daniel Willmann56f62732015-12-02 11:22:53 +0100493 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200494
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200495 /* Handle options after vty_init(), for --version */
496 handle_options(argc, argv);
497
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200498 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
499 if (rc < 0) {
500 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
501 hnbgw_cmdline_config.config_file);
502 return 1;
503 }
504
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100505 LOGP(DMAIN, LOGL_NOTICE, "VTY at %s %d\n",
506 vty_get_bind_addr(), 2323);
507 rc = telnet_init_dynif(NULL, g_hnb_gw, vty_get_bind_addr(), 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200508 if (rc < 0) {
509 perror("Error binding VTY port");
510 exit(1);
511 }
512
Harald Welte75a4e652015-12-22 23:59:24 +0100513 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100514 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100515
Daniel Willmann4deab942016-01-14 15:35:11 +0100516 g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT, 0);
517 g_hnb_gw->cnlink_ps = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.2", SUA_PORT, 1);
Harald Weltec4338de2015-12-24 00:40:52 +0100518
Daniel Willmann6480cad2016-01-06 18:06:26 +0100519 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
520 if (!srv) {
521 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200522 exit(1);
523 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100524 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
525 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
526 osmo_stream_srv_link_set_addr(srv, "0.0.0.0");
527 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_listen_port);
528 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
529
530 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100531 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100532 exit(1);
533 }
534 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200535
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200536 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200537 rc = osmo_daemonize();
538 if (rc < 0) {
539 perror("Error during daemonize");
540 exit(1);
541 }
542 }
543
544 while (1) {
545 rc = osmo_select_main(0);
546 if (rc < 0)
547 exit(3);
548 }
549
550 /* not reached */
551 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200552}