blob: 60baf78322137cef47ed7b2868c737fefb97f319 [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 Welteffa7c0a2015-12-23 00:03:41 +010047#include <osmocom/netif/stream.h>
48
Harald Welte75a4e652015-12-22 23:59:24 +010049#include <osmocom/sigtran/sua.h>
50#include <osmocom/sigtran/protocol/sua.h>
51#include <osmocom/sigtran/sccp_sap.h>
52
Harald Weltea2e6a7a2015-08-29 21:47:39 +020053#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020054#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020055#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020056
57static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020058static void *tall_ue_ctx;
Harald Welte75a4e652015-12-22 23:59:24 +010059static void *tall_sua_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020060void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020061
62struct hnb_gw g_hnb_gw = {
63 .config = {
64 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
65 },
66};
67
Harald Welteb534e5c2015-09-11 00:15:16 +020068struct ue_context *ue_context_by_id(uint32_t id)
69{
70 struct ue_context *ue;
71
72 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
73 if (ue->context_id == id)
74 return ue;
75 }
76 return NULL;
77
78}
79
80struct ue_context *ue_context_by_imsi(const char *imsi)
81{
82 struct ue_context *ue;
83
84 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
85 if (!strcmp(ue->imsi, imsi))
86 return ue;
87 }
88 return NULL;
89}
90
91static uint32_t get_next_ue_ctx_id(void)
92{
93 uint32_t id;
94
95 do {
96 id = g_hnb_gw.next_ue_ctx_id++;
97 } while (ue_context_by_id(id));
98
99 return id;
100}
101
102struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
103{
104 struct ue_context *ue;
105
106 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
107 if (!ue)
108 return NULL;
109
110 ue->hnb = hnb;
111 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
112 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +0200113 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200114
115 return ue;
116}
117
118void ue_context_free(struct ue_context *ue)
119{
120 llist_del(&ue->list);
121 talloc_free(ue);
122}
123
124
125
Harald Welte3f712562015-09-07 21:53:25 +0200126static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200127{
128 struct hnb_context *hnb = fd->data;
129 struct sctp_sndrcvinfo sinfo;
130 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200131 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200132 int rc;
133
134 if (!msg)
135 return -ENOMEM;
136
Harald Welte350814a2015-09-10 22:32:15 +0200137 /* we store a reference to the HomeNodeB in the msg->dest for the
138 * benefit of varoius downstream processing functions */
139 msg->dst = hnb;
140
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200141 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
142 NULL, NULL, &sinfo, &flags);
143 if (rc < 0) {
144 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200145 /* FIXME: clean up after disappeared HNB */
Daniel Willmann4267a292015-12-15 20:29:27 +0100146 close(fd->fd);
147 osmo_fd_unregister(fd);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100148 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100149 } else if (rc == 0) {
150 LOGP(DMAIN, LOGL_ERROR, "Connection to HNB closed\n");
151 /* TODO: Remove all UEs from that connection */
152 close(fd->fd);
153 osmo_fd_unregister(fd);
154 fd->fd = -1;
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100155 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100156
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100157 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100158 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200159 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100160 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200161
Harald Welte17878e22015-09-08 00:09:13 +0200162 if (flags & MSG_NOTIFICATION) {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100163 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Harald Welte17878e22015-09-08 00:09:13 +0200164 msgb_free(msg);
165 return 0;
166 }
167
Harald Welte5c11c942015-09-07 19:54:49 +0200168 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
169
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200170 switch (sinfo.sinfo_ppid) {
171 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200172 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200173 rc = hnbgw_hnbap_rx(hnb, msg);
174 break;
175 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200176 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200177 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200178 break;
179 case IUH_PPI_SABP:
180 case IUH_PPI_RNA:
181 case IUH_PPI_PUA:
182 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
183 sinfo.sinfo_ppid);
184 rc = 0;
185 break;
186 default:
187 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
188 sinfo.sinfo_ppid);
189 rc = 0;
190 break;
191 }
192
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100193out:
Harald Weltef2f30002015-09-08 00:09:23 +0200194 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200195 return rc;
196}
197
Harald Welte3f712562015-09-07 21:53:25 +0200198static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
199{
200 struct hnb_context *ctx = fd->data;
201 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100202 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200203 .sinfo_stream = ctx->hnbap_stream,
204 };
205 int rc;
206
207 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
208 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200209 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200210 return rc;
211}
212
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200213/*! call-back when the listen FD has something to read */
214static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
215{
216 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200217 struct hnb_context *ctx;
218 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200219 socklen_t len = sizeof(sockaddr);
220
221 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
222 if (new_fd < 0) {
223 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
224 return new_fd;
225 }
226
227 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
228
229 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
230 if (!ctx)
231 return -ENOMEM;
232
233 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200234 osmo_wqueue_init(&ctx->wqueue, 16);
235 ctx->wqueue.bfd.data = ctx;
236 ctx->wqueue.bfd.fd = new_fd;
237 ctx->wqueue.bfd.when = BSC_FD_READ;
238 ctx->wqueue.read_cb = hnb_read_cb;
239 ctx->wqueue.write_cb = hnb_write_cb;
240 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200241
242 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200243
244 return 0;
245}
246
Harald Welte75a4e652015-12-22 23:59:24 +0100247/* Entry point for primitives coming up from SCCP User SAP */
248static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
249{
250 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
251
252 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
253
254 /* FIXME: Do something */
255
256 msgb_free(oph->msg);
257
258 return 0;
259}
260
Harald Welteb3dae302015-08-30 12:20:09 +0200261static const struct log_info_cat log_cat[] = {
262 [DMAIN] = {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100263 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200264 .color = "",
265 .description = "Main program",
266 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100267 [DHNBAP] = {
268 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
269 .color = "",
270 .description = "Home Node B Application Part",
271 },
Harald Welte75a4e652015-12-22 23:59:24 +0100272 [DSUA] = {
273 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
274 .color = "",
275 .description = "SCCP User Adaptation",
276 },
Harald Welteb3dae302015-08-30 12:20:09 +0200277};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200278
Harald Welteb3dae302015-08-30 12:20:09 +0200279static const struct log_info hnbgw_log_info = {
280 .cat = log_cat,
281 .num_cat = ARRAY_SIZE(log_cat),
282};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200283
Harald Welteb3dae302015-08-30 12:20:09 +0200284static struct vty_app_info vty_info = {
285 .name = "OsmoHNBGW",
286 .version = "0",
287};
288
289static int daemonize = 0;
290
Harald Welte5c11c942015-09-07 19:54:49 +0200291static int sctp_sock_init(int fd)
292{
293 struct sctp_event_subscribe event;
294 int rc;
295
296 /* subscribe for all events */
297 memset((uint8_t *)&event, 1, sizeof(event));
298 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
299 &event, sizeof(event));
300
301 return rc;
302}
303
Daniel Willmann56f62732015-12-02 11:22:53 +0100304static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
305{
306 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
307 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
308 VTY_NEWLINE);
309 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
310}
311
312static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
313{
314 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
315}
316
317DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
318{
319 struct hnb_context *hnb;
320
321 llist_for_each_entry(hnb, &g_hnb_gw.hnb_list, list) {
322 vty_dump_hnb_info(vty, hnb);
323 }
324
325 return CMD_SUCCESS;
326}
327
328DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
329{
330 struct ue_context *ue;
331
332 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
333 vty_dump_ue_info(vty, ue);
334 }
335
336 return CMD_SUCCESS;
337}
338
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100339DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
340{
341 talloc_report_full(tall_hnb_ctx, stderr);
342 talloc_report_full(tall_ue_ctx, stderr);
343 talloc_report_full(talloc_asn1_ctx, stderr);
344
345 return CMD_SUCCESS;
346}
347
Daniel Willmann56f62732015-12-02 11:22:53 +0100348static void hnbgw_vty_init(void)
349{
350 install_element_ve(&show_hnb_cmd);
351 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100352 install_element_ve(&show_talloc_cmd);
Daniel Willmann56f62732015-12-02 11:22:53 +0100353}
354
Harald Welteb3dae302015-08-30 12:20:09 +0200355int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200356{
Harald Welte75a4e652015-12-22 23:59:24 +0100357 struct osmo_sua_user *sua_user;
358 struct osmo_sua_link *sua_link;
Harald Welteb3dae302015-08-30 12:20:09 +0200359 int rc;
360
361 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200362 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte75a4e652015-12-22 23:59:24 +0100363 tall_sua_ctx = talloc_named_const(NULL, 0, "sua");
Harald Welte08de6382015-08-31 09:54:45 +0200364 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200365
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200366 g_hnb_gw.listen_fd.cb = listen_fd_cb;
367 g_hnb_gw.listen_fd.when = BSC_FD_READ;
368 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200369 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200370 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200371 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200372
Harald Welteb3dae302015-08-30 12:20:09 +0200373 rc = osmo_init_logging(&hnbgw_log_info);
374 if (rc < 0)
375 exit(1);
376
377 vty_init(&vty_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100378 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200379
380 rc = telnet_init(NULL, &g_hnb_gw, 2323);
381 if (rc < 0) {
382 perror("Error binding VTY port");
383 exit(1);
384 }
385
Harald Welte75a4e652015-12-22 23:59:24 +0100386 osmo_sua_set_log_area(DSUA);
387 sua_user = osmo_sua_user_create(tall_sua_ctx, sccp_sap_up);
388 if (!sua_user) {
389 perror("Failed to init SUA");
390 exit(1);
391 }
392 rc = osmo_sua_client_connect(sua_user, "127.0.0.1", SUA_PORT);
393 if (rc < 0) {
394 perror("Failed to connect SUA");
395 exit(1);
396 }
397 sua_link = osmo_sua_client_get_link(sua_user);
398 if (!sua_link) {
399 perror("Failed to get SUA link");
400 exit(1);
401 }
402
Harald Welteb3dae302015-08-30 12:20:09 +0200403 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200404 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200405 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200406 if (rc < 0) {
407 perror("Error binding Iuh port");
408 exit(1);
409 }
Harald Welte5c11c942015-09-07 19:54:49 +0200410 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200411
412 if (daemonize) {
413 rc = osmo_daemonize();
414 if (rc < 0) {
415 perror("Error during daemonize");
416 exit(1);
417 }
418 }
419
420 while (1) {
421 rc = osmo_select_main(0);
422 if (rc < 0)
423 exit(3);
424 }
425
426 /* not reached */
427 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200428}