blob: f2130844a65f0e9dbf01aa181bc93101a9072f7a [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>
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010029#include <osmocom/core/rate_ctr.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020030
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010031#include <openbsc/gprs_sgsn.h>
32
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020033
34/* support */
35
36/* TODO move to osmocom/core/socket.c ? */
37#include <netdb.h> /* for IPPROTO_* etc */
38struct osmo_sockaddr {
39 struct sockaddr_storage a;
40 socklen_t l;
41};
42
43/* TODO move to osmocom/core/socket.c ? */
44/*! \brief Initialize a sockaddr
45 * \param[out] addr Valid osmo_sockaddr pointer to write result to
46 * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
47 * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
48 * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP
49 * \param[in] host Remote host name or IP address in string form
50 * \param[in] port Remote port number in host byte order
51 * \returns 0 on success, otherwise an error code (from getaddrinfo()).
52 *
53 * Copy the first result from a getaddrinfo() call with the given parameters to
54 * *addr and *addr_len. On error, do not change *addr and return nonzero.
55 */
56int osmo_sockaddr_init(struct osmo_sockaddr *addr,
57 uint16_t family, uint16_t type, uint8_t proto,
58 const char *host, uint16_t port);
59
60/* Conveniently pass AF_UNSPEC, SOCK_DGRAM and IPPROTO_UDP to
61 * osmo_sockaddr_init(). */
62static inline int osmo_sockaddr_init_udp(struct osmo_sockaddr *addr,
63 const char *host, uint16_t port)
64{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010065 return osmo_sockaddr_init(addr, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
66 host, port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020067}
68
69/*! \brief convert sockaddr to human readable string.
70 * \param[out] addr_str Valid pointer to a buffer of length addr_str_len.
71 * \param[in] addr_str_len Size of buffer addr_str points at.
72 * \param[out] port_str Valid pointer to a buffer of length port_str_len.
73 * \param[in] port_str_len Size of buffer port_str points at.
74 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
75 * \param[in] flags flags as passed to getnameinfo().
76 * \returns 0 on success, an error code on error.
77 *
78 * Return the IPv4 or IPv6 address string and the port (a.k.a. service) string
79 * representations of the given struct osmo_sockaddr in two caller provided
80 * char buffers. Flags of (NI_NUMERICHOST | NI_NUMERICSERV) return numeric
81 * address and port. Either one of addr_str or port_str may be NULL, in which
82 * case nothing is returned there.
83 *
84 * See also osmo_sockaddr_to_str() (less flexible, but much more convenient). */
85int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
86 char *port_str, size_t port_str_len,
87 const struct osmo_sockaddr *addr,
88 int flags);
89
90
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010091/*! \brief concatenate the parts returned by osmo_sockaddr_to_strs().
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020092 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
93 * \param[in] buf A buffer to use for string operations.
94 * \param[in] buf_len Length of the buffer.
95 * \returns Address string (in buffer).
96 *
97 * Compose a string of the numeric IP-address and port represented by *addr of
98 * the form "<ip-addr> port <port>". The returned string is valid until the
99 * next invocation of this function.
100 */
101const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
102 char *buf, size_t buf_len);
103
104/*! \brief conveniently return osmo_sockaddr_to_strb() in a static buffer.
105 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
106 * \returns Address string in static buffer.
107 *
108 * See osmo_sockaddr_to_strb().
109 *
110 * Note: only one osmo_sockaddr_to_str() call will work per print/log
111 * statement. For two or more, use osmo_sockaddr_to_strb() with a separate
112 * buffer each.
113 */
114const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr);
115
116/*! \brief compare two osmo_sockaddr.
117 * \param[in] a The first address to compare.
118 * \param[in] b The other address to compare.
119 * \returns 0 if equal, otherwise -1 or 1.
120 */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100121int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
122 const struct osmo_sockaddr *b);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200123
124/*! \brief Overwrite *dst with *src.
125 * Like memcpy(), but copy only the valid bytes. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100126void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
127 const struct osmo_sockaddr *src);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200128
129
130/* general */
131
132enum gtphub_plane_idx {
133 GTPH_PLANE_CTRL = 0,
134 GTPH_PLANE_USER = 1,
135 GTPH_PLANE_N
136};
137
138extern const char* const gtphub_plane_idx_names[GTPH_PLANE_N];
139extern const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N];
140
141/* A host address in the form that is expected in the 7.7.32 GSN Address IE.
142 * len is either 4 (IPv4) or 16 (IPv6), any other value is invalid. If no
143 * address is set, len shall be 0. */
144struct gsn_addr {
145 uint16_t len;
146 uint8_t buf[16];
147};
148
149void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src);
150int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str);
151
152/* Return gsna in numeric string form, in a static buffer. */
153const char *gsn_addr_to_str(const struct gsn_addr *gsna);
154
155/* note: strbuf_len doesn't need to be larger than INET6_ADDRSTRLEN + 1. */
156const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
157 char *strbuf, int strbuf_len);
158
159/* Return 1 on match, zero otherwise. */
160int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b);
161
Neels Hofmeyrc2275942015-11-10 22:07:04 +0100162/* Decode sa to gsna. Return 0 on success. If port is non-NULL, the port number
163 * from sa is also returned. */
164int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
165 const struct osmo_sockaddr *sa);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200166
167/* expiry */
168
169struct expiring_item;
170typedef void (*del_cb_t)(struct expiring_item *);
171
172struct expiring_item {
173 struct llist_head entry;
174 time_t expiry;
175 del_cb_t del_cb;
176};
177
178struct expiry {
179 int expiry_in_seconds;
180 struct llist_head items;
181};
182
183/* Initialize an expiry queue. */
184void expiry_init(struct expiry *exq, int expiry_in_seconds);
185
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100186/* Add a new mapping, or restart the expiry timeout for an already listed
187 * mapping. */
Neels Hofmeyr231653a2015-11-24 13:23:44 +0100188void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200189
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100190/* Initialize to all-empty; must be called before using the item in any way. */
191void expiring_item_init(struct expiring_item *item);
192
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200193/* Remove the given item from its expiry queue, and call item->del_cb, if set.
194 * This sets item->del_cb to NULL and is harmless when run a second time on the
195 * same item, so the del_cb may choose to call this function, too, to allow
196 * deleting items from several code paths. */
197void expiring_item_del(struct expiring_item *item);
198
199/* Carry out due expiry of mappings. Must be invoked regularly.
200 * 'now' is the current clock count in seconds and must correspond to the clock
201 * count passed to nr_map_add(). A monotonous clock counter should be used. */
202int expiry_tick(struct expiry *exq, time_t now);
203
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100204/* Expire all items. */
205void expiry_clear(struct expiry *exq);
206
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200207
208/* number map */
209
210/* A number map assigns a "random" mapped number to each user provided number.
211 * If the same number is requested multiple times, the same mapped number is
212 * returned.
213 *
214 * Number maps plug into possibly shared pools and expiry queues, for example:
215 *
216 * mapA -----------+-> pool1 <-+-- mapB
217 * {10->1, 11->5} | {1, 2, 3, ...} | {10->2, 11->3}
218 * | |
219 * | |
220 * /-> \-> expiry1 <-/
221 * | (30 seconds)
222 * |
223 * mapC -------+-----> pool2 <-+-- mapD
224 * {10->1, 11->3} {1, 2, 3, ...} | {10->2, 11->5}
225 * |
226 * expiry2 <-/
227 * (60 seconds)
228 *
229 * A map contains mappings ("10->1"). Each map needs a number pool, which can
230 * be shared with other maps. Each new mapping receives a number from the pool,
231 * which is then unavailable to any other map using the same pool.
232 *
233 * A map may point at an expiry queue, in which case all mappings added to it
234 * are also appended to the expiry queue (using a separate llist entry in the
235 * mapping). Any number of maps may submit to the same expiry queue, if they
236 * desire the same expiry timeout. An expiry queue stores the mappings in
237 * chronological order, so that expiry checking is needed only from the start
238 * of the queue; hence only mappings with identical expiry timeout can be added
239 * to the same expiry queue. Upon expiry, a mapping is dropped from the map it
240 * was submitted at. expiry_tick() needs to be called regularly for each expiry
241 * queue.
242 *
243 * A nr_mapping can be embedded in a larger struct: each mapping can have a
244 * distinct destructor (del_cb), and each del_cb can figure out the container
245 * struct's address and free that upon expiry or manual deletion. So in expiry
246 * queues (and even maps), mappings of different container types can be mixed.
247 * This can help to drastically reduce the amount of unnecessary visits during
248 * expiry checking, for the case that no expiry is pending. An expiry queue
249 * always knows which mappings to expire next, because they are right at the
250 * start of its list.
251 *
252 * Mapping allocation and a del_cb are provided by the caller. If del_cb is
253 * NULL, no deallocation will be done (allowing statically allocated entries).
254 */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200255
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100256typedef unsigned int nr_t;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200257
258/* Generator for unused numbers. So far this counts upwards from zero, but the
259 * implementation may change in the future. Treat this like an opaque struct.
260 * If this becomes random, the tests need to be fixed. */
261struct nr_pool {
262 nr_t last_nr;
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100263 nr_t nr_min;
264 nr_t nr_max;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200265};
266
267struct nr_mapping {
268 struct llist_head entry;
269 struct expiring_item expiry_entry;
270
271 void *origin;
272 nr_t orig;
273 nr_t repl;
274};
275
276struct nr_map {
277 struct nr_pool *pool; /* multiple nr_maps can share a nr_pool. */
278 struct expiry *add_items_to_expiry;
279 struct llist_head mappings;
280};
281
282
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100283void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200284
285/* Return the next unused number from the nr_pool. */
286nr_t nr_pool_next(struct nr_pool *pool);
287
288/* Initialize the nr_mapping to zero/empty values. */
289void nr_mapping_init(struct nr_mapping *mapping);
290
291/* Remove the given mapping from its parent map and expiry queue, and call
292 * mapping->del_cb, if set. */
293void nr_mapping_del(struct nr_mapping *mapping);
294
295/* Initialize an (already allocated) nr_map, and set the map's number pool.
296 * Multiple nr_map instances may use the same nr_pool. Set the nr_map's expiry
297 * queue to exq, so that all added mappings are automatically expired after the
298 * time configured in exq. exq may be NULL to disable automatic expiry. */
299void nr_map_init(struct nr_map *map, struct nr_pool *pool,
300 struct expiry *exq);
301
302/* Add a new entry to the map. mapping->orig, mapping->origin and
303 * mapping->del_cb must be set before calling this function. The remaining
304 * fields of *mapping will be overwritten. mapping->repl is set to the next
305 * available mapped number from map->pool. 'now' is the current clock count in
306 * seconds; if no map->expiry is used, just pass 0 for 'now'. */
307void nr_map_add(struct nr_map *map, struct nr_mapping *mapping,
308 time_t now);
309
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100310/* Restart the timeout for the given mapping. mapping must be a member of map.
311 */
312void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping,
313 time_t now);
314
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200315/* Return a known mapping from nr_orig and the given origin. If nr_orig is
316 * unknown, return NULL. */
317struct nr_mapping *nr_map_get(const struct nr_map *map,
318 void *origin, nr_t nr_orig);
319
320/* Return a known mapping to nr_repl. If nr_repl is unknown, return NULL. */
321struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl);
322
323/* Remove all mappings from map. */
324void nr_map_clear(struct nr_map *map);
325
326/* Return 1 if map has no entries, 0 otherwise. */
327int nr_map_empty(const struct nr_map *map);
328
329
330/* config */
331
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +0100332static const int GTPH_EXPIRE_QUICKLY_SECS = 30; /* TODO is there a spec for this? */
333static const int GTPH_EXPIRE_SLOWLY_MINUTES = 6 * 60; /* TODO is there a spec for this? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200334
335struct gtphub_cfg_addr {
336 const char *addr_str;
337 uint16_t port;
338};
339
340struct gtphub_cfg_bind {
341 struct gtphub_cfg_addr bind;
342};
343
344struct gtphub_cfg {
345 struct gtphub_cfg_bind to_sgsns[GTPH_PLANE_N];
346 struct gtphub_cfg_bind to_ggsns[GTPH_PLANE_N];
347 struct gtphub_cfg_addr sgsn_proxy[GTPH_PLANE_N];
348 struct gtphub_cfg_addr ggsn_proxy[GTPH_PLANE_N];
349};
350
351
352/* state */
353
354struct gtphub_peer {
355 struct llist_head entry;
356
357 struct llist_head addresses; /* Alternatives, not load balancing. */
358 struct nr_pool seq_pool;
359 struct nr_map seq_map;
360};
361
362struct gtphub_peer_addr {
363 struct llist_head entry;
364
365 struct gtphub_peer *peer;
366 struct gsn_addr addr;
367 struct llist_head ports;
368};
369
370struct gtphub_peer_port {
371 struct llist_head entry;
372
373 struct gtphub_peer_addr *peer_addr;
374 uint16_t port;
375 unsigned int ref_count; /* references from other peers' seq_maps */
376 struct osmo_sockaddr sa;
377};
378
379struct gtphub_bind {
380 struct gsn_addr local_addr;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100381 uint16_t local_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200382 struct osmo_fd ofd;
383
384 /* list of struct gtphub_peer */
385 struct llist_head peers;
Neels Hofmeyr390e9102015-11-16 13:45:13 +0100386
387 const char *label; /* For logging */
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100388 struct rate_ctr_group *counters_io;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200389};
390
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100391struct gtphub_resolved_ggsn {
392 struct llist_head entry;
393 struct expiring_item expiry_entry;
394
395 /* The APN OI, the Operator Identifier, is the combined address,
396 * including parts of the IMSI and APN NI, and ending with ".gprs". */
397 char apn_oi_str[GSM_APN_LENGTH];
398
399 /* Which address and port we resolved that to. */
400 struct gtphub_peer_port *peer;
401};
402
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200403struct gtphub {
404 struct gtphub_bind to_sgsns[GTPH_PLANE_N];
405 struct gtphub_bind to_ggsns[GTPH_PLANE_N];
406
407 /* pointers to an entry of to_sgsns[x].peers */
408 struct gtphub_peer_port *sgsn_proxy[GTPH_PLANE_N];
409
410 /* pointers to an entry of to_ggsns[x].peers */
411 struct gtphub_peer_port *ggsn_proxy[GTPH_PLANE_N];
412
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100413 /* The TEI numbers will simply wrap and be reused, which will work out
414 * in practice. Problems would arise if one given peer maintained the
415 * same TEI for a time long enough for the TEI nr map to wrap an entire
416 * uint32_t; if a new TEI were mapped every second, this would take
417 * more than 100 years (in which a single given TEI must not time out)
418 * to cause a problem. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200419 struct nr_map tei_map[GTPH_PLANE_N];
420 struct nr_pool tei_pool[GTPH_PLANE_N];
421
Neels Hofmeyr4960fab2015-11-18 17:53:00 +0100422 struct llist_head ggsn_lookups; /* opaque (gtphub_ares.c) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100423 struct llist_head resolved_ggsns; /* struct gtphub_resolved_ggsn */
424
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200425 struct osmo_timer_list gc_timer;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +0100426 struct expiry expire_quickly;
427 struct expiry expire_slowly;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100428
429 uint16_t restart_counter;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200430};
431
432struct gtp_packet_desc;
433
434
435/* api */
436
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100437int gtphub_vty_init(struct gtphub *global_hub, struct gtphub_cfg *global_cfg);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200438int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file);
439
440/* Initialize and start gtphub: bind to ports, run expiry timers. */
441int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg);
442
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100443/* Close all sockets, expire all maps and peers and free all allocations. The
444 * struct is then unusable, unless gtphub_start() is run on it again. */
445void gtphub_stop(struct gtphub *hub);
446
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200447time_t gtphub_now(void);
448
449/* Remove expired items, empty peers, ... */
450void gtphub_gc(struct gtphub *hub, time_t now);
451
452/* Return the string of the first address for this peer. */
453const char *gtphub_peer_str(struct gtphub_peer *peer);
454/* Same with a different static buffer. We often want to print two peers. */
455const char *gtphub_peer_str2(struct gtphub_peer *peer);
456
457int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
458 unsigned int port_idx,
459 const struct osmo_sockaddr *from_addr,
460 uint8_t *buf,
461 size_t received,
462 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100463 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200464 struct osmo_fd **to_ofd,
465 struct osmo_sockaddr *to_addr);
466
467int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
468 unsigned int port_idx,
469 const struct osmo_sockaddr *from_addr,
470 uint8_t *buf,
471 size_t received,
472 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100473 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200474 struct osmo_fd **to_ofd,
475 struct osmo_sockaddr *to_addr);
476
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100477struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
478 struct gtphub_bind *bind,
479 const struct gsn_addr *addr,
480 uint16_t port);
481
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200482struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
483 const struct osmo_sockaddr *addr);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100484
485void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
486 struct gsn_addr *resolved_addr,
487 time_t now);
488
489const char *gtphub_port_str(struct gtphub_peer_port *port);