blob: 5d9f87bbd50a8fbcde82d1f451fc3ba3d22441ad [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
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020050#include <osmocom/sigtran/protocol/m3ua.h>
Harald Welte75a4e652015-12-22 23:59:24 +010051#include <osmocom/sigtran/sccp_sap.h>
52
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020053#include <osmocom/iuh/hnbgw.h>
54#include <osmocom/iuh/hnbgw_hnbap.h>
55#include <osmocom/iuh/hnbgw_rua.h>
56#include <osmocom/iuh/hnbgw_cn.h>
57#include <osmocom/iuh/context_map.h>
Harald Welteb3dae302015-08-30 12:20:09 +020058
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +020059static const char * const osmo_hnbgw_copyright =
60 "OsmoHNBGW - Osmocom Home Node B Gateway implementation\r\n"
61 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
62 "Contributions by Daniel Willmann, Harald Welte, Neels Hofmeyr\r\n"
63 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
64 "This is free software: you are free to change and redistribute it.\r\n"
65 "There is NO WARRANTY, to the extent permitted by law.\r\n";
66
Harald Welteb3dae302015-08-30 12:20:09 +020067static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020068void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020069
Harald Weltec4338de2015-12-24 00:40:52 +010070static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020071
Harald Weltec4338de2015-12-24 00:40:52 +010072static int listen_fd_cb(struct osmo_fd *fd, unsigned int what);
73
74static struct hnb_gw *hnb_gw_create(void *ctx)
75{
76 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
77
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +020078 /* strdup so we can easily talloc_free in the VTY code */
79 gw->config.iuh_local_ip = talloc_strdup(gw, HNBGW_LOCAL_IP_DEFAULT);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +020080 gw->config.iuh_local_port = IUH_DEFAULT_SCTP_PORT;
Harald Weltec4338de2015-12-24 00:40:52 +010081
Neels Hofmeyr5ee050c2016-10-13 15:12:18 +020082 gw->config.iucs_remote_ip = talloc_strdup(gw,
83 HNBGW_IUCS_REMOTE_IP_DEFAULT);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020084 gw->config.iucs_remote_port = M3UA_PORT;
Neels Hofmeyr5ee050c2016-10-13 15:12:18 +020085
86 gw->config.iups_remote_ip = talloc_strdup(gw,
87 HNBGW_IUPS_REMOTE_IP_DEFAULT);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020088 gw->config.iups_remote_port = M3UA_PORT;
Neels Hofmeyr5ee050c2016-10-13 15:12:18 +020089
Harald Weltec4338de2015-12-24 00:40:52 +010090 gw->next_ue_ctx_id = 23;
91 INIT_LLIST_HEAD(&gw->hnb_list);
92 INIT_LLIST_HEAD(&gw->ue_list);
Harald Weltec4338de2015-12-24 00:40:52 +010093
94 context_map_init(gw);
95
96 return gw;
97}
98
99struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +0200100{
101 struct ue_context *ue;
102
Harald Weltec4338de2015-12-24 00:40:52 +0100103 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200104 if (ue->context_id == id)
105 return ue;
106 }
107 return NULL;
108
109}
110
Harald Weltec4338de2015-12-24 00:40:52 +0100111struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200112{
113 struct ue_context *ue;
114
Harald Weltec4338de2015-12-24 00:40:52 +0100115 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200116 if (!strcmp(ue->imsi, imsi))
117 return ue;
118 }
119 return NULL;
120}
121
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200122struct ue_context *ue_context_by_tmsi(struct hnb_gw *gw, uint32_t tmsi)
123{
124 struct ue_context *ue;
125
126 llist_for_each_entry(ue, &gw->ue_list, list) {
127 if (ue->tmsi == tmsi)
128 return ue;
129 }
130 return NULL;
131}
132
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100133void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb)
134{
135 struct ue_context *ue, *tmp;
136
137 llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) {
138 if (ue->hnb == hnb)
139 ue_context_free(ue);
140 }
141}
142
Harald Weltec4338de2015-12-24 00:40:52 +0100143static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200144{
145 uint32_t id;
146
147 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100148 id = gw->next_ue_ctx_id++;
149 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200150
151 return id;
152}
153
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200154struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi,
155 uint32_t tmsi)
Harald Welteb534e5c2015-09-11 00:15:16 +0200156{
157 struct ue_context *ue;
158
159 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
160 if (!ue)
161 return NULL;
162
163 ue->hnb = hnb;
Harald Weltee08cdd92016-11-26 00:01:53 +0100164 if (imsi) {
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200165 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltee08cdd92016-11-26 00:01:53 +0100166 ue->imsi[sizeof(ue->imsi)-1] = '\0';
167 } else
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200168 ue->imsi[0] = '\0';
169 ue->tmsi = tmsi;
Harald Weltec4338de2015-12-24 00:40:52 +0100170 ue->context_id = get_next_ue_ctx_id(hnb->gw);
171 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200172
Neels Hofmeyr0476e282016-09-22 19:37:29 +0200173 LOGP(DHNBAP, LOGL_INFO, "created UE context: id 0x%x, imsi %s, tmsi 0x%x\n",
174 ue->context_id, imsi? imsi : "-", tmsi);
175
Harald Welteb534e5c2015-09-11 00:15:16 +0200176 return ue;
177}
178
179void ue_context_free(struct ue_context *ue)
180{
181 llist_del(&ue->list);
182 talloc_free(ue);
183}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100184static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200185{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100186}
187
188static int hnb_read_cb(struct osmo_stream_srv *conn)
189{
190 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200191 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200192 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193 int rc;
194
195 if (!msg)
196 return -ENOMEM;
197
Harald Welte350814a2015-09-10 22:32:15 +0200198 /* we store a reference to the HomeNodeB in the msg->dest for the
199 * benefit of varoius downstream processing functions */
200 msg->dst = hnb;
201
Daniel Willmann6480cad2016-01-06 18:06:26 +0100202 rc = osmo_stream_srv_recv(conn, msg);
203 if (rc == -EAGAIN) {
204 /* Notification received */
205 msgb_free(msg);
206 return 0;
207 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200208 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200209 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100210 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100211 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100212 } else if (rc == 0) {
Daniel Willmann6480cad2016-01-06 18:06:26 +0100213 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100214 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100215
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100216 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100217 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200218 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100219 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200220
Daniel Willmann6480cad2016-01-06 18:06:26 +0100221 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200222 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100223 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200224 rc = hnbgw_hnbap_rx(hnb, msg);
225 break;
226 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100227 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200228 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200229 break;
230 case IUH_PPI_SABP:
231 case IUH_PPI_RNA:
232 case IUH_PPI_PUA:
233 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100234 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200235 rc = 0;
236 break;
237 default:
238 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100239 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200240 rc = 0;
241 break;
242 }
243
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100244out:
Harald Weltef2f30002015-09-08 00:09:23 +0200245 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200246 return rc;
247}
248
Harald Welte3f712562015-09-07 21:53:25 +0200249static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
250{
251 struct hnb_context *ctx = fd->data;
252 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100253 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200254 .sinfo_stream = ctx->hnbap_stream,
255 };
256 int rc;
257
258 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
259 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200260 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200261 return rc;
262}
263
Daniel Willmann6480cad2016-01-06 18:06:26 +0100264struct 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 +0100265{
266 struct hnb_context *ctx;
267
268 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
269 if (!ctx)
270 return NULL;
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100271 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100272
273 ctx->gw = gw;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100274 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
275 if (!ctx->conn) {
276 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100277 talloc_free(ctx);
278 return NULL;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100279 }
280
Harald Welte90256ba2015-12-23 20:16:36 +0100281 llist_add_tail(&ctx->list, &gw->hnb_list);
Neels Hofmeyr44beab92016-02-18 01:26:59 +0100282 return ctx;
Harald Welte90256ba2015-12-23 20:16:36 +0100283}
284
285void hnb_context_release(struct hnb_context *ctx)
286{
287 struct hnbgw_context_map *map, *map2;
288
289 /* remove from the list of HNB contexts */
290 llist_del(&ctx->list);
291
292 /* deactivate all context maps */
293 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
294 /* remove it from list, as HNB context will soon be
Neels Hofmeyr525a69e2016-04-19 17:54:25 +0200295 * gone. Let's hope the second osmo_llist_del in the
296 * map garbage collector works fine? */
Harald Welte90256ba2015-12-23 20:16:36 +0100297 llist_del(&map->hnb_list);
Neels Hofmeyr0a461562016-04-23 13:53:28 +0200298 llist_del(&map->cn_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100299 context_map_deactivate(map);
300 }
Daniel Willmann1ee089f2016-01-06 18:07:02 +0100301 ue_context_free_by_hnb(ctx->gw, ctx);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100302 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100303
304 talloc_free(ctx);
305}
306
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200307/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100308static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200309{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100310 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200311 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200312
Daniel Willmann6480cad2016-01-06 18:06:26 +0100313 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200314 if (!ctx)
315 return -ENOMEM;
316
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200317 return 0;
318}
319
Harald Welteb3dae302015-08-30 12:20:09 +0200320static const struct log_info_cat log_cat[] = {
321 [DMAIN] = {
Neels Hofmeyr6341f4d2016-04-04 18:02:49 +0200322 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200323 .color = "",
324 .description = "Main program",
325 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100326 [DHNBAP] = {
327 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
328 .color = "",
329 .description = "Home Node B Application Part",
330 },
Harald Weltef42317b2015-12-23 15:36:31 +0100331 [DRUA] = {
332 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
333 .color = "",
334 .description = "RANAP User Adaptation",
335 },
336 [DRANAP] = {
337 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
338 .color = "",
339 .description = "RAN Application Part",
340 },
Harald Welteb3dae302015-08-30 12:20:09 +0200341};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200342
Harald Welteb3dae302015-08-30 12:20:09 +0200343static const struct log_info hnbgw_log_info = {
344 .cat = log_cat,
345 .num_cat = ARRAY_SIZE(log_cat),
346};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200347
Harald Welteb3dae302015-08-30 12:20:09 +0200348static struct vty_app_info vty_info = {
349 .name = "OsmoHNBGW",
350 .version = "0",
Neels Hofmeyrc510fc22016-10-13 16:58:04 +0200351 .go_parent_cb = hnbgw_vty_go_parent,
Harald Welteb3dae302015-08-30 12:20:09 +0200352};
353
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200354static struct {
355 int daemonize;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200356 const char *config_file;
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200357 bool log_disable_color;
358 bool log_enable_timestamp;
359 int log_level;
360 const char *log_category_mask;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200361} hnbgw_cmdline_config = {
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200362 0,
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200363 "osmo-hnbgw.cfg",
364 true,
365 false,
366 0,
367 NULL,
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200368};
369
370static void print_usage()
371{
372 printf("Usage: osmo-hnbgw\n");
373}
374
375static void print_help()
376{
377 printf(" -h --help This text.\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200378 printf(" -d option --debug=DHNBAP:DRUA:DRANAP:DMAIN Enable debugging.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200379 printf(" -D --daemonize Fork the process into a background daemon.\n");
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200380 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200381 printf(" -s --disable-color\n");
382 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
383 printf(" -V --version Print the version of OsmoHNBGW.\n");
384 printf(" -e --log-level number Set a global loglevel.\n");
385}
386
387static void handle_options(int argc, char **argv)
388{
389 while (1) {
390 int option_index = 0, c;
391 static struct option long_options[] = {
392 {"help", 0, 0, 'h'},
393 {"debug", 1, 0, 'd'},
394 {"daemonize", 0, 0, 'D'},
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200395 {"config-file", 1, 0, 'c'},
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200396 {"disable-color", 0, 0, 's'},
397 {"timestamp", 0, 0, 'T'},
398 {"version", 0, 0, 'V' },
399 {"log-level", 1, 0, 'e'},
400 {0, 0, 0, 0}
401 };
402
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200403 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200404 long_options, &option_index);
405 if (c == -1)
406 break;
407
408 switch (c) {
409 case 'h':
410 print_usage();
411 print_help();
412 exit(0);
413 case 's':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200414 hnbgw_cmdline_config.log_disable_color = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200415 break;
416 case 'd':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200417 hnbgw_cmdline_config.log_category_mask = optarg;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200418 break;
419 case 'D':
420 hnbgw_cmdline_config.daemonize = 1;
421 break;
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200422 case 'c':
423 hnbgw_cmdline_config.config_file = optarg;
424 break;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200425 case 'T':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200426 hnbgw_cmdline_config.log_enable_timestamp = true;
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200427 break;
428 case 'e':
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200429 hnbgw_cmdline_config.log_level = atoi(optarg);
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200430 break;
431 case 'V':
432 print_version(1);
433 exit(0);
434 break;
435 default:
436 /* catch unknown options *as well as* missing arguments. */
437 fprintf(stderr, "Error in command line options. Exiting.\n");
438 exit(-1);
439 break;
440 }
441 }
442}
443
444
Harald Welteb3dae302015-08-30 12:20:09 +0200445int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200446{
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200447 struct osmo_sccp_user *sccp_user;
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200448 struct osmo_sccp_link *sua_link;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100449 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200450 int rc;
451
452 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200453 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Neels Hofmeyr4ff1a5a2016-09-16 01:38:46 +0200454 msgb_talloc_ctx_init(tall_hnb_ctx, 0);
Harald Welteb3dae302015-08-30 12:20:09 +0200455
Harald Weltec4338de2015-12-24 00:40:52 +0100456 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Neels Hofmeyra4540be2016-04-05 14:27:48 +0200457 g_hnb_gw->config.rnc_id = 23;
Harald Welte90256ba2015-12-23 20:16:36 +0100458
Harald Welteb3dae302015-08-30 12:20:09 +0200459 rc = osmo_init_logging(&hnbgw_log_info);
460 if (rc < 0)
461 exit(1);
462
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200463 osmo_ss7_init();
464
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200465 vty_info.copyright = osmo_hnbgw_copyright;
Harald Welteb3dae302015-08-30 12:20:09 +0200466 vty_init(&vty_info);
Neels Hofmeyr4c45d2b2016-08-18 02:10:57 +0200467
Neels Hofmeyr4d8eb4c2016-08-18 01:03:44 +0200468 hnbgw_vty_init(g_hnb_gw, tall_hnb_ctx);
469 logging_vty_add_cmds(&hnbgw_log_info);
Harald Welteb3dae302015-08-30 12:20:09 +0200470
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200471 /* Handle options after vty_init(), for --version */
472 handle_options(argc, argv);
473
Neels Hofmeyr085cd672016-08-17 13:54:00 +0200474 rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL);
475 if (rc < 0) {
476 LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
477 hnbgw_cmdline_config.config_file);
478 return 1;
479 }
480
Neels Hofmeyr1d03f192016-08-18 03:15:14 +0200481 /*
482 * cmdline options take precedence over config file, but if no options
483 * were passed we must not override the config file.
484 */
485 if (hnbgw_cmdline_config.log_disable_color)
486 log_set_use_color(osmo_stderr_target, 0);
487 if (hnbgw_cmdline_config.log_category_mask)
488 log_parse_category_mask(osmo_stderr_target,
489 hnbgw_cmdline_config.log_category_mask);
490 if (hnbgw_cmdline_config.log_enable_timestamp)
491 log_set_print_timestamp(osmo_stderr_target, 1);
492 if (hnbgw_cmdline_config.log_level)
493 log_set_log_level(osmo_stderr_target,
494 hnbgw_cmdline_config.log_level);
495
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100496 rc = telnet_init_dynif(NULL, g_hnb_gw, vty_get_bind_addr(), 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200497 if (rc < 0) {
498 perror("Error binding VTY port");
499 exit(1);
500 }
501
Harald Weltebdf3fd12016-01-03 17:04:09 +0100502 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100503
Neels Hofmeyr5ee050c2016-10-13 15:12:18 +0200504 OSMO_ASSERT(g_hnb_gw->config.iucs_remote_ip);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200505 rc = hnbgw_cnlink_init(g_hnb_gw,
506 g_hnb_gw->config.iucs_remote_ip,
507 g_hnb_gw->config.iucs_remote_port,
508 "127.0.0.5" /* FIXME: configurable */,
509 23 /* FIXME: configurable */);
510 if (rc < 0) {
511 LOGP(DMAIN, LOGL_ERROR, "Failed to initialize SCCP link to CN\n");
512 exit(1);
513 }
514
515 osmo_sccp_make_addr_pc_ssn(&g_hnb_gw->sccp.remote_addr_cs,
516 1 /* FIXME: configurable */,
517 OSMO_SCCP_SSN_RANAP);
518 osmo_sccp_make_addr_pc_ssn(&g_hnb_gw->sccp.remote_addr_ps,
519 2 /* FIXME: configurable */,
520 OSMO_SCCP_SSN_RANAP);
Harald Weltec4338de2015-12-24 00:40:52 +0100521
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200522 OSMO_ASSERT(g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrf495b232016-08-18 02:18:00 +0200523 LOGP(DMAIN, LOGL_NOTICE, "Listening for Iuh at %s %d\n",
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200524 g_hnb_gw->config.iuh_local_ip,
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200525 g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100526 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
527 if (!srv) {
528 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200529 exit(1);
530 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100531 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
532 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
Neels Hofmeyr6e3e5942016-10-27 13:13:50 +0200533 osmo_stream_srv_link_set_addr(srv, g_hnb_gw->config.iuh_local_ip);
Neels Hofmeyrc7ccdd42016-10-13 14:43:49 +0200534 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_local_port);
Daniel Willmann6480cad2016-01-06 18:06:26 +0100535 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
536
537 if (osmo_stream_srv_link_open(srv) < 0) {
Neels Hofmeyrde111bc2016-01-13 11:54:40 +0100538 perror("Cannot open server");
Daniel Willmann6480cad2016-01-06 18:06:26 +0100539 exit(1);
540 }
541 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200542
Neels Hofmeyr4d675a72016-08-17 13:53:18 +0200543 if (hnbgw_cmdline_config.daemonize) {
Harald Welteb3dae302015-08-30 12:20:09 +0200544 rc = osmo_daemonize();
545 if (rc < 0) {
546 perror("Error during daemonize");
547 exit(1);
548 }
549 }
550
551 while (1) {
552 rc = osmo_select_main(0);
553 if (rc < 0)
554 exit(3);
555 }
556
557 /* not reached */
558 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200559}