blob: 79f138b0a0bce5ad84d0c77321da38f09da6321f [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{
64 return osmo_sockaddr_init(addr, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port);
65}
66
67/*! \brief convert sockaddr to human readable string.
68 * \param[out] addr_str Valid pointer to a buffer of length addr_str_len.
69 * \param[in] addr_str_len Size of buffer addr_str points at.
70 * \param[out] port_str Valid pointer to a buffer of length port_str_len.
71 * \param[in] port_str_len Size of buffer port_str points at.
72 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
73 * \param[in] flags flags as passed to getnameinfo().
74 * \returns 0 on success, an error code on error.
75 *
76 * Return the IPv4 or IPv6 address string and the port (a.k.a. service) string
77 * representations of the given struct osmo_sockaddr in two caller provided
78 * char buffers. Flags of (NI_NUMERICHOST | NI_NUMERICSERV) return numeric
79 * address and port. Either one of addr_str or port_str may be NULL, in which
80 * case nothing is returned there.
81 *
82 * See also osmo_sockaddr_to_str() (less flexible, but much more convenient). */
83int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
84 char *port_str, size_t port_str_len,
85 const struct osmo_sockaddr *addr,
86 int flags);
87
88
89/*! \brief conveniently concatenate the parts returned by osmo_sockaddr_to_strs().
90 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
91 * \param[in] buf A buffer to use for string operations.
92 * \param[in] buf_len Length of the buffer.
93 * \returns Address string (in buffer).
94 *
95 * Compose a string of the numeric IP-address and port represented by *addr of
96 * the form "<ip-addr> port <port>". The returned string is valid until the
97 * next invocation of this function.
98 */
99const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
100 char *buf, size_t buf_len);
101
102/*! \brief conveniently return osmo_sockaddr_to_strb() in a static buffer.
103 * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
104 * \returns Address string in static buffer.
105 *
106 * See osmo_sockaddr_to_strb().
107 *
108 * Note: only one osmo_sockaddr_to_str() call will work per print/log
109 * statement. For two or more, use osmo_sockaddr_to_strb() with a separate
110 * buffer each.
111 */
112const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr);
113
114/*! \brief compare two osmo_sockaddr.
115 * \param[in] a The first address to compare.
116 * \param[in] b The other address to compare.
117 * \returns 0 if equal, otherwise -1 or 1.
118 */
119int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, const struct osmo_sockaddr *b);
120
121/*! \brief Overwrite *dst with *src.
122 * Like memcpy(), but copy only the valid bytes. */
123void osmo_sockaddr_copy(struct osmo_sockaddr *dst, const struct osmo_sockaddr *src);
124
125
126/* general */
127
128enum gtphub_plane_idx {
129 GTPH_PLANE_CTRL = 0,
130 GTPH_PLANE_USER = 1,
131 GTPH_PLANE_N
132};
133
134extern const char* const gtphub_plane_idx_names[GTPH_PLANE_N];
135extern const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N];
136
137/* A host address in the form that is expected in the 7.7.32 GSN Address IE.
138 * len is either 4 (IPv4) or 16 (IPv6), any other value is invalid. If no
139 * address is set, len shall be 0. */
140struct gsn_addr {
141 uint16_t len;
142 uint8_t buf[16];
143};
144
145void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src);
146int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str);
147
148/* Return gsna in numeric string form, in a static buffer. */
149const char *gsn_addr_to_str(const struct gsn_addr *gsna);
150
151/* note: strbuf_len doesn't need to be larger than INET6_ADDRSTRLEN + 1. */
152const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
153 char *strbuf, int strbuf_len);
154
155/* Return 1 on match, zero otherwise. */
156int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b);
157
Neels Hofmeyrc2275942015-11-10 22:07:04 +0100158/* Decode sa to gsna. Return 0 on success. If port is non-NULL, the port number
159 * from sa is also returned. */
160int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
161 const struct osmo_sockaddr *sa);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200162
163/* expiry */
164
165struct expiring_item;
166typedef void (*del_cb_t)(struct expiring_item *);
167
168struct expiring_item {
169 struct llist_head entry;
170 time_t expiry;
171 del_cb_t del_cb;
172};
173
174struct expiry {
175 int expiry_in_seconds;
176 struct llist_head items;
177};
178
179/* Initialize an expiry queue. */
180void expiry_init(struct expiry *exq, int expiry_in_seconds);
181
182/* Add a new mapping, or restart the expiry timeout for an already listed mapping. */
183void expiry_add(struct expiry *exq, struct expiring_item *mapping, time_t now);
184
185/* Remove the given item from its expiry queue, and call item->del_cb, if set.
186 * This sets item->del_cb to NULL and is harmless when run a second time on the
187 * same item, so the del_cb may choose to call this function, too, to allow
188 * deleting items from several code paths. */
189void expiring_item_del(struct expiring_item *item);
190
191/* Carry out due expiry of mappings. Must be invoked regularly.
192 * 'now' is the current clock count in seconds and must correspond to the clock
193 * count passed to nr_map_add(). A monotonous clock counter should be used. */
194int expiry_tick(struct expiry *exq, time_t now);
195
196
197/* number map */
198
199/* A number map assigns a "random" mapped number to each user provided number.
200 * If the same number is requested multiple times, the same mapped number is
201 * returned.
202 *
203 * Number maps plug into possibly shared pools and expiry queues, for example:
204 *
205 * mapA -----------+-> pool1 <-+-- mapB
206 * {10->1, 11->5} | {1, 2, 3, ...} | {10->2, 11->3}
207 * | |
208 * | |
209 * /-> \-> expiry1 <-/
210 * | (30 seconds)
211 * |
212 * mapC -------+-----> pool2 <-+-- mapD
213 * {10->1, 11->3} {1, 2, 3, ...} | {10->2, 11->5}
214 * |
215 * expiry2 <-/
216 * (60 seconds)
217 *
218 * A map contains mappings ("10->1"). Each map needs a number pool, which can
219 * be shared with other maps. Each new mapping receives a number from the pool,
220 * which is then unavailable to any other map using the same pool.
221 *
222 * A map may point at an expiry queue, in which case all mappings added to it
223 * are also appended to the expiry queue (using a separate llist entry in the
224 * mapping). Any number of maps may submit to the same expiry queue, if they
225 * desire the same expiry timeout. An expiry queue stores the mappings in
226 * chronological order, so that expiry checking is needed only from the start
227 * of the queue; hence only mappings with identical expiry timeout can be added
228 * to the same expiry queue. Upon expiry, a mapping is dropped from the map it
229 * was submitted at. expiry_tick() needs to be called regularly for each expiry
230 * queue.
231 *
232 * A nr_mapping can be embedded in a larger struct: each mapping can have a
233 * distinct destructor (del_cb), and each del_cb can figure out the container
234 * struct's address and free that upon expiry or manual deletion. So in expiry
235 * queues (and even maps), mappings of different container types can be mixed.
236 * This can help to drastically reduce the amount of unnecessary visits during
237 * expiry checking, for the case that no expiry is pending. An expiry queue
238 * always knows which mappings to expire next, because they are right at the
239 * start of its list.
240 *
241 * Mapping allocation and a del_cb are provided by the caller. If del_cb is
242 * NULL, no deallocation will be done (allowing statically allocated entries).
243 */
244/* TODO at some point I thought the allocation & del_cb complexity was
245 * needed/helpful, but by now it seems like overkill. Maybe lose that again. */
246
247typedef int nr_t;
248
249/* Generator for unused numbers. So far this counts upwards from zero, but the
250 * implementation may change in the future. Treat this like an opaque struct.
251 * If this becomes random, the tests need to be fixed. */
252struct nr_pool {
253 nr_t last_nr;
254 /* TODO add min, max, for safe wrapping */
255};
256
257struct nr_mapping {
258 struct llist_head entry;
259 struct expiring_item expiry_entry;
260
261 void *origin;
262 nr_t orig;
263 nr_t repl;
264};
265
266struct nr_map {
267 struct nr_pool *pool; /* multiple nr_maps can share a nr_pool. */
268 struct expiry *add_items_to_expiry;
269 struct llist_head mappings;
270};
271
272
273void nr_pool_init(struct nr_pool *pool);
274
275/* Return the next unused number from the nr_pool. */
276nr_t nr_pool_next(struct nr_pool *pool);
277
278/* Initialize the nr_mapping to zero/empty values. */
279void nr_mapping_init(struct nr_mapping *mapping);
280
281/* Remove the given mapping from its parent map and expiry queue, and call
282 * mapping->del_cb, if set. */
283void nr_mapping_del(struct nr_mapping *mapping);
284
285/* Initialize an (already allocated) nr_map, and set the map's number pool.
286 * Multiple nr_map instances may use the same nr_pool. Set the nr_map's expiry
287 * queue to exq, so that all added mappings are automatically expired after the
288 * time configured in exq. exq may be NULL to disable automatic expiry. */
289void nr_map_init(struct nr_map *map, struct nr_pool *pool,
290 struct expiry *exq);
291
292/* Add a new entry to the map. mapping->orig, mapping->origin and
293 * mapping->del_cb must be set before calling this function. The remaining
294 * fields of *mapping will be overwritten. mapping->repl is set to the next
295 * available mapped number from map->pool. 'now' is the current clock count in
296 * seconds; if no map->expiry is used, just pass 0 for 'now'. */
297void nr_map_add(struct nr_map *map, struct nr_mapping *mapping,
298 time_t now);
299
300/* Return a known mapping from nr_orig and the given origin. If nr_orig is
301 * unknown, return NULL. */
302struct nr_mapping *nr_map_get(const struct nr_map *map,
303 void *origin, nr_t nr_orig);
304
305/* Return a known mapping to nr_repl. If nr_repl is unknown, return NULL. */
306struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl);
307
308/* Remove all mappings from map. */
309void nr_map_clear(struct nr_map *map);
310
311/* Return 1 if map has no entries, 0 otherwise. */
312int nr_map_empty(const struct nr_map *map);
313
314
315/* config */
316
317static const int GTPH_SEQ_MAPPING_EXPIRY_SECS = 30; /* TODO is there a spec for this? */
318static const int GTPH_TEI_MAPPING_EXPIRY_MINUTES = 6 * 60; /* TODO is there a spec for this? */
319
320struct gtphub_cfg_addr {
321 const char *addr_str;
322 uint16_t port;
323};
324
325struct gtphub_cfg_bind {
326 struct gtphub_cfg_addr bind;
327};
328
329struct gtphub_cfg {
330 struct gtphub_cfg_bind to_sgsns[GTPH_PLANE_N];
331 struct gtphub_cfg_bind to_ggsns[GTPH_PLANE_N];
332 struct gtphub_cfg_addr sgsn_proxy[GTPH_PLANE_N];
333 struct gtphub_cfg_addr ggsn_proxy[GTPH_PLANE_N];
334};
335
336
337/* state */
338
339struct gtphub_peer {
340 struct llist_head entry;
341
342 struct llist_head addresses; /* Alternatives, not load balancing. */
343 struct nr_pool seq_pool;
344 struct nr_map seq_map;
345};
346
347struct gtphub_peer_addr {
348 struct llist_head entry;
349
350 struct gtphub_peer *peer;
351 struct gsn_addr addr;
352 struct llist_head ports;
353};
354
355struct gtphub_peer_port {
356 struct llist_head entry;
357
358 struct gtphub_peer_addr *peer_addr;
359 uint16_t port;
360 unsigned int ref_count; /* references from other peers' seq_maps */
361 struct osmo_sockaddr sa;
362};
363
364struct gtphub_bind {
365 struct gsn_addr local_addr;
366 struct osmo_fd ofd;
367
368 /* list of struct gtphub_peer */
369 struct llist_head peers;
370};
371
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100372struct gtphub_resolved_ggsn {
373 struct llist_head entry;
374 struct expiring_item expiry_entry;
375
376 /* The APN OI, the Operator Identifier, is the combined address,
377 * including parts of the IMSI and APN NI, and ending with ".gprs". */
378 char apn_oi_str[GSM_APN_LENGTH];
379
380 /* Which address and port we resolved that to. */
381 struct gtphub_peer_port *peer;
382};
383
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200384struct gtphub {
385 struct gtphub_bind to_sgsns[GTPH_PLANE_N];
386 struct gtphub_bind to_ggsns[GTPH_PLANE_N];
387
388 /* pointers to an entry of to_sgsns[x].peers */
389 struct gtphub_peer_port *sgsn_proxy[GTPH_PLANE_N];
390
391 /* pointers to an entry of to_ggsns[x].peers */
392 struct gtphub_peer_port *ggsn_proxy[GTPH_PLANE_N];
393
394 struct nr_map tei_map[GTPH_PLANE_N];
395 struct nr_pool tei_pool[GTPH_PLANE_N];
396
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100397 struct llist_head ggsn_lookups; /* opaque (gtphub_ext.c) */
398 struct llist_head resolved_ggsns; /* struct gtphub_resolved_ggsn */
399
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200400 struct osmo_timer_list gc_timer;
401 struct expiry expire_seq_maps;
402 struct expiry expire_tei_maps;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100403
404 uint16_t restart_counter;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200405};
406
407struct gtp_packet_desc;
408
409
410/* api */
411
412int gtphub_vty_init(void);
413int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file);
414
415/* Initialize and start gtphub: bind to ports, run expiry timers. */
416int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg);
417
418time_t gtphub_now(void);
419
420/* Remove expired items, empty peers, ... */
421void gtphub_gc(struct gtphub *hub, time_t now);
422
423/* Return the string of the first address for this peer. */
424const char *gtphub_peer_str(struct gtphub_peer *peer);
425/* Same with a different static buffer. We often want to print two peers. */
426const char *gtphub_peer_str2(struct gtphub_peer *peer);
427
428int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
429 unsigned int port_idx,
430 const struct osmo_sockaddr *from_addr,
431 uint8_t *buf,
432 size_t received,
433 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100434 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200435 struct osmo_fd **to_ofd,
436 struct osmo_sockaddr *to_addr);
437
438int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
439 unsigned int port_idx,
440 const struct osmo_sockaddr *from_addr,
441 uint8_t *buf,
442 size_t received,
443 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100444 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200445 struct osmo_fd **to_ofd,
446 struct osmo_sockaddr *to_addr);
447
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100448struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
449 struct gtphub_bind *bind,
450 const struct gsn_addr *addr,
451 uint16_t port);
452
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200453struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
454 const struct osmo_sockaddr *addr);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100455
456void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
457 struct gsn_addr *resolved_addr,
458 time_t now);
459
460const char *gtphub_port_str(struct gtphub_peer_port *port);