blob: 7938c5b8ce2dc87f6bba17042697fcee7febcd02 [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>
Daniel Willmann56f62732015-12-02 11:22:53 +010045#include <osmocom/vty/command.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020046
Harald Welte75a4e652015-12-22 23:59:24 +010047#include <osmocom/sigtran/sua.h>
48#include <osmocom/sigtran/protocol/sua.h>
49#include <osmocom/sigtran/sccp_sap.h>
50
Harald Weltea2e6a7a2015-08-29 21:47:39 +020051#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020052#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020053#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020054
55static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020056static void *tall_ue_ctx;
Harald Welte75a4e652015-12-22 23:59:24 +010057static void *tall_sua_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020058void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020059
60struct hnb_gw g_hnb_gw = {
61 .config = {
62 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
63 },
64};
65
Harald Welteb534e5c2015-09-11 00:15:16 +020066struct ue_context *ue_context_by_id(uint32_t id)
67{
68 struct ue_context *ue;
69
70 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
71 if (ue->context_id == id)
72 return ue;
73 }
74 return NULL;
75
76}
77
78struct ue_context *ue_context_by_imsi(const char *imsi)
79{
80 struct ue_context *ue;
81
82 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
83 if (!strcmp(ue->imsi, imsi))
84 return ue;
85 }
86 return NULL;
87}
88
89static uint32_t get_next_ue_ctx_id(void)
90{
91 uint32_t id;
92
93 do {
94 id = g_hnb_gw.next_ue_ctx_id++;
95 } while (ue_context_by_id(id));
96
97 return id;
98}
99
100struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
101{
102 struct ue_context *ue;
103
104 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
105 if (!ue)
106 return NULL;
107
108 ue->hnb = hnb;
109 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
110 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +0200111 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200112
113 return ue;
114}
115
116void ue_context_free(struct ue_context *ue)
117{
118 llist_del(&ue->list);
119 talloc_free(ue);
120}
121
122
123
Harald Welte3f712562015-09-07 21:53:25 +0200124static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200125{
126 struct hnb_context *hnb = fd->data;
127 struct sctp_sndrcvinfo sinfo;
128 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200129 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200130 int rc;
131
132 if (!msg)
133 return -ENOMEM;
134
Harald Welte350814a2015-09-10 22:32:15 +0200135 /* we store a reference to the HomeNodeB in the msg->dest for the
136 * benefit of varoius downstream processing functions */
137 msg->dst = hnb;
138
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200139 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
140 NULL, NULL, &sinfo, &flags);
141 if (rc < 0) {
142 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200143 /* FIXME: clean up after disappeared HNB */
Daniel Willmann4267a292015-12-15 20:29:27 +0100144 close(fd->fd);
145 osmo_fd_unregister(fd);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100146 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100147 } else if (rc == 0) {
148 LOGP(DMAIN, LOGL_ERROR, "Connection to HNB closed\n");
149 /* TODO: Remove all UEs from that connection */
150 close(fd->fd);
151 osmo_fd_unregister(fd);
152 fd->fd = -1;
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100153 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100154
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100155 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100156 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200157 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100158 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200159
Harald Welte17878e22015-09-08 00:09:13 +0200160 if (flags & MSG_NOTIFICATION) {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100161 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Harald Welte17878e22015-09-08 00:09:13 +0200162 msgb_free(msg);
163 return 0;
164 }
165
Harald Welte5c11c942015-09-07 19:54:49 +0200166 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
167
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200168 switch (sinfo.sinfo_ppid) {
169 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200170 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200171 rc = hnbgw_hnbap_rx(hnb, msg);
172 break;
173 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200174 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200175 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200176 break;
177 case IUH_PPI_SABP:
178 case IUH_PPI_RNA:
179 case IUH_PPI_PUA:
180 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
181 sinfo.sinfo_ppid);
182 rc = 0;
183 break;
184 default:
185 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
186 sinfo.sinfo_ppid);
187 rc = 0;
188 break;
189 }
190
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100191out:
Harald Weltef2f30002015-09-08 00:09:23 +0200192 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193 return rc;
194}
195
Harald Welte3f712562015-09-07 21:53:25 +0200196static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
197{
198 struct hnb_context *ctx = fd->data;
199 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200200 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200201 .sinfo_stream = ctx->hnbap_stream,
202 };
203 int rc;
204
205 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
206 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200207 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200208 return rc;
209}
210
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200211/*! call-back when the listen FD has something to read */
212static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
213{
214 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200215 struct hnb_context *ctx;
216 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200217 socklen_t len = sizeof(sockaddr);
218
219 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
220 if (new_fd < 0) {
221 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
222 return new_fd;
223 }
224
225 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
226
227 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
228 if (!ctx)
229 return -ENOMEM;
230
231 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200232 osmo_wqueue_init(&ctx->wqueue, 16);
233 ctx->wqueue.bfd.data = ctx;
234 ctx->wqueue.bfd.fd = new_fd;
235 ctx->wqueue.bfd.when = BSC_FD_READ;
236 ctx->wqueue.read_cb = hnb_read_cb;
237 ctx->wqueue.write_cb = hnb_write_cb;
238 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200239
240 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200241
242 return 0;
243}
244
Harald Welte75a4e652015-12-22 23:59:24 +0100245/* Entry point for primitives coming up from SCCP User SAP */
246static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
247{
248 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
249
250 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
251
252 /* FIXME: Do something */
253
254 msgb_free(oph->msg);
255
256 return 0;
257}
258
Harald Welteb3dae302015-08-30 12:20:09 +0200259static const struct log_info_cat log_cat[] = {
260 [DMAIN] = {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100261 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200262 .color = "",
263 .description = "Main program",
264 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100265 [DHNBAP] = {
266 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
267 .color = "",
268 .description = "Home Node B Application Part",
269 },
Harald Welte75a4e652015-12-22 23:59:24 +0100270 [DSUA] = {
271 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
272 .color = "",
273 .description = "SCCP User Adaptation",
274 },
Harald Welteb3dae302015-08-30 12:20:09 +0200275};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200276
Harald Welteb3dae302015-08-30 12:20:09 +0200277static const struct log_info hnbgw_log_info = {
278 .cat = log_cat,
279 .num_cat = ARRAY_SIZE(log_cat),
280};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200281
Harald Welteb3dae302015-08-30 12:20:09 +0200282static struct vty_app_info vty_info = {
283 .name = "OsmoHNBGW",
284 .version = "0",
285};
286
287static int daemonize = 0;
288
Harald Welte5c11c942015-09-07 19:54:49 +0200289static int sctp_sock_init(int fd)
290{
291 struct sctp_event_subscribe event;
292 int rc;
293
294 /* subscribe for all events */
295 memset((uint8_t *)&event, 1, sizeof(event));
296 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
297 &event, sizeof(event));
298
299 return rc;
300}
301
Daniel Willmann56f62732015-12-02 11:22:53 +0100302static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
303{
304 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
305 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
306 VTY_NEWLINE);
307 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
308}
309
310static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
311{
312 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
313}
314
315DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
316{
317 struct hnb_context *hnb;
318
319 llist_for_each_entry(hnb, &g_hnb_gw.hnb_list, list) {
320 vty_dump_hnb_info(vty, hnb);
321 }
322
323 return CMD_SUCCESS;
324}
325
326DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
327{
328 struct ue_context *ue;
329
330 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
331 vty_dump_ue_info(vty, ue);
332 }
333
334 return CMD_SUCCESS;
335}
336
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100337DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
338{
339 talloc_report_full(tall_hnb_ctx, stderr);
340 talloc_report_full(tall_ue_ctx, stderr);
341 talloc_report_full(talloc_asn1_ctx, stderr);
342
343 return CMD_SUCCESS;
344}
345
Daniel Willmann56f62732015-12-02 11:22:53 +0100346static void hnbgw_vty_init(void)
347{
348 install_element_ve(&show_hnb_cmd);
349 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100350 install_element_ve(&show_talloc_cmd);
Daniel Willmann56f62732015-12-02 11:22:53 +0100351}
352
Harald Welteb3dae302015-08-30 12:20:09 +0200353int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200354{
Harald Welte75a4e652015-12-22 23:59:24 +0100355 struct osmo_sua_user *sua_user;
356 struct osmo_sua_link *sua_link;
Harald Welteb3dae302015-08-30 12:20:09 +0200357 int rc;
358
359 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200360 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte75a4e652015-12-22 23:59:24 +0100361 tall_sua_ctx = talloc_named_const(NULL, 0, "sua");
Harald Welte08de6382015-08-31 09:54:45 +0200362 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200363
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200364 g_hnb_gw.listen_fd.cb = listen_fd_cb;
365 g_hnb_gw.listen_fd.when = BSC_FD_READ;
366 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200367 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200368 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200369 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200370
Harald Welteb3dae302015-08-30 12:20:09 +0200371 rc = osmo_init_logging(&hnbgw_log_info);
372 if (rc < 0)
373 exit(1);
374
375 vty_init(&vty_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100376 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200377
378 rc = telnet_init(NULL, &g_hnb_gw, 2323);
379 if (rc < 0) {
380 perror("Error binding VTY port");
381 exit(1);
382 }
383
Harald Welte75a4e652015-12-22 23:59:24 +0100384 osmo_sua_set_log_area(DSUA);
385 sua_user = osmo_sua_user_create(tall_sua_ctx, sccp_sap_up);
386 if (!sua_user) {
387 perror("Failed to init SUA");
388 exit(1);
389 }
390 rc = osmo_sua_client_connect(sua_user, "127.0.0.1", SUA_PORT);
391 if (rc < 0) {
392 perror("Failed to connect SUA");
393 exit(1);
394 }
395 sua_link = osmo_sua_client_get_link(sua_user);
396 if (!sua_link) {
397 perror("Failed to get SUA link");
398 exit(1);
399 }
400
Harald Welteb3dae302015-08-30 12:20:09 +0200401 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200402 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200403 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200404 if (rc < 0) {
405 perror("Error binding Iuh port");
406 exit(1);
407 }
Harald Welte5c11c942015-09-07 19:54:49 +0200408 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200409
410 if (daemonize) {
411 rc = osmo_daemonize();
412 if (rc < 0) {
413 perror("Error during daemonize");
414 exit(1);
415 }
416 }
417
418 while (1) {
419 rc = osmo_select_main(0);
420 if (rc < 0)
421 exit(3);
422 }
423
424 /* not reached */
425 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200426}