blob: e5c8c7afd96a23ccba1b434579654f772e006e3b [file] [log] [blame]
Harald Welteb3dae302015-08-30 12:20:09 +02001#include <unistd.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <getopt.h>
6#include <errno.h>
7#include <signal.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +02008
Harald Welteb3dae302015-08-30 12:20:09 +02009#include <sys/types.h>
10#include <sys/socket.h>
11#include <netinet/sctp.h>
12
13#include <osmocom/core/application.h>
14#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020015#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020016#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020017#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020018#include <osmocom/core/msgb.h>
19
20#include <osmocom/vty/telnet_interface.h>
21#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020022
23#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020024#include "hnbgw_hnbap.h"
25
26static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020027void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020028
29struct hnb_gw g_hnb_gw = {
30 .config = {
31 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
32 },
33};
34
35static int hnb_socket_cb(struct osmo_fd *fd, unsigned int what)
36{
37 struct hnb_context *hnb = fd->data;
38 struct sctp_sndrcvinfo sinfo;
39 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
40 int flags;
41 int rc;
42
43 if (!msg)
44 return -ENOMEM;
45
46 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
47 NULL, NULL, &sinfo, &flags);
48 if (rc < 0) {
49 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +020050 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +020051 return rc;
52 } else
53 msgb_put(msg, rc);
54
55 switch (sinfo.sinfo_ppid) {
56 case IUH_PPI_HNBAP:
57 rc = hnbgw_hnbap_rx(hnb, msg);
58 break;
59 case IUH_PPI_RUA:
Harald Welteb3dae302015-08-30 12:20:09 +020060 //rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020061 break;
62 case IUH_PPI_SABP:
63 case IUH_PPI_RNA:
64 case IUH_PPI_PUA:
65 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
66 sinfo.sinfo_ppid);
67 rc = 0;
68 break;
69 default:
70 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
71 sinfo.sinfo_ppid);
72 rc = 0;
73 break;
74 }
75
76 return rc;
77}
78
79/*! call-back when the listen FD has something to read */
80static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
81{
82 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +020083 struct hnb_context *ctx;
84 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020085 socklen_t len = sizeof(sockaddr);
86
87 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
88 if (new_fd < 0) {
89 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
90 return new_fd;
91 }
92
93 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
94
95 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
96 if (!ctx)
97 return -ENOMEM;
98
99 ctx->gw = gw;
100 ctx->socket.data = ctx;
101 ctx->socket.fd = new_fd;
102 ctx->socket.when = BSC_FD_READ;
103 ctx->socket.cb = hnb_socket_cb;
Harald Welteb3dae302015-08-30 12:20:09 +0200104 osmo_fd_register(&ctx->socket);
105
106 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200107
108 return 0;
109}
110
Harald Welteb3dae302015-08-30 12:20:09 +0200111static const struct log_info_cat log_cat[] = {
112 [DMAIN] = {
113 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
114 .color = "",
115 .description = "Main program",
116 },
117};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200118
Harald Welteb3dae302015-08-30 12:20:09 +0200119static const struct log_info hnbgw_log_info = {
120 .cat = log_cat,
121 .num_cat = ARRAY_SIZE(log_cat),
122};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200123
Harald Welteb3dae302015-08-30 12:20:09 +0200124static struct vty_app_info vty_info = {
125 .name = "OsmoHNBGW",
126 .version = "0",
127};
128
129static int daemonize = 0;
130
131int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200132{
Harald Welteb3dae302015-08-30 12:20:09 +0200133 int rc;
134
135 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200136 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200137
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200138 g_hnb_gw.listen_fd.cb = listen_fd_cb;
139 g_hnb_gw.listen_fd.when = BSC_FD_READ;
140 g_hnb_gw.listen_fd.data = &g_hnb_gw;
141
Harald Welteb3dae302015-08-30 12:20:09 +0200142 rc = osmo_init_logging(&hnbgw_log_info);
143 if (rc < 0)
144 exit(1);
145
146 vty_init(&vty_info);
147
148 rc = telnet_init(NULL, &g_hnb_gw, 2323);
149 if (rc < 0) {
150 perror("Error binding VTY port");
151 exit(1);
152 }
153
154 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200155 IPPROTO_SCTP, "127.0.0.1",
156 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200157 if (rc < 0) {
158 perror("Error binding Iuh port");
159 exit(1);
160 }
161
162 if (daemonize) {
163 rc = osmo_daemonize();
164 if (rc < 0) {
165 perror("Error during daemonize");
166 exit(1);
167 }
168 }
169
170 while (1) {
171 rc = osmo_select_main(0);
172 if (rc < 0)
173 exit(3);
174 }
175
176 /* not reached */
177 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200178}