Harald Welte | f736559 | 2020-04-15 21:48:45 +0200 | [diff] [blame] | 1 | /* 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 | |
| 12 | struct nl_sock; |
| 13 | struct 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 | |
| 23 | bool sockaddr_equals(const struct sockaddr *a, const struct sockaddr *b); |
| 24 | |
| 25 | struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto, |
| 26 | const char *host, uint16_t port, bool passive); |
| 27 | enum { |
| 28 | DTUN, |
| 29 | DEP, |
| 30 | DGT, |
| 31 | DUECUPS, |
| 32 | }; |
| 33 | |
| 34 | /*********************************************************************** |
| 35 | * netdev / netlink |
| 36 | ***********************************************************************/ |
| 37 | |
| 38 | int netdev_add_addr(struct nl_sock *nlsk, int ifindex, const struct sockaddr_storage *ss); |
| 39 | int netdev_del_addr(struct nl_sock *nlsk, int ifindex, const struct sockaddr_storage *ss); |
| 40 | int netdev_set_link(struct nl_sock *nlsk, int ifindex, bool up); |
| 41 | int netdev_add_defaultroute(struct nl_sock *nlsk, int ifindex, uint8_t family); |
| 42 | |
| 43 | |
| 44 | /*********************************************************************** |
| 45 | * GTP Endpoint (UDP socket) |
| 46 | ***********************************************************************/ |
| 47 | |
| 48 | struct gtp_daemon; |
| 49 | |
| 50 | /* local UDP socket for GTP communication */ |
| 51 | struct 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 | |
| 70 | struct gtp_endpoint * |
| 71 | gtp_endpoint_find_or_create(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr); |
| 72 | |
| 73 | struct gtp_endpoint * |
| 74 | _gtp_endpoint_find(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr); |
| 75 | |
| 76 | void _gtp_endpoint_deref_destroy(struct gtp_endpoint *ep); |
| 77 | |
| 78 | bool _gtp_endpoint_release(struct gtp_endpoint *ep); |
| 79 | |
| 80 | bool gtp_endpoint_release(struct gtp_endpoint *ep); |
| 81 | |
| 82 | |
| 83 | |
| 84 | /*********************************************************************** |
| 85 | * TUN Device |
| 86 | ***********************************************************************/ |
| 87 | |
| 88 | struct 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 | |
| 115 | struct tun_device * |
| 116 | tun_device_find_or_create(struct gtp_daemon *d, const char *devname, const char *netns_name); |
| 117 | |
| 118 | struct tun_device * |
| 119 | _tun_device_find(struct gtp_daemon *d, const char *devname); |
| 120 | |
| 121 | void _tun_device_deref_destroy(struct tun_device *tun); |
| 122 | |
| 123 | bool _tun_device_release(struct tun_device *tun); |
| 124 | |
| 125 | bool tun_device_release(struct tun_device *tun); |
| 126 | |
| 127 | |
| 128 | |
| 129 | /*********************************************************************** |
| 130 | * GTP Tunnel |
| 131 | ***********************************************************************/ |
| 132 | |
| 133 | /* Every tunnel is identified uniquely by the following tuples: |
| 134 | * |
| 135 | * a) local endpoint + TEID |
| 136 | * this is what happens on incoming GTP messages |
| 137 | * |
| 138 | * b) tun device + end-user-address (+ filter, if any) |
| 139 | * this is what happens when IP arrives on the tun device |
| 140 | */ |
| 141 | |
| 142 | struct gtp_tunnel { |
| 143 | /* entry in global list / hash table */ |
| 144 | struct llist_head list; |
| 145 | /* back-pointer to daemon */ |
| 146 | struct gtp_daemon *d; |
| 147 | |
| 148 | const char *name; |
| 149 | |
| 150 | /* the TUN device associated with this tunnel */ |
| 151 | struct tun_device *tun_dev; |
| 152 | /* the GTP endpoint (UDP socket) associated with this tunnel */ |
| 153 | struct gtp_endpoint *gtp_ep; |
| 154 | |
| 155 | /* TEID on transmit (host byte order) */ |
| 156 | uint32_t tx_teid; |
| 157 | /* TEID one receive (host byte order) */ |
| 158 | uint32_t rx_teid; |
| 159 | |
| 160 | /* End user Address (inner IP) */ |
| 161 | struct sockaddr_storage user_addr; |
| 162 | |
| 163 | /* Remote UDP IP/Port*/ |
| 164 | struct sockaddr_storage remote_udp; |
| 165 | |
| 166 | /* TODO: Filter */ |
| 167 | }; |
| 168 | |
| 169 | struct gtp_tunnel * |
| 170 | _gtp_tunnel_find_r(struct gtp_daemon *d, uint32_t rx_teid, struct gtp_endpoint *ep); |
| 171 | |
| 172 | struct gtp_tunnel * |
| 173 | _gtp_tunnel_find_eua(struct tun_device *tun, const struct sockaddr *sa, uint8_t proto); |
| 174 | |
| 175 | struct gtp_tunnel_params { |
| 176 | /* TEID in receive and transmit direction */ |
| 177 | uint32_t rx_teid; |
| 178 | uint32_t tx_teid; |
| 179 | |
| 180 | /* end user address */ |
| 181 | struct sockaddr_storage user_addr; |
| 182 | |
| 183 | /* remote GTP/UDP IP+Port */ |
| 184 | struct sockaddr_storage remote_udp; |
| 185 | |
| 186 | /* local GTP/UDP IP+Port (used to lookup/create local EP) */ |
| 187 | struct sockaddr_storage local_udp; |
| 188 | |
| 189 | /* local TUN device name (used to lookup/create local tun) */ |
| 190 | const char *tun_name; |
| 191 | const char *tun_netns_name; |
| 192 | }; |
| 193 | struct gtp_tunnel *gtp_tunnel_alloc(struct gtp_daemon *d, const struct gtp_tunnel_params *cpars); |
| 194 | |
| 195 | void _gtp_tunnel_destroy(struct gtp_tunnel *t); |
| 196 | bool gtp_tunnel_destroy(struct gtp_daemon *d, const struct sockaddr_storage *bind_addr, uint32_t rx_teid); |
| 197 | |
| 198 | |
| 199 | /*********************************************************************** |
| 200 | * GTP Daemon |
| 201 | ***********************************************************************/ |
| 202 | |
Harald Welte | 432a130 | 2020-04-17 12:52:40 +0200 | [diff] [blame^] | 203 | #define UECUPS_SCTP_PORT 4268 |
| 204 | |
Harald Welte | f736559 | 2020-04-15 21:48:45 +0200 | [diff] [blame] | 205 | struct gtp_daemon { |
| 206 | /* global lists of various objects */ |
| 207 | struct llist_head gtp_endpoints; |
| 208 | struct llist_head tun_devices; |
| 209 | struct llist_head gtp_tunnels; |
| 210 | /* lock protecting all of the above lists */ |
| 211 | pthread_rwlock_t rwlock; |
| 212 | /* main thread ID */ |
| 213 | pthread_t main_thread; |
| 214 | /* client CUPS interface */ |
| 215 | struct llist_head cups_clients; |
| 216 | struct osmo_stream_srv_link *cups_link; |
| 217 | |
| 218 | struct { |
| 219 | char *cups_local_ip; |
| 220 | uint16_t cups_local_port; |
| 221 | } cfg; |
| 222 | }; |
| 223 | extern struct gtp_daemon *g_daemon; |
| 224 | |
| 225 | int gtpud_vty_init(void); |