blob: 5f5c31184cfdfb26e2a9d54716cc08572188a21b [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 Welteda86fe52017-11-21 08:14:37 +010047#include <osmocom/vty/command.h>
Neels Hofmeyr2c56f6d2017-12-20 02:31:49 +010048#include <osmocom/vty/ports.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020049
Harald Welteffa7c0a2015-12-23 00:03:41 +010050#include <osmocom/netif/stream.h>
51
Harald Welteda86fe52017-11-21 08:14:37 +010052#include <osmocom/ranap/ranap_common.h>
53
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020054#include <osmocom/sigtran/protocol/m3ua.h>
Harald Welte75a4e652015-12-22 23:59:24 +010055#include <osmocom/sigtran/sccp_sap.h>
56
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020057#include <osmocom/iuh/hnbgw.h>
58#include <osmocom/iuh/hnbgw_hnbap.h>
59#include <osmocom/iuh/hnbgw_rua.h>
60#include <osmocom/iuh/hnbgw_cn.h>
61#include <osmocom/iuh/context_map.h>
Harald Welteb3dae302015-08-30 12:20:09 +020062
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +020063static const char * const osmo_hnbgw_copyright =
64 "OsmoHNBGW - Osmocom Home Node B Gateway implementation\r\n"
65 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
66 "Contributions by Daniel Willmann, Harald Welte, Neels Hofmeyr\r\n"
67 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
68 "This is free software: you are free to change and redistribute it.\r\n"
69 "There is NO WARRANTY, to the extent permitted by law.\r\n";
70
Harald Welteb3dae302015-08-30 12:20:09 +020071static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020072void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020073
Harald Weltec4338de2015-12-24 00:40:52 +010074static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020075
Harald Weltec4338de2015-12-24 00:40:52 +010076static struct hnb_gw *hnb_gw_create(void *ctx)
77{
78 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
79
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +020080 /* strdup so we can easily talloc_free in the VTY code */
81 gw->config.iuh_local_ip = talloc_strdup(gw, HNBGW_LOCAL_IP_DEFAULT);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +020082 gw->config.iuh_local_port = IUH_DEFAULT_SCTP_PORT;
Harald Weltec4338de2015-12-24 00:40:52 +010083
Harald Weltec4338de2015-12-24 00:40:52 +010084 gw->next_ue_ctx_id = 23;
85 INIT_LLIST_HEAD(&gw->hnb_list);
86 INIT_LLIST_HEAD(&gw->ue_list);
Harald Weltec4338de2015-12-24 00:40:52 +010087
88 context_map_init(gw);
89
90 return gw;
91}
92
93struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +020094{
95 struct ue_context *ue;
96
Harald Weltec4338de2015-12-24 00:40:52 +010097 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +020098 if (ue->context_id == id)
99 return ue;
100 }
101 return NULL;
102
103}
104
Harald Weltec4338de2015-12-24 00:40:52 +0100105struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200106{
107 struct ue_context *ue;
108
Harald Weltec4338de2015-12-24 00:40:52 +0100109 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200110 if (!strcmp(ue->imsi, imsi))
111 return ue;
112 }
113 return NULL;
114}
115
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200116struct ue_context *ue_context_by_tmsi(struct hnb_gw *gw, uint32_t tmsi)
117{
118 struct ue_context *ue;
119
120 llist_for_each_entry(ue, &gw->ue_list, list) {
121 if (ue->tmsi == tmsi)
122 return ue;
123 }
124 return NULL;
125}
126
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100127void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
128{
129 struct ue_context *ue, *tmp;
130
131 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
132 if (ue->hnb == hnb)
133 ue_context_free(ue);
134 }
135}
136
Harald Weltec4338de2015-12-24 00:40:52 +0100137static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200138{
139 uint32_t id;
140
141 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100142 id = gw->next_ue_ctx_id++;
143 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200144
145 return id;
146}
147
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200148struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi,
149 uint32_t tmsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200150{
151 struct ue_context *ue;
152
153 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
154 if (!ue)
155 return NULL;
156
157 ue->hnb = hnb;
Harald Weltee08cdd92016-11-26 00:01:53 +0100158 if (imsi) {
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200159 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltee08cdd92016-11-26 00:01:53 +0100160 ue->imsi[sizeof(ue->imsi)-1] = '\0';
161 } else
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200162 ue->imsi[0] = '\0';
163 ue->tmsi = tmsi;
Harald Weltec4338de2015-12-24 00:40:52 +0100164 ue->context_id = get_next_ue_ctx_id(hnb->gw);
165 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200166
Neels Hofmeyr0476e282016-09-22 19:37:29 +0200167 LOGP(DHNBAP, LOGL_INFO, "created UE context: id 0x%x, imsi %s, tmsi 0x%x\n",
168 ue->context_id, imsi? imsi : "-", tmsi);
169
Harald Welteb534e5c2015-09-11 00:15:16 +0200170 return ue;
171}
172
173void ue_context_free(struct ue_context *ue)
174{
175 llist_del(&ue->list);
176 talloc_free(ue);
177}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100178static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200179{
Harald Welteda86fe52017-11-21 08:14:37 +0100180 return 0;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100181}
182
183static int hnb_read_cb(struct osmo_stream_srv *conn)
184{
185 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200186 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200187 int rc;
188
189 if (!msg)
190 return -ENOMEM;
191
Harald Welte350814a2015-09-10 22:32:15 +0200192 /* we store a reference to the HomeNodeB in the msg->dest for the
193 * benefit of varoius downstream processing functions */
194 msg->dst = hnb;
195
Daniel Willmann6480cad2016-01-06 18:06:26 +0100196 rc = osmo_stream_srv_recv(conn, msg);
197 if (rc == -EAGAIN) {
198 /* Notification received */
199 msgb_free(msg);
200 return 0;
201 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200202 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200203 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100204 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100205 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100206 } else if (rc == 0) {
Daniel Willmann6480cad2016-01-06 18:06:26 +0100207 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100208 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100209
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100210 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100211 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200212 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100213 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200214
Daniel Willmann6480cad2016-01-06 18:06:26 +0100215 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200216 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100217 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200218 rc = hnbgw_hnbap_rx(hnb, msg);
219 break;
220 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100221 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200222 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200223 break;
224 case IUH_PPI_SABP:
225 case IUH_PPI_RNA:
226 case IUH_PPI_PUA:
Harald Welteda86fe52017-11-21 08:14:37 +0100227 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100228 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200229 rc = 0;
230 break;
231 default:
Harald Welteda86fe52017-11-21 08:14:37 +0100232 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%lu received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100233 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200234 rc = 0;
235 break;
236 }
237
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100238out:
Harald Weltef2f30002015-09-08 00:09:23 +0200239 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200240 return rc;
241}
242
Daniel Willmann6480cad2016-01-06 18:06:26 +0100243struct 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 +0100244{
245 struct hnb_context *ctx;
246
247 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
248 if (!ctx)
249 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100250 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100251
252 ctx->gw = gw;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100253 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
254 if (!ctx->conn) {
255 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100256 talloc_free(ctx);
257 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100258 }
259
Harald Welte90256ba2015-12-23 20:16:36 +0100260 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100261 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100262}
263
264void hnb_context_release(struct hnb_context *ctx)
265{
266 struct hnbgw_context_map *map, *map2;
267
268 /* remove from the list of HNB contexts */
269 llist_del(&ctx->list);
270
271 /* deactivate all context maps */
272 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
273 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200274 * gone. Let's hope the second osmo_llist_del in the
275 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100276 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200277 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100278 context_map_deactivate(map);
279 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100280 ue_context_free_by_hnb(ctx->gw, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100281 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100282
283 talloc_free(ctx);
284}
285
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200286/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100287static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200288{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100289 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200290 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200291
Daniel Willmann6480cad2016-01-06 18:06:26 +0100292 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200293 if (!ctx)
294 return -ENOMEM;
295
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200296 return 0;
297}
298
Harald Welteb3dae302015-08-30 12:20:09 +0200299static const struct log_info_cat log_cat[] = {
300 [DMAIN] = {
Neels Hofmeyr6341f4d2016-04-04 18:02:49 +0200301 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200302 .color = "",
303 .description = "Main program",
304 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100305 [DHNBAP] = {
306 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
307 .color = "",
308 .description = "Home Node B Application Part",
309 },
Harald Weltef42317b2015-12-23 15:36:31 +0100310 [DRUA] = {
311 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
312 .color = "",
313 .description = "RANAP User Adaptation",
314 },
315 [DRANAP] = {
316 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
317 .color = "",
318 .description = "RAN Application Part",
319 },
Harald Welteb3dae302015-08-30 12:20:09 +0200320};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200321
Harald Welteb3dae302015-08-30 12:20:09 +0200322static const struct log_info hnbgw_log_info = {
323 .cat = log_cat,
324 .num_cat = ARRAY_SIZE(log_cat),
325};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200326
Harald Welteb3dae302015-08-30 12:20:09 +0200327static struct vty_app_info vty_info = {
328 .name = "OsmoHNBGW",
Max1e57ba52017-12-28 12:27:41 +0100329 .version = PACKAGE_VERSION,
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200330 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200331};
332
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200333static struct {
334 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200335 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200336 bool log_disable_color;
337 bool log_enable_timestamp;
338 int log_level;
339 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200340} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200341 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200342 "osmo-hnbgw.cfg",
343 true,
344 false,
345 0,
346 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200347};
348
349static void print_usage()
350{
351 printf("Usage: osmo-hnbgw\n");
352}
353
354static void print_help()
355{
356 printf(" -h --help This text.\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200357 printf(" -d option --debug=DHNBAP:DRUA:DRANAP:DMAIN Enable debugging.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200358 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200359 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200360 printf(" -s --disable-color\n");
361 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
362 printf(" -V --version Print the version of OsmoHNBGW.\n");
363 printf(" -e --log-level number Set a global loglevel.\n");
364}
365
366static void handle_options(int argc, char **argv)
367{
368 while (1) {
369 int option_index = 0, c;
370 static struct option long_options[] = {
371 {"help", 0, 0, 'h'},
372 {"debug", 1, 0, 'd'},
373 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200374 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200375 {"disable-color", 0, 0, 's'},
376 {"timestamp", 0, 0, 'T'},
377 {"version", 0, 0, 'V' },
378 {"log-level", 1, 0, 'e'},
379 {0, 0, 0, 0}
380 };
381
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200382 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200383 long_options, &option_index);
384 if (c == -1)
385 break;
386
387 switch (c) {
388 case 'h':
389 print_usage();
390 print_help();
391 exit(0);
392 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200393 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200394 break;
395 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200396 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200397 break;
398 case 'D':
399 hnbgw_cmdline_config.daemonize = 1;
400 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200401 case 'c':
402 hnbgw_cmdline_config.config_file = optarg;
403 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200404 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200405 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200406 break;
407 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200408 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200409 break;
410 case 'V':
411 print_version(1);
412 exit(0);
413 break;
414 default:
415 /* catch unknown options *as well as* missing arguments. */
416 fprintf(stderr, "Error in command line options. Exiting.\n");
417 exit(-1);
418 break;
419 }
420 }
421}
422
423
Harald Welteb3dae302015-08-30 12:20:09 +0200424int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200425{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100426 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200427 int rc;
428
429 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200430 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200431 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200432
Harald Weltec4338de2015-12-24 00:40:52 +0100433 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200434 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100435
Harald Welteb3dae302015-08-30 12:20:09 +0200436 rc = osmo_init_logging(&hnbgw_log_info);
437 if (rc < 0)
438 exit(1);
439
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200440 osmo_ss7_init();
441
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200442 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200443 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200444
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200445 osmo_ss7_vty_init_asp(tall_hnb_ctx);
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200446 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
447 logging_vty_add_cmds(&hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200448
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200449 /* Handle options after vty_init(), for --version */
450 handle_options(argc, argv);
451
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200452 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
453 if (rc < 0) {
454 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
455 hnbgw_cmdline_config.config_file);
456 return 1;
457 }
458
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200459 /*
460 * cmdline options take precedence over config file, but if no options
461 * were passed we must not override the config file.
462 */
463 if (hnbgw_cmdline_config.log_disable_color)
464 log_set_use_color(osmo_stderr_target, 0);
465 if (hnbgw_cmdline_config.log_category_mask)
466 log_parse_category_mask(osmo_stderr_target,
467 hnbgw_cmdline_config.log_category_mask);
468 if (hnbgw_cmdline_config.log_enable_timestamp)
469 log_set_print_timestamp(osmo_stderr_target, 1);
470 if (hnbgw_cmdline_config.log_level)
471 log_set_log_level(osmo_stderr_target,
472 hnbgw_cmdline_config.log_level);
473
Neels Hofmeyrade7e8b2017-12-20 02:33:44 +0100474 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 +0200475 if (rc < 0) {
476 perror("Error binding VTY port");
477 exit(1);
478 }
479
Harald Weltebdf3fd12016-01-03 17:04:09 +0100480 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100481
Neels Hofmeyraccb7802017-12-04 15:24:32 +0100482 rc = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", M3UA_PORT, NULL);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200483 if (rc < 0) {
484 LOGP(DMAIN, LOGL_ERROR, "Failed to initialize SCCP link to CN\n");
485 exit(1);
486 }
487
Neels Hofmeyr4e2681d2017-12-24 23:54:07 +0100488 LOGP(DHNBAP, LOGL_NOTICE, "Using RNC-Id %u\n", g_hnb_gw->config.rnc_id);
489
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200490 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200491 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200492 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200493 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100494 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
495 if (!srv) {
496 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200497 exit(1);
498 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100499 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
500 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200501 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200502 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100503 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
504
505 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100506 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100507 exit(1);
508 }
509 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200510
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200511 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200512 rc = osmo_daemonize();
513 if (rc < 0) {
514 perror("Error during daemonize");
515 exit(1);
516 }
517 }
518
519 while (1) {
520 rc = osmo_select_main(0);
521 if (rc < 0)
522 exit(3);
523 }
524
525 /* not reached */
526 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200527}