blob: 0875524092a7920fdc099044ce436a8a2c6567c0 [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"
26
27static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020028void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020029
30struct hnb_gw g_hnb_gw = {
31 .config = {
32 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
33 },
34};
35
Harald Welte3f712562015-09-07 21:53:25 +020036static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +020037{
38 struct hnb_context *hnb = fd->data;
39 struct sctp_sndrcvinfo sinfo;
40 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +020041 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020042 int rc;
43
44 if (!msg)
45 return -ENOMEM;
46
47 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
48 NULL, NULL, &sinfo, &flags);
49 if (rc < 0) {
50 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +020051 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +020052 return rc;
53 } else
54 msgb_put(msg, rc);
55
Harald Welte5c11c942015-09-07 19:54:49 +020056 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
57
Harald Weltea2e6a7a2015-08-29 21:47:39 +020058 switch (sinfo.sinfo_ppid) {
59 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +020060 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020061 rc = hnbgw_hnbap_rx(hnb, msg);
62 break;
63 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +020064 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welteb3dae302015-08-30 12:20:09 +020065 //rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020066 break;
67 case IUH_PPI_SABP:
68 case IUH_PPI_RNA:
69 case IUH_PPI_PUA:
70 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
71 sinfo.sinfo_ppid);
72 rc = 0;
73 break;
74 default:
75 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
76 sinfo.sinfo_ppid);
77 rc = 0;
78 break;
79 }
80
81 return rc;
82}
83
Harald Welte3f712562015-09-07 21:53:25 +020084static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
85{
86 struct hnb_context *ctx = fd->data;
87 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +020088 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +020089 .sinfo_stream = ctx->hnbap_stream,
90 };
91 int rc;
92
93 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
94 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +020095 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +020096 return rc;
97}
98
Harald Weltea2e6a7a2015-08-29 21:47:39 +020099/*! call-back when the listen FD has something to read */
100static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
101{
102 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200103 struct hnb_context *ctx;
104 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200105 socklen_t len = sizeof(sockaddr);
106
107 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
108 if (new_fd < 0) {
109 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
110 return new_fd;
111 }
112
113 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
114
115 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
116 if (!ctx)
117 return -ENOMEM;
118
119 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200120 osmo_wqueue_init(&ctx->wqueue, 16);
121 ctx->wqueue.bfd.data = ctx;
122 ctx->wqueue.bfd.fd = new_fd;
123 ctx->wqueue.bfd.when = BSC_FD_READ;
124 ctx->wqueue.read_cb = hnb_read_cb;
125 ctx->wqueue.write_cb = hnb_write_cb;
126 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200127
128 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200129
130 return 0;
131}
132
Harald Welteb3dae302015-08-30 12:20:09 +0200133static const struct log_info_cat log_cat[] = {
134 [DMAIN] = {
135 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
136 .color = "",
137 .description = "Main program",
138 },
139};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200140
Harald Welteb3dae302015-08-30 12:20:09 +0200141static const struct log_info hnbgw_log_info = {
142 .cat = log_cat,
143 .num_cat = ARRAY_SIZE(log_cat),
144};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200145
Harald Welteb3dae302015-08-30 12:20:09 +0200146static struct vty_app_info vty_info = {
147 .name = "OsmoHNBGW",
148 .version = "0",
149};
150
151static int daemonize = 0;
152
Harald Welte5c11c942015-09-07 19:54:49 +0200153static int sctp_sock_init(int fd)
154{
155 struct sctp_event_subscribe event;
156 int rc;
157
158 /* subscribe for all events */
159 memset((uint8_t *)&event, 1, sizeof(event));
160 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
161 &event, sizeof(event));
162
163 return rc;
164}
165
Harald Welteb3dae302015-08-30 12:20:09 +0200166int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200167{
Harald Welteb3dae302015-08-30 12:20:09 +0200168 int rc;
169
170 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200171 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200172
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200173 g_hnb_gw.listen_fd.cb = listen_fd_cb;
174 g_hnb_gw.listen_fd.when = BSC_FD_READ;
175 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200176 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200177
Harald Welteb3dae302015-08-30 12:20:09 +0200178 rc = osmo_init_logging(&hnbgw_log_info);
179 if (rc < 0)
180 exit(1);
181
182 vty_init(&vty_info);
183
184 rc = telnet_init(NULL, &g_hnb_gw, 2323);
185 if (rc < 0) {
186 perror("Error binding VTY port");
187 exit(1);
188 }
189
190 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200191 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200192 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200193 if (rc < 0) {
194 perror("Error binding Iuh port");
195 exit(1);
196 }
Harald Welte5c11c942015-09-07 19:54:49 +0200197 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200198
199 if (daemonize) {
200 rc = osmo_daemonize();
201 if (rc < 0) {
202 perror("Error during daemonize");
203 exit(1);
204 }
205 }
206
207 while (1) {
208 rc = osmo_select_main(0);
209 if (rc < 0)
210 exit(3);
211 }
212
213 /* not reached */
214 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200215}