blob: 2375877a1ad1a9f05b6f94a0b815a72bedddf2f4 [file] [log] [blame]
Harald Weltef7365592020-04-15 21:48:45 +02001/* SPDX-License-Identifier: GPL-2.0 */
2#include <unistd.h>
3#include <stdint.h>
4#include <stdbool.h>
5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
8#include <assert.h>
9#include <sys/types.h>
10#include <signal.h>
11#include <errno.h>
12
13#include <pthread.h>
14
15#include <osmocom/core/linuxlist.h>
16#include <osmocom/core/talloc.h>
17#include <osmocom/core/select.h>
18#include <osmocom/core/application.h>
19#include <osmocom/core/logging.h>
20#include <osmocom/core/stats.h>
21#include <osmocom/core/rate_ctr.h>
22#include <osmocom/core/socket.h>
23#include <osmocom/vty/telnet_interface.h>
24#include <osmocom/vty/logging.h>
25#include <osmocom/vty/stats.h>
26#include <osmocom/vty/ports.h>
27#include <osmocom/vty/command.h>
28#include <osmocom/vty/misc.h>
29
30#include <osmocom/netif/stream.h>
31#include <netinet/sctp.h>
32
33#include <jansson.h>
34
35#include "internal.h"
36#include "netns.h"
37#include "gtp.h"
38
39/***********************************************************************
40 * Client (Contol/User Plane Separation) Socket
41 ***********************************************************************/
42
43#define CUPS_MSGB_SIZE 1024
44
45#define LOGCC(cc, lvl, fmt, args ...) \
46 LOGP(DUECUPS, lvl, "%s: " fmt, (cc)->sockname, ## args)
47
48struct cups_client {
49 /* member in daemon->cups_clients */
50 struct llist_head list;
51 /* back-pointer to daemon */
52 struct gtp_daemon *d;
53 /* client socket */
54 struct osmo_stream_srv *srv;
55 char sockname[OSMO_SOCK_NAME_MAXLEN];
56};
57
58/* Send JSON to a given client/connection */
59static int cups_client_tx_json(struct cups_client *cc, json_t *jtx)
60{
61 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Tx JSON");
62 char *json_str = json_dumps(jtx, JSON_SORT_KEYS);
63 char *out;
64 int json_strlen;
65
66 json_decref(jtx);
67 if (!json_str) {
68 LOGCC(cc, LOGL_ERROR, "Error encoding JSON\n");
69 return 0;
70 }
71 json_strlen = strlen(json_str);
72
73 LOGCC(cc, LOGL_DEBUG, "JSON Tx '%s'\n", json_str);
74
75 if (json_strlen > msgb_tailroom(msg)) {
76 LOGCC(cc, LOGL_ERROR, "Not enough room for JSON in msgb\n");
77 free(json_str);
78 return 0;
79 }
80
81 out = (char *)msgb_put(msg, json_strlen);
82 memcpy(out, json_str, json_strlen);
83 free(json_str);
84 osmo_stream_srv_send(cc->srv, msg);
85
86 return 0;
87}
88
89static json_t *gen_uecups_result(const char *name, const char *res)
90{
91 json_t *jres = json_object();
92 json_t *jret = json_object();
93
94 json_object_set_new(jres, "result", json_string(res));
95 json_object_set_new(jret, name, jres);
96
97 return jret;
98}
99
100static int parse_ep(struct sockaddr_storage *out, json_t *in)
101{
102 json_t *jaddr_type, *jport, *jip;
103 const char *addr_type, *ip;
104 uint8_t buf[16];
105
106 /* {"addr_type":"IPV4","ip":"31323334","Port":2152} */
107
108 if (!json_is_object(in))
109 return -EINVAL;
110
111 jaddr_type = json_object_get(in, "addr_type");
112 jport = json_object_get(in, "Port");
113 jip = json_object_get(in, "ip");
114
115 if (!jaddr_type || !jport || !jip)
116 return -EINVAL;
117
118 if (!json_is_string(jaddr_type) || !json_is_integer(jport) || !json_is_string(jip))
119 return -EINVAL;
120
121 addr_type = json_string_value(jaddr_type);
122 ip = json_string_value(jip);
123
124 memset(out, 0, sizeof(*out));
125
126 if (!strcmp(addr_type, "IPV4")) {
127 struct sockaddr_in *sin = (struct sockaddr_in *) out;
128 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
129 return -EINVAL;
130 memcpy(&sin->sin_addr, buf, 4);
131 sin->sin_family = AF_INET;
132 sin->sin_port = htons(json_integer_value(jport));
133 } else if (!strcmp(addr_type, "IPV6")) {
134 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
135 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
136 return -EINVAL;
137 memcpy(&sin6->sin6_addr, buf, 16);
138 sin6->sin6_family = AF_INET6;
139 sin6->sin6_port = htons(json_integer_value(jport));
140 } else
141 return -EINVAL;
142
143 return 0;
144}
145
146static int parse_eua(struct sockaddr_storage *out, json_t *jip, json_t *jaddr_type)
147{
148 const char *addr_type, *ip;
149 uint8_t buf[16];
150
151 if (!json_is_string(jip) || !json_is_string(jaddr_type))
152 return -EINVAL;
153
154 addr_type = json_string_value(jaddr_type);
155 ip = json_string_value(jip);
156
157 memset(out, 0, sizeof(*out));
158
159 if (!strcmp(addr_type, "IPV4")) {
160 struct sockaddr_in *sin = (struct sockaddr_in *) out;
161 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
162 return -EINVAL;
163 memcpy(&sin->sin_addr, buf, 4);
164 sin->sin_family = AF_INET;
165 } else if (!strcmp(addr_type, "IPV6")) {
166 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
167 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
168 return -EINVAL;
169 memcpy(&sin6->sin6_addr, buf, 16);
170 sin6->sin6_family = AF_INET6;
171 } else
172 return -EINVAL;
173
174 return 0;
175}
176
177
178static int parse_create_tun(struct gtp_tunnel_params *out, json_t *ctun)
179{
180 json_t *jlocal_gtp_ep, *jremote_gtp_ep;
181 json_t *jrx_teid, *jtx_teid;
182 json_t *jtun_dev_name, *jtun_netns_name;
183 json_t *juser_addr, *juser_addr_type;
184 int rc;
185
186 /* '{"create_tun":{"tx_teid":1234,"rx_teid":5678,"user_addr_type":"IPV4","user_addr":"21222324","local_gtp_ep":{"addr_type":"IPV4","ip":"31323334","Port":2152},"remote_gtp_ep":{"addr_type":"IPV4","ip":"41424344","Port":2152},"tun_dev_name":"tun23","tun_netns_name":"foo"}}' */
187
188 if (!json_is_object(ctun))
189 return -EINVAL;
190
191 /* mandatory IEs */
192 jlocal_gtp_ep = json_object_get(ctun, "local_gtp_ep");
193 jremote_gtp_ep = json_object_get(ctun, "remote_gtp_ep");
194 jrx_teid = json_object_get(ctun, "rx_teid");
195 jtx_teid = json_object_get(ctun, "tx_teid");
196 jtun_dev_name = json_object_get(ctun, "tun_dev_name");
197 juser_addr = json_object_get(ctun, "user_addr");
198 juser_addr_type = json_object_get(ctun, "user_addr_type");
199
200 if (!jlocal_gtp_ep || !jremote_gtp_ep || !jrx_teid || !jtx_teid || !jtun_dev_name ||
201 !juser_addr || !juser_addr_type)
202 return -EINVAL;
203 if (!json_is_object(jlocal_gtp_ep) || !json_is_object(jremote_gtp_ep) ||
204 !json_is_integer(jrx_teid) || !json_is_integer(jtx_teid) ||
205 !json_is_string(jtun_dev_name) ||
206 !json_is_string(juser_addr) || !json_is_string(juser_addr_type))
207 return -EINVAL;
208
209 memset(out, 0, sizeof(*out));
210
211 rc = parse_ep(&out->local_udp, jlocal_gtp_ep);
212 if (rc < 0)
213 return rc;
214 rc = parse_ep(&out->remote_udp, jremote_gtp_ep);
215 if (rc < 0)
216 return rc;
217 rc = parse_eua(&out->user_addr, juser_addr, juser_addr_type);
218 if (rc < 0)
219 return rc;
220 out->rx_teid = json_integer_value(jrx_teid);
221 out->tx_teid = json_integer_value(jtx_teid);
222 out->tun_name = talloc_strdup(out, json_string_value(jtun_dev_name));
223
224 /* optional IEs */
225 jtun_netns_name = json_object_get(ctun, "tun_netns_name");
226 if (jtun_netns_name) {
227 if (!json_is_string(jtun_netns_name))
228 return -EINVAL;
229 out->tun_netns_name = talloc_strdup(out, json_string_value(jtun_netns_name));
230 }
231
232 return 0;
233}
234
235
236static int cups_client_handle_create_tun(struct cups_client *cc, json_t *ctun)
237{
238 int rc;
239 struct gtp_tunnel_params *tpars = talloc_zero(cc, struct gtp_tunnel_params);
240 struct gtp_tunnel *t;
241
242 rc = parse_create_tun(tpars, ctun);
243 if (rc < 0) {
244 talloc_free(tpars);
245 return rc;
246 }
247
248 t = gtp_tunnel_alloc(g_daemon, tpars);
249 if (!t) {
250 LOGCC(cc, LOGL_NOTICE, "Failed to allocate tunnel\n");
251 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "ERR_NOT_FOUND"));
252 } else {
253 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "OK"));
254 }
255
256 talloc_free(tpars);
257 return 0;
258}
259
260static int cups_client_handle_destroy_tun(struct cups_client *cc, json_t *dtun)
261{
262 struct sockaddr_storage local_ep_addr;
263 json_t *jlocal_gtp_ep, *jrx_teid;
264 uint32_t rx_teid;
265 int rc;
266
267 jlocal_gtp_ep = json_object_get(dtun, "local_gtp_ep");
268 jrx_teid = json_object_get(dtun, "rx_teid");
269
270 if (!jlocal_gtp_ep || !jrx_teid)
271 return -EINVAL;
272
273 if (!json_is_object(jlocal_gtp_ep) || !json_is_integer(jrx_teid))
274 return -EINVAL;
275
276 rc = parse_ep(&local_ep_addr, jlocal_gtp_ep);
277 if (rc < 0)
278 return rc;
279 rx_teid = json_integer_value(jrx_teid);
280
281 rc = gtp_tunnel_destroy(g_daemon, &local_ep_addr, rx_teid);
282 if (rc < 0) {
283 LOGCC(cc, LOGL_NOTICE, "Failed to destroy tunnel\n");
284 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "ERR_NOT_FOUND"));
285 } else {
286 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "OK"));
287 }
288
289 return 0;
290}
291
292static int cups_client_handle_json(struct cups_client *cc, json_t *jroot)
293{
294 void *iter;
295 const char *key;
296 json_t *cmd;
297 int rc;
298
299 if (!json_is_object(jroot))
300 return -EINVAL;
301
302 iter = json_object_iter(jroot);
303 key = json_object_iter_key(iter);
304 cmd = json_object_iter_value(iter);
305 if (!iter || !key || !cmd)
306 return -EINVAL;
307
308 if (!strcmp(key, "create_tun")) {
309 rc = cups_client_handle_create_tun(cc, cmd);
310 } else if (!strcmp(key, "destroy_tun")) {
311 rc = cups_client_handle_destroy_tun(cc, cmd);
312 } else {
313 LOGCC(cc, LOGL_NOTICE, "Unknown command '%s' received\n", key);
314 return -EINVAL;
315 }
316
317 if (rc < 0) {
318 LOGCC(cc, LOGL_NOTICE, "Error %d handling '%s' command\n", rc, key);
319 char buf[64];
320 snprintf(buf, sizeof(buf), "%s_res", key);
321 cups_client_tx_json(cc, gen_uecups_result(buf, "ERR_INVALID_DATA"));
322 return -EINVAL;
323 }
324
325 return 0;
326}
327
328/* control/user plane separation per-client read cb */
329static int cups_client_read_cb(struct osmo_stream_srv *conn)
330{
331 struct osmo_fd *ofd = osmo_stream_srv_get_ofd(conn);
332 struct cups_client *cc = osmo_stream_srv_get_data(conn);
333 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Rx JSON");
334 struct sctp_sndrcvinfo sinfo;
335 json_error_t jerr;
336 json_t *jroot;
337 int flags = 0;
338 int rc = 0;
339
340 /* Read message from socket */
341 /* we cannot use osmo_stream_srv_recv() here, as we might get some out-of-band info from
342 * SCTP. FIXME: add something like osmo_stream_srv_recv_sctp() to libosmo-netif and use
343 * it here as well as in libosmo-sigtran and osmo-msc */
344 rc = sctp_recvmsg(ofd->fd, msg->tail, msgb_tailroom(msg), NULL, NULL, &sinfo,&flags);
345 if (rc <= 0) {
346 osmo_stream_srv_destroy(conn);
347 rc = -1;
348 goto out;
349 } else
350 msgb_put(msg, rc);
351
352 if (flags & MSG_NOTIFICATION) {
353 union sctp_notification *notif = (union sctp_notification *) msgb_data(msg);
354 switch (notif->sn_header.sn_type) {
355 case SCTP_SHUTDOWN_EVENT:
356 osmo_stream_srv_destroy(conn);
357 rc = -EBADF;
358 goto out;
359 default:
360 break;
361 }
362 goto out;
363 }
364
365 LOGCC(cc, LOGL_DEBUG, "Rx '%s'\n", msgb_data(msg));
366
367 /* Parse the JSON */
368 jroot = json_loadb((const char *) msgb_data(msg), msgb_length(msg), 0, &jerr);
369 if (!jroot) {
370 LOGCC(cc, LOGL_ERROR, "Error decoding JSON (%s)", jerr.text);
371 rc = -1;
372 goto out;
373 }
374
375 /* Dispatch */
376 rc = cups_client_handle_json(cc, jroot);
377
378 json_decref(jroot);
379 msgb_free(msg);
380
381 return 0;
382out:
383 msgb_free(msg);
384 return rc;
385}
386
387static int cups_client_closed_cb(struct osmo_stream_srv *conn)
388{
389 struct cups_client *cc = osmo_stream_srv_get_data(conn);
390
391 LOGCC(cc, LOGL_INFO, "UECUPS connection lost\n");
392 llist_del(&cc->list);
393 return 0;
394}
395
396
397/* the control/user plane separation server bind/accept fd */
398static int cups_accept_cb(struct osmo_stream_srv_link *link, int fd)
399{
400 struct gtp_daemon *d = osmo_stream_srv_link_get_data(link);
401 struct cups_client *cc;
402
403 cc = talloc_zero(d, struct cups_client);
404 if (!cc)
405 return -1;
406
407 osmo_sock_get_name_buf(cc->sockname, sizeof(cc->sockname), fd);
408 cc->srv = osmo_stream_srv_create(cc, link, fd, cups_client_read_cb, cups_client_closed_cb, cc);
409 if (!cc->srv) {
410 talloc_free(cc);
411 return -1;
412 }
413 LOGCC(cc, LOGL_INFO, "Accepted new UECUPS connection\n");
414
415 llist_add_tail(&cc->list, &d->cups_clients);
416
417 return 0;
418}
419
420/***********************************************************************
421 * GTP Daemon
422 ***********************************************************************/
423
424struct gtp_daemon *g_daemon;
425static int g_daemonize;
426static char *g_config_file = "osmo-gtpu-daemon.cfg";
427extern struct vty_app_info g_vty_info;
428
429static struct gtp_daemon *gtp_daemon_alloc(void *ctx)
430{
431 struct gtp_daemon *d = talloc_zero(ctx, struct gtp_daemon);
432 if (!d)
433 return NULL;
434
435 INIT_LLIST_HEAD(&d->gtp_endpoints);
436 INIT_LLIST_HEAD(&d->tun_devices);
437 INIT_LLIST_HEAD(&d->gtp_tunnels);
438 pthread_rwlock_init(&d->rwlock, NULL);
439 d->main_thread = pthread_self();
440
441 INIT_LLIST_HEAD(&d->cups_clients);
442
443 d->cfg.cups_local_ip = talloc_strdup(d, "localhost");
444 d->cfg.cups_local_port = 4300;
445
446 return d;
447}
448
449static const struct log_info_cat log_categories[] = {
450 [DTUN] = {
451 .name ="DTUN",
452 .description = "Tunnel interface (tun device)",
453 .enabled = 1, .loglevel = LOGL_INFO,
454 },
455 [DEP] = {
456 .name = "DEP",
457 .description = "GTP endpoint (UDP socket)",
458 .enabled = 1, .loglevel = LOGL_INFO,
459 },
460 [DGT] = {
461 .name = "DGT",
462 .description = "GTP tunnel (session)",
463 .enabled = 1, .loglevel = LOGL_INFO,
464 },
465 [DUECUPS] = {
466 .name = "DUECUPS",
467 .description = "UE Control User Plane Separation",
468 .enabled = 1, .loglevel = LOGL_DEBUG,
469 },
470
471};
472
473static const struct log_info log_info = {
474 .cat = log_categories,
475 .num_cat = ARRAY_SIZE(log_categories),
476};
477
478int main(int argc, char **argv)
479{
480 void *ctx = talloc_named_const(NULL, 0, "root");
481 int rc;
482
483 g_vty_info.tall_ctx = ctx;
484
485 osmo_init_ignore_signals();
486 osmo_init_logging2(ctx, &log_info);
487
488 g_daemon = gtp_daemon_alloc(ctx);
489 OSMO_ASSERT(g_daemon);
490
491 osmo_stats_init(ctx);
492 vty_init(&g_vty_info);
493 logging_vty_add_cmds();
494 osmo_talloc_vty_add_cmds();
495 osmo_stats_vty_add_cmds();
496 rate_ctr_init(ctx);
497 gtpud_vty_init();
498
499 init_netns();
500
501 rc = vty_read_config_file(g_config_file, NULL);
502 if (rc < 0) {
503 fprintf(stderr, "Failed to open config file: '%s'\n", g_config_file);
504 exit(2);
505 }
506
507 rc = telnet_init_dynif(ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN);
508 if (rc < 0)
509 exit(1);
510
511 g_daemon->cups_link = osmo_stream_srv_link_create(g_daemon);
512 if (!g_daemon->cups_link) {
513 fprintf(stderr, "Failed to create CUPS socket %s:%u (%s)\n",
514 g_daemon->cfg.cups_local_ip, g_daemon->cfg.cups_local_port, strerror(errno));
515 exit(1);
516 }
517
518 /* UECUPS socket for control from control plane side */
519 osmo_stream_srv_link_set_nodelay(g_daemon->cups_link, true);
520 osmo_stream_srv_link_set_addr(g_daemon->cups_link, g_daemon->cfg.cups_local_ip);
521 osmo_stream_srv_link_set_port(g_daemon->cups_link, g_daemon->cfg.cups_local_port);
522 osmo_stream_srv_link_set_proto(g_daemon->cups_link, IPPROTO_SCTP);
523 osmo_stream_srv_link_set_data(g_daemon->cups_link, g_daemon);
524 osmo_stream_srv_link_set_accept_cb(g_daemon->cups_link, cups_accept_cb);
525 osmo_stream_srv_link_open(g_daemon->cups_link);
526
527 if (g_daemonize) {
528 rc = osmo_daemonize();
529 if (rc < 0) {
530 perror("Error during daemonize");
531 exit(1);
532 }
533 }
534
535 while (1) {
536 osmo_select_main(0);
537 }
538}