blob: 2c49c4dda9f85b2ddff4be65135f2e46f4aa11cd [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>
Harald Welte3f712562015-09-07 21:53:25 +020019#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020020
21#include <osmocom/vty/telnet_interface.h>
22#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020023
24#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020025#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020026#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020027
28static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020029void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020030
31struct hnb_gw g_hnb_gw = {
32 .config = {
33 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
34 },
35};
36
Harald Welte3f712562015-09-07 21:53:25 +020037static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +020038{
39 struct hnb_context *hnb = fd->data;
40 struct sctp_sndrcvinfo sinfo;
41 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +020042 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020043 int rc;
44
45 if (!msg)
46 return -ENOMEM;
47
Harald Welte350814a2015-09-10 22:32:15 +020048 /* we store a reference to the HomeNodeB in the msg->dest for the
49 * benefit of varoius downstream processing functions */
50 msg->dst = hnb;
51
Harald Weltea2e6a7a2015-08-29 21:47:39 +020052 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
53 NULL, NULL, &sinfo, &flags);
54 if (rc < 0) {
55 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +020056 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +020057 return rc;
58 } else
59 msgb_put(msg, rc);
60
Harald Welte17878e22015-09-08 00:09:13 +020061 if (flags & MSG_NOTIFICATION) {
62 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
63 msgb_free(msg);
64 return 0;
65 }
66
Harald Welte5c11c942015-09-07 19:54:49 +020067 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
68
Harald Weltea2e6a7a2015-08-29 21:47:39 +020069 switch (sinfo.sinfo_ppid) {
70 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +020071 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020072 rc = hnbgw_hnbap_rx(hnb, msg);
73 break;
74 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +020075 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +020076 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020077 break;
78 case IUH_PPI_SABP:
79 case IUH_PPI_RNA:
80 case IUH_PPI_PUA:
81 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
82 sinfo.sinfo_ppid);
83 rc = 0;
84 break;
85 default:
86 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
87 sinfo.sinfo_ppid);
88 rc = 0;
89 break;
90 }
91
Harald Weltef2f30002015-09-08 00:09:23 +020092 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020093 return rc;
94}
95
Harald Welte3f712562015-09-07 21:53:25 +020096static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
97{
98 struct hnb_context *ctx = fd->data;
99 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200100 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200101 .sinfo_stream = ctx->hnbap_stream,
102 };
103 int rc;
104
105 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
106 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200107 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200108 return rc;
109}
110
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200111/*! call-back when the listen FD has something to read */
112static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
113{
114 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200115 struct hnb_context *ctx;
116 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200117 socklen_t len = sizeof(sockaddr);
118
119 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
120 if (new_fd < 0) {
121 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
122 return new_fd;
123 }
124
125 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
126
127 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
128 if (!ctx)
129 return -ENOMEM;
130
131 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200132 osmo_wqueue_init(&ctx->wqueue, 16);
133 ctx->wqueue.bfd.data = ctx;
134 ctx->wqueue.bfd.fd = new_fd;
135 ctx->wqueue.bfd.when = BSC_FD_READ;
136 ctx->wqueue.read_cb = hnb_read_cb;
137 ctx->wqueue.write_cb = hnb_write_cb;
138 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200139
140 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200141
142 return 0;
143}
144
Harald Welteb3dae302015-08-30 12:20:09 +0200145static const struct log_info_cat log_cat[] = {
146 [DMAIN] = {
147 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
148 .color = "",
149 .description = "Main program",
150 },
151};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200152
Harald Welteb3dae302015-08-30 12:20:09 +0200153static const struct log_info hnbgw_log_info = {
154 .cat = log_cat,
155 .num_cat = ARRAY_SIZE(log_cat),
156};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200157
Harald Welteb3dae302015-08-30 12:20:09 +0200158static struct vty_app_info vty_info = {
159 .name = "OsmoHNBGW",
160 .version = "0",
161};
162
163static int daemonize = 0;
164
Harald Welte5c11c942015-09-07 19:54:49 +0200165static int sctp_sock_init(int fd)
166{
167 struct sctp_event_subscribe event;
168 int rc;
169
170 /* subscribe for all events */
171 memset((uint8_t *)&event, 1, sizeof(event));
172 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
173 &event, sizeof(event));
174
175 return rc;
176}
177
Harald Welteb3dae302015-08-30 12:20:09 +0200178int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200179{
Harald Welteb3dae302015-08-30 12:20:09 +0200180 int rc;
181
182 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200183 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200184
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200185 g_hnb_gw.listen_fd.cb = listen_fd_cb;
186 g_hnb_gw.listen_fd.when = BSC_FD_READ;
187 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200188 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200189
Harald Welteb3dae302015-08-30 12:20:09 +0200190 rc = osmo_init_logging(&hnbgw_log_info);
191 if (rc < 0)
192 exit(1);
193
194 vty_init(&vty_info);
195
196 rc = telnet_init(NULL, &g_hnb_gw, 2323);
197 if (rc < 0) {
198 perror("Error binding VTY port");
199 exit(1);
200 }
201
202 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200203 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200204 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200205 if (rc < 0) {
206 perror("Error binding Iuh port");
207 exit(1);
208 }
Harald Welte5c11c942015-09-07 19:54:49 +0200209 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200210
211 if (daemonize) {
212 rc = osmo_daemonize();
213 if (rc < 0) {
214 perror("Error during daemonize");
215 exit(1);
216 }
217 }
218
219 while (1) {
220 rc = osmo_select_main(0);
221 if (rc < 0)
222 exit(3);
223 }
224
225 /* not reached */
226 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200227}