blob: bc8be99bb51001f591146db58f4dd444c6b2786f [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 Welte90256ba2015-12-23 20:16:36 +010056#include "context_map.h"
Harald Welteb3dae302015-08-30 12:20:09 +020057
58static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020059static void *tall_ue_ctx;
Harald Welte75a4e652015-12-22 23:59:24 +010060static void *tall_sua_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020061void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020062
63struct hnb_gw g_hnb_gw = {
64 .config = {
65 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
66 },
67};
68
Harald Welteb534e5c2015-09-11 00:15:16 +020069struct ue_context *ue_context_by_id(uint32_t id)
70{
71 struct ue_context *ue;
72
73 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
74 if (ue->context_id == id)
75 return ue;
76 }
77 return NULL;
78
79}
80
81struct ue_context *ue_context_by_imsi(const char *imsi)
82{
83 struct ue_context *ue;
84
85 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
86 if (!strcmp(ue->imsi, imsi))
87 return ue;
88 }
89 return NULL;
90}
91
92static uint32_t get_next_ue_ctx_id(void)
93{
94 uint32_t id;
95
96 do {
97 id = g_hnb_gw.next_ue_ctx_id++;
98 } while (ue_context_by_id(id));
99
100 return id;
101}
102
103struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
104{
105 struct ue_context *ue;
106
107 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
108 if (!ue)
109 return NULL;
110
111 ue->hnb = hnb;
112 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
113 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +0200114 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200115
116 return ue;
117}
118
119void ue_context_free(struct ue_context *ue)
120{
121 llist_del(&ue->list);
122 talloc_free(ue);
123}
124
125
126
Harald Welte3f712562015-09-07 21:53:25 +0200127static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200128{
129 struct hnb_context *hnb = fd->data;
130 struct sctp_sndrcvinfo sinfo;
131 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200132 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200133 int rc;
134
135 if (!msg)
136 return -ENOMEM;
137
Harald Welte350814a2015-09-10 22:32:15 +0200138 /* we store a reference to the HomeNodeB in the msg->dest for the
139 * benefit of varoius downstream processing functions */
140 msg->dst = hnb;
141
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200142 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
143 NULL, NULL, &sinfo, &flags);
144 if (rc < 0) {
145 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200146 /* FIXME: clean up after disappeared HNB */
Daniel Willmann4267a292015-12-15 20:29:27 +0100147 close(fd->fd);
148 osmo_fd_unregister(fd);
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100149 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100150 } else if (rc == 0) {
151 LOGP(DMAIN, LOGL_ERROR, "Connection to HNB closed\n");
152 /* TODO: Remove all UEs from that connection */
153 close(fd->fd);
154 osmo_fd_unregister(fd);
155 fd->fd = -1;
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100156 rc = -1;
Daniel Willmann4267a292015-12-15 20:29:27 +0100157
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100158 goto out;
Daniel Willmann4267a292015-12-15 20:29:27 +0100159 } else {
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200160 msgb_put(msg, rc);
Daniel Willmann4267a292015-12-15 20:29:27 +0100161 }
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200162
Harald Welte17878e22015-09-08 00:09:13 +0200163 if (flags & MSG_NOTIFICATION) {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100164 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Harald Welte17878e22015-09-08 00:09:13 +0200165 msgb_free(msg);
166 return 0;
167 }
168
Harald Welte5c11c942015-09-07 19:54:49 +0200169 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
170
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200171 switch (sinfo.sinfo_ppid) {
172 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200173 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200174 rc = hnbgw_hnbap_rx(hnb, msg);
175 break;
176 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200177 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200178 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200179 break;
180 case IUH_PPI_SABP:
181 case IUH_PPI_RNA:
182 case IUH_PPI_PUA:
183 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
184 sinfo.sinfo_ppid);
185 rc = 0;
186 break;
187 default:
188 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
189 sinfo.sinfo_ppid);
190 rc = 0;
191 break;
192 }
193
Daniel Willmann269b8ac2015-12-22 16:26:48 +0100194out:
Harald Weltef2f30002015-09-08 00:09:23 +0200195 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200196 return rc;
197}
198
Harald Welte3f712562015-09-07 21:53:25 +0200199static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
200{
201 struct hnb_context *ctx = fd->data;
202 struct sctp_sndrcvinfo sinfo = {
Harald Welteffa7c0a2015-12-23 00:03:41 +0100203 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200204 .sinfo_stream = ctx->hnbap_stream,
205 };
206 int rc;
207
208 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
209 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200210 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200211 return rc;
212}
213
Harald Welte90256ba2015-12-23 20:16:36 +0100214struct hnb_context *hnb_context_alloc(struct hnb_gw *gw, int new_fd)
215{
216 struct hnb_context *ctx;
217
218 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
219 if (!ctx)
220 return NULL;
221
222 ctx->gw = gw;
223 osmo_wqueue_init(&ctx->wqueue, 16);
224 ctx->wqueue.bfd.data = ctx;
225 ctx->wqueue.bfd.fd = new_fd;
226 ctx->wqueue.bfd.when = BSC_FD_READ;
227 ctx->wqueue.read_cb = hnb_read_cb;
228 ctx->wqueue.write_cb = hnb_write_cb;
229 osmo_fd_register(&ctx->wqueue.bfd);
230
231 llist_add_tail(&ctx->list, &gw->hnb_list);
232}
233
234void hnb_context_release(struct hnb_context *ctx)
235{
236 struct hnbgw_context_map *map, *map2;
237
238 /* remove from the list of HNB contexts */
239 llist_del(&ctx->list);
240
241 /* deactivate all context maps */
242 llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) {
243 /* remove it from list, as HNB context will soon be
244 * gone. Let's hope the seccond osmo_llist_del in the
245 * map garbage collector wors fine? */
246 llist_del(&map->hnb_list);
247 context_map_deactivate(map);
248 }
249 /* FIXME: flush write queue items */
250 osmo_fd_unregister(&ctx->wqueue.bfd);
251
252 talloc_free(ctx);
253}
254
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200255/*! call-back when the listen FD has something to read */
256static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
257{
258 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200259 struct hnb_context *ctx;
260 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200261 socklen_t len = sizeof(sockaddr);
262
263 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
264 if (new_fd < 0) {
265 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
266 return new_fd;
267 }
268
269 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
270
Harald Welte90256ba2015-12-23 20:16:36 +0100271 ctx = hnb_context_alloc(gw, new_fd);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200272 if (!ctx)
273 return -ENOMEM;
274
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200275 return 0;
276}
277
Harald Welte75a4e652015-12-22 23:59:24 +0100278/* Entry point for primitives coming up from SCCP User SAP */
279static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
280{
281 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
282
283 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
284
285 /* FIXME: Do something */
286
287 msgb_free(oph->msg);
288
289 return 0;
290}
291
Harald Welteb3dae302015-08-30 12:20:09 +0200292static const struct log_info_cat log_cat[] = {
293 [DMAIN] = {
Daniel Willmann7c27f7b2015-12-15 17:41:41 +0100294 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Harald Welteb3dae302015-08-30 12:20:09 +0200295 .color = "",
296 .description = "Main program",
297 },
Daniel Willmannbded9842015-12-17 11:51:17 +0100298 [DHNBAP] = {
299 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
300 .color = "",
301 .description = "Home Node B Application Part",
302 },
Harald Welte75a4e652015-12-22 23:59:24 +0100303 [DSUA] = {
304 .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1,
305 .color = "",
306 .description = "SCCP User Adaptation",
307 },
Harald Weltef42317b2015-12-23 15:36:31 +0100308 [DRUA] = {
309 .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1,
310 .color = "",
311 .description = "RANAP User Adaptation",
312 },
313 [DRANAP] = {
314 .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1,
315 .color = "",
316 .description = "RAN Application Part",
317 },
Harald Welteb3dae302015-08-30 12:20:09 +0200318};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200319
Harald Welteb3dae302015-08-30 12:20:09 +0200320static const struct log_info hnbgw_log_info = {
321 .cat = log_cat,
322 .num_cat = ARRAY_SIZE(log_cat),
323};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200324
Harald Welteb3dae302015-08-30 12:20:09 +0200325static struct vty_app_info vty_info = {
326 .name = "OsmoHNBGW",
327 .version = "0",
328};
329
330static int daemonize = 0;
331
Harald Welte5c11c942015-09-07 19:54:49 +0200332static int sctp_sock_init(int fd)
333{
334 struct sctp_event_subscribe event;
335 int rc;
336
337 /* subscribe for all events */
338 memset((uint8_t *)&event, 1, sizeof(event));
339 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
340 &event, sizeof(event));
341
342 return rc;
343}
344
Daniel Willmann56f62732015-12-02 11:22:53 +0100345static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
346{
347 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
348 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
349 VTY_NEWLINE);
350 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
351}
352
353static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
354{
355 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
356}
357
358DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
359{
360 struct hnb_context *hnb;
361
362 llist_for_each_entry(hnb, &g_hnb_gw.hnb_list, list) {
363 vty_dump_hnb_info(vty, hnb);
364 }
365
366 return CMD_SUCCESS;
367}
368
369DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
370{
371 struct ue_context *ue;
372
373 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
374 vty_dump_ue_info(vty, ue);
375 }
376
377 return CMD_SUCCESS;
378}
379
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100380DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info")
381{
382 talloc_report_full(tall_hnb_ctx, stderr);
383 talloc_report_full(tall_ue_ctx, stderr);
384 talloc_report_full(talloc_asn1_ctx, stderr);
385
386 return CMD_SUCCESS;
387}
388
Daniel Willmann56f62732015-12-02 11:22:53 +0100389static void hnbgw_vty_init(void)
390{
391 install_element_ve(&show_hnb_cmd);
392 install_element_ve(&show_ue_cmd);
Daniel Willmann28b9ec12015-12-17 18:02:37 +0100393 install_element_ve(&show_talloc_cmd);
Daniel Willmann56f62732015-12-02 11:22:53 +0100394}
395
Harald Welteb3dae302015-08-30 12:20:09 +0200396int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200397{
Harald Welte75a4e652015-12-22 23:59:24 +0100398 struct osmo_sua_user *sua_user;
399 struct osmo_sua_link *sua_link;
Harald Welteb3dae302015-08-30 12:20:09 +0200400 int rc;
401
402 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200403 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte75a4e652015-12-22 23:59:24 +0100404 tall_sua_ctx = talloc_named_const(NULL, 0, "sua");
Harald Welte08de6382015-08-31 09:54:45 +0200405 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200406
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200407 g_hnb_gw.listen_fd.cb = listen_fd_cb;
408 g_hnb_gw.listen_fd.when = BSC_FD_READ;
409 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200410 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200411 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200412 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200413
Harald Welte90256ba2015-12-23 20:16:36 +0100414 context_map_init(&g_hnb_gw);
415
Harald Welteb3dae302015-08-30 12:20:09 +0200416 rc = osmo_init_logging(&hnbgw_log_info);
417 if (rc < 0)
418 exit(1);
419
420 vty_init(&vty_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100421 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200422
423 rc = telnet_init(NULL, &g_hnb_gw, 2323);
424 if (rc < 0) {
425 perror("Error binding VTY port");
426 exit(1);
427 }
428
Harald Welte75a4e652015-12-22 23:59:24 +0100429 osmo_sua_set_log_area(DSUA);
430 sua_user = osmo_sua_user_create(tall_sua_ctx, sccp_sap_up);
431 if (!sua_user) {
432 perror("Failed to init SUA");
433 exit(1);
434 }
435 rc = osmo_sua_client_connect(sua_user, "127.0.0.1", SUA_PORT);
436 if (rc < 0) {
437 perror("Failed to connect SUA");
438 exit(1);
439 }
440 sua_link = osmo_sua_client_get_link(sua_user);
441 if (!sua_link) {
442 perror("Failed to get SUA link");
443 exit(1);
444 }
445
Harald Welteb3dae302015-08-30 12:20:09 +0200446 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200447 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200448 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200449 if (rc < 0) {
450 perror("Error binding Iuh port");
451 exit(1);
452 }
Harald Welte5c11c942015-09-07 19:54:49 +0200453 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200454
455 if (daemonize) {
456 rc = osmo_daemonize();
457 if (rc < 0) {
458 perror("Error during daemonize");
459 exit(1);
460 }
461 }
462
463 while (1) {
464 rc = osmo_select_main(0);
465 if (rc < 0)
466 exit(3);
467 }
468
469 /* not reached */
470 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200471}