blob: 998492798135e59c3464b3ed8c967c7c55f6cf31 [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
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020054#include <osmocom/iuh/hnbgw.h>
55#include <osmocom/iuh/hnbgw_hnbap.h>
56#include <osmocom/iuh/hnbgw_rua.h>
57#include <osmocom/iuh/hnbgw_cn.h>
58#include <osmocom/iuh/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
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +020079 /* strdup so we can easily talloc_free in the VTY code */
80 gw->config.iuh_local_ip = talloc_strdup(gw, HNBGW_LOCAL_IP_DEFAULT);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +020081 gw->config.iuh_local_port = IUH_DEFAULT_SCTP_PORT;
Harald Weltec4338de2015-12-24 00:40:52 +010082
Neels Hofmeyr5ee050c2016-10-13 15:12:18 +020083 gw->config.iucs_remote_ip = talloc_strdup(gw,
84 HNBGW_IUCS_REMOTE_IP_DEFAULT);
85 gw->config.iucs_remote_port = SUA_PORT;
86
87 gw->config.iups_remote_ip = talloc_strdup(gw,
88 HNBGW_IUPS_REMOTE_IP_DEFAULT);
89 gw->config.iups_remote_port = SUA_PORT;
90
Harald Weltec4338de2015-12-24 00:40:52 +010091 gw->next_ue_ctx_id = 23;
92 INIT_LLIST_HEAD(&gw->hnb_list);
93 INIT_LLIST_HEAD(&gw->ue_list);
94 INIT_LLIST_HEAD(&gw->cn_list);
95
96 context_map_init(gw);
97
98 return gw;
99}
100
101struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +0200102{
103 struct ue_context *ue;
104
Harald Weltec4338de2015-12-24 00:40:52 +0100105 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200106 if (ue->context_id == id)
107 return ue;
108 }
109 return NULL;
110
111}
112
Harald Weltec4338de2015-12-24 00:40:52 +0100113struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200114{
115 struct ue_context *ue;
116
Harald Weltec4338de2015-12-24 00:40:52 +0100117 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200118 if (!strcmp(ue->imsi, imsi))
119 return ue;
120 }
121 return NULL;
122}
123
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200124struct ue_context *ue_context_by_tmsi(struct hnb_gw *gw, uint32_t tmsi)
125{
126 struct ue_context *ue;
127
128 llist_for_each_entry(ue, &gw->ue_list, list) {
129 if (ue->tmsi == tmsi)
130 return ue;
131 }
132 return NULL;
133}
134
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100135void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
136{
137 struct ue_context *ue, *tmp;
138
139 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
140 if (ue->hnb == hnb)
141 ue_context_free(ue);
142 }
143}
144
Harald Weltec4338de2015-12-24 00:40:52 +0100145static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200146{
147 uint32_t id;
148
149 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100150 id = gw->next_ue_ctx_id++;
151 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200152
153 return id;
154}
155
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200156struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi,
157 uint32_t tmsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200158{
159 struct ue_context *ue;
160
161 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
162 if (!ue)
163 return NULL;
164
165 ue->hnb = hnb;
Harald Weltee08cdd92016-11-26 00:01:53 +0100166 if (imsi) {
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200167 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltee08cdd92016-11-26 00:01:53 +0100168 ue->imsi[sizeof(ue->imsi)-1] = '\0';
169 } else
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200170 ue->imsi[0] = '\0';
171 ue->tmsi = tmsi;
Harald Weltec4338de2015-12-24 00:40:52 +0100172 ue->context_id = get_next_ue_ctx_id(hnb->gw);
173 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200174
Neels Hofmeyr0476e282016-09-22 19:37:29 +0200175 LOGP(DHNBAP, LOGL_INFO, "created UE context: id 0x%x, imsi %s, tmsi 0x%x\n",
176 ue->context_id, imsi? imsi : "-", tmsi);
177
Harald Welteb534e5c2015-09-11 00:15:16 +0200178 return ue;
179}
180
181void ue_context_free(struct ue_context *ue)
182{
183 llist_del(&ue->list);
184 talloc_free(ue);
185}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100186static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200187{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100188}
189
190static int hnb_read_cb(struct osmo_stream_srv *conn)
191{
192 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200194 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200195 int rc;
196
197 if (!msg)
198 return -ENOMEM;
199
Harald Welte350814a2015-09-10 22:32:15 +0200200 /* we store a reference to the HomeNodeB in the msg->dest for the
201 * benefit of varoius downstream processing functions */
202 msg->dst = hnb;
203
Daniel Willmann6480cad2016-01-06 18:06:26 +0100204 rc = osmo_stream_srv_recv(conn, msg);
205 if (rc == -EAGAIN) {
206 /* Notification received */
207 msgb_free(msg);
208 return 0;
209 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200210 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200211 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100212 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100213 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100214 } else if (rc == 0) {
Daniel Willmann6480cad2016-01-06 18:06:26 +0100215 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100216 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100217
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100218 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100219 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200220 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100221 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200222
Daniel Willmann6480cad2016-01-06 18:06:26 +0100223 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200224 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100225 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200226 rc = hnbgw_hnbap_rx(hnb, msg);
227 break;
228 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100229 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200230 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200231 break;
232 case IUH_PPI_SABP:
233 case IUH_PPI_RNA:
234 case IUH_PPI_PUA:
235 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100236 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200237 rc = 0;
238 break;
239 default:
240 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100241 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200242 rc = 0;
243 break;
244 }
245
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100246out:
Harald Weltef2f30002015-09-08 00:09:23 +0200247 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200248 return rc;
249}
250
Harald Welte3f712562015-09-07 21:53:25 +0200251static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
252{
253 struct hnb_context *ctx = fd->data;
254 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100255 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200256 .sinfo_stream = ctx->hnbap_stream,
257 };
258 int rc;
259
260 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
261 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200262 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200263 return rc;
264}
265
Daniel Willmann6480cad2016-01-06 18:06:26 +0100266struct 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 +0100267{
268 struct hnb_context *ctx;
269
270 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
271 if (!ctx)
272 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100273 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100274
275 ctx->gw = gw;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100276 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
277 if (!ctx->conn) {
278 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100279 talloc_free(ctx);
280 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100281 }
282
Harald Welte90256ba2015-12-23 20:16:36 +0100283 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100284 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100285}
286
287void hnb_context_release(struct hnb_context *ctx)
288{
289 struct hnbgw_context_map *map, *map2;
290
291 /* remove from the list of HNB contexts */
292 llist_del(&ctx->list);
293
294 /* deactivate all context maps */
295 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
296 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200297 * gone. Let's hope the second osmo_llist_del in the
298 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100299 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200300 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100301 context_map_deactivate(map);
302 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100303 ue_context_free_by_hnb(ctx->gw, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100304 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100305
306 talloc_free(ctx);
307}
308
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200309/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100310static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200311{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100312 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200313 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200314
Daniel Willmann6480cad2016-01-06 18:06:26 +0100315 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200316 if (!ctx)
317 return -ENOMEM;
318
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200319 return 0;
320}
321
Harald Welteb3dae302015-08-30 12:20:09 +0200322static const struct log_info_cat log_cat[] = {
323 [DMAIN] = {
Neels Hofmeyr6341f4d2016-04-04 18:02:49 +0200324 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200325 .color = "",
326 .description = "Main program",
327 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100328 [DHNBAP] = {
329 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
330 .color = "",
331 .description = "Home Node B Application Part",
332 },
Harald Welte75a4e652015-12-22 23:59:24 +0100333 [DSUA] = {
334 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
335 .color = "",
336 .description = "SCCP User Adaptation",
337 },
Harald Weltef42317b2015-12-23 15:36:31 +0100338 [DRUA] = {
339 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
340 .color = "",
341 .description = "RANAP User Adaptation",
342 },
343 [DRANAP] = {
344 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
345 .color = "",
346 .description = "RAN Application Part",
347 },
Harald Welteb3dae302015-08-30 12:20:09 +0200348};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200349
Harald Welteb3dae302015-08-30 12:20:09 +0200350static const struct log_info hnbgw_log_info = {
351 .cat = log_cat,
352 .num_cat = ARRAY_SIZE(log_cat),
353};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200354
Harald Welteb3dae302015-08-30 12:20:09 +0200355static struct vty_app_info vty_info = {
356 .name = "OsmoHNBGW",
357 .version = "0",
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200358 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200359};
360
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200361static struct {
362 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200363 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200364 bool log_disable_color;
365 bool log_enable_timestamp;
366 int log_level;
367 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200368} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200369 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200370 "osmo-hnbgw.cfg",
371 true,
372 false,
373 0,
374 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200375};
376
377static void print_usage()
378{
379 printf("Usage: osmo-hnbgw\n");
380}
381
382static void print_help()
383{
384 printf(" -h --help This text.\n");
385 printf(" -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN Enable debugging.\n");
386 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200387 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200388 printf(" -s --disable-color\n");
389 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
390 printf(" -V --version Print the version of OsmoHNBGW.\n");
391 printf(" -e --log-level number Set a global loglevel.\n");
392}
393
394static void handle_options(int argc, char **argv)
395{
396 while (1) {
397 int option_index = 0, c;
398 static struct option long_options[] = {
399 {"help", 0, 0, 'h'},
400 {"debug", 1, 0, 'd'},
401 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200402 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200403 {"disable-color", 0, 0, 's'},
404 {"timestamp", 0, 0, 'T'},
405 {"version", 0, 0, 'V' },
406 {"log-level", 1, 0, 'e'},
407 {0, 0, 0, 0}
408 };
409
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200410 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200411 long_options, &option_index);
412 if (c == -1)
413 break;
414
415 switch (c) {
416 case 'h':
417 print_usage();
418 print_help();
419 exit(0);
420 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200421 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200422 break;
423 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200424 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200425 break;
426 case 'D':
427 hnbgw_cmdline_config.daemonize = 1;
428 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200429 case 'c':
430 hnbgw_cmdline_config.config_file = optarg;
431 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200432 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200433 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200434 break;
435 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200436 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200437 break;
438 case 'V':
439 print_version(1);
440 exit(0);
441 break;
442 default:
443 /* catch unknown options *as well as* missing arguments. */
444 fprintf(stderr, "Error in command line options. Exiting.\n");
445 exit(-1);
446 break;
447 }
448 }
449}
450
451
Harald Welteb3dae302015-08-30 12:20:09 +0200452int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200453{
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200454 struct osmo_sccp_user *sua_user;
455 struct osmo_sccp_link *sua_link;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100456 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200457 int rc;
458
459 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200460 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200461 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200462
Harald Weltec4338de2015-12-24 00:40:52 +0100463 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200464 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100465
Harald Welteb3dae302015-08-30 12:20:09 +0200466 rc = osmo_init_logging(&hnbgw_log_info);
467 if (rc < 0)
468 exit(1);
469
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200470 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200471 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200472
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200473 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
474 logging_vty_add_cmds(&hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200475
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200476 /* Handle options after vty_init(), for --version */
477 handle_options(argc, argv);
478
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200479 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
480 if (rc < 0) {
481 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
482 hnbgw_cmdline_config.config_file);
483 return 1;
484 }
485
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200486 /*
487 * cmdline options take precedence over config file, but if no options
488 * were passed we must not override the config file.
489 */
490 if (hnbgw_cmdline_config.log_disable_color)
491 log_set_use_color(osmo_stderr_target, 0);
492 if (hnbgw_cmdline_config.log_category_mask)
493 log_parse_category_mask(osmo_stderr_target,
494 hnbgw_cmdline_config.log_category_mask);
495 if (hnbgw_cmdline_config.log_enable_timestamp)
496 log_set_print_timestamp(osmo_stderr_target, 1);
497 if (hnbgw_cmdline_config.log_level)
498 log_set_log_level(osmo_stderr_target,
499 hnbgw_cmdline_config.log_level);
500
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100501 rc = telnet_init_dynif(NULL, g_hnb_gw, vty_get_bind_addr(), 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200502 if (rc < 0) {
503 perror("Error binding VTY port");
504 exit(1);
505 }
506
Harald Welte75a4e652015-12-22 23:59:24 +0100507 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100508 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100509
Neels Hofmeyr5ee050c2016-10-13 15:12:18 +0200510 OSMO_ASSERT(g_hnb_gw->config.iucs_remote_ip);
511 g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw,
512 g_hnb_gw->config.iucs_remote_ip,
513 g_hnb_gw->config.iucs_remote_port,
514 0);
515 OSMO_ASSERT(g_hnb_gw->config.iups_remote_ip);
516 g_hnb_gw->cnlink_ps = hnbgw_cnlink_init(g_hnb_gw,
517 g_hnb_gw->config.iups_remote_ip,
518 g_hnb_gw->config.iups_remote_port,
519 1);
Harald Weltec4338de2015-12-24 00:40:52 +0100520
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200521 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200522 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200523 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200524 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100525 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
526 if (!srv) {
527 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200528 exit(1);
529 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100530 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
531 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200532 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200533 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100534 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
535
536 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100537 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100538 exit(1);
539 }
540 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200541
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200542 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200543 rc = osmo_daemonize();
544 if (rc < 0) {
545 perror("Error during daemonize");
546 exit(1);
547 }
548 }
549
550 while (1) {
551 rc = osmo_select_main(0);
552 if (rc < 0)
553 exit(3);
554 }
555
556 /* not reached */
557 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200558}