blob: c24652e07f352c5503ada71e493314e8ee25d0ef [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#include <string.h>
23#include <errno.h>
24#include <inttypes.h>
25#include <time.h>
26#include <limits.h>
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +010027#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#include <gtp.h>
33#include <gtpie.h>
34
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020035#include <osmocom/sgsn/gtphub.h>
36#include <osmocom/sgsn/debug.h>
37#include <osmocom/sgsn/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020038
39#include <osmocom/core/utils.h>
40#include <osmocom/core/logging.h>
41#include <osmocom/core/socket.h>
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010042#include <osmocom/core/rate_ctr.h>
43#include <osmocom/core/stats.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020044
Harald Welte7e82b742017-08-12 13:43:54 +020045#include <osmocom/gsm/apn.h>
46
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010047
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020048static const int GTPH_GC_TICK_SECONDS = 1;
49
50void *osmo_gtphub_ctx;
51
Neels Hofmeyr063a8022015-11-16 14:35:13 +010052/* Convenience makro, note: only within this C file. */
53#define LOG(level, fmt, args...) \
54 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020055
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010056#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
57 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020058
59/* TODO move this to osmocom/core/select.h ? */
60typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
61
62/* TODO move this to osmocom/core/linuxlist.h ? */
63#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010064#define llist_first(head, type, entry) \
65 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020066
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +010067#define __llist_last(head) (((head)->next == (head)) ? NULL : (head)->prev)
68#define llist_last(head, type, entry) \
69 llist_entry(__llist_last(head), type, entry)
70
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020071/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
72
73enum gtp_rc {
74 GTP_RC_UNKNOWN = 0,
75 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010076 GTP_RC_PDU_C = 2, /* a real packet with IEs */
77 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020078
79 GTP_RC_TOOSHORT = -1,
80 GTP_RC_UNSUPPORTED_VERSION = -2,
81 GTP_RC_INVALID_IE = -3,
82};
83
84struct gtp_packet_desc {
85 union gtp_packet *data;
86 int data_len;
87 int header_len;
88 int version;
89 uint8_t type;
90 uint16_t seq;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010091 uint32_t header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020092 uint32_t header_tei;
93 int rc; /* enum gtp_rc */
94 unsigned int plane_idx;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010095 unsigned int side_idx;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +010096 struct gtphub_tunnel *tun;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010097 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020098 union gtpie_member *ie[GTPIE_SIZE];
99};
100
Neels Hofmeyr10fc0242015-12-01 00:23:45 +0100101struct pending_delete {
102 struct llist_head entry;
103 struct expiring_item expiry_entry;
104
105 struct gtphub_tunnel *tun;
106 uint8_t teardown_ind;
107 uint8_t nsapi;
108};
109
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100110
111/* counters */
112
113enum gtphub_counters_io {
114 GTPH_CTR_PKTS_IN = 0,
115 GTPH_CTR_PKTS_OUT,
116 GTPH_CTR_BYTES_IN,
117 GTPH_CTR_BYTES_OUT
118};
119
120static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
121 { "packets.in", "Packets ( In)" },
122 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100123 { "bytes.in", "Bytes ( In)" },
124 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100125};
126
127static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
Max8a01a802017-12-20 13:10:11 +0100128 .group_name_prefix = "gtphub:bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100129 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100130 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
131 .ctr_desc = gtphub_counters_io_desc,
132 .class_id = OSMO_STATS_CLASS_GLOBAL,
133};
134
135
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100136/* support */
137
138static const char *gtp_type_str(uint8_t type)
139{
140 switch (type) {
141 case 1:
142 return " (Echo Request)";
143 case 2:
144 return " (Echo Response)";
145 case 16:
146 return " (Create PDP Ctx Request)";
147 case 17:
148 return " (Create PDP Ctx Response)";
149 case 18:
150 return " (Update PDP Ctx Request)";
151 case 19:
152 return " (Update PDP Ctx Response)";
153 case 20:
154 return " (Delete PDP Ctx Request)";
155 case 21:
156 return " (Delete PDP Ctx Response)";
157 case 255:
158 return " (User Data)";
159 default:
160 return "";
161 }
162}
163
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200164void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
165{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100166 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200167}
168
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200169int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
Alexander Couzensdd930a22020-07-18 16:45:25 +0200170 const struct sgsn_sockaddr *sa)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200171{
172 char addr_str[256];
173 char port_str[6];
174
Alexander Couzensdd930a22020-07-18 16:45:25 +0200175 if (sgsn_sockaddr_to_strs(addr_str, sizeof(addr_str),
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200176 port_str, sizeof(port_str),
177 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
178 != 0) {
179 return -1;
180 }
181
182 if (port)
183 *port = atoi(port_str);
184
185 return gsn_addr_from_str(gsna, addr_str);
186}
187
188int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
189{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100190 if ((!gsna) || (!numeric_addr_str))
191 return -1;
192
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200193 int af = AF_INET;
194 gsna->len = 4;
195 const char *pos = numeric_addr_str;
196 for (; *pos; pos++) {
197 if (*pos == ':') {
198 af = AF_INET6;
199 gsna->len = 16;
200 break;
201 }
202 }
203
204 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
205 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100206 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
207 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200208 return -1;
209 }
210 return 0;
211}
212
213const char *gsn_addr_to_str(const struct gsn_addr *gsna)
214{
215 static char buf[INET6_ADDRSTRLEN + 1];
216 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
217}
218
219const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
220 char *strbuf,
221 int strbuf_len)
222{
223 int af;
224 switch (gsna->len) {
225 case 4:
226 af = AF_INET;
227 break;
228 case 16:
229 af = AF_INET6;
230 break;
231 default:
232 return NULL;
233 }
234
235 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
236 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100237 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
238 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100239 strerror(errno),
240 (int)gsna->len,
241 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200242 }
243 return r;
244}
245
246int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
247{
248 if (a == b)
249 return 1;
250 if ((!a) || (!b))
251 return 0;
252 if (a->len != b->len)
253 return 0;
254 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
255}
256
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100257static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
258 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100260 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200261 return -1;
262
263 unsigned int len;
264 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
265 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
266 &len, gsna->buf, sizeof(gsna->buf))
267 != 0)
268 return -1;
269 gsna->len = len;
270 return 0;
271}
272
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100273static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
274 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200275{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100276 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200277 return -1;
278
279 int ie_idx;
280 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
281
282 if (ie_idx < 0)
283 return -1;
284
285 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
286 int ie_l = ntoh16(ie->l);
287 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100288 LOG(LOGL_ERROR, "Not implemented:"
289 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200290 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
291 return -1;
292 }
293
294 memcpy(ie->v, gsna->buf, (int)ie_l);
295 return 0;
296}
297
298/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
299 */
300void validate_gtp0_header(struct gtp_packet_desc *p)
301{
302 const struct gtp0_header *pheader = &(p->data->gtp0.h);
303 p->rc = GTP_RC_UNKNOWN;
304 p->header_len = 0;
305
306 OSMO_ASSERT(p->data_len >= 1);
307 OSMO_ASSERT(p->version == 0);
308
309 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100310 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200311 p->rc = GTP_RC_TOOSHORT;
312 return;
313 }
314
315 p->type = ntoh8(pheader->type);
316 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100317 p->header_tei_rx = 0; /* TODO */
318 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200319
320 if (p->data_len == GTP0_HEADER_SIZE) {
321 p->rc = GTP_RC_TINY;
322 p->header_len = GTP0_HEADER_SIZE;
323 return;
324 }
325
326 /* Check packet length field versus length of packet */
327 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100328 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
329 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100330 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
331 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200332 p->rc = GTP_RC_TOOSHORT;
333 return;
334 }
335
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100336 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200337 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100338 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200339}
340
341/* Validate GTP version 1 data, and update p->rc with the result, as well as
342 * p->header_len in case of a valid header. */
343void validate_gtp1_header(struct gtp_packet_desc *p)
344{
345 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
346 p->rc = GTP_RC_UNKNOWN;
347 p->header_len = 0;
348
349 OSMO_ASSERT(p->data_len >= 1);
350 OSMO_ASSERT(p->version == 1);
351
352 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
353 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100354 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200355 p->rc = GTP_RC_TOOSHORT;
356 return;
357 }
358
359 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100360 p->header_tei_rx = ntoh32(pheader->tei);
361 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200362 p->seq = ntoh16(pheader->seq);
363
Neels Hofmeyr28a70f22015-12-06 15:22:54 +0100364 LOG(LOGL_DEBUG, "| GTPv1\n");
365 LOG(LOGL_DEBUG, "| type = %" PRIu8 " 0x%02" PRIx8 "\n", p->type, p->type);
366 LOG(LOGL_DEBUG, "| length = %" PRIu16 " 0x%04" PRIx16 "\n", ntoh16(pheader->length), ntoh16(pheader->length));
367 LOG(LOGL_DEBUG, "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", p->header_tei_rx, p->header_tei_rx);
368 LOG(LOGL_DEBUG, "| seq = %" PRIu16 " 0x%04" PRIx16 "\n", p->seq, p->seq);
369 LOG(LOGL_DEBUG, "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->npdu, pheader->npdu);
370 LOG(LOGL_DEBUG, "| next = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->next, pheader->next);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200371
372 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
373 p->rc = GTP_RC_TINY;
374 p->header_len = GTP1_HEADER_SIZE_SHORT;
375 return;
376 }
377
378 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100379 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
380 if (p->data_len != announced_len) {
381 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
382 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100383 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
384 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200385 p->rc = GTP_RC_TOOSHORT;
386 return;
387 }
388
Neels Hofmeyre921e322015-11-11 00:45:50 +0100389 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200390 p->header_len = GTP1_HEADER_SIZE_LONG;
391}
392
393/* Examine whether p->data of size p->data_len has a valid GTP header. Set
394 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
395 * gtp_rc). p->data must point at a buffer with p->data_len set. */
396void validate_gtp_header(struct gtp_packet_desc *p)
397{
398 p->rc = GTP_RC_UNKNOWN;
399
400 /* Need at least 1 byte in order to check version */
401 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100402 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
403 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200404 p->rc = GTP_RC_TOOSHORT;
405 return;
406 }
407
408 p->version = p->data->flags >> 5;
409
410 switch (p->version) {
411 case 0:
412 validate_gtp0_header(p);
413 break;
414 case 1:
415 validate_gtp1_header(p);
416 break;
417 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100418 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200419 p->rc = GTP_RC_UNSUPPORTED_VERSION;
420 break;
421 }
422}
423
424
425/* Return the value of the i'th IMSI IEI by copying to *imsi.
426 * The first IEI is reached by passing i = 0.
427 * imsi must point at allocated space of (at least) 8 bytes.
428 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100429static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200430{
431 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
432}
433
434/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100435static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200436{
437 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
438}
439
440static char imsi_digit_to_char(uint8_t nibble)
441{
442 nibble &= 0x0f;
443 if (nibble > 9)
444 return (nibble == 0x0f) ? '\0' : '?';
445 return '0' + nibble;
446}
447
448/* Return a human readable IMSI string, in a static buffer.
449 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100450static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200451{
452 static char str[17];
453 int i;
454
455 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100456 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100457 c = imsi_digit_to_char(imsi[i]);
458 if (c == '?')
459 return -1;
460 str[2*i] = c;
461
462 c = imsi_digit_to_char(imsi[i] >> 4);
463 if (c == '?')
464 return -1;
465 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200466 }
467 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100468 *imsi_str = str;
469 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200470}
471
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100472/* Return 0 if not present, 1 if present and decoded successfully, -1 if
473 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100474static int get_ie_imsi_str(union gtpie_member *ie[], int i,
475 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100476{
477 uint8_t imsi_buf[8];
478 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100479 return 0;
480 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100481}
482
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100483/* Return 0 if not present, 1 if present and decoded successfully, -1 if
484 * present but cannot be decoded. */
485static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100486{
487 static char apn_buf[GSM_APN_LENGTH];
488 unsigned int len;
489 if (gtpie_gettlv(ie, GTPIE_APN, 0,
490 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100491 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100492
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100493 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100494 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
495 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100496 return -1;
497 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100498
499 if (len > (sizeof(apn_buf) - 1))
500 len = sizeof(apn_buf) - 1;
501 apn_buf[len] = '\0';
502
Harald Welte7e82b742017-08-12 13:43:54 +0200503 *apn_str = osmo_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100504 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100505 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
506 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100507 return -1;
508 }
509 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100510}
511
512
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200513/* Validate header, and index information elements. Write decoded packet
514 * information to *res. res->data will point at the given data buffer. On
515 * error, p->rc is set <= 0 (see enum gtp_rc). */
516static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100517 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200518 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100519 struct gtp_packet_desc *res,
520 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200521{
522 ZERO_STRUCT(res);
523 res->data = (union gtp_packet*)data;
524 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100525 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200526 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100527 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200528
529 validate_gtp_header(res);
530
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100531 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200532 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100534 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200535
Neels Hofmeyre921e322015-11-11 00:45:50 +0100536 if (from_plane_idx == GTPH_PLANE_USER) {
537 res->rc = GTP_RC_PDU_U;
538 return;
539 }
540
541 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100542 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200543 return;
544 }
545
546 if (gtpie_decaps(res->ie, res->version,
547 (void*)(data + res->header_len),
548 res->data_len - res->header_len) != 0) {
549 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100550 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100551 " Dropping GTP packet%s.\n",
552 gtp_type_str(res->type)
553 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200554 return;
555 }
556
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100557#if 1
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100558 /* TODO if (<loglevel is debug>)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100559 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200560 int i;
561
562 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100563 const char *imsi;
564 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200565 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100566 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200567 }
568
569 for (i = 0; i < 10; i++) {
570 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100571 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200572 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100573 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200574 }
575
576 for (i = 0; i < 2; i++) {
577 struct gsn_addr addr;
578 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100579 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200580 }
581
582 for (i = 0; i < 10; i++) {
583 uint32_t tei;
584 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
585 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100586 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200587 tei, tei);
588 }
589
590 for (i = 0; i < 10; i++) {
591 uint32_t tei;
592 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
593 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100594 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200595 tei, tei);
596 }
597#endif
598}
599
600
601/* expiry */
602
603void expiry_init(struct expiry *exq, int expiry_in_seconds)
604{
605 ZERO_STRUCT(exq);
606 exq->expiry_in_seconds = expiry_in_seconds;
607 INIT_LLIST_HEAD(&exq->items);
608}
609
610void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
611{
612 item->expiry = now + exq->expiry_in_seconds;
613
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100614 OSMO_ASSERT(llist_empty(&exq->items)
615 || (item->expiry
616 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
617
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200618 /* Add/move to the tail to always sort by expiry, ascending. */
619 llist_del(&item->entry);
620 llist_add_tail(&item->entry, &exq->items);
621}
622
623int expiry_tick(struct expiry *exq, time_t now)
624{
625 int expired = 0;
626 struct expiring_item *m, *n;
627 llist_for_each_entry_safe(m, n, &exq->items, entry) {
628 if (m->expiry <= now) {
629 expiring_item_del(m);
630 expired ++;
631 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200632 /* The items are added sorted by expiry. So when we hit
633 * an unexpired entry, only more unexpired ones will
634 * follow. */
635 break;
636 }
637 }
638 return expired;
639}
640
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100641void expiry_clear(struct expiry *exq)
642{
643 struct expiring_item *m, *n;
644 llist_for_each_entry_safe(m, n, &exq->items, entry) {
645 expiring_item_del(m);
646 }
647}
648
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200649void expiring_item_init(struct expiring_item *item)
650{
651 ZERO_STRUCT(item);
652 INIT_LLIST_HEAD(&item->entry);
653}
654
655void expiring_item_del(struct expiring_item *item)
656{
657 OSMO_ASSERT(item);
658 llist_del(&item->entry);
659 INIT_LLIST_HEAD(&item->entry);
660 if (item->del_cb) {
661 /* avoid loops */
662 del_cb_t del_cb = item->del_cb;
663 item->del_cb = 0;
664 (del_cb)(item);
665 }
666}
667
668
669/* nr_map, nr_pool */
670
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100671void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200672{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100673 *pool = (struct nr_pool){
674 .nr_min = nr_min,
675 .nr_max = nr_max,
676 .last_nr = nr_max
677 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200678}
679
680nr_t nr_pool_next(struct nr_pool *pool)
681{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100682 if (pool->last_nr >= pool->nr_max)
683 pool->last_nr = pool->nr_min;
684 else
685 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200686
687 return pool->last_nr;
688}
689
690void nr_map_init(struct nr_map *map, struct nr_pool *pool,
691 struct expiry *exq)
692{
693 ZERO_STRUCT(map);
694 map->pool = pool;
695 map->add_items_to_expiry = exq;
696 INIT_LLIST_HEAD(&map->mappings);
697}
698
699void nr_mapping_init(struct nr_mapping *m)
700{
701 ZERO_STRUCT(m);
702 INIT_LLIST_HEAD(&m->entry);
703 expiring_item_init(&m->expiry_entry);
704}
705
706void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
707{
708 /* Generate a mapped number */
709 mapping->repl = nr_pool_next(map->pool);
710
711 /* Add to the tail to always yield a list sorted by expiry, in
712 * ascending order. */
713 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100714 nr_map_refresh(map, mapping, now);
715}
716
717void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
718{
719 if (!map->add_items_to_expiry)
720 return;
721 expiry_add(map->add_items_to_expiry,
722 &mapping->expiry_entry,
723 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200724}
725
726void nr_map_clear(struct nr_map *map)
727{
728 struct nr_mapping *m;
729 struct nr_mapping *n;
730 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
731 nr_mapping_del(m);
732 }
733}
734
735int nr_map_empty(const struct nr_map *map)
736{
737 return llist_empty(&map->mappings);
738}
739
740struct nr_mapping *nr_map_get(const struct nr_map *map,
741 void *origin, nr_t nr_orig)
742{
743 struct nr_mapping *mapping;
744 llist_for_each_entry(mapping, &map->mappings, entry) {
745 if ((mapping->origin == origin)
746 && (mapping->orig == nr_orig))
747 return mapping;
748 }
749 /* Not found. */
750 return NULL;
751}
752
753struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
754{
755 struct nr_mapping *mapping;
756 llist_for_each_entry(mapping, &map->mappings, entry) {
757 if (mapping->repl == nr_repl) {
758 return mapping;
759 }
760 }
761 /* Not found. */
762 return NULL;
763}
764
765void nr_mapping_del(struct nr_mapping *mapping)
766{
767 OSMO_ASSERT(mapping);
768 llist_del(&mapping->entry);
769 INIT_LLIST_HEAD(&mapping->entry);
770 expiring_item_del(&mapping->expiry_entry);
771}
772
773
774/* gtphub */
775
776const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
777 "CTRL",
778 "USER",
779};
780
781const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
782 2123,
783 2152,
784};
785
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100786const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
787 "SGSN",
788 "GGSN",
789};
790
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200791time_t gtphub_now(void)
792{
793 struct timespec now_tp;
Pau Espin Pedrol36abead2018-08-17 13:27:20 +0200794 OSMO_ASSERT(osmo_clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200795 return now_tp.tv_sec;
796}
797
798/* Remove a gtphub_peer from its list and free it. */
799static void gtphub_peer_del(struct gtphub_peer *peer)
800{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100801 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200802 nr_map_clear(&peer->seq_map);
803 llist_del(&peer->entry);
804 talloc_free(peer);
805}
806
807static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
808{
809 OSMO_ASSERT(llist_empty(&pa->ports));
810 llist_del(&pa->entry);
811 talloc_free(pa);
812}
813
814static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
815{
816 OSMO_ASSERT(pp->ref_count == 0);
817 llist_del(&pp->entry);
Neels Hofmeyre38fb662015-12-06 16:44:14 +0100818 rate_ctr_group_free(pp->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200819 talloc_free(pp);
820}
821
822/* From the information in the gtp_packet_desc, return the address of a GGSN.
823 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100824static int gtphub_resolve_ggsn(struct gtphub *hub,
825 struct gtp_packet_desc *p,
826 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200827
828/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100829struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
830 const char *imsi_str,
831 const char *apn_ni_str);
832int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200833
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200834static void gtphub_zero(struct gtphub *hub)
835{
836 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100837 INIT_LLIST_HEAD(&hub->ggsn_lookups);
838 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200839}
840
841static int gtphub_sock_init(struct osmo_fd *ofd,
842 const struct gtphub_cfg_addr *addr,
843 osmo_fd_cb_t cb,
844 void *data,
845 int ofd_id)
846{
847 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100848 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200849 return -1;
850 }
851 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100852 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200853 return -1;
854 }
855
Harald Weltebe2330f2020-10-19 12:32:20 +0200856 osmo_fd_setup(ofd, -1, OSMO_FD_READ, cb, data, ofd_id);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200857
858 int rc;
859 rc = osmo_sock_init_ofd(ofd,
860 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
861 addr->addr_str, addr->port,
862 OSMO_SOCK_F_BIND);
863 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100864 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
865 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200866 return -1;
867 }
868
869 return 0;
870}
871
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100872static void gtphub_sock_close(struct osmo_fd *ofd)
873{
874 close(ofd->fd);
875 osmo_fd_unregister(ofd);
876 ofd->cb = NULL;
877}
878
Maxbb6b00a2017-12-20 12:14:50 +0100879static void gtphub_bind_init(struct gtphub_bind *b, uint32_t idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880{
881 ZERO_STRUCT(b);
882
883 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100884
885 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
Maxbb6b00a2017-12-20 12:14:50 +0100886 &gtphub_ctrg_io_desc, idx);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100887 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200888}
889
890static int gtphub_bind_start(struct gtphub_bind *b,
891 const struct gtphub_cfg_bind *cfg,
892 osmo_fd_cb_t cb, void *cb_data,
893 unsigned int ofd_id)
894{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100895 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
896 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
897 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
898 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200899 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100900 }
901 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
902 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
903 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200904 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100905 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100906 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200907 return 0;
908}
909
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100910static void gtphub_bind_free(struct gtphub_bind *b)
911{
912 OSMO_ASSERT(llist_empty(&b->peers));
Maxbb6b00a2017-12-20 12:14:50 +0100913 if (b->counters_io)
914 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100915}
916
917static void gtphub_bind_stop(struct gtphub_bind *b) {
918 gtphub_sock_close(&b->ofd);
919 gtphub_bind_free(b);
920}
921
Neels Hofmeyr40348972015-11-17 12:22:28 +0100922/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923 * Return the number of bytes read, zero on error. */
924static int gtphub_read(const struct osmo_fd *from,
Alexander Couzensdd930a22020-07-18 16:45:25 +0200925 struct sgsn_sockaddr *from_addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200926 uint8_t *buf, size_t buf_len)
927{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100928 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200929
Neels Hofmeyr40348972015-11-17 12:22:28 +0100930 /* recvfrom requires the available length set in *from_addr_len. */
931 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200932 errno = 0;
933 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100934 (struct sockaddr*)&from_addr->a,
935 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200936 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
937 * is not truncated. Then maybe reduce buf's size. */
938
939 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100940 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
941 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200942 return 0;
943 }
944
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100945 LOG(LOGL_DEBUG, "Received %d bytes from %s: %s%s\n",
Alexander Couzensdd930a22020-07-18 16:45:25 +0200946 (int)received, sgsn_sockaddr_to_str(from_addr),
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100947 osmo_hexdump(buf, received > 1000? 1000 : received),
948 received > 1000 ? "..." : "");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200949
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200950 return received;
951}
952
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100953static inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200954{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100955 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200956 OSMO_ASSERT(pp->ref_count < UINT_MAX);
957 pp->ref_count++;
958}
959
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100960static inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200961{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100962 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200963 OSMO_ASSERT(pp->ref_count > 0);
964 pp->ref_count--;
965}
966
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100967static inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200968{
969 OSMO_ASSERT(p->version == 1);
970 p->data->gtp1l.h.seq = hton16(seq);
971 p->seq = seq;
972}
973
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100974static inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200975{
976 OSMO_ASSERT(p->version == 1);
977 p->data->gtp1l.h.tei = hton32(tei);
978 p->header_tei = tei;
979}
980
981static void gtphub_mapping_del_cb(struct expiring_item *expi);
982
983static struct nr_mapping *gtphub_mapping_new()
984{
985 struct nr_mapping *nrm;
986 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
987 OSMO_ASSERT(nrm);
988
989 nr_mapping_init(nrm);
990 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
991 return nrm;
992}
993
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100994
995#define APPEND(args...) \
996 l = snprintf(pos, left, args); \
997 pos += l; \
998 left -= l
999
1000static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
1001 int side_idx)
1002{
1003 static char buf[256];
1004 char *pos = buf;
1005 int left = sizeof(buf);
1006 int l;
1007
1008 struct gtphub_tunnel_endpoint *c, *u;
1009 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1010 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
1011
1012 /* print both only if they differ. */
1013 if (!c->peer) {
1014 APPEND("(uninitialized)");
1015 } else {
1016 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
1017 }
1018
1019 if (!u->peer) {
1020 if (c->peer) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001021 APPEND("/(uninitialized)");
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001022 }
1023 } else if ((!c->peer)
1024 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1025 &c->peer->peer_addr->addr))) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001026 APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr));
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001027 }
1028
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001029 APPEND(" (TEI C=%x U=%x)",
1030 c->tei_orig,
1031 u->tei_orig);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001032 return buf;
1033}
1034
1035const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1036{
1037 static char buf[512];
1038 char *pos = buf;
1039 int left = sizeof(buf);
1040 int l;
1041
Neels Hofmeyrea619f12016-12-16 13:09:00 +01001042 if (!tun)
1043 return "null-tunnel";
1044
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001045 APPEND("TEI=%x: ", tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001046 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1047 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1048
1049 return buf;
1050}
1051
1052#undef APPEND
1053
1054void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1055 struct gtphub_peer_port *pp)
1056{
1057 if (te->peer)
1058 gtphub_port_ref_count_dec(te->peer);
1059 te->peer = pp;
1060 if (te->peer)
1061 gtphub_port_ref_count_inc(te->peer);
1062}
1063
1064int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1065{
1066 if (!tun)
1067 return 0;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001068 if (!tun->tei_repl)
1069 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001070 int side_idx;
1071 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001072 for_each_side_and_plane(side_idx, plane_idx) {
1073 struct gtphub_tunnel_endpoint *te =
1074 &tun->endpoint[side_idx][plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001075 if (!(te->peer && te->tei_orig))
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001076 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001077 }
1078 return 1;
1079}
1080
1081static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1082{
1083 struct gtphub_tunnel *tun = container_of(expi,
1084 struct gtphub_tunnel,
1085 expiry_entry);
1086 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1087
1088 llist_del(&tun->entry);
1089 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1090
1091 expi->del_cb = 0; /* avoid recursion loops */
1092 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1093
1094 int side_idx;
1095 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001096 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001097 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1098
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001099 /* clear ref count */
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001100 gtphub_tunnel_endpoint_set_peer(te, NULL);
1101
1102 rate_ctr_group_free(te->counters_io);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001103 }
1104
1105 talloc_free(tun);
1106}
1107
Maxbb6b00a2017-12-20 12:14:50 +01001108#define CTR_IDX(s, p, a, b) (a + s + (p + b) * 2)
1109
1110/* rate counter index for tunnels: [3; 6] */
1111#define CTR_IDX_TUN(s, p) CTR_IDX(s, p, 1, 1)
1112
1113/* rate counter index for hubs: [7; 10] */
1114#define CTR_IDX_HUB(s, p) CTR_IDX(s, p, 3, 2)
1115
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001116static struct gtphub_tunnel *gtphub_tunnel_new()
1117{
1118 struct gtphub_tunnel *tun;
1119 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1120 OSMO_ASSERT(tun);
1121
1122 INIT_LLIST_HEAD(&tun->entry);
1123 expiring_item_init(&tun->expiry_entry);
1124
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001125 int side_idx, plane_idx;
1126 for_each_side_and_plane(side_idx, plane_idx) {
1127 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1128 te->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
1129 &gtphub_ctrg_io_desc,
Maxbb6b00a2017-12-20 12:14:50 +01001130 CTR_IDX_TUN(side_idx, plane_idx));
1131 if (!te->counters_io)
1132 return NULL;
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001133 }
1134
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001135 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1136 return tun;
1137}
1138
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001139static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1140 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001141{
1142 if (llist_empty(&peer->addresses))
1143 return "(addressless)";
1144
1145 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1146 struct gtphub_peer_addr,
1147 entry);
1148 return gsn_addr_to_strb(&a->addr, buf, buflen);
1149}
1150
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001151static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1152 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001153{
1154 if (!port)
1155 return "(null port)";
1156
1157 snprintf(buf, buflen, "%s port %d",
1158 gsn_addr_to_str(&port->peer_addr->addr),
1159 (int)port->port);
1160 return buf;
1161}
1162
1163const char *gtphub_peer_str(struct gtphub_peer *peer)
1164{
1165 static char buf[256];
1166 return gtphub_peer_strb(peer, buf, sizeof(buf));
1167}
1168
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001169const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001170{
1171 static char buf[256];
1172 return gtphub_port_strb(port, buf, sizeof(buf));
1173}
1174
1175static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1176{
1177 static char buf[256];
1178 return gtphub_port_strb(port, buf, sizeof(buf));
1179}
1180
1181static void gtphub_mapping_del_cb(struct expiring_item *expi)
1182{
1183 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001184 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001185
1186 struct nr_mapping *nrm = container_of(expi,
1187 struct nr_mapping,
1188 expiry_entry);
1189 llist_del(&nrm->entry);
1190 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1191
1192 /* Just for log */
1193 struct gtphub_peer_port *from = nrm->origin;
1194 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001195 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001196 (int)nrm->expiry_entry.expiry,
1197 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001198 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001199
1200 gtphub_port_ref_count_dec(from);
1201
1202 talloc_free(nrm);
1203}
1204
1205static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1206 struct gtphub_peer_port *from,
1207 nr_t orig_nr,
1208 time_t now)
1209{
1210 struct nr_mapping *nrm;
1211
1212 nrm = nr_map_get(map, from, orig_nr);
1213
1214 if (!nrm) {
1215 nrm = gtphub_mapping_new();
1216 nrm->orig = orig_nr;
1217 nrm->origin = from;
1218 nr_map_add(map, nrm, now);
1219 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001220 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001221 gtphub_port_str(from),
1222 (int)(nrm->orig), (int)(nrm->repl));
1223 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001224 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001225 }
1226
1227 OSMO_ASSERT(nrm);
1228 return nrm;
1229}
1230
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001231static void gtphub_map_seq(struct gtp_packet_desc *p,
1232 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001233 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001234{
1235 /* Store a mapping in to_peer's map, so when we later receive a GTP
1236 * packet back from to_peer, the seq nr can be unmapped back to its
1237 * origin (from_peer here). */
1238 struct nr_mapping *nrm;
1239 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001240 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001241
1242 /* Change the GTP packet to yield the new, mapped seq nr */
1243 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001244}
1245
1246static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1247 struct gtphub_peer_port *responding_port)
1248{
1249 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001250 struct nr_mapping *nrm =
1251 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1252 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001253 if (!nrm)
1254 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001255 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1256 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001257 set_seq(p, nrm->orig);
1258 return nrm->origin;
1259}
1260
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001261static int gtphub_check_mapped_tei(struct gtphub_tunnel *new_tun,
1262 struct gtphub_tunnel *iterated_tun,
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001263 uint32_t *tei_min,
1264 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001265{
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001266 if (!new_tun->tei_repl || !iterated_tun->tei_repl)
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001267 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001268
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001269 *tei_min = (*tei_min < iterated_tun->tei_repl)? *tei_min : iterated_tun->tei_repl;
1270 *tei_max = (*tei_max > iterated_tun->tei_repl)? *tei_max : iterated_tun->tei_repl;
1271
1272 if (new_tun->tei_repl != iterated_tun->tei_repl)
1273 return 1;
1274
1275 /* new_tun->tei_repl is already taken. Try to find one out of the known
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001276 * range. */
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001277 LOG(LOGL_DEBUG, "TEI replacement %d already taken.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001278
1279 if ((*tei_max) < 0xffffffff) {
1280 (*tei_max)++;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001281 new_tun->tei_repl = *tei_max;
1282 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001283 return 1;
1284 } else if ((*tei_min) > 1) {
1285 (*tei_min)--;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001286 new_tun->tei_repl = *tei_min;
1287 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001288 return 1;
1289 }
1290
1291 /* None seems to be available. */
1292 return 0;
1293}
1294
1295static int gtphub_check_reused_teis(struct gtphub *hub,
1296 struct gtphub_tunnel *new_tun)
1297{
1298 uint32_t tei_min = 0xffffffff;
1299 uint32_t tei_max = 0;
1300 int side_idx;
1301 int plane_idx;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001302 struct gtphub_tunnel_endpoint *te;
1303 struct gtphub_tunnel_endpoint *te2;
1304
1305 struct gtphub_tunnel *tun, *ntun;
1306
1307 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1308 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001309 continue;
1310
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001311 /* Check whether the GSN sent a TEI that it is reusing from a
1312 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001313 int tun_continue = 0;
1314 for_each_side(side_idx) {
1315 for_each_plane(plane_idx) {
1316 te = &tun->endpoint[side_idx][plane_idx];
1317 te2 = &new_tun->endpoint[side_idx][plane_idx];
Neels Hofmeyrf6e4d082015-12-06 19:03:35 +01001318 if ((te->tei_orig == 0)
1319 || (te->tei_orig != te2->tei_orig)
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001320 || (!te->peer)
1321 || (!te2->peer)
1322 || !gsn_addr_same(&te->peer->peer_addr->addr,
1323 &te2->peer->peer_addr->addr))
1324 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001325
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001326 /* The peer is reusing a TEI that I believe to
1327 * be part of another tunnel. The other tunnel
1328 * must be stale, then. */
1329 LOG(LOGL_NOTICE,
1330 "Expiring tunnel due to reused TEI:"
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001331 " %s peer %s sent %s TEI %x,"
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001332 " previously used by tunnel %s...\n",
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001333 gtphub_side_idx_names[side_idx],
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001334 gtphub_port_str(te->peer),
1335 gtphub_plane_idx_names[plane_idx],
1336 te->tei_orig,
1337 gtphub_tunnel_str(tun));
1338 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1339 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001340
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001341 expiring_item_del(&tun->expiry_entry);
1342 /* continue to find more matches. There shouldn't be
1343 * any, but let's make sure. However, tun is deleted,
1344 * so we need to skip to the next tunnel. */
1345 tun_continue = 1;
1346 break;
1347 }
1348 if (tun_continue)
1349 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001350 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001351 if (tun_continue)
1352 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001353
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001354 /* Check whether the mapped TEI is already used by another
1355 * tunnel. */
1356 if (!gtphub_check_mapped_tei(new_tun, tun, &tei_min, &tei_max)) {
1357 LOG(LOGL_ERROR,
1358 "No mapped TEI is readily available."
1359 " Searching for holes between occupied"
1360 " TEIs not implemented.");
1361 return 0;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001362 }
1363
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001364 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001365
1366 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001367}
1368
1369static void gtphub_tunnel_refresh(struct gtphub *hub,
1370 struct gtphub_tunnel *tun,
1371 time_t now)
1372{
1373 expiry_add(&hub->expire_slowly,
1374 &tun->expiry_entry,
1375 now);
1376}
1377
1378static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1379 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001380 struct gtphub_peer_port *from,
1381 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001382{
1383 OSMO_ASSERT(from);
1384 int other_side = other_side_idx(p->side_idx);
1385
1386 struct gtphub_tunnel *tun;
1387 llist_for_each_entry(tun, &hub->tunnels, entry) {
1388 struct gtphub_tunnel_endpoint *te_from =
1389 &tun->endpoint[p->side_idx][p->plane_idx];
1390 struct gtphub_tunnel_endpoint *te_to =
1391 &tun->endpoint[other_side][p->plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001392 if ((tun->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001393 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001394 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1395 &from->peer_addr->addr)) {
1396 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001397 if (unmapped_from_tun)
1398 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001399 return te_to;
1400 }
1401 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001402
1403 if (unmapped_from_tun)
1404 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001405 return NULL;
1406}
1407
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001409 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001410{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001411 if (p->rc != GTP_RC_PDU_C)
1412 return;
1413
1414 int ie_idx;
1415 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1416 if (ie_idx < 0)
1417 return;
1418
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001419 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001420 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001421}
1422
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001423static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001424 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001425 struct gtphub *hub,
1426 struct gtp_packet_desc *p,
1427 struct gtphub_peer_port *from_port)
1428{
1429 OSMO_ASSERT(p->version == 1);
1430 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001431 if (unmapped_from_tun)
1432 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001433
1434 /* If the header's TEI is zero, no PDP context has been established
1435 * yet. If nonzero, a mapping should actually already exist for this
1436 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001437 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001438 return 0;
1439
1440 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001441 * mapped in a tunnel struct. */
1442 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001443 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001444 if (!to) {
1445 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1446 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001447 return -1;
1448 }
Neels Hofmeyr312bf6c2016-04-14 15:21:31 +02001449
1450 if (unmapped_from_tun) {
1451 OSMO_ASSERT(*unmapped_from_tun);
1452 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1453 gtphub_tunnel_str(*unmapped_from_tun));
1454 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001455
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001456 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001457 set_tei(p, unmapped_tei);
1458
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001459 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001460 *to_port_p = to->peer;
1461
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001462 return 0;
1463}
1464
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001465static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1466 struct gtp_packet_desc *p,
1467 struct gtphub_peer_port *from_ctrl,
1468 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001469{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001470 int plane_idx;
1471
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001472 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1473 plane_nrs_match_GSN_addr_IE_indices);
1474
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001475 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001476
1477 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001478 if (p->side_idx != GTPH_SIDE_SGSN) {
1479 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001480 " Request from the GGSN side: %s",
1481 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001482 return -1;
1483 }
1484
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001485 if (tun) {
1486 LOG(LOGL_ERROR, "Not implemented: Received"
1487 " Create PDP Context Request for an already"
1488 " established tunnel:"
1489 " from %s, tunnel %s\n",
1490 gtphub_port_str(from_ctrl),
1491 gtphub_tunnel_str(p->tun));
1492 return -1;
1493 }
1494
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001495 /* A new tunnel. */
Maxbb6b00a2017-12-20 12:14:50 +01001496 tun = gtphub_tunnel_new();
1497 if (!tun) {
1498 LOG(LOGL_ERROR, "Failed to allocate new tunnel %s <-> %s\n",
1499 gtphub_port_str(from_ctrl), gtphub_port_str(to_ctrl));
1500 return -1;
1501 }
1502 p->tun = tun;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001503
1504 /* Create TEI mapping */
1505 tun->tei_repl = nr_pool_next(&hub->tei_pool);
1506
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001507 llist_add(&tun->entry, &hub->tunnels);
1508 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001509 /* The endpoint peers on this side (SGSN) will be set from IEs
1510 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001511 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001512 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001513 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001514 if (p->side_idx != GTPH_SIDE_GGSN) {
1515 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001516 " Response from the SGSN side: %s",
1517 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001518 return -1;
1519 }
1520
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001521 /* The tunnel should already have been resolved from the header
1522 * TEI and be available in tun (== p->tun). Just fill in the
1523 * GSN Addresses below.*/
1524 OSMO_ASSERT(tun);
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001525 OSMO_ASSERT(tun->tei_repl == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001526 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001527 }
1528
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001529 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1530 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001531 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001532
Maxe9bcbe12017-12-20 17:01:05 +01001533 for_each_plane(plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001534 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001535 struct gsn_addr use_addr;
1536 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001537 uint32_t tei_from_ie;
1538 int ie_idx;
1539
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001540 /* Fetch GSN Address and TEI from IEs. As ensured by above
1541 * static asserts, plane_idx corresponds to the GSN Address IE
1542 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001543 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001544 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001545 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1546 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001547 return -1;
1548 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001549 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001551 gsn_addr_to_str(&use_addr),
1552 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001553
1554 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1555 if (ie_idx < 0) {
1556 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001557 LOG(LOGL_ERROR,
1558 "Create PDP Context message invalid:"
1559 " missing IE %d\n",
1560 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001561 return -1;
1562 }
1563 tei_from_ie = 0;
1564 }
1565 else
1566 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1567
1568 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001569 * exists.
1570 *
1571 * Exception: if sgsn_use_sender is set, instead use the
1572 * sender's address and port for Ctrl -- the User port is not
1573 * known until the first User packet arrives.
1574 *
1575 * Note: doing this here is just an optimization, because
1576 * gtphub_handle_buf() has code to replace the tunnel
1577 * endpoints' addresses with the sender (needed for User
1578 * plane). We could just ignore sgsn_use_sender here. But if we
1579 * set up a default port here and replace it in
1580 * gtphub_handle_buf(), we'd be creating a peer port just to
1581 * expire it right away. */
1582 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
Max99f99532017-12-20 17:02:15 +01001583 int rc = gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1584 if (rc < 0)
1585 LOG(LOGL_ERROR, "%s(): failed to obtain GSN address\n", __func__);
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001586 } else {
1587 use_port = gtphub_plane_idx_default_port[plane_idx];
1588
1589 }
1590
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001591 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001592 peer_from_ie = gtphub_port_have(hub,
1593 &hub->to_gsns[side_idx][plane_idx],
1594 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001595
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001596 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1597 peer_from_ie);
1598
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001599 if (!tei_from_ie &&
1600 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1601 LOG(LOGL_ERROR,
Neels Hofmeyr328d2f72015-12-06 19:13:21 +01001602 "Create PDP Context message omits %s TEI, but"
1603 " no TEI has been announced for this tunnel: %s\n",
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001604 gtphub_plane_idx_names[plane_idx],
1605 gtphub_tunnel_str(tun));
1606 return -1;
1607 }
1608
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001609 if (tei_from_ie) {
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001610 /* Replace TEI in GTP packet IE */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001611 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001612 p->ie[ie_idx]->tv4.v = hton32(tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001613
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001614 if (!gtphub_check_reused_teis(hub, tun)) {
1615 /* It's highly unlikely that all TEIs are
1616 * taken. But the code looking for an unused
1617 * TEI is, at the time of writing this comment,
1618 * not able to find gaps in the TEI space. To
1619 * explicitly alert the user of this problem,
1620 * rather abort than carry on. */
1621 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1622 abort();
1623 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001624 }
1625
1626 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001627 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1628 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001629 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001630 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1631 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001632 return -1;
1633 }
1634 }
1635
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001636 if (p->type == GTP_CREATE_PDP_REQ) {
1637 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1638 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001639 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001640 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1641 gtphub_tunnel_str(tun));
1642 }
1643
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001644 return 0;
1645}
1646
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001647static void pending_delete_del_cb(struct expiring_item *expi)
1648{
1649 struct pending_delete *pd;
1650 pd = container_of(expi, struct pending_delete, expiry_entry);
1651
1652 llist_del(&pd->entry);
1653 INIT_LLIST_HEAD(&pd->entry);
1654
1655 pd->expiry_entry.del_cb = 0;
1656 expiring_item_del(&pd->expiry_entry);
1657
1658 talloc_free(pd);
1659}
1660
1661static struct pending_delete *pending_delete_new(void)
1662{
1663 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1664 INIT_LLIST_HEAD(&pd->entry);
1665 expiring_item_init(&pd->expiry_entry);
1666 pd->expiry_entry.del_cb = pending_delete_del_cb;
1667 return pd;
1668}
1669
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001670static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1671 struct gtp_packet_desc *p,
1672 struct gtphub_peer_port *from_ctrl,
1673 struct gtphub_peer_port *to_ctrl)
1674{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001675 struct gtphub_tunnel *known_tun = p->tun;
1676
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001677 if (p->type == GTP_DELETE_PDP_REQ) {
1678 if (!known_tun) {
1679 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1680 return -1;
1681 }
1682
1683 /* Store the Delete Request until a successful Response is seen. */
1684 uint8_t teardown_ind;
1685 uint8_t nsapi;
1686
1687 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1688 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1689 return -1;
1690 }
1691
1692 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1693 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1694 return -1;
1695 }
1696
1697 struct pending_delete *pd = NULL;
1698
1699 struct pending_delete *pdi = NULL;
1700 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1701 if ((pdi->tun == known_tun)
1702 && (pdi->teardown_ind == teardown_ind)
1703 && (pdi->nsapi == nsapi)) {
1704 pd = pdi;
1705 break;
1706 }
1707 }
1708
1709 if (!pd) {
1710 pd = pending_delete_new();
1711 pd->tun = known_tun;
1712 pd->teardown_ind = teardown_ind;
1713 pd->nsapi = nsapi;
1714
1715 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1716 gtphub_tunnel_str(known_tun));
1717 llist_add(&pd->entry, &hub->pending_deletes);
1718 }
1719
1720 /* Add or refresh timeout. */
1721 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1722
1723 /* If a pending_delete should expire before the response to
1724 * indicate success comes in, the responding peer will have the
1725 * tunnel deactivated, while the requesting peer gets no reply
1726 * and keeps the tunnel. The hope is that the requesting peer
1727 * will try again and get a useful response. */
1728 } else if (p->type == GTP_DELETE_PDP_RSP) {
1729 /* Find the Delete Request for this Response. */
1730 struct pending_delete *pd = NULL;
1731
1732 struct pending_delete *pdi;
1733 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1734 if (known_tun == pdi->tun) {
1735 pd = pdi;
1736 break;
1737 }
1738 }
1739
1740 if (!pd) {
1741 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1742 " Cannot find matching request.");
1743 /* If we delete the tunnel now, anyone can send a
1744 * Delete response to kill tunnels at will. */
1745 return -1;
1746 }
1747
1748 /* TODO handle teardown_ind and nsapi */
1749
1750 expiring_item_del(&pd->expiry_entry);
1751
1752 uint8_t cause;
1753 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1754 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1755 " Missing Cause IE.");
1756 /* If we delete the tunnel now, at least one of the
1757 * peers may still think it is active. */
1758 return -1;
1759 }
1760
1761 if (cause != GTPCAUSE_ACC_REQ) {
1762 LOG(LOGL_NOTICE,
1763 "Delete PDP Context Response indicates failure;"
1764 "for %s\n",
1765 gtphub_tunnel_str(known_tun));
1766 return -1;
1767 }
1768
1769 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1770 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001771 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001772 expiring_item_del(&known_tun->expiry_entry);
1773 }
1774
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001775 return 0;
1776}
1777
1778static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1779 struct gtp_packet_desc *p,
1780 struct gtphub_peer_port *from_ctrl,
1781 struct gtphub_peer_port *to_ctrl)
1782{
1783 /* TODO */
1784 return 0;
1785}
1786
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001787/* Read GSN address IEs from p, and make sure these peer addresses exist in
1788 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1789 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1790 * the packet p. */
1791static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1792 struct gtp_packet_desc *p,
1793 struct gtphub_peer_port *from_ctrl,
1794 struct gtphub_peer_port *to_ctrl)
1795{
1796 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1797
1798 switch (p->type) {
1799 case GTP_CREATE_PDP_REQ:
1800 case GTP_CREATE_PDP_RSP:
1801 return gtphub_handle_create_pdp_ctx(hub, p,
1802 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001803
1804 case GTP_DELETE_PDP_REQ:
1805 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001806 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001807 from_ctrl, to_ctrl);
1808
1809 case GTP_UPDATE_PDP_REQ:
1810 case GTP_UPDATE_PDP_RSP:
1811 return gtphub_handle_update_pdp_ctx(hub, p,
1812 from_ctrl, to_ctrl);
1813
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001814 default:
1815 /* Nothing to do for this message type. */
1816 return 0;
1817 }
1818
1819}
1820
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001821static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1822 struct gtphub_tunnel *tun,
1823 int to_side)
1824{
1825 static uint8_t del_ctx_msg[16] = {
1826 0x32, /* GTP v1 flags */
1827 GTP_DELETE_PDP_REQ,
1828 0x00, 0x08, /* Length in network byte order */
1829 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1830 0, 0, /* Seq, to be replaced */
1831 0, 0, /* no extensions */
1832 0x13, 0xff, /* 19: Teardown ind = 1 */
1833 0x14, 0 /* 20: NSAPI = 0 */
1834 };
1835
1836 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1837 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1838
1839 struct gtphub_tunnel_endpoint *te =
1840 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1841
1842 if (! te->peer)
1843 return 0;
1844
1845 *tei = hton32(te->tei_orig);
1846 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1847
1848 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001849 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1850 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001851 if (rc != 0) {
1852 LOG(LOGL_ERROR,
1853 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1854 gtphub_port_str(te->peer));
1855 }
1856 return rc;
1857}
1858
1859/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1860static void gtphub_restarted(struct gtphub *hub,
1861 struct gtp_packet_desc *p,
1862 struct gtphub_peer_port *pp)
1863{
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001864 LOG(LOGL_NOTICE, "Peer has restarted: %s\n",
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001865 gtphub_port_str(pp));
1866
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001867 int deleted_count = 0;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001868 struct gtphub_tunnel *tun;
1869 llist_for_each_entry(tun, &hub->tunnels, entry) {
1870 int side_idx;
1871 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001872 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1873 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1874 if ((!te->peer)
1875 || (!te2->tei_orig)
1876 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001877 continue;
1878
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001879 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1880 gtphub_tunnel_str(tun));
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001881 deleted_count ++;
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001882
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001883 /* Send a Delete PDP Context Request to the
1884 * peer on the other side, remember the pending
1885 * delete and wait for the response to delete
1886 * the tunnel. Clear this side of the tunnel to
1887 * make sure it isn't used.
1888 *
1889 * Should the delete message send fail, or if no
1890 * response is received, this tunnel will expire. If
1891 * its TEIs come up in a new PDP Context Request, it
1892 * will be removed. If messages for this tunnel should
1893 * come in (from the not restarted side), they will be
1894 * dropped because the tunnel is rendered unusable. */
1895 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1896
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001897 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1898 NULL);
1899 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1900 NULL);
1901 }
1902 }
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001903
1904 if (deleted_count)
1905 LOG(LOGL_NOTICE, "Deleting %d tunnels due to restart of: %s\n",
1906 deleted_count,
1907 gtphub_port_str(pp));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001908}
1909
1910static int get_restart_count(struct gtp_packet_desc *p)
1911{
1912 int ie_idx;
1913 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1914 if (ie_idx < 0)
1915 return -1;
1916 return ntoh8(p->ie[ie_idx]->tv1.v);
1917}
1918
1919static void gtphub_check_restart_counter(struct gtphub *hub,
1920 struct gtp_packet_desc *p,
1921 struct gtphub_peer_port *from)
1922{
1923 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1924 * that doesn't match the peer's previously sent restart counter, clear
1925 * that peer and cancel PDP contexts. */
1926
1927 int restart = get_restart_count(p);
1928
1929 if ((restart < 0) || (restart > 255))
1930 return;
1931
1932 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1933 if (from->last_restart_count != restart) {
1934 gtphub_restarted(hub, p, from);
1935 }
1936 }
1937
1938 from->last_restart_count = restart;
1939}
1940
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001941static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1942{
1943 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1944 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001945 LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n",
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001946 gtphub_plane_idx_names[plane_idx]);
1947
Pau Espin Pedrolb2ebc592020-05-09 19:21:15 +02001948 if (!(what & OSMO_FD_READ))
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001949 return 0;
1950
1951 struct gtphub *hub = from_sgsns_ofd->data;
1952
1953 static uint8_t buf[4096];
Alexander Couzensdd930a22020-07-18 16:45:25 +02001954 struct sgsn_sockaddr from_addr;
1955 struct sgsn_sockaddr to_addr;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001956 struct osmo_fd *to_ofd;
1957 int len;
1958 uint8_t *reply_buf;
1959
1960 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1961 if (len < 1)
1962 return 0;
1963
1964 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1965 buf, len, gtphub_now(),
1966 &reply_buf, &to_ofd, &to_addr);
1967 if (len < 1)
1968 return 0;
1969
1970 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1971}
1972
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001973static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1974{
1975 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1976 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001977 LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001978 gtphub_plane_idx_names[plane_idx]);
Pau Espin Pedrolb2ebc592020-05-09 19:21:15 +02001979 if (!(what & OSMO_FD_READ))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001980 return 0;
1981
1982 struct gtphub *hub = from_ggsns_ofd->data;
1983
1984 static uint8_t buf[4096];
Alexander Couzensdd930a22020-07-18 16:45:25 +02001985 struct sgsn_sockaddr from_addr;
1986 struct sgsn_sockaddr to_addr;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001987 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001988 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001989 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990
1991 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1992 if (len < 1)
1993 return 0;
1994
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001995 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1996 buf, len, gtphub_now(),
1997 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001998 if (len < 1)
1999 return 0;
2000
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002001 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002002}
2003
2004static int gtphub_unmap(struct gtphub *hub,
2005 struct gtp_packet_desc *p,
2006 struct gtphub_peer_port *from,
2007 struct gtphub_peer_port *to_proxy,
2008 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002009 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002010{
2011 /* Always (try to) unmap sequence and TEI numbers, which need to be
2012 * replaced in the packet. Either way, give precedence to the proxy, if
2013 * configured. */
2014
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002015 if (unmapped_from_seq)
2016 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002017 if (final_unmapped)
2018 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002019 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002020
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002021 struct gtphub_peer_port *from_seq = NULL;
2022 struct gtphub_peer_port *from_tei = NULL;
2023 struct gtphub_peer_port *unmapped = NULL;
2024
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002025 from_seq = gtphub_unmap_seq(p, from);
2026
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002027 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002028 return -1;
2029
2030 struct gtphub_peer *from_peer = from->peer_addr->peer;
2031 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002032 LOG(LOGL_DEBUG,
2033 "Seq unmap and TEI unmap yield two different peers."
2034 " Using seq unmap."
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01002035 " (from %s %s: seq %d yields %s, tei %u yields %s)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002036 gtphub_plane_idx_names[p->plane_idx],
2037 gtphub_peer_str(from_peer),
2038 (int)p->seq,
2039 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002040 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002041 gtphub_port_str2(from_tei)
2042 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002043 }
2044 unmapped = (from_seq? from_seq : from_tei);
2045
2046 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002047 LOG(LOGL_NOTICE,
2048 "Unmap yields a different peer than the configured proxy."
2049 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002050 " unmapped: %s proxy: %s\n",
2051 gtphub_port_str(unmapped),
2052 gtphub_port_str2(to_proxy)
2053 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002054 }
2055 unmapped = (to_proxy? to_proxy : unmapped);
2056
2057 if (!unmapped) {
2058 /* Return no error, but returned pointers are all NULL. */
2059 return 0;
2060 }
2061
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002062 if (unmapped_from_seq)
2063 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002064 if (final_unmapped)
2065 *final_unmapped = unmapped;
2066 return 0;
2067}
2068
2069static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2070 uint16_t port,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002071 struct sgsn_sockaddr *dst)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002072{
Alexander Couzensdd930a22020-07-18 16:45:25 +02002073 return sgsn_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002074}
2075
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002076/* If p is an Echo request, replace p's data with the matching response and
2077 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2078 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002079static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2080 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002081{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002082 if (p->type != GTP_ECHO_REQ)
2083 return 0;
2084
2085 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002086 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002087 GTP_ECHO_RSP,
2088 0x00, 14 - 8, /* Length in network byte order */
2089 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2090 0, 0, /* Seq, to be replaced */
2091 0, 0, /* no extensions */
2092 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002093 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002094 };
2095 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2096 uint8_t *recovery = &echo_response_data[13];
2097
2098 *seq = hton16(p->seq);
2099 *recovery = hub->restart_counter;
2100
2101 *reply_buf = echo_response_data;
2102
2103 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002104}
2105
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002106struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002107 const struct sgsn_sockaddr *addr);
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002108
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002109/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2110 * address to forward to. Return a pointer to the osmo_fd, but copy the
2111 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2112 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2113 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002114int gtphub_handle_buf(struct gtphub *hub,
2115 unsigned int side_idx,
2116 unsigned int plane_idx,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002117 const struct sgsn_sockaddr *from_addr,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002118 uint8_t *buf,
2119 size_t received,
2120 time_t now,
2121 uint8_t **reply_buf,
2122 struct osmo_fd **to_ofd,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002123 struct sgsn_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002124{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002125 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2126 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002127
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002128 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2129 received);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002130
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002131 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002132 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002133
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002134 LOG(LOGL_DEBUG, "%s rx %s from %s %s%s\n",
2135 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
2136 gtphub_plane_idx_names[plane_idx],
2137 gtphub_side_idx_names[side_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002138 sgsn_sockaddr_to_str(from_addr),
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002139 gtp_type_str(p.type));
2140
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002141 if (p.rc <= 0) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002142 LOG(LOGL_ERROR, "INVALID: dropping GTP packet%s from %s %s %s\n",
2143 gtp_type_str(p.type),
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002144 gtphub_side_idx_names[side_idx],
2145 gtphub_plane_idx_names[plane_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002146 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002147 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002148 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002149
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002150 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2151
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002152 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002153 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002154 if (reply_len > 0) {
2155 /* It was an echo. Nothing left to do. */
Alexander Couzensdd930a22020-07-18 16:45:25 +02002156 sgsn_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002157 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002158
2159 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2160 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2161 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002162 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2163 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2164 gtphub_side_idx_names[side_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002165 (int)reply_len, sgsn_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002166 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002167 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002168 if (reply_len < 0)
2169 return -1;
2170
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002171 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002172
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002173 /* If a proxy is configured, check that it's indeed that proxy talking
2174 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2175 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002176 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2177 if (from_peer) {
Alexander Couzensdd930a22020-07-18 16:45:25 +02002178 if (sgsn_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002179 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002180 "Rejecting: %s proxy configured, but GTP packet"
2181 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002182 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002183 gtphub_side_idx_names[side_idx],
2184 gtphub_side_idx_names[side_idx],
2185 gtphub_port_str(from_peer),
Alexander Couzensdd930a22020-07-18 16:45:25 +02002186 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002187 return -1;
2188 }
2189 }
2190
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002191 if (!from_peer) {
2192 /* Find or create a peer with a matching address. The sender's
2193 * port may in fact differ. */
2194 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002195 }
2196
2197 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002198 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2199 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002200 if (!from_peer) {
2201 if (side_idx == GTPH_SIDE_GGSN) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002202 LOG(LOGL_ERROR, "Dropping packet%s: unknown GGSN peer: %s\n",
2203 gtp_type_str(p.type),
Alexander Couzensdd930a22020-07-18 16:45:25 +02002204 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002205 return -1;
2206 } else {
2207 /* SGSN */
2208 /* A new peer. If this is on the Ctrl plane, an SGSN
2209 * may make first contact without being known yet, so
2210 * create the peer struct for the current sender. */
2211 if (plane_idx != GTPH_PLANE_CTRL) {
2212 LOG(LOGL_ERROR,
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002213 "Dropping packet%s: User plane peer was not"
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002214 "announced by PDP Context: %s\n",
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002215 gtp_type_str(p.type),
Alexander Couzensdd930a22020-07-18 16:45:25 +02002216 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002217 return -1;
2218 }
2219
2220 struct gsn_addr from_gsna;
2221 uint16_t from_port;
2222 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2223 return -1;
2224
2225 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2226 }
2227 }
2228
2229 if (!from_peer) {
2230 /* This could theoretically happen for invalid address data or
2231 * somesuch. */
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002232 LOG(LOGL_ERROR, "Dropping packet%s: invalid %s peer: %s\n",
2233 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002234 gtphub_side_idx_names[side_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002235 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002236 return -1;
2237 }
2238
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002239 rate_ctr_add(&from_peer->counters_io->ctr[GTPH_CTR_BYTES_IN],
2240 received);
2241 rate_ctr_inc(&from_peer->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2242
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002243 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2244 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002245
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002246 gtphub_check_restart_counter(hub, &p, from_peer);
2247 gtphub_map_restart_counter(hub, &p);
2248
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002249 struct gtphub_peer_port *to_peer_from_seq;
2250 struct gtphub_peer_port *to_peer;
2251 if (gtphub_unmap(hub, &p, from_peer,
2252 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002253 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002254 != 0) {
2255 return -1;
2256 }
2257
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002258 if (p.tun) {
2259 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[p.side_idx][p.plane_idx];
2260 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_IN],
2261 received);
2262 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2263 }
2264
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002265 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2266 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2267 return -1;
2268 }
2269
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002270 if (!to_peer && p.tun && p.type == GTP_DELETE_PDP_RSP) {
2271 /* It's a delete confirmation for a tunnel that is partly
2272 * invalid, probably marked unsuable due to a restarted peer.
2273 * Remove the tunnel and be happy without forwarding. */
2274 expiring_item_del(&p.tun->expiry_entry);
2275 p.tun = NULL;
2276 return 0;
2277 }
2278
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002279 if (!to_peer) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002280 LOG(LOGL_ERROR, "No %s to send to. Dropping packet%s"
2281 " (type=%" PRIu8 ", header-TEI=%" PRIx32 ", seq=%" PRIx16 ").\n",
2282 gtphub_side_idx_names[other_side_idx(side_idx)],
2283 gtp_type_str(p.type),
2284 p.type, p.header_tei_rx, p.seq
2285 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002286 return -1;
2287 }
2288
2289 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002290 /* This may be a Create PDP Context response. If it is, there
2291 * are other addresses in the GTP message to set up apart from
2292 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002293 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002294 != 0)
2295 return -1;
2296 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002297
2298 /* Either to_peer was resolved from an existing tunnel,
2299 * or a PDP Ctx and thus a tunnel has just been created,
2300 * or the tunnel has been deleted due to this message. */
2301 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002302
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002303 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002304 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002305 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002306 if (!to_peer_from_seq)
2307 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002308
Alexander Couzensdd930a22020-07-18 16:45:25 +02002309 sgsn_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002310
2311 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002312
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002313 if (received) {
2314 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2315 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2316 received);
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002317
2318 rate_ctr_inc(&to_peer->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2319 rate_ctr_add(&to_peer->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2320 received);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002321 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002322
2323 if (p.tun) {
2324 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[other_side_idx(p.side_idx)][p.plane_idx];
2325 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2326 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2327 received);
2328 }
2329
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002330 LOG(LOGL_DEBUG, "%s Forward to %s:"
2331 " header-TEI %" PRIx32", seq %" PRIx16", %d bytes to %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002332 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2333 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002334 p.header_tei, p.seq,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002335 (int)received, sgsn_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002336 return received;
2337}
2338
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002339static void resolved_gssn_del_cb(struct expiring_item *expi)
2340{
2341 struct gtphub_resolved_ggsn *ggsn;
2342 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2343
2344 gtphub_port_ref_count_dec(ggsn->peer);
2345 llist_del(&ggsn->entry);
2346
2347 ggsn->expiry_entry.del_cb = 0;
2348 expiring_item_del(&ggsn->expiry_entry);
2349
2350 talloc_free(ggsn);
2351}
2352
2353void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2354 struct gsn_addr *resolved_addr,
2355 time_t now)
2356{
2357 struct gtphub_peer_port *pp;
2358 struct gtphub_resolved_ggsn *ggsn;
2359
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002360 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002361 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2362 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002363
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002364 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002365 resolved_addr, 2123);
2366 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002367 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2368 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002369 return;
2370 }
2371
2372 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2373 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002374 INIT_LLIST_HEAD(&ggsn->entry);
2375 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002376
2377 ggsn->peer = pp;
2378 gtphub_port_ref_count_inc(pp);
2379
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002380 osmo_strlcpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002381
2382 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002383 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002384
2385 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2386}
2387
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002388static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2389{
2390 return pp->ref_count == 0;
2391}
2392
2393static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2394{
2395 struct gtphub_peer_port *pp, *npp;
2396 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2397 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002398 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002399 gtphub_port_str(pp));
2400 gtphub_peer_port_del(pp);
2401 }
2402 }
2403 return llist_empty(&pa->ports);
2404}
2405
2406static int gtphub_gc_peer(struct gtphub_peer *p)
2407{
2408 struct gtphub_peer_addr *pa, *npa;
2409 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2410 if (gtphub_gc_peer_addr(pa)) {
2411 gtphub_peer_addr_del(pa);
2412 }
2413 }
2414
2415 /* Note that there's a ref_count in each gtphub_peer_port instance
2416 * listed within p->addresses, referenced by TEI mappings from
2417 * hub->tei_map. As long as those don't expire, this peer will stay. */
2418
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002419 return llist_empty(&p->addresses)
2420 && nr_map_empty(&p->seq_map);
2421}
2422
2423static void gtphub_gc_bind(struct gtphub_bind *b)
2424{
2425 struct gtphub_peer *p, *n;
2426 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2427 if (gtphub_gc_peer(p)) {
2428 gtphub_peer_del(p);
2429 }
2430 }
2431}
2432
2433void gtphub_gc(struct gtphub *hub, time_t now)
2434{
2435 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002436 expired = expiry_tick(&hub->expire_quickly, now);
2437 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002438
2439 /* ... */
2440
2441 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002442 int s, p;
2443 for_each_side_and_plane(s, p) {
2444 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002445 }
2446 }
2447}
2448
2449static void gtphub_gc_cb(void *data)
2450{
2451 struct gtphub *hub = data;
2452 gtphub_gc(hub, gtphub_now());
2453 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2454}
2455
2456static void gtphub_gc_start(struct gtphub *hub)
2457{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02002458 osmo_timer_setup(&hub->gc_timer, gtphub_gc_cb, hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002459 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2460}
2461
2462/* called by unit tests */
2463void gtphub_init(struct gtphub *hub)
2464{
2465 gtphub_zero(hub);
2466
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002467 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002468 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002469
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002470 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2471 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002472
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002473 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2474
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002475 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002476 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002477 for_each_side_and_plane(side_idx, plane_idx) {
Maxbb6b00a2017-12-20 12:14:50 +01002478 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx], CTR_IDX_HUB(side_idx, plane_idx));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002479 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002480
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002481 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2482 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2483 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2484 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002485}
2486
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002487/* For the test suite, this is kept separate from gtphub_stop(), which also
2488 * closes sockets. The test suite avoids using sockets and would cause
2489 * segfaults when trying to close uninitialized ofds. */
2490void gtphub_free(struct gtphub *hub)
2491{
2492 /* By expiring all mappings, a garbage collection should free
2493 * everything else. A gtphub_bind_free() will assert that everything is
2494 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002495 expiry_clear(&hub->expire_quickly);
2496 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002497
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002498 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002499 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002500 for_each_side_and_plane(side_idx, plane_idx) {
2501 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2502 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002503 }
2504}
2505
2506void gtphub_stop(struct gtphub *hub)
2507{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002508 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002509 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002510 for_each_side_and_plane(side_idx, plane_idx) {
2511 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002512 }
2513 gtphub_free(hub);
2514}
2515
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002516static int gtphub_make_proxy(struct gtphub *hub,
2517 struct gtphub_peer_port **pp,
2518 struct gtphub_bind *bind,
2519 const struct gtphub_cfg_addr *addr)
2520{
2521 if (!addr->addr_str)
2522 return 0;
2523
2524 struct gsn_addr gsna;
2525 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2526 return -1;
2527
2528 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2529
2530 /* This is *the* proxy. Make sure it is never expired. */
2531 gtphub_port_ref_count_inc(*pp);
2532 return 0;
2533}
2534
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002535int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2536 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002537{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002538 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002539
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002540 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002541 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002542
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002543 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002544 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002545 if (gtphub_ares_init(hub) != 0) {
2546 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2547 return -1;
2548 }
2549 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002550
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002551 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002552 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002553 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002554 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002555 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2556 &cfg->to_gsns[side_idx][plane_idx],
2557 (side_idx == GTPH_SIDE_SGSN)
2558 ? from_sgsns_read_cb
2559 : from_ggsns_read_cb,
2560 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002561 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002562 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2563 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002564 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002565 return rc;
2566 }
2567 }
2568
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002569 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002570 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002571 &hub->proxy[side_idx][plane_idx],
2572 &hub->to_gsns[side_idx][plane_idx],
2573 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002574 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002575 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002576 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002577 gtphub_side_idx_names[side_idx],
2578 cfg->proxy[side_idx][plane_idx].addr_str,
2579 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002580 return -1;
2581 }
2582 }
2583
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002584 for_each_side_and_plane(side_idx, plane_idx) {
2585 if (hub->proxy[side_idx][plane_idx])
2586 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2587 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002588 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002589 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002590 }
2591
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002592 if (hub->sgsn_use_sender)
Neels Hofmeyr9d8f5062015-12-03 14:52:33 +01002593 LOG(LOGL_NOTICE, "Using sender address and port for SGSN instead of GSN Addr IE and default ports.\n");
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002594
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002595 gtphub_gc_start(hub);
2596 return 0;
2597}
2598
2599static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2600 const struct gsn_addr *addr)
2601{
2602 struct gtphub_peer_addr *a;
2603 llist_for_each_entry(a, &peer->addresses, entry) {
2604 if (gsn_addr_same(&a->addr, addr))
2605 return a;
2606 }
2607 return NULL;
2608}
2609
2610static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2611 uint16_t port)
2612{
2613 OSMO_ASSERT(port);
2614 struct gtphub_peer_port *pp;
2615 llist_for_each_entry(pp, &a->ports, entry) {
2616 if (pp->port == port)
2617 return pp;
2618 }
2619 return NULL;
2620}
2621
2622static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2623 const struct gsn_addr *addr)
2624{
2625 struct gtphub_peer *peer;
2626 llist_for_each_entry(peer, &bind->peers, entry) {
2627 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2628 if (a)
2629 return a;
2630 }
2631 return NULL;
2632}
2633
2634static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2635 const struct gsn_addr *addr,
2636 uint16_t port)
2637{
2638 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2639 if (!a)
2640 return NULL;
2641 return gtphub_addr_find_port(a, port);
2642}
2643
2644struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002645 const struct sgsn_sockaddr *addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002646{
2647 struct gsn_addr gsna;
2648 uint16_t port;
Maxb643f562018-01-30 10:09:00 +01002649 if (gsn_addr_from_sockaddr(&gsna, &port, addr) != 0)
2650 return NULL;
2651
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002652 return gtphub_port_find(bind, &gsna, port);
2653}
2654
2655static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2656 struct gtphub_bind *bind)
2657{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002658 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2659 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002660 OSMO_ASSERT(peer);
2661
2662 INIT_LLIST_HEAD(&peer->addresses);
2663
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002664 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002665 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002666
2667 /* TODO use something random to pick the initial sequence nr.
2668 0x6d31 produces the ASCII character sequence 'm1', currently used in
2669 gtphub_nc_test.sh. */
2670 peer->seq_pool.last_nr = 0x6d31 - 1;
2671
2672 llist_add(&peer->entry, &bind->peers);
2673 return peer;
2674}
2675
2676static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2677 const struct gsn_addr *addr)
2678{
2679 struct gtphub_peer_addr *a;
2680 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2681 OSMO_ASSERT(a);
2682 a->peer = peer;
2683 gsn_addr_copy(&a->addr, addr);
2684 INIT_LLIST_HEAD(&a->ports);
2685 llist_add(&a->entry, &peer->addresses);
2686
2687 return a;
2688}
2689
2690static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2691 struct gtphub_bind *bind,
2692 const struct gsn_addr *addr)
2693{
2694 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2695 if (a)
2696 return a;
2697
2698 /* If we haven't found an address, that means we need to create an
2699 * entirely new peer for the new address. More addresses may be added
2700 * to this peer later, but not via this function. */
2701 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002702
2703 a = gtphub_peer_add_addr(peer, addr);
2704
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002705 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002706 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002707 gsn_addr_to_str(&a->addr));
2708
2709 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002710}
2711
2712static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2713 uint16_t port)
2714{
2715 struct gtphub_peer_port *pp;
2716
2717 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2718 OSMO_ASSERT(pp);
2719 pp->peer_addr = a;
2720 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002721 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002722
2723 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2724 talloc_free(pp);
2725 return NULL;
2726 }
2727
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002728 pp->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
Maxbb6b00a2017-12-20 12:14:50 +01002729 &gtphub_ctrg_io_desc, port);
Harald Welted939ac02017-07-17 23:59:39 +02002730 if (!pp->counters_io) {
Maxbb6b00a2017-12-20 12:14:50 +01002731 LOG(LOGL_ERROR, "Failed to allocate rate counters for %s:%u\n", gsn_addr_to_str(&a->addr), port);
Harald Welted939ac02017-07-17 23:59:39 +02002732 talloc_free(pp);
2733 return NULL;
2734 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002735
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002736 llist_add(&pp->entry, &a->ports);
2737
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002738 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002739 gsn_addr_to_str(&a->addr),
2740 (int)port);
2741
2742 return pp;
2743}
2744
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002745struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2746 struct gtphub_bind *bind,
2747 const struct gsn_addr *addr,
2748 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002749{
2750 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2751
2752 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2753 if (pp)
2754 return pp;
2755
2756 return gtphub_addr_add_port(a, port);
2757}
2758
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002759/* Find a GGSN peer with a matching address. If the address is known but the
2760 * port not, create a new port for that peer address. */
2761struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002762 const struct sgsn_sockaddr *addr)
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002763{
2764 struct gtphub_peer_addr *pa;
2765 struct gtphub_peer_port *pp;
2766
2767 struct gsn_addr gsna;
2768 uint16_t port;
Max99f99532017-12-20 17:02:15 +01002769 int rc = gsn_addr_from_sockaddr(&gsna, &port, addr);
2770 if (rc < 0)
2771 LOG(LOGL_ERROR, "%s(): failed to obtain GSN address\n", __func__);
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002772
2773 pa = gtphub_addr_find(bind, &gsna);
2774 if (!pa)
2775 return NULL;
2776
2777 pp = gtphub_addr_find_port(pa, port);
2778
2779 if (!pp)
2780 pp = gtphub_addr_add_port(pa, port);
2781
2782 return pp;
2783}
2784
2785
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002786/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2787 * resolution should be possible but failed, and 1 if resolution was
2788 * successful. *pp will be set to NULL if <1 is returned. */
2789static int gtphub_resolve_ggsn(struct gtphub *hub,
2790 struct gtp_packet_desc *p,
2791 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002792{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002793 *pp = NULL;
2794
2795 /* TODO determine from message type whether IEs should be present? */
2796
2797 int rc;
2798 const char *imsi_str;
2799 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2800 if (rc < 1)
2801 return rc;
2802 OSMO_ASSERT(imsi_str);
2803
2804 const char *apn_str;
2805 rc = get_ie_apn_str(p->ie, &apn_str);
2806 if (rc < 1)
2807 return rc;
2808 OSMO_ASSERT(apn_str);
2809
2810 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2811 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002812}
2813
2814
2815/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002816/* use this in osmo_sock_init() to remove dup. */
Alexander Couzensdd930a22020-07-18 16:45:25 +02002817/* Internal: call getaddrinfo for sgsn_sockaddr_init(). The caller is required
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002818 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002819static int _osmo_getaddrinfo(struct addrinfo **result,
2820 uint16_t family, uint16_t type, uint8_t proto,
2821 const char *host, uint16_t port)
2822{
2823 struct addrinfo hints;
2824 char portbuf[16];
2825
2826 sprintf(portbuf, "%u", port);
2827 memset(&hints, '\0', sizeof(struct addrinfo));
2828 hints.ai_family = family;
2829 if (type == SOCK_RAW) {
2830 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2831 * SOCK_RAW and IPPROTO_GRE is used.
2832 */
2833 hints.ai_socktype = SOCK_DGRAM;
2834 hints.ai_protocol = IPPROTO_UDP;
2835 } else {
2836 hints.ai_socktype = type;
2837 hints.ai_protocol = proto;
2838 }
2839
2840 return getaddrinfo(host, portbuf, &hints, result);
2841}
2842
2843/* TODO move to osmocom/core/socket.c ? */
Alexander Couzensdd930a22020-07-18 16:45:25 +02002844int sgsn_sockaddr_init(struct sgsn_sockaddr *addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002845 uint16_t family, uint16_t type, uint8_t proto,
2846 const char *host, uint16_t port)
2847{
2848 struct addrinfo *res;
2849 int rc;
2850 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2851
2852 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002853 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002854 return -EINVAL;
2855 }
2856
2857 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2858 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2859 addr->l = res->ai_addrlen;
2860 freeaddrinfo(res);
2861
2862 return 0;
2863}
2864
Alexander Couzensdd930a22020-07-18 16:45:25 +02002865int sgsn_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002866 char *port_str, size_t port_str_len,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002867 const struct sgsn_sockaddr *addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002868 int flags)
2869{
2870 int rc;
2871
2872 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2873 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2874 return -1;
2875 }
2876
2877 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002878 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2879 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002880 return -1;
2881 }
2882
2883 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2884 addr_str, addr_str_len,
2885 port_str, port_str_len,
2886 flags);
2887
2888 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002889 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2890 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2891 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002892
2893 return rc;
2894}
2895
Alexander Couzensdd930a22020-07-18 16:45:25 +02002896const char *sgsn_sockaddr_to_strb(const struct sgsn_sockaddr *addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002897 char *buf, size_t buf_len)
2898{
Alexander Couzens92ef0c82020-10-02 00:15:43 +02002899 char portbuf[6];
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002900 const int portbuf_len = 6;
2901 OSMO_ASSERT(buf_len > portbuf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002902 buf_len -= portbuf_len;
Alexander Couzensdd930a22020-07-18 16:45:25 +02002903 if (sgsn_sockaddr_to_strs(buf, buf_len,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002904 portbuf, portbuf_len,
2905 addr,
2906 NI_NUMERICHOST | NI_NUMERICSERV))
2907 return NULL;
2908
2909 char *pos = buf + strnlen(buf, buf_len-1);
2910 size_t len = buf_len - (pos - buf);
2911
2912 snprintf(pos, len, " port %s", portbuf);
2913 buf[buf_len-1] = '\0';
2914
2915 return buf;
2916}
2917
Alexander Couzensdd930a22020-07-18 16:45:25 +02002918const char *sgsn_sockaddr_to_str(const struct sgsn_sockaddr *addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002919{
2920 static char buf[256];
Alexander Couzensdd930a22020-07-18 16:45:25 +02002921 const char *result = sgsn_sockaddr_to_strb(addr, buf, sizeof(buf));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002922 if (! result)
2923 return "(invalid)";
2924 return result;
2925}
2926
Alexander Couzensdd930a22020-07-18 16:45:25 +02002927int sgsn_sockaddr_cmp(const struct sgsn_sockaddr *a,
2928 const struct sgsn_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002929{
2930 if (a == b)
2931 return 0;
2932 if (!a)
2933 return -1;
2934 if (!b)
2935 return 1;
2936 if (a->l != b->l) {
2937 /* Lengths are not the same, but determine the order. Will
Alexander Couzensdd930a22020-07-18 16:45:25 +02002938 * anyone ever sort a list by sgsn_sockaddr though...? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002939 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2940 if (cmp == 0) {
2941 if (a->l < b->l)
2942 return -1;
2943 else
2944 return 1;
2945 }
2946 return cmp;
2947 }
2948 return memcmp(&a->a, &b->a, a->l);
2949}
2950
Alexander Couzensdd930a22020-07-18 16:45:25 +02002951void sgsn_sockaddr_copy(struct sgsn_sockaddr *dst,
2952 const struct sgsn_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002953{
2954 OSMO_ASSERT(src->l <= sizeof(dst->a));
2955 memcpy(&dst->a, &src->a, src->l);
2956 dst->l = src->l;
2957}