blob: ea3e754e5293495b3344e395503404cc6c7bb4a2 [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
72 gw->listen_fd.cb = listen_fd_cb;
73 gw->listen_fd.when = BSC_FD_READ;
74 gw->listen_fd.data = gw;
75 gw->next_ue_ctx_id = 23;
76 INIT_LLIST_HEAD(&gw->hnb_list);
77 INIT_LLIST_HEAD(&gw->ue_list);
78 INIT_LLIST_HEAD(&gw->cn_list);
79
80 context_map_init(gw);
81
82 return gw;
83}
84
85struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id)
Harald Welteb534e5c2015-09-11 00:15:16 +020086{
87 struct ue_context *ue;
88
Harald Weltec4338de2015-12-24 00:40:52 +010089 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +020090 if (ue->context_id == id)
91 return ue;
92 }
93 return NULL;
94
95}
96
Harald Weltec4338de2015-12-24 00:40:52 +010097struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi)
Harald Welteb534e5c2015-09-11 00:15:16 +020098{
99 struct ue_context *ue;
100
Harald Weltec4338de2015-12-24 00:40:52 +0100101 llist_for_each_entry(ue, &gw->ue_list, list) {
Harald Welteb534e5c2015-09-11 00:15:16 +0200102 if (!strcmp(ue->imsi, imsi))
103 return ue;
104 }
105 return NULL;
106}
107
Harald Weltec4338de2015-12-24 00:40:52 +0100108static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw)
Harald Welteb534e5c2015-09-11 00:15:16 +0200109{
110 uint32_t id;
111
112 do {
Harald Weltec4338de2015-12-24 00:40:52 +0100113 id = gw->next_ue_ctx_id++;
114 } while (ue_context_by_id(gw, id));
Harald Welteb534e5c2015-09-11 00:15:16 +0200115
116 return id;
117}
118
119struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
120{
121 struct ue_context *ue;
122
123 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
124 if (!ue)
125 return NULL;
126
127 ue->hnb = hnb;
128 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
Harald Weltec4338de2015-12-24 00:40:52 +0100129 ue->context_id = get_next_ue_ctx_id(hnb->gw);
130 llist_add_tail(&ue->list, &hnb->gw->ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200131
132 return ue;
133}
134
135void ue_context_free(struct ue_context *ue)
136{
137 llist_del(&ue->list);
138 talloc_free(ue);
139}
140
141
142
Harald Welte3f712562015-09-07 21:53:25 +0200143static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200144{
145 struct hnb_context *hnb = fd->data;
146 struct sctp_sndrcvinfo sinfo;
147 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200148 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200149 int rc;
150
151 if (!msg)
152 return -ENOMEM;
153
Harald Welte350814a2015-09-10 22:32:15 +0200154 /* we store a reference to the HomeNodeB in the msg->dest for the
155 * benefit of varoius downstream processing functions */
156 msg->dst = hnb;
157
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200158 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
159 NULL, NULL, &sinfo, &flags);
160 if (rc < 0) {
161 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200162 /* FIXME: clean up after disappeared HNB */
Daniel Willmann4267a292015-12-15 20:29:27 +0100163 close(fd->fd);
164 osmo_fd_unregister(fd);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100165 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100166 } else if (rc == 0) {
167 LOGP(DMAIN, LOGL_ERROR, "Connection to HNB closed\n");
168 /* TODO: Remove all UEs from that connection */
169 close(fd->fd);
170 osmo_fd_unregister(fd);
171 fd->fd = -1;
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100172 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100173
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100174 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100175 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200176 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100177 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200178
Harald Welte17878e22015-09-08 00:09:13 +0200179 if (flags & MSG_NOTIFICATION) {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100180 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Harald Welte17878e22015-09-08 00:09:13 +0200181 msgb_free(msg);
182 return 0;
183 }
184
Harald Welte5c11c942015-09-07 19:54:49 +0200185 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
186
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200187 switch (sinfo.sinfo_ppid) {
188 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200189 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200190 rc = hnbgw_hnbap_rx(hnb, msg);
191 break;
192 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200193 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200194 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200195 break;
196 case IUH_PPI_SABP:
197 case IUH_PPI_RNA:
198 case IUH_PPI_PUA:
199 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
200 sinfo.sinfo_ppid);
201 rc = 0;
202 break;
203 default:
204 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
205 sinfo.sinfo_ppid);
206 rc = 0;
207 break;
208 }
209
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100210out:
Harald Weltef2f30002015-09-08 00:09:23 +0200211 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200212 return rc;
213}
214
Harald Welte3f712562015-09-07 21:53:25 +0200215static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
216{
217 struct hnb_context *ctx = fd->data;
218 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100219 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200220 .sinfo_stream = ctx->hnbap_stream,
221 };
222 int rc;
223
224 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
225 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200226 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200227 return rc;
228}
229
Harald Welte90256ba2015-12-23 20:16:36 +0100230struct hnb_context *hnb_context_alloc(struct hnb_gw *gw, int new_fd)
231{
232 struct hnb_context *ctx;
233
234 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
235 if (!ctx)
236 return NULL;
237
238 ctx->gw = gw;
239 osmo_wqueue_init(&ctx->wqueue, 16);
240 ctx->wqueue.bfd.data = ctx;
241 ctx->wqueue.bfd.fd = new_fd;
242 ctx->wqueue.bfd.when = BSC_FD_READ;
243 ctx->wqueue.read_cb = hnb_read_cb;
244 ctx->wqueue.write_cb = hnb_write_cb;
245 osmo_fd_register(&ctx->wqueue.bfd);
Harald Weltec4338de2015-12-24 00:40:52 +0100246 INIT_LLIST_HEAD(&ctx->map_list);
Harald Welte90256ba2015-12-23 20:16:36 +0100247
248 llist_add_tail(&ctx->list, &gw->hnb_list);
249}
250
251void hnb_context_release(struct hnb_context *ctx)
252{
253 struct hnbgw_context_map *map, *map2;
254
255 /* remove from the list of HNB contexts */
256 llist_del(&ctx->list);
257
258 /* deactivate all context maps */
259 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
260 /* remove it from list, as HNB context will soon be
261 * gone. Let's hope the seccond osmo_llist_del in the
262 * map garbage collector wors fine? */
263 llist_del(&map->hnb_list);
264 context_map_deactivate(map);
265 }
266 /* FIXME: flush write queue items */
267 osmo_fd_unregister(&ctx->wqueue.bfd);
268
269 talloc_free(ctx);
270}
271
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200272/*! call-back when the listen FD has something to read */
273static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
274{
275 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200276 struct hnb_context *ctx;
277 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200278 socklen_t len = sizeof(sockaddr);
279
280 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
281 if (new_fd < 0) {
282 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
283 return new_fd;
284 }
285
286 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
287
Harald Welte90256ba2015-12-23 20:16:36 +0100288 ctx = hnb_context_alloc(gw, new_fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200289 if (!ctx)
290 return -ENOMEM;
291
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200292 return 0;
293}
294
Harald Welte75a4e652015-12-22 23:59:24 +0100295/* Entry point for primitives coming up from SCCP User SAP */
296static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
297{
298 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
299
300 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
301
302 /* FIXME: Do something */
303
304 msgb_free(oph->msg);
305
306 return 0;
307}
308
Harald Welteb3dae302015-08-30 12:20:09 +0200309static const struct log_info_cat log_cat[] = {
310 [DMAIN] = {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100311 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200312 .color = "",
313 .description = "Main program",
314 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100315 [DHNBAP] = {
316 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
317 .color = "",
318 .description = "Home Node B Application Part",
319 },
Harald Welte75a4e652015-12-22 23:59:24 +0100320 [DSUA] = {
321 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
322 .color = "",
323 .description = "SCCP User Adaptation",
324 },
Harald Weltef42317b2015-12-23 15:36:31 +0100325 [DRUA] = {
326 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
327 .color = "",
328 .description = "RANAP User Adaptation",
329 },
330 [DRANAP] = {
331 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
332 .color = "",
333 .description = "RAN Application Part",
334 },
Harald Welteb3dae302015-08-30 12:20:09 +0200335};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200336
Harald Welteb3dae302015-08-30 12:20:09 +0200337static const struct log_info hnbgw_log_info = {
338 .cat = log_cat,
339 .num_cat = ARRAY_SIZE(log_cat),
340};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200341
Harald Welteb3dae302015-08-30 12:20:09 +0200342static struct vty_app_info vty_info = {
343 .name = "OsmoHNBGW",
344 .version = "0",
345};
346
347static int daemonize = 0;
348
Harald Welte5c11c942015-09-07 19:54:49 +0200349static int sctp_sock_init(int fd)
350{
351 struct sctp_event_subscribe event;
352 int rc;
353
354 /* subscribe for all events */
355 memset((uint8_t *)&event, 1, sizeof(event));
356 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
357 &event, sizeof(event));
358
359 return rc;
360}
361
Daniel Willmann56f62732015-12-02 11:22:53 +0100362static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
363{
364 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
365 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
366 VTY_NEWLINE);
367 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
368}
369
370static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
371{
372 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
373}
374
375DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
376{
377 struct hnb_context *hnb;
378
Harald Weltec4338de2015-12-24 00:40:52 +0100379 llist_for_each_entry(hnb, &g_hnb_gw->hnb_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100380 vty_dump_hnb_info(vty, hnb);
381 }
382
383 return CMD_SUCCESS;
384}
385
386DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
387{
388 struct ue_context *ue;
389
Harald Weltec4338de2015-12-24 00:40:52 +0100390 llist_for_each_entry(ue, &g_hnb_gw->ue_list, list) {
Daniel Willmann56f62732015-12-02 11:22:53 +0100391 vty_dump_ue_info(vty, ue);
392 }
393
394 return CMD_SUCCESS;
395}
396
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100397DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
398{
399 talloc_report_full(tall_hnb_ctx, stderr);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100400 talloc_report_full(talloc_asn1_ctx, stderr);
401
402 return CMD_SUCCESS;
403}
404
Daniel Willmann56f62732015-12-02 11:22:53 +0100405static void hnbgw_vty_init(void)
406{
407 install_element_ve(&show_hnb_cmd);
408 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100409 install_element_ve(&show_talloc_cmd);
Daniel Willmann56f62732015-12-02 11:22:53 +0100410}
411
Harald Welteb3dae302015-08-30 12:20:09 +0200412int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200413{
Harald Welte75a4e652015-12-22 23:59:24 +0100414 struct osmo_sua_user *sua_user;
415 struct osmo_sua_link *sua_link;
Harald Welteb3dae302015-08-30 12:20:09 +0200416 int rc;
417
418 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200419 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200420
Harald Weltec4338de2015-12-24 00:40:52 +0100421 g_hnb_gw = hnb_gw_create(tall_hnb_ctx);
Harald Welte90256ba2015-12-23 20:16:36 +0100422
Harald Welteb3dae302015-08-30 12:20:09 +0200423 rc = osmo_init_logging(&hnbgw_log_info);
424 if (rc < 0)
425 exit(1);
426
427 vty_init(&vty_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100428 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200429
Harald Weltec4338de2015-12-24 00:40:52 +0100430 rc = telnet_init(NULL, g_hnb_gw, 2323);
Harald Welteb3dae302015-08-30 12:20:09 +0200431 if (rc < 0) {
432 perror("Error binding VTY port");
433 exit(1);
434 }
435
Harald Welte75a4e652015-12-22 23:59:24 +0100436 osmo_sua_set_log_area(DSUA);
Harald Welte75a4e652015-12-22 23:59:24 +0100437
Harald Weltec4338de2015-12-24 00:40:52 +0100438 g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT);
439 g_hnb_gw->cnlink_ps = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.2", SUA_PORT);
440
441 rc = osmo_sock_init_ofd(&g_hnb_gw->listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200442 IPPROTO_SCTP, NULL,
Harald Weltec4338de2015-12-24 00:40:52 +0100443 g_hnb_gw->config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200444 if (rc < 0) {
445 perror("Error binding Iuh port");
446 exit(1);
447 }
Harald Weltec4338de2015-12-24 00:40:52 +0100448 sctp_sock_init(g_hnb_gw->listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200449
450 if (daemonize) {
451 rc = osmo_daemonize();
452 if (rc < 0) {
453 perror("Error during daemonize");
454 exit(1);
455 }
456 }
457
458 while (1) {
459 rc = osmo_select_main(0);
460 if (rc < 0)
461 exit(3);
462 }
463
464 /* not reached */
465 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200466}