blob: 76aeab5f3f293b317016d8d30d9a8f5f05f23243 [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{
Pau Espin Pedrol687eafa2022-04-11 16:17:05 +020075 LOGCC(p->cups_client, LOGL_DEBUG, "Kill subprocess pid %llu with signal %u\n",
76 (unsigned long long)p->pid, signal);
Harald Welte01744692020-04-18 21:00:13 +020077 kill(p->pid, signal);
78 llist_del(&p->list);
79 talloc_free(p);
80}
81
Harald Weltef7365592020-04-15 21:48:45 +020082/* Send JSON to a given client/connection */
83static int cups_client_tx_json(struct cups_client *cc, json_t *jtx)
84{
85 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Tx JSON");
86 char *json_str = json_dumps(jtx, JSON_SORT_KEYS);
87 char *out;
88 int json_strlen;
89
90 json_decref(jtx);
91 if (!json_str) {
92 LOGCC(cc, LOGL_ERROR, "Error encoding JSON\n");
93 return 0;
94 }
95 json_strlen = strlen(json_str);
96
97 LOGCC(cc, LOGL_DEBUG, "JSON Tx '%s'\n", json_str);
98
99 if (json_strlen > msgb_tailroom(msg)) {
100 LOGCC(cc, LOGL_ERROR, "Not enough room for JSON in msgb\n");
101 free(json_str);
102 return 0;
103 }
104
105 out = (char *)msgb_put(msg, json_strlen);
106 memcpy(out, json_str, json_strlen);
107 free(json_str);
108 osmo_stream_srv_send(cc->srv, msg);
109
110 return 0;
111}
112
113static json_t *gen_uecups_result(const char *name, const char *res)
114{
115 json_t *jres = json_object();
116 json_t *jret = json_object();
117
118 json_object_set_new(jres, "result", json_string(res));
119 json_object_set_new(jret, name, jres);
120
121 return jret;
122}
123
124static int parse_ep(struct sockaddr_storage *out, json_t *in)
125{
126 json_t *jaddr_type, *jport, *jip;
127 const char *addr_type, *ip;
128 uint8_t buf[16];
129
130 /* {"addr_type":"IPV4","ip":"31323334","Port":2152} */
131
132 if (!json_is_object(in))
133 return -EINVAL;
134
135 jaddr_type = json_object_get(in, "addr_type");
136 jport = json_object_get(in, "Port");
137 jip = json_object_get(in, "ip");
138
139 if (!jaddr_type || !jport || !jip)
140 return -EINVAL;
141
142 if (!json_is_string(jaddr_type) || !json_is_integer(jport) || !json_is_string(jip))
143 return -EINVAL;
144
145 addr_type = json_string_value(jaddr_type);
146 ip = json_string_value(jip);
147
148 memset(out, 0, sizeof(*out));
149
150 if (!strcmp(addr_type, "IPV4")) {
151 struct sockaddr_in *sin = (struct sockaddr_in *) out;
152 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
153 return -EINVAL;
154 memcpy(&sin->sin_addr, buf, 4);
155 sin->sin_family = AF_INET;
156 sin->sin_port = htons(json_integer_value(jport));
157 } else if (!strcmp(addr_type, "IPV6")) {
158 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
159 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
160 return -EINVAL;
161 memcpy(&sin6->sin6_addr, buf, 16);
162 sin6->sin6_family = AF_INET6;
163 sin6->sin6_port = htons(json_integer_value(jport));
164 } else
165 return -EINVAL;
166
167 return 0;
168}
169
170static int parse_eua(struct sockaddr_storage *out, json_t *jip, json_t *jaddr_type)
171{
172 const char *addr_type, *ip;
173 uint8_t buf[16];
174
175 if (!json_is_string(jip) || !json_is_string(jaddr_type))
176 return -EINVAL;
177
178 addr_type = json_string_value(jaddr_type);
179 ip = json_string_value(jip);
180
181 memset(out, 0, sizeof(*out));
182
183 if (!strcmp(addr_type, "IPV4")) {
184 struct sockaddr_in *sin = (struct sockaddr_in *) out;
185 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
186 return -EINVAL;
187 memcpy(&sin->sin_addr, buf, 4);
188 sin->sin_family = AF_INET;
189 } else if (!strcmp(addr_type, "IPV6")) {
190 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
191 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
192 return -EINVAL;
193 memcpy(&sin6->sin6_addr, buf, 16);
194 sin6->sin6_family = AF_INET6;
195 } else
196 return -EINVAL;
197
198 return 0;
199}
200
201
202static int parse_create_tun(struct gtp_tunnel_params *out, json_t *ctun)
203{
204 json_t *jlocal_gtp_ep, *jremote_gtp_ep;
205 json_t *jrx_teid, *jtx_teid;
206 json_t *jtun_dev_name, *jtun_netns_name;
207 json_t *juser_addr, *juser_addr_type;
208 int rc;
209
210 /* '{"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"}}' */
211
212 if (!json_is_object(ctun))
213 return -EINVAL;
214
215 /* mandatory IEs */
216 jlocal_gtp_ep = json_object_get(ctun, "local_gtp_ep");
217 jremote_gtp_ep = json_object_get(ctun, "remote_gtp_ep");
218 jrx_teid = json_object_get(ctun, "rx_teid");
219 jtx_teid = json_object_get(ctun, "tx_teid");
220 jtun_dev_name = json_object_get(ctun, "tun_dev_name");
221 juser_addr = json_object_get(ctun, "user_addr");
222 juser_addr_type = json_object_get(ctun, "user_addr_type");
223
224 if (!jlocal_gtp_ep || !jremote_gtp_ep || !jrx_teid || !jtx_teid || !jtun_dev_name ||
225 !juser_addr || !juser_addr_type)
226 return -EINVAL;
227 if (!json_is_object(jlocal_gtp_ep) || !json_is_object(jremote_gtp_ep) ||
228 !json_is_integer(jrx_teid) || !json_is_integer(jtx_teid) ||
229 !json_is_string(jtun_dev_name) ||
230 !json_is_string(juser_addr) || !json_is_string(juser_addr_type))
231 return -EINVAL;
232
233 memset(out, 0, sizeof(*out));
234
235 rc = parse_ep(&out->local_udp, jlocal_gtp_ep);
236 if (rc < 0)
237 return rc;
238 rc = parse_ep(&out->remote_udp, jremote_gtp_ep);
239 if (rc < 0)
240 return rc;
241 rc = parse_eua(&out->user_addr, juser_addr, juser_addr_type);
242 if (rc < 0)
243 return rc;
244 out->rx_teid = json_integer_value(jrx_teid);
245 out->tx_teid = json_integer_value(jtx_teid);
246 out->tun_name = talloc_strdup(out, json_string_value(jtun_dev_name));
247
248 /* optional IEs */
249 jtun_netns_name = json_object_get(ctun, "tun_netns_name");
250 if (jtun_netns_name) {
251 if (!json_is_string(jtun_netns_name))
252 return -EINVAL;
253 out->tun_netns_name = talloc_strdup(out, json_string_value(jtun_netns_name));
254 }
255
256 return 0;
257}
258
259
260static int cups_client_handle_create_tun(struct cups_client *cc, json_t *ctun)
261{
262 int rc;
263 struct gtp_tunnel_params *tpars = talloc_zero(cc, struct gtp_tunnel_params);
264 struct gtp_tunnel *t;
265
266 rc = parse_create_tun(tpars, ctun);
267 if (rc < 0) {
268 talloc_free(tpars);
269 return rc;
270 }
271
272 t = gtp_tunnel_alloc(g_daemon, tpars);
273 if (!t) {
274 LOGCC(cc, LOGL_NOTICE, "Failed to allocate tunnel\n");
275 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "ERR_NOT_FOUND"));
276 } else {
277 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "OK"));
278 }
279
280 talloc_free(tpars);
281 return 0;
282}
283
284static int cups_client_handle_destroy_tun(struct cups_client *cc, json_t *dtun)
285{
286 struct sockaddr_storage local_ep_addr;
287 json_t *jlocal_gtp_ep, *jrx_teid;
288 uint32_t rx_teid;
289 int rc;
290
291 jlocal_gtp_ep = json_object_get(dtun, "local_gtp_ep");
292 jrx_teid = json_object_get(dtun, "rx_teid");
293
294 if (!jlocal_gtp_ep || !jrx_teid)
295 return -EINVAL;
296
297 if (!json_is_object(jlocal_gtp_ep) || !json_is_integer(jrx_teid))
298 return -EINVAL;
299
300 rc = parse_ep(&local_ep_addr, jlocal_gtp_ep);
301 if (rc < 0)
302 return rc;
303 rx_teid = json_integer_value(jrx_teid);
304
305 rc = gtp_tunnel_destroy(g_daemon, &local_ep_addr, rx_teid);
306 if (rc < 0) {
307 LOGCC(cc, LOGL_NOTICE, "Failed to destroy tunnel\n");
308 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "ERR_NOT_FOUND"));
309 } else {
310 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "OK"));
311 }
312
313 return 0;
314}
315
Harald Welte24557a72020-04-17 22:08:29 +0200316static json_t *gen_uecups_term_ind(pid_t pid, int status)
317{
318 json_t *jterm = json_object();
319 json_t *jret = json_object();
320
321 json_object_set_new(jterm, "pid", json_integer(pid));
322 json_object_set_new(jterm, "exit_code", json_integer(status));
323
324 json_object_set_new(jret, "program_term_ind", jterm);
325
326 return jret;
327}
328
329
330static struct subprocess *subprocess_by_pid(struct gtp_daemon *d, pid_t pid)
331{
332 struct subprocess *sproc;
333 llist_for_each_entry(sproc, &d->subprocesses, list) {
334 if (sproc->pid == pid)
335 return sproc;
336 }
337 return NULL;
338}
339
Harald Weltef9bb1772020-04-21 22:46:34 +0200340static void child_terminated(struct gtp_daemon *d, int pid, int status)
Harald Welte24557a72020-04-17 22:08:29 +0200341{
Harald Welte24557a72020-04-17 22:08:29 +0200342 struct subprocess *sproc;
343 json_t *jterm_ind;
344
Harald Weltef9bb1772020-04-21 22:46:34 +0200345 LOGP(DUECUPS, LOGL_DEBUG, "SIGCHLD receive from pid %u; status=%d\n", pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200346
Harald Weltef9bb1772020-04-21 22:46:34 +0200347 sproc = subprocess_by_pid(d, pid);
Harald Welte24557a72020-04-17 22:08:29 +0200348 if (!sproc) {
349 LOGP(DUECUPS, LOGL_NOTICE, "subprocess %u terminated (status=%d) but we don't know it?\n",
Harald Weltef9bb1772020-04-21 22:46:34 +0200350 pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200351 return;
352 }
353
Harald Weltef9bb1772020-04-21 22:46:34 +0200354 /* generate prog_term_ind towards control plane */
355 jterm_ind = gen_uecups_term_ind(pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200356 if (!jterm_ind)
357 return;
358
359 cups_client_tx_json(sproc->cups_client, jterm_ind);
360
361 llist_del(&sproc->list);
362 talloc_free(sproc);
363}
364
Harald Weltef9bb1772020-04-21 22:46:34 +0200365static void sigchild_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)
366{
367 struct gtp_daemon *d = osfd->data;
368 int pid, status;
369
370 OSMO_ASSERT(fdsi->ssi_signo == SIGCHLD);
371
Harald Weltef9bb1772020-04-21 22:46:34 +0200372 /* it is known that classic signals coalesce: If you get multiple signals of the
Pau Espin Pedrol95ad8c92022-04-11 15:47:36 +0200373 * same type before a process is scheduled, the subsequent signals are dropped. This
Harald Weltef9bb1772020-04-21 22:46:34 +0200374 * 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
Pau Espin Pedrol687eafa2022-04-11 16:17:05 +0200483 LOGCC(cc, LOGL_DEBUG, "Destroying all tunnels\n");
Harald Welte01744692020-04-18 21:00:13 +0200484 pthread_rwlock_wrlock(&d->rwlock);
485 llist_for_each_entry_safe(t, t2, &d->gtp_tunnels, list) {
486 _gtp_tunnel_destroy(t);
487 }
488 pthread_rwlock_unlock(&d->rwlock);
489
490 /* no locking needed as this list is only used by main thread */
Pau Espin Pedrol687eafa2022-04-11 16:17:05 +0200491 LOGCC(cc, LOGL_DEBUG, "Destroying all subprocesses\n");
Harald Welte01744692020-04-18 21:00:13 +0200492 llist_for_each_entry_safe(p, p2, &d->subprocesses, list) {
493 subprocess_destroy(p, SIGKILL);
494 }
495
496 jres = gen_uecups_result("reset_all_state_res", "OK");
497 cups_client_tx_json(cc, jres);
498
499 return 0;
500}
Harald Welte24557a72020-04-17 22:08:29 +0200501
Harald Weltef7365592020-04-15 21:48:45 +0200502static int cups_client_handle_json(struct cups_client *cc, json_t *jroot)
503{
504 void *iter;
505 const char *key;
506 json_t *cmd;
507 int rc;
508
509 if (!json_is_object(jroot))
510 return -EINVAL;
511
512 iter = json_object_iter(jroot);
513 key = json_object_iter_key(iter);
514 cmd = json_object_iter_value(iter);
515 if (!iter || !key || !cmd)
516 return -EINVAL;
517
518 if (!strcmp(key, "create_tun")) {
519 rc = cups_client_handle_create_tun(cc, cmd);
520 } else if (!strcmp(key, "destroy_tun")) {
521 rc = cups_client_handle_destroy_tun(cc, cmd);
Harald Welte24557a72020-04-17 22:08:29 +0200522 } else if (!strcmp(key, "start_program")) {
523 rc = cups_client_handle_start_program(cc, cmd);
Harald Welte01744692020-04-18 21:00:13 +0200524 } else if (!strcmp(key, "reset_all_state")) {
525 rc = cups_client_handle_reset_all_state(cc, cmd);
Harald Weltef7365592020-04-15 21:48:45 +0200526 } else {
527 LOGCC(cc, LOGL_NOTICE, "Unknown command '%s' received\n", key);
528 return -EINVAL;
529 }
530
531 if (rc < 0) {
532 LOGCC(cc, LOGL_NOTICE, "Error %d handling '%s' command\n", rc, key);
533 char buf[64];
534 snprintf(buf, sizeof(buf), "%s_res", key);
535 cups_client_tx_json(cc, gen_uecups_result(buf, "ERR_INVALID_DATA"));
536 return -EINVAL;
537 }
538
539 return 0;
540}
541
542/* control/user plane separation per-client read cb */
543static int cups_client_read_cb(struct osmo_stream_srv *conn)
544{
545 struct osmo_fd *ofd = osmo_stream_srv_get_ofd(conn);
546 struct cups_client *cc = osmo_stream_srv_get_data(conn);
547 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Rx JSON");
548 struct sctp_sndrcvinfo sinfo;
549 json_error_t jerr;
550 json_t *jroot;
551 int flags = 0;
552 int rc = 0;
553
554 /* Read message from socket */
555 /* we cannot use osmo_stream_srv_recv() here, as we might get some out-of-band info from
556 * SCTP. FIXME: add something like osmo_stream_srv_recv_sctp() to libosmo-netif and use
557 * it here as well as in libosmo-sigtran and osmo-msc */
558 rc = sctp_recvmsg(ofd->fd, msg->tail, msgb_tailroom(msg), NULL, NULL, &sinfo,&flags);
559 if (rc <= 0) {
560 osmo_stream_srv_destroy(conn);
561 rc = -1;
562 goto out;
563 } else
564 msgb_put(msg, rc);
565
566 if (flags & MSG_NOTIFICATION) {
567 union sctp_notification *notif = (union sctp_notification *) msgb_data(msg);
568 switch (notif->sn_header.sn_type) {
569 case SCTP_SHUTDOWN_EVENT:
570 osmo_stream_srv_destroy(conn);
571 rc = -EBADF;
572 goto out;
573 default:
574 break;
575 }
576 goto out;
577 }
578
579 LOGCC(cc, LOGL_DEBUG, "Rx '%s'\n", msgb_data(msg));
580
581 /* Parse the JSON */
582 jroot = json_loadb((const char *) msgb_data(msg), msgb_length(msg), 0, &jerr);
583 if (!jroot) {
584 LOGCC(cc, LOGL_ERROR, "Error decoding JSON (%s)", jerr.text);
585 rc = -1;
586 goto out;
587 }
588
589 /* Dispatch */
590 rc = cups_client_handle_json(cc, jroot);
591
592 json_decref(jroot);
593 msgb_free(msg);
594
595 return 0;
596out:
597 msgb_free(msg);
598 return rc;
599}
600
601static int cups_client_closed_cb(struct osmo_stream_srv *conn)
602{
603 struct cups_client *cc = osmo_stream_srv_get_data(conn);
Harald Welte24557a72020-04-17 22:08:29 +0200604 struct gtp_daemon *d = cc->d;
605 struct subprocess *p, *p2;
606
607 /* kill + forget about all subprocesses of this client */
Harald Welte01744692020-04-18 21:00:13 +0200608 /* We need no locking here as the subprocess list is only used from the main thread */
Harald Welte24557a72020-04-17 22:08:29 +0200609 llist_for_each_entry_safe(p, p2, &d->subprocesses, list) {
Harald Welte01744692020-04-18 21:00:13 +0200610 if (p->cups_client == cc)
611 subprocess_destroy(p, SIGKILL);
Harald Welte24557a72020-04-17 22:08:29 +0200612 }
Harald Weltef7365592020-04-15 21:48:45 +0200613
614 LOGCC(cc, LOGL_INFO, "UECUPS connection lost\n");
615 llist_del(&cc->list);
616 return 0;
617}
618
619
620/* the control/user plane separation server bind/accept fd */
621static int cups_accept_cb(struct osmo_stream_srv_link *link, int fd)
622{
623 struct gtp_daemon *d = osmo_stream_srv_link_get_data(link);
624 struct cups_client *cc;
625
626 cc = talloc_zero(d, struct cups_client);
627 if (!cc)
628 return -1;
629
Harald Welte24557a72020-04-17 22:08:29 +0200630 cc->d = d;
Harald Weltef7365592020-04-15 21:48:45 +0200631 osmo_sock_get_name_buf(cc->sockname, sizeof(cc->sockname), fd);
632 cc->srv = osmo_stream_srv_create(cc, link, fd, cups_client_read_cb, cups_client_closed_cb, cc);
633 if (!cc->srv) {
634 talloc_free(cc);
635 return -1;
636 }
637 LOGCC(cc, LOGL_INFO, "Accepted new UECUPS connection\n");
638
639 llist_add_tail(&cc->list, &d->cups_clients);
640
641 return 0;
642}
643
644/***********************************************************************
645 * GTP Daemon
646 ***********************************************************************/
647
Harald Welte432a1302020-04-17 12:52:40 +0200648#ifndef OSMO_VTY_PORT_UECUPS
649#define OSMO_VTY_PORT_UECUPS 4268
650#endif
651
Harald Welte933dec72020-04-18 21:41:51 +0200652static void *g_tall_ctx;
Harald Weltef7365592020-04-15 21:48:45 +0200653struct gtp_daemon *g_daemon;
654static int g_daemonize;
Harald Welte27dc4c42020-04-20 12:39:13 +0200655static char *g_config_file = "osmo-uecups-daemon.cfg";
Harald Weltef7365592020-04-15 21:48:45 +0200656extern struct vty_app_info g_vty_info;
657
Harald Welte933dec72020-04-18 21:41:51 +0200658static void signal_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)
659{
660 switch (fdsi->ssi_signo) {
661 case SIGCHLD:
662 sigchild_cb(osfd, fdsi);
663 break;
664 case SIGUSR1:
665 talloc_report_full(g_tall_ctx, stderr);
666 break;
667 default:
668 break;
669 }
670}
671
Harald Weltef7365592020-04-15 21:48:45 +0200672static struct gtp_daemon *gtp_daemon_alloc(void *ctx)
673{
674 struct gtp_daemon *d = talloc_zero(ctx, struct gtp_daemon);
675 if (!d)
676 return NULL;
677
678 INIT_LLIST_HEAD(&d->gtp_endpoints);
679 INIT_LLIST_HEAD(&d->tun_devices);
680 INIT_LLIST_HEAD(&d->gtp_tunnels);
Harald Welte24557a72020-04-17 22:08:29 +0200681 INIT_LLIST_HEAD(&d->subprocesses);
Harald Weltef7365592020-04-15 21:48:45 +0200682 pthread_rwlock_init(&d->rwlock, NULL);
683 d->main_thread = pthread_self();
684
685 INIT_LLIST_HEAD(&d->cups_clients);
686
687 d->cfg.cups_local_ip = talloc_strdup(d, "localhost");
Harald Welte432a1302020-04-17 12:52:40 +0200688 d->cfg.cups_local_port = UECUPS_SCTP_PORT;
Harald Weltef7365592020-04-15 21:48:45 +0200689
690 return d;
691}
692
693static const struct log_info_cat log_categories[] = {
694 [DTUN] = {
695 .name ="DTUN",
696 .description = "Tunnel interface (tun device)",
697 .enabled = 1, .loglevel = LOGL_INFO,
698 },
699 [DEP] = {
700 .name = "DEP",
701 .description = "GTP endpoint (UDP socket)",
702 .enabled = 1, .loglevel = LOGL_INFO,
703 },
704 [DGT] = {
705 .name = "DGT",
706 .description = "GTP tunnel (session)",
707 .enabled = 1, .loglevel = LOGL_INFO,
708 },
709 [DUECUPS] = {
710 .name = "DUECUPS",
711 .description = "UE Control User Plane Separation",
712 .enabled = 1, .loglevel = LOGL_DEBUG,
713 },
714
715};
716
717static const struct log_info log_info = {
718 .cat = log_categories,
719 .num_cat = ARRAY_SIZE(log_categories),
720};
721
722int main(int argc, char **argv)
723{
Harald Weltef7365592020-04-15 21:48:45 +0200724 int rc;
725
Harald Welte933dec72020-04-18 21:41:51 +0200726 g_tall_ctx = talloc_named_const(NULL, 0, "root");
727 g_vty_info.tall_ctx = g_tall_ctx;
Harald Weltef7365592020-04-15 21:48:45 +0200728
729 osmo_init_ignore_signals();
Harald Welte933dec72020-04-18 21:41:51 +0200730 osmo_init_logging2(g_tall_ctx, &log_info);
Vadim Yanitskiy82809242022-01-28 20:32:12 +0600731 log_enable_multithread();
Harald Weltef7365592020-04-15 21:48:45 +0200732
Harald Welte933dec72020-04-18 21:41:51 +0200733 g_daemon = gtp_daemon_alloc(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200734 OSMO_ASSERT(g_daemon);
735
Harald Weltea932e4e2020-04-20 12:10:58 +0200736 msgb_talloc_ctx_init(g_tall_ctx, 10);
Harald Welte933dec72020-04-18 21:41:51 +0200737 osmo_stats_init(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200738 vty_init(&g_vty_info);
739 logging_vty_add_cmds();
740 osmo_talloc_vty_add_cmds();
741 osmo_stats_vty_add_cmds();
Harald Welte933dec72020-04-18 21:41:51 +0200742 rate_ctr_init(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200743 gtpud_vty_init();
744
745 init_netns();
746
747 rc = vty_read_config_file(g_config_file, NULL);
748 if (rc < 0) {
749 fprintf(stderr, "Failed to open config file: '%s'\n", g_config_file);
750 exit(2);
751 }
752
Harald Welte933dec72020-04-18 21:41:51 +0200753 rc = telnet_init_dynif(g_daemon, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_UECUPS);
Harald Weltef7365592020-04-15 21:48:45 +0200754 if (rc < 0)
755 exit(1);
756
757 g_daemon->cups_link = osmo_stream_srv_link_create(g_daemon);
758 if (!g_daemon->cups_link) {
759 fprintf(stderr, "Failed to create CUPS socket %s:%u (%s)\n",
760 g_daemon->cfg.cups_local_ip, g_daemon->cfg.cups_local_port, strerror(errno));
761 exit(1);
762 }
763
764 /* UECUPS socket for control from control plane side */
765 osmo_stream_srv_link_set_nodelay(g_daemon->cups_link, true);
766 osmo_stream_srv_link_set_addr(g_daemon->cups_link, g_daemon->cfg.cups_local_ip);
767 osmo_stream_srv_link_set_port(g_daemon->cups_link, g_daemon->cfg.cups_local_port);
768 osmo_stream_srv_link_set_proto(g_daemon->cups_link, IPPROTO_SCTP);
769 osmo_stream_srv_link_set_data(g_daemon->cups_link, g_daemon);
770 osmo_stream_srv_link_set_accept_cb(g_daemon->cups_link, cups_accept_cb);
771 osmo_stream_srv_link_open(g_daemon->cups_link);
772
Harald Welte24557a72020-04-17 22:08:29 +0200773 /* block SIGCHLD via normal delivery; redirect it to signalfd */
774 sigset_t sigset;
775 sigemptyset(&sigset);
776 sigaddset(&sigset, SIGCHLD);
Harald Welte933dec72020-04-18 21:41:51 +0200777 sigaddset(&sigset, SIGUSR1);
Harald Welte24557a72020-04-17 22:08:29 +0200778 sigprocmask(SIG_BLOCK, &sigset, NULL);
Harald Welte933dec72020-04-18 21:41:51 +0200779 g_daemon->signalfd = osmo_signalfd_setup(g_daemon, sigset, signal_cb, g_daemon);
780 osmo_init_ignore_signals();
Harald Welte24557a72020-04-17 22:08:29 +0200781
Harald Weltef7365592020-04-15 21:48:45 +0200782 if (g_daemonize) {
783 rc = osmo_daemonize();
784 if (rc < 0) {
785 perror("Error during daemonize");
786 exit(1);
787 }
788 }
789
790 while (1) {
791 osmo_select_main(0);
792 }
793}