blob: 0d049c70da0d02dd9cf50dee2ad20625a4060c32 [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 <errno.h>
10
11#include <pthread.h>
12
13#include <osmocom/core/linuxlist.h>
14#include <osmocom/core/talloc.h>
15#include <osmocom/core/logging.h>
16
17#include "internal.h"
18
19#define LOGT(t, lvl, fmt, args ...) \
20 LOGP(DGT, lvl, "%s: " fmt, (t)->name, ## args)
21
22/***********************************************************************
23 * GTP Tunnel
24 ***********************************************************************/
25struct gtp_tunnel *gtp_tunnel_alloc(struct gtp_daemon *d, const struct gtp_tunnel_params *cpars)
26{
27 struct gtp_tunnel *t;
28
29 t = talloc_zero(d, struct gtp_tunnel);
30 if (!t)
31 goto out_unlock;
32 t->d = d;
33 t->name = talloc_asprintf(t, "%s-R%08x-T%08x", cpars->tun_name, cpars->rx_teid, cpars->tx_teid);
34 t->tun_dev = tun_device_find_or_create(d, cpars->tun_name, cpars->tun_netns_name);
35 if (!t->tun_dev) {
36 LOGT(t, LOGL_ERROR, "Cannot find or create tun device %s\n", cpars->tun_name);
37 goto out_free;
38 }
39
40 t->gtp_ep = gtp_endpoint_find_or_create(d, &cpars->local_udp);
41 if (!t->gtp_ep) {
42 LOGT(t, LOGL_ERROR, "Cannot find or create GTP endpoint\n");
43 goto out_tun;
44 }
45
46 pthread_rwlock_wrlock(&d->rwlock);
47 /* check if we already have a tunnel with same Rx-TEID + endpoint */
48 if (_gtp_tunnel_find_r(d, cpars->rx_teid, t->gtp_ep)) {
49 LOGT(t, LOGL_ERROR, "Error: We already have a tunnel for RxTEID 0x%08x "
50 "on this endpoint (%s)\n", cpars->rx_teid, t->gtp_ep->name);
51 goto out_ep;
52 }
53
54 /* FIXME: check if we already have a tunnel with same Tx-TEID + peer */
55 /* FIXME: check if we already have a tunnel with same tun + EUA + filter */
56
57 t->rx_teid = cpars->rx_teid;
58 t->tx_teid = cpars->tx_teid;
59 memcpy(&t->user_addr, &cpars->user_addr, sizeof(t->user_addr));
60 memcpy(&t->remote_udp, &cpars->remote_udp, sizeof(t->remote_udp));
61
62 if (netdev_add_addr(t->tun_dev->nl, t->tun_dev->ifindex, &t->user_addr) < 0) {
63 LOGT(t, LOGL_ERROR, "Cannot add user addr to tun device: %s\n",
64 strerror(errno));
65 }
66
67 /* TODO: hash table? */
68 llist_add_tail(&t->list, &d->gtp_tunnels);
69 pthread_rwlock_unlock(&d->rwlock);
70 LOGT(t, LOGL_NOTICE, "Created\n");
71
72 return t;
73
74out_ep:
75 _gtp_endpoint_release(t->gtp_ep);
76out_tun:
77 _tun_device_release(t->tun_dev);
78out_free:
79 talloc_free(t);
80out_unlock:
81 pthread_rwlock_unlock(&d->rwlock);
82
83 return NULL;
84}
85
86/* find tunnel by R(x_teid), T(x_teid) + A(ddr) */
87static struct gtp_tunnel *
88_gtp_tunnel_find_rta(struct gtp_daemon *d, uint32_t rx_teid, uint32_t tx_teid,
89 const struct sockaddr_storage *user_addr)
90{
91 struct gtp_tunnel *t;
92 llist_for_each_entry(t, &d->gtp_tunnels, list) {
93 if (t->rx_teid == rx_teid && t->tx_teid == tx_teid &&
94 sockaddr_equals((struct sockaddr *) &t->user_addr, (struct sockaddr *)user_addr))
95 return t;
96 }
97 return NULL;
98}
99
100/* find tunnel by R(x_teid) + optionally local endpoint */
101struct gtp_tunnel *
102_gtp_tunnel_find_r(struct gtp_daemon *d, uint32_t rx_teid, struct gtp_endpoint *ep)
103{
104 struct gtp_tunnel *t;
105 llist_for_each_entry(t, &d->gtp_tunnels, list) {
106 if (t->rx_teid == rx_teid) {
107 if (!ep)
108 return t;
109 if (t->gtp_ep == ep)
110 return t;
111 }
112 }
113 return NULL;
114}
115
116/* UNLOCKED find tunnel by tun + EUA ip (+proto/port) */
117struct gtp_tunnel *
118_gtp_tunnel_find_eua(struct tun_device *tun, const struct sockaddr *sa, uint8_t proto)
119{
120 struct gtp_daemon *d = tun->d;
121 struct gtp_tunnel *t;
122
123 llist_for_each_entry(t, &d->gtp_tunnels, list) {
124 /* TODO: Find best matching filter */
125 if (t->tun_dev == tun && sockaddr_equals(sa, (struct sockaddr *) &t->user_addr))
126 return t;
127 }
128 return NULL;
129}
130
131/* UNLOCKED destroy of tunnel; drops references to EP + TUN */
132void _gtp_tunnel_destroy(struct gtp_tunnel *t)
133{
134 LOGT(t, LOGL_NOTICE, "Destroying\n");
135 /* talloc is not thread safe, all alloc/free must come from main thread */
136 ASSERT_MAIN_THREAD(t->d);
137
138 if (netdev_del_addr(t->tun_dev->nl, t->tun_dev->ifindex, &t->user_addr) < 0)
139 LOGT(t, LOGL_ERROR, "Cannot remove user address: %s\n", strerror(errno));
140
141 llist_del(&t->list);
142
143 /* drop reference to endpoint + tun */
144 _gtp_endpoint_release(t->gtp_ep);
145 _tun_device_release(t->tun_dev);
146
147 talloc_free(t);
148}
149
150bool gtp_tunnel_destroy(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr, uint32_t rx_teid)
151{
152 struct gtp_endpoint *ep;
153 bool rc = false;
154
155 pthread_rwlock_wrlock(&d->rwlock);
156 /* find endpoint for bind_addr */
157 ep = _gtp_endpoint_find(d, bind_addr);
158 if (ep) {
159 /* find tunnel for rx TEID within endpoint */
160 struct gtp_tunnel *t = _gtp_tunnel_find_r(d, rx_teid, ep);
161 if (t) {
162 _gtp_tunnel_destroy(t);
163 rc = true;
164 }
165 }
166 pthread_rwlock_unlock(&d->rwlock);
167
168 return rc;
169}