blob: 89ca676564fc2bb6fc78c5bab3bd0820e691d16e [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
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020062/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
63
64enum gtp_rc {
65 GTP_RC_UNKNOWN = 0,
66 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010067 GTP_RC_PDU_C = 2, /* a real packet with IEs */
68 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069
70 GTP_RC_TOOSHORT = -1,
71 GTP_RC_UNSUPPORTED_VERSION = -2,
72 GTP_RC_INVALID_IE = -3,
73};
74
75struct gtp_packet_desc {
76 union gtp_packet *data;
77 int data_len;
78 int header_len;
79 int version;
80 uint8_t type;
81 uint16_t seq;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010082 uint32_t header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020083 uint32_t header_tei;
84 int rc; /* enum gtp_rc */
85 unsigned int plane_idx;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010086 unsigned int side_idx;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +010087 struct gtphub_tunnel *tun;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010088 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020089 union gtpie_member *ie[GTPIE_SIZE];
90};
91
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010092struct pending_delete {
93 struct llist_head entry;
94 struct expiring_item expiry_entry;
95
96 struct gtphub_tunnel *tun;
97 uint8_t teardown_ind;
98 uint8_t nsapi;
99};
100
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100101
102/* counters */
103
104enum gtphub_counters_io {
105 GTPH_CTR_PKTS_IN = 0,
106 GTPH_CTR_PKTS_OUT,
107 GTPH_CTR_BYTES_IN,
108 GTPH_CTR_BYTES_OUT
109};
110
111static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
112 { "packets.in", "Packets ( In)" },
113 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100114 { "bytes.in", "Bytes ( In)" },
115 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100116};
117
118static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
Max8a01a802017-12-20 13:10:11 +0100119 .group_name_prefix = "gtphub:bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100120 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100121 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
122 .ctr_desc = gtphub_counters_io_desc,
123 .class_id = OSMO_STATS_CLASS_GLOBAL,
124};
125
126
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100127/* support */
128
129static const char *gtp_type_str(uint8_t type)
130{
131 switch (type) {
132 case 1:
133 return " (Echo Request)";
134 case 2:
135 return " (Echo Response)";
136 case 16:
137 return " (Create PDP Ctx Request)";
138 case 17:
139 return " (Create PDP Ctx Response)";
140 case 18:
141 return " (Update PDP Ctx Request)";
142 case 19:
143 return " (Update PDP Ctx Response)";
144 case 20:
145 return " (Delete PDP Ctx Request)";
146 case 21:
147 return " (Delete PDP Ctx Response)";
148 case 255:
149 return " (User Data)";
150 default:
151 return "";
152 }
153}
154
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200155void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
156{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100157 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200158}
159
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200160int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
Alexander Couzensdd930a22020-07-18 16:45:25 +0200161 const struct sgsn_sockaddr *sa)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200162{
163 char addr_str[256];
164 char port_str[6];
165
Alexander Couzensdd930a22020-07-18 16:45:25 +0200166 if (sgsn_sockaddr_to_strs(addr_str, sizeof(addr_str),
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200167 port_str, sizeof(port_str),
168 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
169 != 0) {
170 return -1;
171 }
172
173 if (port)
174 *port = atoi(port_str);
175
176 return gsn_addr_from_str(gsna, addr_str);
177}
178
179int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
180{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100181 if ((!gsna) || (!numeric_addr_str))
182 return -1;
183
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200184 int af = AF_INET;
185 gsna->len = 4;
186 const char *pos = numeric_addr_str;
187 for (; *pos; pos++) {
188 if (*pos == ':') {
189 af = AF_INET6;
190 gsna->len = 16;
191 break;
192 }
193 }
194
195 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
196 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100197 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
198 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200199 return -1;
200 }
201 return 0;
202}
203
204const char *gsn_addr_to_str(const struct gsn_addr *gsna)
205{
206 static char buf[INET6_ADDRSTRLEN + 1];
207 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
208}
209
210const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
211 char *strbuf,
212 int strbuf_len)
213{
214 int af;
215 switch (gsna->len) {
216 case 4:
217 af = AF_INET;
218 break;
219 case 16:
220 af = AF_INET6;
221 break;
222 default:
223 return NULL;
224 }
225
226 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
227 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100228 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
229 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100230 strerror(errno),
231 (int)gsna->len,
232 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200233 }
234 return r;
235}
236
237int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
238{
239 if (a == b)
240 return 1;
241 if ((!a) || (!b))
242 return 0;
243 if (a->len != b->len)
244 return 0;
245 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
246}
247
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100248static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
249 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200250{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100251 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200252 return -1;
253
254 unsigned int len;
255 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
256 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
257 &len, gsna->buf, sizeof(gsna->buf))
258 != 0)
259 return -1;
260 gsna->len = len;
261 return 0;
262}
263
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100264static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
265 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200266{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100267 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200268 return -1;
269
270 int ie_idx;
271 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
272
273 if (ie_idx < 0)
274 return -1;
275
276 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
277 int ie_l = ntoh16(ie->l);
278 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100279 LOG(LOGL_ERROR, "Not implemented:"
280 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200281 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
282 return -1;
283 }
284
285 memcpy(ie->v, gsna->buf, (int)ie_l);
286 return 0;
287}
288
289/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
290 */
291void validate_gtp0_header(struct gtp_packet_desc *p)
292{
293 const struct gtp0_header *pheader = &(p->data->gtp0.h);
294 p->rc = GTP_RC_UNKNOWN;
295 p->header_len = 0;
296
297 OSMO_ASSERT(p->data_len >= 1);
298 OSMO_ASSERT(p->version == 0);
299
300 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100301 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200302 p->rc = GTP_RC_TOOSHORT;
303 return;
304 }
305
306 p->type = ntoh8(pheader->type);
307 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100308 p->header_tei_rx = 0; /* TODO */
309 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200310
311 if (p->data_len == GTP0_HEADER_SIZE) {
312 p->rc = GTP_RC_TINY;
313 p->header_len = GTP0_HEADER_SIZE;
314 return;
315 }
316
317 /* Check packet length field versus length of packet */
318 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100319 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
320 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100321 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
322 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200323 p->rc = GTP_RC_TOOSHORT;
324 return;
325 }
326
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100327 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200328 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100329 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200330}
331
332/* Validate GTP version 1 data, and update p->rc with the result, as well as
333 * p->header_len in case of a valid header. */
334void validate_gtp1_header(struct gtp_packet_desc *p)
335{
336 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
337 p->rc = GTP_RC_UNKNOWN;
338 p->header_len = 0;
339
340 OSMO_ASSERT(p->data_len >= 1);
341 OSMO_ASSERT(p->version == 1);
342
343 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
344 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100345 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200346 p->rc = GTP_RC_TOOSHORT;
347 return;
348 }
349
350 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100351 p->header_tei_rx = ntoh32(pheader->tei);
352 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200353 p->seq = ntoh16(pheader->seq);
354
Neels Hofmeyr28a70f22015-12-06 15:22:54 +0100355 LOG(LOGL_DEBUG, "| GTPv1\n");
356 LOG(LOGL_DEBUG, "| type = %" PRIu8 " 0x%02" PRIx8 "\n", p->type, p->type);
357 LOG(LOGL_DEBUG, "| length = %" PRIu16 " 0x%04" PRIx16 "\n", ntoh16(pheader->length), ntoh16(pheader->length));
358 LOG(LOGL_DEBUG, "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", p->header_tei_rx, p->header_tei_rx);
359 LOG(LOGL_DEBUG, "| seq = %" PRIu16 " 0x%04" PRIx16 "\n", p->seq, p->seq);
360 LOG(LOGL_DEBUG, "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->npdu, pheader->npdu);
361 LOG(LOGL_DEBUG, "| next = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->next, pheader->next);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200362
363 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
364 p->rc = GTP_RC_TINY;
365 p->header_len = GTP1_HEADER_SIZE_SHORT;
366 return;
367 }
368
369 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100370 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
371 if (p->data_len != announced_len) {
372 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
373 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100374 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
375 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200376 p->rc = GTP_RC_TOOSHORT;
377 return;
378 }
379
Neels Hofmeyre921e322015-11-11 00:45:50 +0100380 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200381 p->header_len = GTP1_HEADER_SIZE_LONG;
382}
383
384/* Examine whether p->data of size p->data_len has a valid GTP header. Set
385 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
386 * gtp_rc). p->data must point at a buffer with p->data_len set. */
387void validate_gtp_header(struct gtp_packet_desc *p)
388{
389 p->rc = GTP_RC_UNKNOWN;
390
391 /* Need at least 1 byte in order to check version */
392 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100393 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
394 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200395 p->rc = GTP_RC_TOOSHORT;
396 return;
397 }
398
399 p->version = p->data->flags >> 5;
400
401 switch (p->version) {
402 case 0:
403 validate_gtp0_header(p);
404 break;
405 case 1:
406 validate_gtp1_header(p);
407 break;
408 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100409 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200410 p->rc = GTP_RC_UNSUPPORTED_VERSION;
411 break;
412 }
413}
414
415
416/* Return the value of the i'th IMSI IEI by copying to *imsi.
417 * The first IEI is reached by passing i = 0.
418 * imsi must point at allocated space of (at least) 8 bytes.
419 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100420static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200421{
422 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
423}
424
425/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100426static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200427{
428 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
429}
430
431static char imsi_digit_to_char(uint8_t nibble)
432{
433 nibble &= 0x0f;
434 if (nibble > 9)
435 return (nibble == 0x0f) ? '\0' : '?';
436 return '0' + nibble;
437}
438
439/* Return a human readable IMSI string, in a static buffer.
440 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100441static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200442{
443 static char str[17];
444 int i;
445
446 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100447 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100448 c = imsi_digit_to_char(imsi[i]);
449 if (c == '?')
450 return -1;
451 str[2*i] = c;
452
453 c = imsi_digit_to_char(imsi[i] >> 4);
454 if (c == '?')
455 return -1;
456 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200457 }
458 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100459 *imsi_str = str;
460 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200461}
462
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100463/* Return 0 if not present, 1 if present and decoded successfully, -1 if
464 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100465static int get_ie_imsi_str(union gtpie_member *ie[], int i,
466 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100467{
468 uint8_t imsi_buf[8];
469 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100470 return 0;
471 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100472}
473
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100474/* Return 0 if not present, 1 if present and decoded successfully, -1 if
475 * present but cannot be decoded. */
476static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100477{
478 static char apn_buf[GSM_APN_LENGTH];
479 unsigned int len;
480 if (gtpie_gettlv(ie, GTPIE_APN, 0,
481 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100482 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100483
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100484 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100485 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
486 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100487 return -1;
488 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100489
490 if (len > (sizeof(apn_buf) - 1))
491 len = sizeof(apn_buf) - 1;
492 apn_buf[len] = '\0';
493
Harald Welte7e82b742017-08-12 13:43:54 +0200494 *apn_str = osmo_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100495 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100496 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
497 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100498 return -1;
499 }
500 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100501}
502
503
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200504/* Validate header, and index information elements. Write decoded packet
505 * information to *res. res->data will point at the given data buffer. On
506 * error, p->rc is set <= 0 (see enum gtp_rc). */
507static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100508 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200509 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100510 struct gtp_packet_desc *res,
511 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200512{
513 ZERO_STRUCT(res);
514 res->data = (union gtp_packet*)data;
515 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100516 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200517 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100518 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200519
520 validate_gtp_header(res);
521
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100522 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200523 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200524
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100525 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200526
Neels Hofmeyre921e322015-11-11 00:45:50 +0100527 if (from_plane_idx == GTPH_PLANE_USER) {
528 res->rc = GTP_RC_PDU_U;
529 return;
530 }
531
532 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100533 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200534 return;
535 }
536
537 if (gtpie_decaps(res->ie, res->version,
538 (void*)(data + res->header_len),
539 res->data_len - res->header_len) != 0) {
540 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100541 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100542 " Dropping GTP packet%s.\n",
543 gtp_type_str(res->type)
544 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200545 return;
546 }
547
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100548#if 1
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100549 /* TODO if (<loglevel is debug>)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100550 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200551 int i;
552
553 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100554 const char *imsi;
555 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200556 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100557 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200558 }
559
560 for (i = 0; i < 10; i++) {
561 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100562 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200563 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100564 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200565 }
566
567 for (i = 0; i < 2; i++) {
568 struct gsn_addr addr;
569 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100570 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200571 }
572
573 for (i = 0; i < 10; i++) {
574 uint32_t tei;
575 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
576 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100577 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200578 tei, tei);
579 }
580
581 for (i = 0; i < 10; i++) {
582 uint32_t tei;
583 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
584 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100585 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200586 tei, tei);
587 }
588#endif
589}
590
591
592/* expiry */
593
594void expiry_init(struct expiry *exq, int expiry_in_seconds)
595{
596 ZERO_STRUCT(exq);
597 exq->expiry_in_seconds = expiry_in_seconds;
598 INIT_LLIST_HEAD(&exq->items);
599}
600
601void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
602{
603 item->expiry = now + exq->expiry_in_seconds;
604
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100605 OSMO_ASSERT(llist_empty(&exq->items)
606 || (item->expiry
Oliver Smith3aba7ad2021-07-12 11:34:51 +0200607 >= llist_last_entry(&exq->items, struct expiring_item, entry)->expiry));
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100608
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200609 /* Add/move to the tail to always sort by expiry, ascending. */
610 llist_del(&item->entry);
611 llist_add_tail(&item->entry, &exq->items);
612}
613
614int expiry_tick(struct expiry *exq, time_t now)
615{
616 int expired = 0;
617 struct expiring_item *m, *n;
618 llist_for_each_entry_safe(m, n, &exq->items, entry) {
619 if (m->expiry <= now) {
620 expiring_item_del(m);
621 expired ++;
622 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200623 /* The items are added sorted by expiry. So when we hit
624 * an unexpired entry, only more unexpired ones will
625 * follow. */
626 break;
627 }
628 }
629 return expired;
630}
631
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100632void expiry_clear(struct expiry *exq)
633{
634 struct expiring_item *m, *n;
635 llist_for_each_entry_safe(m, n, &exq->items, entry) {
636 expiring_item_del(m);
637 }
638}
639
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200640void expiring_item_init(struct expiring_item *item)
641{
642 ZERO_STRUCT(item);
643 INIT_LLIST_HEAD(&item->entry);
644}
645
646void expiring_item_del(struct expiring_item *item)
647{
648 OSMO_ASSERT(item);
649 llist_del(&item->entry);
650 INIT_LLIST_HEAD(&item->entry);
651 if (item->del_cb) {
652 /* avoid loops */
653 del_cb_t del_cb = item->del_cb;
654 item->del_cb = 0;
655 (del_cb)(item);
656 }
657}
658
659
660/* nr_map, nr_pool */
661
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100662void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200663{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100664 *pool = (struct nr_pool){
665 .nr_min = nr_min,
666 .nr_max = nr_max,
667 .last_nr = nr_max
668 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200669}
670
671nr_t nr_pool_next(struct nr_pool *pool)
672{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100673 if (pool->last_nr >= pool->nr_max)
674 pool->last_nr = pool->nr_min;
675 else
676 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200677
678 return pool->last_nr;
679}
680
681void nr_map_init(struct nr_map *map, struct nr_pool *pool,
682 struct expiry *exq)
683{
684 ZERO_STRUCT(map);
685 map->pool = pool;
686 map->add_items_to_expiry = exq;
687 INIT_LLIST_HEAD(&map->mappings);
688}
689
690void nr_mapping_init(struct nr_mapping *m)
691{
692 ZERO_STRUCT(m);
693 INIT_LLIST_HEAD(&m->entry);
694 expiring_item_init(&m->expiry_entry);
695}
696
697void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
698{
699 /* Generate a mapped number */
700 mapping->repl = nr_pool_next(map->pool);
701
702 /* Add to the tail to always yield a list sorted by expiry, in
703 * ascending order. */
704 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100705 nr_map_refresh(map, mapping, now);
706}
707
708void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
709{
710 if (!map->add_items_to_expiry)
711 return;
712 expiry_add(map->add_items_to_expiry,
713 &mapping->expiry_entry,
714 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200715}
716
717void nr_map_clear(struct nr_map *map)
718{
719 struct nr_mapping *m;
720 struct nr_mapping *n;
721 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
722 nr_mapping_del(m);
723 }
724}
725
726int nr_map_empty(const struct nr_map *map)
727{
728 return llist_empty(&map->mappings);
729}
730
731struct nr_mapping *nr_map_get(const struct nr_map *map,
732 void *origin, nr_t nr_orig)
733{
734 struct nr_mapping *mapping;
735 llist_for_each_entry(mapping, &map->mappings, entry) {
736 if ((mapping->origin == origin)
737 && (mapping->orig == nr_orig))
738 return mapping;
739 }
740 /* Not found. */
741 return NULL;
742}
743
744struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
745{
746 struct nr_mapping *mapping;
747 llist_for_each_entry(mapping, &map->mappings, entry) {
748 if (mapping->repl == nr_repl) {
749 return mapping;
750 }
751 }
752 /* Not found. */
753 return NULL;
754}
755
756void nr_mapping_del(struct nr_mapping *mapping)
757{
758 OSMO_ASSERT(mapping);
759 llist_del(&mapping->entry);
760 INIT_LLIST_HEAD(&mapping->entry);
761 expiring_item_del(&mapping->expiry_entry);
762}
763
764
765/* gtphub */
766
767const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
768 "CTRL",
769 "USER",
770};
771
772const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
773 2123,
774 2152,
775};
776
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100777const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
778 "SGSN",
779 "GGSN",
780};
781
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200782time_t gtphub_now(void)
783{
784 struct timespec now_tp;
Pau Espin Pedrol36abead2018-08-17 13:27:20 +0200785 OSMO_ASSERT(osmo_clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200786 return now_tp.tv_sec;
787}
788
789/* Remove a gtphub_peer from its list and free it. */
790static void gtphub_peer_del(struct gtphub_peer *peer)
791{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100792 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200793 nr_map_clear(&peer->seq_map);
794 llist_del(&peer->entry);
795 talloc_free(peer);
796}
797
798static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
799{
800 OSMO_ASSERT(llist_empty(&pa->ports));
801 llist_del(&pa->entry);
802 talloc_free(pa);
803}
804
805static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
806{
807 OSMO_ASSERT(pp->ref_count == 0);
808 llist_del(&pp->entry);
Neels Hofmeyre38fb662015-12-06 16:44:14 +0100809 rate_ctr_group_free(pp->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200810 talloc_free(pp);
811}
812
813/* From the information in the gtp_packet_desc, return the address of a GGSN.
814 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100815static int gtphub_resolve_ggsn(struct gtphub *hub,
816 struct gtp_packet_desc *p,
817 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200818
819/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100820struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
821 const char *imsi_str,
822 const char *apn_ni_str);
823int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200824
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200825static void gtphub_zero(struct gtphub *hub)
826{
827 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100828 INIT_LLIST_HEAD(&hub->ggsn_lookups);
829 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200830}
831
832static int gtphub_sock_init(struct osmo_fd *ofd,
833 const struct gtphub_cfg_addr *addr,
834 osmo_fd_cb_t cb,
835 void *data,
836 int ofd_id)
837{
838 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100839 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200840 return -1;
841 }
842 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100843 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200844 return -1;
845 }
846
Harald Weltebe2330f2020-10-19 12:32:20 +0200847 osmo_fd_setup(ofd, -1, OSMO_FD_READ, cb, data, ofd_id);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200848
849 int rc;
850 rc = osmo_sock_init_ofd(ofd,
851 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
852 addr->addr_str, addr->port,
853 OSMO_SOCK_F_BIND);
854 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100855 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
856 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200857 return -1;
858 }
859
860 return 0;
861}
862
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100863static void gtphub_sock_close(struct osmo_fd *ofd)
864{
865 close(ofd->fd);
866 osmo_fd_unregister(ofd);
867 ofd->cb = NULL;
868}
869
Maxbb6b00a2017-12-20 12:14:50 +0100870static void gtphub_bind_init(struct gtphub_bind *b, uint32_t idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200871{
872 ZERO_STRUCT(b);
873
874 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100875
876 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
Maxbb6b00a2017-12-20 12:14:50 +0100877 &gtphub_ctrg_io_desc, idx);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100878 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200879}
880
881static int gtphub_bind_start(struct gtphub_bind *b,
882 const struct gtphub_cfg_bind *cfg,
883 osmo_fd_cb_t cb, void *cb_data,
884 unsigned int ofd_id)
885{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100886 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
887 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
888 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
889 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200890 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100891 }
892 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
893 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
894 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200895 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100896 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100897 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200898 return 0;
899}
900
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100901static void gtphub_bind_free(struct gtphub_bind *b)
902{
903 OSMO_ASSERT(llist_empty(&b->peers));
Maxbb6b00a2017-12-20 12:14:50 +0100904 if (b->counters_io)
905 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100906}
907
908static void gtphub_bind_stop(struct gtphub_bind *b) {
909 gtphub_sock_close(&b->ofd);
910 gtphub_bind_free(b);
911}
912
Neels Hofmeyr40348972015-11-17 12:22:28 +0100913/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200914 * Return the number of bytes read, zero on error. */
915static int gtphub_read(const struct osmo_fd *from,
Alexander Couzensdd930a22020-07-18 16:45:25 +0200916 struct sgsn_sockaddr *from_addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200917 uint8_t *buf, size_t buf_len)
918{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100919 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200920
Neels Hofmeyr40348972015-11-17 12:22:28 +0100921 /* recvfrom requires the available length set in *from_addr_len. */
922 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923 errno = 0;
924 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100925 (struct sockaddr*)&from_addr->a,
926 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200927 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
928 * is not truncated. Then maybe reduce buf's size. */
929
930 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100931 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
932 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200933 return 0;
934 }
935
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100936 LOG(LOGL_DEBUG, "Received %d bytes from %s: %s%s\n",
Alexander Couzensdd930a22020-07-18 16:45:25 +0200937 (int)received, sgsn_sockaddr_to_str(from_addr),
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100938 osmo_hexdump(buf, received > 1000? 1000 : received),
939 received > 1000 ? "..." : "");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200940
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200941 return received;
942}
943
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100944static inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200945{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100946 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200947 OSMO_ASSERT(pp->ref_count < UINT_MAX);
948 pp->ref_count++;
949}
950
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100951static inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200952{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100953 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200954 OSMO_ASSERT(pp->ref_count > 0);
955 pp->ref_count--;
956}
957
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100958static inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200959{
960 OSMO_ASSERT(p->version == 1);
961 p->data->gtp1l.h.seq = hton16(seq);
962 p->seq = seq;
963}
964
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100965static inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200966{
967 OSMO_ASSERT(p->version == 1);
968 p->data->gtp1l.h.tei = hton32(tei);
969 p->header_tei = tei;
970}
971
972static void gtphub_mapping_del_cb(struct expiring_item *expi);
973
Pau Espin Pedrolf37aedb2023-01-05 14:20:27 +0100974static struct nr_mapping *gtphub_mapping_new(void)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200975{
976 struct nr_mapping *nrm;
977 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
978 OSMO_ASSERT(nrm);
979
980 nr_mapping_init(nrm);
981 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
982 return nrm;
983}
984
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100985
986#define APPEND(args...) \
987 l = snprintf(pos, left, args); \
988 pos += l; \
989 left -= l
990
991static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
992 int side_idx)
993{
994 static char buf[256];
995 char *pos = buf;
996 int left = sizeof(buf);
997 int l;
Pau Espin Pedrolf37aedb2023-01-05 14:20:27 +0100998
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100999 struct gtphub_tunnel_endpoint *c, *u;
1000 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1001 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
1002
1003 /* print both only if they differ. */
1004 if (!c->peer) {
1005 APPEND("(uninitialized)");
1006 } else {
1007 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
1008 }
1009
1010 if (!u->peer) {
1011 if (c->peer) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001012 APPEND("/(uninitialized)");
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001013 }
1014 } else if ((!c->peer)
1015 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1016 &c->peer->peer_addr->addr))) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001017 APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr));
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001018 }
1019
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001020 APPEND(" (TEI C=%x U=%x)",
1021 c->tei_orig,
1022 u->tei_orig);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001023 return buf;
1024}
1025
1026const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1027{
1028 static char buf[512];
1029 char *pos = buf;
1030 int left = sizeof(buf);
1031 int l;
1032
Neels Hofmeyrea619f12016-12-16 13:09:00 +01001033 if (!tun)
1034 return "null-tunnel";
1035
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001036 APPEND("TEI=%x: ", tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001037 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1038 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1039
1040 return buf;
1041}
1042
1043#undef APPEND
1044
1045void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1046 struct gtphub_peer_port *pp)
1047{
1048 if (te->peer)
1049 gtphub_port_ref_count_dec(te->peer);
1050 te->peer = pp;
1051 if (te->peer)
1052 gtphub_port_ref_count_inc(te->peer);
1053}
1054
1055int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1056{
1057 if (!tun)
1058 return 0;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001059 if (!tun->tei_repl)
1060 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001061 int side_idx;
1062 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001063 for_each_side_and_plane(side_idx, plane_idx) {
1064 struct gtphub_tunnel_endpoint *te =
1065 &tun->endpoint[side_idx][plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001066 if (!(te->peer && te->tei_orig))
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001067 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001068 }
1069 return 1;
1070}
1071
1072static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1073{
1074 struct gtphub_tunnel *tun = container_of(expi,
1075 struct gtphub_tunnel,
1076 expiry_entry);
1077 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1078
1079 llist_del(&tun->entry);
1080 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1081
1082 expi->del_cb = 0; /* avoid recursion loops */
1083 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1084
1085 int side_idx;
1086 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001087 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001088 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1089
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001090 /* clear ref count */
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001091 gtphub_tunnel_endpoint_set_peer(te, NULL);
1092
1093 rate_ctr_group_free(te->counters_io);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001094 }
1095
1096 talloc_free(tun);
1097}
1098
Maxbb6b00a2017-12-20 12:14:50 +01001099#define CTR_IDX(s, p, a, b) (a + s + (p + b) * 2)
1100
1101/* rate counter index for tunnels: [3; 6] */
1102#define CTR_IDX_TUN(s, p) CTR_IDX(s, p, 1, 1)
1103
1104/* rate counter index for hubs: [7; 10] */
1105#define CTR_IDX_HUB(s, p) CTR_IDX(s, p, 3, 2)
1106
Pau Espin Pedrolf37aedb2023-01-05 14:20:27 +01001107static struct gtphub_tunnel *gtphub_tunnel_new(void)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001108{
1109 struct gtphub_tunnel *tun;
1110 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1111 OSMO_ASSERT(tun);
1112
1113 INIT_LLIST_HEAD(&tun->entry);
1114 expiring_item_init(&tun->expiry_entry);
1115
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001116 int side_idx, plane_idx;
1117 for_each_side_and_plane(side_idx, plane_idx) {
1118 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1119 te->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
1120 &gtphub_ctrg_io_desc,
Maxbb6b00a2017-12-20 12:14:50 +01001121 CTR_IDX_TUN(side_idx, plane_idx));
1122 if (!te->counters_io)
1123 return NULL;
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001124 }
1125
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001126 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1127 return tun;
1128}
1129
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001130static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1131 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001132{
1133 if (llist_empty(&peer->addresses))
1134 return "(addressless)";
1135
Oliver Smith3aba7ad2021-07-12 11:34:51 +02001136 struct gtphub_peer_addr *a = llist_first_entry_or_null(&peer->addresses, struct gtphub_peer_addr, entry);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001137 return gsn_addr_to_strb(&a->addr, buf, buflen);
1138}
1139
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001140static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1141 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001142{
1143 if (!port)
1144 return "(null port)";
1145
1146 snprintf(buf, buflen, "%s port %d",
1147 gsn_addr_to_str(&port->peer_addr->addr),
1148 (int)port->port);
1149 return buf;
1150}
1151
1152const char *gtphub_peer_str(struct gtphub_peer *peer)
1153{
1154 static char buf[256];
1155 return gtphub_peer_strb(peer, buf, sizeof(buf));
1156}
1157
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001158const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001159{
1160 static char buf[256];
1161 return gtphub_port_strb(port, buf, sizeof(buf));
1162}
1163
1164static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1165{
1166 static char buf[256];
1167 return gtphub_port_strb(port, buf, sizeof(buf));
1168}
1169
1170static void gtphub_mapping_del_cb(struct expiring_item *expi)
1171{
1172 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001173 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001174
1175 struct nr_mapping *nrm = container_of(expi,
1176 struct nr_mapping,
1177 expiry_entry);
1178 llist_del(&nrm->entry);
1179 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1180
1181 /* Just for log */
1182 struct gtphub_peer_port *from = nrm->origin;
1183 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001184 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001185 (int)nrm->expiry_entry.expiry,
1186 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001187 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001188
1189 gtphub_port_ref_count_dec(from);
1190
1191 talloc_free(nrm);
1192}
1193
1194static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1195 struct gtphub_peer_port *from,
1196 nr_t orig_nr,
1197 time_t now)
1198{
1199 struct nr_mapping *nrm;
1200
1201 nrm = nr_map_get(map, from, orig_nr);
1202
1203 if (!nrm) {
1204 nrm = gtphub_mapping_new();
1205 nrm->orig = orig_nr;
1206 nrm->origin = from;
1207 nr_map_add(map, nrm, now);
1208 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001209 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001210 gtphub_port_str(from),
1211 (int)(nrm->orig), (int)(nrm->repl));
1212 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001213 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001214 }
1215
1216 OSMO_ASSERT(nrm);
1217 return nrm;
1218}
1219
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001220static void gtphub_map_seq(struct gtp_packet_desc *p,
1221 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001222 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001223{
1224 /* Store a mapping in to_peer's map, so when we later receive a GTP
1225 * packet back from to_peer, the seq nr can be unmapped back to its
1226 * origin (from_peer here). */
1227 struct nr_mapping *nrm;
1228 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001229 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001230
1231 /* Change the GTP packet to yield the new, mapped seq nr */
1232 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001233}
1234
1235static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1236 struct gtphub_peer_port *responding_port)
1237{
1238 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001239 struct nr_mapping *nrm =
1240 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1241 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001242 if (!nrm)
1243 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001244 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1245 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001246 set_seq(p, nrm->orig);
1247 return nrm->origin;
1248}
1249
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001250static int gtphub_check_mapped_tei(struct gtphub_tunnel *new_tun,
1251 struct gtphub_tunnel *iterated_tun,
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001252 uint32_t *tei_min,
1253 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001254{
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001255 if (!new_tun->tei_repl || !iterated_tun->tei_repl)
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001256 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001257
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001258 *tei_min = (*tei_min < iterated_tun->tei_repl)? *tei_min : iterated_tun->tei_repl;
1259 *tei_max = (*tei_max > iterated_tun->tei_repl)? *tei_max : iterated_tun->tei_repl;
1260
1261 if (new_tun->tei_repl != iterated_tun->tei_repl)
1262 return 1;
1263
1264 /* new_tun->tei_repl is already taken. Try to find one out of the known
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001265 * range. */
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001266 LOG(LOGL_DEBUG, "TEI replacement %d already taken.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001267
1268 if ((*tei_max) < 0xffffffff) {
1269 (*tei_max)++;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001270 new_tun->tei_repl = *tei_max;
1271 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001272 return 1;
1273 } else if ((*tei_min) > 1) {
1274 (*tei_min)--;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001275 new_tun->tei_repl = *tei_min;
1276 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001277 return 1;
1278 }
1279
1280 /* None seems to be available. */
1281 return 0;
1282}
1283
1284static int gtphub_check_reused_teis(struct gtphub *hub,
1285 struct gtphub_tunnel *new_tun)
1286{
1287 uint32_t tei_min = 0xffffffff;
1288 uint32_t tei_max = 0;
1289 int side_idx;
1290 int plane_idx;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001291 struct gtphub_tunnel_endpoint *te;
1292 struct gtphub_tunnel_endpoint *te2;
1293
1294 struct gtphub_tunnel *tun, *ntun;
1295
1296 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1297 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001298 continue;
1299
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001300 /* Check whether the GSN sent a TEI that it is reusing from a
1301 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001302 int tun_continue = 0;
1303 for_each_side(side_idx) {
1304 for_each_plane(plane_idx) {
1305 te = &tun->endpoint[side_idx][plane_idx];
1306 te2 = &new_tun->endpoint[side_idx][plane_idx];
Neels Hofmeyrf6e4d082015-12-06 19:03:35 +01001307 if ((te->tei_orig == 0)
1308 || (te->tei_orig != te2->tei_orig)
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001309 || (!te->peer)
1310 || (!te2->peer)
1311 || !gsn_addr_same(&te->peer->peer_addr->addr,
1312 &te2->peer->peer_addr->addr))
1313 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001314
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001315 /* The peer is reusing a TEI that I believe to
1316 * be part of another tunnel. The other tunnel
1317 * must be stale, then. */
1318 LOG(LOGL_NOTICE,
1319 "Expiring tunnel due to reused TEI:"
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001320 " %s peer %s sent %s TEI %x,"
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001321 " previously used by tunnel %s...\n",
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001322 gtphub_side_idx_names[side_idx],
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001323 gtphub_port_str(te->peer),
1324 gtphub_plane_idx_names[plane_idx],
1325 te->tei_orig,
1326 gtphub_tunnel_str(tun));
1327 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1328 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001329
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001330 expiring_item_del(&tun->expiry_entry);
1331 /* continue to find more matches. There shouldn't be
1332 * any, but let's make sure. However, tun is deleted,
1333 * so we need to skip to the next tunnel. */
1334 tun_continue = 1;
1335 break;
1336 }
1337 if (tun_continue)
1338 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001339 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001340 if (tun_continue)
1341 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001342
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001343 /* Check whether the mapped TEI is already used by another
1344 * tunnel. */
1345 if (!gtphub_check_mapped_tei(new_tun, tun, &tei_min, &tei_max)) {
1346 LOG(LOGL_ERROR,
1347 "No mapped TEI is readily available."
1348 " Searching for holes between occupied"
1349 " TEIs not implemented.");
1350 return 0;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001351 }
1352
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001353 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001354
1355 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001356}
1357
1358static void gtphub_tunnel_refresh(struct gtphub *hub,
1359 struct gtphub_tunnel *tun,
1360 time_t now)
1361{
1362 expiry_add(&hub->expire_slowly,
1363 &tun->expiry_entry,
1364 now);
1365}
1366
1367static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1368 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001369 struct gtphub_peer_port *from,
1370 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001371{
1372 OSMO_ASSERT(from);
1373 int other_side = other_side_idx(p->side_idx);
1374
1375 struct gtphub_tunnel *tun;
1376 llist_for_each_entry(tun, &hub->tunnels, entry) {
1377 struct gtphub_tunnel_endpoint *te_from =
1378 &tun->endpoint[p->side_idx][p->plane_idx];
1379 struct gtphub_tunnel_endpoint *te_to =
1380 &tun->endpoint[other_side][p->plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001381 if ((tun->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001382 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001383 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1384 &from->peer_addr->addr)) {
1385 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001386 if (unmapped_from_tun)
1387 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001388 return te_to;
1389 }
1390 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001391
1392 if (unmapped_from_tun)
1393 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001394 return NULL;
1395}
1396
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001397static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001398 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001399{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001400 if (p->rc != GTP_RC_PDU_C)
1401 return;
1402
1403 int ie_idx;
1404 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1405 if (ie_idx < 0)
1406 return;
1407
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001408 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001409 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001410}
1411
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001412static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001413 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414 struct gtphub *hub,
1415 struct gtp_packet_desc *p,
1416 struct gtphub_peer_port *from_port)
1417{
1418 OSMO_ASSERT(p->version == 1);
1419 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001420 if (unmapped_from_tun)
1421 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001422
1423 /* If the header's TEI is zero, no PDP context has been established
1424 * yet. If nonzero, a mapping should actually already exist for this
1425 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001426 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001427 return 0;
1428
1429 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001430 * mapped in a tunnel struct. */
1431 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001432 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001433 if (!to) {
1434 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1435 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001436 return -1;
1437 }
Neels Hofmeyr312bf6c2016-04-14 15:21:31 +02001438
1439 if (unmapped_from_tun) {
1440 OSMO_ASSERT(*unmapped_from_tun);
1441 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1442 gtphub_tunnel_str(*unmapped_from_tun));
1443 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001444
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001445 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001446 set_tei(p, unmapped_tei);
1447
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001448 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001449 *to_port_p = to->peer;
1450
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001451 return 0;
1452}
1453
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001454static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1455 struct gtp_packet_desc *p,
1456 struct gtphub_peer_port *from_ctrl,
1457 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001458{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001459 int plane_idx;
1460
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001461 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1462 plane_nrs_match_GSN_addr_IE_indices);
1463
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001464 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001465
1466 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001467 if (p->side_idx != GTPH_SIDE_SGSN) {
1468 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001469 " Request from the GGSN side: %s",
1470 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001471 return -1;
1472 }
1473
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001474 if (tun) {
1475 LOG(LOGL_ERROR, "Not implemented: Received"
1476 " Create PDP Context Request for an already"
1477 " established tunnel:"
1478 " from %s, tunnel %s\n",
1479 gtphub_port_str(from_ctrl),
1480 gtphub_tunnel_str(p->tun));
1481 return -1;
1482 }
1483
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001484 /* A new tunnel. */
Maxbb6b00a2017-12-20 12:14:50 +01001485 tun = gtphub_tunnel_new();
1486 if (!tun) {
1487 LOG(LOGL_ERROR, "Failed to allocate new tunnel %s <-> %s\n",
1488 gtphub_port_str(from_ctrl), gtphub_port_str(to_ctrl));
1489 return -1;
1490 }
1491 p->tun = tun;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001492
1493 /* Create TEI mapping */
1494 tun->tei_repl = nr_pool_next(&hub->tei_pool);
1495
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001496 llist_add(&tun->entry, &hub->tunnels);
1497 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001498 /* The endpoint peers on this side (SGSN) will be set from IEs
1499 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001500 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001501 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001502 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001503 if (p->side_idx != GTPH_SIDE_GGSN) {
1504 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001505 " Response from the SGSN side: %s",
1506 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001507 return -1;
1508 }
1509
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001510 /* The tunnel should already have been resolved from the header
1511 * TEI and be available in tun (== p->tun). Just fill in the
1512 * GSN Addresses below.*/
1513 OSMO_ASSERT(tun);
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001514 OSMO_ASSERT(tun->tei_repl == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001515 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001516 }
1517
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001518 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1519 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001520 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001521
Maxe9bcbe12017-12-20 17:01:05 +01001522 for_each_plane(plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001523 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001524 struct gsn_addr use_addr;
1525 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001526 uint32_t tei_from_ie;
1527 int ie_idx;
1528
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001529 /* Fetch GSN Address and TEI from IEs. As ensured by above
1530 * static asserts, plane_idx corresponds to the GSN Address IE
1531 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001532 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001533 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001534 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1535 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001536 return -1;
1537 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001538 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001539 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001540 gsn_addr_to_str(&use_addr),
1541 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001542
1543 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1544 if (ie_idx < 0) {
1545 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001546 LOG(LOGL_ERROR,
1547 "Create PDP Context message invalid:"
1548 " missing IE %d\n",
1549 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550 return -1;
1551 }
1552 tei_from_ie = 0;
1553 }
1554 else
1555 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1556
1557 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001558 * exists.
1559 *
1560 * Exception: if sgsn_use_sender is set, instead use the
1561 * sender's address and port for Ctrl -- the User port is not
1562 * known until the first User packet arrives.
1563 *
1564 * Note: doing this here is just an optimization, because
1565 * gtphub_handle_buf() has code to replace the tunnel
1566 * endpoints' addresses with the sender (needed for User
1567 * plane). We could just ignore sgsn_use_sender here. But if we
1568 * set up a default port here and replace it in
1569 * gtphub_handle_buf(), we'd be creating a peer port just to
1570 * expire it right away. */
1571 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
Max99f99532017-12-20 17:02:15 +01001572 int rc = gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1573 if (rc < 0)
1574 LOG(LOGL_ERROR, "%s(): failed to obtain GSN address\n", __func__);
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001575 } else {
1576 use_port = gtphub_plane_idx_default_port[plane_idx];
1577
1578 }
1579
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001580 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001581 peer_from_ie = gtphub_port_have(hub,
1582 &hub->to_gsns[side_idx][plane_idx],
1583 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001584
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001585 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1586 peer_from_ie);
1587
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001588 if (!tei_from_ie &&
1589 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1590 LOG(LOGL_ERROR,
Neels Hofmeyr328d2f72015-12-06 19:13:21 +01001591 "Create PDP Context message omits %s TEI, but"
1592 " no TEI has been announced for this tunnel: %s\n",
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001593 gtphub_plane_idx_names[plane_idx],
1594 gtphub_tunnel_str(tun));
1595 return -1;
1596 }
1597
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001598 if (tei_from_ie) {
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001599 /* Replace TEI in GTP packet IE */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001600 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001601 p->ie[ie_idx]->tv4.v = hton32(tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001602
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001603 if (!gtphub_check_reused_teis(hub, tun)) {
1604 /* It's highly unlikely that all TEIs are
1605 * taken. But the code looking for an unused
1606 * TEI is, at the time of writing this comment,
1607 * not able to find gaps in the TEI space. To
1608 * explicitly alert the user of this problem,
1609 * rather abort than carry on. */
1610 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1611 abort();
1612 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001613 }
1614
1615 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001616 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1617 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001618 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001619 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1620 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001621 return -1;
1622 }
1623 }
1624
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001625 if (p->type == GTP_CREATE_PDP_REQ) {
1626 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1627 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001628 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001629 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1630 gtphub_tunnel_str(tun));
1631 }
1632
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001633 return 0;
1634}
1635
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001636static void pending_delete_del_cb(struct expiring_item *expi)
1637{
1638 struct pending_delete *pd;
1639 pd = container_of(expi, struct pending_delete, expiry_entry);
1640
1641 llist_del(&pd->entry);
1642 INIT_LLIST_HEAD(&pd->entry);
1643
1644 pd->expiry_entry.del_cb = 0;
1645 expiring_item_del(&pd->expiry_entry);
1646
1647 talloc_free(pd);
1648}
1649
1650static struct pending_delete *pending_delete_new(void)
1651{
1652 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1653 INIT_LLIST_HEAD(&pd->entry);
1654 expiring_item_init(&pd->expiry_entry);
1655 pd->expiry_entry.del_cb = pending_delete_del_cb;
1656 return pd;
1657}
1658
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001659static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1660 struct gtp_packet_desc *p,
1661 struct gtphub_peer_port *from_ctrl,
1662 struct gtphub_peer_port *to_ctrl)
1663{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001664 struct gtphub_tunnel *known_tun = p->tun;
1665
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001666 if (p->type == GTP_DELETE_PDP_REQ) {
1667 if (!known_tun) {
1668 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1669 return -1;
1670 }
1671
1672 /* Store the Delete Request until a successful Response is seen. */
1673 uint8_t teardown_ind;
1674 uint8_t nsapi;
1675
1676 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1677 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1678 return -1;
1679 }
1680
1681 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1682 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1683 return -1;
1684 }
1685
1686 struct pending_delete *pd = NULL;
1687
1688 struct pending_delete *pdi = NULL;
1689 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1690 if ((pdi->tun == known_tun)
1691 && (pdi->teardown_ind == teardown_ind)
1692 && (pdi->nsapi == nsapi)) {
1693 pd = pdi;
1694 break;
1695 }
1696 }
1697
1698 if (!pd) {
1699 pd = pending_delete_new();
1700 pd->tun = known_tun;
1701 pd->teardown_ind = teardown_ind;
1702 pd->nsapi = nsapi;
1703
1704 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1705 gtphub_tunnel_str(known_tun));
1706 llist_add(&pd->entry, &hub->pending_deletes);
1707 }
1708
1709 /* Add or refresh timeout. */
1710 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1711
1712 /* If a pending_delete should expire before the response to
1713 * indicate success comes in, the responding peer will have the
1714 * tunnel deactivated, while the requesting peer gets no reply
1715 * and keeps the tunnel. The hope is that the requesting peer
1716 * will try again and get a useful response. */
1717 } else if (p->type == GTP_DELETE_PDP_RSP) {
1718 /* Find the Delete Request for this Response. */
1719 struct pending_delete *pd = NULL;
1720
1721 struct pending_delete *pdi;
1722 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1723 if (known_tun == pdi->tun) {
1724 pd = pdi;
1725 break;
1726 }
1727 }
1728
1729 if (!pd) {
1730 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1731 " Cannot find matching request.");
1732 /* If we delete the tunnel now, anyone can send a
1733 * Delete response to kill tunnels at will. */
1734 return -1;
1735 }
1736
1737 /* TODO handle teardown_ind and nsapi */
1738
1739 expiring_item_del(&pd->expiry_entry);
1740
1741 uint8_t cause;
1742 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1743 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1744 " Missing Cause IE.");
1745 /* If we delete the tunnel now, at least one of the
1746 * peers may still think it is active. */
1747 return -1;
1748 }
1749
1750 if (cause != GTPCAUSE_ACC_REQ) {
1751 LOG(LOGL_NOTICE,
1752 "Delete PDP Context Response indicates failure;"
1753 "for %s\n",
1754 gtphub_tunnel_str(known_tun));
1755 return -1;
1756 }
1757
1758 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1759 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001760 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001761 expiring_item_del(&known_tun->expiry_entry);
1762 }
1763
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001764 return 0;
1765}
1766
1767static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1768 struct gtp_packet_desc *p,
1769 struct gtphub_peer_port *from_ctrl,
1770 struct gtphub_peer_port *to_ctrl)
1771{
1772 /* TODO */
1773 return 0;
1774}
1775
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001776/* Read GSN address IEs from p, and make sure these peer addresses exist in
1777 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1778 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1779 * the packet p. */
1780static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1781 struct gtp_packet_desc *p,
1782 struct gtphub_peer_port *from_ctrl,
1783 struct gtphub_peer_port *to_ctrl)
1784{
1785 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1786
1787 switch (p->type) {
1788 case GTP_CREATE_PDP_REQ:
1789 case GTP_CREATE_PDP_RSP:
1790 return gtphub_handle_create_pdp_ctx(hub, p,
1791 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001792
1793 case GTP_DELETE_PDP_REQ:
1794 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001795 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001796 from_ctrl, to_ctrl);
1797
1798 case GTP_UPDATE_PDP_REQ:
1799 case GTP_UPDATE_PDP_RSP:
1800 return gtphub_handle_update_pdp_ctx(hub, p,
1801 from_ctrl, to_ctrl);
1802
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001803 default:
1804 /* Nothing to do for this message type. */
1805 return 0;
1806 }
1807
1808}
1809
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001810static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1811 struct gtphub_tunnel *tun,
1812 int to_side)
1813{
1814 static uint8_t del_ctx_msg[16] = {
1815 0x32, /* GTP v1 flags */
1816 GTP_DELETE_PDP_REQ,
1817 0x00, 0x08, /* Length in network byte order */
1818 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1819 0, 0, /* Seq, to be replaced */
1820 0, 0, /* no extensions */
1821 0x13, 0xff, /* 19: Teardown ind = 1 */
1822 0x14, 0 /* 20: NSAPI = 0 */
1823 };
1824
1825 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1826 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1827
1828 struct gtphub_tunnel_endpoint *te =
1829 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1830
1831 if (! te->peer)
1832 return 0;
1833
1834 *tei = hton32(te->tei_orig);
1835 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1836
1837 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001838 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1839 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001840 if (rc != 0) {
1841 LOG(LOGL_ERROR,
1842 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1843 gtphub_port_str(te->peer));
1844 }
1845 return rc;
1846}
1847
1848/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1849static void gtphub_restarted(struct gtphub *hub,
1850 struct gtp_packet_desc *p,
1851 struct gtphub_peer_port *pp)
1852{
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001853 LOG(LOGL_NOTICE, "Peer has restarted: %s\n",
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001854 gtphub_port_str(pp));
1855
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001856 int deleted_count = 0;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001857 struct gtphub_tunnel *tun;
1858 llist_for_each_entry(tun, &hub->tunnels, entry) {
1859 int side_idx;
1860 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001861 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1862 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1863 if ((!te->peer)
1864 || (!te2->tei_orig)
1865 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001866 continue;
1867
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001868 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1869 gtphub_tunnel_str(tun));
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001870 deleted_count ++;
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001871
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001872 /* Send a Delete PDP Context Request to the
1873 * peer on the other side, remember the pending
1874 * delete and wait for the response to delete
1875 * the tunnel. Clear this side of the tunnel to
1876 * make sure it isn't used.
1877 *
1878 * Should the delete message send fail, or if no
1879 * response is received, this tunnel will expire. If
1880 * its TEIs come up in a new PDP Context Request, it
1881 * will be removed. If messages for this tunnel should
1882 * come in (from the not restarted side), they will be
1883 * dropped because the tunnel is rendered unusable. */
1884 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1885
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001886 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1887 NULL);
1888 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1889 NULL);
1890 }
1891 }
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001892
1893 if (deleted_count)
1894 LOG(LOGL_NOTICE, "Deleting %d tunnels due to restart of: %s\n",
1895 deleted_count,
1896 gtphub_port_str(pp));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001897}
1898
1899static int get_restart_count(struct gtp_packet_desc *p)
1900{
1901 int ie_idx;
1902 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1903 if (ie_idx < 0)
1904 return -1;
1905 return ntoh8(p->ie[ie_idx]->tv1.v);
1906}
1907
1908static void gtphub_check_restart_counter(struct gtphub *hub,
1909 struct gtp_packet_desc *p,
1910 struct gtphub_peer_port *from)
1911{
1912 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1913 * that doesn't match the peer's previously sent restart counter, clear
1914 * that peer and cancel PDP contexts. */
1915
1916 int restart = get_restart_count(p);
1917
1918 if ((restart < 0) || (restart > 255))
1919 return;
1920
1921 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1922 if (from->last_restart_count != restart) {
1923 gtphub_restarted(hub, p, from);
1924 }
1925 }
1926
1927 from->last_restart_count = restart;
1928}
1929
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001930static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1931{
1932 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1933 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001934 LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n",
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001935 gtphub_plane_idx_names[plane_idx]);
1936
Pau Espin Pedrolb2ebc592020-05-09 19:21:15 +02001937 if (!(what & OSMO_FD_READ))
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001938 return 0;
1939
1940 struct gtphub *hub = from_sgsns_ofd->data;
1941
1942 static uint8_t buf[4096];
Alexander Couzensdd930a22020-07-18 16:45:25 +02001943 struct sgsn_sockaddr from_addr;
1944 struct sgsn_sockaddr to_addr;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001945 struct osmo_fd *to_ofd;
1946 int len;
1947 uint8_t *reply_buf;
1948
1949 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1950 if (len < 1)
1951 return 0;
1952
1953 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1954 buf, len, gtphub_now(),
1955 &reply_buf, &to_ofd, &to_addr);
1956 if (len < 1)
1957 return 0;
1958
1959 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1960}
1961
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001962static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1963{
1964 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1965 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001966 LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001967 gtphub_plane_idx_names[plane_idx]);
Pau Espin Pedrolb2ebc592020-05-09 19:21:15 +02001968 if (!(what & OSMO_FD_READ))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001969 return 0;
1970
1971 struct gtphub *hub = from_ggsns_ofd->data;
1972
1973 static uint8_t buf[4096];
Alexander Couzensdd930a22020-07-18 16:45:25 +02001974 struct sgsn_sockaddr from_addr;
1975 struct sgsn_sockaddr to_addr;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001976 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001977 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001978 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001979
1980 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1981 if (len < 1)
1982 return 0;
1983
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001984 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1985 buf, len, gtphub_now(),
1986 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001987 if (len < 1)
1988 return 0;
1989
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001990 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001991}
1992
1993static int gtphub_unmap(struct gtphub *hub,
1994 struct gtp_packet_desc *p,
1995 struct gtphub_peer_port *from,
1996 struct gtphub_peer_port *to_proxy,
1997 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001998 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001999{
2000 /* Always (try to) unmap sequence and TEI numbers, which need to be
2001 * replaced in the packet. Either way, give precedence to the proxy, if
2002 * configured. */
2003
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002004 if (unmapped_from_seq)
2005 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002006 if (final_unmapped)
2007 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002008 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002009
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002010 struct gtphub_peer_port *from_seq = NULL;
2011 struct gtphub_peer_port *from_tei = NULL;
2012 struct gtphub_peer_port *unmapped = NULL;
2013
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002014 from_seq = gtphub_unmap_seq(p, from);
2015
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002016 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002017 return -1;
2018
2019 struct gtphub_peer *from_peer = from->peer_addr->peer;
2020 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002021 LOG(LOGL_DEBUG,
2022 "Seq unmap and TEI unmap yield two different peers."
2023 " Using seq unmap."
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01002024 " (from %s %s: seq %d yields %s, tei %u yields %s)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002025 gtphub_plane_idx_names[p->plane_idx],
2026 gtphub_peer_str(from_peer),
2027 (int)p->seq,
2028 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002029 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002030 gtphub_port_str2(from_tei)
2031 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002032 }
2033 unmapped = (from_seq? from_seq : from_tei);
2034
2035 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002036 LOG(LOGL_NOTICE,
2037 "Unmap yields a different peer than the configured proxy."
2038 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002039 " unmapped: %s proxy: %s\n",
2040 gtphub_port_str(unmapped),
2041 gtphub_port_str2(to_proxy)
2042 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002043 }
2044 unmapped = (to_proxy? to_proxy : unmapped);
2045
2046 if (!unmapped) {
2047 /* Return no error, but returned pointers are all NULL. */
2048 return 0;
2049 }
2050
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002051 if (unmapped_from_seq)
2052 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002053 if (final_unmapped)
2054 *final_unmapped = unmapped;
2055 return 0;
2056}
2057
2058static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2059 uint16_t port,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002060 struct sgsn_sockaddr *dst)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002061{
Alexander Couzensdd930a22020-07-18 16:45:25 +02002062 return sgsn_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002063}
2064
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002065/* If p is an Echo request, replace p's data with the matching response and
2066 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2067 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002068static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2069 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002070{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002071 if (p->type != GTP_ECHO_REQ)
2072 return 0;
2073
2074 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002075 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002076 GTP_ECHO_RSP,
2077 0x00, 14 - 8, /* Length in network byte order */
2078 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2079 0, 0, /* Seq, to be replaced */
2080 0, 0, /* no extensions */
2081 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002082 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002083 };
2084 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2085 uint8_t *recovery = &echo_response_data[13];
2086
2087 *seq = hton16(p->seq);
2088 *recovery = hub->restart_counter;
2089
2090 *reply_buf = echo_response_data;
2091
2092 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002093}
2094
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002095struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002096 const struct sgsn_sockaddr *addr);
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002097
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002098/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2099 * address to forward to. Return a pointer to the osmo_fd, but copy the
2100 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2101 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2102 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002103int gtphub_handle_buf(struct gtphub *hub,
2104 unsigned int side_idx,
2105 unsigned int plane_idx,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002106 const struct sgsn_sockaddr *from_addr,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002107 uint8_t *buf,
2108 size_t received,
2109 time_t now,
2110 uint8_t **reply_buf,
2111 struct osmo_fd **to_ofd,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002112 struct sgsn_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002113{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002114 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2115 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002116
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002117 rate_ctr_add(rate_ctr_group_get_ctr(from_bind->counters_io, GTPH_CTR_BYTES_IN),
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002118 received);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002119
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002120 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002121 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002122
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002123 LOG(LOGL_DEBUG, "%s rx %s from %s %s%s\n",
2124 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
2125 gtphub_plane_idx_names[plane_idx],
2126 gtphub_side_idx_names[side_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002127 sgsn_sockaddr_to_str(from_addr),
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002128 gtp_type_str(p.type));
2129
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002130 if (p.rc <= 0) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002131 LOG(LOGL_ERROR, "INVALID: dropping GTP packet%s from %s %s %s\n",
2132 gtp_type_str(p.type),
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002133 gtphub_side_idx_names[side_idx],
2134 gtphub_plane_idx_names[plane_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002135 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002136 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002137 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002138
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002139 rate_ctr_inc(rate_ctr_group_get_ctr(from_bind->counters_io, GTPH_CTR_PKTS_IN));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002140
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002141 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002142 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002143 if (reply_len > 0) {
2144 /* It was an echo. Nothing left to do. */
Alexander Couzensdd930a22020-07-18 16:45:25 +02002145 sgsn_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002146 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002147
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002148 rate_ctr_inc(rate_ctr_group_get_ctr(from_bind->counters_io, GTPH_CTR_PKTS_OUT));
2149 rate_ctr_add(rate_ctr_group_get_ctr(from_bind->counters_io, GTPH_CTR_BYTES_OUT),
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002150 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002151 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2152 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2153 gtphub_side_idx_names[side_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002154 (int)reply_len, sgsn_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002155 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002156 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002157 if (reply_len < 0)
2158 return -1;
2159
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002160 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002161
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002162 /* If a proxy is configured, check that it's indeed that proxy talking
2163 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2164 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002165 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2166 if (from_peer) {
Alexander Couzensdd930a22020-07-18 16:45:25 +02002167 if (sgsn_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002168 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002169 "Rejecting: %s proxy configured, but GTP packet"
2170 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002171 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002172 gtphub_side_idx_names[side_idx],
2173 gtphub_side_idx_names[side_idx],
2174 gtphub_port_str(from_peer),
Alexander Couzensdd930a22020-07-18 16:45:25 +02002175 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002176 return -1;
2177 }
2178 }
2179
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002180 if (!from_peer) {
2181 /* Find or create a peer with a matching address. The sender's
2182 * port may in fact differ. */
2183 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002184 }
2185
2186 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002187 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2188 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002189 if (!from_peer) {
2190 if (side_idx == GTPH_SIDE_GGSN) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002191 LOG(LOGL_ERROR, "Dropping packet%s: unknown GGSN peer: %s\n",
2192 gtp_type_str(p.type),
Alexander Couzensdd930a22020-07-18 16:45:25 +02002193 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002194 return -1;
2195 } else {
2196 /* SGSN */
2197 /* A new peer. If this is on the Ctrl plane, an SGSN
2198 * may make first contact without being known yet, so
2199 * create the peer struct for the current sender. */
2200 if (plane_idx != GTPH_PLANE_CTRL) {
2201 LOG(LOGL_ERROR,
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002202 "Dropping packet%s: User plane peer was not"
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002203 "announced by PDP Context: %s\n",
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002204 gtp_type_str(p.type),
Alexander Couzensdd930a22020-07-18 16:45:25 +02002205 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002206 return -1;
2207 }
2208
2209 struct gsn_addr from_gsna;
2210 uint16_t from_port;
2211 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2212 return -1;
2213
2214 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2215 }
2216 }
2217
2218 if (!from_peer) {
2219 /* This could theoretically happen for invalid address data or
2220 * somesuch. */
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002221 LOG(LOGL_ERROR, "Dropping packet%s: invalid %s peer: %s\n",
2222 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002223 gtphub_side_idx_names[side_idx],
Alexander Couzensdd930a22020-07-18 16:45:25 +02002224 sgsn_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002225 return -1;
2226 }
2227
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002228 rate_ctr_add(rate_ctr_group_get_ctr(from_peer->counters_io, GTPH_CTR_BYTES_IN),
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002229 received);
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002230 rate_ctr_inc(rate_ctr_group_get_ctr(from_peer->counters_io, GTPH_CTR_PKTS_IN));
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002231
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002232 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2233 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002234
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002235 gtphub_check_restart_counter(hub, &p, from_peer);
2236 gtphub_map_restart_counter(hub, &p);
2237
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002238 struct gtphub_peer_port *to_peer_from_seq;
2239 struct gtphub_peer_port *to_peer;
2240 if (gtphub_unmap(hub, &p, from_peer,
2241 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002242 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002243 != 0) {
2244 return -1;
2245 }
2246
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002247 if (p.tun) {
2248 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[p.side_idx][p.plane_idx];
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002249 rate_ctr_add(rate_ctr_group_get_ctr(te->counters_io, GTPH_CTR_BYTES_IN),
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002250 received);
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002251 rate_ctr_inc(rate_ctr_group_get_ctr(te->counters_io, GTPH_CTR_PKTS_IN));
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002252 }
2253
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002254 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2255 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2256 return -1;
2257 }
2258
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002259 if (!to_peer && p.tun && p.type == GTP_DELETE_PDP_RSP) {
2260 /* It's a delete confirmation for a tunnel that is partly
2261 * invalid, probably marked unsuable due to a restarted peer.
2262 * Remove the tunnel and be happy without forwarding. */
2263 expiring_item_del(&p.tun->expiry_entry);
2264 p.tun = NULL;
2265 return 0;
2266 }
2267
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002268 if (!to_peer) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002269 LOG(LOGL_ERROR, "No %s to send to. Dropping packet%s"
2270 " (type=%" PRIu8 ", header-TEI=%" PRIx32 ", seq=%" PRIx16 ").\n",
2271 gtphub_side_idx_names[other_side_idx(side_idx)],
2272 gtp_type_str(p.type),
2273 p.type, p.header_tei_rx, p.seq
2274 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002275 return -1;
2276 }
2277
2278 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002279 /* This may be a Create PDP Context response. If it is, there
2280 * are other addresses in the GTP message to set up apart from
2281 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002282 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002283 != 0)
2284 return -1;
2285 }
Pau Espin Pedrolf37aedb2023-01-05 14:20:27 +01002286
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002287 /* Either to_peer was resolved from an existing tunnel,
2288 * or a PDP Ctx and thus a tunnel has just been created,
2289 * or the tunnel has been deleted due to this message. */
2290 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002291
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002292 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002293 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002294 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002295 if (!to_peer_from_seq)
2296 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002297
Alexander Couzensdd930a22020-07-18 16:45:25 +02002298 sgsn_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002299
2300 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002301
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002302 if (received) {
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002303 rate_ctr_inc(rate_ctr_group_get_ctr(to_bind->counters_io, GTPH_CTR_PKTS_OUT));
2304 rate_ctr_add(rate_ctr_group_get_ctr(to_bind->counters_io, GTPH_CTR_BYTES_OUT),
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002305 received);
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002306
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002307 rate_ctr_inc(rate_ctr_group_get_ctr(to_peer->counters_io, GTPH_CTR_PKTS_OUT));
2308 rate_ctr_add(rate_ctr_group_get_ctr(to_peer->counters_io, GTPH_CTR_BYTES_OUT),
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002309 received);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002310 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002311
2312 if (p.tun) {
2313 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[other_side_idx(p.side_idx)][p.plane_idx];
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02002314 rate_ctr_inc(rate_ctr_group_get_ctr(te->counters_io, GTPH_CTR_PKTS_OUT));
2315 rate_ctr_add(rate_ctr_group_get_ctr(te->counters_io, GTPH_CTR_BYTES_OUT),
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002316 received);
2317 }
2318
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002319 LOG(LOGL_DEBUG, "%s Forward to %s:"
2320 " header-TEI %" PRIx32", seq %" PRIx16", %d bytes to %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002321 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2322 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002323 p.header_tei, p.seq,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002324 (int)received, sgsn_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002325 return received;
2326}
2327
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002328static void resolved_gssn_del_cb(struct expiring_item *expi)
2329{
2330 struct gtphub_resolved_ggsn *ggsn;
2331 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2332
2333 gtphub_port_ref_count_dec(ggsn->peer);
2334 llist_del(&ggsn->entry);
2335
2336 ggsn->expiry_entry.del_cb = 0;
2337 expiring_item_del(&ggsn->expiry_entry);
2338
2339 talloc_free(ggsn);
2340}
2341
2342void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2343 struct gsn_addr *resolved_addr,
2344 time_t now)
2345{
2346 struct gtphub_peer_port *pp;
2347 struct gtphub_resolved_ggsn *ggsn;
2348
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002349 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002350 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2351 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002352
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002353 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002354 resolved_addr, 2123);
2355 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002356 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2357 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002358 return;
2359 }
2360
2361 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2362 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002363 INIT_LLIST_HEAD(&ggsn->entry);
2364 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002365
2366 ggsn->peer = pp;
2367 gtphub_port_ref_count_inc(pp);
2368
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002369 osmo_strlcpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002370
2371 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002372 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002373
2374 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2375}
2376
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002377static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2378{
2379 return pp->ref_count == 0;
2380}
2381
2382static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2383{
2384 struct gtphub_peer_port *pp, *npp;
2385 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2386 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002387 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002388 gtphub_port_str(pp));
2389 gtphub_peer_port_del(pp);
2390 }
2391 }
2392 return llist_empty(&pa->ports);
2393}
2394
2395static int gtphub_gc_peer(struct gtphub_peer *p)
2396{
2397 struct gtphub_peer_addr *pa, *npa;
2398 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2399 if (gtphub_gc_peer_addr(pa)) {
2400 gtphub_peer_addr_del(pa);
2401 }
2402 }
2403
2404 /* Note that there's a ref_count in each gtphub_peer_port instance
2405 * listed within p->addresses, referenced by TEI mappings from
2406 * hub->tei_map. As long as those don't expire, this peer will stay. */
2407
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002408 return llist_empty(&p->addresses)
2409 && nr_map_empty(&p->seq_map);
2410}
2411
2412static void gtphub_gc_bind(struct gtphub_bind *b)
2413{
2414 struct gtphub_peer *p, *n;
2415 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2416 if (gtphub_gc_peer(p)) {
2417 gtphub_peer_del(p);
2418 }
2419 }
2420}
2421
2422void gtphub_gc(struct gtphub *hub, time_t now)
2423{
2424 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002425 expired = expiry_tick(&hub->expire_quickly, now);
2426 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002427
2428 /* ... */
2429
2430 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002431 int s, p;
2432 for_each_side_and_plane(s, p) {
2433 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002434 }
2435 }
2436}
2437
2438static void gtphub_gc_cb(void *data)
2439{
2440 struct gtphub *hub = data;
2441 gtphub_gc(hub, gtphub_now());
2442 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2443}
2444
2445static void gtphub_gc_start(struct gtphub *hub)
2446{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02002447 osmo_timer_setup(&hub->gc_timer, gtphub_gc_cb, hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002448 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2449}
2450
2451/* called by unit tests */
2452void gtphub_init(struct gtphub *hub)
2453{
2454 gtphub_zero(hub);
2455
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002456 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002457 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002458
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002459 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2460 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002461
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002462 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2463
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002464 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002465 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002466 for_each_side_and_plane(side_idx, plane_idx) {
Maxbb6b00a2017-12-20 12:14:50 +01002467 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx], CTR_IDX_HUB(side_idx, plane_idx));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002468 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002469
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002470 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2471 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2472 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2473 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002474}
2475
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002476/* For the test suite, this is kept separate from gtphub_stop(), which also
2477 * closes sockets. The test suite avoids using sockets and would cause
2478 * segfaults when trying to close uninitialized ofds. */
2479void gtphub_free(struct gtphub *hub)
2480{
2481 /* By expiring all mappings, a garbage collection should free
2482 * everything else. A gtphub_bind_free() will assert that everything is
2483 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002484 expiry_clear(&hub->expire_quickly);
2485 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002486
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002487 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002488 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002489 for_each_side_and_plane(side_idx, plane_idx) {
2490 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2491 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002492 }
2493}
2494
2495void gtphub_stop(struct gtphub *hub)
2496{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002497 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002498 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002499 for_each_side_and_plane(side_idx, plane_idx) {
2500 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002501 }
2502 gtphub_free(hub);
2503}
2504
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002505static int gtphub_make_proxy(struct gtphub *hub,
2506 struct gtphub_peer_port **pp,
2507 struct gtphub_bind *bind,
2508 const struct gtphub_cfg_addr *addr)
2509{
2510 if (!addr->addr_str)
2511 return 0;
2512
2513 struct gsn_addr gsna;
2514 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2515 return -1;
2516
2517 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2518
2519 /* This is *the* proxy. Make sure it is never expired. */
2520 gtphub_port_ref_count_inc(*pp);
2521 return 0;
2522}
2523
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002524int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2525 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002526{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002527 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002528
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002529 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002530 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002531
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002532 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002533 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002534 if (gtphub_ares_init(hub) != 0) {
2535 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2536 return -1;
2537 }
2538 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002539
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002540 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002541 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002542 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002543 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002544 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2545 &cfg->to_gsns[side_idx][plane_idx],
2546 (side_idx == GTPH_SIDE_SGSN)
2547 ? from_sgsns_read_cb
2548 : from_ggsns_read_cb,
2549 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002550 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002551 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2552 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002553 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002554 return rc;
2555 }
2556 }
2557
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002558 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002559 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002560 &hub->proxy[side_idx][plane_idx],
2561 &hub->to_gsns[side_idx][plane_idx],
2562 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002563 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002564 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002565 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002566 gtphub_side_idx_names[side_idx],
2567 cfg->proxy[side_idx][plane_idx].addr_str,
2568 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002569 return -1;
2570 }
2571 }
2572
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002573 for_each_side_and_plane(side_idx, plane_idx) {
2574 if (hub->proxy[side_idx][plane_idx])
2575 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2576 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002577 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002578 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002579 }
2580
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002581 if (hub->sgsn_use_sender)
Neels Hofmeyr9d8f5062015-12-03 14:52:33 +01002582 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 +01002583
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002584 gtphub_gc_start(hub);
2585 return 0;
2586}
2587
2588static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2589 const struct gsn_addr *addr)
2590{
2591 struct gtphub_peer_addr *a;
2592 llist_for_each_entry(a, &peer->addresses, entry) {
2593 if (gsn_addr_same(&a->addr, addr))
2594 return a;
2595 }
2596 return NULL;
2597}
2598
2599static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2600 uint16_t port)
2601{
2602 OSMO_ASSERT(port);
2603 struct gtphub_peer_port *pp;
2604 llist_for_each_entry(pp, &a->ports, entry) {
2605 if (pp->port == port)
2606 return pp;
2607 }
2608 return NULL;
2609}
2610
2611static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2612 const struct gsn_addr *addr)
2613{
2614 struct gtphub_peer *peer;
2615 llist_for_each_entry(peer, &bind->peers, entry) {
2616 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2617 if (a)
2618 return a;
2619 }
2620 return NULL;
2621}
2622
2623static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2624 const struct gsn_addr *addr,
2625 uint16_t port)
2626{
2627 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2628 if (!a)
2629 return NULL;
2630 return gtphub_addr_find_port(a, port);
2631}
2632
2633struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002634 const struct sgsn_sockaddr *addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002635{
2636 struct gsn_addr gsna;
2637 uint16_t port;
Maxb643f562018-01-30 10:09:00 +01002638 if (gsn_addr_from_sockaddr(&gsna, &port, addr) != 0)
2639 return NULL;
2640
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002641 return gtphub_port_find(bind, &gsna, port);
2642}
2643
2644static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2645 struct gtphub_bind *bind)
2646{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002647 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2648 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002649 OSMO_ASSERT(peer);
2650
2651 INIT_LLIST_HEAD(&peer->addresses);
2652
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002653 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002654 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002655
2656 /* TODO use something random to pick the initial sequence nr.
2657 0x6d31 produces the ASCII character sequence 'm1', currently used in
2658 gtphub_nc_test.sh. */
2659 peer->seq_pool.last_nr = 0x6d31 - 1;
2660
2661 llist_add(&peer->entry, &bind->peers);
2662 return peer;
2663}
2664
2665static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2666 const struct gsn_addr *addr)
2667{
2668 struct gtphub_peer_addr *a;
2669 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2670 OSMO_ASSERT(a);
2671 a->peer = peer;
2672 gsn_addr_copy(&a->addr, addr);
2673 INIT_LLIST_HEAD(&a->ports);
2674 llist_add(&a->entry, &peer->addresses);
2675
2676 return a;
2677}
2678
2679static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2680 struct gtphub_bind *bind,
2681 const struct gsn_addr *addr)
2682{
2683 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2684 if (a)
2685 return a;
2686
2687 /* If we haven't found an address, that means we need to create an
2688 * entirely new peer for the new address. More addresses may be added
2689 * to this peer later, but not via this function. */
2690 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002691
2692 a = gtphub_peer_add_addr(peer, addr);
Pau Espin Pedrolf37aedb2023-01-05 14:20:27 +01002693
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002694 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002695 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002696 gsn_addr_to_str(&a->addr));
2697
2698 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002699}
2700
2701static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2702 uint16_t port)
2703{
2704 struct gtphub_peer_port *pp;
2705
2706 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2707 OSMO_ASSERT(pp);
2708 pp->peer_addr = a;
2709 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002710 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002711
2712 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2713 talloc_free(pp);
2714 return NULL;
2715 }
2716
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002717 pp->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
Maxbb6b00a2017-12-20 12:14:50 +01002718 &gtphub_ctrg_io_desc, port);
Harald Welted939ac02017-07-17 23:59:39 +02002719 if (!pp->counters_io) {
Maxbb6b00a2017-12-20 12:14:50 +01002720 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 +02002721 talloc_free(pp);
2722 return NULL;
2723 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002724
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002725 llist_add(&pp->entry, &a->ports);
2726
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002727 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002728 gsn_addr_to_str(&a->addr),
2729 (int)port);
2730
2731 return pp;
2732}
2733
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002734struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2735 struct gtphub_bind *bind,
2736 const struct gsn_addr *addr,
2737 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002738{
2739 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2740
2741 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2742 if (pp)
2743 return pp;
2744
2745 return gtphub_addr_add_port(a, port);
2746}
2747
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002748/* Find a GGSN peer with a matching address. If the address is known but the
2749 * port not, create a new port for that peer address. */
2750struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002751 const struct sgsn_sockaddr *addr)
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002752{
2753 struct gtphub_peer_addr *pa;
2754 struct gtphub_peer_port *pp;
2755
2756 struct gsn_addr gsna;
2757 uint16_t port;
Max99f99532017-12-20 17:02:15 +01002758 int rc = gsn_addr_from_sockaddr(&gsna, &port, addr);
2759 if (rc < 0)
2760 LOG(LOGL_ERROR, "%s(): failed to obtain GSN address\n", __func__);
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002761
2762 pa = gtphub_addr_find(bind, &gsna);
2763 if (!pa)
2764 return NULL;
2765
2766 pp = gtphub_addr_find_port(pa, port);
2767
2768 if (!pp)
2769 pp = gtphub_addr_add_port(pa, port);
2770
2771 return pp;
2772}
2773
2774
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002775/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2776 * resolution should be possible but failed, and 1 if resolution was
2777 * successful. *pp will be set to NULL if <1 is returned. */
2778static int gtphub_resolve_ggsn(struct gtphub *hub,
2779 struct gtp_packet_desc *p,
2780 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002781{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002782 *pp = NULL;
2783
2784 /* TODO determine from message type whether IEs should be present? */
2785
2786 int rc;
2787 const char *imsi_str;
2788 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2789 if (rc < 1)
2790 return rc;
2791 OSMO_ASSERT(imsi_str);
2792
2793 const char *apn_str;
2794 rc = get_ie_apn_str(p->ie, &apn_str);
2795 if (rc < 1)
2796 return rc;
2797 OSMO_ASSERT(apn_str);
2798
2799 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2800 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002801}
2802
2803
2804/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002805/* use this in osmo_sock_init() to remove dup. */
Alexander Couzensdd930a22020-07-18 16:45:25 +02002806/* Internal: call getaddrinfo for sgsn_sockaddr_init(). The caller is required
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002807 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002808static int _osmo_getaddrinfo(struct addrinfo **result,
2809 uint16_t family, uint16_t type, uint8_t proto,
2810 const char *host, uint16_t port)
2811{
2812 struct addrinfo hints;
2813 char portbuf[16];
2814
2815 sprintf(portbuf, "%u", port);
2816 memset(&hints, '\0', sizeof(struct addrinfo));
2817 hints.ai_family = family;
2818 if (type == SOCK_RAW) {
2819 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2820 * SOCK_RAW and IPPROTO_GRE is used.
2821 */
2822 hints.ai_socktype = SOCK_DGRAM;
2823 hints.ai_protocol = IPPROTO_UDP;
2824 } else {
2825 hints.ai_socktype = type;
2826 hints.ai_protocol = proto;
2827 }
2828
2829 return getaddrinfo(host, portbuf, &hints, result);
2830}
2831
2832/* TODO move to osmocom/core/socket.c ? */
Alexander Couzensdd930a22020-07-18 16:45:25 +02002833int sgsn_sockaddr_init(struct sgsn_sockaddr *addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002834 uint16_t family, uint16_t type, uint8_t proto,
2835 const char *host, uint16_t port)
2836{
2837 struct addrinfo *res;
2838 int rc;
2839 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2840
2841 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002842 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002843 return -EINVAL;
2844 }
2845
2846 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2847 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2848 addr->l = res->ai_addrlen;
2849 freeaddrinfo(res);
2850
2851 return 0;
2852}
2853
Alexander Couzensdd930a22020-07-18 16:45:25 +02002854int sgsn_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002855 char *port_str, size_t port_str_len,
Alexander Couzensdd930a22020-07-18 16:45:25 +02002856 const struct sgsn_sockaddr *addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002857 int flags)
2858{
2859 int rc;
2860
2861 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2862 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2863 return -1;
2864 }
2865
2866 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002867 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2868 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002869 return -1;
2870 }
2871
2872 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2873 addr_str, addr_str_len,
2874 port_str, port_str_len,
2875 flags);
2876
2877 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002878 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2879 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2880 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002881
2882 return rc;
2883}
2884
Alexander Couzensdd930a22020-07-18 16:45:25 +02002885const char *sgsn_sockaddr_to_strb(const struct sgsn_sockaddr *addr,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002886 char *buf, size_t buf_len)
2887{
Alexander Couzens92ef0c82020-10-02 00:15:43 +02002888 char portbuf[6];
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002889 const int portbuf_len = 6;
2890 OSMO_ASSERT(buf_len > portbuf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002891 buf_len -= portbuf_len;
Alexander Couzensdd930a22020-07-18 16:45:25 +02002892 if (sgsn_sockaddr_to_strs(buf, buf_len,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002893 portbuf, portbuf_len,
2894 addr,
2895 NI_NUMERICHOST | NI_NUMERICSERV))
2896 return NULL;
2897
2898 char *pos = buf + strnlen(buf, buf_len-1);
2899 size_t len = buf_len - (pos - buf);
2900
2901 snprintf(pos, len, " port %s", portbuf);
2902 buf[buf_len-1] = '\0';
2903
2904 return buf;
2905}
2906
Alexander Couzensdd930a22020-07-18 16:45:25 +02002907const char *sgsn_sockaddr_to_str(const struct sgsn_sockaddr *addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002908{
2909 static char buf[256];
Alexander Couzensdd930a22020-07-18 16:45:25 +02002910 const char *result = sgsn_sockaddr_to_strb(addr, buf, sizeof(buf));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002911 if (! result)
2912 return "(invalid)";
2913 return result;
2914}
2915
Alexander Couzensdd930a22020-07-18 16:45:25 +02002916int sgsn_sockaddr_cmp(const struct sgsn_sockaddr *a,
2917 const struct sgsn_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002918{
2919 if (a == b)
2920 return 0;
2921 if (!a)
2922 return -1;
2923 if (!b)
2924 return 1;
2925 if (a->l != b->l) {
2926 /* Lengths are not the same, but determine the order. Will
Alexander Couzensdd930a22020-07-18 16:45:25 +02002927 * anyone ever sort a list by sgsn_sockaddr though...? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002928 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2929 if (cmp == 0) {
2930 if (a->l < b->l)
2931 return -1;
2932 else
2933 return 1;
2934 }
2935 return cmp;
2936 }
2937 return memcmp(&a->a, &b->a, a->l);
2938}
2939
Alexander Couzensdd930a22020-07-18 16:45:25 +02002940void sgsn_sockaddr_copy(struct sgsn_sockaddr *dst,
2941 const struct sgsn_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002942{
2943 OSMO_ASSERT(src->l <= sizeof(dst->a));
2944 memcpy(&dst->a, &src->a, src->l);
2945 dst->l = src->l;
2946}