blob: 5e033fae71ddb2bc8a83a8f4f4978da66a271e72 [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>
19
20#include <osmocom/vty/telnet_interface.h>
21#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020022
23#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020024#include "hnbgw_hnbap.h"
25
26static void *tall_hnb_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020027void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020028
29struct hnb_gw g_hnb_gw = {
30 .config = {
31 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
32 },
33};
34
35static int hnb_socket_cb(struct osmo_fd *fd, unsigned int what)
36{
37 struct hnb_context *hnb = fd->data;
38 struct sctp_sndrcvinfo sinfo;
39 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
40 int flags;
41 int rc;
42
43 if (!msg)
44 return -ENOMEM;
45
46 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
47 NULL, NULL, &sinfo, &flags);
48 if (rc < 0) {
49 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +020050 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +020051 return rc;
52 } else
53 msgb_put(msg, rc);
54
Harald Welte5c11c942015-09-07 19:54:49 +020055 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
56
Harald Weltea2e6a7a2015-08-29 21:47:39 +020057 switch (sinfo.sinfo_ppid) {
58 case IUH_PPI_HNBAP:
59 rc = hnbgw_hnbap_rx(hnb, msg);
60 break;
61 case IUH_PPI_RUA:
Harald Welteb3dae302015-08-30 12:20:09 +020062 //rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020063 break;
64 case IUH_PPI_SABP:
65 case IUH_PPI_RNA:
66 case IUH_PPI_PUA:
67 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
68 sinfo.sinfo_ppid);
69 rc = 0;
70 break;
71 default:
72 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
73 sinfo.sinfo_ppid);
74 rc = 0;
75 break;
76 }
77
78 return rc;
79}
80
81/*! call-back when the listen FD has something to read */
82static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
83{
84 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +020085 struct hnb_context *ctx;
86 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020087 socklen_t len = sizeof(sockaddr);
88
89 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
90 if (new_fd < 0) {
91 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
92 return new_fd;
93 }
94
95 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
96
97 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
98 if (!ctx)
99 return -ENOMEM;
100
101 ctx->gw = gw;
102 ctx->socket.data = ctx;
103 ctx->socket.fd = new_fd;
104 ctx->socket.when = BSC_FD_READ;
105 ctx->socket.cb = hnb_socket_cb;
Harald Welteb3dae302015-08-30 12:20:09 +0200106 osmo_fd_register(&ctx->socket);
107
108 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200109
110 return 0;
111}
112
Harald Welteb3dae302015-08-30 12:20:09 +0200113static const struct log_info_cat log_cat[] = {
114 [DMAIN] = {
115 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
116 .color = "",
117 .description = "Main program",
118 },
119};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200120
Harald Welteb3dae302015-08-30 12:20:09 +0200121static const struct log_info hnbgw_log_info = {
122 .cat = log_cat,
123 .num_cat = ARRAY_SIZE(log_cat),
124};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200125
Harald Welteb3dae302015-08-30 12:20:09 +0200126static struct vty_app_info vty_info = {
127 .name = "OsmoHNBGW",
128 .version = "0",
129};
130
131static int daemonize = 0;
132
Harald Welte5c11c942015-09-07 19:54:49 +0200133static int sctp_sock_init(int fd)
134{
135 struct sctp_event_subscribe event;
136 int rc;
137
138 /* subscribe for all events */
139 memset((uint8_t *)&event, 1, sizeof(event));
140 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
141 &event, sizeof(event));
142
143 return rc;
144}
145
Harald Welteb3dae302015-08-30 12:20:09 +0200146int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200147{
Harald Welteb3dae302015-08-30 12:20:09 +0200148 int rc;
149
150 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welte08de6382015-08-31 09:54:45 +0200151 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200152
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200153 g_hnb_gw.listen_fd.cb = listen_fd_cb;
154 g_hnb_gw.listen_fd.when = BSC_FD_READ;
155 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200156 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200157
Harald Welteb3dae302015-08-30 12:20:09 +0200158 rc = osmo_init_logging(&hnbgw_log_info);
159 if (rc < 0)
160 exit(1);
161
162 vty_init(&vty_info);
163
164 rc = telnet_init(NULL, &g_hnb_gw, 2323);
165 if (rc < 0) {
166 perror("Error binding VTY port");
167 exit(1);
168 }
169
170 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200171 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200172 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200173 if (rc < 0) {
174 perror("Error binding Iuh port");
175 exit(1);
176 }
Harald Welte5c11c942015-09-07 19:54:49 +0200177 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200178
179 if (daemonize) {
180 rc = osmo_daemonize();
181 if (rc < 0) {
182 perror("Error during daemonize");
183 exit(1);
184 }
185 }
186
187 while (1) {
188 rc = osmo_select_main(0);
189 if (rc < 0)
190 exit(3);
191 }
192
193 /* not reached */
194 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200195}