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