blob: e7dff3f9dc130a0bec36714d2cefdb4b1dfb45ea [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>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020028
Harald Welteb3dae302015-08-30 12:20:09 +020029#include <sys/types.h>
30#include <sys/socket.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020031#include <netinet/in.h>
Harald Welteb3dae302015-08-30 12:20:09 +020032#include <netinet/sctp.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020033#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020034
35#include <osmocom/core/application.h>
36#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020037#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020038#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020039#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020040#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020041#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020042
43#include <osmocom/vty/telnet_interface.h>
44#include <osmocom/vty/logging.h>
Daniel Willmann56f62732015-12-02 11:22:53 +010045#include <osmocom/vty/command.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020046
Harald Welteffa7c0a2015-12-23 00:03:41 +010047#include <osmocom/netif/stream.h>
48
Harald Welte75a4e652015-12-22 23:59:24 +010049#include <osmocom/sigtran/sua.h>
50#include <osmocom/sigtran/protocol/sua.h>
51#include <osmocom/sigtran/sccp_sap.h>
52
Harald Weltea2e6a7a2015-08-29 21:47:39 +020053#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020054#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020055#include "hnbgw_rua.h"
Harald Weltec4338de2015-12-24 00:40:52 +010056#include "hnbgw_cn.h"
Harald Welte90256ba2015-12-23 20:16:36 +010057#include "context_map.h"
Harald Welteb3dae302015-08-30 12:20:09 +020058
59static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020060void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020061
Harald Weltec4338de2015-12-24 00:40:52 +010062static struct hnb_gw *g_hnb_gw;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020063
Harald Weltec4338de2015-12-24 00:40:52 +010064static int listen_fd_cb(struct osmo_fd *fd, unsigned int what);
65
66static struct hnb_gw *hnb_gw_create(void *ctx)
67{
68 struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw);
69
70 gw->config.iuh_listen_port = IUH_DEFAULT_SCTP_PORT;
71
Harald Weltec4338de2015-12-24 00:40:52 +010072 gw->next_ue_ctx_id = 23;
73 INIT_LLIST_HEAD(&gw->hnb_list);
74 INIT_LLIST_HEAD(&gw->ue_list);
75 INIT_LLIST_HEAD(&gw->cn_list);
76
77 context_map_init(gw);
78
79 return gw;
80}
81
82struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +020083{
84 struct ue_context *ue;
85
Harald Weltec4338de2015-12-24 00:40:52 +010086 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +020087 if (ue->context_id == id)
88 return ue;
89 }
90 return NULL;
91
92}
93
Harald Weltec4338de2015-12-24 00:40:52 +010094struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +020095{
96 struct ue_context *ue;
97
Harald Weltec4338de2015-12-24 00:40:52 +010098 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +020099 if (!strcmp(ue->imsi, imsi))
100 return ue;
101 }
102 return NULL;
103}
104
Harald Weltec4338de2015-12-24 00:40:52 +0100105static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200106{
107 uint32_t id;
108
109 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100110 id = gw->next_ue_ctx_id++;
111 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200112
113 return id;
114}
115
116struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
117{
118 struct ue_context *ue;
119
120 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
121 if (!ue)
122 return NULL;
123
124 ue->hnb = hnb;
125 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltec4338de2015-12-24 00:40:52 +0100126 ue->context_id = get_next_ue_ctx_id(hnb->gw);
127 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200128
129 return ue;
130}
131
132void ue_context_free(struct ue_context *ue)
133{
134 llist_del(&ue->list);
135 talloc_free(ue);
136}
Daniel Willmann6480cad2016-01-06 18:06:26 +0100137static int hnb_close_cb(struct osmo_stream_srv *conn)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200138{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100139}
140
141static int hnb_read_cb(struct osmo_stream_srv *conn)
142{
143 struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200144 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200145 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200146 int rc;
147
148 if (!msg)
149 return -ENOMEM;
150
Harald Welte350814a2015-09-10 22:32:15 +0200151 /* we store a reference to the HomeNodeB in the msg->dest for the
152 * benefit of varoius downstream processing functions */
153 msg->dst = hnb;
154
Daniel Willmann6480cad2016-01-06 18:06:26 +0100155 rc = osmo_stream_srv_recv(conn, msg);
156 if (rc == -EAGAIN) {
157 /* Notification received */
158 msgb_free(msg);
159 return 0;
160 } else if (rc < 0) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200161 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200162 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100163 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100164 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100165 } else if (rc == 0) {
Daniel Willmann6480cad2016-01-06 18:06:26 +0100166 hnb_context_release(hnb);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100167 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100168
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100169 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100170 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200171 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100172 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200173
Daniel Willmann6480cad2016-01-06 18:06:26 +0100174 switch (msgb_sctp_ppid(msg)) {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200175 case IUH_PPI_HNBAP:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100176 hnb->hnbap_stream = msgb_sctp_stream(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200177 rc = hnbgw_hnbap_rx(hnb, msg);
178 break;
179 case IUH_PPI_RUA:
Daniel Willmann6480cad2016-01-06 18:06:26 +0100180 hnb->rua_stream = msgb_sctp_stream(msg);
Harald Welte318e4d52015-09-10 18:47:08 +0200181 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200182 break;
183 case IUH_PPI_SABP:
184 case IUH_PPI_RNA:
185 case IUH_PPI_PUA:
186 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100187 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200188 rc = 0;
189 break;
190 default:
191 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
Daniel Willmann6480cad2016-01-06 18:06:26 +0100192 msgb_sctp_ppid(msg));
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193 rc = 0;
194 break;
195 }
196
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100197out:
Harald Weltef2f30002015-09-08 00:09:23 +0200198 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200199 return rc;
200}
201
Harald Welte3f712562015-09-07 21:53:25 +0200202static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
203{
204 struct hnb_context *ctx = fd->data;
205 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100206 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200207 .sinfo_stream = ctx->hnbap_stream,
208 };
209 int rc;
210
211 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
212 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200213 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200214 return rc;
215}
216
Daniel Willmann6480cad2016-01-06 18:06:26 +0100217struct 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 +0100218{
219 struct hnb_context *ctx;
220
221 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
222 if (!ctx)
223 return NULL;
224
225 ctx->gw = gw;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100226 ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx);
227 if (!ctx->conn) {
228 LOGP(DMAIN, LOGL_INFO, "error while creating connection\n");
229 return -1;
230 }
231
Harald Weltec4338de2015-12-24 00:40:52 +0100232 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100233
234 llist_add_tail(&ctx->list, &gw->hnb_list);
235}
236
237void hnb_context_release(struct hnb_context *ctx)
238{
239 struct hnbgw_context_map *map, *map2;
240
241 /* remove from the list of HNB contexts */
242 llist_del(&ctx->list);
243
244 /* deactivate all context maps */
245 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
246 /* remove it from list, as HNB context will soon be
247 * gone. Let's hope the seccond osmo_llist_del in the
248 * map garbage collector wors fine? */
249 llist_del(&map->hnb_list);
250 context_map_deactivate(map);
251 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100252 osmo_stream_srv_destroy(ctx->conn);
Harald Welte90256ba2015-12-23 20:16:36 +0100253
254 talloc_free(ctx);
255}
256
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200257/*! call-back when the listen FD has something to read */
Daniel Willmann6480cad2016-01-06 18:06:26 +0100258static int accept_cb(struct osmo_stream_srv_link *srv, int fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200259{
Daniel Willmann6480cad2016-01-06 18:06:26 +0100260 struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv);
Harald Welteb3dae302015-08-30 12:20:09 +0200261 struct hnb_context *ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200262
Daniel Willmann6480cad2016-01-06 18:06:26 +0100263 ctx = hnb_context_alloc(gw, srv, fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200264 if (!ctx)
265 return -ENOMEM;
266
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200267 return 0;
268}
269
Harald Welte75a4e652015-12-22 23:59:24 +0100270/* Entry point for primitives coming up from SCCP User SAP */
271static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
272{
273 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
274
275 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
276
277 /* FIXME: Do something */
278
279 msgb_free(oph->msg);
280
281 return 0;
282}
283
Harald Welteb3dae302015-08-30 12:20:09 +0200284static const struct log_info_cat log_cat[] = {
285 [DMAIN] = {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100286 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200287 .color = "",
288 .description = "Main program",
289 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100290 [DHNBAP] = {
291 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
292 .color = "",
293 .description = "Home Node B Application Part",
294 },
Harald Welte75a4e652015-12-22 23:59:24 +0100295 [DSUA] = {
296 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
297 .color = "",
298 .description = "SCCP User Adaptation",
299 },
Harald Weltef42317b2015-12-23 15:36:31 +0100300 [DRUA] = {
301 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
302 .color = "",
303 .description = "RANAP User Adaptation",
304 },
305 [DRANAP] = {
306 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
307 .color = "",
308 .description = "RAN Application Part",
309 },
Harald Welteb3dae302015-08-30 12:20:09 +0200310};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200311
Harald Welteb3dae302015-08-30 12:20:09 +0200312static const struct log_info hnbgw_log_info = {
313 .cat = log_cat,
314 .num_cat = ARRAY_SIZE(log_cat),
315};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200316
Harald Welteb3dae302015-08-30 12:20:09 +0200317static struct vty_app_info vty_info = {
318 .name = "OsmoHNBGW",
319 .version = "0",
320};
321
322static int daemonize = 0;
323
Daniel Willmann56f62732015-12-02 11:22:53 +0100324static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
325{
326 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
327 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
328 VTY_NEWLINE);
329 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
330}
331
332static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
333{
334 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
335}
336
337DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
338{
339 struct hnb_context *hnb;
340
Harald Weltec4338de2015-12-24 00:40:52 +0100341 llist_for_each_entry(hnb, &g_hnb_gw->hnb_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100342 vty_dump_hnb_info(vty, hnb);
343 }
344
345 return CMD_SUCCESS;
346}
347
348DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
349{
350 struct ue_context *ue;
351
Harald Weltec4338de2015-12-24 00:40:52 +0100352 llist_for_each_entry(ue, &g_hnb_gw->ue_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100353 vty_dump_ue_info(vty, ue);
354 }
355
356 return CMD_SUCCESS;
357}
358
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100359DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
360{
361 talloc_report_full(tall_hnb_ctx, stderr);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100362 talloc_report_full(talloc_asn1_ctx, stderr);
363
364 return CMD_SUCCESS;
365}
366
Daniel Willmann56f62732015-12-02 11:22:53 +0100367static void hnbgw_vty_init(void)
368{
369 install_element_ve(&show_hnb_cmd);
370 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100371 install_element_ve(&show_talloc_cmd);
Daniel Willmann56f62732015-12-02 11:22:53 +0100372}
373
Harald Welteb3dae302015-08-30 12:20:09 +0200374int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200375{
Harald Welte75a4e652015-12-22 23:59:24 +0100376 struct osmo_sua_user *sua_user;
377 struct osmo_sua_link *sua_link;
Daniel Willmann6480cad2016-01-06 18:06:26 +0100378 struct osmo_stream_srv_link *srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200379 int rc;
380
381 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200382 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200383
Harald Weltec4338de2015-12-24 00:40:52 +0100384 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Harald Welte90256ba2015-12-23 20:16:36 +0100385
Harald Welteb3dae302015-08-30 12:20:09 +0200386 rc = osmo_init_logging(&hnbgw_log_info);
387 if (rc < 0)
388 exit(1);
389
390 vty_init(&vty_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100391 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200392
Harald Weltec4338de2015-12-24 00:40:52 +0100393 rc = telnet_init(NULL, g_hnb_gw, 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200394 if (rc < 0) {
395 perror("Error binding VTY port");
396 exit(1);
397 }
398
Harald Welte75a4e652015-12-22 23:59:24 +0100399 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100400 ranap_set_log_area(DRANAP);
Harald Welte75a4e652015-12-22 23:59:24 +0100401
Harald Weltec4338de2015-12-24 00:40:52 +0100402 g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT);
403 g_hnb_gw->cnlink_ps = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.2", SUA_PORT);
404
Daniel Willmann6480cad2016-01-06 18:06:26 +0100405 srv = osmo_stream_srv_link_create(tall_hnb_ctx);
406 if (!srv) {
407 perror("cannot create server");
Harald Welteb3dae302015-08-30 12:20:09 +0200408 exit(1);
409 }
Daniel Willmann6480cad2016-01-06 18:06:26 +0100410 osmo_stream_srv_link_set_data(srv, g_hnb_gw);
411 osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP);
412 osmo_stream_srv_link_set_addr(srv, "0.0.0.0");
413 osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_listen_port);
414 osmo_stream_srv_link_set_accept_cb(srv, accept_cb);
415
416 if (osmo_stream_srv_link_open(srv) < 0) {
417 perror("Cannot oper server");
418 exit(1);
419 }
420 g_hnb_gw->iuh = srv;
Harald Welteb3dae302015-08-30 12:20:09 +0200421
422 if (daemonize) {
423 rc = osmo_daemonize();
424 if (rc < 0) {
425 perror("Error during daemonize");
426 exit(1);
427 }
428 }
429
430 while (1) {
431 rc = osmo_select_main(0);
432 if (rc < 0)
433 exit(3);
434 }
435
436 /* not reached */
437 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200438}