blob: d2e7b30eb19fce5b2f52e543bf6ff0f46753caaf [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>
Harald Welteb3dae302015-08-30 12:20:09 +020044
45#include <osmocom/vty/telnet_interface.h>
46#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020047
Harald Welteffa7c0a2015-12-23 00:03:41 +010048#include <osmocom/netif/stream.h>
49
Harald Welte75a4e652015-12-22 23:59:24 +010050#include <osmocom/sigtran/sua.h>
51#include <osmocom/sigtran/protocol/sua.h>
52#include <osmocom/sigtran/sccp_sap.h>
53
Harald Weltea2e6a7a2015-08-29 21:47:39 +020054#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020055#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020056#include "hnbgw_rua.h"
Harald Weltec4338de2015-12-24 00:40:52 +010057#include "hnbgw_cn.h"
Harald Welte90256ba2015-12-23 20:16:36 +010058#include "context_map.h"
Harald Welteb3dae302015-08-30 12:20:09 +020059
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +020060static const char * const osmo_hnbgw_copyright =
61 "OsmoHNBGW - Osmocom Home Node B Gateway implementation\r\n"
62 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
63 "Contributions by Daniel Willmann, Harald Welte, Neels Hofmeyr\r\n"
64 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
65 "This is free software: you are free to change and redistribute it.\r\n"
66 "There is NO WARRANTY, to the extent permitted by law.\r\n";
67
Harald Welteb3dae302015-08-30 12:20:09 +020068static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020069void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020070
Harald Weltec4338de2015-12-24 00:40:52 +010071static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020072
Harald Weltec4338de2015-12-24 00:40:52 +010073static int listen_fd_cb(struct osmo_fd *fd, unsigned int what);
74
75static struct hnb_gw *hnb_gw_create(void *ctx)
76{
77 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
78
79 gw->config.iuh_listen_port = IUH_DEFAULT_SCTP_PORT;
80
Harald Weltec4338de2015-12-24 00:40:52 +010081 gw->next_ue_ctx_id = 23;
82 INIT_LLIST_HEAD(&gw->hnb_list);
83 INIT_LLIST_HEAD(&gw->ue_list);
84 INIT_LLIST_HEAD(&gw->cn_list);
85
86 context_map_init(gw);
87
88 return gw;
89}
90
91struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +020092{
93 struct ue_context *ue;
94
Harald Weltec4338de2015-12-24 00:40:52 +010095 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +020096 if (ue->context_id == id)
97 return ue;
98 }
99 return NULL;
100
101}
102
Harald Weltec4338de2015-12-24 00:40:52 +0100103struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200104{
105 struct ue_context *ue;
106
Harald Weltec4338de2015-12-24 00:40:52 +0100107 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200108 if (!strcmp(ue->imsi, imsi))
109 return ue;
110 }
111 return NULL;
112}
113
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100114void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
115{
116 struct ue_context *ue, *tmp;
117
118 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
119 if (ue->hnb == hnb)
120 ue_context_free(ue);
121 }
122}
123
Harald Weltec4338de2015-12-24 00:40:52 +0100124static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200125{
126 uint32_t id;
127
128 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100129 id = gw->next_ue_ctx_id++;
130 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200131
132 return id;
133}
134
135struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
136{
137 struct ue_context *ue;
138
139 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
140 if (!ue)
141 return NULL;
142
143 ue->hnb = hnb;
144 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltec4338de2015-12-24 00:40:52 +0100145 ue->context_id = get_next_ue_ctx_id(hnb->gw);
146 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200147
148 return ue;
149}
150
151void ue_context_free(struct ue_context *ue)
152{
153 llist_del(&ue->list);
154 talloc_free(ue);
155}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100156static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200157{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100158}
159
160static int hnb_read_cb(struct osmo_stream_srv *conn)
161{
162 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200163 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200164 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200165 int rc;
166
167 if (!msg)
168 return -ENOMEM;
169
Harald Welte350814a2015-09-10 22:32:15 +0200170 /* we store a reference to the HomeNodeB in the msg->dest for the
171 * benefit of varoius downstream processing functions */
172 msg->dst = hnb;
173
Daniel Willmann6480cad2016-01-06 18:06:26 +0100174 rc = osmo_stream_srv_recv(conn, msg);
175 if (rc == -EAGAIN) {
176 /* Notification received */
177 msgb_free(msg);
178 return 0;
179 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200180 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200181 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100182 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100183 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100184 } else if (rc == 0) {
Daniel Willmann6480cad2016-01-06 18:06:26 +0100185 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100186 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100187
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100188 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100189 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200190 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100191 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200192
Daniel Willmann6480cad2016-01-06 18:06:26 +0100193 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200194 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100195 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200196 rc = hnbgw_hnbap_rx(hnb, msg);
197 break;
198 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100199 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200200 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200201 break;
202 case IUH_PPI_SABP:
203 case IUH_PPI_RNA:
204 case IUH_PPI_PUA:
205 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100206 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200207 rc = 0;
208 break;
209 default:
210 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100211 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200212 rc = 0;
213 break;
214 }
215
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100216out:
Harald Weltef2f30002015-09-08 00:09:23 +0200217 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200218 return rc;
219}
220
Harald Welte3f712562015-09-07 21:53:25 +0200221static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
222{
223 struct hnb_context *ctx = fd->data;
224 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100225 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200226 .sinfo_stream = ctx->hnbap_stream,
227 };
228 int rc;
229
230 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
231 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200232 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200233 return rc;
234}
235
Daniel Willmann6480cad2016-01-06 18:06:26 +0100236struct 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 +0100237{
238 struct hnb_context *ctx;
239
240 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
241 if (!ctx)
242 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100243 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100244
245 ctx->gw = gw;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100246 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
247 if (!ctx->conn) {
248 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100249 talloc_free(ctx);
250 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100251 }
252
Harald Welte90256ba2015-12-23 20:16:36 +0100253 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100254 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100255}
256
257void hnb_context_release(struct hnb_context *ctx)
258{
259 struct hnbgw_context_map *map, *map2;
260
261 /* remove from the list of HNB contexts */
262 llist_del(&ctx->list);
263
264 /* deactivate all context maps */
265 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
266 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200267 * gone. Let's hope the second osmo_llist_del in the
268 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100269 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200270 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100271 context_map_deactivate(map);
272 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100273 ue_context_free_by_hnb(ctx->gw, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100274 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100275
276 talloc_free(ctx);
277}
278
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200279/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100280static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200281{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100282 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200283 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200284
Daniel Willmann6480cad2016-01-06 18:06:26 +0100285 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200286 if (!ctx)
287 return -ENOMEM;
288
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200289 return 0;
290}
291
Harald Welteb3dae302015-08-30 12:20:09 +0200292static const struct log_info_cat log_cat[] = {
293 [DMAIN] = {
Neels Hofmeyr6341f4d2016-04-04 18:02:49 +0200294 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200295 .color = "",
296 .description = "Main program",
297 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100298 [DHNBAP] = {
299 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
300 .color = "",
301 .description = "Home Node B Application Part",
302 },
Harald Welte75a4e652015-12-22 23:59:24 +0100303 [DSUA] = {
304 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
305 .color = "",
306 .description = "SCCP User Adaptation",
307 },
Harald Weltef42317b2015-12-23 15:36:31 +0100308 [DRUA] = {
309 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
310 .color = "",
311 .description = "RANAP User Adaptation",
312 },
313 [DRANAP] = {
314 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
315 .color = "",
316 .description = "RAN Application Part",
317 },
Harald Welteb3dae302015-08-30 12:20:09 +0200318};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200319
Harald Welteb3dae302015-08-30 12:20:09 +0200320static const struct log_info hnbgw_log_info = {
321 .cat = log_cat,
322 .num_cat = ARRAY_SIZE(log_cat),
323};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200324
Harald Welteb3dae302015-08-30 12:20:09 +0200325static struct vty_app_info vty_info = {
326 .name = "OsmoHNBGW",
327 .version = "0",
328};
329
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200330static struct {
331 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200332 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200333 bool log_disable_color;
334 bool log_enable_timestamp;
335 int log_level;
336 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200337} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200338 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200339 "osmo-hnbgw.cfg",
340 true,
341 false,
342 0,
343 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200344};
345
346static void print_usage()
347{
348 printf("Usage: osmo-hnbgw\n");
349}
350
351static void print_help()
352{
353 printf(" -h --help This text.\n");
354 printf(" -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN Enable debugging.\n");
355 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200356 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200357 printf(" -s --disable-color\n");
358 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
359 printf(" -V --version Print the version of OsmoHNBGW.\n");
360 printf(" -e --log-level number Set a global loglevel.\n");
361}
362
363static void handle_options(int argc, char **argv)
364{
365 while (1) {
366 int option_index = 0, c;
367 static struct option long_options[] = {
368 {"help", 0, 0, 'h'},
369 {"debug", 1, 0, 'd'},
370 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200371 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200372 {"disable-color", 0, 0, 's'},
373 {"timestamp", 0, 0, 'T'},
374 {"version", 0, 0, 'V' },
375 {"log-level", 1, 0, 'e'},
376 {0, 0, 0, 0}
377 };
378
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200379 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200380 long_options, &option_index);
381 if (c == -1)
382 break;
383
384 switch (c) {
385 case 'h':
386 print_usage();
387 print_help();
388 exit(0);
389 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200390 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200391 break;
392 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200393 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200394 break;
395 case 'D':
396 hnbgw_cmdline_config.daemonize = 1;
397 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200398 case 'c':
399 hnbgw_cmdline_config.config_file = optarg;
400 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200401 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200402 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200403 break;
404 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200405 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200406 break;
407 case 'V':
408 print_version(1);
409 exit(0);
410 break;
411 default:
412 /* catch unknown options *as well as* missing arguments. */
413 fprintf(stderr, "Error in command line options. Exiting.\n");
414 exit(-1);
415 break;
416 }
417 }
418}
419
420
Harald Welteb3dae302015-08-30 12:20:09 +0200421int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200422{
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200423 struct osmo_sccp_user *sua_user;
424 struct osmo_sccp_link *sua_link;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100425 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200426 int rc;
427
428 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200429 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200430
Harald Weltec4338de2015-12-24 00:40:52 +0100431 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200432 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100433
Harald Welteb3dae302015-08-30 12:20:09 +0200434 rc = osmo_init_logging(&hnbgw_log_info);
435 if (rc < 0)
436 exit(1);
437
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200438 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200439 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200440
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200441 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
442 logging_vty_add_cmds(&hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200443
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200444 /* Handle options after vty_init(), for --version */
445 handle_options(argc, argv);
446
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200447 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
448 if (rc < 0) {
449 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
450 hnbgw_cmdline_config.config_file);
451 return 1;
452 }
453
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200454 /*
455 * cmdline options take precedence over config file, but if no options
456 * were passed we must not override the config file.
457 */
458 if (hnbgw_cmdline_config.log_disable_color)
459 log_set_use_color(osmo_stderr_target, 0);
460 if (hnbgw_cmdline_config.log_category_mask)
461 log_parse_category_mask(osmo_stderr_target,
462 hnbgw_cmdline_config.log_category_mask);
463 if (hnbgw_cmdline_config.log_enable_timestamp)
464 log_set_print_timestamp(osmo_stderr_target, 1);
465 if (hnbgw_cmdline_config.log_level)
466 log_set_log_level(osmo_stderr_target,
467 hnbgw_cmdline_config.log_level);
468
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100469 LOGP(DMAIN, LOGL_NOTICE, "VTY at %s %d\n",
470 vty_get_bind_addr(), 2323);
471 rc = telnet_init_dynif(NULL, g_hnb_gw, vty_get_bind_addr(), 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200472 if (rc < 0) {
473 perror("Error binding VTY port");
474 exit(1);
475 }
476
Harald Welte75a4e652015-12-22 23:59:24 +0100477 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100478 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100479
Daniel Willmann4deab942016-01-14 15:35:11 +0100480 g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT, 0);
481 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 +0100482
Daniel Willmann6480cad2016-01-06 18:06:26 +0100483 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
484 if (!srv) {
485 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200486 exit(1);
487 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100488 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
489 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
490 osmo_stream_srv_link_set_addr(srv, "0.0.0.0");
491 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_listen_port);
492 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
493
494 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100495 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100496 exit(1);
497 }
498 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200499
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200500 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200501 rc = osmo_daemonize();
502 if (rc < 0) {
503 perror("Error during daemonize");
504 exit(1);
505 }
506 }
507
508 while (1) {
509 rc = osmo_select_main(0);
510 if (rc < 0)
511 exit(3);
512 }
513
514 /* not reached */
515 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200516}