blob: de401dd8b99a5cda0cb6e5814e26445cb739f7c1 [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
87 return rc;
88}
89
Harald Welte3f712562015-09-07 21:53:25 +020090static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
91{
92 struct hnb_context *ctx = fd->data;
93 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +020094 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +020095 .sinfo_stream = ctx->hnbap_stream,
96 };
97 int rc;
98
99 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
100 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200101 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200102 return rc;
103}
104
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200105/*! call-back when the listen FD has something to read */
106static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
107{
108 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200109 struct hnb_context *ctx;
110 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200111 socklen_t len = sizeof(sockaddr);
112
113 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
114 if (new_fd < 0) {
115 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
116 return new_fd;
117 }
118
119 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
120
121 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
122 if (!ctx)
123 return -ENOMEM;
124
125 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200126 osmo_wqueue_init(&ctx->wqueue, 16);
127 ctx->wqueue.bfd.data = ctx;
128 ctx->wqueue.bfd.fd = new_fd;
129 ctx->wqueue.bfd.when = BSC_FD_READ;
130 ctx->wqueue.read_cb = hnb_read_cb;
131 ctx->wqueue.write_cb = hnb_write_cb;
132 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200133
134 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200135
136 return 0;
137}
138
Harald Welteb3dae302015-08-30 12:20:09 +0200139static const struct log_info_cat log_cat[] = {
140 [DMAIN] = {
141 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
142 .color = "",
143 .description = "Main program",
144 },
145};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200146
Harald Welteb3dae302015-08-30 12:20:09 +0200147static const struct log_info hnbgw_log_info = {
148 .cat = log_cat,
149 .num_cat = ARRAY_SIZE(log_cat),
150};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200151
Harald Welteb3dae302015-08-30 12:20:09 +0200152static struct vty_app_info vty_info = {
153 .name = "OsmoHNBGW",
154 .version = "0",
155};
156
157static int daemonize = 0;
158
Harald Welte5c11c942015-09-07 19:54:49 +0200159static int sctp_sock_init(int fd)
160{
161 struct sctp_event_subscribe event;
162 int rc;
163
164 /* subscribe for all events */
165 memset((uint8_t *)&event, 1, sizeof(event));
166 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
167 &event, sizeof(event));
168
169 return rc;
170}
171
Harald Welteb3dae302015-08-30 12:20:09 +0200172int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200173{
Harald Welteb3dae302015-08-30 12:20:09 +0200174 int rc;
175
176 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200177 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200178
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200179 g_hnb_gw.listen_fd.cb = listen_fd_cb;
180 g_hnb_gw.listen_fd.when = BSC_FD_READ;
181 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200182 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200183
Harald Welteb3dae302015-08-30 12:20:09 +0200184 rc = osmo_init_logging(&hnbgw_log_info);
185 if (rc < 0)
186 exit(1);
187
188 vty_init(&vty_info);
189
190 rc = telnet_init(NULL, &g_hnb_gw, 2323);
191 if (rc < 0) {
192 perror("Error binding VTY port");
193 exit(1);
194 }
195
196 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200197 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200198 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200199 if (rc < 0) {
200 perror("Error binding Iuh port");
201 exit(1);
202 }
Harald Welte5c11c942015-09-07 19:54:49 +0200203 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200204
205 if (daemonize) {
206 rc = osmo_daemonize();
207 if (rc < 0) {
208 perror("Error during daemonize");
209 exit(1);
210 }
211 }
212
213 while (1) {
214 rc = osmo_select_main(0);
215 if (rc < 0)
216 exit(3);
217 }
218
219 /* not reached */
220 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200221}