blob: 76eebb1bc1e91400ce32fcd285d009a41a86b9d9 [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
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200882static void gtphub_bind_init(struct gtphub_bind *b)
883{
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,
889 &gtphub_ctrg_io_desc, 0);
890 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));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100916 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100917}
918
919static void gtphub_bind_stop(struct gtphub_bind *b) {
920 gtphub_sock_close(&b->ofd);
921 gtphub_bind_free(b);
922}
923
Neels Hofmeyr40348972015-11-17 12:22:28 +0100924/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200925 * Return the number of bytes read, zero on error. */
926static int gtphub_read(const struct osmo_fd *from,
927 struct osmo_sockaddr *from_addr,
928 uint8_t *buf, size_t buf_len)
929{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100930 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200931
Neels Hofmeyr40348972015-11-17 12:22:28 +0100932 /* recvfrom requires the available length set in *from_addr_len. */
933 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200934 errno = 0;
935 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100936 (struct sockaddr*)&from_addr->a,
937 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200938 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
939 * is not truncated. Then maybe reduce buf's size. */
940
941 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100942 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
943 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200944 return 0;
945 }
946
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100947 LOG(LOGL_DEBUG, "Received %d bytes from %s: %s%s\n",
Neels Hofmeyr40348972015-11-17 12:22:28 +0100948 (int)received, osmo_sockaddr_to_str(from_addr),
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100949 osmo_hexdump(buf, received > 1000? 1000 : received),
950 received > 1000 ? "..." : "");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200951
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200952 return received;
953}
954
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100955static inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200956{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100957 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200958 OSMO_ASSERT(pp->ref_count < UINT_MAX);
959 pp->ref_count++;
960}
961
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100962static inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200963{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100964 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200965 OSMO_ASSERT(pp->ref_count > 0);
966 pp->ref_count--;
967}
968
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100969static inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200970{
971 OSMO_ASSERT(p->version == 1);
972 p->data->gtp1l.h.seq = hton16(seq);
973 p->seq = seq;
974}
975
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100976static inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200977{
978 OSMO_ASSERT(p->version == 1);
979 p->data->gtp1l.h.tei = hton32(tei);
980 p->header_tei = tei;
981}
982
983static void gtphub_mapping_del_cb(struct expiring_item *expi);
984
985static struct nr_mapping *gtphub_mapping_new()
986{
987 struct nr_mapping *nrm;
988 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
989 OSMO_ASSERT(nrm);
990
991 nr_mapping_init(nrm);
992 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
993 return nrm;
994}
995
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100996
997#define APPEND(args...) \
998 l = snprintf(pos, left, args); \
999 pos += l; \
1000 left -= l
1001
1002static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
1003 int side_idx)
1004{
1005 static char buf[256];
1006 char *pos = buf;
1007 int left = sizeof(buf);
1008 int l;
1009
1010 struct gtphub_tunnel_endpoint *c, *u;
1011 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1012 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
1013
1014 /* print both only if they differ. */
1015 if (!c->peer) {
1016 APPEND("(uninitialized)");
1017 } else {
1018 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
1019 }
1020
1021 if (!u->peer) {
1022 if (c->peer) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001023 APPEND("/(uninitialized)");
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001024 }
1025 } else if ((!c->peer)
1026 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1027 &c->peer->peer_addr->addr))) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001028 APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr));
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001029 }
1030
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001031 APPEND(" (TEI C=%x U=%x)",
1032 c->tei_orig,
1033 u->tei_orig);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001034 return buf;
1035}
1036
1037const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1038{
1039 static char buf[512];
1040 char *pos = buf;
1041 int left = sizeof(buf);
1042 int l;
1043
Neels Hofmeyrea619f12016-12-16 13:09:00 +01001044 if (!tun)
1045 return "null-tunnel";
1046
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001047 APPEND("TEI=%x: ", tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001048 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1049 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1050
1051 return buf;
1052}
1053
1054#undef APPEND
1055
1056void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1057 struct gtphub_peer_port *pp)
1058{
1059 if (te->peer)
1060 gtphub_port_ref_count_dec(te->peer);
1061 te->peer = pp;
1062 if (te->peer)
1063 gtphub_port_ref_count_inc(te->peer);
1064}
1065
1066int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1067{
1068 if (!tun)
1069 return 0;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001070 if (!tun->tei_repl)
1071 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001072 int side_idx;
1073 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001074 for_each_side_and_plane(side_idx, plane_idx) {
1075 struct gtphub_tunnel_endpoint *te =
1076 &tun->endpoint[side_idx][plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001077 if (!(te->peer && te->tei_orig))
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001078 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001079 }
1080 return 1;
1081}
1082
1083static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1084{
1085 struct gtphub_tunnel *tun = container_of(expi,
1086 struct gtphub_tunnel,
1087 expiry_entry);
1088 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1089
1090 llist_del(&tun->entry);
1091 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1092
1093 expi->del_cb = 0; /* avoid recursion loops */
1094 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1095
1096 int side_idx;
1097 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001098 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001099 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1100
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001101 /* clear ref count */
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001102 gtphub_tunnel_endpoint_set_peer(te, NULL);
1103
1104 rate_ctr_group_free(te->counters_io);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001105 }
1106
1107 talloc_free(tun);
1108}
1109
1110static struct gtphub_tunnel *gtphub_tunnel_new()
1111{
1112 struct gtphub_tunnel *tun;
1113 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1114 OSMO_ASSERT(tun);
1115
1116 INIT_LLIST_HEAD(&tun->entry);
1117 expiring_item_init(&tun->expiry_entry);
1118
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001119 int side_idx, plane_idx;
1120 for_each_side_and_plane(side_idx, plane_idx) {
1121 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1122 te->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
1123 &gtphub_ctrg_io_desc,
1124 0);
1125 OSMO_ASSERT(te->counters_io);
1126 }
1127
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001128 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1129 return tun;
1130}
1131
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001132static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1133 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001134{
1135 if (llist_empty(&peer->addresses))
1136 return "(addressless)";
1137
1138 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1139 struct gtphub_peer_addr,
1140 entry);
1141 return gsn_addr_to_strb(&a->addr, buf, buflen);
1142}
1143
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001144static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1145 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001146{
1147 if (!port)
1148 return "(null port)";
1149
1150 snprintf(buf, buflen, "%s port %d",
1151 gsn_addr_to_str(&port->peer_addr->addr),
1152 (int)port->port);
1153 return buf;
1154}
1155
1156const char *gtphub_peer_str(struct gtphub_peer *peer)
1157{
1158 static char buf[256];
1159 return gtphub_peer_strb(peer, buf, sizeof(buf));
1160}
1161
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001162const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001163{
1164 static char buf[256];
1165 return gtphub_port_strb(port, buf, sizeof(buf));
1166}
1167
1168static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1169{
1170 static char buf[256];
1171 return gtphub_port_strb(port, buf, sizeof(buf));
1172}
1173
1174static void gtphub_mapping_del_cb(struct expiring_item *expi)
1175{
1176 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001177 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001178
1179 struct nr_mapping *nrm = container_of(expi,
1180 struct nr_mapping,
1181 expiry_entry);
1182 llist_del(&nrm->entry);
1183 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1184
1185 /* Just for log */
1186 struct gtphub_peer_port *from = nrm->origin;
1187 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001188 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001189 (int)nrm->expiry_entry.expiry,
1190 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001191 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001192
1193 gtphub_port_ref_count_dec(from);
1194
1195 talloc_free(nrm);
1196}
1197
1198static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1199 struct gtphub_peer_port *from,
1200 nr_t orig_nr,
1201 time_t now)
1202{
1203 struct nr_mapping *nrm;
1204
1205 nrm = nr_map_get(map, from, orig_nr);
1206
1207 if (!nrm) {
1208 nrm = gtphub_mapping_new();
1209 nrm->orig = orig_nr;
1210 nrm->origin = from;
1211 nr_map_add(map, nrm, now);
1212 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001213 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001214 gtphub_port_str(from),
1215 (int)(nrm->orig), (int)(nrm->repl));
1216 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001217 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001218 }
1219
1220 OSMO_ASSERT(nrm);
1221 return nrm;
1222}
1223
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001224static void gtphub_map_seq(struct gtp_packet_desc *p,
1225 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001226 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001227{
1228 /* Store a mapping in to_peer's map, so when we later receive a GTP
1229 * packet back from to_peer, the seq nr can be unmapped back to its
1230 * origin (from_peer here). */
1231 struct nr_mapping *nrm;
1232 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001233 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001234
1235 /* Change the GTP packet to yield the new, mapped seq nr */
1236 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001237}
1238
1239static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1240 struct gtphub_peer_port *responding_port)
1241{
1242 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001243 struct nr_mapping *nrm =
1244 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1245 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001246 if (!nrm)
1247 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001248 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1249 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001250 set_seq(p, nrm->orig);
1251 return nrm->origin;
1252}
1253
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001254static int gtphub_check_mapped_tei(struct gtphub_tunnel *new_tun,
1255 struct gtphub_tunnel *iterated_tun,
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001256 uint32_t *tei_min,
1257 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001258{
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001259 if (!new_tun->tei_repl || !iterated_tun->tei_repl)
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001260 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001261
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001262 *tei_min = (*tei_min < iterated_tun->tei_repl)? *tei_min : iterated_tun->tei_repl;
1263 *tei_max = (*tei_max > iterated_tun->tei_repl)? *tei_max : iterated_tun->tei_repl;
1264
1265 if (new_tun->tei_repl != iterated_tun->tei_repl)
1266 return 1;
1267
1268 /* new_tun->tei_repl is already taken. Try to find one out of the known
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001269 * range. */
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001270 LOG(LOGL_DEBUG, "TEI replacement %d already taken.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001271
1272 if ((*tei_max) < 0xffffffff) {
1273 (*tei_max)++;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001274 new_tun->tei_repl = *tei_max;
1275 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001276 return 1;
1277 } else if ((*tei_min) > 1) {
1278 (*tei_min)--;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001279 new_tun->tei_repl = *tei_min;
1280 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001281 return 1;
1282 }
1283
1284 /* None seems to be available. */
1285 return 0;
1286}
1287
1288static int gtphub_check_reused_teis(struct gtphub *hub,
1289 struct gtphub_tunnel *new_tun)
1290{
1291 uint32_t tei_min = 0xffffffff;
1292 uint32_t tei_max = 0;
1293 int side_idx;
1294 int plane_idx;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001295 struct gtphub_tunnel_endpoint *te;
1296 struct gtphub_tunnel_endpoint *te2;
1297
1298 struct gtphub_tunnel *tun, *ntun;
1299
1300 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1301 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001302 continue;
1303
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001304 /* Check whether the GSN sent a TEI that it is reusing from a
1305 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001306 int tun_continue = 0;
1307 for_each_side(side_idx) {
1308 for_each_plane(plane_idx) {
1309 te = &tun->endpoint[side_idx][plane_idx];
1310 te2 = &new_tun->endpoint[side_idx][plane_idx];
Neels Hofmeyrf6e4d082015-12-06 19:03:35 +01001311 if ((te->tei_orig == 0)
1312 || (te->tei_orig != te2->tei_orig)
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001313 || (!te->peer)
1314 || (!te2->peer)
1315 || !gsn_addr_same(&te->peer->peer_addr->addr,
1316 &te2->peer->peer_addr->addr))
1317 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001318
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001319 /* The peer is reusing a TEI that I believe to
1320 * be part of another tunnel. The other tunnel
1321 * must be stale, then. */
1322 LOG(LOGL_NOTICE,
1323 "Expiring tunnel due to reused TEI:"
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001324 " %s peer %s sent %s TEI %x,"
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001325 " previously used by tunnel %s...\n",
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001326 gtphub_side_idx_names[side_idx],
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001327 gtphub_port_str(te->peer),
1328 gtphub_plane_idx_names[plane_idx],
1329 te->tei_orig,
1330 gtphub_tunnel_str(tun));
1331 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1332 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001333
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001334 expiring_item_del(&tun->expiry_entry);
1335 /* continue to find more matches. There shouldn't be
1336 * any, but let's make sure. However, tun is deleted,
1337 * so we need to skip to the next tunnel. */
1338 tun_continue = 1;
1339 break;
1340 }
1341 if (tun_continue)
1342 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001343 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001344 if (tun_continue)
1345 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001346
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001347 /* Check whether the mapped TEI is already used by another
1348 * tunnel. */
1349 if (!gtphub_check_mapped_tei(new_tun, tun, &tei_min, &tei_max)) {
1350 LOG(LOGL_ERROR,
1351 "No mapped TEI is readily available."
1352 " Searching for holes between occupied"
1353 " TEIs not implemented.");
1354 return 0;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001355 }
1356
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001357 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001358
1359 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001360}
1361
1362static void gtphub_tunnel_refresh(struct gtphub *hub,
1363 struct gtphub_tunnel *tun,
1364 time_t now)
1365{
1366 expiry_add(&hub->expire_slowly,
1367 &tun->expiry_entry,
1368 now);
1369}
1370
1371static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1372 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001373 struct gtphub_peer_port *from,
1374 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001375{
1376 OSMO_ASSERT(from);
1377 int other_side = other_side_idx(p->side_idx);
1378
1379 struct gtphub_tunnel *tun;
1380 llist_for_each_entry(tun, &hub->tunnels, entry) {
1381 struct gtphub_tunnel_endpoint *te_from =
1382 &tun->endpoint[p->side_idx][p->plane_idx];
1383 struct gtphub_tunnel_endpoint *te_to =
1384 &tun->endpoint[other_side][p->plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001385 if ((tun->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001386 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001387 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1388 &from->peer_addr->addr)) {
1389 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001390 if (unmapped_from_tun)
1391 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001392 return te_to;
1393 }
1394 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001395
1396 if (unmapped_from_tun)
1397 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001398 return NULL;
1399}
1400
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001401static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001402 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001403{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001404 if (p->rc != GTP_RC_PDU_C)
1405 return;
1406
1407 int ie_idx;
1408 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1409 if (ie_idx < 0)
1410 return;
1411
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001412 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001413 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414}
1415
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001416static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001417 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001418 struct gtphub *hub,
1419 struct gtp_packet_desc *p,
1420 struct gtphub_peer_port *from_port)
1421{
1422 OSMO_ASSERT(p->version == 1);
1423 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001424 if (unmapped_from_tun)
1425 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001426
1427 /* If the header's TEI is zero, no PDP context has been established
1428 * yet. If nonzero, a mapping should actually already exist for this
1429 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001430 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001431 return 0;
1432
1433 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001434 * mapped in a tunnel struct. */
1435 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001436 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001437 if (!to) {
1438 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1439 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001440 return -1;
1441 }
Neels Hofmeyr312bf6c2016-04-14 15:21:31 +02001442
1443 if (unmapped_from_tun) {
1444 OSMO_ASSERT(*unmapped_from_tun);
1445 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1446 gtphub_tunnel_str(*unmapped_from_tun));
1447 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001448
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001449 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001450 set_tei(p, unmapped_tei);
1451
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001452 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001453 *to_port_p = to->peer;
1454
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001455 return 0;
1456}
1457
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001458static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1459 struct gtp_packet_desc *p,
1460 struct gtphub_peer_port *from_ctrl,
1461 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001462{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001463 int plane_idx;
1464
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001465 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1466 plane_nrs_match_GSN_addr_IE_indices);
1467
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001468 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001469
1470 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001471 if (p->side_idx != GTPH_SIDE_SGSN) {
1472 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001473 " Request from the GGSN side: %s",
1474 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001475 return -1;
1476 }
1477
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001478 if (tun) {
1479 LOG(LOGL_ERROR, "Not implemented: Received"
1480 " Create PDP Context Request for an already"
1481 " established tunnel:"
1482 " from %s, tunnel %s\n",
1483 gtphub_port_str(from_ctrl),
1484 gtphub_tunnel_str(p->tun));
1485 return -1;
1486 }
1487
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001488 /* A new tunnel. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001489 p->tun = tun = gtphub_tunnel_new();
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001490
1491 /* Create TEI mapping */
1492 tun->tei_repl = nr_pool_next(&hub->tei_pool);
1493
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001494 llist_add(&tun->entry, &hub->tunnels);
1495 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001496 /* The endpoint peers on this side (SGSN) will be set from IEs
1497 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001498 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001499 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001500 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001501 if (p->side_idx != GTPH_SIDE_GGSN) {
1502 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001503 " Response from the SGSN side: %s",
1504 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001505 return -1;
1506 }
1507
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001508 /* The tunnel should already have been resolved from the header
1509 * TEI and be available in tun (== p->tun). Just fill in the
1510 * GSN Addresses below.*/
1511 OSMO_ASSERT(tun);
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001512 OSMO_ASSERT(tun->tei_repl == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001513 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001514 }
1515
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001516 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1517 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001518 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001519
Maxe9bcbe12017-12-20 17:01:05 +01001520 for_each_plane(plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001521 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001522 struct gsn_addr use_addr;
1523 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001524 uint32_t tei_from_ie;
1525 int ie_idx;
1526
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001527 /* Fetch GSN Address and TEI from IEs. As ensured by above
1528 * static asserts, plane_idx corresponds to the GSN Address IE
1529 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001530 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001531 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001532 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1533 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001534 return -1;
1535 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001536 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001537 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001538 gsn_addr_to_str(&use_addr),
1539 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001540
1541 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1542 if (ie_idx < 0) {
1543 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001544 LOG(LOGL_ERROR,
1545 "Create PDP Context message invalid:"
1546 " missing IE %d\n",
1547 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001548 return -1;
1549 }
1550 tei_from_ie = 0;
1551 }
1552 else
1553 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1554
1555 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001556 * exists.
1557 *
1558 * Exception: if sgsn_use_sender is set, instead use the
1559 * sender's address and port for Ctrl -- the User port is not
1560 * known until the first User packet arrives.
1561 *
1562 * Note: doing this here is just an optimization, because
1563 * gtphub_handle_buf() has code to replace the tunnel
1564 * endpoints' addresses with the sender (needed for User
1565 * plane). We could just ignore sgsn_use_sender here. But if we
1566 * set up a default port here and replace it in
1567 * gtphub_handle_buf(), we'd be creating a peer port just to
1568 * expire it right away. */
1569 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
1570 gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1571 } else {
1572 use_port = gtphub_plane_idx_default_port[plane_idx];
1573
1574 }
1575
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001576 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001577 peer_from_ie = gtphub_port_have(hub,
1578 &hub->to_gsns[side_idx][plane_idx],
1579 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001580
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001581 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1582 peer_from_ie);
1583
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001584 if (!tei_from_ie &&
1585 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1586 LOG(LOGL_ERROR,
Neels Hofmeyr328d2f72015-12-06 19:13:21 +01001587 "Create PDP Context message omits %s TEI, but"
1588 " no TEI has been announced for this tunnel: %s\n",
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001589 gtphub_plane_idx_names[plane_idx],
1590 gtphub_tunnel_str(tun));
1591 return -1;
1592 }
1593
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001594 if (tei_from_ie) {
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001595 /* Replace TEI in GTP packet IE */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001596 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001597 p->ie[ie_idx]->tv4.v = hton32(tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001598
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001599 if (!gtphub_check_reused_teis(hub, tun)) {
1600 /* It's highly unlikely that all TEIs are
1601 * taken. But the code looking for an unused
1602 * TEI is, at the time of writing this comment,
1603 * not able to find gaps in the TEI space. To
1604 * explicitly alert the user of this problem,
1605 * rather abort than carry on. */
1606 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1607 abort();
1608 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001609 }
1610
1611 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001612 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1613 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001614 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001615 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1616 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001617 return -1;
1618 }
1619 }
1620
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001621 if (p->type == GTP_CREATE_PDP_REQ) {
1622 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1623 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001624 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001625 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1626 gtphub_tunnel_str(tun));
1627 }
1628
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001629 return 0;
1630}
1631
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001632static void pending_delete_del_cb(struct expiring_item *expi)
1633{
1634 struct pending_delete *pd;
1635 pd = container_of(expi, struct pending_delete, expiry_entry);
1636
1637 llist_del(&pd->entry);
1638 INIT_LLIST_HEAD(&pd->entry);
1639
1640 pd->expiry_entry.del_cb = 0;
1641 expiring_item_del(&pd->expiry_entry);
1642
1643 talloc_free(pd);
1644}
1645
1646static struct pending_delete *pending_delete_new(void)
1647{
1648 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1649 INIT_LLIST_HEAD(&pd->entry);
1650 expiring_item_init(&pd->expiry_entry);
1651 pd->expiry_entry.del_cb = pending_delete_del_cb;
1652 return pd;
1653}
1654
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001655static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1656 struct gtp_packet_desc *p,
1657 struct gtphub_peer_port *from_ctrl,
1658 struct gtphub_peer_port *to_ctrl)
1659{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001660 struct gtphub_tunnel *known_tun = p->tun;
1661
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001662 if (p->type == GTP_DELETE_PDP_REQ) {
1663 if (!known_tun) {
1664 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1665 return -1;
1666 }
1667
1668 /* Store the Delete Request until a successful Response is seen. */
1669 uint8_t teardown_ind;
1670 uint8_t nsapi;
1671
1672 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1673 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1674 return -1;
1675 }
1676
1677 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1678 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1679 return -1;
1680 }
1681
1682 struct pending_delete *pd = NULL;
1683
1684 struct pending_delete *pdi = NULL;
1685 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1686 if ((pdi->tun == known_tun)
1687 && (pdi->teardown_ind == teardown_ind)
1688 && (pdi->nsapi == nsapi)) {
1689 pd = pdi;
1690 break;
1691 }
1692 }
1693
1694 if (!pd) {
1695 pd = pending_delete_new();
1696 pd->tun = known_tun;
1697 pd->teardown_ind = teardown_ind;
1698 pd->nsapi = nsapi;
1699
1700 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1701 gtphub_tunnel_str(known_tun));
1702 llist_add(&pd->entry, &hub->pending_deletes);
1703 }
1704
1705 /* Add or refresh timeout. */
1706 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1707
1708 /* If a pending_delete should expire before the response to
1709 * indicate success comes in, the responding peer will have the
1710 * tunnel deactivated, while the requesting peer gets no reply
1711 * and keeps the tunnel. The hope is that the requesting peer
1712 * will try again and get a useful response. */
1713 } else if (p->type == GTP_DELETE_PDP_RSP) {
1714 /* Find the Delete Request for this Response. */
1715 struct pending_delete *pd = NULL;
1716
1717 struct pending_delete *pdi;
1718 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1719 if (known_tun == pdi->tun) {
1720 pd = pdi;
1721 break;
1722 }
1723 }
1724
1725 if (!pd) {
1726 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1727 " Cannot find matching request.");
1728 /* If we delete the tunnel now, anyone can send a
1729 * Delete response to kill tunnels at will. */
1730 return -1;
1731 }
1732
1733 /* TODO handle teardown_ind and nsapi */
1734
1735 expiring_item_del(&pd->expiry_entry);
1736
1737 uint8_t cause;
1738 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1739 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1740 " Missing Cause IE.");
1741 /* If we delete the tunnel now, at least one of the
1742 * peers may still think it is active. */
1743 return -1;
1744 }
1745
1746 if (cause != GTPCAUSE_ACC_REQ) {
1747 LOG(LOGL_NOTICE,
1748 "Delete PDP Context Response indicates failure;"
1749 "for %s\n",
1750 gtphub_tunnel_str(known_tun));
1751 return -1;
1752 }
1753
1754 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1755 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001756 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001757 expiring_item_del(&known_tun->expiry_entry);
1758 }
1759
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001760 return 0;
1761}
1762
1763static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1764 struct gtp_packet_desc *p,
1765 struct gtphub_peer_port *from_ctrl,
1766 struct gtphub_peer_port *to_ctrl)
1767{
1768 /* TODO */
1769 return 0;
1770}
1771
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001772/* Read GSN address IEs from p, and make sure these peer addresses exist in
1773 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1774 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1775 * the packet p. */
1776static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1777 struct gtp_packet_desc *p,
1778 struct gtphub_peer_port *from_ctrl,
1779 struct gtphub_peer_port *to_ctrl)
1780{
1781 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1782
1783 switch (p->type) {
1784 case GTP_CREATE_PDP_REQ:
1785 case GTP_CREATE_PDP_RSP:
1786 return gtphub_handle_create_pdp_ctx(hub, p,
1787 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001788
1789 case GTP_DELETE_PDP_REQ:
1790 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001791 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001792 from_ctrl, to_ctrl);
1793
1794 case GTP_UPDATE_PDP_REQ:
1795 case GTP_UPDATE_PDP_RSP:
1796 return gtphub_handle_update_pdp_ctx(hub, p,
1797 from_ctrl, to_ctrl);
1798
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001799 default:
1800 /* Nothing to do for this message type. */
1801 return 0;
1802 }
1803
1804}
1805
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001806static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1807 struct gtphub_tunnel *tun,
1808 int to_side)
1809{
1810 static uint8_t del_ctx_msg[16] = {
1811 0x32, /* GTP v1 flags */
1812 GTP_DELETE_PDP_REQ,
1813 0x00, 0x08, /* Length in network byte order */
1814 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1815 0, 0, /* Seq, to be replaced */
1816 0, 0, /* no extensions */
1817 0x13, 0xff, /* 19: Teardown ind = 1 */
1818 0x14, 0 /* 20: NSAPI = 0 */
1819 };
1820
1821 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1822 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1823
1824 struct gtphub_tunnel_endpoint *te =
1825 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1826
1827 if (! te->peer)
1828 return 0;
1829
1830 *tei = hton32(te->tei_orig);
1831 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1832
1833 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001834 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1835 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001836 if (rc != 0) {
1837 LOG(LOGL_ERROR,
1838 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1839 gtphub_port_str(te->peer));
1840 }
1841 return rc;
1842}
1843
1844/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1845static void gtphub_restarted(struct gtphub *hub,
1846 struct gtp_packet_desc *p,
1847 struct gtphub_peer_port *pp)
1848{
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001849 LOG(LOGL_NOTICE, "Peer has restarted: %s\n",
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001850 gtphub_port_str(pp));
1851
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001852 int deleted_count = 0;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001853 struct gtphub_tunnel *tun;
1854 llist_for_each_entry(tun, &hub->tunnels, entry) {
1855 int side_idx;
1856 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001857 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1858 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1859 if ((!te->peer)
1860 || (!te2->tei_orig)
1861 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001862 continue;
1863
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001864 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1865 gtphub_tunnel_str(tun));
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001866 deleted_count ++;
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001867
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001868 /* Send a Delete PDP Context Request to the
1869 * peer on the other side, remember the pending
1870 * delete and wait for the response to delete
1871 * the tunnel. Clear this side of the tunnel to
1872 * make sure it isn't used.
1873 *
1874 * Should the delete message send fail, or if no
1875 * response is received, this tunnel will expire. If
1876 * its TEIs come up in a new PDP Context Request, it
1877 * will be removed. If messages for this tunnel should
1878 * come in (from the not restarted side), they will be
1879 * dropped because the tunnel is rendered unusable. */
1880 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1881
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001882 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1883 NULL);
1884 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1885 NULL);
1886 }
1887 }
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001888
1889 if (deleted_count)
1890 LOG(LOGL_NOTICE, "Deleting %d tunnels due to restart of: %s\n",
1891 deleted_count,
1892 gtphub_port_str(pp));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001893}
1894
1895static int get_restart_count(struct gtp_packet_desc *p)
1896{
1897 int ie_idx;
1898 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1899 if (ie_idx < 0)
1900 return -1;
1901 return ntoh8(p->ie[ie_idx]->tv1.v);
1902}
1903
1904static void gtphub_check_restart_counter(struct gtphub *hub,
1905 struct gtp_packet_desc *p,
1906 struct gtphub_peer_port *from)
1907{
1908 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1909 * that doesn't match the peer's previously sent restart counter, clear
1910 * that peer and cancel PDP contexts. */
1911
1912 int restart = get_restart_count(p);
1913
1914 if ((restart < 0) || (restart > 255))
1915 return;
1916
1917 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1918 if (from->last_restart_count != restart) {
1919 gtphub_restarted(hub, p, from);
1920 }
1921 }
1922
1923 from->last_restart_count = restart;
1924}
1925
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001926static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1927{
1928 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1929 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001930 LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n",
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001931 gtphub_plane_idx_names[plane_idx]);
1932
1933 if (!(what & BSC_FD_READ))
1934 return 0;
1935
1936 struct gtphub *hub = from_sgsns_ofd->data;
1937
1938 static uint8_t buf[4096];
1939 struct osmo_sockaddr from_addr;
1940 struct osmo_sockaddr to_addr;
1941 struct osmo_fd *to_ofd;
1942 int len;
1943 uint8_t *reply_buf;
1944
1945 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1946 if (len < 1)
1947 return 0;
1948
1949 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1950 buf, len, gtphub_now(),
1951 &reply_buf, &to_ofd, &to_addr);
1952 if (len < 1)
1953 return 0;
1954
1955 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1956}
1957
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001958static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1959{
1960 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1961 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001962 LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001963 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001964 if (!(what & BSC_FD_READ))
1965 return 0;
1966
1967 struct gtphub *hub = from_ggsns_ofd->data;
1968
1969 static uint8_t buf[4096];
1970 struct osmo_sockaddr from_addr;
1971 struct osmo_sockaddr to_addr;
1972 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001973 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001974 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001975
1976 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1977 if (len < 1)
1978 return 0;
1979
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001980 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1981 buf, len, gtphub_now(),
1982 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001983 if (len < 1)
1984 return 0;
1985
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001986 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001987}
1988
1989static int gtphub_unmap(struct gtphub *hub,
1990 struct gtp_packet_desc *p,
1991 struct gtphub_peer_port *from,
1992 struct gtphub_peer_port *to_proxy,
1993 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001994 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001995{
1996 /* Always (try to) unmap sequence and TEI numbers, which need to be
1997 * replaced in the packet. Either way, give precedence to the proxy, if
1998 * configured. */
1999
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002000 if (unmapped_from_seq)
2001 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002002 if (final_unmapped)
2003 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002004 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002005
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002006 struct gtphub_peer_port *from_seq = NULL;
2007 struct gtphub_peer_port *from_tei = NULL;
2008 struct gtphub_peer_port *unmapped = NULL;
2009
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002010 from_seq = gtphub_unmap_seq(p, from);
2011
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002012 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002013 return -1;
2014
2015 struct gtphub_peer *from_peer = from->peer_addr->peer;
2016 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002017 LOG(LOGL_DEBUG,
2018 "Seq unmap and TEI unmap yield two different peers."
2019 " Using seq unmap."
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01002020 " (from %s %s: seq %d yields %s, tei %u yields %s)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002021 gtphub_plane_idx_names[p->plane_idx],
2022 gtphub_peer_str(from_peer),
2023 (int)p->seq,
2024 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002025 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002026 gtphub_port_str2(from_tei)
2027 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002028 }
2029 unmapped = (from_seq? from_seq : from_tei);
2030
2031 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002032 LOG(LOGL_NOTICE,
2033 "Unmap yields a different peer than the configured proxy."
2034 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002035 " unmapped: %s proxy: %s\n",
2036 gtphub_port_str(unmapped),
2037 gtphub_port_str2(to_proxy)
2038 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002039 }
2040 unmapped = (to_proxy? to_proxy : unmapped);
2041
2042 if (!unmapped) {
2043 /* Return no error, but returned pointers are all NULL. */
2044 return 0;
2045 }
2046
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002047 if (unmapped_from_seq)
2048 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002049 if (final_unmapped)
2050 *final_unmapped = unmapped;
2051 return 0;
2052}
2053
2054static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2055 uint16_t port,
2056 struct osmo_sockaddr *dst)
2057{
2058 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2059}
2060
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002061/* If p is an Echo request, replace p's data with the matching response and
2062 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2063 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002064static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2065 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002066{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002067 if (p->type != GTP_ECHO_REQ)
2068 return 0;
2069
2070 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002071 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002072 GTP_ECHO_RSP,
2073 0x00, 14 - 8, /* Length in network byte order */
2074 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2075 0, 0, /* Seq, to be replaced */
2076 0, 0, /* no extensions */
2077 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002078 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002079 };
2080 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2081 uint8_t *recovery = &echo_response_data[13];
2082
2083 *seq = hton16(p->seq);
2084 *recovery = hub->restart_counter;
2085
2086 *reply_buf = echo_response_data;
2087
2088 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002089}
2090
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002091struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2092 const struct osmo_sockaddr *addr);
2093
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002094/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2095 * address to forward to. Return a pointer to the osmo_fd, but copy the
2096 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2097 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2098 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002099int gtphub_handle_buf(struct gtphub *hub,
2100 unsigned int side_idx,
2101 unsigned int plane_idx,
2102 const struct osmo_sockaddr *from_addr,
2103 uint8_t *buf,
2104 size_t received,
2105 time_t now,
2106 uint8_t **reply_buf,
2107 struct osmo_fd **to_ofd,
2108 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002109{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002110 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2111 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002112
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002113 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2114 received);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002115
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002116 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002117 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002118
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002119 LOG(LOGL_DEBUG, "%s rx %s from %s %s%s\n",
2120 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
2121 gtphub_plane_idx_names[plane_idx],
2122 gtphub_side_idx_names[side_idx],
2123 osmo_sockaddr_to_str(from_addr),
2124 gtp_type_str(p.type));
2125
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002126 if (p.rc <= 0) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002127 LOG(LOGL_ERROR, "INVALID: dropping GTP packet%s from %s %s %s\n",
2128 gtp_type_str(p.type),
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002129 gtphub_side_idx_names[side_idx],
2130 gtphub_plane_idx_names[plane_idx],
2131 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002132 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002133 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002134
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002135 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2136
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002137 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002138 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002139 if (reply_len > 0) {
2140 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002141 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002142 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002143
2144 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2145 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2146 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002147 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2148 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2149 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002150 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002151 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002152 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002153 if (reply_len < 0)
2154 return -1;
2155
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002156 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002157
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002158 /* If a proxy is configured, check that it's indeed that proxy talking
2159 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2160 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002161 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2162 if (from_peer) {
2163 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002164 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002165 "Rejecting: %s proxy configured, but GTP packet"
2166 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002167 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002168 gtphub_side_idx_names[side_idx],
2169 gtphub_side_idx_names[side_idx],
2170 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002171 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002172 return -1;
2173 }
2174 }
2175
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002176 if (!from_peer) {
2177 /* Find or create a peer with a matching address. The sender's
2178 * port may in fact differ. */
2179 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002180 }
2181
2182 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002183 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2184 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002185 if (!from_peer) {
2186 if (side_idx == GTPH_SIDE_GGSN) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002187 LOG(LOGL_ERROR, "Dropping packet%s: unknown GGSN peer: %s\n",
2188 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002189 osmo_sockaddr_to_str(from_addr));
2190 return -1;
2191 } else {
2192 /* SGSN */
2193 /* A new peer. If this is on the Ctrl plane, an SGSN
2194 * may make first contact without being known yet, so
2195 * create the peer struct for the current sender. */
2196 if (plane_idx != GTPH_PLANE_CTRL) {
2197 LOG(LOGL_ERROR,
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002198 "Dropping packet%s: User plane peer was not"
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002199 "announced by PDP Context: %s\n",
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002200 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002201 osmo_sockaddr_to_str(from_addr));
2202 return -1;
2203 }
2204
2205 struct gsn_addr from_gsna;
2206 uint16_t from_port;
2207 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2208 return -1;
2209
2210 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2211 }
2212 }
2213
2214 if (!from_peer) {
2215 /* This could theoretically happen for invalid address data or
2216 * somesuch. */
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002217 LOG(LOGL_ERROR, "Dropping packet%s: invalid %s peer: %s\n",
2218 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002219 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002220 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002221 return -1;
2222 }
2223
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002224 rate_ctr_add(&from_peer->counters_io->ctr[GTPH_CTR_BYTES_IN],
2225 received);
2226 rate_ctr_inc(&from_peer->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2227
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002228 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2229 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002230
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002231 gtphub_check_restart_counter(hub, &p, from_peer);
2232 gtphub_map_restart_counter(hub, &p);
2233
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002234 struct gtphub_peer_port *to_peer_from_seq;
2235 struct gtphub_peer_port *to_peer;
2236 if (gtphub_unmap(hub, &p, from_peer,
2237 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002238 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002239 != 0) {
2240 return -1;
2241 }
2242
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002243 if (p.tun) {
2244 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[p.side_idx][p.plane_idx];
2245 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_IN],
2246 received);
2247 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2248 }
2249
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002250 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2251 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2252 return -1;
2253 }
2254
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002255 if (!to_peer && p.tun && p.type == GTP_DELETE_PDP_RSP) {
2256 /* It's a delete confirmation for a tunnel that is partly
2257 * invalid, probably marked unsuable due to a restarted peer.
2258 * Remove the tunnel and be happy without forwarding. */
2259 expiring_item_del(&p.tun->expiry_entry);
2260 p.tun = NULL;
2261 return 0;
2262 }
2263
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002264 if (!to_peer) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002265 LOG(LOGL_ERROR, "No %s to send to. Dropping packet%s"
2266 " (type=%" PRIu8 ", header-TEI=%" PRIx32 ", seq=%" PRIx16 ").\n",
2267 gtphub_side_idx_names[other_side_idx(side_idx)],
2268 gtp_type_str(p.type),
2269 p.type, p.header_tei_rx, p.seq
2270 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002271 return -1;
2272 }
2273
2274 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002275 /* This may be a Create PDP Context response. If it is, there
2276 * are other addresses in the GTP message to set up apart from
2277 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002278 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002279 != 0)
2280 return -1;
2281 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002282
2283 /* Either to_peer was resolved from an existing tunnel,
2284 * or a PDP Ctx and thus a tunnel has just been created,
2285 * or the tunnel has been deleted due to this message. */
2286 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002287
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002288 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002289 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002290 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002291 if (!to_peer_from_seq)
2292 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002293
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002294 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002295
2296 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002297
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002298 if (received) {
2299 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2300 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2301 received);
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002302
2303 rate_ctr_inc(&to_peer->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2304 rate_ctr_add(&to_peer->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2305 received);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002306 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002307
2308 if (p.tun) {
2309 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[other_side_idx(p.side_idx)][p.plane_idx];
2310 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2311 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2312 received);
2313 }
2314
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002315 LOG(LOGL_DEBUG, "%s Forward to %s:"
2316 " header-TEI %" PRIx32", seq %" PRIx16", %d bytes to %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002317 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2318 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002319 p.header_tei, p.seq,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002320 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002321 return received;
2322}
2323
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002324static void resolved_gssn_del_cb(struct expiring_item *expi)
2325{
2326 struct gtphub_resolved_ggsn *ggsn;
2327 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2328
2329 gtphub_port_ref_count_dec(ggsn->peer);
2330 llist_del(&ggsn->entry);
2331
2332 ggsn->expiry_entry.del_cb = 0;
2333 expiring_item_del(&ggsn->expiry_entry);
2334
2335 talloc_free(ggsn);
2336}
2337
2338void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2339 struct gsn_addr *resolved_addr,
2340 time_t now)
2341{
2342 struct gtphub_peer_port *pp;
2343 struct gtphub_resolved_ggsn *ggsn;
2344
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002345 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002346 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2347 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002348
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002349 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002350 resolved_addr, 2123);
2351 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002352 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2353 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002354 return;
2355 }
2356
2357 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2358 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002359 INIT_LLIST_HEAD(&ggsn->entry);
2360 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002361
2362 ggsn->peer = pp;
2363 gtphub_port_ref_count_inc(pp);
2364
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002365 osmo_strlcpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002366
2367 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002368 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002369
2370 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2371}
2372
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002373static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2374{
2375 return pp->ref_count == 0;
2376}
2377
2378static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2379{
2380 struct gtphub_peer_port *pp, *npp;
2381 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2382 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002383 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002384 gtphub_port_str(pp));
2385 gtphub_peer_port_del(pp);
2386 }
2387 }
2388 return llist_empty(&pa->ports);
2389}
2390
2391static int gtphub_gc_peer(struct gtphub_peer *p)
2392{
2393 struct gtphub_peer_addr *pa, *npa;
2394 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2395 if (gtphub_gc_peer_addr(pa)) {
2396 gtphub_peer_addr_del(pa);
2397 }
2398 }
2399
2400 /* Note that there's a ref_count in each gtphub_peer_port instance
2401 * listed within p->addresses, referenced by TEI mappings from
2402 * hub->tei_map. As long as those don't expire, this peer will stay. */
2403
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002404 return llist_empty(&p->addresses)
2405 && nr_map_empty(&p->seq_map);
2406}
2407
2408static void gtphub_gc_bind(struct gtphub_bind *b)
2409{
2410 struct gtphub_peer *p, *n;
2411 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2412 if (gtphub_gc_peer(p)) {
2413 gtphub_peer_del(p);
2414 }
2415 }
2416}
2417
2418void gtphub_gc(struct gtphub *hub, time_t now)
2419{
2420 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002421 expired = expiry_tick(&hub->expire_quickly, now);
2422 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002423
2424 /* ... */
2425
2426 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002427 int s, p;
2428 for_each_side_and_plane(s, p) {
2429 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002430 }
2431 }
2432}
2433
2434static void gtphub_gc_cb(void *data)
2435{
2436 struct gtphub *hub = data;
2437 gtphub_gc(hub, gtphub_now());
2438 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2439}
2440
2441static void gtphub_gc_start(struct gtphub *hub)
2442{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02002443 osmo_timer_setup(&hub->gc_timer, gtphub_gc_cb, hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002444 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2445}
2446
2447/* called by unit tests */
2448void gtphub_init(struct gtphub *hub)
2449{
2450 gtphub_zero(hub);
2451
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002452 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002453 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002454
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002455 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2456 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002457
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002458 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2459
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002460 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002461 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002462 for_each_side_and_plane(side_idx, plane_idx) {
2463 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002464 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002465
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002466 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2467 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2468 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2469 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002470}
2471
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002472/* For the test suite, this is kept separate from gtphub_stop(), which also
2473 * closes sockets. The test suite avoids using sockets and would cause
2474 * segfaults when trying to close uninitialized ofds. */
2475void gtphub_free(struct gtphub *hub)
2476{
2477 /* By expiring all mappings, a garbage collection should free
2478 * everything else. A gtphub_bind_free() will assert that everything is
2479 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002480 expiry_clear(&hub->expire_quickly);
2481 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002482
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002483 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002484 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002485 for_each_side_and_plane(side_idx, plane_idx) {
2486 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2487 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002488 }
2489}
2490
2491void gtphub_stop(struct gtphub *hub)
2492{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002493 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002494 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002495 for_each_side_and_plane(side_idx, plane_idx) {
2496 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002497 }
2498 gtphub_free(hub);
2499}
2500
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002501static int gtphub_make_proxy(struct gtphub *hub,
2502 struct gtphub_peer_port **pp,
2503 struct gtphub_bind *bind,
2504 const struct gtphub_cfg_addr *addr)
2505{
2506 if (!addr->addr_str)
2507 return 0;
2508
2509 struct gsn_addr gsna;
2510 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2511 return -1;
2512
2513 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2514
2515 /* This is *the* proxy. Make sure it is never expired. */
2516 gtphub_port_ref_count_inc(*pp);
2517 return 0;
2518}
2519
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002520int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2521 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002522{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002523 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002524
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002525 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002526 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002527
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002528 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002529 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002530 if (gtphub_ares_init(hub) != 0) {
2531 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2532 return -1;
2533 }
2534 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002535
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002536 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002537 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002538 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002539 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002540 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2541 &cfg->to_gsns[side_idx][plane_idx],
2542 (side_idx == GTPH_SIDE_SGSN)
2543 ? from_sgsns_read_cb
2544 : from_ggsns_read_cb,
2545 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002546 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002547 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2548 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002549 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002550 return rc;
2551 }
2552 }
2553
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002554 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002555 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002556 &hub->proxy[side_idx][plane_idx],
2557 &hub->to_gsns[side_idx][plane_idx],
2558 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002559 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002560 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002561 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002562 gtphub_side_idx_names[side_idx],
2563 cfg->proxy[side_idx][plane_idx].addr_str,
2564 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002565 return -1;
2566 }
2567 }
2568
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002569 for_each_side_and_plane(side_idx, plane_idx) {
2570 if (hub->proxy[side_idx][plane_idx])
2571 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2572 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002573 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002574 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002575 }
2576
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002577 if (hub->sgsn_use_sender)
Neels Hofmeyr9d8f5062015-12-03 14:52:33 +01002578 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 +01002579
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002580 gtphub_gc_start(hub);
2581 return 0;
2582}
2583
2584static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2585 const struct gsn_addr *addr)
2586{
2587 struct gtphub_peer_addr *a;
2588 llist_for_each_entry(a, &peer->addresses, entry) {
2589 if (gsn_addr_same(&a->addr, addr))
2590 return a;
2591 }
2592 return NULL;
2593}
2594
2595static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2596 uint16_t port)
2597{
2598 OSMO_ASSERT(port);
2599 struct gtphub_peer_port *pp;
2600 llist_for_each_entry(pp, &a->ports, entry) {
2601 if (pp->port == port)
2602 return pp;
2603 }
2604 return NULL;
2605}
2606
2607static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2608 const struct gsn_addr *addr)
2609{
2610 struct gtphub_peer *peer;
2611 llist_for_each_entry(peer, &bind->peers, entry) {
2612 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2613 if (a)
2614 return a;
2615 }
2616 return NULL;
2617}
2618
2619static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2620 const struct gsn_addr *addr,
2621 uint16_t port)
2622{
2623 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2624 if (!a)
2625 return NULL;
2626 return gtphub_addr_find_port(a, port);
2627}
2628
2629struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2630 const struct osmo_sockaddr *addr)
2631{
2632 struct gsn_addr gsna;
2633 uint16_t port;
2634 gsn_addr_from_sockaddr(&gsna, &port, addr);
2635 return gtphub_port_find(bind, &gsna, port);
2636}
2637
2638static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2639 struct gtphub_bind *bind)
2640{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002641 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2642 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002643 OSMO_ASSERT(peer);
2644
2645 INIT_LLIST_HEAD(&peer->addresses);
2646
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002647 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002648 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002649
2650 /* TODO use something random to pick the initial sequence nr.
2651 0x6d31 produces the ASCII character sequence 'm1', currently used in
2652 gtphub_nc_test.sh. */
2653 peer->seq_pool.last_nr = 0x6d31 - 1;
2654
2655 llist_add(&peer->entry, &bind->peers);
2656 return peer;
2657}
2658
2659static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2660 const struct gsn_addr *addr)
2661{
2662 struct gtphub_peer_addr *a;
2663 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2664 OSMO_ASSERT(a);
2665 a->peer = peer;
2666 gsn_addr_copy(&a->addr, addr);
2667 INIT_LLIST_HEAD(&a->ports);
2668 llist_add(&a->entry, &peer->addresses);
2669
2670 return a;
2671}
2672
2673static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2674 struct gtphub_bind *bind,
2675 const struct gsn_addr *addr)
2676{
2677 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2678 if (a)
2679 return a;
2680
2681 /* If we haven't found an address, that means we need to create an
2682 * entirely new peer for the new address. More addresses may be added
2683 * to this peer later, but not via this function. */
2684 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002685
2686 a = gtphub_peer_add_addr(peer, addr);
2687
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002688 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002689 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002690 gsn_addr_to_str(&a->addr));
2691
2692 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002693}
2694
2695static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2696 uint16_t port)
2697{
2698 struct gtphub_peer_port *pp;
2699
2700 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2701 OSMO_ASSERT(pp);
2702 pp->peer_addr = a;
2703 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002704 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002705
2706 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2707 talloc_free(pp);
2708 return NULL;
2709 }
2710
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002711 pp->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
2712 &gtphub_ctrg_io_desc, 0);
Harald Welted939ac02017-07-17 23:59:39 +02002713 if (!pp->counters_io) {
2714 talloc_free(pp);
2715 return NULL;
2716 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002717
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002718 llist_add(&pp->entry, &a->ports);
2719
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002720 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002721 gsn_addr_to_str(&a->addr),
2722 (int)port);
2723
2724 return pp;
2725}
2726
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002727struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2728 struct gtphub_bind *bind,
2729 const struct gsn_addr *addr,
2730 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002731{
2732 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2733
2734 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2735 if (pp)
2736 return pp;
2737
2738 return gtphub_addr_add_port(a, port);
2739}
2740
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002741/* Find a GGSN peer with a matching address. If the address is known but the
2742 * port not, create a new port for that peer address. */
2743struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2744 const struct osmo_sockaddr *addr)
2745{
2746 struct gtphub_peer_addr *pa;
2747 struct gtphub_peer_port *pp;
2748
2749 struct gsn_addr gsna;
2750 uint16_t port;
2751 gsn_addr_from_sockaddr(&gsna, &port, addr);
2752
2753 pa = gtphub_addr_find(bind, &gsna);
2754 if (!pa)
2755 return NULL;
2756
2757 pp = gtphub_addr_find_port(pa, port);
2758
2759 if (!pp)
2760 pp = gtphub_addr_add_port(pa, port);
2761
2762 return pp;
2763}
2764
2765
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002766/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2767 * resolution should be possible but failed, and 1 if resolution was
2768 * successful. *pp will be set to NULL if <1 is returned. */
2769static int gtphub_resolve_ggsn(struct gtphub *hub,
2770 struct gtp_packet_desc *p,
2771 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002772{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002773 *pp = NULL;
2774
2775 /* TODO determine from message type whether IEs should be present? */
2776
2777 int rc;
2778 const char *imsi_str;
2779 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2780 if (rc < 1)
2781 return rc;
2782 OSMO_ASSERT(imsi_str);
2783
2784 const char *apn_str;
2785 rc = get_ie_apn_str(p->ie, &apn_str);
2786 if (rc < 1)
2787 return rc;
2788 OSMO_ASSERT(apn_str);
2789
2790 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2791 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002792}
2793
2794
2795/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002796/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002797/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2798 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002799static int _osmo_getaddrinfo(struct addrinfo **result,
2800 uint16_t family, uint16_t type, uint8_t proto,
2801 const char *host, uint16_t port)
2802{
2803 struct addrinfo hints;
2804 char portbuf[16];
2805
2806 sprintf(portbuf, "%u", port);
2807 memset(&hints, '\0', sizeof(struct addrinfo));
2808 hints.ai_family = family;
2809 if (type == SOCK_RAW) {
2810 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2811 * SOCK_RAW and IPPROTO_GRE is used.
2812 */
2813 hints.ai_socktype = SOCK_DGRAM;
2814 hints.ai_protocol = IPPROTO_UDP;
2815 } else {
2816 hints.ai_socktype = type;
2817 hints.ai_protocol = proto;
2818 }
2819
2820 return getaddrinfo(host, portbuf, &hints, result);
2821}
2822
2823/* TODO move to osmocom/core/socket.c ? */
2824int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2825 uint16_t family, uint16_t type, uint8_t proto,
2826 const char *host, uint16_t port)
2827{
2828 struct addrinfo *res;
2829 int rc;
2830 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2831
2832 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002833 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002834 return -EINVAL;
2835 }
2836
2837 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2838 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2839 addr->l = res->ai_addrlen;
2840 freeaddrinfo(res);
2841
2842 return 0;
2843}
2844
2845int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2846 char *port_str, size_t port_str_len,
2847 const struct osmo_sockaddr *addr,
2848 int flags)
2849{
2850 int rc;
2851
2852 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2853 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2854 return -1;
2855 }
2856
2857 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002858 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2859 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002860 return -1;
2861 }
2862
2863 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2864 addr_str, addr_str_len,
2865 port_str, port_str_len,
2866 flags);
2867
2868 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002869 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2870 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2871 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002872
2873 return rc;
2874}
2875
2876const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2877 char *buf, size_t buf_len)
2878{
2879 const int portbuf_len = 6;
2880 OSMO_ASSERT(buf_len > portbuf_len);
2881 char *portbuf = buf + buf_len - portbuf_len;
2882 buf_len -= portbuf_len;
2883 if (osmo_sockaddr_to_strs(buf, buf_len,
2884 portbuf, portbuf_len,
2885 addr,
2886 NI_NUMERICHOST | NI_NUMERICSERV))
2887 return NULL;
2888
2889 char *pos = buf + strnlen(buf, buf_len-1);
2890 size_t len = buf_len - (pos - buf);
2891
2892 snprintf(pos, len, " port %s", portbuf);
2893 buf[buf_len-1] = '\0';
2894
2895 return buf;
2896}
2897
2898const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2899{
2900 static char buf[256];
2901 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2902 if (! result)
2903 return "(invalid)";
2904 return result;
2905}
2906
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002907int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2908 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002909{
2910 if (a == b)
2911 return 0;
2912 if (!a)
2913 return -1;
2914 if (!b)
2915 return 1;
2916 if (a->l != b->l) {
2917 /* Lengths are not the same, but determine the order. Will
2918 * anyone ever sort a list by osmo_sockaddr though...? */
2919 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2920 if (cmp == 0) {
2921 if (a->l < b->l)
2922 return -1;
2923 else
2924 return 1;
2925 }
2926 return cmp;
2927 }
2928 return memcmp(&a->a, &b->a, a->l);
2929}
2930
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002931void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2932 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002933{
2934 OSMO_ASSERT(src->l <= sizeof(dst->a));
2935 memcpy(&dst->a, &src->a, src->l);
2936 dst->l = src->l;
2937}