blob: 2db0afb66821f50203f77fe8c550410efbf89dbc [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 = {
88 .sinfo_ppid = msgb_ppid(msg),
89 .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);
95 msgb_free(msg);
96
97 return rc;
98}
99
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200100/*! call-back when the listen FD has something to read */
101static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
102{
103 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200104 struct hnb_context *ctx;
105 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200106 socklen_t len = sizeof(sockaddr);
107
108 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
109 if (new_fd < 0) {
110 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
111 return new_fd;
112 }
113
114 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
115
116 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
117 if (!ctx)
118 return -ENOMEM;
119
120 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200121 osmo_wqueue_init(&ctx->wqueue, 16);
122 ctx->wqueue.bfd.data = ctx;
123 ctx->wqueue.bfd.fd = new_fd;
124 ctx->wqueue.bfd.when = BSC_FD_READ;
125 ctx->wqueue.read_cb = hnb_read_cb;
126 ctx->wqueue.write_cb = hnb_write_cb;
127 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200128
129 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200130
131 return 0;
132}
133
Harald Welteb3dae302015-08-30 12:20:09 +0200134static const struct log_info_cat log_cat[] = {
135 [DMAIN] = {
136 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
137 .color = "",
138 .description = "Main program",
139 },
140};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200141
Harald Welteb3dae302015-08-30 12:20:09 +0200142static const struct log_info hnbgw_log_info = {
143 .cat = log_cat,
144 .num_cat = ARRAY_SIZE(log_cat),
145};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200146
Harald Welteb3dae302015-08-30 12:20:09 +0200147static struct vty_app_info vty_info = {
148 .name = "OsmoHNBGW",
149 .version = "0",
150};
151
152static int daemonize = 0;
153
Harald Welte5c11c942015-09-07 19:54:49 +0200154static int sctp_sock_init(int fd)
155{
156 struct sctp_event_subscribe event;
157 int rc;
158
159 /* subscribe for all events */
160 memset((uint8_t *)&event, 1, sizeof(event));
161 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
162 &event, sizeof(event));
163
164 return rc;
165}
166
Harald Welteb3dae302015-08-30 12:20:09 +0200167int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200168{
Harald Welteb3dae302015-08-30 12:20:09 +0200169 int rc;
170
171 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200172 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200173
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200174 g_hnb_gw.listen_fd.cb = listen_fd_cb;
175 g_hnb_gw.listen_fd.when = BSC_FD_READ;
176 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200177 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200178
Harald Welteb3dae302015-08-30 12:20:09 +0200179 rc = osmo_init_logging(&hnbgw_log_info);
180 if (rc < 0)
181 exit(1);
182
183 vty_init(&vty_info);
184
185 rc = telnet_init(NULL, &g_hnb_gw, 2323);
186 if (rc < 0) {
187 perror("Error binding VTY port");
188 exit(1);
189 }
190
191 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200192 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200194 if (rc < 0) {
195 perror("Error binding Iuh port");
196 exit(1);
197 }
Harald Welte5c11c942015-09-07 19:54:49 +0200198 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200199
200 if (daemonize) {
201 rc = osmo_daemonize();
202 if (rc < 0) {
203 perror("Error during daemonize");
204 exit(1);
205 }
206 }
207
208 while (1) {
209 rc = osmo_select_main(0);
210 if (rc < 0)
211 exit(3);
212 }
213
214 /* not reached */
215 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200216}