2015-08-30 10:20:09 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/sctp.h>
|
2015-09-10 23:34:45 +00:00
|
|
|
//#include <arpa/inet.h>
|
2015-08-30 10:20:09 +00:00
|
|
|
|
|
|
|
#include <osmocom/core/application.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
2015-08-29 19:47:39 +00:00
|
|
|
#include <osmocom/core/select.h>
|
2015-08-30 10:20:09 +00:00
|
|
|
#include <osmocom/core/logging.h>
|
2015-08-29 19:47:39 +00:00
|
|
|
#include <osmocom/core/socket.h>
|
2015-08-30 10:20:09 +00:00
|
|
|
#include <osmocom/core/msgb.h>
|
2015-09-07 19:53:25 +00:00
|
|
|
#include <osmocom/core/write_queue.h>
|
2015-08-30 10:20:09 +00:00
|
|
|
|
|
|
|
#include <osmocom/vty/telnet_interface.h>
|
|
|
|
#include <osmocom/vty/logging.h>
|
2015-08-29 19:47:39 +00:00
|
|
|
|
|
|
|
#include "hnbgw.h"
|
2015-08-30 10:20:09 +00:00
|
|
|
#include "hnbgw_hnbap.h"
|
2015-09-10 16:47:08 +00:00
|
|
|
#include "hnbgw_rua.h"
|
2015-08-30 10:20:09 +00:00
|
|
|
|
|
|
|
static void *tall_hnb_ctx;
|
2015-09-10 22:15:16 +00:00
|
|
|
static void *tall_ue_ctx;
|
2015-08-31 07:54:45 +00:00
|
|
|
void *talloc_asn1_ctx;
|
2015-08-29 19:47:39 +00:00
|
|
|
|
|
|
|
struct hnb_gw g_hnb_gw = {
|
|
|
|
.config = {
|
|
|
|
.iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-09-10 22:15:16 +00:00
|
|
|
struct ue_context *ue_context_by_id(uint32_t id)
|
|
|
|
{
|
|
|
|
struct ue_context *ue;
|
|
|
|
|
|
|
|
llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
|
|
|
|
if (ue->context_id == id)
|
|
|
|
return ue;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ue_context *ue_context_by_imsi(const char *imsi)
|
|
|
|
{
|
|
|
|
struct ue_context *ue;
|
|
|
|
|
|
|
|
llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
|
|
|
|
if (!strcmp(ue->imsi, imsi))
|
|
|
|
return ue;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_next_ue_ctx_id(void)
|
|
|
|
{
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
do {
|
|
|
|
id = g_hnb_gw.next_ue_ctx_id++;
|
|
|
|
} while (ue_context_by_id(id));
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
|
|
|
|
{
|
|
|
|
struct ue_context *ue;
|
|
|
|
|
|
|
|
ue = talloc_zero(tall_hnb_ctx, struct ue_context);
|
|
|
|
if (!ue)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ue->hnb = hnb;
|
|
|
|
strncpy(ue->imsi, imsi, sizeof(ue->imsi));
|
|
|
|
ue->context_id = get_next_ue_ctx_id();
|
2015-09-10 23:22:29 +00:00
|
|
|
llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
|
2015-09-10 22:15:16 +00:00
|
|
|
|
|
|
|
return ue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ue_context_free(struct ue_context *ue)
|
|
|
|
{
|
|
|
|
llist_del(&ue->list);
|
|
|
|
talloc_free(ue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-07 19:53:25 +00:00
|
|
|
static int hnb_read_cb(struct osmo_fd *fd)
|
2015-08-29 19:47:39 +00:00
|
|
|
{
|
|
|
|
struct hnb_context *hnb = fd->data;
|
|
|
|
struct sctp_sndrcvinfo sinfo;
|
|
|
|
struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
|
2015-09-07 19:53:25 +00:00
|
|
|
int flags = 0;
|
2015-08-29 19:47:39 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-09-10 20:32:15 +00:00
|
|
|
/* we store a reference to the HomeNodeB in the msg->dest for the
|
|
|
|
* benefit of varoius downstream processing functions */
|
|
|
|
msg->dst = hnb;
|
|
|
|
|
2015-08-29 19:47:39 +00:00
|
|
|
rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
|
|
|
|
NULL, NULL, &sinfo, &flags);
|
|
|
|
if (rc < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
|
2015-08-30 14:57:53 +00:00
|
|
|
/* FIXME: clean up after disappeared HNB */
|
2015-08-29 19:47:39 +00:00
|
|
|
return rc;
|
|
|
|
} else
|
|
|
|
msgb_put(msg, rc);
|
|
|
|
|
2015-09-07 22:09:13 +00:00
|
|
|
if (flags & MSG_NOTIFICATION) {
|
|
|
|
LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-07 17:54:49 +00:00
|
|
|
sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
|
|
|
|
|
2015-08-29 19:47:39 +00:00
|
|
|
switch (sinfo.sinfo_ppid) {
|
|
|
|
case IUH_PPI_HNBAP:
|
2015-09-07 19:53:25 +00:00
|
|
|
hnb->hnbap_stream = sinfo.sinfo_stream;
|
2015-08-29 19:47:39 +00:00
|
|
|
rc = hnbgw_hnbap_rx(hnb, msg);
|
|
|
|
break;
|
|
|
|
case IUH_PPI_RUA:
|
2015-09-07 19:53:25 +00:00
|
|
|
hnb->rua_stream = sinfo.sinfo_stream;
|
2015-09-10 16:47:08 +00:00
|
|
|
rc = hnbgw_rua_rx(hnb, msg);
|
2015-08-29 19:47:39 +00:00
|
|
|
break;
|
|
|
|
case IUH_PPI_SABP:
|
|
|
|
case IUH_PPI_RNA:
|
|
|
|
case IUH_PPI_PUA:
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
|
|
|
|
sinfo.sinfo_ppid);
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
|
|
|
|
sinfo.sinfo_ppid);
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-07 22:09:23 +00:00
|
|
|
msgb_free(msg);
|
2015-08-29 19:47:39 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-09-07 19:53:25 +00:00
|
|
|
static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct hnb_context *ctx = fd->data;
|
|
|
|
struct sctp_sndrcvinfo sinfo = {
|
2015-09-07 20:39:56 +00:00
|
|
|
.sinfo_ppid = htonl(msgb_ppid(msg)),
|
2015-09-07 19:53:25 +00:00
|
|
|
.sinfo_stream = ctx->hnbap_stream,
|
|
|
|
};
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
|
|
|
|
&sinfo, 0);
|
2015-09-07 20:41:26 +00:00
|
|
|
/* we don't need to msgb_free(), write_queue does this for us */
|
2015-09-07 19:53:25 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-29 19:47:39 +00:00
|
|
|
/*! call-back when the listen FD has something to read */
|
|
|
|
static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
|
|
|
|
{
|
|
|
|
struct hnb_gw *gw = fd->data;
|
2015-08-30 10:20:09 +00:00
|
|
|
struct hnb_context *ctx;
|
|
|
|
struct sockaddr_storage sockaddr;
|
2015-08-29 19:47:39 +00:00
|
|
|
socklen_t len = sizeof(sockaddr);
|
|
|
|
|
|
|
|
int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
|
|
|
|
if (new_fd < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
|
|
|
|
return new_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
|
|
|
|
|
|
|
|
ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
|
|
|
|
if (!ctx)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ctx->gw = gw;
|
2015-09-07 19:53:25 +00:00
|
|
|
osmo_wqueue_init(&ctx->wqueue, 16);
|
|
|
|
ctx->wqueue.bfd.data = ctx;
|
|
|
|
ctx->wqueue.bfd.fd = new_fd;
|
|
|
|
ctx->wqueue.bfd.when = BSC_FD_READ;
|
|
|
|
ctx->wqueue.read_cb = hnb_read_cb;
|
|
|
|
ctx->wqueue.write_cb = hnb_write_cb;
|
|
|
|
osmo_fd_register(&ctx->wqueue.bfd);
|
2015-08-30 10:20:09 +00:00
|
|
|
|
|
|
|
llist_add_tail(&ctx->list, &gw->hnb_list);
|
2015-08-29 19:47:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-30 10:20:09 +00:00
|
|
|
static const struct log_info_cat log_cat[] = {
|
|
|
|
[DMAIN] = {
|
|
|
|
.name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
|
|
|
|
.color = "",
|
|
|
|
.description = "Main program",
|
|
|
|
},
|
|
|
|
};
|
2015-08-29 19:47:39 +00:00
|
|
|
|
2015-08-30 10:20:09 +00:00
|
|
|
static const struct log_info hnbgw_log_info = {
|
|
|
|
.cat = log_cat,
|
|
|
|
.num_cat = ARRAY_SIZE(log_cat),
|
|
|
|
};
|
2015-08-29 19:47:39 +00:00
|
|
|
|
2015-08-30 10:20:09 +00:00
|
|
|
static struct vty_app_info vty_info = {
|
|
|
|
.name = "OsmoHNBGW",
|
|
|
|
.version = "0",
|
|
|
|
};
|
|
|
|
|
|
|
|
static int daemonize = 0;
|
|
|
|
|
2015-09-07 17:54:49 +00:00
|
|
|
static int sctp_sock_init(int fd)
|
|
|
|
{
|
|
|
|
struct sctp_event_subscribe event;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* subscribe for all events */
|
|
|
|
memset((uint8_t *)&event, 1, sizeof(event));
|
|
|
|
rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
|
|
|
|
&event, sizeof(event));
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-30 10:20:09 +00:00
|
|
|
int main(int argc, char **argv)
|
2015-08-29 19:47:39 +00:00
|
|
|
{
|
2015-08-30 10:20:09 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
|
2015-09-10 22:15:16 +00:00
|
|
|
tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
|
2015-08-31 07:54:45 +00:00
|
|
|
talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
|
2015-08-30 10:20:09 +00:00
|
|
|
|
2015-08-29 19:47:39 +00:00
|
|
|
g_hnb_gw.listen_fd.cb = listen_fd_cb;
|
|
|
|
g_hnb_gw.listen_fd.when = BSC_FD_READ;
|
|
|
|
g_hnb_gw.listen_fd.data = &g_hnb_gw;
|
2015-09-10 23:34:45 +00:00
|
|
|
g_hnb_gw.next_ue_ctx_id = 23;
|
2015-09-07 17:53:46 +00:00
|
|
|
INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
|
2015-09-10 22:15:16 +00:00
|
|
|
INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
|
2015-08-29 19:47:39 +00:00
|
|
|
|
2015-08-30 10:20:09 +00:00
|
|
|
rc = osmo_init_logging(&hnbgw_log_info);
|
|
|
|
if (rc < 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
vty_init(&vty_info);
|
|
|
|
|
|
|
|
rc = telnet_init(NULL, &g_hnb_gw, 2323);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Error binding VTY port");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
|
2015-09-07 16:46:58 +00:00
|
|
|
IPPROTO_SCTP, NULL,
|
2015-08-29 19:47:39 +00:00
|
|
|
g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
|
2015-08-30 10:20:09 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("Error binding Iuh port");
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-09-07 17:54:49 +00:00
|
|
|
sctp_sock_init(g_hnb_gw.listen_fd.fd);
|
2015-08-30 10:20:09 +00:00
|
|
|
|
|
|
|
if (daemonize) {
|
|
|
|
rc = osmo_daemonize();
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Error during daemonize");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rc = osmo_select_main(0);
|
|
|
|
if (rc < 0)
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not reached */
|
|
|
|
exit(0);
|
2015-08-29 19:47:39 +00:00
|
|
|
}
|