blob: 7c121554a0ac136096b052b1adad58801b94e662 [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
48 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
49 NULL, NULL, &sinfo, &flags);
50 if (rc < 0) {
51 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +020052 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +020053 return rc;
54 } else
55 msgb_put(msg, rc);
56
Harald Welte17878e22015-09-08 00:09:13 +020057 if (flags & MSG_NOTIFICATION) {
58 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
59 msgb_free(msg);
60 return 0;
61 }
62
Harald Welte5c11c942015-09-07 19:54:49 +020063 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
64
Harald Weltea2e6a7a2015-08-29 21:47:39 +020065 switch (sinfo.sinfo_ppid) {
66 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +020067 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020068 rc = hnbgw_hnbap_rx(hnb, msg);
69 break;
70 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +020071 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +020072 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020073 break;
74 case IUH_PPI_SABP:
75 case IUH_PPI_RNA:
76 case IUH_PPI_PUA:
77 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
78 sinfo.sinfo_ppid);
79 rc = 0;
80 break;
81 default:
82 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
83 sinfo.sinfo_ppid);
84 rc = 0;
85 break;
86 }
87
Harald Weltef2f30002015-09-08 00:09:23 +020088 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020089 return rc;
90}
91
Harald Welte3f712562015-09-07 21:53:25 +020092static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
93{
94 struct hnb_context *ctx = fd->data;
95 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +020096 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +020097 .sinfo_stream = ctx->hnbap_stream,
98 };
99 int rc;
100
101 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
102 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200103 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200104 return rc;
105}
106
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200107/*! call-back when the listen FD has something to read */
108static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
109{
110 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200111 struct hnb_context *ctx;
112 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200113 socklen_t len = sizeof(sockaddr);
114
115 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
116 if (new_fd < 0) {
117 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
118 return new_fd;
119 }
120
121 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
122
123 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
124 if (!ctx)
125 return -ENOMEM;
126
127 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200128 osmo_wqueue_init(&ctx->wqueue, 16);
129 ctx->wqueue.bfd.data = ctx;
130 ctx->wqueue.bfd.fd = new_fd;
131 ctx->wqueue.bfd.when = BSC_FD_READ;
132 ctx->wqueue.read_cb = hnb_read_cb;
133 ctx->wqueue.write_cb = hnb_write_cb;
134 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200135
136 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200137
138 return 0;
139}
140
Harald Welteb3dae302015-08-30 12:20:09 +0200141static const struct log_info_cat log_cat[] = {
142 [DMAIN] = {
143 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
144 .color = "",
145 .description = "Main program",
146 },
147};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200148
Harald Welteb3dae302015-08-30 12:20:09 +0200149static const struct log_info hnbgw_log_info = {
150 .cat = log_cat,
151 .num_cat = ARRAY_SIZE(log_cat),
152};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200153
Harald Welteb3dae302015-08-30 12:20:09 +0200154static struct vty_app_info vty_info = {
155 .name = "OsmoHNBGW",
156 .version = "0",
157};
158
159static int daemonize = 0;
160
Harald Welte5c11c942015-09-07 19:54:49 +0200161static int sctp_sock_init(int fd)
162{
163 struct sctp_event_subscribe event;
164 int rc;
165
166 /* subscribe for all events */
167 memset((uint8_t *)&event, 1, sizeof(event));
168 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
169 &event, sizeof(event));
170
171 return rc;
172}
173
Harald Welteb3dae302015-08-30 12:20:09 +0200174int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200175{
Harald Welteb3dae302015-08-30 12:20:09 +0200176 int rc;
177
178 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200179 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200180
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200181 g_hnb_gw.listen_fd.cb = listen_fd_cb;
182 g_hnb_gw.listen_fd.when = BSC_FD_READ;
183 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200184 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200185
Harald Welteb3dae302015-08-30 12:20:09 +0200186 rc = osmo_init_logging(&hnbgw_log_info);
187 if (rc < 0)
188 exit(1);
189
190 vty_init(&vty_info);
191
192 rc = telnet_init(NULL, &g_hnb_gw, 2323);
193 if (rc < 0) {
194 perror("Error binding VTY port");
195 exit(1);
196 }
197
198 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200199 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200200 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200201 if (rc < 0) {
202 perror("Error binding Iuh port");
203 exit(1);
204 }
Harald Welte5c11c942015-09-07 19:54:49 +0200205 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200206
207 if (daemonize) {
208 rc = osmo_daemonize();
209 if (rc < 0) {
210 perror("Error during daemonize");
211 exit(1);
212 }
213 }
214
215 while (1) {
216 rc = osmo_select_main(0);
217 if (rc < 0)
218 exit(3);
219 }
220
221 /* not reached */
222 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200223}