blob: 09ba52e5dbc38207b126ef20535b43293bcc1bba [file] [log] [blame]
Harald Weltef7365592020-04-15 21:48:45 +02001/* SPDX-License-Identifier: GPL-2.0 */
2#pragma once
3
4#include <stdint.h>
5#include <stdbool.h>
6#include <pthread.h>
7#include <sys/socket.h>
8#include <osmocom/core/linuxlist.h>
9#include <osmocom/core/write_queue.h>
10#include <osmocom/core/utils.h>
11
12struct nl_sock;
13struct osmo_stream_srv_link;
14
15/***********************************************************************
16 * Utility
17 ***********************************************************************/
18/* ensure we are called from main thread context */
19#define ASSERT_MAIN_THREAD(d) OSMO_ASSERT(pthread_self() == (d)->main_thread)
20
21#define MAX_UDP_PACKET 65535
22
23bool sockaddr_equals(const struct sockaddr *a, const struct sockaddr *b);
24
25struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto,
26 const char *host, uint16_t port, bool passive);
27enum {
28 DTUN,
29 DEP,
30 DGT,
31 DUECUPS,
32};
33
34/***********************************************************************
35 * netdev / netlink
36 ***********************************************************************/
37
38int netdev_add_addr(struct nl_sock *nlsk, int ifindex, const struct sockaddr_storage *ss);
39int netdev_del_addr(struct nl_sock *nlsk, int ifindex, const struct sockaddr_storage *ss);
40int netdev_set_link(struct nl_sock *nlsk, int ifindex, bool up);
41int netdev_add_defaultroute(struct nl_sock *nlsk, int ifindex, uint8_t family);
42
43
44/***********************************************************************
45 * GTP Endpoint (UDP socket)
46 ***********************************************************************/
47
48struct gtp_daemon;
49
50/* local UDP socket for GTP communication */
51struct gtp_endpoint {
52 /* entry in global list */
53 struct llist_head list;
54 /* back-pointer to daemon */
55 struct gtp_daemon *d;
56 unsigned long use_count;
57
58 /* file descriptor */
59 int fd;
60
61 /* local IP:port */
62 struct sockaddr_storage bind_addr;
63 char *name;
64
65 /* the thread handling Rx from the fd/socket */
66 pthread_t thread;
67};
68
69
70struct gtp_endpoint *
71gtp_endpoint_find_or_create(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr);
72
73struct gtp_endpoint *
74_gtp_endpoint_find(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr);
75
76void _gtp_endpoint_deref_destroy(struct gtp_endpoint *ep);
77
78bool _gtp_endpoint_release(struct gtp_endpoint *ep);
79
80bool gtp_endpoint_release(struct gtp_endpoint *ep);
81
82
83
84/***********************************************************************
85 * TUN Device
86 ***********************************************************************/
87
88struct tun_device {
89 /* entry in global list */
90 struct llist_head list;
91 /* back-pointer to daemon */
92 struct gtp_daemon *d;
93 unsigned long use_count;
94
95 /* which device we refer to */
96 const char *devname;
97 int ifindex;
98
99 /* file descriptor */
100 int fd;
101
102 /* network namespace */
103 const char *netns_name;
104 int netns_fd;
105
106 /* netlink socket in the namespace of the tun device */
107 struct nl_sock *nl;
108
109 /* list of local addresses? or simply only have the kernel know thses? */
110
111 /* the thread handling Rx from the tun fd */
112 pthread_t thread;
113};
114
115struct tun_device *
116tun_device_find_or_create(struct gtp_daemon *d, const char *devname, const char *netns_name);
117
118struct tun_device *
Harald Welte24557a72020-04-17 22:08:29 +0200119tun_device_find_netns(struct gtp_daemon *d, const char *netns_name);
120
121struct tun_device *
Harald Weltef7365592020-04-15 21:48:45 +0200122_tun_device_find(struct gtp_daemon *d, const char *devname);
123
124void _tun_device_deref_destroy(struct tun_device *tun);
125
126bool _tun_device_release(struct tun_device *tun);
127
128bool tun_device_release(struct tun_device *tun);
129
130
131
132/***********************************************************************
133 * GTP Tunnel
134 ***********************************************************************/
135
136/* Every tunnel is identified uniquely by the following tuples:
137 *
138 * a) local endpoint + TEID
139 * this is what happens on incoming GTP messages
140 *
141 * b) tun device + end-user-address (+ filter, if any)
142 * this is what happens when IP arrives on the tun device
143 */
144
145struct gtp_tunnel {
146 /* entry in global list / hash table */
147 struct llist_head list;
148 /* back-pointer to daemon */
149 struct gtp_daemon *d;
150
151 const char *name;
152
153 /* the TUN device associated with this tunnel */
154 struct tun_device *tun_dev;
155 /* the GTP endpoint (UDP socket) associated with this tunnel */
156 struct gtp_endpoint *gtp_ep;
157
158 /* TEID on transmit (host byte order) */
159 uint32_t tx_teid;
160 /* TEID one receive (host byte order) */
161 uint32_t rx_teid;
162
163 /* End user Address (inner IP) */
164 struct sockaddr_storage user_addr;
165
166 /* Remote UDP IP/Port*/
167 struct sockaddr_storage remote_udp;
168
169 /* TODO: Filter */
170};
171
172struct gtp_tunnel *
173_gtp_tunnel_find_r(struct gtp_daemon *d, uint32_t rx_teid, struct gtp_endpoint *ep);
174
175struct gtp_tunnel *
176_gtp_tunnel_find_eua(struct tun_device *tun, const struct sockaddr *sa, uint8_t proto);
177
178struct gtp_tunnel_params {
179 /* TEID in receive and transmit direction */
180 uint32_t rx_teid;
181 uint32_t tx_teid;
182
183 /* end user address */
184 struct sockaddr_storage user_addr;
185
186 /* remote GTP/UDP IP+Port */
187 struct sockaddr_storage remote_udp;
188
189 /* local GTP/UDP IP+Port (used to lookup/create local EP) */
190 struct sockaddr_storage local_udp;
191
192 /* local TUN device name (used to lookup/create local tun) */
193 const char *tun_name;
194 const char *tun_netns_name;
195};
196struct gtp_tunnel *gtp_tunnel_alloc(struct gtp_daemon *d, const struct gtp_tunnel_params *cpars);
197
198void _gtp_tunnel_destroy(struct gtp_tunnel *t);
199bool gtp_tunnel_destroy(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr, uint32_t rx_teid);
200
201
202/***********************************************************************
203 * GTP Daemon
204 ***********************************************************************/
205
Harald Welte432a1302020-04-17 12:52:40 +0200206#define UECUPS_SCTP_PORT 4268
207
Harald Welte24557a72020-04-17 22:08:29 +0200208struct osmo_signalfd;
209
Harald Weltef7365592020-04-15 21:48:45 +0200210struct gtp_daemon {
211 /* global lists of various objects */
212 struct llist_head gtp_endpoints;
213 struct llist_head tun_devices;
214 struct llist_head gtp_tunnels;
Harald Welte24557a72020-04-17 22:08:29 +0200215 struct llist_head subprocesses;
Harald Weltef7365592020-04-15 21:48:45 +0200216 /* lock protecting all of the above lists */
217 pthread_rwlock_t rwlock;
218 /* main thread ID */
219 pthread_t main_thread;
220 /* client CUPS interface */
221 struct llist_head cups_clients;
222 struct osmo_stream_srv_link *cups_link;
Harald Welte24557a72020-04-17 22:08:29 +0200223 struct osmo_signalfd *signalfd;
Harald Weltef7365592020-04-15 21:48:45 +0200224
225 struct {
226 char *cups_local_ip;
227 uint16_t cups_local_port;
228 } cfg;
229};
230extern struct gtp_daemon *g_daemon;
231
232int gtpud_vty_init(void);