blob: f9075f737e8b6b00873990a8fcd5b0393b8f474d [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
Harald Weltef23abd72020-04-20 12:09:32 +020086#if 0
Harald Weltef7365592020-04-15 21:48:45 +020087/* find tunnel by R(x_teid), T(x_teid) + A(ddr) */
88static struct gtp_tunnel *
89_gtp_tunnel_find_rta(struct gtp_daemon *d, uint32_t rx_teid, uint32_t tx_teid,
90 const struct sockaddr_storage *user_addr)
91{
92 struct gtp_tunnel *t;
93 llist_for_each_entry(t, &d->gtp_tunnels, list) {
94 if (t->rx_teid == rx_teid && t->tx_teid == tx_teid &&
95 sockaddr_equals((struct sockaddr *) &t->user_addr, (struct sockaddr *)user_addr))
96 return t;
97 }
98 return NULL;
99}
Harald Weltef23abd72020-04-20 12:09:32 +0200100#endif
Harald Weltef7365592020-04-15 21:48:45 +0200101
102/* find tunnel by R(x_teid) + optionally local endpoint */
103struct gtp_tunnel *
104_gtp_tunnel_find_r(struct gtp_daemon *d, uint32_t rx_teid, struct gtp_endpoint *ep)
105{
106 struct gtp_tunnel *t;
107 llist_for_each_entry(t, &d->gtp_tunnels, list) {
108 if (t->rx_teid == rx_teid) {
109 if (!ep)
110 return t;
111 if (t->gtp_ep == ep)
112 return t;
113 }
114 }
115 return NULL;
116}
117
118/* UNLOCKED find tunnel by tun + EUA ip (+proto/port) */
119struct gtp_tunnel *
120_gtp_tunnel_find_eua(struct tun_device *tun, const struct sockaddr *sa, uint8_t proto)
121{
122 struct gtp_daemon *d = tun->d;
123 struct gtp_tunnel *t;
124
125 llist_for_each_entry(t, &d->gtp_tunnels, list) {
126 /* TODO: Find best matching filter */
127 if (t->tun_dev == tun && sockaddr_equals(sa, (struct sockaddr *) &t->user_addr))
128 return t;
129 }
130 return NULL;
131}
132
133/* UNLOCKED destroy of tunnel; drops references to EP + TUN */
134void _gtp_tunnel_destroy(struct gtp_tunnel *t)
135{
136 LOGT(t, LOGL_NOTICE, "Destroying\n");
137 /* talloc is not thread safe, all alloc/free must come from main thread */
138 ASSERT_MAIN_THREAD(t->d);
139
140 if (netdev_del_addr(t->tun_dev->nl, t->tun_dev->ifindex, &t->user_addr) < 0)
141 LOGT(t, LOGL_ERROR, "Cannot remove user address: %s\n", strerror(errno));
142
143 llist_del(&t->list);
144
145 /* drop reference to endpoint + tun */
146 _gtp_endpoint_release(t->gtp_ep);
147 _tun_device_release(t->tun_dev);
148
149 talloc_free(t);
150}
151
152bool gtp_tunnel_destroy(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr, uint32_t rx_teid)
153{
154 struct gtp_endpoint *ep;
155 bool rc = false;
156
157 pthread_rwlock_wrlock(&d->rwlock);
158 /* find endpoint for bind_addr */
159 ep = _gtp_endpoint_find(d, bind_addr);
160 if (ep) {
161 /* find tunnel for rx TEID within endpoint */
162 struct gtp_tunnel *t = _gtp_tunnel_find_r(d, rx_teid, ep);
163 if (t) {
164 _gtp_tunnel_destroy(t);
165 rc = true;
166 }
167 }
168 pthread_rwlock_unlock(&d->rwlock);
169
170 return rc;
171}