blob: 9f1885fccd36775900433b7732b1ecf8774cf4c3 [file] [log] [blame]
Harald Weltef7365592020-04-15 21:48:45 +02001/* SPDX-License-Identifier: GPL-2.0 */
Pau Espin Pedrolafa22c62022-04-12 11:53:16 +02002#define _GNU_SOURCE
3#include <getopt.h>
Harald Weltef7365592020-04-15 21:48:45 +02004#include <unistd.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdio.h>
10#include <assert.h>
11#include <sys/types.h>
Harald Weltef9bb1772020-04-21 22:46:34 +020012#include <sys/wait.h>
Harald Welte24557a72020-04-17 22:08:29 +020013#include <sys/signalfd.h>
Harald Weltef7365592020-04-15 21:48:45 +020014#include <signal.h>
15#include <errno.h>
16
17#include <pthread.h>
18
19#include <osmocom/core/linuxlist.h>
20#include <osmocom/core/talloc.h>
21#include <osmocom/core/select.h>
22#include <osmocom/core/application.h>
23#include <osmocom/core/logging.h>
24#include <osmocom/core/stats.h>
25#include <osmocom/core/rate_ctr.h>
26#include <osmocom/core/socket.h>
Harald Welte24557a72020-04-17 22:08:29 +020027#include <osmocom/core/exec.h>
Harald Weltef7365592020-04-15 21:48:45 +020028#include <osmocom/vty/telnet_interface.h>
29#include <osmocom/vty/logging.h>
30#include <osmocom/vty/stats.h>
31#include <osmocom/vty/ports.h>
32#include <osmocom/vty/command.h>
33#include <osmocom/vty/misc.h>
34
35#include <osmocom/netif/stream.h>
36#include <netinet/sctp.h>
37
38#include <jansson.h>
39
40#include "internal.h"
41#include "netns.h"
42#include "gtp.h"
43
44/***********************************************************************
45 * Client (Contol/User Plane Separation) Socket
46 ***********************************************************************/
47
Harald Welte24557a72020-04-17 22:08:29 +020048#include <pwd.h>
49
Harald Weltef7365592020-04-15 21:48:45 +020050#define CUPS_MSGB_SIZE 1024
51
52#define LOGCC(cc, lvl, fmt, args ...) \
53 LOGP(DUECUPS, lvl, "%s: " fmt, (cc)->sockname, ## args)
54
55struct cups_client {
56 /* member in daemon->cups_clients */
57 struct llist_head list;
58 /* back-pointer to daemon */
59 struct gtp_daemon *d;
60 /* client socket */
61 struct osmo_stream_srv *srv;
62 char sockname[OSMO_SOCK_NAME_MAXLEN];
Pau Espin Pedrola459b5c2022-04-11 17:00:36 +020063 bool reset_all_state_res_pending;
Harald Weltef7365592020-04-15 21:48:45 +020064};
65
Harald Welte24557a72020-04-17 22:08:29 +020066struct subprocess {
67 /* member in daemon->cups_clients */
68 struct llist_head list;
69 /* pointer to the client that started us */
70 struct cups_client *cups_client;
71 /* PID of the process */
72 pid_t pid;
73};
74
Harald Welte01744692020-04-18 21:00:13 +020075/* kill the specified subprocess and forget about it */
76static void subprocess_destroy(struct subprocess *p, int signal)
77{
Pau Espin Pedrol687eafa2022-04-11 16:17:05 +020078 LOGCC(p->cups_client, LOGL_DEBUG, "Kill subprocess pid %llu with signal %u\n",
79 (unsigned long long)p->pid, signal);
Harald Welte01744692020-04-18 21:00:13 +020080 kill(p->pid, signal);
81 llist_del(&p->list);
82 talloc_free(p);
83}
84
Harald Weltef7365592020-04-15 21:48:45 +020085/* Send JSON to a given client/connection */
86static int cups_client_tx_json(struct cups_client *cc, json_t *jtx)
87{
88 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Tx JSON");
89 char *json_str = json_dumps(jtx, JSON_SORT_KEYS);
90 char *out;
91 int json_strlen;
92
93 json_decref(jtx);
94 if (!json_str) {
95 LOGCC(cc, LOGL_ERROR, "Error encoding JSON\n");
96 return 0;
97 }
98 json_strlen = strlen(json_str);
99
100 LOGCC(cc, LOGL_DEBUG, "JSON Tx '%s'\n", json_str);
101
102 if (json_strlen > msgb_tailroom(msg)) {
103 LOGCC(cc, LOGL_ERROR, "Not enough room for JSON in msgb\n");
104 free(json_str);
105 return 0;
106 }
107
108 out = (char *)msgb_put(msg, json_strlen);
109 memcpy(out, json_str, json_strlen);
110 free(json_str);
111 osmo_stream_srv_send(cc->srv, msg);
112
113 return 0;
114}
115
116static json_t *gen_uecups_result(const char *name, const char *res)
117{
118 json_t *jres = json_object();
119 json_t *jret = json_object();
120
121 json_object_set_new(jres, "result", json_string(res));
122 json_object_set_new(jret, name, jres);
123
124 return jret;
125}
126
127static int parse_ep(struct sockaddr_storage *out, json_t *in)
128{
129 json_t *jaddr_type, *jport, *jip;
130 const char *addr_type, *ip;
131 uint8_t buf[16];
132
133 /* {"addr_type":"IPV4","ip":"31323334","Port":2152} */
134
135 if (!json_is_object(in))
136 return -EINVAL;
137
138 jaddr_type = json_object_get(in, "addr_type");
139 jport = json_object_get(in, "Port");
140 jip = json_object_get(in, "ip");
141
142 if (!jaddr_type || !jport || !jip)
143 return -EINVAL;
144
145 if (!json_is_string(jaddr_type) || !json_is_integer(jport) || !json_is_string(jip))
146 return -EINVAL;
147
148 addr_type = json_string_value(jaddr_type);
149 ip = json_string_value(jip);
150
151 memset(out, 0, sizeof(*out));
152
153 if (!strcmp(addr_type, "IPV4")) {
154 struct sockaddr_in *sin = (struct sockaddr_in *) out;
155 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
156 return -EINVAL;
157 memcpy(&sin->sin_addr, buf, 4);
158 sin->sin_family = AF_INET;
159 sin->sin_port = htons(json_integer_value(jport));
160 } else if (!strcmp(addr_type, "IPV6")) {
161 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
162 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
163 return -EINVAL;
164 memcpy(&sin6->sin6_addr, buf, 16);
165 sin6->sin6_family = AF_INET6;
166 sin6->sin6_port = htons(json_integer_value(jport));
167 } else
168 return -EINVAL;
169
170 return 0;
171}
172
173static int parse_eua(struct sockaddr_storage *out, json_t *jip, json_t *jaddr_type)
174{
175 const char *addr_type, *ip;
176 uint8_t buf[16];
177
178 if (!json_is_string(jip) || !json_is_string(jaddr_type))
179 return -EINVAL;
180
181 addr_type = json_string_value(jaddr_type);
182 ip = json_string_value(jip);
183
184 memset(out, 0, sizeof(*out));
185
186 if (!strcmp(addr_type, "IPV4")) {
187 struct sockaddr_in *sin = (struct sockaddr_in *) out;
188 if (osmo_hexparse(ip, buf, sizeof(buf)) != 4)
189 return -EINVAL;
190 memcpy(&sin->sin_addr, buf, 4);
191 sin->sin_family = AF_INET;
192 } else if (!strcmp(addr_type, "IPV6")) {
193 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) out;
194 if (osmo_hexparse(ip, buf, sizeof(buf)) != 16)
195 return -EINVAL;
196 memcpy(&sin6->sin6_addr, buf, 16);
197 sin6->sin6_family = AF_INET6;
198 } else
199 return -EINVAL;
200
201 return 0;
202}
203
204
205static int parse_create_tun(struct gtp_tunnel_params *out, json_t *ctun)
206{
207 json_t *jlocal_gtp_ep, *jremote_gtp_ep;
208 json_t *jrx_teid, *jtx_teid;
209 json_t *jtun_dev_name, *jtun_netns_name;
210 json_t *juser_addr, *juser_addr_type;
211 int rc;
212
213 /* '{"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"}}' */
214
215 if (!json_is_object(ctun))
216 return -EINVAL;
217
218 /* mandatory IEs */
219 jlocal_gtp_ep = json_object_get(ctun, "local_gtp_ep");
220 jremote_gtp_ep = json_object_get(ctun, "remote_gtp_ep");
221 jrx_teid = json_object_get(ctun, "rx_teid");
222 jtx_teid = json_object_get(ctun, "tx_teid");
223 jtun_dev_name = json_object_get(ctun, "tun_dev_name");
224 juser_addr = json_object_get(ctun, "user_addr");
225 juser_addr_type = json_object_get(ctun, "user_addr_type");
226
227 if (!jlocal_gtp_ep || !jremote_gtp_ep || !jrx_teid || !jtx_teid || !jtun_dev_name ||
228 !juser_addr || !juser_addr_type)
229 return -EINVAL;
230 if (!json_is_object(jlocal_gtp_ep) || !json_is_object(jremote_gtp_ep) ||
231 !json_is_integer(jrx_teid) || !json_is_integer(jtx_teid) ||
232 !json_is_string(jtun_dev_name) ||
233 !json_is_string(juser_addr) || !json_is_string(juser_addr_type))
234 return -EINVAL;
235
236 memset(out, 0, sizeof(*out));
237
238 rc = parse_ep(&out->local_udp, jlocal_gtp_ep);
239 if (rc < 0)
240 return rc;
241 rc = parse_ep(&out->remote_udp, jremote_gtp_ep);
242 if (rc < 0)
243 return rc;
244 rc = parse_eua(&out->user_addr, juser_addr, juser_addr_type);
245 if (rc < 0)
246 return rc;
247 out->rx_teid = json_integer_value(jrx_teid);
248 out->tx_teid = json_integer_value(jtx_teid);
249 out->tun_name = talloc_strdup(out, json_string_value(jtun_dev_name));
250
251 /* optional IEs */
252 jtun_netns_name = json_object_get(ctun, "tun_netns_name");
253 if (jtun_netns_name) {
254 if (!json_is_string(jtun_netns_name))
255 return -EINVAL;
256 out->tun_netns_name = talloc_strdup(out, json_string_value(jtun_netns_name));
257 }
258
259 return 0;
260}
261
262
263static int cups_client_handle_create_tun(struct cups_client *cc, json_t *ctun)
264{
265 int rc;
266 struct gtp_tunnel_params *tpars = talloc_zero(cc, struct gtp_tunnel_params);
267 struct gtp_tunnel *t;
268
269 rc = parse_create_tun(tpars, ctun);
270 if (rc < 0) {
271 talloc_free(tpars);
272 return rc;
273 }
274
275 t = gtp_tunnel_alloc(g_daemon, tpars);
276 if (!t) {
277 LOGCC(cc, LOGL_NOTICE, "Failed to allocate tunnel\n");
278 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "ERR_NOT_FOUND"));
279 } else {
280 cups_client_tx_json(cc, gen_uecups_result("create_tun_res", "OK"));
281 }
282
283 talloc_free(tpars);
284 return 0;
285}
286
287static int cups_client_handle_destroy_tun(struct cups_client *cc, json_t *dtun)
288{
289 struct sockaddr_storage local_ep_addr;
290 json_t *jlocal_gtp_ep, *jrx_teid;
291 uint32_t rx_teid;
292 int rc;
293
294 jlocal_gtp_ep = json_object_get(dtun, "local_gtp_ep");
295 jrx_teid = json_object_get(dtun, "rx_teid");
296
297 if (!jlocal_gtp_ep || !jrx_teid)
298 return -EINVAL;
299
300 if (!json_is_object(jlocal_gtp_ep) || !json_is_integer(jrx_teid))
301 return -EINVAL;
302
303 rc = parse_ep(&local_ep_addr, jlocal_gtp_ep);
304 if (rc < 0)
305 return rc;
306 rx_teid = json_integer_value(jrx_teid);
307
308 rc = gtp_tunnel_destroy(g_daemon, &local_ep_addr, rx_teid);
309 if (rc < 0) {
310 LOGCC(cc, LOGL_NOTICE, "Failed to destroy tunnel\n");
311 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "ERR_NOT_FOUND"));
312 } else {
313 cups_client_tx_json(cc, gen_uecups_result("destroy_tun_res", "OK"));
314 }
315
316 return 0;
317}
318
Harald Welte24557a72020-04-17 22:08:29 +0200319static json_t *gen_uecups_term_ind(pid_t pid, int status)
320{
321 json_t *jterm = json_object();
322 json_t *jret = json_object();
323
324 json_object_set_new(jterm, "pid", json_integer(pid));
325 json_object_set_new(jterm, "exit_code", json_integer(status));
326
327 json_object_set_new(jret, "program_term_ind", jterm);
328
329 return jret;
330}
331
332
333static struct subprocess *subprocess_by_pid(struct gtp_daemon *d, pid_t pid)
334{
335 struct subprocess *sproc;
336 llist_for_each_entry(sproc, &d->subprocesses, list) {
337 if (sproc->pid == pid)
338 return sproc;
339 }
340 return NULL;
341}
342
Harald Weltef9bb1772020-04-21 22:46:34 +0200343static void child_terminated(struct gtp_daemon *d, int pid, int status)
Harald Welte24557a72020-04-17 22:08:29 +0200344{
Harald Welte24557a72020-04-17 22:08:29 +0200345 struct subprocess *sproc;
346 json_t *jterm_ind;
347
Harald Weltef9bb1772020-04-21 22:46:34 +0200348 LOGP(DUECUPS, LOGL_DEBUG, "SIGCHLD receive from pid %u; status=%d\n", pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200349
Harald Weltef9bb1772020-04-21 22:46:34 +0200350 sproc = subprocess_by_pid(d, pid);
Harald Welte24557a72020-04-17 22:08:29 +0200351 if (!sproc) {
352 LOGP(DUECUPS, LOGL_NOTICE, "subprocess %u terminated (status=%d) but we don't know it?\n",
Harald Weltef9bb1772020-04-21 22:46:34 +0200353 pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200354 return;
355 }
356
Harald Weltef9bb1772020-04-21 22:46:34 +0200357 /* generate prog_term_ind towards control plane */
358 jterm_ind = gen_uecups_term_ind(pid, status);
Harald Welte24557a72020-04-17 22:08:29 +0200359 if (!jterm_ind)
360 return;
361
362 cups_client_tx_json(sproc->cups_client, jterm_ind);
363
364 llist_del(&sproc->list);
365 talloc_free(sproc);
366}
367
Harald Weltef9bb1772020-04-21 22:46:34 +0200368static void sigchild_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)
369{
370 struct gtp_daemon *d = osfd->data;
371 int pid, status;
372
373 OSMO_ASSERT(fdsi->ssi_signo == SIGCHLD);
374
Harald Weltef9bb1772020-04-21 22:46:34 +0200375 /* it is known that classic signals coalesce: If you get multiple signals of the
Pau Espin Pedrol95ad8c92022-04-11 15:47:36 +0200376 * same type before a process is scheduled, the subsequent signals are dropped. This
Harald Weltef9bb1772020-04-21 22:46:34 +0200377 * makes sense for SIGINT or something like this, but for SIGCHLD carrying the PID of
378 * the terminated process, it doesn't really. Linux had the chance to fix this when
379 * introducing signalfd() - but the developers decided not to fix it. So the signalfd_siginfo
380 * contains the PID of one process that terminated - but there may be any number of other
381 * processes that also have terminated, and for which we don't get events this way. */
382
383 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
384 child_terminated(d, pid, status);
385
386}
387
Harald Welte24557a72020-04-17 22:08:29 +0200388static json_t *gen_uecups_start_res(pid_t pid, const char *result)
389{
390 json_t *ret = gen_uecups_result("start_program_res", result);
391 json_object_set_new(json_object_get(ret, "start_program_res"), "pid", json_integer(pid));
392
393 return ret;
394}
395
396static int cups_client_handle_start_program(struct cups_client *cc, json_t *sprog)
397{
398 json_t *juser, *jcmd, *jenv, *jnetns, *jres;
399 struct gtp_daemon *d = cc->d;
400 const char *cmd, *user;
401 char **addl_env = NULL;
402 sigset_t oldmask;
Harald Weltef23abd72020-04-20 12:09:32 +0200403 int nsfd = -1, rc;
Harald Welte24557a72020-04-17 22:08:29 +0200404
405 juser = json_object_get(sprog, "run_as_user");
406 jcmd = json_object_get(sprog, "command");
407 jenv = json_object_get(sprog, "environment");
408 jnetns = json_object_get(sprog, "tun_netns_name");
409
410 /* mandatory parts */
411 if (!juser || !jcmd)
412 return -EINVAL;
413 if (!json_is_string(juser) || !json_is_string(jcmd))
414 return -EINVAL;
415
416 /* optional parts */
417 if (jenv && !json_is_array(jenv))
418 return -EINVAL;
419 if (jnetns && !json_is_string(jnetns))
420 return -EINVAL;
421
422 cmd = json_string_value(jcmd);
423 user = json_string_value(juser);
424 if (jnetns) {
425 struct tun_device *tun = tun_device_find_netns(d, json_string_value(jnetns));
426 if (!tun)
427 return -ENODEV;
428 nsfd = tun->netns_fd;
429 }
430
431 /* build environment */
432 if (jenv) {
433 json_t *j;
434 int i;
435 addl_env = talloc_zero_array(cc, char *, json_array_size(jenv)+1);
436 if (!addl_env)
437 return -ENOMEM;
438 json_array_foreach(jenv, i, j) {
439 addl_env[i] = talloc_strdup(addl_env, json_string_value(j));
440 }
441 }
442
443 if (jnetns) {
444 rc = switch_ns(nsfd, &oldmask);
445 if (rc < 0) {
446 talloc_free(addl_env);
447 return -EIO;
448 }
449 }
450
451 rc = osmo_system_nowait2(cmd, osmo_environment_whitelist, addl_env, user);
452
453 if (jnetns) {
454 OSMO_ASSERT(restore_ns(&oldmask) == 0);
455 }
456
457 talloc_free(addl_env);
458
459 if (rc > 0) {
460 /* create a record about the subprocess we started, so we can notify the
461 * client that crated it upon termination */
462 struct subprocess *sproc = talloc_zero(cc, struct subprocess);
463 if (!sproc)
464 return -ENOMEM;
465
466 sproc->cups_client = cc;
467 sproc->pid = rc;
468 llist_add_tail(&sproc->list, &d->subprocesses);
469 jres = gen_uecups_start_res(sproc->pid, "OK");
470 } else {
471 jres = gen_uecups_start_res(0, "ERR_INVALID_DATA");
472 }
473
474 cups_client_tx_json(cc, jres);
475
476 return 0;
477}
478
Harald Welte01744692020-04-18 21:00:13 +0200479static int cups_client_handle_reset_all_state(struct cups_client *cc, json_t *sprog)
480{
481 struct gtp_daemon *d = cc->d;
482 struct gtp_tunnel *t, *t2;
483 struct subprocess *p, *p2;
484 json_t *jres;
485
Pau Espin Pedrol687eafa2022-04-11 16:17:05 +0200486 LOGCC(cc, LOGL_DEBUG, "Destroying all tunnels\n");
Harald Welte01744692020-04-18 21:00:13 +0200487 pthread_rwlock_wrlock(&d->rwlock);
488 llist_for_each_entry_safe(t, t2, &d->gtp_tunnels, list) {
489 _gtp_tunnel_destroy(t);
490 }
491 pthread_rwlock_unlock(&d->rwlock);
492
493 /* no locking needed as this list is only used by main thread */
Pau Espin Pedrol687eafa2022-04-11 16:17:05 +0200494 LOGCC(cc, LOGL_DEBUG, "Destroying all subprocesses\n");
Harald Welte01744692020-04-18 21:00:13 +0200495 llist_for_each_entry_safe(p, p2, &d->subprocesses, list) {
496 subprocess_destroy(p, SIGKILL);
497 }
498
Pau Espin Pedrola459b5c2022-04-11 17:00:36 +0200499 if (d->reset_all_state_tun_remaining == 0) {
500 jres = gen_uecups_result("reset_all_state_res", "OK");
501 cups_client_tx_json(cc, jres);
502 } else {
503 cc->reset_all_state_res_pending = true;
504 }
Harald Welte01744692020-04-18 21:00:13 +0200505
506 return 0;
507}
Harald Welte24557a72020-04-17 22:08:29 +0200508
Harald Weltef7365592020-04-15 21:48:45 +0200509static int cups_client_handle_json(struct cups_client *cc, json_t *jroot)
510{
511 void *iter;
512 const char *key;
513 json_t *cmd;
514 int rc;
515
516 if (!json_is_object(jroot))
517 return -EINVAL;
518
519 iter = json_object_iter(jroot);
520 key = json_object_iter_key(iter);
521 cmd = json_object_iter_value(iter);
522 if (!iter || !key || !cmd)
523 return -EINVAL;
524
525 if (!strcmp(key, "create_tun")) {
526 rc = cups_client_handle_create_tun(cc, cmd);
527 } else if (!strcmp(key, "destroy_tun")) {
528 rc = cups_client_handle_destroy_tun(cc, cmd);
Harald Welte24557a72020-04-17 22:08:29 +0200529 } else if (!strcmp(key, "start_program")) {
530 rc = cups_client_handle_start_program(cc, cmd);
Harald Welte01744692020-04-18 21:00:13 +0200531 } else if (!strcmp(key, "reset_all_state")) {
532 rc = cups_client_handle_reset_all_state(cc, cmd);
Harald Weltef7365592020-04-15 21:48:45 +0200533 } else {
534 LOGCC(cc, LOGL_NOTICE, "Unknown command '%s' received\n", key);
535 return -EINVAL;
536 }
537
538 if (rc < 0) {
539 LOGCC(cc, LOGL_NOTICE, "Error %d handling '%s' command\n", rc, key);
540 char buf[64];
541 snprintf(buf, sizeof(buf), "%s_res", key);
542 cups_client_tx_json(cc, gen_uecups_result(buf, "ERR_INVALID_DATA"));
543 return -EINVAL;
544 }
545
546 return 0;
547}
548
549/* control/user plane separation per-client read cb */
550static int cups_client_read_cb(struct osmo_stream_srv *conn)
551{
552 struct osmo_fd *ofd = osmo_stream_srv_get_ofd(conn);
553 struct cups_client *cc = osmo_stream_srv_get_data(conn);
554 struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Rx JSON");
555 struct sctp_sndrcvinfo sinfo;
556 json_error_t jerr;
557 json_t *jroot;
558 int flags = 0;
559 int rc = 0;
560
561 /* Read message from socket */
562 /* we cannot use osmo_stream_srv_recv() here, as we might get some out-of-band info from
563 * SCTP. FIXME: add something like osmo_stream_srv_recv_sctp() to libosmo-netif and use
564 * it here as well as in libosmo-sigtran and osmo-msc */
565 rc = sctp_recvmsg(ofd->fd, msg->tail, msgb_tailroom(msg), NULL, NULL, &sinfo,&flags);
566 if (rc <= 0) {
567 osmo_stream_srv_destroy(conn);
568 rc = -1;
569 goto out;
570 } else
571 msgb_put(msg, rc);
572
573 if (flags & MSG_NOTIFICATION) {
574 union sctp_notification *notif = (union sctp_notification *) msgb_data(msg);
575 switch (notif->sn_header.sn_type) {
576 case SCTP_SHUTDOWN_EVENT:
577 osmo_stream_srv_destroy(conn);
578 rc = -EBADF;
579 goto out;
580 default:
581 break;
582 }
583 goto out;
584 }
585
586 LOGCC(cc, LOGL_DEBUG, "Rx '%s'\n", msgb_data(msg));
587
588 /* Parse the JSON */
589 jroot = json_loadb((const char *) msgb_data(msg), msgb_length(msg), 0, &jerr);
590 if (!jroot) {
591 LOGCC(cc, LOGL_ERROR, "Error decoding JSON (%s)", jerr.text);
592 rc = -1;
593 goto out;
594 }
595
596 /* Dispatch */
597 rc = cups_client_handle_json(cc, jroot);
598
599 json_decref(jroot);
600 msgb_free(msg);
601
602 return 0;
603out:
604 msgb_free(msg);
605 return rc;
606}
607
608static int cups_client_closed_cb(struct osmo_stream_srv *conn)
609{
610 struct cups_client *cc = osmo_stream_srv_get_data(conn);
Harald Welte24557a72020-04-17 22:08:29 +0200611 struct gtp_daemon *d = cc->d;
612 struct subprocess *p, *p2;
613
614 /* kill + forget about all subprocesses of this client */
Harald Welte01744692020-04-18 21:00:13 +0200615 /* We need no locking here as the subprocess list is only used from the main thread */
Harald Welte24557a72020-04-17 22:08:29 +0200616 llist_for_each_entry_safe(p, p2, &d->subprocesses, list) {
Harald Welte01744692020-04-18 21:00:13 +0200617 if (p->cups_client == cc)
618 subprocess_destroy(p, SIGKILL);
Harald Welte24557a72020-04-17 22:08:29 +0200619 }
Harald Weltef7365592020-04-15 21:48:45 +0200620
621 LOGCC(cc, LOGL_INFO, "UECUPS connection lost\n");
622 llist_del(&cc->list);
623 return 0;
624}
625
626
627/* the control/user plane separation server bind/accept fd */
628static int cups_accept_cb(struct osmo_stream_srv_link *link, int fd)
629{
630 struct gtp_daemon *d = osmo_stream_srv_link_get_data(link);
631 struct cups_client *cc;
632
633 cc = talloc_zero(d, struct cups_client);
634 if (!cc)
635 return -1;
636
Harald Welte24557a72020-04-17 22:08:29 +0200637 cc->d = d;
Harald Weltef7365592020-04-15 21:48:45 +0200638 osmo_sock_get_name_buf(cc->sockname, sizeof(cc->sockname), fd);
639 cc->srv = osmo_stream_srv_create(cc, link, fd, cups_client_read_cb, cups_client_closed_cb, cc);
640 if (!cc->srv) {
641 talloc_free(cc);
642 return -1;
643 }
644 LOGCC(cc, LOGL_INFO, "Accepted new UECUPS connection\n");
645
646 llist_add_tail(&cc->list, &d->cups_clients);
647
648 return 0;
649}
650
651/***********************************************************************
652 * GTP Daemon
653 ***********************************************************************/
654
Harald Welte432a1302020-04-17 12:52:40 +0200655#ifndef OSMO_VTY_PORT_UECUPS
656#define OSMO_VTY_PORT_UECUPS 4268
657#endif
658
Harald Welte933dec72020-04-18 21:41:51 +0200659static void *g_tall_ctx;
Harald Weltef7365592020-04-15 21:48:45 +0200660struct gtp_daemon *g_daemon;
661static int g_daemonize;
Harald Welte27dc4c42020-04-20 12:39:13 +0200662static char *g_config_file = "osmo-uecups-daemon.cfg";
Harald Weltef7365592020-04-15 21:48:45 +0200663extern struct vty_app_info g_vty_info;
664
Harald Welte933dec72020-04-18 21:41:51 +0200665static void signal_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)
666{
667 switch (fdsi->ssi_signo) {
668 case SIGCHLD:
669 sigchild_cb(osfd, fdsi);
670 break;
671 case SIGUSR1:
672 talloc_report_full(g_tall_ctx, stderr);
673 break;
674 default:
675 break;
676 }
677}
678
Pau Espin Pedrola459b5c2022-04-11 17:00:36 +0200679static void gtp_daemon_itq_read_cb(struct osmo_it_q *q, struct llist_head *item)
680{
681 struct gtp_daemon *d = (struct gtp_daemon *)q->data;
682 struct gtp_daemon_itq_msg *itq_msg = container_of(item, struct gtp_daemon_itq_msg, list);
683
684 LOGP(DTUN, LOGL_DEBUG, "Rx new itq message from %s\n",
685 itq_msg->tun_released.tun->devname);
686
687 _tun_device_destroy(itq_msg->tun_released.tun);
688 if (d->reset_all_state_tun_remaining > 0) {
689 d->reset_all_state_tun_remaining--;
690 if (d->reset_all_state_tun_remaining == 0) {
691 struct cups_client *cc;
692 llist_for_each_entry(cc, &d->cups_clients, list) {
693 json_t *jres;
694 if (!cc->reset_all_state_res_pending)
695 continue;
696 cc->reset_all_state_res_pending = false;
697 jres = gen_uecups_result("reset_all_state_res", "OK");
698 cups_client_tx_json(cc, jres);
699 }
700 }
701 }
702}
703
Harald Weltef7365592020-04-15 21:48:45 +0200704static struct gtp_daemon *gtp_daemon_alloc(void *ctx)
705{
706 struct gtp_daemon *d = talloc_zero(ctx, struct gtp_daemon);
707 if (!d)
708 return NULL;
709
710 INIT_LLIST_HEAD(&d->gtp_endpoints);
711 INIT_LLIST_HEAD(&d->tun_devices);
712 INIT_LLIST_HEAD(&d->gtp_tunnels);
Harald Welte24557a72020-04-17 22:08:29 +0200713 INIT_LLIST_HEAD(&d->subprocesses);
Harald Weltef7365592020-04-15 21:48:45 +0200714 pthread_rwlock_init(&d->rwlock, NULL);
715 d->main_thread = pthread_self();
716
Pau Espin Pedrola459b5c2022-04-11 17:00:36 +0200717 d->itq = osmo_it_q_alloc(d, "itq", 4096, gtp_daemon_itq_read_cb, d);
718 osmo_fd_register(&d->itq->event_ofd);
719
Harald Weltef7365592020-04-15 21:48:45 +0200720 INIT_LLIST_HEAD(&d->cups_clients);
721
722 d->cfg.cups_local_ip = talloc_strdup(d, "localhost");
Harald Welte432a1302020-04-17 12:52:40 +0200723 d->cfg.cups_local_port = UECUPS_SCTP_PORT;
Harald Weltef7365592020-04-15 21:48:45 +0200724
725 return d;
726}
727
728static const struct log_info_cat log_categories[] = {
729 [DTUN] = {
730 .name ="DTUN",
731 .description = "Tunnel interface (tun device)",
732 .enabled = 1, .loglevel = LOGL_INFO,
733 },
734 [DEP] = {
735 .name = "DEP",
736 .description = "GTP endpoint (UDP socket)",
737 .enabled = 1, .loglevel = LOGL_INFO,
738 },
739 [DGT] = {
740 .name = "DGT",
741 .description = "GTP tunnel (session)",
742 .enabled = 1, .loglevel = LOGL_INFO,
743 },
744 [DUECUPS] = {
745 .name = "DUECUPS",
746 .description = "UE Control User Plane Separation",
747 .enabled = 1, .loglevel = LOGL_DEBUG,
748 },
749
750};
751
752static const struct log_info log_info = {
753 .cat = log_categories,
754 .num_cat = ARRAY_SIZE(log_categories),
755};
756
Pau Espin Pedrolafa22c62022-04-12 11:53:16 +0200757static void print_help()
758{
759 printf("Some useful options:\n");
760 printf(" -h --help is printing this text.\n");
761 printf(" -c --config-file filename The config file to use.\n");
762 printf(" -s --disable-color\n");
763 printf(" -D --daemonize Fork the process into a background daemon\n");
764 printf(" -V --version Print the version number\n");
765
766 printf("\nVTY reference generation:\n");
767 printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n");
768 printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n");
769}
770
771static void handle_long_options(const char *prog_name, const int long_option)
772{
773 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
774
775 switch (long_option) {
776 case 1:
777 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
778 if (vty_ref_mode < 0) {
779 fprintf(stderr, "%s: Unknown VTY reference generation "
780 "mode '%s'\n", prog_name, optarg);
781 exit(2);
782 }
783 break;
784 case 2:
785 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
786 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
787 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
788 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
789 exit(0);
790 default:
791 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
792 exit(2);
793 }
794}
795
796static void handle_options(int argc, char **argv)
797{
798 while (1) {
799 int option_index = 0, c;
800 static int long_option = 0;
801 static struct option long_options[] = {
802 {"help", 0, 0, 'h'},
803 {"config-file", 1, 0, 'c'},
804 {"daemonize", 0, 0, 'D'},
805 {"version", 0, 0, 'V'},
806 {"disable-color", 0, 0, 's'},
807 {"vty-ref-mode", 1, &long_option, 1},
808 {"vty-ref-xml", 0, &long_option, 2},
809 {0, 0, 0, 0},
810 };
811
812 c = getopt_long(argc, argv, "hc:sVD", long_options, &option_index);
813
814 if (c == -1)
815 break;
816
817 switch (c) {
818 case 'h':
819 print_help();
820 exit(0);
821 break;
822 case 0:
823 handle_long_options(argv[0], long_option);
824 break;
825 case 'c':
826 g_config_file = talloc_strdup(g_tall_ctx, optarg);
827 break;
828 case 's':
829 log_set_use_color(osmo_stderr_target, 0);
830 break;
831 case 'V':
832 print_version(1);
833 exit(0);
834 break;
835 case 'D':
836 g_daemonize = 1;
837 break;
838 default:
839 /* ignore */
840 break;
841 };
842 }
843 if (argc > optind) {
844 fprintf(stderr, "Unsupported positional arguments on command line\n");
845 exit(2);
846 }
847}
848
Harald Weltef7365592020-04-15 21:48:45 +0200849int main(int argc, char **argv)
850{
Harald Weltef7365592020-04-15 21:48:45 +0200851 int rc;
852
Harald Welte933dec72020-04-18 21:41:51 +0200853 g_tall_ctx = talloc_named_const(NULL, 0, "root");
854 g_vty_info.tall_ctx = g_tall_ctx;
Harald Weltef7365592020-04-15 21:48:45 +0200855
856 osmo_init_ignore_signals();
Harald Welte933dec72020-04-18 21:41:51 +0200857 osmo_init_logging2(g_tall_ctx, &log_info);
Vadim Yanitskiy82809242022-01-28 20:32:12 +0600858 log_enable_multithread();
Harald Weltef7365592020-04-15 21:48:45 +0200859
Harald Welte933dec72020-04-18 21:41:51 +0200860 g_daemon = gtp_daemon_alloc(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200861 OSMO_ASSERT(g_daemon);
862
Harald Weltea932e4e2020-04-20 12:10:58 +0200863 msgb_talloc_ctx_init(g_tall_ctx, 10);
Harald Welte933dec72020-04-18 21:41:51 +0200864 osmo_stats_init(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200865 vty_init(&g_vty_info);
866 logging_vty_add_cmds();
867 osmo_talloc_vty_add_cmds();
868 osmo_stats_vty_add_cmds();
Harald Welte933dec72020-04-18 21:41:51 +0200869 rate_ctr_init(g_tall_ctx);
Harald Weltef7365592020-04-15 21:48:45 +0200870 gtpud_vty_init();
871
Pau Espin Pedrolafa22c62022-04-12 11:53:16 +0200872 handle_options(argc, argv);
873
Harald Weltef7365592020-04-15 21:48:45 +0200874 init_netns();
875
876 rc = vty_read_config_file(g_config_file, NULL);
877 if (rc < 0) {
878 fprintf(stderr, "Failed to open config file: '%s'\n", g_config_file);
879 exit(2);
880 }
881
Harald Welte933dec72020-04-18 21:41:51 +0200882 rc = telnet_init_dynif(g_daemon, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_UECUPS);
Harald Weltef7365592020-04-15 21:48:45 +0200883 if (rc < 0)
884 exit(1);
885
886 g_daemon->cups_link = osmo_stream_srv_link_create(g_daemon);
887 if (!g_daemon->cups_link) {
888 fprintf(stderr, "Failed to create CUPS socket %s:%u (%s)\n",
889 g_daemon->cfg.cups_local_ip, g_daemon->cfg.cups_local_port, strerror(errno));
890 exit(1);
891 }
892
893 /* UECUPS socket for control from control plane side */
894 osmo_stream_srv_link_set_nodelay(g_daemon->cups_link, true);
895 osmo_stream_srv_link_set_addr(g_daemon->cups_link, g_daemon->cfg.cups_local_ip);
896 osmo_stream_srv_link_set_port(g_daemon->cups_link, g_daemon->cfg.cups_local_port);
897 osmo_stream_srv_link_set_proto(g_daemon->cups_link, IPPROTO_SCTP);
898 osmo_stream_srv_link_set_data(g_daemon->cups_link, g_daemon);
899 osmo_stream_srv_link_set_accept_cb(g_daemon->cups_link, cups_accept_cb);
900 osmo_stream_srv_link_open(g_daemon->cups_link);
901
Harald Welte24557a72020-04-17 22:08:29 +0200902 /* block SIGCHLD via normal delivery; redirect it to signalfd */
903 sigset_t sigset;
904 sigemptyset(&sigset);
905 sigaddset(&sigset, SIGCHLD);
Harald Welte933dec72020-04-18 21:41:51 +0200906 sigaddset(&sigset, SIGUSR1);
Harald Welte24557a72020-04-17 22:08:29 +0200907 sigprocmask(SIG_BLOCK, &sigset, NULL);
Harald Welte933dec72020-04-18 21:41:51 +0200908 g_daemon->signalfd = osmo_signalfd_setup(g_daemon, sigset, signal_cb, g_daemon);
909 osmo_init_ignore_signals();
Harald Welte24557a72020-04-17 22:08:29 +0200910
Harald Weltef7365592020-04-15 21:48:45 +0200911 if (g_daemonize) {
912 rc = osmo_daemonize();
913 if (rc < 0) {
914 perror("Error during daemonize");
915 exit(1);
916 }
917 }
918
919 while (1) {
920 osmo_select_main(0);
921 }
922}