blob: fec6f8499429d36f9f842652cae52584a8086d87 [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>
Harald Weltef9bb1772020-04-21 22:46:34 +020010#include <sys/wait.h>
Harald Welte24557a72020-04-17 22:08:29 +020011#include <sys/signalfd.h>
Harald Weltef7365592020-04-15 21:48:45 +020012#include <signal.h>
13#include <errno.h>
14
15#include <pthread.h>
16
17#include <osmocom/core/linuxlist.h>
18#include <osmocom/core/talloc.h>
19#include <osmocom/core/select.h>
20#include <osmocom/core/application.h>
21#include <osmocom/core/logging.h>
22#include <osmocom/core/stats.h>
23#include <osmocom/core/rate_ctr.h>
24#include <osmocom/core/socket.h>
Harald Welte24557a72020-04-17 22:08:29 +020025#include <osmocom/core/exec.h>
Harald Weltef7365592020-04-15 21:48:45 +020026#include <osmocom/vty/telnet_interface.h>
27#include <osmocom/vty/logging.h>
28#include <osmocom/vty/stats.h>
29#include <osmocom/vty/ports.h>
30#include <osmocom/vty/command.h>
31#include <osmocom/vty/misc.h>
32
33#include <osmocom/netif/stream.h>
34#include <netinet/sctp.h>
35
36#include <jansson.h>
37
38#include "internal.h"
39#include "netns.h"
40#include "gtp.h"
41
42/***********************************************************************
43 * Client (Contol/User Plane Separation) Socket
44 ***********************************************************************/
45
Harald Welte24557a72020-04-17 22:08:29 +020046#include <pwd.h>
47
Harald Weltef7365592020-04-15 21:48:45 +020048#define CUPS_MSGB_SIZE 1024
49
50#define LOGCC(cc, lvl, fmt, args ...) \
51 LOGP(DUECUPS, lvl, "%s: " fmt, (cc)->sockname, ## args)
52
53struct cups_client {
54 /* member in daemon->cups_clients */
55 struct llist_head list;
56 /* back-pointer to daemon */
57 struct gtp_daemon *d;
58 /* client socket */
59 struct osmo_stream_srv *srv;
60 char sockname[OSMO_SOCK_NAME_MAXLEN];
61};
62
Harald Welte24557a72020-04-17 22:08:29 +020063struct subprocess {
64 /* member in daemon->cups_clients */
65 struct llist_head list;
66 /* pointer to the client that started us */
67 struct cups_client *cups_client;
68 /* PID of the process */
69 pid_t pid;
70};
71
Harald Welte01744692020-04-18 21:00:13 +020072/* kill the specified subprocess and forget about it */
73static void subprocess_destroy(struct subprocess *p, int signal)
74{
75 kill(p->pid, signal);
76 llist_del(&p->list);
77 talloc_free(p);
78}
79
Harald Weltef7365592020-04-15 21:48:45 +020080/* Send JSON to a given client/connection */
81static int cups_client_tx_json(struct cups_client *cc, json_t *jtx)
82{
83 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Tx JSON");
84 char *json_str = json_dumps(jtx, JSON_SORT_KEYS);
85 char *out;
86 int json_strlen;
87
88 json_decref(jtx);
89 if (!json_str) {
90 LOGCC(cc, LOGL_ERROR, "Error encoding JSON\n");
91 return 0;
92 }
93 json_strlen = strlen(json_str);
94
95 LOGCC(cc, LOGL_DEBUG, "JSON Tx '%s'\n", json_str);
96
97 if (json_strlen > msgb_tailroom(msg)) {
98 LOGCC(cc, LOGL_ERROR, "Not enough room for JSON in msgb\n");
99 free(json_str);
100 return 0;
101 }
102
103 out = (char *)msgb_put(msg, json_strlen);
104 memcpy(out, json_str, json_strlen);
105 free(json_str);
106 osmo_stream_srv_send(cc->srv, msg);
107
108 return 0;
109}
110
111static json_t *gen_uecups_result(const char *name, const char *res)
112{
113 json_t *jres = json_object();
114 json_t *jret = json_object();
115
116 json_object_set_new(jres, "result", json_string(res));
117 json_object_set_new(jret, name, jres);
118
119 return jret;
120}
121
122static int parse_ep(struct sockaddr_storage *out, json_t *in)
123{
124 json_t *jaddr_type, *jport, *jip;
125 const char *addr_type, *ip;
126 uint8_t buf[16];
127
128 /* {"addr_type":"IPV4","ip":"31323334","Port":2152} */
129
130 if (!json_is_object(in))
131 return -EINVAL;
132
133 jaddr_type = json_object_get(in, "addr_type");
134 jport = json_object_get(in, "Port");
135 jip = json_object_get(in, "ip");
136
137 if (!jaddr_type || !jport || !jip)
138 return -EINVAL;
139
140 if (!json_is_string(jaddr_type) || !json_is_integer(jport) || !json_is_string(jip))
141 return -EINVAL;
142
143 addr_type = json_string_value(jaddr_type);
144 ip = json_string_value(jip);
145
146 memset(out, 0, sizeof(*out));
147
148 if (!strcmp(addr_type, "IPV4")) {
149 struct sockaddr_in *sin = (struct sockaddr_in *) out;
150 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
151 return -EINVAL;
152 memcpy(&sin->sin_addr, buf, 4);
153 sin->sin_family = AF_INET;
154 sin->sin_port = htons(json_integer_value(jport));
155 } else if (!strcmp(addr_type, "IPV6")) {
156 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
157 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
158 return -EINVAL;
159 memcpy(&sin6->sin6_addr, buf, 16);
160 sin6->sin6_family = AF_INET6;
161 sin6->sin6_port = htons(json_integer_value(jport));
162 } else
163 return -EINVAL;
164
165 return 0;
166}
167
168static int parse_eua(struct sockaddr_storage *out, json_t *jip, json_t *jaddr_type)
169{
170 const char *addr_type, *ip;
171 uint8_t buf[16];
172
173 if (!json_is_string(jip) || !json_is_string(jaddr_type))
174 return -EINVAL;
175
176 addr_type = json_string_value(jaddr_type);
177 ip = json_string_value(jip);
178
179 memset(out, 0, sizeof(*out));
180
181 if (!strcmp(addr_type, "IPV4")) {
182 struct sockaddr_in *sin = (struct sockaddr_in *) out;
183 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
184 return -EINVAL;
185 memcpy(&sin->sin_addr, buf, 4);
186 sin->sin_family = AF_INET;
187 } else if (!strcmp(addr_type, "IPV6")) {
188 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
189 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
190 return -EINVAL;
191 memcpy(&sin6->sin6_addr, buf, 16);
192 sin6->sin6_family = AF_INET6;
193 } else
194 return -EINVAL;
195
196 return 0;
197}
198
199
200static int parse_create_tun(struct gtp_tunnel_params *out, json_t *ctun)
201{
202 json_t *jlocal_gtp_ep, *jremote_gtp_ep;
203 json_t *jrx_teid, *jtx_teid;
204 json_t *jtun_dev_name, *jtun_netns_name;
205 json_t *juser_addr, *juser_addr_type;
206 int rc;
207
208 /* '{"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"}}' */
209
210 if (!json_is_object(ctun))
211 return -EINVAL;
212
213 /* mandatory IEs */
214 jlocal_gtp_ep = json_object_get(ctun, "local_gtp_ep");
215 jremote_gtp_ep = json_object_get(ctun, "remote_gtp_ep");
216 jrx_teid = json_object_get(ctun, "rx_teid");
217 jtx_teid = json_object_get(ctun, "tx_teid");
218 jtun_dev_name = json_object_get(ctun, "tun_dev_name");
219 juser_addr = json_object_get(ctun, "user_addr");
220 juser_addr_type = json_object_get(ctun, "user_addr_type");
221
222 if (!jlocal_gtp_ep || !jremote_gtp_ep || !jrx_teid || !jtx_teid || !jtun_dev_name ||
223 !juser_addr || !juser_addr_type)
224 return -EINVAL;
225 if (!json_is_object(jlocal_gtp_ep) || !json_is_object(jremote_gtp_ep) ||
226 !json_is_integer(jrx_teid) || !json_is_integer(jtx_teid) ||
227 !json_is_string(jtun_dev_name) ||
228 !json_is_string(juser_addr) || !json_is_string(juser_addr_type))
229 return -EINVAL;
230
231 memset(out, 0, sizeof(*out));
232
233 rc = parse_ep(&out->local_udp, jlocal_gtp_ep);
234 if (rc < 0)
235 return rc;
236 rc = parse_ep(&out->remote_udp, jremote_gtp_ep);
237 if (rc < 0)
238 return rc;
239 rc = parse_eua(&out->user_addr, juser_addr, juser_addr_type);
240 if (rc < 0)
241 return rc;
242 out->rx_teid = json_integer_value(jrx_teid);
243 out->tx_teid = json_integer_value(jtx_teid);
244 out->tun_name = talloc_strdup(out, json_string_value(jtun_dev_name));
245
246 /* optional IEs */
247 jtun_netns_name = json_object_get(ctun, "tun_netns_name");
248 if (jtun_netns_name) {
249 if (!json_is_string(jtun_netns_name))
250 return -EINVAL;
251 out->tun_netns_name = talloc_strdup(out, json_string_value(jtun_netns_name));
252 }
253
254 return 0;
255}
256
257
258static int cups_client_handle_create_tun(struct cups_client *cc, json_t *ctun)
259{
260 int rc;
261 struct gtp_tunnel_params *tpars = talloc_zero(cc, struct gtp_tunnel_params);
262 struct gtp_tunnel *t;
263
264 rc = parse_create_tun(tpars, ctun);
265 if (rc < 0) {
266 talloc_free(tpars);
267 return rc;
268 }
269
270 t = gtp_tunnel_alloc(g_daemon, tpars);
271 if (!t) {
272 LOGCC(cc, LOGL_NOTICE, "Failed to allocate tunnel\n");
273 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "ERR_NOT_FOUND"));
274 } else {
275 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "OK"));
276 }
277
278 talloc_free(tpars);
279 return 0;
280}
281
282static int cups_client_handle_destroy_tun(struct cups_client *cc, json_t *dtun)
283{
284 struct sockaddr_storage local_ep_addr;
285 json_t *jlocal_gtp_ep, *jrx_teid;
286 uint32_t rx_teid;
287 int rc;
288
289 jlocal_gtp_ep = json_object_get(dtun, "local_gtp_ep");
290 jrx_teid = json_object_get(dtun, "rx_teid");
291
292 if (!jlocal_gtp_ep || !jrx_teid)
293 return -EINVAL;
294
295 if (!json_is_object(jlocal_gtp_ep) || !json_is_integer(jrx_teid))
296 return -EINVAL;
297
298 rc = parse_ep(&local_ep_addr, jlocal_gtp_ep);
299 if (rc < 0)
300 return rc;
301 rx_teid = json_integer_value(jrx_teid);
302
303 rc = gtp_tunnel_destroy(g_daemon, &local_ep_addr, rx_teid);
304 if (rc < 0) {
305 LOGCC(cc, LOGL_NOTICE, "Failed to destroy tunnel\n");
306 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "ERR_NOT_FOUND"));
307 } else {
308 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "OK"));
309 }
310
311 return 0;
312}
313
Harald Welte24557a72020-04-17 22:08:29 +0200314static json_t *gen_uecups_term_ind(pid_t pid, int status)
315{
316 json_t *jterm = json_object();
317 json_t *jret = json_object();
318
319 json_object_set_new(jterm, "pid", json_integer(pid));
320 json_object_set_new(jterm, "exit_code", json_integer(status));
321
322 json_object_set_new(jret, "program_term_ind", jterm);
323
324 return jret;
325}
326
327
328static struct subprocess *subprocess_by_pid(struct gtp_daemon *d, pid_t pid)
329{
330 struct subprocess *sproc;
331 llist_for_each_entry(sproc, &d->subprocesses, list) {
332 if (sproc->pid == pid)
333 return sproc;
334 }
335 return NULL;
336}
337
Harald Weltef9bb1772020-04-21 22:46:34 +0200338static void child_terminated(struct gtp_daemon *d, int pid, int status)
Harald Welte24557a72020-04-17 22:08:29 +0200339{
Harald Welte24557a72020-04-17 22:08:29 +0200340 struct subprocess *sproc;
341 json_t *jterm_ind;
342
Harald Weltef9bb1772020-04-21 22:46:34 +0200343 LOGP(DUECUPS, LOGL_DEBUG, "SIGCHLD receive from pid %u; status=%d\n", pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200344
Harald Weltef9bb1772020-04-21 22:46:34 +0200345 sproc = subprocess_by_pid(d, pid);
Harald Welte24557a72020-04-17 22:08:29 +0200346 if (!sproc) {
347 LOGP(DUECUPS, LOGL_NOTICE, "subprocess %u terminated (status=%d) but we don't know it?\n",
Harald Weltef9bb1772020-04-21 22:46:34 +0200348 pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200349 return;
350 }
351
Harald Weltef9bb1772020-04-21 22:46:34 +0200352 /* generate prog_term_ind towards control plane */
353 jterm_ind = gen_uecups_term_ind(pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200354 if (!jterm_ind)
355 return;
356
357 cups_client_tx_json(sproc->cups_client, jterm_ind);
358
359 llist_del(&sproc->list);
360 talloc_free(sproc);
361}
362
Harald Weltef9bb1772020-04-21 22:46:34 +0200363static void sigchild_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)
364{
365 struct gtp_daemon *d = osfd->data;
366 int pid, status;
367
368 OSMO_ASSERT(fdsi->ssi_signo == SIGCHLD);
369
370 child_terminated(d, fdsi->ssi_pid, fdsi->ssi_status);
371
372 /* it is known that classic signals coalesce: If you get multiple signals of the
373 * same type before a process is scheduled, the subsequent signaals are dropped. This
374 * makes sense for SIGINT or something like this, but for SIGCHLD carrying the PID of
375 * the terminated process, it doesn't really. Linux had the chance to fix this when
376 * introducing signalfd() - but the developers decided not to fix it. So the signalfd_siginfo
377 * contains the PID of one process that terminated - but there may be any number of other
378 * processes that also have terminated, and for which we don't get events this way. */
379
380 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
381 child_terminated(d, pid, status);
382
383}
384
Harald Welte24557a72020-04-17 22:08:29 +0200385static json_t *gen_uecups_start_res(pid_t pid, const char *result)
386{
387 json_t *ret = gen_uecups_result("start_program_res", result);
388 json_object_set_new(json_object_get(ret, "start_program_res"), "pid", json_integer(pid));
389
390 return ret;
391}
392
393static int cups_client_handle_start_program(struct cups_client *cc, json_t *sprog)
394{
395 json_t *juser, *jcmd, *jenv, *jnetns, *jres;
396 struct gtp_daemon *d = cc->d;
397 const char *cmd, *user;
398 char **addl_env = NULL;
399 sigset_t oldmask;
Harald Weltef23abd72020-04-20 12:09:32 +0200400 int nsfd = -1, rc;
Harald Welte24557a72020-04-17 22:08:29 +0200401
402 juser = json_object_get(sprog, "run_as_user");
403 jcmd = json_object_get(sprog, "command");
404 jenv = json_object_get(sprog, "environment");
405 jnetns = json_object_get(sprog, "tun_netns_name");
406
407 /* mandatory parts */
408 if (!juser || !jcmd)
409 return -EINVAL;
410 if (!json_is_string(juser) || !json_is_string(jcmd))
411 return -EINVAL;
412
413 /* optional parts */
414 if (jenv && !json_is_array(jenv))
415 return -EINVAL;
416 if (jnetns && !json_is_string(jnetns))
417 return -EINVAL;
418
419 cmd = json_string_value(jcmd);
420 user = json_string_value(juser);
421 if (jnetns) {
422 struct tun_device *tun = tun_device_find_netns(d, json_string_value(jnetns));
423 if (!tun)
424 return -ENODEV;
425 nsfd = tun->netns_fd;
426 }
427
428 /* build environment */
429 if (jenv) {
430 json_t *j;
431 int i;
432 addl_env = talloc_zero_array(cc, char *, json_array_size(jenv)+1);
433 if (!addl_env)
434 return -ENOMEM;
435 json_array_foreach(jenv, i, j) {
436 addl_env[i] = talloc_strdup(addl_env, json_string_value(j));
437 }
438 }
439
440 if (jnetns) {
441 rc = switch_ns(nsfd, &oldmask);
442 if (rc < 0) {
443 talloc_free(addl_env);
444 return -EIO;
445 }
446 }
447
448 rc = osmo_system_nowait2(cmd, osmo_environment_whitelist, addl_env, user);
449
450 if (jnetns) {
451 OSMO_ASSERT(restore_ns(&oldmask) == 0);
452 }
453
454 talloc_free(addl_env);
455
456 if (rc > 0) {
457 /* create a record about the subprocess we started, so we can notify the
458 * client that crated it upon termination */
459 struct subprocess *sproc = talloc_zero(cc, struct subprocess);
460 if (!sproc)
461 return -ENOMEM;
462
463 sproc->cups_client = cc;
464 sproc->pid = rc;
465 llist_add_tail(&sproc->list, &d->subprocesses);
466 jres = gen_uecups_start_res(sproc->pid, "OK");
467 } else {
468 jres = gen_uecups_start_res(0, "ERR_INVALID_DATA");
469 }
470
471 cups_client_tx_json(cc, jres);
472
473 return 0;
474}
475
Harald Welte01744692020-04-18 21:00:13 +0200476static int cups_client_handle_reset_all_state(struct cups_client *cc, json_t *sprog)
477{
478 struct gtp_daemon *d = cc->d;
479 struct gtp_tunnel *t, *t2;
480 struct subprocess *p, *p2;
481 json_t *jres;
482
483 pthread_rwlock_wrlock(&d->rwlock);
484 llist_for_each_entry_safe(t, t2, &d->gtp_tunnels, list) {
485 _gtp_tunnel_destroy(t);
486 }
487 pthread_rwlock_unlock(&d->rwlock);
488
489 /* no locking needed as this list is only used by main thread */
490 llist_for_each_entry_safe(p, p2, &d->subprocesses, list) {
491 subprocess_destroy(p, SIGKILL);
492 }
493
494 jres = gen_uecups_result("reset_all_state_res", "OK");
495 cups_client_tx_json(cc, jres);
496
497 return 0;
498}
Harald Welte24557a72020-04-17 22:08:29 +0200499
Harald Weltef7365592020-04-15 21:48:45 +0200500static int cups_client_handle_json(struct cups_client *cc, json_t *jroot)
501{
502 void *iter;
503 const char *key;
504 json_t *cmd;
505 int rc;
506
507 if (!json_is_object(jroot))
508 return -EINVAL;
509
510 iter = json_object_iter(jroot);
511 key = json_object_iter_key(iter);
512 cmd = json_object_iter_value(iter);
513 if (!iter || !key || !cmd)
514 return -EINVAL;
515
516 if (!strcmp(key, "create_tun")) {
517 rc = cups_client_handle_create_tun(cc, cmd);
518 } else if (!strcmp(key, "destroy_tun")) {
519 rc = cups_client_handle_destroy_tun(cc, cmd);
Harald Welte24557a72020-04-17 22:08:29 +0200520 } else if (!strcmp(key, "start_program")) {
521 rc = cups_client_handle_start_program(cc, cmd);
Harald Welte01744692020-04-18 21:00:13 +0200522 } else if (!strcmp(key, "reset_all_state")) {
523 rc = cups_client_handle_reset_all_state(cc, cmd);
Harald Weltef7365592020-04-15 21:48:45 +0200524 } else {
525 LOGCC(cc, LOGL_NOTICE, "Unknown command '%s' received\n", key);
526 return -EINVAL;
527 }
528
529 if (rc < 0) {
530 LOGCC(cc, LOGL_NOTICE, "Error %d handling '%s' command\n", rc, key);
531 char buf[64];
532 snprintf(buf, sizeof(buf), "%s_res", key);
533 cups_client_tx_json(cc, gen_uecups_result(buf, "ERR_INVALID_DATA"));
534 return -EINVAL;
535 }
536
537 return 0;
538}
539
540/* control/user plane separation per-client read cb */
541static int cups_client_read_cb(struct osmo_stream_srv *conn)
542{
543 struct osmo_fd *ofd = osmo_stream_srv_get_ofd(conn);
544 struct cups_client *cc = osmo_stream_srv_get_data(conn);
545 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Rx JSON");
546 struct sctp_sndrcvinfo sinfo;
547 json_error_t jerr;
548 json_t *jroot;
549 int flags = 0;
550 int rc = 0;
551
552 /* Read message from socket */
553 /* we cannot use osmo_stream_srv_recv() here, as we might get some out-of-band info from
554 * SCTP. FIXME: add something like osmo_stream_srv_recv_sctp() to libosmo-netif and use
555 * it here as well as in libosmo-sigtran and osmo-msc */
556 rc = sctp_recvmsg(ofd->fd, msg->tail, msgb_tailroom(msg), NULL, NULL, &sinfo,&flags);
557 if (rc <= 0) {
558 osmo_stream_srv_destroy(conn);
559 rc = -1;
560 goto out;
561 } else
562 msgb_put(msg, rc);
563
564 if (flags & MSG_NOTIFICATION) {
565 union sctp_notification *notif = (union sctp_notification *) msgb_data(msg);
566 switch (notif->sn_header.sn_type) {
567 case SCTP_SHUTDOWN_EVENT:
568 osmo_stream_srv_destroy(conn);
569 rc = -EBADF;
570 goto out;
571 default:
572 break;
573 }
574 goto out;
575 }
576
577 LOGCC(cc, LOGL_DEBUG, "Rx '%s'\n", msgb_data(msg));
578
579 /* Parse the JSON */
580 jroot = json_loadb((const char *) msgb_data(msg), msgb_length(msg), 0, &jerr);
581 if (!jroot) {
582 LOGCC(cc, LOGL_ERROR, "Error decoding JSON (%s)", jerr.text);
583 rc = -1;
584 goto out;
585 }
586
587 /* Dispatch */
588 rc = cups_client_handle_json(cc, jroot);
589
590 json_decref(jroot);
591 msgb_free(msg);
592
593 return 0;
594out:
595 msgb_free(msg);
596 return rc;
597}
598
599static int cups_client_closed_cb(struct osmo_stream_srv *conn)
600{
601 struct cups_client *cc = osmo_stream_srv_get_data(conn);
Harald Welte24557a72020-04-17 22:08:29 +0200602 struct gtp_daemon *d = cc->d;
603 struct subprocess *p, *p2;
604
605 /* kill + forget about all subprocesses of this client */
Harald Welte01744692020-04-18 21:00:13 +0200606 /* We need no locking here as the subprocess list is only used from the main thread */
Harald Welte24557a72020-04-17 22:08:29 +0200607 llist_for_each_entry_safe(p, p2, &d->subprocesses, list) {
Harald Welte01744692020-04-18 21:00:13 +0200608 if (p->cups_client == cc)
609 subprocess_destroy(p, SIGKILL);
Harald Welte24557a72020-04-17 22:08:29 +0200610 }
Harald Weltef7365592020-04-15 21:48:45 +0200611
612 LOGCC(cc, LOGL_INFO, "UECUPS connection lost\n");
613 llist_del(&cc->list);
614 return 0;
615}
616
617
618/* the control/user plane separation server bind/accept fd */
619static int cups_accept_cb(struct osmo_stream_srv_link *link, int fd)
620{
621 struct gtp_daemon *d = osmo_stream_srv_link_get_data(link);
622 struct cups_client *cc;
623
624 cc = talloc_zero(d, struct cups_client);
625 if (!cc)
626 return -1;
627
Harald Welte24557a72020-04-17 22:08:29 +0200628 cc->d = d;
Harald Weltef7365592020-04-15 21:48:45 +0200629 osmo_sock_get_name_buf(cc->sockname, sizeof(cc->sockname), fd);
630 cc->srv = osmo_stream_srv_create(cc, link, fd, cups_client_read_cb, cups_client_closed_cb, cc);
631 if (!cc->srv) {
632 talloc_free(cc);
633 return -1;
634 }
635 LOGCC(cc, LOGL_INFO, "Accepted new UECUPS connection\n");
636
637 llist_add_tail(&cc->list, &d->cups_clients);
638
639 return 0;
640}
641
642/***********************************************************************
643 * GTP Daemon
644 ***********************************************************************/
645
Harald Welte432a1302020-04-17 12:52:40 +0200646#ifndef OSMO_VTY_PORT_UECUPS
647#define OSMO_VTY_PORT_UECUPS 4268
648#endif
649
Harald Welte933dec72020-04-18 21:41:51 +0200650static void *g_tall_ctx;
Harald Weltef7365592020-04-15 21:48:45 +0200651struct gtp_daemon *g_daemon;
652static int g_daemonize;
Harald Welte27dc4c42020-04-20 12:39:13 +0200653static char *g_config_file = "osmo-uecups-daemon.cfg";
Harald Weltef7365592020-04-15 21:48:45 +0200654extern struct vty_app_info g_vty_info;
655
Harald Welte933dec72020-04-18 21:41:51 +0200656static void signal_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)
657{
658 switch (fdsi->ssi_signo) {
659 case SIGCHLD:
660 sigchild_cb(osfd, fdsi);
661 break;
662 case SIGUSR1:
663 talloc_report_full(g_tall_ctx, stderr);
664 break;
665 default:
666 break;
667 }
668}
669
Harald Weltef7365592020-04-15 21:48:45 +0200670static struct gtp_daemon *gtp_daemon_alloc(void *ctx)
671{
672 struct gtp_daemon *d = talloc_zero(ctx, struct gtp_daemon);
673 if (!d)
674 return NULL;
675
676 INIT_LLIST_HEAD(&d->gtp_endpoints);
677 INIT_LLIST_HEAD(&d->tun_devices);
678 INIT_LLIST_HEAD(&d->gtp_tunnels);
Harald Welte24557a72020-04-17 22:08:29 +0200679 INIT_LLIST_HEAD(&d->subprocesses);
Harald Weltef7365592020-04-15 21:48:45 +0200680 pthread_rwlock_init(&d->rwlock, NULL);
681 d->main_thread = pthread_self();
682
683 INIT_LLIST_HEAD(&d->cups_clients);
684
685 d->cfg.cups_local_ip = talloc_strdup(d, "localhost");
Harald Welte432a1302020-04-17 12:52:40 +0200686 d->cfg.cups_local_port = UECUPS_SCTP_PORT;
Harald Weltef7365592020-04-15 21:48:45 +0200687
688 return d;
689}
690
691static const struct log_info_cat log_categories[] = {
692 [DTUN] = {
693 .name ="DTUN",
694 .description = "Tunnel interface (tun device)",
695 .enabled = 1, .loglevel = LOGL_INFO,
696 },
697 [DEP] = {
698 .name = "DEP",
699 .description = "GTP endpoint (UDP socket)",
700 .enabled = 1, .loglevel = LOGL_INFO,
701 },
702 [DGT] = {
703 .name = "DGT",
704 .description = "GTP tunnel (session)",
705 .enabled = 1, .loglevel = LOGL_INFO,
706 },
707 [DUECUPS] = {
708 .name = "DUECUPS",
709 .description = "UE Control User Plane Separation",
710 .enabled = 1, .loglevel = LOGL_DEBUG,
711 },
712
713};
714
715static const struct log_info log_info = {
716 .cat = log_categories,
717 .num_cat = ARRAY_SIZE(log_categories),
718};
719
720int main(int argc, char **argv)
721{
Harald Weltef7365592020-04-15 21:48:45 +0200722 int rc;
723
Harald Welte933dec72020-04-18 21:41:51 +0200724 g_tall_ctx = talloc_named_const(NULL, 0, "root");
725 g_vty_info.tall_ctx = g_tall_ctx;
Harald Weltef7365592020-04-15 21:48:45 +0200726
727 osmo_init_ignore_signals();
Harald Welte933dec72020-04-18 21:41:51 +0200728 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Weltef7365592020-04-15 21:48:45 +0200729
Harald Welte933dec72020-04-18 21:41:51 +0200730 g_daemon = gtp_daemon_alloc(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200731 OSMO_ASSERT(g_daemon);
732
Harald Weltea932e4e2020-04-20 12:10:58 +0200733 msgb_talloc_ctx_init(g_tall_ctx, 10);
Harald Welte933dec72020-04-18 21:41:51 +0200734 osmo_stats_init(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200735 vty_init(&g_vty_info);
736 logging_vty_add_cmds();
737 osmo_talloc_vty_add_cmds();
738 osmo_stats_vty_add_cmds();
Harald Welte933dec72020-04-18 21:41:51 +0200739 rate_ctr_init(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200740 gtpud_vty_init();
741
742 init_netns();
743
744 rc = vty_read_config_file(g_config_file, NULL);
745 if (rc < 0) {
746 fprintf(stderr, "Failed to open config file: '%s'\n", g_config_file);
747 exit(2);
748 }
749
Harald Welte933dec72020-04-18 21:41:51 +0200750 rc = telnet_init_dynif(g_daemon, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_UECUPS);
Harald Weltef7365592020-04-15 21:48:45 +0200751 if (rc < 0)
752 exit(1);
753
754 g_daemon->cups_link = osmo_stream_srv_link_create(g_daemon);
755 if (!g_daemon->cups_link) {
756 fprintf(stderr, "Failed to create CUPS socket %s:%u (%s)\n",
757 g_daemon->cfg.cups_local_ip, g_daemon->cfg.cups_local_port, strerror(errno));
758 exit(1);
759 }
760
761 /* UECUPS socket for control from control plane side */
762 osmo_stream_srv_link_set_nodelay(g_daemon->cups_link, true);
763 osmo_stream_srv_link_set_addr(g_daemon->cups_link, g_daemon->cfg.cups_local_ip);
764 osmo_stream_srv_link_set_port(g_daemon->cups_link, g_daemon->cfg.cups_local_port);
765 osmo_stream_srv_link_set_proto(g_daemon->cups_link, IPPROTO_SCTP);
766 osmo_stream_srv_link_set_data(g_daemon->cups_link, g_daemon);
767 osmo_stream_srv_link_set_accept_cb(g_daemon->cups_link, cups_accept_cb);
768 osmo_stream_srv_link_open(g_daemon->cups_link);
769
Harald Welte24557a72020-04-17 22:08:29 +0200770 /* block SIGCHLD via normal delivery; redirect it to signalfd */
771 sigset_t sigset;
772 sigemptyset(&sigset);
773 sigaddset(&sigset, SIGCHLD);
Harald Welte933dec72020-04-18 21:41:51 +0200774 sigaddset(&sigset, SIGUSR1);
Harald Welte24557a72020-04-17 22:08:29 +0200775 sigprocmask(SIG_BLOCK, &sigset, NULL);
Harald Welte933dec72020-04-18 21:41:51 +0200776 g_daemon->signalfd = osmo_signalfd_setup(g_daemon, sigset, signal_cb, g_daemon);
777 osmo_init_ignore_signals();
Harald Welte24557a72020-04-17 22:08:29 +0200778
Harald Weltef7365592020-04-15 21:48:45 +0200779 if (g_daemonize) {
780 rc = osmo_daemonize();
781 if (rc < 0) {
782 perror("Error during daemonize");
783 exit(1);
784 }
785 }
786
787 while (1) {
788 osmo_select_main(0);
789 }
790}