blob: d26f166274f0c77017b96275c1aa51d1e047a8f3 [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>
Harald Welte1d2c39d2015-09-11 17:49:37 +020011#include <netinet/in.h>
Harald Welteb3dae302015-08-30 12:20:09 +020012#include <netinet/sctp.h>
Harald Welte1d2c39d2015-09-11 17:49:37 +020013#include <arpa/inet.h>
Harald Welteb3dae302015-08-30 12:20:09 +020014
15#include <osmocom/core/application.h>
16#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020017#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020018#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020019#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020020#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020021#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020022
23#include <osmocom/vty/telnet_interface.h>
24#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020025
26#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020027#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020028#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020029
30static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020031static void *tall_ue_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020032void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020033
34struct hnb_gw g_hnb_gw = {
35 .config = {
36 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
37 },
38};
39
Harald Welteb534e5c2015-09-11 00:15:16 +020040struct ue_context *ue_context_by_id(uint32_t id)
41{
42 struct ue_context *ue;
43
44 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
45 if (ue->context_id == id)
46 return ue;
47 }
48 return NULL;
49
50}
51
52struct ue_context *ue_context_by_imsi(const char *imsi)
53{
54 struct ue_context *ue;
55
56 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
57 if (!strcmp(ue->imsi, imsi))
58 return ue;
59 }
60 return NULL;
61}
62
63static uint32_t get_next_ue_ctx_id(void)
64{
65 uint32_t id;
66
67 do {
68 id = g_hnb_gw.next_ue_ctx_id++;
69 } while (ue_context_by_id(id));
70
71 return id;
72}
73
74struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
75{
76 struct ue_context *ue;
77
78 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
79 if (!ue)
80 return NULL;
81
82 ue->hnb = hnb;
83 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
84 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +020085 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +020086
87 return ue;
88}
89
90void ue_context_free(struct ue_context *ue)
91{
92 llist_del(&ue->list);
93 talloc_free(ue);
94}
95
96
97
Harald Welte3f712562015-09-07 21:53:25 +020098static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +020099{
100 struct hnb_context *hnb = fd->data;
101 struct sctp_sndrcvinfo sinfo;
102 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200103 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200104 int rc;
105
106 if (!msg)
107 return -ENOMEM;
108
Harald Welte350814a2015-09-10 22:32:15 +0200109 /* we store a reference to the HomeNodeB in the msg->dest for the
110 * benefit of varoius downstream processing functions */
111 msg->dst = hnb;
112
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200113 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
114 NULL, NULL, &sinfo, &flags);
115 if (rc < 0) {
116 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200117 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200118 return rc;
119 } else
120 msgb_put(msg, rc);
121
Harald Welte17878e22015-09-08 00:09:13 +0200122 if (flags & MSG_NOTIFICATION) {
123 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
124 msgb_free(msg);
125 return 0;
126 }
127
Harald Welte5c11c942015-09-07 19:54:49 +0200128 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
129
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200130 switch (sinfo.sinfo_ppid) {
131 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200132 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200133 rc = hnbgw_hnbap_rx(hnb, msg);
134 break;
135 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200136 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200137 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200138 break;
139 case IUH_PPI_SABP:
140 case IUH_PPI_RNA:
141 case IUH_PPI_PUA:
142 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
143 sinfo.sinfo_ppid);
144 rc = 0;
145 break;
146 default:
147 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
148 sinfo.sinfo_ppid);
149 rc = 0;
150 break;
151 }
152
Harald Weltef2f30002015-09-08 00:09:23 +0200153 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200154 return rc;
155}
156
Harald Welte3f712562015-09-07 21:53:25 +0200157static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
158{
159 struct hnb_context *ctx = fd->data;
160 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200161 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200162 .sinfo_stream = ctx->hnbap_stream,
163 };
164 int rc;
165
166 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
167 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200168 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200169 return rc;
170}
171
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200172/*! call-back when the listen FD has something to read */
173static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
174{
175 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200176 struct hnb_context *ctx;
177 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200178 socklen_t len = sizeof(sockaddr);
179
180 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
181 if (new_fd < 0) {
182 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
183 return new_fd;
184 }
185
186 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
187
188 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
189 if (!ctx)
190 return -ENOMEM;
191
192 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200193 osmo_wqueue_init(&ctx->wqueue, 16);
194 ctx->wqueue.bfd.data = ctx;
195 ctx->wqueue.bfd.fd = new_fd;
196 ctx->wqueue.bfd.when = BSC_FD_READ;
197 ctx->wqueue.read_cb = hnb_read_cb;
198 ctx->wqueue.write_cb = hnb_write_cb;
199 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200200
201 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200202
203 return 0;
204}
205
Harald Welteb3dae302015-08-30 12:20:09 +0200206static const struct log_info_cat log_cat[] = {
207 [DMAIN] = {
208 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
209 .color = "",
210 .description = "Main program",
211 },
212};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200213
Harald Welteb3dae302015-08-30 12:20:09 +0200214static const struct log_info hnbgw_log_info = {
215 .cat = log_cat,
216 .num_cat = ARRAY_SIZE(log_cat),
217};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200218
Harald Welteb3dae302015-08-30 12:20:09 +0200219static struct vty_app_info vty_info = {
220 .name = "OsmoHNBGW",
221 .version = "0",
222};
223
224static int daemonize = 0;
225
Harald Welte5c11c942015-09-07 19:54:49 +0200226static int sctp_sock_init(int fd)
227{
228 struct sctp_event_subscribe event;
229 int rc;
230
231 /* subscribe for all events */
232 memset((uint8_t *)&event, 1, sizeof(event));
233 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
234 &event, sizeof(event));
235
236 return rc;
237}
238
Harald Welteb3dae302015-08-30 12:20:09 +0200239int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200240{
Harald Welteb3dae302015-08-30 12:20:09 +0200241 int rc;
242
243 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200244 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte08de6382015-08-31 09:54:45 +0200245 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200246
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200247 g_hnb_gw.listen_fd.cb = listen_fd_cb;
248 g_hnb_gw.listen_fd.when = BSC_FD_READ;
249 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200250 g_hnb_gw.next_ue_ctx_id = 23;
Harald Welte08a793b2015-09-07 19:53:46 +0200251 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200252 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200253
Harald Welteb3dae302015-08-30 12:20:09 +0200254 rc = osmo_init_logging(&hnbgw_log_info);
255 if (rc < 0)
256 exit(1);
257
258 vty_init(&vty_info);
259
260 rc = telnet_init(NULL, &g_hnb_gw, 2323);
261 if (rc < 0) {
262 perror("Error binding VTY port");
263 exit(1);
264 }
265
266 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200267 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200268 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200269 if (rc < 0) {
270 perror("Error binding Iuh port");
271 exit(1);
272 }
Harald Welte5c11c942015-09-07 19:54:49 +0200273 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200274
275 if (daemonize) {
276 rc = osmo_daemonize();
277 if (rc < 0) {
278 perror("Error during daemonize");
279 exit(1);
280 }
281 }
282
283 while (1) {
284 rc = osmo_select_main(0);
285 if (rc < 0)
286 exit(3);
287 }
288
289 /* not reached */
290 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200291}