blob: 0f05033cc16e3d89a7e869b6049bb215f75159a3 [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
47#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020048#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020049#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020050
51static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020052static void *tall_ue_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020053void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020054
55struct hnb_gw g_hnb_gw = {
56 .config = {
57 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
58 },
59};
60
Harald Welteb534e5c2015-09-11 00:15:16 +020061struct ue_context *ue_context_by_id(uint32_t id)
62{
63 struct ue_context *ue;
64
65 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
66 if (ue->context_id == id)
67 return ue;
68 }
69 return NULL;
70
71}
72
73struct ue_context *ue_context_by_imsi(const char *imsi)
74{
75 struct ue_context *ue;
76
77 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
78 if (!strcmp(ue->imsi, imsi))
79 return ue;
80 }
81 return NULL;
82}
83
84static uint32_t get_next_ue_ctx_id(void)
85{
86 uint32_t id;
87
88 do {
89 id = g_hnb_gw.next_ue_ctx_id++;
90 } while (ue_context_by_id(id));
91
92 return id;
93}
94
95struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
96{
97 struct ue_context *ue;
98
99 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
100 if (!ue)
101 return NULL;
102
103 ue->hnb = hnb;
104 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
105 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +0200106 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200107
108 return ue;
109}
110
111void ue_context_free(struct ue_context *ue)
112{
113 llist_del(&ue->list);
114 talloc_free(ue);
115}
116
117
118
Harald Welte3f712562015-09-07 21:53:25 +0200119static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200120{
121 struct hnb_context *hnb = fd->data;
122 struct sctp_sndrcvinfo sinfo;
123 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200124 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200125 int rc;
126
127 if (!msg)
128 return -ENOMEM;
129
Harald Welte350814a2015-09-10 22:32:15 +0200130 /* we store a reference to the HomeNodeB in the msg->dest for the
131 * benefit of varoius downstream processing functions */
132 msg->dst = hnb;
133
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200134 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
135 NULL, NULL, &sinfo, &flags);
136 if (rc < 0) {
137 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200138 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200139 return rc;
140 } else
141 msgb_put(msg, rc);
142
Harald Welte17878e22015-09-08 00:09:13 +0200143 if (flags & MSG_NOTIFICATION) {
144 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
145 msgb_free(msg);
146 return 0;
147 }
148
Harald Welte5c11c942015-09-07 19:54:49 +0200149 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
150
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200151 switch (sinfo.sinfo_ppid) {
152 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200153 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200154 rc = hnbgw_hnbap_rx(hnb, msg);
155 break;
156 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200157 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200158 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200159 break;
160 case IUH_PPI_SABP:
161 case IUH_PPI_RNA:
162 case IUH_PPI_PUA:
163 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
164 sinfo.sinfo_ppid);
165 rc = 0;
166 break;
167 default:
168 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
169 sinfo.sinfo_ppid);
170 rc = 0;
171 break;
172 }
173
Harald Weltef2f30002015-09-08 00:09:23 +0200174 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200175 return rc;
176}
177
Harald Welte3f712562015-09-07 21:53:25 +0200178static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
179{
180 struct hnb_context *ctx = fd->data;
181 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200182 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200183 .sinfo_stream = ctx->hnbap_stream,
184 };
185 int rc;
186
187 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
188 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200189 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200190 return rc;
191}
192
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200193/*! call-back when the listen FD has something to read */
194static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
195{
196 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200197 struct hnb_context *ctx;
198 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200199 socklen_t len = sizeof(sockaddr);
200
201 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
202 if (new_fd < 0) {
203 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
204 return new_fd;
205 }
206
207 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
208
209 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
210 if (!ctx)
211 return -ENOMEM;
212
213 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200214 osmo_wqueue_init(&ctx->wqueue, 16);
215 ctx->wqueue.bfd.data = ctx;
216 ctx->wqueue.bfd.fd = new_fd;
217 ctx->wqueue.bfd.when = BSC_FD_READ;
218 ctx->wqueue.read_cb = hnb_read_cb;
219 ctx->wqueue.write_cb = hnb_write_cb;
220 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200221
222 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200223
224 return 0;
225}
226
Harald Welteb3dae302015-08-30 12:20:09 +0200227static const struct log_info_cat log_cat[] = {
228 [DMAIN] = {
229 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
230 .color = "",
231 .description = "Main program",
232 },
233};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200234
Harald Welteb3dae302015-08-30 12:20:09 +0200235static const struct log_info hnbgw_log_info = {
236 .cat = log_cat,
237 .num_cat = ARRAY_SIZE(log_cat),
238};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200239
Harald Welteb3dae302015-08-30 12:20:09 +0200240static struct vty_app_info vty_info = {
241 .name = "OsmoHNBGW",
242 .version = "0",
243};
244
245static int daemonize = 0;
246
Harald Welte5c11c942015-09-07 19:54:49 +0200247static int sctp_sock_init(int fd)
248{
249 struct sctp_event_subscribe event;
250 int rc;
251
252 /* subscribe for all events */
253 memset((uint8_t *)&event, 1, sizeof(event));
254 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
255 &event, sizeof(event));
256
257 return rc;
258}
259
Daniel Willmann56f62732015-12-02 11:22:53 +0100260static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
261{
262 vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info,
263 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid,
264 VTY_NEWLINE);
265 vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE);
266}
267
268static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue)
269{
270 vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE);
271}
272
273DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB")
274{
275 struct hnb_context *hnb;
276
277 llist_for_each_entry(hnb, &g_hnb_gw.hnb_list, list) {
278 vty_dump_hnb_info(vty, hnb);
279 }
280
281 return CMD_SUCCESS;
282}
283
284DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE")
285{
286 struct ue_context *ue;
287
288 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
289 vty_dump_ue_info(vty, ue);
290 }
291
292 return CMD_SUCCESS;
293}
294
295static void hnbgw_vty_init(void)
296{
297 install_element_ve(&show_hnb_cmd);
298 install_element_ve(&show_ue_cmd);
299}
300
Harald Welteb3dae302015-08-30 12:20:09 +0200301int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200302{
Harald Welteb3dae302015-08-30 12:20:09 +0200303 int rc;
304
305 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200306 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte08de6382015-08-31 09:54:45 +0200307 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200308
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200309 g_hnb_gw.listen_fd.cb = listen_fd_cb;
310 g_hnb_gw.listen_fd.when = BSC_FD_READ;
311 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200312 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200313 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200314 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200315
Harald Welteb3dae302015-08-30 12:20:09 +0200316 rc = osmo_init_logging(&hnbgw_log_info);
317 if (rc < 0)
318 exit(1);
319
320 vty_init(&vty_info);
Daniel Willmann56f62732015-12-02 11:22:53 +0100321 hnbgw_vty_init();
Harald Welteb3dae302015-08-30 12:20:09 +0200322
323 rc = telnet_init(NULL, &g_hnb_gw, 2323);
324 if (rc < 0) {
325 perror("Error binding VTY port");
326 exit(1);
327 }
328
329 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200330 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200331 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200332 if (rc < 0) {
333 perror("Error binding Iuh port");
334 exit(1);
335 }
Harald Welte5c11c942015-09-07 19:54:49 +0200336 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200337
338 if (daemonize) {
339 rc = osmo_daemonize();
340 if (rc < 0) {
341 perror("Error during daemonize");
342 exit(1);
343 }
344 }
345
346 while (1) {
347 rc = osmo_select_main(0);
348 if (rc < 0)
349 exit(3);
350 }
351
352 /* not reached */
353 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200354}