blob: b50cd9db559f8d0a43513a2616d823fc51f286b3 [file] [log] [blame]
Harald Welteb3dae302015-08-30 12:20:09 +02001#include <unistd.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <getopt.h>
6#include <errno.h>
7#include <signal.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +02008
Harald Welteb3dae302015-08-30 12:20:09 +02009#include <sys/types.h>
10#include <sys/socket.h>
11#include <netinet/sctp.h>
Harald Welte10dfc5a2015-09-11 01:34:45 +020012//#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020013
14#include <osmocom/core/application.h>
15#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020016#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020017#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020018#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020019#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020020#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020021
22#include <osmocom/vty/telnet_interface.h>
23#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020024
25#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020026#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020027#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020028
29static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020030static void *tall_ue_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020031void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020032
33struct hnb_gw g_hnb_gw = {
34 .config = {
35 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
36 },
37};
38
Harald Welteb534e5c2015-09-11 00:15:16 +020039struct ue_context *ue_context_by_id(uint32_t id)
40{
41 struct ue_context *ue;
42
43 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
44 if (ue->context_id == id)
45 return ue;
46 }
47 return NULL;
48
49}
50
51struct ue_context *ue_context_by_imsi(const char *imsi)
52{
53 struct ue_context *ue;
54
55 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
56 if (!strcmp(ue->imsi, imsi))
57 return ue;
58 }
59 return NULL;
60}
61
62static uint32_t get_next_ue_ctx_id(void)
63{
64 uint32_t id;
65
66 do {
67 id = g_hnb_gw.next_ue_ctx_id++;
68 } while (ue_context_by_id(id));
69
70 return id;
71}
72
73struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
74{
75 struct ue_context *ue;
76
77 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
78 if (!ue)
79 return NULL;
80
81 ue->hnb = hnb;
82 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
83 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +020084 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +020085
86 return ue;
87}
88
89void ue_context_free(struct ue_context *ue)
90{
91 llist_del(&ue->list);
92 talloc_free(ue);
93}
94
95
96
Harald Welte3f712562015-09-07 21:53:25 +020097static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +020098{
99 struct hnb_context *hnb = fd->data;
100 struct sctp_sndrcvinfo sinfo;
101 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200102 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200103 int rc;
104
105 if (!msg)
106 return -ENOMEM;
107
Harald Welte350814a2015-09-10 22:32:15 +0200108 /* we store a reference to the HomeNodeB in the msg->dest for the
109 * benefit of varoius downstream processing functions */
110 msg->dst = hnb;
111
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200112 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
113 NULL, NULL, &sinfo, &flags);
114 if (rc < 0) {
115 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200116 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200117 return rc;
118 } else
119 msgb_put(msg, rc);
120
Harald Welte17878e22015-09-08 00:09:13 +0200121 if (flags & MSG_NOTIFICATION) {
122 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
123 msgb_free(msg);
124 return 0;
125 }
126
Harald Welte5c11c942015-09-07 19:54:49 +0200127 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
128
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200129 switch (sinfo.sinfo_ppid) {
130 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200131 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200132 rc = hnbgw_hnbap_rx(hnb, msg);
133 break;
134 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200135 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200136 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200137 break;
138 case IUH_PPI_SABP:
139 case IUH_PPI_RNA:
140 case IUH_PPI_PUA:
141 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
142 sinfo.sinfo_ppid);
143 rc = 0;
144 break;
145 default:
146 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
147 sinfo.sinfo_ppid);
148 rc = 0;
149 break;
150 }
151
Harald Weltef2f30002015-09-08 00:09:23 +0200152 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200153 return rc;
154}
155
Harald Welte3f712562015-09-07 21:53:25 +0200156static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
157{
158 struct hnb_context *ctx = fd->data;
159 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200160 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200161 .sinfo_stream = ctx->hnbap_stream,
162 };
163 int rc;
164
165 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
166 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200167 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200168 return rc;
169}
170
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200171/*! call-back when the listen FD has something to read */
172static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
173{
174 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200175 struct hnb_context *ctx;
176 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200177 socklen_t len = sizeof(sockaddr);
178
179 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
180 if (new_fd < 0) {
181 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
182 return new_fd;
183 }
184
185 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
186
187 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
188 if (!ctx)
189 return -ENOMEM;
190
191 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200192 osmo_wqueue_init(&ctx->wqueue, 16);
193 ctx->wqueue.bfd.data = ctx;
194 ctx->wqueue.bfd.fd = new_fd;
195 ctx->wqueue.bfd.when = BSC_FD_READ;
196 ctx->wqueue.read_cb = hnb_read_cb;
197 ctx->wqueue.write_cb = hnb_write_cb;
198 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200199
200 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200201
202 return 0;
203}
204
Harald Welteb3dae302015-08-30 12:20:09 +0200205static const struct log_info_cat log_cat[] = {
206 [DMAIN] = {
207 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
208 .color = "",
209 .description = "Main program",
210 },
211};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200212
Harald Welteb3dae302015-08-30 12:20:09 +0200213static const struct log_info hnbgw_log_info = {
214 .cat = log_cat,
215 .num_cat = ARRAY_SIZE(log_cat),
216};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200217
Harald Welteb3dae302015-08-30 12:20:09 +0200218static struct vty_app_info vty_info = {
219 .name = "OsmoHNBGW",
220 .version = "0",
221};
222
223static int daemonize = 0;
224
Harald Welte5c11c942015-09-07 19:54:49 +0200225static int sctp_sock_init(int fd)
226{
227 struct sctp_event_subscribe event;
228 int rc;
229
230 /* subscribe for all events */
231 memset((uint8_t *)&event, 1, sizeof(event));
232 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
233 &event, sizeof(event));
234
235 return rc;
236}
237
Harald Welteb3dae302015-08-30 12:20:09 +0200238int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200239{
Harald Welteb3dae302015-08-30 12:20:09 +0200240 int rc;
241
242 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200243 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte08de6382015-08-31 09:54:45 +0200244 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200245
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200246 g_hnb_gw.listen_fd.cb = listen_fd_cb;
247 g_hnb_gw.listen_fd.when = BSC_FD_READ;
248 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200249 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200250 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200251 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200252
Harald Welteb3dae302015-08-30 12:20:09 +0200253 rc = osmo_init_logging(&hnbgw_log_info);
254 if (rc < 0)
255 exit(1);
256
257 vty_init(&vty_info);
258
259 rc = telnet_init(NULL, &g_hnb_gw, 2323);
260 if (rc < 0) {
261 perror("Error binding VTY port");
262 exit(1);
263 }
264
265 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200266 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200267 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200268 if (rc < 0) {
269 perror("Error binding Iuh port");
270 exit(1);
271 }
Harald Welte5c11c942015-09-07 19:54:49 +0200272 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200273
274 if (daemonize) {
275 rc = osmo_daemonize();
276 if (rc < 0) {
277 perror("Error during daemonize");
278 exit(1);
279 }
280 }
281
282 while (1) {
283 rc = osmo_select_main(0);
284 if (rc < 0)
285 exit(3);
286 }
287
288 /* not reached */
289 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200290}