blob: 71224a10d051c61f8eed355eada63c1bcde4dd52 [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 Welte17878e22015-09-08 00:09:13 +020056 if (flags & MSG_NOTIFICATION) {
57 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
58 msgb_free(msg);
59 return 0;
60 }
61
Harald Welte5c11c942015-09-07 19:54:49 +020062 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
63
Harald Weltea2e6a7a2015-08-29 21:47:39 +020064 switch (sinfo.sinfo_ppid) {
65 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +020066 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020067 rc = hnbgw_hnbap_rx(hnb, msg);
68 break;
69 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +020070 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welteb3dae302015-08-30 12:20:09 +020071 //rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020072 break;
73 case IUH_PPI_SABP:
74 case IUH_PPI_RNA:
75 case IUH_PPI_PUA:
76 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
77 sinfo.sinfo_ppid);
78 rc = 0;
79 break;
80 default:
81 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
82 sinfo.sinfo_ppid);
83 rc = 0;
84 break;
85 }
86
Harald Weltef2f30002015-09-08 00:09:23 +020087 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020088 return rc;
89}
90
Harald Welte3f712562015-09-07 21:53:25 +020091static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
92{
93 struct hnb_context *ctx = fd->data;
94 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +020095 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +020096 .sinfo_stream = ctx->hnbap_stream,
97 };
98 int rc;
99
100 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
101 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200102 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200103 return rc;
104}
105
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200106/*! call-back when the listen FD has something to read */
107static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
108{
109 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200110 struct hnb_context *ctx;
111 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200112 socklen_t len = sizeof(sockaddr);
113
114 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
115 if (new_fd < 0) {
116 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
117 return new_fd;
118 }
119
120 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
121
122 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
123 if (!ctx)
124 return -ENOMEM;
125
126 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200127 osmo_wqueue_init(&ctx->wqueue, 16);
128 ctx->wqueue.bfd.data = ctx;
129 ctx->wqueue.bfd.fd = new_fd;
130 ctx->wqueue.bfd.when = BSC_FD_READ;
131 ctx->wqueue.read_cb = hnb_read_cb;
132 ctx->wqueue.write_cb = hnb_write_cb;
133 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200134
135 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200136
137 return 0;
138}
139
Harald Welteb3dae302015-08-30 12:20:09 +0200140static const struct log_info_cat log_cat[] = {
141 [DMAIN] = {
142 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
143 .color = "",
144 .description = "Main program",
145 },
146};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200147
Harald Welteb3dae302015-08-30 12:20:09 +0200148static const struct log_info hnbgw_log_info = {
149 .cat = log_cat,
150 .num_cat = ARRAY_SIZE(log_cat),
151};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200152
Harald Welteb3dae302015-08-30 12:20:09 +0200153static struct vty_app_info vty_info = {
154 .name = "OsmoHNBGW",
155 .version = "0",
156};
157
158static int daemonize = 0;
159
Harald Welte5c11c942015-09-07 19:54:49 +0200160static int sctp_sock_init(int fd)
161{
162 struct sctp_event_subscribe event;
163 int rc;
164
165 /* subscribe for all events */
166 memset((uint8_t *)&event, 1, sizeof(event));
167 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
168 &event, sizeof(event));
169
170 return rc;
171}
172
Harald Welteb3dae302015-08-30 12:20:09 +0200173int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200174{
Harald Welteb3dae302015-08-30 12:20:09 +0200175 int rc;
176
177 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200178 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200179
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200180 g_hnb_gw.listen_fd.cb = listen_fd_cb;
181 g_hnb_gw.listen_fd.when = BSC_FD_READ;
182 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200183 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200184
Harald Welteb3dae302015-08-30 12:20:09 +0200185 rc = osmo_init_logging(&hnbgw_log_info);
186 if (rc < 0)
187 exit(1);
188
189 vty_init(&vty_info);
190
191 rc = telnet_init(NULL, &g_hnb_gw, 2323);
192 if (rc < 0) {
193 perror("Error binding VTY port");
194 exit(1);
195 }
196
197 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200198 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200199 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200200 if (rc < 0) {
201 perror("Error binding Iuh port");
202 exit(1);
203 }
Harald Welte5c11c942015-09-07 19:54:49 +0200204 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200205
206 if (daemonize) {
207 rc = osmo_daemonize();
208 if (rc < 0) {
209 perror("Error during daemonize");
210 exit(1);
211 }
212 }
213
214 while (1) {
215 rc = osmo_select_main(0);
216 if (rc < 0)
217 exit(3);
218 }
219
220 /* not reached */
221 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200222}