blob: 4cb3c694e8f3bccd669e12c9a1f479f01a8749af [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>
Neels Hofmeyr1d03f192016-08-18 03:15:14 +020028#include <stdbool.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020029
Harald Welteb3dae302015-08-30 12:20:09 +020030#include <sys/types.h>
31#include <sys/socket.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020032#include <netinet/in.h>
Harald Welteb3dae302015-08-30 12:20:09 +020033#include <netinet/sctp.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020034#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020035
36#include <osmocom/core/application.h>
37#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020038#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020039#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020040#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020041#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020042#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020043
44#include <osmocom/vty/telnet_interface.h>
45#include <osmocom/vty/logging.h>
Daniel Willmann56f62732015-12-02 11:22:53 +010046#include <osmocom/vty/command.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
Daniel Willmann56f62732015-12-02 11:22:53 +0100330static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
331{
Daniel Willmann5f8c7182016-01-14 15:42:07 +0100332 struct hnbgw_context_map *map;
333
Daniel Willmann56f62732015-12-02 11:22:53 +0100334 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
335 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
336 VTY_NEWLINE);
337 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 +0100338
339 llist_for_each_entry(map, &hnb->map_list, hnb_list) {
340 vty_out(vty, " Map %u->%u (RUA->SUA) cnlink=%p state=%u%s", map->rua_ctx_id, map->scu_conn_id,
341 map->cn_link, map->state, VTY_NEWLINE);
342
343 }
Daniel Willmann56f62732015-12-02 11:22:53 +0100344}
345
346static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
347{
348 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
349}
350
351DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
352{
353 struct hnb_context *hnb;
354
Harald Weltec4338de2015-12-24 00:40:52 +0100355 llist_for_each_entry(hnb, &g_hnb_gw->hnb_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100356 vty_dump_hnb_info(vty, hnb);
357 }
358
359 return CMD_SUCCESS;
360}
361
362DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
363{
364 struct ue_context *ue;
365
Harald Weltec4338de2015-12-24 00:40:52 +0100366 llist_for_each_entry(ue, &g_hnb_gw->ue_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100367 vty_dump_ue_info(vty, ue);
368 }
369
370 return CMD_SUCCESS;
371}
372
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100373DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
374{
375 talloc_report_full(tall_hnb_ctx, stderr);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100376 talloc_report_full(talloc_asn1_ctx, stderr);
377
378 return CMD_SUCCESS;
379}
380
Daniel Willmann56f62732015-12-02 11:22:53 +0100381static void hnbgw_vty_init(void)
382{
383 install_element_ve(&show_hnb_cmd);
384 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100385 install_element_ve(&show_talloc_cmd);
Neels Hofmeyr110bb5c2016-02-11 17:51:11 +0100386
387 logging_vty_add_cmds(&hnbgw_log_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100388}
389
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200390
391static struct {
392 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200393 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200394 bool log_disable_color;
395 bool log_enable_timestamp;
396 int log_level;
397 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200398} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200399 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200400 "osmo-hnbgw.cfg",
401 true,
402 false,
403 0,
404 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200405};
406
407static void print_usage()
408{
409 printf("Usage: osmo-hnbgw\n");
410}
411
412static void print_help()
413{
414 printf(" -h --help This text.\n");
415 printf(" -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN Enable debugging.\n");
416 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200417 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200418 printf(" -s --disable-color\n");
419 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
420 printf(" -V --version Print the version of OsmoHNBGW.\n");
421 printf(" -e --log-level number Set a global loglevel.\n");
422}
423
424static void handle_options(int argc, char **argv)
425{
426 while (1) {
427 int option_index = 0, c;
428 static struct option long_options[] = {
429 {"help", 0, 0, 'h'},
430 {"debug", 1, 0, 'd'},
431 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200432 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200433 {"disable-color", 0, 0, 's'},
434 {"timestamp", 0, 0, 'T'},
435 {"version", 0, 0, 'V' },
436 {"log-level", 1, 0, 'e'},
437 {0, 0, 0, 0}
438 };
439
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200440 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200441 long_options, &option_index);
442 if (c == -1)
443 break;
444
445 switch (c) {
446 case 'h':
447 print_usage();
448 print_help();
449 exit(0);
450 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200451 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200452 break;
453 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200454 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200455 break;
456 case 'D':
457 hnbgw_cmdline_config.daemonize = 1;
458 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200459 case 'c':
460 hnbgw_cmdline_config.config_file = optarg;
461 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200462 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200463 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200464 break;
465 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200466 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200467 break;
468 case 'V':
469 print_version(1);
470 exit(0);
471 break;
472 default:
473 /* catch unknown options *as well as* missing arguments. */
474 fprintf(stderr, "Error in command line options. Exiting.\n");
475 exit(-1);
476 break;
477 }
478 }
479}
480
481
Harald Welteb3dae302015-08-30 12:20:09 +0200482int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200483{
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200484 struct osmo_sccp_user *sua_user;
485 struct osmo_sccp_link *sua_link;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100486 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200487 int rc;
488
489 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200490 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200491
Harald Weltec4338de2015-12-24 00:40:52 +0100492 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200493 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100494
Harald Welteb3dae302015-08-30 12:20:09 +0200495 rc = osmo_init_logging(&hnbgw_log_info);
496 if (rc < 0)
497 exit(1);
498
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200499 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200500 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200501
Daniel Willmann56f62732015-12-02 11:22:53 +0100502 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200503
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200504 /* Handle options after vty_init(), for --version */
505 handle_options(argc, argv);
506
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200507 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
508 if (rc < 0) {
509 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
510 hnbgw_cmdline_config.config_file);
511 return 1;
512 }
513
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200514 /*
515 * cmdline options take precedence over config file, but if no options
516 * were passed we must not override the config file.
517 */
518 if (hnbgw_cmdline_config.log_disable_color)
519 log_set_use_color(osmo_stderr_target, 0);
520 if (hnbgw_cmdline_config.log_category_mask)
521 log_parse_category_mask(osmo_stderr_target,
522 hnbgw_cmdline_config.log_category_mask);
523 if (hnbgw_cmdline_config.log_enable_timestamp)
524 log_set_print_timestamp(osmo_stderr_target, 1);
525 if (hnbgw_cmdline_config.log_level)
526 log_set_log_level(osmo_stderr_target,
527 hnbgw_cmdline_config.log_level);
528
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100529 LOGP(DMAIN, LOGL_NOTICE, "VTY at %s %d\n",
530 vty_get_bind_addr(), 2323);
531 rc = telnet_init_dynif(NULL, g_hnb_gw, vty_get_bind_addr(), 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200532 if (rc < 0) {
533 perror("Error binding VTY port");
534 exit(1);
535 }
536
Harald Welte75a4e652015-12-22 23:59:24 +0100537 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100538 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100539
Daniel Willmann4deab942016-01-14 15:35:11 +0100540 g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT, 0);
541 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 +0100542
Daniel Willmann6480cad2016-01-06 18:06:26 +0100543 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
544 if (!srv) {
545 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200546 exit(1);
547 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100548 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
549 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
550 osmo_stream_srv_link_set_addr(srv, "0.0.0.0");
551 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_listen_port);
552 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
553
554 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100555 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100556 exit(1);
557 }
558 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200559
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200560 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200561 rc = osmo_daemonize();
562 if (rc < 0) {
563 perror("Error during daemonize");
564 exit(1);
565 }
566 }
567
568 while (1) {
569 rc = osmo_select_main(0);
570 if (rc < 0)
571 exit(3);
572 }
573
574 /* not reached */
575 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200576}