blob: ac0d03c9a65e21827d3fbe09f4b9781d9e607d6a [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>
12
13#include <osmocom/core/application.h>
14#include <osmocom/core/talloc.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020015#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020016#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020017#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020018#include <osmocom/core/msgb.h>
Harald Welte3f712562015-09-07 21:53:25 +020019#include <osmocom/core/write_queue.h>
Harald Welteb3dae302015-08-30 12:20:09 +020020
21#include <osmocom/vty/telnet_interface.h>
22#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020023
24#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020025#include "hnbgw_hnbap.h"
Harald Welte318e4d52015-09-10 18:47:08 +020026#include "hnbgw_rua.h"
Harald Welteb3dae302015-08-30 12:20:09 +020027
28static void *tall_hnb_ctx;
Harald Welteb534e5c2015-09-11 00:15:16 +020029static void *tall_ue_ctx;
Harald Welte08de6382015-08-31 09:54:45 +020030void *talloc_asn1_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020031
32struct hnb_gw g_hnb_gw = {
33 .config = {
34 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
35 },
36};
37
Harald Welteb534e5c2015-09-11 00:15:16 +020038struct ue_context *ue_context_by_id(uint32_t id)
39{
40 struct ue_context *ue;
41
42 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
43 if (ue->context_id == id)
44 return ue;
45 }
46 return NULL;
47
48}
49
50struct ue_context *ue_context_by_imsi(const char *imsi)
51{
52 struct ue_context *ue;
53
54 llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) {
55 if (!strcmp(ue->imsi, imsi))
56 return ue;
57 }
58 return NULL;
59}
60
61static uint32_t get_next_ue_ctx_id(void)
62{
63 uint32_t id;
64
65 do {
66 id = g_hnb_gw.next_ue_ctx_id++;
67 } while (ue_context_by_id(id));
68
69 return id;
70}
71
72struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi)
73{
74 struct ue_context *ue;
75
76 ue = talloc_zero(tall_hnb_ctx, struct ue_context);
77 if (!ue)
78 return NULL;
79
80 ue->hnb = hnb;
81 strncpy(ue->imsi, imsi, sizeof(ue->imsi));
82 ue->context_id = get_next_ue_ctx_id();
Harald Welte256c67a2015-09-11 01:22:29 +020083 llist_add_tail(&ue->list, &g_hnb_gw.ue_list);
Harald Welteb534e5c2015-09-11 00:15:16 +020084
85 return ue;
86}
87
88void ue_context_free(struct ue_context *ue)
89{
90 llist_del(&ue->list);
91 talloc_free(ue);
92}
93
94
95
Harald Welte3f712562015-09-07 21:53:25 +020096static int hnb_read_cb(struct osmo_fd *fd)
Harald Weltea2e6a7a2015-08-29 21:47:39 +020097{
98 struct hnb_context *hnb = fd->data;
99 struct sctp_sndrcvinfo sinfo;
100 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
Harald Welte3f712562015-09-07 21:53:25 +0200101 int flags = 0;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200102 int rc;
103
104 if (!msg)
105 return -ENOMEM;
106
Harald Welte350814a2015-09-10 22:32:15 +0200107 /* we store a reference to the HomeNodeB in the msg->dest for the
108 * benefit of varoius downstream processing functions */
109 msg->dst = hnb;
110
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200111 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
112 NULL, NULL, &sinfo, &flags);
113 if (rc < 0) {
114 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200115 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200116 return rc;
117 } else
118 msgb_put(msg, rc);
119
Harald Welte17878e22015-09-08 00:09:13 +0200120 if (flags & MSG_NOTIFICATION) {
121 LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n");
122 msgb_free(msg);
123 return 0;
124 }
125
Harald Welte5c11c942015-09-07 19:54:49 +0200126 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
127
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200128 switch (sinfo.sinfo_ppid) {
129 case IUH_PPI_HNBAP:
Harald Welte3f712562015-09-07 21:53:25 +0200130 hnb->hnbap_stream = sinfo.sinfo_stream;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200131 rc = hnbgw_hnbap_rx(hnb, msg);
132 break;
133 case IUH_PPI_RUA:
Harald Welte3f712562015-09-07 21:53:25 +0200134 hnb->rua_stream = sinfo.sinfo_stream;
Harald Welte318e4d52015-09-10 18:47:08 +0200135 rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200136 break;
137 case IUH_PPI_SABP:
138 case IUH_PPI_RNA:
139 case IUH_PPI_PUA:
140 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
141 sinfo.sinfo_ppid);
142 rc = 0;
143 break;
144 default:
145 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
146 sinfo.sinfo_ppid);
147 rc = 0;
148 break;
149 }
150
Harald Weltef2f30002015-09-08 00:09:23 +0200151 msgb_free(msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200152 return rc;
153}
154
Harald Welte3f712562015-09-07 21:53:25 +0200155static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
156{
157 struct hnb_context *ctx = fd->data;
158 struct sctp_sndrcvinfo sinfo = {
Harald Weltecfcc1e62015-09-07 22:39:56 +0200159 .sinfo_ppid = htonl(msgb_ppid(msg)),
Harald Welte3f712562015-09-07 21:53:25 +0200160 .sinfo_stream = ctx->hnbap_stream,
161 };
162 int rc;
163
164 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
165 &sinfo, 0);
Harald Welte9e270b42015-09-07 22:41:26 +0200166 /* we don't need to msgb_free(), write_queue does this for us */
Harald Welte3f712562015-09-07 21:53:25 +0200167 return rc;
168}
169
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200170/*! call-back when the listen FD has something to read */
171static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
172{
173 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +0200174 struct hnb_context *ctx;
175 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200176 socklen_t len = sizeof(sockaddr);
177
178 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
179 if (new_fd < 0) {
180 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
181 return new_fd;
182 }
183
184 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
185
186 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
187 if (!ctx)
188 return -ENOMEM;
189
190 ctx->gw = gw;
Harald Welte3f712562015-09-07 21:53:25 +0200191 osmo_wqueue_init(&ctx->wqueue, 16);
192 ctx->wqueue.bfd.data = ctx;
193 ctx->wqueue.bfd.fd = new_fd;
194 ctx->wqueue.bfd.when = BSC_FD_READ;
195 ctx->wqueue.read_cb = hnb_read_cb;
196 ctx->wqueue.write_cb = hnb_write_cb;
197 osmo_fd_register(&ctx->wqueue.bfd);
Harald Welteb3dae302015-08-30 12:20:09 +0200198
199 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200200
201 return 0;
202}
203
Harald Welteb3dae302015-08-30 12:20:09 +0200204static const struct log_info_cat log_cat[] = {
205 [DMAIN] = {
206 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
207 .color = "",
208 .description = "Main program",
209 },
210};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200211
Harald Welteb3dae302015-08-30 12:20:09 +0200212static const struct log_info hnbgw_log_info = {
213 .cat = log_cat,
214 .num_cat = ARRAY_SIZE(log_cat),
215};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200216
Harald Welteb3dae302015-08-30 12:20:09 +0200217static struct vty_app_info vty_info = {
218 .name = "OsmoHNBGW",
219 .version = "0",
220};
221
222static int daemonize = 0;
223
Harald Welte5c11c942015-09-07 19:54:49 +0200224static int sctp_sock_init(int fd)
225{
226 struct sctp_event_subscribe event;
227 int rc;
228
229 /* subscribe for all events */
230 memset((uint8_t *)&event, 1, sizeof(event));
231 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
232 &event, sizeof(event));
233
234 return rc;
235}
236
Harald Welteb3dae302015-08-30 12:20:09 +0200237int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200238{
Harald Welteb3dae302015-08-30 12:20:09 +0200239 int rc;
240
241 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Harald Welteb534e5c2015-09-11 00:15:16 +0200242 tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context");
Harald Welte08de6382015-08-31 09:54:45 +0200243 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
Harald Welteb3dae302015-08-30 12:20:09 +0200244
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200245 g_hnb_gw.listen_fd.cb = listen_fd_cb;
246 g_hnb_gw.listen_fd.when = BSC_FD_READ;
247 g_hnb_gw.listen_fd.data = &g_hnb_gw;
Harald Welte08a793b2015-09-07 19:53:46 +0200248 INIT_LLIST_HEAD(&g_hnb_gw.hnb_list);
Harald Welteb534e5c2015-09-11 00:15:16 +0200249 INIT_LLIST_HEAD(&g_hnb_gw.ue_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200250
Harald Welteb3dae302015-08-30 12:20:09 +0200251 rc = osmo_init_logging(&hnbgw_log_info);
252 if (rc < 0)
253 exit(1);
254
255 vty_init(&vty_info);
256
257 rc = telnet_init(NULL, &g_hnb_gw, 2323);
258 if (rc < 0) {
259 perror("Error binding VTY port");
260 exit(1);
261 }
262
263 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Welte1c0f5382015-09-07 18:46:58 +0200264 IPPROTO_SCTP, NULL,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200265 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200266 if (rc < 0) {
267 perror("Error binding Iuh port");
268 exit(1);
269 }
Harald Welte5c11c942015-09-07 19:54:49 +0200270 sctp_sock_init(g_hnb_gw.listen_fd.fd);
Harald Welteb3dae302015-08-30 12:20:09 +0200271
272 if (daemonize) {
273 rc = osmo_daemonize();
274 if (rc < 0) {
275 perror("Error during daemonize");
276 exit(1);
277 }
278 }
279
280 while (1) {
281 rc = osmo_select_main(0);
282 if (rc < 0)
283 exit(3);
284 }
285
286 /* not reached */
287 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200288}