blob: b0e75cad28a07046561df41182702e6709a2efc2 [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <string.h>
23#include <errno.h>
24#include <inttypes.h>
25#include <time.h>
26#include <limits.h>
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +010027#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#include <gtp.h>
33#include <gtpie.h>
34
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020035#include <osmocom/sgsn/gtphub.h>
36#include <osmocom/sgsn/debug.h>
37#include <osmocom/sgsn/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020038
39#include <osmocom/core/utils.h>
40#include <osmocom/core/logging.h>
41#include <osmocom/core/socket.h>
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010042#include <osmocom/core/rate_ctr.h>
43#include <osmocom/core/stats.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020044
Harald Welte7e82b742017-08-12 13:43:54 +020045#include <osmocom/gsm/apn.h>
46
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010047
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020048static const int GTPH_GC_TICK_SECONDS = 1;
49
50void *osmo_gtphub_ctx;
51
Neels Hofmeyr063a8022015-11-16 14:35:13 +010052/* Convenience makro, note: only within this C file. */
53#define LOG(level, fmt, args...) \
54 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020055
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010056#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
57 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020058
59/* TODO move this to osmocom/core/select.h ? */
60typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
61
62/* TODO move this to osmocom/core/linuxlist.h ? */
63#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010064#define llist_first(head, type, entry) \
65 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020066
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +010067#define __llist_last(head) (((head)->next == (head)) ? NULL : (head)->prev)
68#define llist_last(head, type, entry) \
69 llist_entry(__llist_last(head), type, entry)
70
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020071/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
72
73enum gtp_rc {
74 GTP_RC_UNKNOWN = 0,
75 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010076 GTP_RC_PDU_C = 2, /* a real packet with IEs */
77 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020078
79 GTP_RC_TOOSHORT = -1,
80 GTP_RC_UNSUPPORTED_VERSION = -2,
81 GTP_RC_INVALID_IE = -3,
82};
83
84struct gtp_packet_desc {
85 union gtp_packet *data;
86 int data_len;
87 int header_len;
88 int version;
89 uint8_t type;
90 uint16_t seq;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010091 uint32_t header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020092 uint32_t header_tei;
93 int rc; /* enum gtp_rc */
94 unsigned int plane_idx;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010095 unsigned int side_idx;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +010096 struct gtphub_tunnel *tun;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010097 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020098 union gtpie_member *ie[GTPIE_SIZE];
99};
100
Neels Hofmeyr10fc0242015-12-01 00:23:45 +0100101struct pending_delete {
102 struct llist_head entry;
103 struct expiring_item expiry_entry;
104
105 struct gtphub_tunnel *tun;
106 uint8_t teardown_ind;
107 uint8_t nsapi;
108};
109
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100110
111/* counters */
112
113enum gtphub_counters_io {
114 GTPH_CTR_PKTS_IN = 0,
115 GTPH_CTR_PKTS_OUT,
116 GTPH_CTR_BYTES_IN,
117 GTPH_CTR_BYTES_OUT
118};
119
120static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
121 { "packets.in", "Packets ( In)" },
122 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100123 { "bytes.in", "Bytes ( In)" },
124 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100125};
126
127static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
Max8a01a802017-12-20 13:10:11 +0100128 .group_name_prefix = "gtphub:bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100129 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100130 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
131 .ctr_desc = gtphub_counters_io_desc,
132 .class_id = OSMO_STATS_CLASS_GLOBAL,
133};
134
135
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100136/* support */
137
138static const char *gtp_type_str(uint8_t type)
139{
140 switch (type) {
141 case 1:
142 return " (Echo Request)";
143 case 2:
144 return " (Echo Response)";
145 case 16:
146 return " (Create PDP Ctx Request)";
147 case 17:
148 return " (Create PDP Ctx Response)";
149 case 18:
150 return " (Update PDP Ctx Request)";
151 case 19:
152 return " (Update PDP Ctx Response)";
153 case 20:
154 return " (Delete PDP Ctx Request)";
155 case 21:
156 return " (Delete PDP Ctx Response)";
157 case 255:
158 return " (User Data)";
159 default:
160 return "";
161 }
162}
163
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200164void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
165{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100166 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200167}
168
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200169int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
170 const struct osmo_sockaddr *sa)
171{
172 char addr_str[256];
173 char port_str[6];
174
175 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
176 port_str, sizeof(port_str),
177 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
178 != 0) {
179 return -1;
180 }
181
182 if (port)
183 *port = atoi(port_str);
184
185 return gsn_addr_from_str(gsna, addr_str);
186}
187
188int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
189{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100190 if ((!gsna) || (!numeric_addr_str))
191 return -1;
192
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200193 int af = AF_INET;
194 gsna->len = 4;
195 const char *pos = numeric_addr_str;
196 for (; *pos; pos++) {
197 if (*pos == ':') {
198 af = AF_INET6;
199 gsna->len = 16;
200 break;
201 }
202 }
203
204 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
205 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100206 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
207 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200208 return -1;
209 }
210 return 0;
211}
212
213const char *gsn_addr_to_str(const struct gsn_addr *gsna)
214{
215 static char buf[INET6_ADDRSTRLEN + 1];
216 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
217}
218
219const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
220 char *strbuf,
221 int strbuf_len)
222{
223 int af;
224 switch (gsna->len) {
225 case 4:
226 af = AF_INET;
227 break;
228 case 16:
229 af = AF_INET6;
230 break;
231 default:
232 return NULL;
233 }
234
235 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
236 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100237 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
238 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100239 strerror(errno),
240 (int)gsna->len,
241 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200242 }
243 return r;
244}
245
246int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
247{
248 if (a == b)
249 return 1;
250 if ((!a) || (!b))
251 return 0;
252 if (a->len != b->len)
253 return 0;
254 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
255}
256
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100257static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
258 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100260 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200261 return -1;
262
263 unsigned int len;
264 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
265 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
266 &len, gsna->buf, sizeof(gsna->buf))
267 != 0)
268 return -1;
269 gsna->len = len;
270 return 0;
271}
272
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100273static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
274 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200275{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100276 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200277 return -1;
278
279 int ie_idx;
280 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
281
282 if (ie_idx < 0)
283 return -1;
284
285 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
286 int ie_l = ntoh16(ie->l);
287 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100288 LOG(LOGL_ERROR, "Not implemented:"
289 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200290 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
291 return -1;
292 }
293
294 memcpy(ie->v, gsna->buf, (int)ie_l);
295 return 0;
296}
297
298/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
299 */
300void validate_gtp0_header(struct gtp_packet_desc *p)
301{
302 const struct gtp0_header *pheader = &(p->data->gtp0.h);
303 p->rc = GTP_RC_UNKNOWN;
304 p->header_len = 0;
305
306 OSMO_ASSERT(p->data_len >= 1);
307 OSMO_ASSERT(p->version == 0);
308
309 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100310 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200311 p->rc = GTP_RC_TOOSHORT;
312 return;
313 }
314
315 p->type = ntoh8(pheader->type);
316 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100317 p->header_tei_rx = 0; /* TODO */
318 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200319
320 if (p->data_len == GTP0_HEADER_SIZE) {
321 p->rc = GTP_RC_TINY;
322 p->header_len = GTP0_HEADER_SIZE;
323 return;
324 }
325
326 /* Check packet length field versus length of packet */
327 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100328 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
329 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100330 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
331 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200332 p->rc = GTP_RC_TOOSHORT;
333 return;
334 }
335
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100336 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200337 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100338 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200339}
340
341/* Validate GTP version 1 data, and update p->rc with the result, as well as
342 * p->header_len in case of a valid header. */
343void validate_gtp1_header(struct gtp_packet_desc *p)
344{
345 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
346 p->rc = GTP_RC_UNKNOWN;
347 p->header_len = 0;
348
349 OSMO_ASSERT(p->data_len >= 1);
350 OSMO_ASSERT(p->version == 1);
351
352 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
353 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100354 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200355 p->rc = GTP_RC_TOOSHORT;
356 return;
357 }
358
359 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100360 p->header_tei_rx = ntoh32(pheader->tei);
361 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200362 p->seq = ntoh16(pheader->seq);
363
Neels Hofmeyr28a70f22015-12-06 15:22:54 +0100364 LOG(LOGL_DEBUG, "| GTPv1\n");
365 LOG(LOGL_DEBUG, "| type = %" PRIu8 " 0x%02" PRIx8 "\n", p->type, p->type);
366 LOG(LOGL_DEBUG, "| length = %" PRIu16 " 0x%04" PRIx16 "\n", ntoh16(pheader->length), ntoh16(pheader->length));
367 LOG(LOGL_DEBUG, "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", p->header_tei_rx, p->header_tei_rx);
368 LOG(LOGL_DEBUG, "| seq = %" PRIu16 " 0x%04" PRIx16 "\n", p->seq, p->seq);
369 LOG(LOGL_DEBUG, "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->npdu, pheader->npdu);
370 LOG(LOGL_DEBUG, "| next = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->next, pheader->next);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200371
372 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
373 p->rc = GTP_RC_TINY;
374 p->header_len = GTP1_HEADER_SIZE_SHORT;
375 return;
376 }
377
378 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100379 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
380 if (p->data_len != announced_len) {
381 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
382 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100383 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
384 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200385 p->rc = GTP_RC_TOOSHORT;
386 return;
387 }
388
Neels Hofmeyre921e322015-11-11 00:45:50 +0100389 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200390 p->header_len = GTP1_HEADER_SIZE_LONG;
391}
392
393/* Examine whether p->data of size p->data_len has a valid GTP header. Set
394 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
395 * gtp_rc). p->data must point at a buffer with p->data_len set. */
396void validate_gtp_header(struct gtp_packet_desc *p)
397{
398 p->rc = GTP_RC_UNKNOWN;
399
400 /* Need at least 1 byte in order to check version */
401 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100402 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
403 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200404 p->rc = GTP_RC_TOOSHORT;
405 return;
406 }
407
408 p->version = p->data->flags >> 5;
409
410 switch (p->version) {
411 case 0:
412 validate_gtp0_header(p);
413 break;
414 case 1:
415 validate_gtp1_header(p);
416 break;
417 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100418 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200419 p->rc = GTP_RC_UNSUPPORTED_VERSION;
420 break;
421 }
422}
423
424
425/* Return the value of the i'th IMSI IEI by copying to *imsi.
426 * The first IEI is reached by passing i = 0.
427 * imsi must point at allocated space of (at least) 8 bytes.
428 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100429static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200430{
431 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
432}
433
434/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100435static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200436{
437 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
438}
439
440static char imsi_digit_to_char(uint8_t nibble)
441{
442 nibble &= 0x0f;
443 if (nibble > 9)
444 return (nibble == 0x0f) ? '\0' : '?';
445 return '0' + nibble;
446}
447
448/* Return a human readable IMSI string, in a static buffer.
449 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100450static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200451{
452 static char str[17];
453 int i;
454
455 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100456 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100457 c = imsi_digit_to_char(imsi[i]);
458 if (c == '?')
459 return -1;
460 str[2*i] = c;
461
462 c = imsi_digit_to_char(imsi[i] >> 4);
463 if (c == '?')
464 return -1;
465 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200466 }
467 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100468 *imsi_str = str;
469 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200470}
471
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100472/* Return 0 if not present, 1 if present and decoded successfully, -1 if
473 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100474static int get_ie_imsi_str(union gtpie_member *ie[], int i,
475 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100476{
477 uint8_t imsi_buf[8];
478 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100479 return 0;
480 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100481}
482
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100483/* Return 0 if not present, 1 if present and decoded successfully, -1 if
484 * present but cannot be decoded. */
485static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100486{
487 static char apn_buf[GSM_APN_LENGTH];
488 unsigned int len;
489 if (gtpie_gettlv(ie, GTPIE_APN, 0,
490 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100491 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100492
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100493 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100494 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
495 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100496 return -1;
497 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100498
499 if (len > (sizeof(apn_buf) - 1))
500 len = sizeof(apn_buf) - 1;
501 apn_buf[len] = '\0';
502
Harald Welte7e82b742017-08-12 13:43:54 +0200503 *apn_str = osmo_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100504 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100505 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
506 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100507 return -1;
508 }
509 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100510}
511
512
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200513/* Validate header, and index information elements. Write decoded packet
514 * information to *res. res->data will point at the given data buffer. On
515 * error, p->rc is set <= 0 (see enum gtp_rc). */
516static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100517 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200518 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100519 struct gtp_packet_desc *res,
520 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200521{
522 ZERO_STRUCT(res);
523 res->data = (union gtp_packet*)data;
524 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100525 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200526 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100527 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200528
529 validate_gtp_header(res);
530
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100531 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200532 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100534 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200535
Neels Hofmeyre921e322015-11-11 00:45:50 +0100536 if (from_plane_idx == GTPH_PLANE_USER) {
537 res->rc = GTP_RC_PDU_U;
538 return;
539 }
540
541 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100542 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200543 return;
544 }
545
546 if (gtpie_decaps(res->ie, res->version,
547 (void*)(data + res->header_len),
548 res->data_len - res->header_len) != 0) {
549 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100550 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100551 " Dropping GTP packet%s.\n",
552 gtp_type_str(res->type)
553 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200554 return;
555 }
556
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100557#if 1
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100558 /* TODO if (<loglevel is debug>)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100559 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200560 int i;
561
562 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100563 const char *imsi;
564 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200565 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100566 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200567 }
568
569 for (i = 0; i < 10; i++) {
570 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100571 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200572 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100573 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200574 }
575
576 for (i = 0; i < 2; i++) {
577 struct gsn_addr addr;
578 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100579 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200580 }
581
582 for (i = 0; i < 10; i++) {
583 uint32_t tei;
584 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
585 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100586 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200587 tei, tei);
588 }
589
590 for (i = 0; i < 10; i++) {
591 uint32_t tei;
592 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
593 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100594 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200595 tei, tei);
596 }
597#endif
598}
599
600
601/* expiry */
602
603void expiry_init(struct expiry *exq, int expiry_in_seconds)
604{
605 ZERO_STRUCT(exq);
606 exq->expiry_in_seconds = expiry_in_seconds;
607 INIT_LLIST_HEAD(&exq->items);
608}
609
610void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
611{
612 item->expiry = now + exq->expiry_in_seconds;
613
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100614 OSMO_ASSERT(llist_empty(&exq->items)
615 || (item->expiry
616 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
617
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200618 /* Add/move to the tail to always sort by expiry, ascending. */
619 llist_del(&item->entry);
620 llist_add_tail(&item->entry, &exq->items);
621}
622
623int expiry_tick(struct expiry *exq, time_t now)
624{
625 int expired = 0;
626 struct expiring_item *m, *n;
627 llist_for_each_entry_safe(m, n, &exq->items, entry) {
628 if (m->expiry <= now) {
629 expiring_item_del(m);
630 expired ++;
631 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200632 /* The items are added sorted by expiry. So when we hit
633 * an unexpired entry, only more unexpired ones will
634 * follow. */
635 break;
636 }
637 }
638 return expired;
639}
640
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100641void expiry_clear(struct expiry *exq)
642{
643 struct expiring_item *m, *n;
644 llist_for_each_entry_safe(m, n, &exq->items, entry) {
645 expiring_item_del(m);
646 }
647}
648
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200649void expiring_item_init(struct expiring_item *item)
650{
651 ZERO_STRUCT(item);
652 INIT_LLIST_HEAD(&item->entry);
653}
654
655void expiring_item_del(struct expiring_item *item)
656{
657 OSMO_ASSERT(item);
658 llist_del(&item->entry);
659 INIT_LLIST_HEAD(&item->entry);
660 if (item->del_cb) {
661 /* avoid loops */
662 del_cb_t del_cb = item->del_cb;
663 item->del_cb = 0;
664 (del_cb)(item);
665 }
666}
667
668
669/* nr_map, nr_pool */
670
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100671void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200672{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100673 *pool = (struct nr_pool){
674 .nr_min = nr_min,
675 .nr_max = nr_max,
676 .last_nr = nr_max
677 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200678}
679
680nr_t nr_pool_next(struct nr_pool *pool)
681{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100682 if (pool->last_nr >= pool->nr_max)
683 pool->last_nr = pool->nr_min;
684 else
685 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200686
687 return pool->last_nr;
688}
689
690void nr_map_init(struct nr_map *map, struct nr_pool *pool,
691 struct expiry *exq)
692{
693 ZERO_STRUCT(map);
694 map->pool = pool;
695 map->add_items_to_expiry = exq;
696 INIT_LLIST_HEAD(&map->mappings);
697}
698
699void nr_mapping_init(struct nr_mapping *m)
700{
701 ZERO_STRUCT(m);
702 INIT_LLIST_HEAD(&m->entry);
703 expiring_item_init(&m->expiry_entry);
704}
705
706void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
707{
708 /* Generate a mapped number */
709 mapping->repl = nr_pool_next(map->pool);
710
711 /* Add to the tail to always yield a list sorted by expiry, in
712 * ascending order. */
713 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100714 nr_map_refresh(map, mapping, now);
715}
716
717void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
718{
719 if (!map->add_items_to_expiry)
720 return;
721 expiry_add(map->add_items_to_expiry,
722 &mapping->expiry_entry,
723 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200724}
725
726void nr_map_clear(struct nr_map *map)
727{
728 struct nr_mapping *m;
729 struct nr_mapping *n;
730 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
731 nr_mapping_del(m);
732 }
733}
734
735int nr_map_empty(const struct nr_map *map)
736{
737 return llist_empty(&map->mappings);
738}
739
740struct nr_mapping *nr_map_get(const struct nr_map *map,
741 void *origin, nr_t nr_orig)
742{
743 struct nr_mapping *mapping;
744 llist_for_each_entry(mapping, &map->mappings, entry) {
745 if ((mapping->origin == origin)
746 && (mapping->orig == nr_orig))
747 return mapping;
748 }
749 /* Not found. */
750 return NULL;
751}
752
753struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
754{
755 struct nr_mapping *mapping;
756 llist_for_each_entry(mapping, &map->mappings, entry) {
757 if (mapping->repl == nr_repl) {
758 return mapping;
759 }
760 }
761 /* Not found. */
762 return NULL;
763}
764
765void nr_mapping_del(struct nr_mapping *mapping)
766{
767 OSMO_ASSERT(mapping);
768 llist_del(&mapping->entry);
769 INIT_LLIST_HEAD(&mapping->entry);
770 expiring_item_del(&mapping->expiry_entry);
771}
772
773
774/* gtphub */
775
776const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
777 "CTRL",
778 "USER",
779};
780
781const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
782 2123,
783 2152,
784};
785
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100786const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
787 "SGSN",
788 "GGSN",
789};
790
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200791time_t gtphub_now(void)
792{
793 struct timespec now_tp;
794 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
795 return now_tp.tv_sec;
796}
797
798/* Remove a gtphub_peer from its list and free it. */
799static void gtphub_peer_del(struct gtphub_peer *peer)
800{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100801 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200802 nr_map_clear(&peer->seq_map);
803 llist_del(&peer->entry);
804 talloc_free(peer);
805}
806
807static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
808{
809 OSMO_ASSERT(llist_empty(&pa->ports));
810 llist_del(&pa->entry);
811 talloc_free(pa);
812}
813
814static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
815{
816 OSMO_ASSERT(pp->ref_count == 0);
817 llist_del(&pp->entry);
Neels Hofmeyre38fb662015-12-06 16:44:14 +0100818 rate_ctr_group_free(pp->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200819 talloc_free(pp);
820}
821
822/* From the information in the gtp_packet_desc, return the address of a GGSN.
823 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100824static int gtphub_resolve_ggsn(struct gtphub *hub,
825 struct gtp_packet_desc *p,
826 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200827
828/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100829struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
830 const char *imsi_str,
831 const char *apn_ni_str);
832int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200833
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200834static void gtphub_zero(struct gtphub *hub)
835{
836 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100837 INIT_LLIST_HEAD(&hub->ggsn_lookups);
838 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200839}
840
841static int gtphub_sock_init(struct osmo_fd *ofd,
842 const struct gtphub_cfg_addr *addr,
843 osmo_fd_cb_t cb,
844 void *data,
845 int ofd_id)
846{
847 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100848 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200849 return -1;
850 }
851 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100852 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200853 return -1;
854 }
855
856 ofd->when = BSC_FD_READ;
857 ofd->cb = cb;
858 ofd->data = data;
859 ofd->priv_nr = ofd_id;
860
861 int rc;
862 rc = osmo_sock_init_ofd(ofd,
863 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
864 addr->addr_str, addr->port,
865 OSMO_SOCK_F_BIND);
866 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100867 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
868 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200869 return -1;
870 }
871
872 return 0;
873}
874
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100875static void gtphub_sock_close(struct osmo_fd *ofd)
876{
877 close(ofd->fd);
878 osmo_fd_unregister(ofd);
879 ofd->cb = NULL;
880}
881
Maxbb6b00a2017-12-20 12:14:50 +0100882static void gtphub_bind_init(struct gtphub_bind *b, uint32_t idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200883{
884 ZERO_STRUCT(b);
885
886 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100887
888 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
Maxbb6b00a2017-12-20 12:14:50 +0100889 &gtphub_ctrg_io_desc, idx);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100890 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200891}
892
893static int gtphub_bind_start(struct gtphub_bind *b,
894 const struct gtphub_cfg_bind *cfg,
895 osmo_fd_cb_t cb, void *cb_data,
896 unsigned int ofd_id)
897{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100898 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
899 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
900 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
901 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200902 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100903 }
904 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
905 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
906 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200907 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100908 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100909 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200910 return 0;
911}
912
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100913static void gtphub_bind_free(struct gtphub_bind *b)
914{
915 OSMO_ASSERT(llist_empty(&b->peers));
Maxbb6b00a2017-12-20 12:14:50 +0100916 if (b->counters_io)
917 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100918}
919
920static void gtphub_bind_stop(struct gtphub_bind *b) {
921 gtphub_sock_close(&b->ofd);
922 gtphub_bind_free(b);
923}
924
Neels Hofmeyr40348972015-11-17 12:22:28 +0100925/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200926 * Return the number of bytes read, zero on error. */
927static int gtphub_read(const struct osmo_fd *from,
928 struct osmo_sockaddr *from_addr,
929 uint8_t *buf, size_t buf_len)
930{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100931 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200932
Neels Hofmeyr40348972015-11-17 12:22:28 +0100933 /* recvfrom requires the available length set in *from_addr_len. */
934 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200935 errno = 0;
936 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100937 (struct sockaddr*)&from_addr->a,
938 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200939 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
940 * is not truncated. Then maybe reduce buf's size. */
941
942 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100943 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
944 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200945 return 0;
946 }
947
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100948 LOG(LOGL_DEBUG, "Received %d bytes from %s: %s%s\n",
Neels Hofmeyr40348972015-11-17 12:22:28 +0100949 (int)received, osmo_sockaddr_to_str(from_addr),
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100950 osmo_hexdump(buf, received > 1000? 1000 : received),
951 received > 1000 ? "..." : "");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200952
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200953 return received;
954}
955
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100956static inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200957{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100958 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200959 OSMO_ASSERT(pp->ref_count < UINT_MAX);
960 pp->ref_count++;
961}
962
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100963static inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200964{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100965 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200966 OSMO_ASSERT(pp->ref_count > 0);
967 pp->ref_count--;
968}
969
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100970static inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200971{
972 OSMO_ASSERT(p->version == 1);
973 p->data->gtp1l.h.seq = hton16(seq);
974 p->seq = seq;
975}
976
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100977static inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200978{
979 OSMO_ASSERT(p->version == 1);
980 p->data->gtp1l.h.tei = hton32(tei);
981 p->header_tei = tei;
982}
983
984static void gtphub_mapping_del_cb(struct expiring_item *expi);
985
986static struct nr_mapping *gtphub_mapping_new()
987{
988 struct nr_mapping *nrm;
989 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
990 OSMO_ASSERT(nrm);
991
992 nr_mapping_init(nrm);
993 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
994 return nrm;
995}
996
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100997
998#define APPEND(args...) \
999 l = snprintf(pos, left, args); \
1000 pos += l; \
1001 left -= l
1002
1003static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
1004 int side_idx)
1005{
1006 static char buf[256];
1007 char *pos = buf;
1008 int left = sizeof(buf);
1009 int l;
1010
1011 struct gtphub_tunnel_endpoint *c, *u;
1012 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1013 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
1014
1015 /* print both only if they differ. */
1016 if (!c->peer) {
1017 APPEND("(uninitialized)");
1018 } else {
1019 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
1020 }
1021
1022 if (!u->peer) {
1023 if (c->peer) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001024 APPEND("/(uninitialized)");
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001025 }
1026 } else if ((!c->peer)
1027 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1028 &c->peer->peer_addr->addr))) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001029 APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr));
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001030 }
1031
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001032 APPEND(" (TEI C=%x U=%x)",
1033 c->tei_orig,
1034 u->tei_orig);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001035 return buf;
1036}
1037
1038const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1039{
1040 static char buf[512];
1041 char *pos = buf;
1042 int left = sizeof(buf);
1043 int l;
1044
Neels Hofmeyrea619f12016-12-16 13:09:00 +01001045 if (!tun)
1046 return "null-tunnel";
1047
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001048 APPEND("TEI=%x: ", tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001049 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1050 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1051
1052 return buf;
1053}
1054
1055#undef APPEND
1056
1057void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1058 struct gtphub_peer_port *pp)
1059{
1060 if (te->peer)
1061 gtphub_port_ref_count_dec(te->peer);
1062 te->peer = pp;
1063 if (te->peer)
1064 gtphub_port_ref_count_inc(te->peer);
1065}
1066
1067int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1068{
1069 if (!tun)
1070 return 0;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001071 if (!tun->tei_repl)
1072 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001073 int side_idx;
1074 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001075 for_each_side_and_plane(side_idx, plane_idx) {
1076 struct gtphub_tunnel_endpoint *te =
1077 &tun->endpoint[side_idx][plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001078 if (!(te->peer && te->tei_orig))
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001079 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001080 }
1081 return 1;
1082}
1083
1084static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1085{
1086 struct gtphub_tunnel *tun = container_of(expi,
1087 struct gtphub_tunnel,
1088 expiry_entry);
1089 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1090
1091 llist_del(&tun->entry);
1092 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1093
1094 expi->del_cb = 0; /* avoid recursion loops */
1095 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1096
1097 int side_idx;
1098 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001099 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001100 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1101
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001102 /* clear ref count */
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001103 gtphub_tunnel_endpoint_set_peer(te, NULL);
1104
1105 rate_ctr_group_free(te->counters_io);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001106 }
1107
1108 talloc_free(tun);
1109}
1110
Maxbb6b00a2017-12-20 12:14:50 +01001111#define CTR_IDX(s, p, a, b) (a + s + (p + b) * 2)
1112
1113/* rate counter index for tunnels: [3; 6] */
1114#define CTR_IDX_TUN(s, p) CTR_IDX(s, p, 1, 1)
1115
1116/* rate counter index for hubs: [7; 10] */
1117#define CTR_IDX_HUB(s, p) CTR_IDX(s, p, 3, 2)
1118
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001119static struct gtphub_tunnel *gtphub_tunnel_new()
1120{
1121 struct gtphub_tunnel *tun;
1122 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1123 OSMO_ASSERT(tun);
1124
1125 INIT_LLIST_HEAD(&tun->entry);
1126 expiring_item_init(&tun->expiry_entry);
1127
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001128 int side_idx, plane_idx;
1129 for_each_side_and_plane(side_idx, plane_idx) {
1130 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1131 te->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
1132 &gtphub_ctrg_io_desc,
Maxbb6b00a2017-12-20 12:14:50 +01001133 CTR_IDX_TUN(side_idx, plane_idx));
1134 if (!te->counters_io)
1135 return NULL;
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001136 }
1137
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001138 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1139 return tun;
1140}
1141
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001142static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1143 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001144{
1145 if (llist_empty(&peer->addresses))
1146 return "(addressless)";
1147
1148 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1149 struct gtphub_peer_addr,
1150 entry);
1151 return gsn_addr_to_strb(&a->addr, buf, buflen);
1152}
1153
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001154static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1155 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001156{
1157 if (!port)
1158 return "(null port)";
1159
1160 snprintf(buf, buflen, "%s port %d",
1161 gsn_addr_to_str(&port->peer_addr->addr),
1162 (int)port->port);
1163 return buf;
1164}
1165
1166const char *gtphub_peer_str(struct gtphub_peer *peer)
1167{
1168 static char buf[256];
1169 return gtphub_peer_strb(peer, buf, sizeof(buf));
1170}
1171
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001172const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001173{
1174 static char buf[256];
1175 return gtphub_port_strb(port, buf, sizeof(buf));
1176}
1177
1178static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1179{
1180 static char buf[256];
1181 return gtphub_port_strb(port, buf, sizeof(buf));
1182}
1183
1184static void gtphub_mapping_del_cb(struct expiring_item *expi)
1185{
1186 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001187 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001188
1189 struct nr_mapping *nrm = container_of(expi,
1190 struct nr_mapping,
1191 expiry_entry);
1192 llist_del(&nrm->entry);
1193 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1194
1195 /* Just for log */
1196 struct gtphub_peer_port *from = nrm->origin;
1197 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001198 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001199 (int)nrm->expiry_entry.expiry,
1200 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001201 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001202
1203 gtphub_port_ref_count_dec(from);
1204
1205 talloc_free(nrm);
1206}
1207
1208static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1209 struct gtphub_peer_port *from,
1210 nr_t orig_nr,
1211 time_t now)
1212{
1213 struct nr_mapping *nrm;
1214
1215 nrm = nr_map_get(map, from, orig_nr);
1216
1217 if (!nrm) {
1218 nrm = gtphub_mapping_new();
1219 nrm->orig = orig_nr;
1220 nrm->origin = from;
1221 nr_map_add(map, nrm, now);
1222 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001223 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001224 gtphub_port_str(from),
1225 (int)(nrm->orig), (int)(nrm->repl));
1226 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001227 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001228 }
1229
1230 OSMO_ASSERT(nrm);
1231 return nrm;
1232}
1233
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001234static void gtphub_map_seq(struct gtp_packet_desc *p,
1235 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001236 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001237{
1238 /* Store a mapping in to_peer's map, so when we later receive a GTP
1239 * packet back from to_peer, the seq nr can be unmapped back to its
1240 * origin (from_peer here). */
1241 struct nr_mapping *nrm;
1242 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001243 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001244
1245 /* Change the GTP packet to yield the new, mapped seq nr */
1246 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001247}
1248
1249static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1250 struct gtphub_peer_port *responding_port)
1251{
1252 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001253 struct nr_mapping *nrm =
1254 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1255 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001256 if (!nrm)
1257 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001258 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1259 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001260 set_seq(p, nrm->orig);
1261 return nrm->origin;
1262}
1263
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001264static int gtphub_check_mapped_tei(struct gtphub_tunnel *new_tun,
1265 struct gtphub_tunnel *iterated_tun,
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001266 uint32_t *tei_min,
1267 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001268{
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001269 if (!new_tun->tei_repl || !iterated_tun->tei_repl)
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001270 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001271
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001272 *tei_min = (*tei_min < iterated_tun->tei_repl)? *tei_min : iterated_tun->tei_repl;
1273 *tei_max = (*tei_max > iterated_tun->tei_repl)? *tei_max : iterated_tun->tei_repl;
1274
1275 if (new_tun->tei_repl != iterated_tun->tei_repl)
1276 return 1;
1277
1278 /* new_tun->tei_repl is already taken. Try to find one out of the known
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001279 * range. */
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001280 LOG(LOGL_DEBUG, "TEI replacement %d already taken.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001281
1282 if ((*tei_max) < 0xffffffff) {
1283 (*tei_max)++;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001284 new_tun->tei_repl = *tei_max;
1285 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001286 return 1;
1287 } else if ((*tei_min) > 1) {
1288 (*tei_min)--;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001289 new_tun->tei_repl = *tei_min;
1290 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001291 return 1;
1292 }
1293
1294 /* None seems to be available. */
1295 return 0;
1296}
1297
1298static int gtphub_check_reused_teis(struct gtphub *hub,
1299 struct gtphub_tunnel *new_tun)
1300{
1301 uint32_t tei_min = 0xffffffff;
1302 uint32_t tei_max = 0;
1303 int side_idx;
1304 int plane_idx;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001305 struct gtphub_tunnel_endpoint *te;
1306 struct gtphub_tunnel_endpoint *te2;
1307
1308 struct gtphub_tunnel *tun, *ntun;
1309
1310 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1311 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001312 continue;
1313
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001314 /* Check whether the GSN sent a TEI that it is reusing from a
1315 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001316 int tun_continue = 0;
1317 for_each_side(side_idx) {
1318 for_each_plane(plane_idx) {
1319 te = &tun->endpoint[side_idx][plane_idx];
1320 te2 = &new_tun->endpoint[side_idx][plane_idx];
Neels Hofmeyrf6e4d082015-12-06 19:03:35 +01001321 if ((te->tei_orig == 0)
1322 || (te->tei_orig != te2->tei_orig)
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001323 || (!te->peer)
1324 || (!te2->peer)
1325 || !gsn_addr_same(&te->peer->peer_addr->addr,
1326 &te2->peer->peer_addr->addr))
1327 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001328
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001329 /* The peer is reusing a TEI that I believe to
1330 * be part of another tunnel. The other tunnel
1331 * must be stale, then. */
1332 LOG(LOGL_NOTICE,
1333 "Expiring tunnel due to reused TEI:"
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001334 " %s peer %s sent %s TEI %x,"
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001335 " previously used by tunnel %s...\n",
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001336 gtphub_side_idx_names[side_idx],
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001337 gtphub_port_str(te->peer),
1338 gtphub_plane_idx_names[plane_idx],
1339 te->tei_orig,
1340 gtphub_tunnel_str(tun));
1341 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1342 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001343
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001344 expiring_item_del(&tun->expiry_entry);
1345 /* continue to find more matches. There shouldn't be
1346 * any, but let's make sure. However, tun is deleted,
1347 * so we need to skip to the next tunnel. */
1348 tun_continue = 1;
1349 break;
1350 }
1351 if (tun_continue)
1352 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001353 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001354 if (tun_continue)
1355 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001356
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001357 /* Check whether the mapped TEI is already used by another
1358 * tunnel. */
1359 if (!gtphub_check_mapped_tei(new_tun, tun, &tei_min, &tei_max)) {
1360 LOG(LOGL_ERROR,
1361 "No mapped TEI is readily available."
1362 " Searching for holes between occupied"
1363 " TEIs not implemented.");
1364 return 0;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001365 }
1366
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001367 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001368
1369 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001370}
1371
1372static void gtphub_tunnel_refresh(struct gtphub *hub,
1373 struct gtphub_tunnel *tun,
1374 time_t now)
1375{
1376 expiry_add(&hub->expire_slowly,
1377 &tun->expiry_entry,
1378 now);
1379}
1380
1381static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1382 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001383 struct gtphub_peer_port *from,
1384 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001385{
1386 OSMO_ASSERT(from);
1387 int other_side = other_side_idx(p->side_idx);
1388
1389 struct gtphub_tunnel *tun;
1390 llist_for_each_entry(tun, &hub->tunnels, entry) {
1391 struct gtphub_tunnel_endpoint *te_from =
1392 &tun->endpoint[p->side_idx][p->plane_idx];
1393 struct gtphub_tunnel_endpoint *te_to =
1394 &tun->endpoint[other_side][p->plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001395 if ((tun->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001396 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001397 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1398 &from->peer_addr->addr)) {
1399 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001400 if (unmapped_from_tun)
1401 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001402 return te_to;
1403 }
1404 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001405
1406 if (unmapped_from_tun)
1407 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001408 return NULL;
1409}
1410
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001411static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001412 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001413{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001414 if (p->rc != GTP_RC_PDU_C)
1415 return;
1416
1417 int ie_idx;
1418 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1419 if (ie_idx < 0)
1420 return;
1421
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001422 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001423 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001424}
1425
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001426static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001427 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001428 struct gtphub *hub,
1429 struct gtp_packet_desc *p,
1430 struct gtphub_peer_port *from_port)
1431{
1432 OSMO_ASSERT(p->version == 1);
1433 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001434 if (unmapped_from_tun)
1435 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001436
1437 /* If the header's TEI is zero, no PDP context has been established
1438 * yet. If nonzero, a mapping should actually already exist for this
1439 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001440 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001441 return 0;
1442
1443 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001444 * mapped in a tunnel struct. */
1445 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001446 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001447 if (!to) {
1448 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1449 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001450 return -1;
1451 }
Neels Hofmeyr312bf6c2016-04-14 15:21:31 +02001452
1453 if (unmapped_from_tun) {
1454 OSMO_ASSERT(*unmapped_from_tun);
1455 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1456 gtphub_tunnel_str(*unmapped_from_tun));
1457 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001458
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001459 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001460 set_tei(p, unmapped_tei);
1461
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001462 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001463 *to_port_p = to->peer;
1464
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001465 return 0;
1466}
1467
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001468static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1469 struct gtp_packet_desc *p,
1470 struct gtphub_peer_port *from_ctrl,
1471 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001472{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001473 int plane_idx;
1474
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001475 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1476 plane_nrs_match_GSN_addr_IE_indices);
1477
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001478 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001479
1480 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001481 if (p->side_idx != GTPH_SIDE_SGSN) {
1482 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001483 " Request from the GGSN side: %s",
1484 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001485 return -1;
1486 }
1487
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001488 if (tun) {
1489 LOG(LOGL_ERROR, "Not implemented: Received"
1490 " Create PDP Context Request for an already"
1491 " established tunnel:"
1492 " from %s, tunnel %s\n",
1493 gtphub_port_str(from_ctrl),
1494 gtphub_tunnel_str(p->tun));
1495 return -1;
1496 }
1497
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001498 /* A new tunnel. */
Maxbb6b00a2017-12-20 12:14:50 +01001499 tun = gtphub_tunnel_new();
1500 if (!tun) {
1501 LOG(LOGL_ERROR, "Failed to allocate new tunnel %s <-> %s\n",
1502 gtphub_port_str(from_ctrl), gtphub_port_str(to_ctrl));
1503 return -1;
1504 }
1505 p->tun = tun;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001506
1507 /* Create TEI mapping */
1508 tun->tei_repl = nr_pool_next(&hub->tei_pool);
1509
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001510 llist_add(&tun->entry, &hub->tunnels);
1511 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001512 /* The endpoint peers on this side (SGSN) will be set from IEs
1513 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001514 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001515 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001516 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001517 if (p->side_idx != GTPH_SIDE_GGSN) {
1518 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001519 " Response from the SGSN side: %s",
1520 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001521 return -1;
1522 }
1523
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001524 /* The tunnel should already have been resolved from the header
1525 * TEI and be available in tun (== p->tun). Just fill in the
1526 * GSN Addresses below.*/
1527 OSMO_ASSERT(tun);
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001528 OSMO_ASSERT(tun->tei_repl == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001529 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001530 }
1531
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001532 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1533 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001534 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001535
Maxe9bcbe12017-12-20 17:01:05 +01001536 for_each_plane(plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001537 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001538 struct gsn_addr use_addr;
1539 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001540 uint32_t tei_from_ie;
1541 int ie_idx;
1542
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001543 /* Fetch GSN Address and TEI from IEs. As ensured by above
1544 * static asserts, plane_idx corresponds to the GSN Address IE
1545 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001546 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001547 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001548 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1549 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550 return -1;
1551 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001552 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001553 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001554 gsn_addr_to_str(&use_addr),
1555 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001556
1557 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1558 if (ie_idx < 0) {
1559 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001560 LOG(LOGL_ERROR,
1561 "Create PDP Context message invalid:"
1562 " missing IE %d\n",
1563 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001564 return -1;
1565 }
1566 tei_from_ie = 0;
1567 }
1568 else
1569 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1570
1571 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001572 * exists.
1573 *
1574 * Exception: if sgsn_use_sender is set, instead use the
1575 * sender's address and port for Ctrl -- the User port is not
1576 * known until the first User packet arrives.
1577 *
1578 * Note: doing this here is just an optimization, because
1579 * gtphub_handle_buf() has code to replace the tunnel
1580 * endpoints' addresses with the sender (needed for User
1581 * plane). We could just ignore sgsn_use_sender here. But if we
1582 * set up a default port here and replace it in
1583 * gtphub_handle_buf(), we'd be creating a peer port just to
1584 * expire it right away. */
1585 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
1586 gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1587 } else {
1588 use_port = gtphub_plane_idx_default_port[plane_idx];
1589
1590 }
1591
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001592 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001593 peer_from_ie = gtphub_port_have(hub,
1594 &hub->to_gsns[side_idx][plane_idx],
1595 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001596
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001597 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1598 peer_from_ie);
1599
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001600 if (!tei_from_ie &&
1601 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1602 LOG(LOGL_ERROR,
Neels Hofmeyr328d2f72015-12-06 19:13:21 +01001603 "Create PDP Context message omits %s TEI, but"
1604 " no TEI has been announced for this tunnel: %s\n",
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001605 gtphub_plane_idx_names[plane_idx],
1606 gtphub_tunnel_str(tun));
1607 return -1;
1608 }
1609
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001610 if (tei_from_ie) {
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001611 /* Replace TEI in GTP packet IE */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001612 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001613 p->ie[ie_idx]->tv4.v = hton32(tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001614
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001615 if (!gtphub_check_reused_teis(hub, tun)) {
1616 /* It's highly unlikely that all TEIs are
1617 * taken. But the code looking for an unused
1618 * TEI is, at the time of writing this comment,
1619 * not able to find gaps in the TEI space. To
1620 * explicitly alert the user of this problem,
1621 * rather abort than carry on. */
1622 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1623 abort();
1624 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001625 }
1626
1627 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001628 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1629 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001630 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001631 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1632 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001633 return -1;
1634 }
1635 }
1636
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001637 if (p->type == GTP_CREATE_PDP_REQ) {
1638 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1639 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001640 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001641 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1642 gtphub_tunnel_str(tun));
1643 }
1644
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001645 return 0;
1646}
1647
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001648static void pending_delete_del_cb(struct expiring_item *expi)
1649{
1650 struct pending_delete *pd;
1651 pd = container_of(expi, struct pending_delete, expiry_entry);
1652
1653 llist_del(&pd->entry);
1654 INIT_LLIST_HEAD(&pd->entry);
1655
1656 pd->expiry_entry.del_cb = 0;
1657 expiring_item_del(&pd->expiry_entry);
1658
1659 talloc_free(pd);
1660}
1661
1662static struct pending_delete *pending_delete_new(void)
1663{
1664 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1665 INIT_LLIST_HEAD(&pd->entry);
1666 expiring_item_init(&pd->expiry_entry);
1667 pd->expiry_entry.del_cb = pending_delete_del_cb;
1668 return pd;
1669}
1670
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001671static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1672 struct gtp_packet_desc *p,
1673 struct gtphub_peer_port *from_ctrl,
1674 struct gtphub_peer_port *to_ctrl)
1675{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001676 struct gtphub_tunnel *known_tun = p->tun;
1677
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001678 if (p->type == GTP_DELETE_PDP_REQ) {
1679 if (!known_tun) {
1680 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1681 return -1;
1682 }
1683
1684 /* Store the Delete Request until a successful Response is seen. */
1685 uint8_t teardown_ind;
1686 uint8_t nsapi;
1687
1688 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1689 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1690 return -1;
1691 }
1692
1693 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1694 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1695 return -1;
1696 }
1697
1698 struct pending_delete *pd = NULL;
1699
1700 struct pending_delete *pdi = NULL;
1701 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1702 if ((pdi->tun == known_tun)
1703 && (pdi->teardown_ind == teardown_ind)
1704 && (pdi->nsapi == nsapi)) {
1705 pd = pdi;
1706 break;
1707 }
1708 }
1709
1710 if (!pd) {
1711 pd = pending_delete_new();
1712 pd->tun = known_tun;
1713 pd->teardown_ind = teardown_ind;
1714 pd->nsapi = nsapi;
1715
1716 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1717 gtphub_tunnel_str(known_tun));
1718 llist_add(&pd->entry, &hub->pending_deletes);
1719 }
1720
1721 /* Add or refresh timeout. */
1722 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1723
1724 /* If a pending_delete should expire before the response to
1725 * indicate success comes in, the responding peer will have the
1726 * tunnel deactivated, while the requesting peer gets no reply
1727 * and keeps the tunnel. The hope is that the requesting peer
1728 * will try again and get a useful response. */
1729 } else if (p->type == GTP_DELETE_PDP_RSP) {
1730 /* Find the Delete Request for this Response. */
1731 struct pending_delete *pd = NULL;
1732
1733 struct pending_delete *pdi;
1734 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1735 if (known_tun == pdi->tun) {
1736 pd = pdi;
1737 break;
1738 }
1739 }
1740
1741 if (!pd) {
1742 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1743 " Cannot find matching request.");
1744 /* If we delete the tunnel now, anyone can send a
1745 * Delete response to kill tunnels at will. */
1746 return -1;
1747 }
1748
1749 /* TODO handle teardown_ind and nsapi */
1750
1751 expiring_item_del(&pd->expiry_entry);
1752
1753 uint8_t cause;
1754 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1755 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1756 " Missing Cause IE.");
1757 /* If we delete the tunnel now, at least one of the
1758 * peers may still think it is active. */
1759 return -1;
1760 }
1761
1762 if (cause != GTPCAUSE_ACC_REQ) {
1763 LOG(LOGL_NOTICE,
1764 "Delete PDP Context Response indicates failure;"
1765 "for %s\n",
1766 gtphub_tunnel_str(known_tun));
1767 return -1;
1768 }
1769
1770 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1771 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001772 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001773 expiring_item_del(&known_tun->expiry_entry);
1774 }
1775
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001776 return 0;
1777}
1778
1779static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1780 struct gtp_packet_desc *p,
1781 struct gtphub_peer_port *from_ctrl,
1782 struct gtphub_peer_port *to_ctrl)
1783{
1784 /* TODO */
1785 return 0;
1786}
1787
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001788/* Read GSN address IEs from p, and make sure these peer addresses exist in
1789 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1790 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1791 * the packet p. */
1792static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1793 struct gtp_packet_desc *p,
1794 struct gtphub_peer_port *from_ctrl,
1795 struct gtphub_peer_port *to_ctrl)
1796{
1797 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1798
1799 switch (p->type) {
1800 case GTP_CREATE_PDP_REQ:
1801 case GTP_CREATE_PDP_RSP:
1802 return gtphub_handle_create_pdp_ctx(hub, p,
1803 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001804
1805 case GTP_DELETE_PDP_REQ:
1806 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001807 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001808 from_ctrl, to_ctrl);
1809
1810 case GTP_UPDATE_PDP_REQ:
1811 case GTP_UPDATE_PDP_RSP:
1812 return gtphub_handle_update_pdp_ctx(hub, p,
1813 from_ctrl, to_ctrl);
1814
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001815 default:
1816 /* Nothing to do for this message type. */
1817 return 0;
1818 }
1819
1820}
1821
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001822static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1823 struct gtphub_tunnel *tun,
1824 int to_side)
1825{
1826 static uint8_t del_ctx_msg[16] = {
1827 0x32, /* GTP v1 flags */
1828 GTP_DELETE_PDP_REQ,
1829 0x00, 0x08, /* Length in network byte order */
1830 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1831 0, 0, /* Seq, to be replaced */
1832 0, 0, /* no extensions */
1833 0x13, 0xff, /* 19: Teardown ind = 1 */
1834 0x14, 0 /* 20: NSAPI = 0 */
1835 };
1836
1837 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1838 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1839
1840 struct gtphub_tunnel_endpoint *te =
1841 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1842
1843 if (! te->peer)
1844 return 0;
1845
1846 *tei = hton32(te->tei_orig);
1847 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1848
1849 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001850 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1851 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001852 if (rc != 0) {
1853 LOG(LOGL_ERROR,
1854 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1855 gtphub_port_str(te->peer));
1856 }
1857 return rc;
1858}
1859
1860/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1861static void gtphub_restarted(struct gtphub *hub,
1862 struct gtp_packet_desc *p,
1863 struct gtphub_peer_port *pp)
1864{
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001865 LOG(LOGL_NOTICE, "Peer has restarted: %s\n",
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001866 gtphub_port_str(pp));
1867
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001868 int deleted_count = 0;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001869 struct gtphub_tunnel *tun;
1870 llist_for_each_entry(tun, &hub->tunnels, entry) {
1871 int side_idx;
1872 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001873 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1874 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1875 if ((!te->peer)
1876 || (!te2->tei_orig)
1877 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001878 continue;
1879
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001880 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1881 gtphub_tunnel_str(tun));
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001882 deleted_count ++;
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001883
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001884 /* Send a Delete PDP Context Request to the
1885 * peer on the other side, remember the pending
1886 * delete and wait for the response to delete
1887 * the tunnel. Clear this side of the tunnel to
1888 * make sure it isn't used.
1889 *
1890 * Should the delete message send fail, or if no
1891 * response is received, this tunnel will expire. If
1892 * its TEIs come up in a new PDP Context Request, it
1893 * will be removed. If messages for this tunnel should
1894 * come in (from the not restarted side), they will be
1895 * dropped because the tunnel is rendered unusable. */
1896 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1897
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001898 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1899 NULL);
1900 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1901 NULL);
1902 }
1903 }
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001904
1905 if (deleted_count)
1906 LOG(LOGL_NOTICE, "Deleting %d tunnels due to restart of: %s\n",
1907 deleted_count,
1908 gtphub_port_str(pp));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001909}
1910
1911static int get_restart_count(struct gtp_packet_desc *p)
1912{
1913 int ie_idx;
1914 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1915 if (ie_idx < 0)
1916 return -1;
1917 return ntoh8(p->ie[ie_idx]->tv1.v);
1918}
1919
1920static void gtphub_check_restart_counter(struct gtphub *hub,
1921 struct gtp_packet_desc *p,
1922 struct gtphub_peer_port *from)
1923{
1924 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1925 * that doesn't match the peer's previously sent restart counter, clear
1926 * that peer and cancel PDP contexts. */
1927
1928 int restart = get_restart_count(p);
1929
1930 if ((restart < 0) || (restart > 255))
1931 return;
1932
1933 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1934 if (from->last_restart_count != restart) {
1935 gtphub_restarted(hub, p, from);
1936 }
1937 }
1938
1939 from->last_restart_count = restart;
1940}
1941
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001942static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1943{
1944 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1945 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001946 LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n",
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001947 gtphub_plane_idx_names[plane_idx]);
1948
1949 if (!(what & BSC_FD_READ))
1950 return 0;
1951
1952 struct gtphub *hub = from_sgsns_ofd->data;
1953
1954 static uint8_t buf[4096];
1955 struct osmo_sockaddr from_addr;
1956 struct osmo_sockaddr to_addr;
1957 struct osmo_fd *to_ofd;
1958 int len;
1959 uint8_t *reply_buf;
1960
1961 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1962 if (len < 1)
1963 return 0;
1964
1965 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1966 buf, len, gtphub_now(),
1967 &reply_buf, &to_ofd, &to_addr);
1968 if (len < 1)
1969 return 0;
1970
1971 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1972}
1973
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001974static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1975{
1976 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1977 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001978 LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001979 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001980 if (!(what & BSC_FD_READ))
1981 return 0;
1982
1983 struct gtphub *hub = from_ggsns_ofd->data;
1984
1985 static uint8_t buf[4096];
1986 struct osmo_sockaddr from_addr;
1987 struct osmo_sockaddr to_addr;
1988 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001989 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001990 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001991
1992 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1993 if (len < 1)
1994 return 0;
1995
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001996 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1997 buf, len, gtphub_now(),
1998 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001999 if (len < 1)
2000 return 0;
2001
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002002 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002003}
2004
2005static int gtphub_unmap(struct gtphub *hub,
2006 struct gtp_packet_desc *p,
2007 struct gtphub_peer_port *from,
2008 struct gtphub_peer_port *to_proxy,
2009 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002010 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002011{
2012 /* Always (try to) unmap sequence and TEI numbers, which need to be
2013 * replaced in the packet. Either way, give precedence to the proxy, if
2014 * configured. */
2015
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002016 if (unmapped_from_seq)
2017 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002018 if (final_unmapped)
2019 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002020 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002021
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002022 struct gtphub_peer_port *from_seq = NULL;
2023 struct gtphub_peer_port *from_tei = NULL;
2024 struct gtphub_peer_port *unmapped = NULL;
2025
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002026 from_seq = gtphub_unmap_seq(p, from);
2027
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002028 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002029 return -1;
2030
2031 struct gtphub_peer *from_peer = from->peer_addr->peer;
2032 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002033 LOG(LOGL_DEBUG,
2034 "Seq unmap and TEI unmap yield two different peers."
2035 " Using seq unmap."
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01002036 " (from %s %s: seq %d yields %s, tei %u yields %s)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002037 gtphub_plane_idx_names[p->plane_idx],
2038 gtphub_peer_str(from_peer),
2039 (int)p->seq,
2040 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002041 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002042 gtphub_port_str2(from_tei)
2043 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002044 }
2045 unmapped = (from_seq? from_seq : from_tei);
2046
2047 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002048 LOG(LOGL_NOTICE,
2049 "Unmap yields a different peer than the configured proxy."
2050 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002051 " unmapped: %s proxy: %s\n",
2052 gtphub_port_str(unmapped),
2053 gtphub_port_str2(to_proxy)
2054 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002055 }
2056 unmapped = (to_proxy? to_proxy : unmapped);
2057
2058 if (!unmapped) {
2059 /* Return no error, but returned pointers are all NULL. */
2060 return 0;
2061 }
2062
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002063 if (unmapped_from_seq)
2064 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002065 if (final_unmapped)
2066 *final_unmapped = unmapped;
2067 return 0;
2068}
2069
2070static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2071 uint16_t port,
2072 struct osmo_sockaddr *dst)
2073{
2074 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2075}
2076
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002077/* If p is an Echo request, replace p's data with the matching response and
2078 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2079 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002080static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2081 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002082{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002083 if (p->type != GTP_ECHO_REQ)
2084 return 0;
2085
2086 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002087 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002088 GTP_ECHO_RSP,
2089 0x00, 14 - 8, /* Length in network byte order */
2090 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2091 0, 0, /* Seq, to be replaced */
2092 0, 0, /* no extensions */
2093 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002094 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002095 };
2096 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2097 uint8_t *recovery = &echo_response_data[13];
2098
2099 *seq = hton16(p->seq);
2100 *recovery = hub->restart_counter;
2101
2102 *reply_buf = echo_response_data;
2103
2104 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002105}
2106
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002107struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2108 const struct osmo_sockaddr *addr);
2109
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002110/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2111 * address to forward to. Return a pointer to the osmo_fd, but copy the
2112 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2113 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2114 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002115int gtphub_handle_buf(struct gtphub *hub,
2116 unsigned int side_idx,
2117 unsigned int plane_idx,
2118 const struct osmo_sockaddr *from_addr,
2119 uint8_t *buf,
2120 size_t received,
2121 time_t now,
2122 uint8_t **reply_buf,
2123 struct osmo_fd **to_ofd,
2124 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002125{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002126 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2127 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002128
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002129 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2130 received);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002131
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002132 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002133 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002134
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002135 LOG(LOGL_DEBUG, "%s rx %s from %s %s%s\n",
2136 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
2137 gtphub_plane_idx_names[plane_idx],
2138 gtphub_side_idx_names[side_idx],
2139 osmo_sockaddr_to_str(from_addr),
2140 gtp_type_str(p.type));
2141
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002142 if (p.rc <= 0) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002143 LOG(LOGL_ERROR, "INVALID: dropping GTP packet%s from %s %s %s\n",
2144 gtp_type_str(p.type),
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002145 gtphub_side_idx_names[side_idx],
2146 gtphub_plane_idx_names[plane_idx],
2147 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002148 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002149 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002150
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002151 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2152
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002153 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002154 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002155 if (reply_len > 0) {
2156 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002157 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002158 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002159
2160 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2161 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2162 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002163 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2164 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2165 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002166 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002167 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002168 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002169 if (reply_len < 0)
2170 return -1;
2171
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002172 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002174 /* If a proxy is configured, check that it's indeed that proxy talking
2175 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2176 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002177 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2178 if (from_peer) {
2179 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002180 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002181 "Rejecting: %s proxy configured, but GTP packet"
2182 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002183 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002184 gtphub_side_idx_names[side_idx],
2185 gtphub_side_idx_names[side_idx],
2186 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002187 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002188 return -1;
2189 }
2190 }
2191
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002192 if (!from_peer) {
2193 /* Find or create a peer with a matching address. The sender's
2194 * port may in fact differ. */
2195 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002196 }
2197
2198 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002199 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2200 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002201 if (!from_peer) {
2202 if (side_idx == GTPH_SIDE_GGSN) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002203 LOG(LOGL_ERROR, "Dropping packet%s: unknown GGSN peer: %s\n",
2204 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002205 osmo_sockaddr_to_str(from_addr));
2206 return -1;
2207 } else {
2208 /* SGSN */
2209 /* A new peer. If this is on the Ctrl plane, an SGSN
2210 * may make first contact without being known yet, so
2211 * create the peer struct for the current sender. */
2212 if (plane_idx != GTPH_PLANE_CTRL) {
2213 LOG(LOGL_ERROR,
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002214 "Dropping packet%s: User plane peer was not"
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002215 "announced by PDP Context: %s\n",
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002216 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002217 osmo_sockaddr_to_str(from_addr));
2218 return -1;
2219 }
2220
2221 struct gsn_addr from_gsna;
2222 uint16_t from_port;
2223 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2224 return -1;
2225
2226 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2227 }
2228 }
2229
2230 if (!from_peer) {
2231 /* This could theoretically happen for invalid address data or
2232 * somesuch. */
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002233 LOG(LOGL_ERROR, "Dropping packet%s: invalid %s peer: %s\n",
2234 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002235 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002236 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002237 return -1;
2238 }
2239
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002240 rate_ctr_add(&from_peer->counters_io->ctr[GTPH_CTR_BYTES_IN],
2241 received);
2242 rate_ctr_inc(&from_peer->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2243
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002244 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2245 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002246
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002247 gtphub_check_restart_counter(hub, &p, from_peer);
2248 gtphub_map_restart_counter(hub, &p);
2249
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002250 struct gtphub_peer_port *to_peer_from_seq;
2251 struct gtphub_peer_port *to_peer;
2252 if (gtphub_unmap(hub, &p, from_peer,
2253 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002254 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002255 != 0) {
2256 return -1;
2257 }
2258
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002259 if (p.tun) {
2260 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[p.side_idx][p.plane_idx];
2261 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_IN],
2262 received);
2263 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2264 }
2265
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002266 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2267 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2268 return -1;
2269 }
2270
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002271 if (!to_peer && p.tun && p.type == GTP_DELETE_PDP_RSP) {
2272 /* It's a delete confirmation for a tunnel that is partly
2273 * invalid, probably marked unsuable due to a restarted peer.
2274 * Remove the tunnel and be happy without forwarding. */
2275 expiring_item_del(&p.tun->expiry_entry);
2276 p.tun = NULL;
2277 return 0;
2278 }
2279
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002280 if (!to_peer) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002281 LOG(LOGL_ERROR, "No %s to send to. Dropping packet%s"
2282 " (type=%" PRIu8 ", header-TEI=%" PRIx32 ", seq=%" PRIx16 ").\n",
2283 gtphub_side_idx_names[other_side_idx(side_idx)],
2284 gtp_type_str(p.type),
2285 p.type, p.header_tei_rx, p.seq
2286 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002287 return -1;
2288 }
2289
2290 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002291 /* This may be a Create PDP Context response. If it is, there
2292 * are other addresses in the GTP message to set up apart from
2293 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002294 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002295 != 0)
2296 return -1;
2297 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002298
2299 /* Either to_peer was resolved from an existing tunnel,
2300 * or a PDP Ctx and thus a tunnel has just been created,
2301 * or the tunnel has been deleted due to this message. */
2302 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002303
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002304 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002305 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002306 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002307 if (!to_peer_from_seq)
2308 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002309
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002310 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002311
2312 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002313
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002314 if (received) {
2315 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2316 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2317 received);
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002318
2319 rate_ctr_inc(&to_peer->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2320 rate_ctr_add(&to_peer->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2321 received);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002322 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002323
2324 if (p.tun) {
2325 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[other_side_idx(p.side_idx)][p.plane_idx];
2326 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2327 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2328 received);
2329 }
2330
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002331 LOG(LOGL_DEBUG, "%s Forward to %s:"
2332 " header-TEI %" PRIx32", seq %" PRIx16", %d bytes to %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002333 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2334 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002335 p.header_tei, p.seq,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002336 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002337 return received;
2338}
2339
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002340static void resolved_gssn_del_cb(struct expiring_item *expi)
2341{
2342 struct gtphub_resolved_ggsn *ggsn;
2343 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2344
2345 gtphub_port_ref_count_dec(ggsn->peer);
2346 llist_del(&ggsn->entry);
2347
2348 ggsn->expiry_entry.del_cb = 0;
2349 expiring_item_del(&ggsn->expiry_entry);
2350
2351 talloc_free(ggsn);
2352}
2353
2354void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2355 struct gsn_addr *resolved_addr,
2356 time_t now)
2357{
2358 struct gtphub_peer_port *pp;
2359 struct gtphub_resolved_ggsn *ggsn;
2360
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002361 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002362 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2363 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002364
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002365 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002366 resolved_addr, 2123);
2367 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002368 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2369 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002370 return;
2371 }
2372
2373 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2374 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002375 INIT_LLIST_HEAD(&ggsn->entry);
2376 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002377
2378 ggsn->peer = pp;
2379 gtphub_port_ref_count_inc(pp);
2380
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002381 osmo_strlcpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002382
2383 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002384 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002385
2386 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2387}
2388
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002389static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2390{
2391 return pp->ref_count == 0;
2392}
2393
2394static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2395{
2396 struct gtphub_peer_port *pp, *npp;
2397 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2398 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002399 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002400 gtphub_port_str(pp));
2401 gtphub_peer_port_del(pp);
2402 }
2403 }
2404 return llist_empty(&pa->ports);
2405}
2406
2407static int gtphub_gc_peer(struct gtphub_peer *p)
2408{
2409 struct gtphub_peer_addr *pa, *npa;
2410 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2411 if (gtphub_gc_peer_addr(pa)) {
2412 gtphub_peer_addr_del(pa);
2413 }
2414 }
2415
2416 /* Note that there's a ref_count in each gtphub_peer_port instance
2417 * listed within p->addresses, referenced by TEI mappings from
2418 * hub->tei_map. As long as those don't expire, this peer will stay. */
2419
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002420 return llist_empty(&p->addresses)
2421 && nr_map_empty(&p->seq_map);
2422}
2423
2424static void gtphub_gc_bind(struct gtphub_bind *b)
2425{
2426 struct gtphub_peer *p, *n;
2427 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2428 if (gtphub_gc_peer(p)) {
2429 gtphub_peer_del(p);
2430 }
2431 }
2432}
2433
2434void gtphub_gc(struct gtphub *hub, time_t now)
2435{
2436 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002437 expired = expiry_tick(&hub->expire_quickly, now);
2438 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002439
2440 /* ... */
2441
2442 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002443 int s, p;
2444 for_each_side_and_plane(s, p) {
2445 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002446 }
2447 }
2448}
2449
2450static void gtphub_gc_cb(void *data)
2451{
2452 struct gtphub *hub = data;
2453 gtphub_gc(hub, gtphub_now());
2454 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2455}
2456
2457static void gtphub_gc_start(struct gtphub *hub)
2458{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02002459 osmo_timer_setup(&hub->gc_timer, gtphub_gc_cb, hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002460 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2461}
2462
2463/* called by unit tests */
2464void gtphub_init(struct gtphub *hub)
2465{
2466 gtphub_zero(hub);
2467
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002468 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002469 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002470
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002471 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2472 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002473
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002474 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2475
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002476 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002477 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002478 for_each_side_and_plane(side_idx, plane_idx) {
Maxbb6b00a2017-12-20 12:14:50 +01002479 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx], CTR_IDX_HUB(side_idx, plane_idx));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002480 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002481
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002482 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2483 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2484 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2485 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002486}
2487
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002488/* For the test suite, this is kept separate from gtphub_stop(), which also
2489 * closes sockets. The test suite avoids using sockets and would cause
2490 * segfaults when trying to close uninitialized ofds. */
2491void gtphub_free(struct gtphub *hub)
2492{
2493 /* By expiring all mappings, a garbage collection should free
2494 * everything else. A gtphub_bind_free() will assert that everything is
2495 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002496 expiry_clear(&hub->expire_quickly);
2497 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002498
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002499 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002500 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002501 for_each_side_and_plane(side_idx, plane_idx) {
2502 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2503 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002504 }
2505}
2506
2507void gtphub_stop(struct gtphub *hub)
2508{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002509 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002510 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002511 for_each_side_and_plane(side_idx, plane_idx) {
2512 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002513 }
2514 gtphub_free(hub);
2515}
2516
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002517static int gtphub_make_proxy(struct gtphub *hub,
2518 struct gtphub_peer_port **pp,
2519 struct gtphub_bind *bind,
2520 const struct gtphub_cfg_addr *addr)
2521{
2522 if (!addr->addr_str)
2523 return 0;
2524
2525 struct gsn_addr gsna;
2526 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2527 return -1;
2528
2529 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2530
2531 /* This is *the* proxy. Make sure it is never expired. */
2532 gtphub_port_ref_count_inc(*pp);
2533 return 0;
2534}
2535
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002536int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2537 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002538{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002539 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002540
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002541 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002542 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002543
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002544 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002545 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002546 if (gtphub_ares_init(hub) != 0) {
2547 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2548 return -1;
2549 }
2550 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002551
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002552 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002553 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002554 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002555 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002556 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2557 &cfg->to_gsns[side_idx][plane_idx],
2558 (side_idx == GTPH_SIDE_SGSN)
2559 ? from_sgsns_read_cb
2560 : from_ggsns_read_cb,
2561 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002562 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002563 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2564 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002565 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002566 return rc;
2567 }
2568 }
2569
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002570 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002571 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002572 &hub->proxy[side_idx][plane_idx],
2573 &hub->to_gsns[side_idx][plane_idx],
2574 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002575 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002576 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002577 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002578 gtphub_side_idx_names[side_idx],
2579 cfg->proxy[side_idx][plane_idx].addr_str,
2580 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002581 return -1;
2582 }
2583 }
2584
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002585 for_each_side_and_plane(side_idx, plane_idx) {
2586 if (hub->proxy[side_idx][plane_idx])
2587 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2588 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002589 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002590 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002591 }
2592
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002593 if (hub->sgsn_use_sender)
Neels Hofmeyr9d8f5062015-12-03 14:52:33 +01002594 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 +01002595
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002596 gtphub_gc_start(hub);
2597 return 0;
2598}
2599
2600static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2601 const struct gsn_addr *addr)
2602{
2603 struct gtphub_peer_addr *a;
2604 llist_for_each_entry(a, &peer->addresses, entry) {
2605 if (gsn_addr_same(&a->addr, addr))
2606 return a;
2607 }
2608 return NULL;
2609}
2610
2611static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2612 uint16_t port)
2613{
2614 OSMO_ASSERT(port);
2615 struct gtphub_peer_port *pp;
2616 llist_for_each_entry(pp, &a->ports, entry) {
2617 if (pp->port == port)
2618 return pp;
2619 }
2620 return NULL;
2621}
2622
2623static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2624 const struct gsn_addr *addr)
2625{
2626 struct gtphub_peer *peer;
2627 llist_for_each_entry(peer, &bind->peers, entry) {
2628 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2629 if (a)
2630 return a;
2631 }
2632 return NULL;
2633}
2634
2635static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2636 const struct gsn_addr *addr,
2637 uint16_t port)
2638{
2639 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2640 if (!a)
2641 return NULL;
2642 return gtphub_addr_find_port(a, port);
2643}
2644
2645struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2646 const struct osmo_sockaddr *addr)
2647{
2648 struct gsn_addr gsna;
2649 uint16_t port;
2650 gsn_addr_from_sockaddr(&gsna, &port, addr);
2651 return gtphub_port_find(bind, &gsna, port);
2652}
2653
2654static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2655 struct gtphub_bind *bind)
2656{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002657 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2658 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002659 OSMO_ASSERT(peer);
2660
2661 INIT_LLIST_HEAD(&peer->addresses);
2662
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002663 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002664 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002665
2666 /* TODO use something random to pick the initial sequence nr.
2667 0x6d31 produces the ASCII character sequence 'm1', currently used in
2668 gtphub_nc_test.sh. */
2669 peer->seq_pool.last_nr = 0x6d31 - 1;
2670
2671 llist_add(&peer->entry, &bind->peers);
2672 return peer;
2673}
2674
2675static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2676 const struct gsn_addr *addr)
2677{
2678 struct gtphub_peer_addr *a;
2679 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2680 OSMO_ASSERT(a);
2681 a->peer = peer;
2682 gsn_addr_copy(&a->addr, addr);
2683 INIT_LLIST_HEAD(&a->ports);
2684 llist_add(&a->entry, &peer->addresses);
2685
2686 return a;
2687}
2688
2689static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2690 struct gtphub_bind *bind,
2691 const struct gsn_addr *addr)
2692{
2693 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2694 if (a)
2695 return a;
2696
2697 /* If we haven't found an address, that means we need to create an
2698 * entirely new peer for the new address. More addresses may be added
2699 * to this peer later, but not via this function. */
2700 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002701
2702 a = gtphub_peer_add_addr(peer, addr);
2703
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002704 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002705 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002706 gsn_addr_to_str(&a->addr));
2707
2708 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002709}
2710
2711static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2712 uint16_t port)
2713{
2714 struct gtphub_peer_port *pp;
2715
2716 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2717 OSMO_ASSERT(pp);
2718 pp->peer_addr = a;
2719 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002720 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002721
2722 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2723 talloc_free(pp);
2724 return NULL;
2725 }
2726
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002727 pp->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
Maxbb6b00a2017-12-20 12:14:50 +01002728 &gtphub_ctrg_io_desc, port);
Harald Welted939ac02017-07-17 23:59:39 +02002729 if (!pp->counters_io) {
Maxbb6b00a2017-12-20 12:14:50 +01002730 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 +02002731 talloc_free(pp);
2732 return NULL;
2733 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002734
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002735 llist_add(&pp->entry, &a->ports);
2736
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002737 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002738 gsn_addr_to_str(&a->addr),
2739 (int)port);
2740
2741 return pp;
2742}
2743
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002744struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2745 struct gtphub_bind *bind,
2746 const struct gsn_addr *addr,
2747 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002748{
2749 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2750
2751 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2752 if (pp)
2753 return pp;
2754
2755 return gtphub_addr_add_port(a, port);
2756}
2757
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002758/* Find a GGSN peer with a matching address. If the address is known but the
2759 * port not, create a new port for that peer address. */
2760struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2761 const struct osmo_sockaddr *addr)
2762{
2763 struct gtphub_peer_addr *pa;
2764 struct gtphub_peer_port *pp;
2765
2766 struct gsn_addr gsna;
2767 uint16_t port;
2768 gsn_addr_from_sockaddr(&gsna, &port, addr);
2769
2770 pa = gtphub_addr_find(bind, &gsna);
2771 if (!pa)
2772 return NULL;
2773
2774 pp = gtphub_addr_find_port(pa, port);
2775
2776 if (!pp)
2777 pp = gtphub_addr_add_port(pa, port);
2778
2779 return pp;
2780}
2781
2782
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002783/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2784 * resolution should be possible but failed, and 1 if resolution was
2785 * successful. *pp will be set to NULL if <1 is returned. */
2786static int gtphub_resolve_ggsn(struct gtphub *hub,
2787 struct gtp_packet_desc *p,
2788 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002789{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002790 *pp = NULL;
2791
2792 /* TODO determine from message type whether IEs should be present? */
2793
2794 int rc;
2795 const char *imsi_str;
2796 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2797 if (rc < 1)
2798 return rc;
2799 OSMO_ASSERT(imsi_str);
2800
2801 const char *apn_str;
2802 rc = get_ie_apn_str(p->ie, &apn_str);
2803 if (rc < 1)
2804 return rc;
2805 OSMO_ASSERT(apn_str);
2806
2807 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2808 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002809}
2810
2811
2812/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002813/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002814/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2815 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002816static int _osmo_getaddrinfo(struct addrinfo **result,
2817 uint16_t family, uint16_t type, uint8_t proto,
2818 const char *host, uint16_t port)
2819{
2820 struct addrinfo hints;
2821 char portbuf[16];
2822
2823 sprintf(portbuf, "%u", port);
2824 memset(&hints, '\0', sizeof(struct addrinfo));
2825 hints.ai_family = family;
2826 if (type == SOCK_RAW) {
2827 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2828 * SOCK_RAW and IPPROTO_GRE is used.
2829 */
2830 hints.ai_socktype = SOCK_DGRAM;
2831 hints.ai_protocol = IPPROTO_UDP;
2832 } else {
2833 hints.ai_socktype = type;
2834 hints.ai_protocol = proto;
2835 }
2836
2837 return getaddrinfo(host, portbuf, &hints, result);
2838}
2839
2840/* TODO move to osmocom/core/socket.c ? */
2841int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2842 uint16_t family, uint16_t type, uint8_t proto,
2843 const char *host, uint16_t port)
2844{
2845 struct addrinfo *res;
2846 int rc;
2847 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2848
2849 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002850 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002851 return -EINVAL;
2852 }
2853
2854 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2855 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2856 addr->l = res->ai_addrlen;
2857 freeaddrinfo(res);
2858
2859 return 0;
2860}
2861
2862int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2863 char *port_str, size_t port_str_len,
2864 const struct osmo_sockaddr *addr,
2865 int flags)
2866{
2867 int rc;
2868
2869 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2870 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2871 return -1;
2872 }
2873
2874 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002875 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2876 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002877 return -1;
2878 }
2879
2880 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2881 addr_str, addr_str_len,
2882 port_str, port_str_len,
2883 flags);
2884
2885 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002886 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2887 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2888 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002889
2890 return rc;
2891}
2892
2893const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2894 char *buf, size_t buf_len)
2895{
2896 const int portbuf_len = 6;
2897 OSMO_ASSERT(buf_len > portbuf_len);
2898 char *portbuf = buf + buf_len - portbuf_len;
2899 buf_len -= portbuf_len;
2900 if (osmo_sockaddr_to_strs(buf, buf_len,
2901 portbuf, portbuf_len,
2902 addr,
2903 NI_NUMERICHOST | NI_NUMERICSERV))
2904 return NULL;
2905
2906 char *pos = buf + strnlen(buf, buf_len-1);
2907 size_t len = buf_len - (pos - buf);
2908
2909 snprintf(pos, len, " port %s", portbuf);
2910 buf[buf_len-1] = '\0';
2911
2912 return buf;
2913}
2914
2915const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2916{
2917 static char buf[256];
2918 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2919 if (! result)
2920 return "(invalid)";
2921 return result;
2922}
2923
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002924int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2925 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002926{
2927 if (a == b)
2928 return 0;
2929 if (!a)
2930 return -1;
2931 if (!b)
2932 return 1;
2933 if (a->l != b->l) {
2934 /* Lengths are not the same, but determine the order. Will
2935 * anyone ever sort a list by osmo_sockaddr though...? */
2936 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2937 if (cmp == 0) {
2938 if (a->l < b->l)
2939 return -1;
2940 else
2941 return 1;
2942 }
2943 return cmp;
2944 }
2945 return memcmp(&a->a, &b->a, a->l);
2946}
2947
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002948void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2949 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002950{
2951 OSMO_ASSERT(src->l <= sizeof(dst->a));
2952 memcpy(&dst->a, &src->a, src->l);
2953 dst->l = src->l;
2954}