blob: 514b1a375d5e2f3d6aba232093f33f26d14923bd [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
158
159/* expiry */
160
161struct expiring_item;
162typedef void (*del_cb_t)(struct expiring_item *);
163
164struct expiring_item {
165 struct llist_head entry;
166 time_t expiry;
167 del_cb_t del_cb;
168};
169
170struct expiry {
171 int expiry_in_seconds;
172 struct llist_head items;
173};
174
175/* Initialize an expiry queue. */
176void expiry_init(struct expiry *exq, int expiry_in_seconds);
177
178/* Add a new mapping, or restart the expiry timeout for an already listed mapping. */
179void expiry_add(struct expiry *exq, struct expiring_item *mapping, time_t now);
180
181/* Remove the given item from its expiry queue, and call item->del_cb, if set.
182 * This sets item->del_cb to NULL and is harmless when run a second time on the
183 * same item, so the del_cb may choose to call this function, too, to allow
184 * deleting items from several code paths. */
185void expiring_item_del(struct expiring_item *item);
186
187/* Carry out due expiry of mappings. Must be invoked regularly.
188 * 'now' is the current clock count in seconds and must correspond to the clock
189 * count passed to nr_map_add(). A monotonous clock counter should be used. */
190int expiry_tick(struct expiry *exq, time_t now);
191
192
193/* number map */
194
195/* A number map assigns a "random" mapped number to each user provided number.
196 * If the same number is requested multiple times, the same mapped number is
197 * returned.
198 *
199 * Number maps plug into possibly shared pools and expiry queues, for example:
200 *
201 * mapA -----------+-> pool1 <-+-- mapB
202 * {10->1, 11->5} | {1, 2, 3, ...} | {10->2, 11->3}
203 * | |
204 * | |
205 * /-> \-> expiry1 <-/
206 * | (30 seconds)
207 * |
208 * mapC -------+-----> pool2 <-+-- mapD
209 * {10->1, 11->3} {1, 2, 3, ...} | {10->2, 11->5}
210 * |
211 * expiry2 <-/
212 * (60 seconds)
213 *
214 * A map contains mappings ("10->1"). Each map needs a number pool, which can
215 * be shared with other maps. Each new mapping receives a number from the pool,
216 * which is then unavailable to any other map using the same pool.
217 *
218 * A map may point at an expiry queue, in which case all mappings added to it
219 * are also appended to the expiry queue (using a separate llist entry in the
220 * mapping). Any number of maps may submit to the same expiry queue, if they
221 * desire the same expiry timeout. An expiry queue stores the mappings in
222 * chronological order, so that expiry checking is needed only from the start
223 * of the queue; hence only mappings with identical expiry timeout can be added
224 * to the same expiry queue. Upon expiry, a mapping is dropped from the map it
225 * was submitted at. expiry_tick() needs to be called regularly for each expiry
226 * queue.
227 *
228 * A nr_mapping can be embedded in a larger struct: each mapping can have a
229 * distinct destructor (del_cb), and each del_cb can figure out the container
230 * struct's address and free that upon expiry or manual deletion. So in expiry
231 * queues (and even maps), mappings of different container types can be mixed.
232 * This can help to drastically reduce the amount of unnecessary visits during
233 * expiry checking, for the case that no expiry is pending. An expiry queue
234 * always knows which mappings to expire next, because they are right at the
235 * start of its list.
236 *
237 * Mapping allocation and a del_cb are provided by the caller. If del_cb is
238 * NULL, no deallocation will be done (allowing statically allocated entries).
239 */
240/* TODO at some point I thought the allocation & del_cb complexity was
241 * needed/helpful, but by now it seems like overkill. Maybe lose that again. */
242
243typedef int nr_t;
244
245/* Generator for unused numbers. So far this counts upwards from zero, but the
246 * implementation may change in the future. Treat this like an opaque struct.
247 * If this becomes random, the tests need to be fixed. */
248struct nr_pool {
249 nr_t last_nr;
250 /* TODO add min, max, for safe wrapping */
251};
252
253struct nr_mapping {
254 struct llist_head entry;
255 struct expiring_item expiry_entry;
256
257 void *origin;
258 nr_t orig;
259 nr_t repl;
260};
261
262struct nr_map {
263 struct nr_pool *pool; /* multiple nr_maps can share a nr_pool. */
264 struct expiry *add_items_to_expiry;
265 struct llist_head mappings;
266};
267
268
269void nr_pool_init(struct nr_pool *pool);
270
271/* Return the next unused number from the nr_pool. */
272nr_t nr_pool_next(struct nr_pool *pool);
273
274/* Initialize the nr_mapping to zero/empty values. */
275void nr_mapping_init(struct nr_mapping *mapping);
276
277/* Remove the given mapping from its parent map and expiry queue, and call
278 * mapping->del_cb, if set. */
279void nr_mapping_del(struct nr_mapping *mapping);
280
281/* Initialize an (already allocated) nr_map, and set the map's number pool.
282 * Multiple nr_map instances may use the same nr_pool. Set the nr_map's expiry
283 * queue to exq, so that all added mappings are automatically expired after the
284 * time configured in exq. exq may be NULL to disable automatic expiry. */
285void nr_map_init(struct nr_map *map, struct nr_pool *pool,
286 struct expiry *exq);
287
288/* Add a new entry to the map. mapping->orig, mapping->origin and
289 * mapping->del_cb must be set before calling this function. The remaining
290 * fields of *mapping will be overwritten. mapping->repl is set to the next
291 * available mapped number from map->pool. 'now' is the current clock count in
292 * seconds; if no map->expiry is used, just pass 0 for 'now'. */
293void nr_map_add(struct nr_map *map, struct nr_mapping *mapping,
294 time_t now);
295
296/* Return a known mapping from nr_orig and the given origin. If nr_orig is
297 * unknown, return NULL. */
298struct nr_mapping *nr_map_get(const struct nr_map *map,
299 void *origin, nr_t nr_orig);
300
301/* Return a known mapping to nr_repl. If nr_repl is unknown, return NULL. */
302struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl);
303
304/* Remove all mappings from map. */
305void nr_map_clear(struct nr_map *map);
306
307/* Return 1 if map has no entries, 0 otherwise. */
308int nr_map_empty(const struct nr_map *map);
309
310
311/* config */
312
313static const int GTPH_SEQ_MAPPING_EXPIRY_SECS = 30; /* TODO is there a spec for this? */
314static const int GTPH_TEI_MAPPING_EXPIRY_MINUTES = 6 * 60; /* TODO is there a spec for this? */
315
316struct gtphub_cfg_addr {
317 const char *addr_str;
318 uint16_t port;
319};
320
321struct gtphub_cfg_bind {
322 struct gtphub_cfg_addr bind;
323};
324
325struct gtphub_cfg {
326 struct gtphub_cfg_bind to_sgsns[GTPH_PLANE_N];
327 struct gtphub_cfg_bind to_ggsns[GTPH_PLANE_N];
328 struct gtphub_cfg_addr sgsn_proxy[GTPH_PLANE_N];
329 struct gtphub_cfg_addr ggsn_proxy[GTPH_PLANE_N];
330};
331
332
333/* state */
334
335struct gtphub_peer {
336 struct llist_head entry;
337
338 struct llist_head addresses; /* Alternatives, not load balancing. */
339 struct nr_pool seq_pool;
340 struct nr_map seq_map;
341};
342
343struct gtphub_peer_addr {
344 struct llist_head entry;
345
346 struct gtphub_peer *peer;
347 struct gsn_addr addr;
348 struct llist_head ports;
349};
350
351struct gtphub_peer_port {
352 struct llist_head entry;
353
354 struct gtphub_peer_addr *peer_addr;
355 uint16_t port;
356 unsigned int ref_count; /* references from other peers' seq_maps */
357 struct osmo_sockaddr sa;
358};
359
360struct gtphub_bind {
361 struct gsn_addr local_addr;
362 struct osmo_fd ofd;
363
364 /* list of struct gtphub_peer */
365 struct llist_head peers;
366};
367
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100368struct gtphub_resolved_ggsn {
369 struct llist_head entry;
370 struct expiring_item expiry_entry;
371
372 /* The APN OI, the Operator Identifier, is the combined address,
373 * including parts of the IMSI and APN NI, and ending with ".gprs". */
374 char apn_oi_str[GSM_APN_LENGTH];
375
376 /* Which address and port we resolved that to. */
377 struct gtphub_peer_port *peer;
378};
379
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200380struct gtphub {
381 struct gtphub_bind to_sgsns[GTPH_PLANE_N];
382 struct gtphub_bind to_ggsns[GTPH_PLANE_N];
383
384 /* pointers to an entry of to_sgsns[x].peers */
385 struct gtphub_peer_port *sgsn_proxy[GTPH_PLANE_N];
386
387 /* pointers to an entry of to_ggsns[x].peers */
388 struct gtphub_peer_port *ggsn_proxy[GTPH_PLANE_N];
389
390 struct nr_map tei_map[GTPH_PLANE_N];
391 struct nr_pool tei_pool[GTPH_PLANE_N];
392
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100393 struct llist_head ggsn_lookups; /* opaque (gtphub_ext.c) */
394 struct llist_head resolved_ggsns; /* struct gtphub_resolved_ggsn */
395
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200396 struct osmo_timer_list gc_timer;
397 struct expiry expire_seq_maps;
398 struct expiry expire_tei_maps;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100399
400 uint16_t restart_counter;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200401};
402
403struct gtp_packet_desc;
404
405
406/* api */
407
408int gtphub_vty_init(void);
409int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file);
410
411/* Initialize and start gtphub: bind to ports, run expiry timers. */
412int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg);
413
414time_t gtphub_now(void);
415
416/* Remove expired items, empty peers, ... */
417void gtphub_gc(struct gtphub *hub, time_t now);
418
419/* Return the string of the first address for this peer. */
420const char *gtphub_peer_str(struct gtphub_peer *peer);
421/* Same with a different static buffer. We often want to print two peers. */
422const char *gtphub_peer_str2(struct gtphub_peer *peer);
423
424int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
425 unsigned int port_idx,
426 const struct osmo_sockaddr *from_addr,
427 uint8_t *buf,
428 size_t received,
429 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100430 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200431 struct osmo_fd **to_ofd,
432 struct osmo_sockaddr *to_addr);
433
434int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
435 unsigned int port_idx,
436 const struct osmo_sockaddr *from_addr,
437 uint8_t *buf,
438 size_t received,
439 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100440 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200441 struct osmo_fd **to_ofd,
442 struct osmo_sockaddr *to_addr);
443
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100444struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
445 struct gtphub_bind *bind,
446 const struct gsn_addr *addr,
447 uint16_t port);
448
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200449struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
450 const struct osmo_sockaddr *addr);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100451
452void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
453 struct gsn_addr *resolved_addr,
454 time_t now);
455
456const char *gtphub_port_str(struct gtphub_peer_port *port);