blob: 651c0089eb8cfae3f5d47bdffb582aded9f28669 [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* main application for hnb-gw part of osmo-iuh */
2
3/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Harald Welteb3dae302015-08-30 12:20:09 +020021#include <unistd.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <getopt.h>
26#include <errno.h>
27#include <signal.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020028
Harald Welteb3dae302015-08-30 12:20:09 +020029#include <sys/types.h>
30#include <sys/socket.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020031#include <netinet/in.h>
Harald Welteb3dae302015-08-30 12:20:09 +020032#include <netinet/sctp.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020033#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020034
35#include <osmocom/core/application.h>
36#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020037#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020038#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020039#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020040#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020041#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020042
43#include <osmocom/vty/telnet_interface.h>
44#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020045
46#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020047#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020048#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020049
50static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020051static void *tall_ue_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020052void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020053
54struct hnb_gw g_hnb_gw = {
55 .config = {
56 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
57 },
58};
59
Harald Welteb534e5c2015-09-11 00:15:16 +020060struct ue_context *ue_context_by_id(uint32_t id)
61{
62 struct ue_context *ue;
63
64 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
65 if (ue->context_id == id)
66 return ue;
67 }
68 return NULL;
69
70}
71
72struct ue_context *ue_context_by_imsi(const char *imsi)
73{
74 struct ue_context *ue;
75
76 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
77 if (!strcmp(ue->imsi, imsi))
78 return ue;
79 }
80 return NULL;
81}
82
83static uint32_t get_next_ue_ctx_id(void)
84{
85 uint32_t id;
86
87 do {
88 id = g_hnb_gw.next_ue_ctx_id++;
89 } while (ue_context_by_id(id));
90
91 return id;
92}
93
94struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
95{
96 struct ue_context *ue;
97
98 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
99 if (!ue)
100 return NULL;
101
102 ue->hnb = hnb;
103 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
104 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +0200105 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200106
107 return ue;
108}
109
110void ue_context_free(struct ue_context *ue)
111{
112 llist_del(&ue->list);
113 talloc_free(ue);
114}
115
116
117
Harald Welte3f712562015-09-07 21:53:25 +0200118static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200119{
120 struct hnb_context *hnb = fd->data;
121 struct sctp_sndrcvinfo sinfo;
122 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200123 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200124 int rc;
125
126 if (!msg)
127 return -ENOMEM;
128
Harald Welte350814a2015-09-10 22:32:15 +0200129 /* we store a reference to the HomeNodeB in the msg->dest for the
130 * benefit of varoius downstream processing functions */
131 msg->dst = hnb;
132
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200133 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
134 NULL, NULL, &sinfo, &flags);
135 if (rc < 0) {
136 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200137 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200138 return rc;
139 } else
140 msgb_put(msg, rc);
141
Harald Welte17878e22015-09-08 00:09:13 +0200142 if (flags & MSG_NOTIFICATION) {
143 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
144 msgb_free(msg);
145 return 0;
146 }
147
Harald Welte5c11c942015-09-07 19:54:49 +0200148 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
149
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200150 switch (sinfo.sinfo_ppid) {
151 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200152 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200153 rc = hnbgw_hnbap_rx(hnb, msg);
154 break;
155 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200156 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200157 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200158 break;
159 case IUH_PPI_SABP:
160 case IUH_PPI_RNA:
161 case IUH_PPI_PUA:
162 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
163 sinfo.sinfo_ppid);
164 rc = 0;
165 break;
166 default:
167 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
168 sinfo.sinfo_ppid);
169 rc = 0;
170 break;
171 }
172
Harald Weltef2f30002015-09-08 00:09:23 +0200173 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200174 return rc;
175}
176
Harald Welte3f712562015-09-07 21:53:25 +0200177static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
178{
179 struct hnb_context *ctx = fd->data;
180 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200181 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200182 .sinfo_stream = ctx->hnbap_stream,
183 };
184 int rc;
185
186 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
187 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200188 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200189 return rc;
190}
191
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200192/*! call-back when the listen FD has something to read */
193static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
194{
195 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200196 struct hnb_context *ctx;
197 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200198 socklen_t len = sizeof(sockaddr);
199
200 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
201 if (new_fd < 0) {
202 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
203 return new_fd;
204 }
205
206 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
207
208 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
209 if (!ctx)
210 return -ENOMEM;
211
212 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200213 osmo_wqueue_init(&ctx->wqueue, 16);
214 ctx->wqueue.bfd.data = ctx;
215 ctx->wqueue.bfd.fd = new_fd;
216 ctx->wqueue.bfd.when = BSC_FD_READ;
217 ctx->wqueue.read_cb = hnb_read_cb;
218 ctx->wqueue.write_cb = hnb_write_cb;
219 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200220
221 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200222
223 return 0;
224}
225
Harald Welteb3dae302015-08-30 12:20:09 +0200226static const struct log_info_cat log_cat[] = {
227 [DMAIN] = {
228 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
229 .color = "",
230 .description = "Main program",
231 },
232};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200233
Harald Welteb3dae302015-08-30 12:20:09 +0200234static const struct log_info hnbgw_log_info = {
235 .cat = log_cat,
236 .num_cat = ARRAY_SIZE(log_cat),
237};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200238
Harald Welteb3dae302015-08-30 12:20:09 +0200239static struct vty_app_info vty_info = {
240 .name = "OsmoHNBGW",
241 .version = "0",
242};
243
244static int daemonize = 0;
245
Harald Welte5c11c942015-09-07 19:54:49 +0200246static int sctp_sock_init(int fd)
247{
248 struct sctp_event_subscribe event;
249 int rc;
250
251 /* subscribe for all events */
252 memset((uint8_t *)&event, 1, sizeof(event));
253 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
254 &event, sizeof(event));
255
256 return rc;
257}
258
Harald Welteb3dae302015-08-30 12:20:09 +0200259int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200260{
Harald Welteb3dae302015-08-30 12:20:09 +0200261 int rc;
262
263 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200264 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte08de6382015-08-31 09:54:45 +0200265 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200266
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200267 g_hnb_gw.listen_fd.cb = listen_fd_cb;
268 g_hnb_gw.listen_fd.when = BSC_FD_READ;
269 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200270 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200271 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200272 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200273
Harald Welteb3dae302015-08-30 12:20:09 +0200274 rc = osmo_init_logging(&hnbgw_log_info);
275 if (rc < 0)
276 exit(1);
277
278 vty_init(&vty_info);
279
280 rc = telnet_init(NULL, &g_hnb_gw, 2323);
281 if (rc < 0) {
282 perror("Error binding VTY port");
283 exit(1);
284 }
285
286 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200287 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200288 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200289 if (rc < 0) {
290 perror("Error binding Iuh port");
291 exit(1);
292 }
Harald Welte5c11c942015-09-07 19:54:49 +0200293 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200294
295 if (daemonize) {
296 rc = osmo_daemonize();
297 if (rc < 0) {
298 perror("Error during daemonize");
299 exit(1);
300 }
301 }
302
303 while (1) {
304 rc = osmo_select_main(0);
305 if (rc < 0)
306 exit(3);
307 }
308
309 /* not reached */
310 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200311}