blob: fa63a20cb03b72f900ce39ed79acf7bcd0960aa7 [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <stdint.h>
25#include <sys/socket.h>
26
27#include <osmocom/core/select.h>
28#include <osmocom/core/timer.h>
29
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010030#include <openbsc/gprs_sgsn.h>
31
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020032
33/* support */
34
35/* TODO move to osmocom/core/socket.c ? */
36#include <netdb.h> /* for IPPROTO_* etc */
37struct osmo_sockaddr {
38 struct sockaddr_storage a;
39 socklen_t l;
40};
41
42/* TODO move to osmocom/core/socket.c ? */
43/*! \brief Initialize a sockaddr
44 * \param[out] addr Valid osmo_sockaddr pointer to write result to
45 * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
46 * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
47 * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP
48 * \param[in] host Remote host name or IP address in string form
49 * \param[in] port Remote port number in host byte order
50 * \returns 0 on success, otherwise an error code (from getaddrinfo()).
51 *
52 * Copy the first result from a getaddrinfo() call with the given parameters to
53 * *addr and *addr_len. On error, do not change *addr and return nonzero.
54 */
55int osmo_sockaddr_init(struct osmo_sockaddr *addr,
56 uint16_t family, uint16_t type, uint8_t proto,
57 const char *host, uint16_t port);
58
59/* Conveniently pass AF_UNSPEC, SOCK_DGRAM and IPPROTO_UDP to
60 * osmo_sockaddr_init(). */
61static inline int osmo_sockaddr_init_udp(struct osmo_sockaddr *addr,
62 const char *host, uint16_t port)
63{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010064 return osmo_sockaddr_init(addr, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
65 host, port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020066}
67
68/*! \brief convert sockaddr to human readable string.
69 * \param[out] addr_str Valid pointer to a buffer of length addr_str_len.
70 * \param[in] addr_str_len Size of buffer addr_str points at.
71 * \param[out] port_str Valid pointer to a buffer of length port_str_len.
72 * \param[in] port_str_len Size of buffer port_str points at.
73 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
74 * \param[in] flags flags as passed to getnameinfo().
75 * \returns 0 on success, an error code on error.
76 *
77 * Return the IPv4 or IPv6 address string and the port (a.k.a. service) string
78 * representations of the given struct osmo_sockaddr in two caller provided
79 * char buffers. Flags of (NI_NUMERICHOST | NI_NUMERICSERV) return numeric
80 * address and port. Either one of addr_str or port_str may be NULL, in which
81 * case nothing is returned there.
82 *
83 * See also osmo_sockaddr_to_str() (less flexible, but much more convenient). */
84int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
85 char *port_str, size_t port_str_len,
86 const struct osmo_sockaddr *addr,
87 int flags);
88
89
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010090/*! \brief concatenate the parts returned by osmo_sockaddr_to_strs().
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020091 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
92 * \param[in] buf A buffer to use for string operations.
93 * \param[in] buf_len Length of the buffer.
94 * \returns Address string (in buffer).
95 *
96 * Compose a string of the numeric IP-address and port represented by *addr of
97 * the form "<ip-addr> port <port>". The returned string is valid until the
98 * next invocation of this function.
99 */
100const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
101 char *buf, size_t buf_len);
102
103/*! \brief conveniently return osmo_sockaddr_to_strb() in a static buffer.
104 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
105 * \returns Address string in static buffer.
106 *
107 * See osmo_sockaddr_to_strb().
108 *
109 * Note: only one osmo_sockaddr_to_str() call will work per print/log
110 * statement. For two or more, use osmo_sockaddr_to_strb() with a separate
111 * buffer each.
112 */
113const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr);
114
115/*! \brief compare two osmo_sockaddr.
116 * \param[in] a The first address to compare.
117 * \param[in] b The other address to compare.
118 * \returns 0 if equal, otherwise -1 or 1.
119 */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100120int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
121 const struct osmo_sockaddr *b);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200122
123/*! \brief Overwrite *dst with *src.
124 * Like memcpy(), but copy only the valid bytes. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100125void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
126 const struct osmo_sockaddr *src);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200127
128
129/* general */
130
131enum gtphub_plane_idx {
132 GTPH_PLANE_CTRL = 0,
133 GTPH_PLANE_USER = 1,
134 GTPH_PLANE_N
135};
136
137extern const char* const gtphub_plane_idx_names[GTPH_PLANE_N];
138extern const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N];
139
140/* A host address in the form that is expected in the 7.7.32 GSN Address IE.
141 * len is either 4 (IPv4) or 16 (IPv6), any other value is invalid. If no
142 * address is set, len shall be 0. */
143struct gsn_addr {
144 uint16_t len;
145 uint8_t buf[16];
146};
147
148void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src);
149int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str);
150
151/* Return gsna in numeric string form, in a static buffer. */
152const char *gsn_addr_to_str(const struct gsn_addr *gsna);
153
154/* note: strbuf_len doesn't need to be larger than INET6_ADDRSTRLEN + 1. */
155const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
156 char *strbuf, int strbuf_len);
157
158/* Return 1 on match, zero otherwise. */
159int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b);
160
Neels Hofmeyrc2275942015-11-10 22:07:04 +0100161/* Decode sa to gsna. Return 0 on success. If port is non-NULL, the port number
162 * from sa is also returned. */
163int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
164 const struct osmo_sockaddr *sa);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200165
166/* expiry */
167
168struct expiring_item;
169typedef void (*del_cb_t)(struct expiring_item *);
170
171struct expiring_item {
172 struct llist_head entry;
173 time_t expiry;
174 del_cb_t del_cb;
175};
176
177struct expiry {
178 int expiry_in_seconds;
179 struct llist_head items;
180};
181
182/* Initialize an expiry queue. */
183void expiry_init(struct expiry *exq, int expiry_in_seconds);
184
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100185/* Add a new mapping, or restart the expiry timeout for an already listed
186 * mapping. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200187void expiry_add(struct expiry *exq, struct expiring_item *mapping, time_t now);
188
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100189/* Initialize to all-empty; must be called before using the item in any way. */
190void expiring_item_init(struct expiring_item *item);
191
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200192/* Remove the given item from its expiry queue, and call item->del_cb, if set.
193 * This sets item->del_cb to NULL and is harmless when run a second time on the
194 * same item, so the del_cb may choose to call this function, too, to allow
195 * deleting items from several code paths. */
196void expiring_item_del(struct expiring_item *item);
197
198/* Carry out due expiry of mappings. Must be invoked regularly.
199 * 'now' is the current clock count in seconds and must correspond to the clock
200 * count passed to nr_map_add(). A monotonous clock counter should be used. */
201int expiry_tick(struct expiry *exq, time_t now);
202
203
204/* number map */
205
206/* A number map assigns a "random" mapped number to each user provided number.
207 * If the same number is requested multiple times, the same mapped number is
208 * returned.
209 *
210 * Number maps plug into possibly shared pools and expiry queues, for example:
211 *
212 * mapA -----------+-> pool1 <-+-- mapB
213 * {10->1, 11->5} | {1, 2, 3, ...} | {10->2, 11->3}
214 * | |
215 * | |
216 * /-> \-> expiry1 <-/
217 * | (30 seconds)
218 * |
219 * mapC -------+-----> pool2 <-+-- mapD
220 * {10->1, 11->3} {1, 2, 3, ...} | {10->2, 11->5}
221 * |
222 * expiry2 <-/
223 * (60 seconds)
224 *
225 * A map contains mappings ("10->1"). Each map needs a number pool, which can
226 * be shared with other maps. Each new mapping receives a number from the pool,
227 * which is then unavailable to any other map using the same pool.
228 *
229 * A map may point at an expiry queue, in which case all mappings added to it
230 * are also appended to the expiry queue (using a separate llist entry in the
231 * mapping). Any number of maps may submit to the same expiry queue, if they
232 * desire the same expiry timeout. An expiry queue stores the mappings in
233 * chronological order, so that expiry checking is needed only from the start
234 * of the queue; hence only mappings with identical expiry timeout can be added
235 * to the same expiry queue. Upon expiry, a mapping is dropped from the map it
236 * was submitted at. expiry_tick() needs to be called regularly for each expiry
237 * queue.
238 *
239 * A nr_mapping can be embedded in a larger struct: each mapping can have a
240 * distinct destructor (del_cb), and each del_cb can figure out the container
241 * struct's address and free that upon expiry or manual deletion. So in expiry
242 * queues (and even maps), mappings of different container types can be mixed.
243 * This can help to drastically reduce the amount of unnecessary visits during
244 * expiry checking, for the case that no expiry is pending. An expiry queue
245 * always knows which mappings to expire next, because they are right at the
246 * start of its list.
247 *
248 * Mapping allocation and a del_cb are provided by the caller. If del_cb is
249 * NULL, no deallocation will be done (allowing statically allocated entries).
250 */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200251
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100252typedef unsigned int nr_t;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200253
254/* Generator for unused numbers. So far this counts upwards from zero, but the
255 * implementation may change in the future. Treat this like an opaque struct.
256 * If this becomes random, the tests need to be fixed. */
257struct nr_pool {
258 nr_t last_nr;
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100259 nr_t nr_min;
260 nr_t nr_max;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200261};
262
263struct nr_mapping {
264 struct llist_head entry;
265 struct expiring_item expiry_entry;
266
267 void *origin;
268 nr_t orig;
269 nr_t repl;
270};
271
272struct nr_map {
273 struct nr_pool *pool; /* multiple nr_maps can share a nr_pool. */
274 struct expiry *add_items_to_expiry;
275 struct llist_head mappings;
276};
277
278
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100279void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200280
281/* Return the next unused number from the nr_pool. */
282nr_t nr_pool_next(struct nr_pool *pool);
283
284/* Initialize the nr_mapping to zero/empty values. */
285void nr_mapping_init(struct nr_mapping *mapping);
286
287/* Remove the given mapping from its parent map and expiry queue, and call
288 * mapping->del_cb, if set. */
289void nr_mapping_del(struct nr_mapping *mapping);
290
291/* Initialize an (already allocated) nr_map, and set the map's number pool.
292 * Multiple nr_map instances may use the same nr_pool. Set the nr_map's expiry
293 * queue to exq, so that all added mappings are automatically expired after the
294 * time configured in exq. exq may be NULL to disable automatic expiry. */
295void nr_map_init(struct nr_map *map, struct nr_pool *pool,
296 struct expiry *exq);
297
298/* Add a new entry to the map. mapping->orig, mapping->origin and
299 * mapping->del_cb must be set before calling this function. The remaining
300 * fields of *mapping will be overwritten. mapping->repl is set to the next
301 * available mapped number from map->pool. 'now' is the current clock count in
302 * seconds; if no map->expiry is used, just pass 0 for 'now'. */
303void nr_map_add(struct nr_map *map, struct nr_mapping *mapping,
304 time_t now);
305
306/* Return a known mapping from nr_orig and the given origin. If nr_orig is
307 * unknown, return NULL. */
308struct nr_mapping *nr_map_get(const struct nr_map *map,
309 void *origin, nr_t nr_orig);
310
311/* Return a known mapping to nr_repl. If nr_repl is unknown, return NULL. */
312struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl);
313
314/* Remove all mappings from map. */
315void nr_map_clear(struct nr_map *map);
316
317/* Return 1 if map has no entries, 0 otherwise. */
318int nr_map_empty(const struct nr_map *map);
319
320
321/* config */
322
323static const int GTPH_SEQ_MAPPING_EXPIRY_SECS = 30; /* TODO is there a spec for this? */
324static const int GTPH_TEI_MAPPING_EXPIRY_MINUTES = 6 * 60; /* TODO is there a spec for this? */
325
326struct gtphub_cfg_addr {
327 const char *addr_str;
328 uint16_t port;
329};
330
331struct gtphub_cfg_bind {
332 struct gtphub_cfg_addr bind;
333};
334
335struct gtphub_cfg {
336 struct gtphub_cfg_bind to_sgsns[GTPH_PLANE_N];
337 struct gtphub_cfg_bind to_ggsns[GTPH_PLANE_N];
338 struct gtphub_cfg_addr sgsn_proxy[GTPH_PLANE_N];
339 struct gtphub_cfg_addr ggsn_proxy[GTPH_PLANE_N];
340};
341
342
343/* state */
344
345struct gtphub_peer {
346 struct llist_head entry;
347
348 struct llist_head addresses; /* Alternatives, not load balancing. */
349 struct nr_pool seq_pool;
350 struct nr_map seq_map;
351};
352
353struct gtphub_peer_addr {
354 struct llist_head entry;
355
356 struct gtphub_peer *peer;
357 struct gsn_addr addr;
358 struct llist_head ports;
359};
360
361struct gtphub_peer_port {
362 struct llist_head entry;
363
364 struct gtphub_peer_addr *peer_addr;
365 uint16_t port;
366 unsigned int ref_count; /* references from other peers' seq_maps */
367 struct osmo_sockaddr sa;
368};
369
370struct gtphub_bind {
371 struct gsn_addr local_addr;
372 struct osmo_fd ofd;
373
374 /* list of struct gtphub_peer */
375 struct llist_head peers;
Neels Hofmeyr390e9102015-11-16 13:45:13 +0100376
377 const char *label; /* For logging */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200378};
379
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100380struct gtphub_resolved_ggsn {
381 struct llist_head entry;
382 struct expiring_item expiry_entry;
383
384 /* The APN OI, the Operator Identifier, is the combined address,
385 * including parts of the IMSI and APN NI, and ending with ".gprs". */
386 char apn_oi_str[GSM_APN_LENGTH];
387
388 /* Which address and port we resolved that to. */
389 struct gtphub_peer_port *peer;
390};
391
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200392struct gtphub {
393 struct gtphub_bind to_sgsns[GTPH_PLANE_N];
394 struct gtphub_bind to_ggsns[GTPH_PLANE_N];
395
396 /* pointers to an entry of to_sgsns[x].peers */
397 struct gtphub_peer_port *sgsn_proxy[GTPH_PLANE_N];
398
399 /* pointers to an entry of to_ggsns[x].peers */
400 struct gtphub_peer_port *ggsn_proxy[GTPH_PLANE_N];
401
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100402 /* The TEI numbers will simply wrap and be reused, which will work out
403 * in practice. Problems would arise if one given peer maintained the
404 * same TEI for a time long enough for the TEI nr map to wrap an entire
405 * uint32_t; if a new TEI were mapped every second, this would take
406 * more than 100 years (in which a single given TEI must not time out)
407 * to cause a problem. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200408 struct nr_map tei_map[GTPH_PLANE_N];
409 struct nr_pool tei_pool[GTPH_PLANE_N];
410
Neels Hofmeyr4960fab2015-11-18 17:53:00 +0100411 struct llist_head ggsn_lookups; /* opaque (gtphub_ares.c) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100412 struct llist_head resolved_ggsns; /* struct gtphub_resolved_ggsn */
413
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200414 struct osmo_timer_list gc_timer;
415 struct expiry expire_seq_maps;
416 struct expiry expire_tei_maps;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100417
418 uint16_t restart_counter;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200419};
420
421struct gtp_packet_desc;
422
423
424/* api */
425
426int gtphub_vty_init(void);
427int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file);
428
429/* Initialize and start gtphub: bind to ports, run expiry timers. */
430int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg);
431
432time_t gtphub_now(void);
433
434/* Remove expired items, empty peers, ... */
435void gtphub_gc(struct gtphub *hub, time_t now);
436
437/* Return the string of the first address for this peer. */
438const char *gtphub_peer_str(struct gtphub_peer *peer);
439/* Same with a different static buffer. We often want to print two peers. */
440const char *gtphub_peer_str2(struct gtphub_peer *peer);
441
442int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
443 unsigned int port_idx,
444 const struct osmo_sockaddr *from_addr,
445 uint8_t *buf,
446 size_t received,
447 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100448 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200449 struct osmo_fd **to_ofd,
450 struct osmo_sockaddr *to_addr);
451
452int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
453 unsigned int port_idx,
454 const struct osmo_sockaddr *from_addr,
455 uint8_t *buf,
456 size_t received,
457 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100458 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200459 struct osmo_fd **to_ofd,
460 struct osmo_sockaddr *to_addr);
461
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100462struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
463 struct gtphub_bind *bind,
464 const struct gsn_addr *addr,
465 uint16_t port);
466
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200467struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
468 const struct osmo_sockaddr *addr);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100469
470void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
471 struct gsn_addr *resolved_addr,
472 time_t now);
473
474const char *gtphub_port_str(struct gtphub_peer_port *port);