blob: 58300ea1372a17934bb61d29765cbe0672e32746 [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
35#include <openbsc/gtphub.h>
36#include <openbsc/debug.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010037#include <openbsc/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
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010045
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020046static const int GTPH_GC_TICK_SECONDS = 1;
47
48void *osmo_gtphub_ctx;
49
Neels Hofmeyr063a8022015-11-16 14:35:13 +010050/* Convenience makro, note: only within this C file. */
51#define LOG(level, fmt, args...) \
52 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020053
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010054#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
55 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020056
57/* TODO move this to osmocom/core/select.h ? */
58typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
59
60/* TODO move this to osmocom/core/linuxlist.h ? */
61#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010062#define llist_first(head, type, entry) \
63 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020064
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +010065#define __llist_last(head) (((head)->next == (head)) ? NULL : (head)->prev)
66#define llist_last(head, type, entry) \
67 llist_entry(__llist_last(head), type, entry)
68
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
70
71enum gtp_rc {
72 GTP_RC_UNKNOWN = 0,
73 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010074 GTP_RC_PDU_C = 2, /* a real packet with IEs */
75 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020076
77 GTP_RC_TOOSHORT = -1,
78 GTP_RC_UNSUPPORTED_VERSION = -2,
79 GTP_RC_INVALID_IE = -3,
80};
81
82struct gtp_packet_desc {
83 union gtp_packet *data;
84 int data_len;
85 int header_len;
86 int version;
87 uint8_t type;
88 uint16_t seq;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010089 uint32_t header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020090 uint32_t header_tei;
91 int rc; /* enum gtp_rc */
92 unsigned int plane_idx;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010093 unsigned int side_idx;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +010094 struct gtphub_tunnel *tun;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010095 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020096 union gtpie_member *ie[GTPIE_SIZE];
97};
98
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010099struct pending_delete {
100 struct llist_head entry;
101 struct expiring_item expiry_entry;
102
103 struct gtphub_tunnel *tun;
104 uint8_t teardown_ind;
105 uint8_t nsapi;
106};
107
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100108
109/* counters */
110
111enum gtphub_counters_io {
112 GTPH_CTR_PKTS_IN = 0,
113 GTPH_CTR_PKTS_OUT,
114 GTPH_CTR_BYTES_IN,
115 GTPH_CTR_BYTES_OUT
116};
117
118static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
119 { "packets.in", "Packets ( In)" },
120 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100121 { "bytes.in", "Bytes ( In)" },
122 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100123};
124
125static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
126 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100127 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100128 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
129 .ctr_desc = gtphub_counters_io_desc,
130 .class_id = OSMO_STATS_CLASS_GLOBAL,
131};
132
133
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100134/* support */
135
136static const char *gtp_type_str(uint8_t type)
137{
138 switch (type) {
139 case 1:
140 return " (Echo Request)";
141 case 2:
142 return " (Echo Response)";
143 case 16:
144 return " (Create PDP Ctx Request)";
145 case 17:
146 return " (Create PDP Ctx Response)";
147 case 18:
148 return " (Update PDP Ctx Request)";
149 case 19:
150 return " (Update PDP Ctx Response)";
151 case 20:
152 return " (Delete PDP Ctx Request)";
153 case 21:
154 return " (Delete PDP Ctx Response)";
155 case 255:
156 return " (User Data)";
157 default:
158 return "";
159 }
160}
161
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200162void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
163{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100164 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200165}
166
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200167int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
168 const struct osmo_sockaddr *sa)
169{
170 char addr_str[256];
171 char port_str[6];
172
173 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
174 port_str, sizeof(port_str),
175 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
176 != 0) {
177 return -1;
178 }
179
180 if (port)
181 *port = atoi(port_str);
182
183 return gsn_addr_from_str(gsna, addr_str);
184}
185
186int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
187{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100188 if ((!gsna) || (!numeric_addr_str))
189 return -1;
190
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200191 int af = AF_INET;
192 gsna->len = 4;
193 const char *pos = numeric_addr_str;
194 for (; *pos; pos++) {
195 if (*pos == ':') {
196 af = AF_INET6;
197 gsna->len = 16;
198 break;
199 }
200 }
201
202 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
203 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100204 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
205 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200206 return -1;
207 }
208 return 0;
209}
210
211const char *gsn_addr_to_str(const struct gsn_addr *gsna)
212{
213 static char buf[INET6_ADDRSTRLEN + 1];
214 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
215}
216
217const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
218 char *strbuf,
219 int strbuf_len)
220{
221 int af;
222 switch (gsna->len) {
223 case 4:
224 af = AF_INET;
225 break;
226 case 16:
227 af = AF_INET6;
228 break;
229 default:
230 return NULL;
231 }
232
233 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
234 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100235 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
236 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100237 strerror(errno),
238 (int)gsna->len,
239 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200240 }
241 return r;
242}
243
244int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
245{
246 if (a == b)
247 return 1;
248 if ((!a) || (!b))
249 return 0;
250 if (a->len != b->len)
251 return 0;
252 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
253}
254
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100255static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
256 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200257{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100258 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259 return -1;
260
261 unsigned int len;
262 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
263 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
264 &len, gsna->buf, sizeof(gsna->buf))
265 != 0)
266 return -1;
267 gsna->len = len;
268 return 0;
269}
270
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100271static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
272 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200273{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100274 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200275 return -1;
276
277 int ie_idx;
278 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
279
280 if (ie_idx < 0)
281 return -1;
282
283 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
284 int ie_l = ntoh16(ie->l);
285 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100286 LOG(LOGL_ERROR, "Not implemented:"
287 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200288 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
289 return -1;
290 }
291
292 memcpy(ie->v, gsna->buf, (int)ie_l);
293 return 0;
294}
295
296/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
297 */
298void validate_gtp0_header(struct gtp_packet_desc *p)
299{
300 const struct gtp0_header *pheader = &(p->data->gtp0.h);
301 p->rc = GTP_RC_UNKNOWN;
302 p->header_len = 0;
303
304 OSMO_ASSERT(p->data_len >= 1);
305 OSMO_ASSERT(p->version == 0);
306
307 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100308 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200309 p->rc = GTP_RC_TOOSHORT;
310 return;
311 }
312
313 p->type = ntoh8(pheader->type);
314 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100315 p->header_tei_rx = 0; /* TODO */
316 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200317
318 if (p->data_len == GTP0_HEADER_SIZE) {
319 p->rc = GTP_RC_TINY;
320 p->header_len = GTP0_HEADER_SIZE;
321 return;
322 }
323
324 /* Check packet length field versus length of packet */
325 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100326 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
327 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100328 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
329 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200330 p->rc = GTP_RC_TOOSHORT;
331 return;
332 }
333
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100334 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200335 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100336 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200337}
338
339/* Validate GTP version 1 data, and update p->rc with the result, as well as
340 * p->header_len in case of a valid header. */
341void validate_gtp1_header(struct gtp_packet_desc *p)
342{
343 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
344 p->rc = GTP_RC_UNKNOWN;
345 p->header_len = 0;
346
347 OSMO_ASSERT(p->data_len >= 1);
348 OSMO_ASSERT(p->version == 1);
349
350 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
351 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100352 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200353 p->rc = GTP_RC_TOOSHORT;
354 return;
355 }
356
357 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100358 p->header_tei_rx = ntoh32(pheader->tei);
359 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200360 p->seq = ntoh16(pheader->seq);
361
Neels Hofmeyr28a70f22015-12-06 15:22:54 +0100362 LOG(LOGL_DEBUG, "| GTPv1\n");
363 LOG(LOGL_DEBUG, "| type = %" PRIu8 " 0x%02" PRIx8 "\n", p->type, p->type);
364 LOG(LOGL_DEBUG, "| length = %" PRIu16 " 0x%04" PRIx16 "\n", ntoh16(pheader->length), ntoh16(pheader->length));
365 LOG(LOGL_DEBUG, "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", p->header_tei_rx, p->header_tei_rx);
366 LOG(LOGL_DEBUG, "| seq = %" PRIu16 " 0x%04" PRIx16 "\n", p->seq, p->seq);
367 LOG(LOGL_DEBUG, "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->npdu, pheader->npdu);
368 LOG(LOGL_DEBUG, "| next = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->next, pheader->next);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200369
370 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
371 p->rc = GTP_RC_TINY;
372 p->header_len = GTP1_HEADER_SIZE_SHORT;
373 return;
374 }
375
376 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100377 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
378 if (p->data_len != announced_len) {
379 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
380 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100381 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
382 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200383 p->rc = GTP_RC_TOOSHORT;
384 return;
385 }
386
Neels Hofmeyre921e322015-11-11 00:45:50 +0100387 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200388 p->header_len = GTP1_HEADER_SIZE_LONG;
389}
390
391/* Examine whether p->data of size p->data_len has a valid GTP header. Set
392 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
393 * gtp_rc). p->data must point at a buffer with p->data_len set. */
394void validate_gtp_header(struct gtp_packet_desc *p)
395{
396 p->rc = GTP_RC_UNKNOWN;
397
398 /* Need at least 1 byte in order to check version */
399 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100400 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
401 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200402 p->rc = GTP_RC_TOOSHORT;
403 return;
404 }
405
406 p->version = p->data->flags >> 5;
407
408 switch (p->version) {
409 case 0:
410 validate_gtp0_header(p);
411 break;
412 case 1:
413 validate_gtp1_header(p);
414 break;
415 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100416 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200417 p->rc = GTP_RC_UNSUPPORTED_VERSION;
418 break;
419 }
420}
421
422
423/* Return the value of the i'th IMSI IEI by copying to *imsi.
424 * The first IEI is reached by passing i = 0.
425 * imsi must point at allocated space of (at least) 8 bytes.
426 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100427static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200428{
429 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
430}
431
432/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100433static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200434{
435 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
436}
437
438static char imsi_digit_to_char(uint8_t nibble)
439{
440 nibble &= 0x0f;
441 if (nibble > 9)
442 return (nibble == 0x0f) ? '\0' : '?';
443 return '0' + nibble;
444}
445
446/* Return a human readable IMSI string, in a static buffer.
447 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100448static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200449{
450 static char str[17];
451 int i;
452
453 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100454 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100455 c = imsi_digit_to_char(imsi[i]);
456 if (c == '?')
457 return -1;
458 str[2*i] = c;
459
460 c = imsi_digit_to_char(imsi[i] >> 4);
461 if (c == '?')
462 return -1;
463 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200464 }
465 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 *imsi_str = str;
467 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200468}
469
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100470/* Return 0 if not present, 1 if present and decoded successfully, -1 if
471 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100472static int get_ie_imsi_str(union gtpie_member *ie[], int i,
473 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100474{
475 uint8_t imsi_buf[8];
476 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100477 return 0;
478 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100479}
480
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100481/* Return 0 if not present, 1 if present and decoded successfully, -1 if
482 * present but cannot be decoded. */
483static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100484{
485 static char apn_buf[GSM_APN_LENGTH];
486 unsigned int len;
487 if (gtpie_gettlv(ie, GTPIE_APN, 0,
488 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100489 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100490
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100491 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100492 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
493 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100494 return -1;
495 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100496
497 if (len > (sizeof(apn_buf) - 1))
498 len = sizeof(apn_buf) - 1;
499 apn_buf[len] = '\0';
500
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100501 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
502 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100503 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
504 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100505 return -1;
506 }
507 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100508}
509
510
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200511/* Validate header, and index information elements. Write decoded packet
512 * information to *res. res->data will point at the given data buffer. On
513 * error, p->rc is set <= 0 (see enum gtp_rc). */
514static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100515 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200516 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100517 struct gtp_packet_desc *res,
518 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200519{
520 ZERO_STRUCT(res);
521 res->data = (union gtp_packet*)data;
522 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100523 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200524 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100525 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200526
527 validate_gtp_header(res);
528
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100529 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200530 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200531
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100532 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533
Neels Hofmeyre921e322015-11-11 00:45:50 +0100534 if (from_plane_idx == GTPH_PLANE_USER) {
535 res->rc = GTP_RC_PDU_U;
536 return;
537 }
538
539 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100540 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200541 return;
542 }
543
544 if (gtpie_decaps(res->ie, res->version,
545 (void*)(data + res->header_len),
546 res->data_len - res->header_len) != 0) {
547 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100548 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
Neels Hofmeyrbee75962015-12-06 23:07:02 +0100549 " Dropping GTP packet%s.\n",
550 gtp_type_str(res->type)
551 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200552 return;
553 }
554
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100555#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100556 /* TODO if (<loglevel is debug>) { ...
557 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200558 int i;
559
560 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100561 const char *imsi;
562 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200563 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100564 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200565 }
566
567 for (i = 0; i < 10; i++) {
568 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100569 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200570 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100571 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200572 }
573
574 for (i = 0; i < 2; i++) {
575 struct gsn_addr addr;
576 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100577 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200578 }
579
580 for (i = 0; i < 10; i++) {
581 uint32_t tei;
582 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
583 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100584 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200585 tei, tei);
586 }
587
588 for (i = 0; i < 10; i++) {
589 uint32_t tei;
590 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
591 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100592 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200593 tei, tei);
594 }
595#endif
596}
597
598
599/* expiry */
600
601void expiry_init(struct expiry *exq, int expiry_in_seconds)
602{
603 ZERO_STRUCT(exq);
604 exq->expiry_in_seconds = expiry_in_seconds;
605 INIT_LLIST_HEAD(&exq->items);
606}
607
608void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
609{
610 item->expiry = now + exq->expiry_in_seconds;
611
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100612 OSMO_ASSERT(llist_empty(&exq->items)
613 || (item->expiry
614 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
615
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200616 /* Add/move to the tail to always sort by expiry, ascending. */
617 llist_del(&item->entry);
618 llist_add_tail(&item->entry, &exq->items);
619}
620
621int expiry_tick(struct expiry *exq, time_t now)
622{
623 int expired = 0;
624 struct expiring_item *m, *n;
625 llist_for_each_entry_safe(m, n, &exq->items, entry) {
626 if (m->expiry <= now) {
627 expiring_item_del(m);
628 expired ++;
629 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200630 /* The items are added sorted by expiry. So when we hit
631 * an unexpired entry, only more unexpired ones will
632 * follow. */
633 break;
634 }
635 }
636 return expired;
637}
638
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100639void expiry_clear(struct expiry *exq)
640{
641 struct expiring_item *m, *n;
642 llist_for_each_entry_safe(m, n, &exq->items, entry) {
643 expiring_item_del(m);
644 }
645}
646
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200647void expiring_item_init(struct expiring_item *item)
648{
649 ZERO_STRUCT(item);
650 INIT_LLIST_HEAD(&item->entry);
651}
652
653void expiring_item_del(struct expiring_item *item)
654{
655 OSMO_ASSERT(item);
656 llist_del(&item->entry);
657 INIT_LLIST_HEAD(&item->entry);
658 if (item->del_cb) {
659 /* avoid loops */
660 del_cb_t del_cb = item->del_cb;
661 item->del_cb = 0;
662 (del_cb)(item);
663 }
664}
665
666
667/* nr_map, nr_pool */
668
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100669void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200670{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100671 *pool = (struct nr_pool){
672 .nr_min = nr_min,
673 .nr_max = nr_max,
674 .last_nr = nr_max
675 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200676}
677
678nr_t nr_pool_next(struct nr_pool *pool)
679{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100680 if (pool->last_nr >= pool->nr_max)
681 pool->last_nr = pool->nr_min;
682 else
683 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200684
685 return pool->last_nr;
686}
687
688void nr_map_init(struct nr_map *map, struct nr_pool *pool,
689 struct expiry *exq)
690{
691 ZERO_STRUCT(map);
692 map->pool = pool;
693 map->add_items_to_expiry = exq;
694 INIT_LLIST_HEAD(&map->mappings);
695}
696
697void nr_mapping_init(struct nr_mapping *m)
698{
699 ZERO_STRUCT(m);
700 INIT_LLIST_HEAD(&m->entry);
701 expiring_item_init(&m->expiry_entry);
702}
703
704void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
705{
706 /* Generate a mapped number */
707 mapping->repl = nr_pool_next(map->pool);
708
709 /* Add to the tail to always yield a list sorted by expiry, in
710 * ascending order. */
711 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100712 nr_map_refresh(map, mapping, now);
713}
714
715void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
716{
717 if (!map->add_items_to_expiry)
718 return;
719 expiry_add(map->add_items_to_expiry,
720 &mapping->expiry_entry,
721 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200722}
723
724void nr_map_clear(struct nr_map *map)
725{
726 struct nr_mapping *m;
727 struct nr_mapping *n;
728 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
729 nr_mapping_del(m);
730 }
731}
732
733int nr_map_empty(const struct nr_map *map)
734{
735 return llist_empty(&map->mappings);
736}
737
738struct nr_mapping *nr_map_get(const struct nr_map *map,
739 void *origin, nr_t nr_orig)
740{
741 struct nr_mapping *mapping;
742 llist_for_each_entry(mapping, &map->mappings, entry) {
743 if ((mapping->origin == origin)
744 && (mapping->orig == nr_orig))
745 return mapping;
746 }
747 /* Not found. */
748 return NULL;
749}
750
751struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
752{
753 struct nr_mapping *mapping;
754 llist_for_each_entry(mapping, &map->mappings, entry) {
755 if (mapping->repl == nr_repl) {
756 return mapping;
757 }
758 }
759 /* Not found. */
760 return NULL;
761}
762
763void nr_mapping_del(struct nr_mapping *mapping)
764{
765 OSMO_ASSERT(mapping);
766 llist_del(&mapping->entry);
767 INIT_LLIST_HEAD(&mapping->entry);
768 expiring_item_del(&mapping->expiry_entry);
769}
770
771
772/* gtphub */
773
774const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
775 "CTRL",
776 "USER",
777};
778
779const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
780 2123,
781 2152,
782};
783
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100784const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
785 "SGSN",
786 "GGSN",
787};
788
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200789time_t gtphub_now(void)
790{
791 struct timespec now_tp;
792 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
793 return now_tp.tv_sec;
794}
795
796/* Remove a gtphub_peer from its list and free it. */
797static void gtphub_peer_del(struct gtphub_peer *peer)
798{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100799 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200800 nr_map_clear(&peer->seq_map);
801 llist_del(&peer->entry);
802 talloc_free(peer);
803}
804
805static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
806{
807 OSMO_ASSERT(llist_empty(&pa->ports));
808 llist_del(&pa->entry);
809 talloc_free(pa);
810}
811
812static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
813{
814 OSMO_ASSERT(pp->ref_count == 0);
815 llist_del(&pp->entry);
Neels Hofmeyre38fb662015-12-06 16:44:14 +0100816 rate_ctr_group_free(pp->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200817 talloc_free(pp);
818}
819
820/* From the information in the gtp_packet_desc, return the address of a GGSN.
821 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100822static int gtphub_resolve_ggsn(struct gtphub *hub,
823 struct gtp_packet_desc *p,
824 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200825
826/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100827struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
828 const char *imsi_str,
829 const char *apn_ni_str);
830int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200831
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200832static void gtphub_zero(struct gtphub *hub)
833{
834 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100835 INIT_LLIST_HEAD(&hub->ggsn_lookups);
836 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200837}
838
839static int gtphub_sock_init(struct osmo_fd *ofd,
840 const struct gtphub_cfg_addr *addr,
841 osmo_fd_cb_t cb,
842 void *data,
843 int ofd_id)
844{
845 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100846 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200847 return -1;
848 }
849 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100850 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200851 return -1;
852 }
853
854 ofd->when = BSC_FD_READ;
855 ofd->cb = cb;
856 ofd->data = data;
857 ofd->priv_nr = ofd_id;
858
859 int rc;
860 rc = osmo_sock_init_ofd(ofd,
861 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
862 addr->addr_str, addr->port,
863 OSMO_SOCK_F_BIND);
864 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100865 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
866 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200867 return -1;
868 }
869
870 return 0;
871}
872
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100873static void gtphub_sock_close(struct osmo_fd *ofd)
874{
875 close(ofd->fd);
876 osmo_fd_unregister(ofd);
877 ofd->cb = NULL;
878}
879
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880static void gtphub_bind_init(struct gtphub_bind *b)
881{
882 ZERO_STRUCT(b);
883
884 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100885
886 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
887 &gtphub_ctrg_io_desc, 0);
888 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200889}
890
891static int gtphub_bind_start(struct gtphub_bind *b,
892 const struct gtphub_cfg_bind *cfg,
893 osmo_fd_cb_t cb, void *cb_data,
894 unsigned int ofd_id)
895{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100896 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
897 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
898 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
899 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200900 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100901 }
902 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
903 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
904 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200905 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100906 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100907 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200908 return 0;
909}
910
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100911static void gtphub_bind_free(struct gtphub_bind *b)
912{
913 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100914 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100915}
916
917static void gtphub_bind_stop(struct gtphub_bind *b) {
918 gtphub_sock_close(&b->ofd);
919 gtphub_bind_free(b);
920}
921
Neels Hofmeyr40348972015-11-17 12:22:28 +0100922/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923 * Return the number of bytes read, zero on error. */
924static int gtphub_read(const struct osmo_fd *from,
925 struct osmo_sockaddr *from_addr,
926 uint8_t *buf, size_t buf_len)
927{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100928 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200929
Neels Hofmeyr40348972015-11-17 12:22:28 +0100930 /* recvfrom requires the available length set in *from_addr_len. */
931 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200932 errno = 0;
933 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100934 (struct sockaddr*)&from_addr->a,
935 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200936 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
937 * is not truncated. Then maybe reduce buf's size. */
938
939 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100940 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
941 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200942 return 0;
943 }
944
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100945 LOG(LOGL_DEBUG, "Received %d bytes from %s: %s%s\n",
Neels Hofmeyr40348972015-11-17 12:22:28 +0100946 (int)received, osmo_sockaddr_to_str(from_addr),
Neels Hofmeyr36948bf2015-12-07 13:36:47 +0100947 osmo_hexdump(buf, received > 1000? 1000 : received),
948 received > 1000 ? "..." : "");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200949
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200950 return received;
951}
952
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100953static inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200954{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100955 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200956 OSMO_ASSERT(pp->ref_count < UINT_MAX);
957 pp->ref_count++;
958}
959
Holger Hans Peter Freyther91e0e1b2016-01-22 23:32:36 +0100960static inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200961{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100962 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200963 OSMO_ASSERT(pp->ref_count > 0);
964 pp->ref_count--;
965}
966
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100967static inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200968{
969 OSMO_ASSERT(p->version == 1);
970 p->data->gtp1l.h.seq = hton16(seq);
971 p->seq = seq;
972}
973
Holger Hans Peter Freytherfec29ab2016-01-22 23:36:22 +0100974static inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200975{
976 OSMO_ASSERT(p->version == 1);
977 p->data->gtp1l.h.tei = hton32(tei);
978 p->header_tei = tei;
979}
980
981static void gtphub_mapping_del_cb(struct expiring_item *expi);
982
983static struct nr_mapping *gtphub_mapping_new()
984{
985 struct nr_mapping *nrm;
986 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
987 OSMO_ASSERT(nrm);
988
989 nr_mapping_init(nrm);
990 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
991 return nrm;
992}
993
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100994
995#define APPEND(args...) \
996 l = snprintf(pos, left, args); \
997 pos += l; \
998 left -= l
999
1000static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
1001 int side_idx)
1002{
1003 static char buf[256];
1004 char *pos = buf;
1005 int left = sizeof(buf);
1006 int l;
1007
1008 struct gtphub_tunnel_endpoint *c, *u;
1009 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1010 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
1011
1012 /* print both only if they differ. */
1013 if (!c->peer) {
1014 APPEND("(uninitialized)");
1015 } else {
1016 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
1017 }
1018
1019 if (!u->peer) {
1020 if (c->peer) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001021 APPEND("/(uninitialized)");
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001022 }
1023 } else if ((!c->peer)
1024 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1025 &c->peer->peer_addr->addr))) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +01001026 APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr));
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001027 }
1028
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001029 APPEND(" (TEI C=%x U=%x)",
1030 c->tei_orig,
1031 u->tei_orig);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001032 return buf;
1033}
1034
1035const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1036{
1037 static char buf[512];
1038 char *pos = buf;
1039 int left = sizeof(buf);
1040 int l;
1041
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001042 APPEND("TEI=%x: ", tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001043 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1044 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1045
1046 return buf;
1047}
1048
1049#undef APPEND
1050
1051void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1052 struct gtphub_peer_port *pp)
1053{
1054 if (te->peer)
1055 gtphub_port_ref_count_dec(te->peer);
1056 te->peer = pp;
1057 if (te->peer)
1058 gtphub_port_ref_count_inc(te->peer);
1059}
1060
1061int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1062{
1063 if (!tun)
1064 return 0;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001065 if (!tun->tei_repl)
1066 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001067 int side_idx;
1068 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001069 for_each_side_and_plane(side_idx, plane_idx) {
1070 struct gtphub_tunnel_endpoint *te =
1071 &tun->endpoint[side_idx][plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001072 if (!(te->peer && te->tei_orig))
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001073 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001074 }
1075 return 1;
1076}
1077
1078static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1079{
1080 struct gtphub_tunnel *tun = container_of(expi,
1081 struct gtphub_tunnel,
1082 expiry_entry);
1083 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1084
1085 llist_del(&tun->entry);
1086 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1087
1088 expi->del_cb = 0; /* avoid recursion loops */
1089 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1090
1091 int side_idx;
1092 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001093 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001094 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1095
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001096 /* clear ref count */
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001097 gtphub_tunnel_endpoint_set_peer(te, NULL);
1098
1099 rate_ctr_group_free(te->counters_io);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001100 }
1101
1102 talloc_free(tun);
1103}
1104
1105static struct gtphub_tunnel *gtphub_tunnel_new()
1106{
1107 struct gtphub_tunnel *tun;
1108 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1109 OSMO_ASSERT(tun);
1110
1111 INIT_LLIST_HEAD(&tun->entry);
1112 expiring_item_init(&tun->expiry_entry);
1113
Neels Hofmeyre38fb662015-12-06 16:44:14 +01001114 int side_idx, plane_idx;
1115 for_each_side_and_plane(side_idx, plane_idx) {
1116 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
1117 te->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
1118 &gtphub_ctrg_io_desc,
1119 0);
1120 OSMO_ASSERT(te->counters_io);
1121 }
1122
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001123 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1124 return tun;
1125}
1126
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001127static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1128 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001129{
1130 if (llist_empty(&peer->addresses))
1131 return "(addressless)";
1132
1133 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1134 struct gtphub_peer_addr,
1135 entry);
1136 return gsn_addr_to_strb(&a->addr, buf, buflen);
1137}
1138
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001139static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1140 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001141{
1142 if (!port)
1143 return "(null port)";
1144
1145 snprintf(buf, buflen, "%s port %d",
1146 gsn_addr_to_str(&port->peer_addr->addr),
1147 (int)port->port);
1148 return buf;
1149}
1150
1151const char *gtphub_peer_str(struct gtphub_peer *peer)
1152{
1153 static char buf[256];
1154 return gtphub_peer_strb(peer, buf, sizeof(buf));
1155}
1156
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001157const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001158{
1159 static char buf[256];
1160 return gtphub_port_strb(port, buf, sizeof(buf));
1161}
1162
1163static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1164{
1165 static char buf[256];
1166 return gtphub_port_strb(port, buf, sizeof(buf));
1167}
1168
1169static void gtphub_mapping_del_cb(struct expiring_item *expi)
1170{
1171 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001172 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001173
1174 struct nr_mapping *nrm = container_of(expi,
1175 struct nr_mapping,
1176 expiry_entry);
1177 llist_del(&nrm->entry);
1178 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1179
1180 /* Just for log */
1181 struct gtphub_peer_port *from = nrm->origin;
1182 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001183 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001184 (int)nrm->expiry_entry.expiry,
1185 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001186 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001187
1188 gtphub_port_ref_count_dec(from);
1189
1190 talloc_free(nrm);
1191}
1192
1193static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1194 struct gtphub_peer_port *from,
1195 nr_t orig_nr,
1196 time_t now)
1197{
1198 struct nr_mapping *nrm;
1199
1200 nrm = nr_map_get(map, from, orig_nr);
1201
1202 if (!nrm) {
1203 nrm = gtphub_mapping_new();
1204 nrm->orig = orig_nr;
1205 nrm->origin = from;
1206 nr_map_add(map, nrm, now);
1207 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001208 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001209 gtphub_port_str(from),
1210 (int)(nrm->orig), (int)(nrm->repl));
1211 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001212 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001213 }
1214
1215 OSMO_ASSERT(nrm);
1216 return nrm;
1217}
1218
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001219static void gtphub_map_seq(struct gtp_packet_desc *p,
1220 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001221 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001222{
1223 /* Store a mapping in to_peer's map, so when we later receive a GTP
1224 * packet back from to_peer, the seq nr can be unmapped back to its
1225 * origin (from_peer here). */
1226 struct nr_mapping *nrm;
1227 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001228 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001229
1230 /* Change the GTP packet to yield the new, mapped seq nr */
1231 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001232}
1233
1234static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1235 struct gtphub_peer_port *responding_port)
1236{
1237 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001238 struct nr_mapping *nrm =
1239 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1240 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001241 if (!nrm)
1242 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001243 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1244 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001245 set_seq(p, nrm->orig);
1246 return nrm->origin;
1247}
1248
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001249static int gtphub_check_mapped_tei(struct gtphub_tunnel *new_tun,
1250 struct gtphub_tunnel *iterated_tun,
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001251 uint32_t *tei_min,
1252 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001253{
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001254 if (!new_tun->tei_repl || !iterated_tun->tei_repl)
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001255 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001256
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001257 *tei_min = (*tei_min < iterated_tun->tei_repl)? *tei_min : iterated_tun->tei_repl;
1258 *tei_max = (*tei_max > iterated_tun->tei_repl)? *tei_max : iterated_tun->tei_repl;
1259
1260 if (new_tun->tei_repl != iterated_tun->tei_repl)
1261 return 1;
1262
1263 /* new_tun->tei_repl is already taken. Try to find one out of the known
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001264 * range. */
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001265 LOG(LOGL_DEBUG, "TEI replacement %d already taken.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001266
1267 if ((*tei_max) < 0xffffffff) {
1268 (*tei_max)++;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001269 new_tun->tei_repl = *tei_max;
1270 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001271 return 1;
1272 } else if ((*tei_min) > 1) {
1273 (*tei_min)--;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001274 new_tun->tei_repl = *tei_min;
1275 LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_tun->tei_repl);
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001276 return 1;
1277 }
1278
1279 /* None seems to be available. */
1280 return 0;
1281}
1282
1283static int gtphub_check_reused_teis(struct gtphub *hub,
1284 struct gtphub_tunnel *new_tun)
1285{
1286 uint32_t tei_min = 0xffffffff;
1287 uint32_t tei_max = 0;
1288 int side_idx;
1289 int plane_idx;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001290 struct gtphub_tunnel_endpoint *te;
1291 struct gtphub_tunnel_endpoint *te2;
1292
1293 struct gtphub_tunnel *tun, *ntun;
1294
1295 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1296 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001297 continue;
1298
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001299 /* Check whether the GSN sent a TEI that it is reusing from a
1300 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001301 int tun_continue = 0;
1302 for_each_side(side_idx) {
1303 for_each_plane(plane_idx) {
1304 te = &tun->endpoint[side_idx][plane_idx];
1305 te2 = &new_tun->endpoint[side_idx][plane_idx];
Neels Hofmeyrf6e4d082015-12-06 19:03:35 +01001306 if ((te->tei_orig == 0)
1307 || (te->tei_orig != te2->tei_orig)
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001308 || (!te->peer)
1309 || (!te2->peer)
1310 || !gsn_addr_same(&te->peer->peer_addr->addr,
1311 &te2->peer->peer_addr->addr))
1312 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001313
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001314 /* The peer is reusing a TEI that I believe to
1315 * be part of another tunnel. The other tunnel
1316 * must be stale, then. */
1317 LOG(LOGL_NOTICE,
1318 "Expiring tunnel due to reused TEI:"
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001319 " %s peer %s sent %s TEI %x,"
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001320 " previously used by tunnel %s...\n",
Neels Hofmeyree1e5d72015-12-06 17:18:49 +01001321 gtphub_side_idx_names[side_idx],
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001322 gtphub_port_str(te->peer),
1323 gtphub_plane_idx_names[plane_idx],
1324 te->tei_orig,
1325 gtphub_tunnel_str(tun));
1326 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1327 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001328
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001329 expiring_item_del(&tun->expiry_entry);
1330 /* continue to find more matches. There shouldn't be
1331 * any, but let's make sure. However, tun is deleted,
1332 * so we need to skip to the next tunnel. */
1333 tun_continue = 1;
1334 break;
1335 }
1336 if (tun_continue)
1337 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001338 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001339 if (tun_continue)
1340 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001341
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001342 /* Check whether the mapped TEI is already used by another
1343 * tunnel. */
1344 if (!gtphub_check_mapped_tei(new_tun, tun, &tei_min, &tei_max)) {
1345 LOG(LOGL_ERROR,
1346 "No mapped TEI is readily available."
1347 " Searching for holes between occupied"
1348 " TEIs not implemented.");
1349 return 0;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001350 }
1351
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001352 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001353
1354 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001355}
1356
1357static void gtphub_tunnel_refresh(struct gtphub *hub,
1358 struct gtphub_tunnel *tun,
1359 time_t now)
1360{
1361 expiry_add(&hub->expire_slowly,
1362 &tun->expiry_entry,
1363 now);
1364}
1365
1366static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1367 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001368 struct gtphub_peer_port *from,
1369 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001370{
1371 OSMO_ASSERT(from);
1372 int other_side = other_side_idx(p->side_idx);
1373
1374 struct gtphub_tunnel *tun;
1375 llist_for_each_entry(tun, &hub->tunnels, entry) {
1376 struct gtphub_tunnel_endpoint *te_from =
1377 &tun->endpoint[p->side_idx][p->plane_idx];
1378 struct gtphub_tunnel_endpoint *te_to =
1379 &tun->endpoint[other_side][p->plane_idx];
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001380 if ((tun->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001381 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001382 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1383 &from->peer_addr->addr)) {
1384 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001385 if (unmapped_from_tun)
1386 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001387 return te_to;
1388 }
1389 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001390
1391 if (unmapped_from_tun)
1392 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001393 return NULL;
1394}
1395
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001396static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001397 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001398{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001399 if (p->rc != GTP_RC_PDU_C)
1400 return;
1401
1402 int ie_idx;
1403 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1404 if (ie_idx < 0)
1405 return;
1406
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001407 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001408 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001409}
1410
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001411static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001412 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001413 struct gtphub *hub,
1414 struct gtp_packet_desc *p,
1415 struct gtphub_peer_port *from_port)
1416{
1417 OSMO_ASSERT(p->version == 1);
1418 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001419 if (unmapped_from_tun)
1420 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001421
1422 /* If the header's TEI is zero, no PDP context has been established
1423 * yet. If nonzero, a mapping should actually already exist for this
1424 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001425 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001426 return 0;
1427
1428 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001429 * mapped in a tunnel struct. */
1430 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001431 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001432 if (!to) {
1433 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1434 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001435 return -1;
1436 }
Neels Hofmeyr312bf6c2016-04-14 15:21:31 +02001437
1438 if (unmapped_from_tun) {
1439 OSMO_ASSERT(*unmapped_from_tun);
1440 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1441 gtphub_tunnel_str(*unmapped_from_tun));
1442 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001443
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001444 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001445 set_tei(p, unmapped_tei);
1446
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001447 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001448 *to_port_p = to->peer;
1449
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001450 return 0;
1451}
1452
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001453static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1454 struct gtp_packet_desc *p,
1455 struct gtphub_peer_port *from_ctrl,
1456 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001457{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001458 int plane_idx;
1459
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001460 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1461 plane_nrs_match_GSN_addr_IE_indices);
1462
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001463 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001464
1465 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001466 if (p->side_idx != GTPH_SIDE_SGSN) {
1467 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001468 " Request from the GGSN side: %s",
1469 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001470 return -1;
1471 }
1472
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001473 if (tun) {
1474 LOG(LOGL_ERROR, "Not implemented: Received"
1475 " Create PDP Context Request for an already"
1476 " established tunnel:"
1477 " from %s, tunnel %s\n",
1478 gtphub_port_str(from_ctrl),
1479 gtphub_tunnel_str(p->tun));
1480 return -1;
1481 }
1482
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001483 /* A new tunnel. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001484 p->tun = tun = gtphub_tunnel_new();
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001485
1486 /* Create TEI mapping */
1487 tun->tei_repl = nr_pool_next(&hub->tei_pool);
1488
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001489 llist_add(&tun->entry, &hub->tunnels);
1490 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001491 /* The endpoint peers on this side (SGSN) will be set from IEs
1492 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001493 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001494 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001495 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001496 if (p->side_idx != GTPH_SIDE_GGSN) {
1497 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001498 " Response from the SGSN side: %s",
1499 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001500 return -1;
1501 }
1502
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001503 /* The tunnel should already have been resolved from the header
1504 * TEI and be available in tun (== p->tun). Just fill in the
1505 * GSN Addresses below.*/
1506 OSMO_ASSERT(tun);
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001507 OSMO_ASSERT(tun->tei_repl == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001508 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001509 }
1510
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001511 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1512 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001513 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001514
1515 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001516 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001517 struct gsn_addr use_addr;
1518 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001519 uint32_t tei_from_ie;
1520 int ie_idx;
1521
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001522 /* Fetch GSN Address and TEI from IEs. As ensured by above
1523 * static asserts, plane_idx corresponds to the GSN Address IE
1524 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001525 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001526 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001527 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1528 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001529 return -1;
1530 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001531 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001532 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001533 gsn_addr_to_str(&use_addr),
1534 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001535
1536 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1537 if (ie_idx < 0) {
1538 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001539 LOG(LOGL_ERROR,
1540 "Create PDP Context message invalid:"
1541 " missing IE %d\n",
1542 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543 return -1;
1544 }
1545 tei_from_ie = 0;
1546 }
1547 else
1548 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1549
1550 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001551 * exists.
1552 *
1553 * Exception: if sgsn_use_sender is set, instead use the
1554 * sender's address and port for Ctrl -- the User port is not
1555 * known until the first User packet arrives.
1556 *
1557 * Note: doing this here is just an optimization, because
1558 * gtphub_handle_buf() has code to replace the tunnel
1559 * endpoints' addresses with the sender (needed for User
1560 * plane). We could just ignore sgsn_use_sender here. But if we
1561 * set up a default port here and replace it in
1562 * gtphub_handle_buf(), we'd be creating a peer port just to
1563 * expire it right away. */
1564 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
1565 gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1566 } else {
1567 use_port = gtphub_plane_idx_default_port[plane_idx];
1568
1569 }
1570
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001571 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001572 peer_from_ie = gtphub_port_have(hub,
1573 &hub->to_gsns[side_idx][plane_idx],
1574 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001575
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001576 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1577 peer_from_ie);
1578
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001579 if (!tei_from_ie &&
1580 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1581 LOG(LOGL_ERROR,
Neels Hofmeyr328d2f72015-12-06 19:13:21 +01001582 "Create PDP Context message omits %s TEI, but"
1583 " no TEI has been announced for this tunnel: %s\n",
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001584 gtphub_plane_idx_names[plane_idx],
1585 gtphub_tunnel_str(tun));
1586 return -1;
1587 }
1588
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001589 if (tei_from_ie) {
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001590 /* Replace TEI in GTP packet IE */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001591 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
Neels Hofmeyree07e4f2015-12-06 19:11:45 +01001592 p->ie[ie_idx]->tv4.v = hton32(tun->tei_repl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001593
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001594 if (!gtphub_check_reused_teis(hub, tun)) {
1595 /* It's highly unlikely that all TEIs are
1596 * taken. But the code looking for an unused
1597 * TEI is, at the time of writing this comment,
1598 * not able to find gaps in the TEI space. To
1599 * explicitly alert the user of this problem,
1600 * rather abort than carry on. */
1601 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1602 abort();
1603 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001604 }
1605
1606 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001607 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1608 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001609 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001610 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1611 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001612 return -1;
1613 }
1614 }
1615
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001616 if (p->type == GTP_CREATE_PDP_REQ) {
1617 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1618 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001619 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001620 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1621 gtphub_tunnel_str(tun));
1622 }
1623
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001624 return 0;
1625}
1626
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001627static void pending_delete_del_cb(struct expiring_item *expi)
1628{
1629 struct pending_delete *pd;
1630 pd = container_of(expi, struct pending_delete, expiry_entry);
1631
1632 llist_del(&pd->entry);
1633 INIT_LLIST_HEAD(&pd->entry);
1634
1635 pd->expiry_entry.del_cb = 0;
1636 expiring_item_del(&pd->expiry_entry);
1637
1638 talloc_free(pd);
1639}
1640
1641static struct pending_delete *pending_delete_new(void)
1642{
1643 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1644 INIT_LLIST_HEAD(&pd->entry);
1645 expiring_item_init(&pd->expiry_entry);
1646 pd->expiry_entry.del_cb = pending_delete_del_cb;
1647 return pd;
1648}
1649
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001650static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1651 struct gtp_packet_desc *p,
1652 struct gtphub_peer_port *from_ctrl,
1653 struct gtphub_peer_port *to_ctrl)
1654{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001655 struct gtphub_tunnel *known_tun = p->tun;
1656
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001657 if (p->type == GTP_DELETE_PDP_REQ) {
1658 if (!known_tun) {
1659 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1660 return -1;
1661 }
1662
1663 /* Store the Delete Request until a successful Response is seen. */
1664 uint8_t teardown_ind;
1665 uint8_t nsapi;
1666
1667 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1668 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1669 return -1;
1670 }
1671
1672 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1673 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1674 return -1;
1675 }
1676
1677 struct pending_delete *pd = NULL;
1678
1679 struct pending_delete *pdi = NULL;
1680 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1681 if ((pdi->tun == known_tun)
1682 && (pdi->teardown_ind == teardown_ind)
1683 && (pdi->nsapi == nsapi)) {
1684 pd = pdi;
1685 break;
1686 }
1687 }
1688
1689 if (!pd) {
1690 pd = pending_delete_new();
1691 pd->tun = known_tun;
1692 pd->teardown_ind = teardown_ind;
1693 pd->nsapi = nsapi;
1694
1695 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1696 gtphub_tunnel_str(known_tun));
1697 llist_add(&pd->entry, &hub->pending_deletes);
1698 }
1699
1700 /* Add or refresh timeout. */
1701 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1702
1703 /* If a pending_delete should expire before the response to
1704 * indicate success comes in, the responding peer will have the
1705 * tunnel deactivated, while the requesting peer gets no reply
1706 * and keeps the tunnel. The hope is that the requesting peer
1707 * will try again and get a useful response. */
1708 } else if (p->type == GTP_DELETE_PDP_RSP) {
1709 /* Find the Delete Request for this Response. */
1710 struct pending_delete *pd = NULL;
1711
1712 struct pending_delete *pdi;
1713 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1714 if (known_tun == pdi->tun) {
1715 pd = pdi;
1716 break;
1717 }
1718 }
1719
1720 if (!pd) {
1721 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1722 " Cannot find matching request.");
1723 /* If we delete the tunnel now, anyone can send a
1724 * Delete response to kill tunnels at will. */
1725 return -1;
1726 }
1727
1728 /* TODO handle teardown_ind and nsapi */
1729
1730 expiring_item_del(&pd->expiry_entry);
1731
1732 uint8_t cause;
1733 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1734 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1735 " Missing Cause IE.");
1736 /* If we delete the tunnel now, at least one of the
1737 * peers may still think it is active. */
1738 return -1;
1739 }
1740
1741 if (cause != GTPCAUSE_ACC_REQ) {
1742 LOG(LOGL_NOTICE,
1743 "Delete PDP Context Response indicates failure;"
1744 "for %s\n",
1745 gtphub_tunnel_str(known_tun));
1746 return -1;
1747 }
1748
1749 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1750 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001751 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001752 expiring_item_del(&known_tun->expiry_entry);
1753 }
1754
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001755 return 0;
1756}
1757
1758static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1759 struct gtp_packet_desc *p,
1760 struct gtphub_peer_port *from_ctrl,
1761 struct gtphub_peer_port *to_ctrl)
1762{
1763 /* TODO */
1764 return 0;
1765}
1766
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001767/* Read GSN address IEs from p, and make sure these peer addresses exist in
1768 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1769 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1770 * the packet p. */
1771static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1772 struct gtp_packet_desc *p,
1773 struct gtphub_peer_port *from_ctrl,
1774 struct gtphub_peer_port *to_ctrl)
1775{
1776 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1777
1778 switch (p->type) {
1779 case GTP_CREATE_PDP_REQ:
1780 case GTP_CREATE_PDP_RSP:
1781 return gtphub_handle_create_pdp_ctx(hub, p,
1782 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001783
1784 case GTP_DELETE_PDP_REQ:
1785 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001786 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001787 from_ctrl, to_ctrl);
1788
1789 case GTP_UPDATE_PDP_REQ:
1790 case GTP_UPDATE_PDP_RSP:
1791 return gtphub_handle_update_pdp_ctx(hub, p,
1792 from_ctrl, to_ctrl);
1793
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001794 default:
1795 /* Nothing to do for this message type. */
1796 return 0;
1797 }
1798
1799}
1800
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001801static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1802 struct gtphub_tunnel *tun,
1803 int to_side)
1804{
1805 static uint8_t del_ctx_msg[16] = {
1806 0x32, /* GTP v1 flags */
1807 GTP_DELETE_PDP_REQ,
1808 0x00, 0x08, /* Length in network byte order */
1809 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1810 0, 0, /* Seq, to be replaced */
1811 0, 0, /* no extensions */
1812 0x13, 0xff, /* 19: Teardown ind = 1 */
1813 0x14, 0 /* 20: NSAPI = 0 */
1814 };
1815
1816 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1817 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1818
1819 struct gtphub_tunnel_endpoint *te =
1820 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1821
1822 if (! te->peer)
1823 return 0;
1824
1825 *tei = hton32(te->tei_orig);
1826 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1827
1828 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001829 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1830 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001831 if (rc != 0) {
1832 LOG(LOGL_ERROR,
1833 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1834 gtphub_port_str(te->peer));
1835 }
1836 return rc;
1837}
1838
1839/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1840static void gtphub_restarted(struct gtphub *hub,
1841 struct gtp_packet_desc *p,
1842 struct gtphub_peer_port *pp)
1843{
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001844 LOG(LOGL_NOTICE, "Peer has restarted: %s\n",
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001845 gtphub_port_str(pp));
1846
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001847 int deleted_count = 0;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001848 struct gtphub_tunnel *tun;
1849 llist_for_each_entry(tun, &hub->tunnels, entry) {
1850 int side_idx;
1851 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001852 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1853 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1854 if ((!te->peer)
1855 || (!te2->tei_orig)
1856 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001857 continue;
1858
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001859 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1860 gtphub_tunnel_str(tun));
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001861 deleted_count ++;
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001862
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001863 /* Send a Delete PDP Context Request to the
1864 * peer on the other side, remember the pending
1865 * delete and wait for the response to delete
1866 * the tunnel. Clear this side of the tunnel to
1867 * make sure it isn't used.
1868 *
1869 * Should the delete message send fail, or if no
1870 * response is received, this tunnel will expire. If
1871 * its TEIs come up in a new PDP Context Request, it
1872 * will be removed. If messages for this tunnel should
1873 * come in (from the not restarted side), they will be
1874 * dropped because the tunnel is rendered unusable. */
1875 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1876
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001877 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1878 NULL);
1879 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1880 NULL);
1881 }
1882 }
Neels Hofmeyrbee75962015-12-06 23:07:02 +01001883
1884 if (deleted_count)
1885 LOG(LOGL_NOTICE, "Deleting %d tunnels due to restart of: %s\n",
1886 deleted_count,
1887 gtphub_port_str(pp));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001888}
1889
1890static int get_restart_count(struct gtp_packet_desc *p)
1891{
1892 int ie_idx;
1893 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1894 if (ie_idx < 0)
1895 return -1;
1896 return ntoh8(p->ie[ie_idx]->tv1.v);
1897}
1898
1899static void gtphub_check_restart_counter(struct gtphub *hub,
1900 struct gtp_packet_desc *p,
1901 struct gtphub_peer_port *from)
1902{
1903 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1904 * that doesn't match the peer's previously sent restart counter, clear
1905 * that peer and cancel PDP contexts. */
1906
1907 int restart = get_restart_count(p);
1908
1909 if ((restart < 0) || (restart > 255))
1910 return;
1911
1912 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1913 if (from->last_restart_count != restart) {
1914 gtphub_restarted(hub, p, from);
1915 }
1916 }
1917
1918 from->last_restart_count = restart;
1919}
1920
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001921static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1922{
1923 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1924 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001925 LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n",
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001926 gtphub_plane_idx_names[plane_idx]);
1927
1928 if (!(what & BSC_FD_READ))
1929 return 0;
1930
1931 struct gtphub *hub = from_sgsns_ofd->data;
1932
1933 static uint8_t buf[4096];
1934 struct osmo_sockaddr from_addr;
1935 struct osmo_sockaddr to_addr;
1936 struct osmo_fd *to_ofd;
1937 int len;
1938 uint8_t *reply_buf;
1939
1940 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1941 if (len < 1)
1942 return 0;
1943
1944 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1945 buf, len, gtphub_now(),
1946 &reply_buf, &to_ofd, &to_addr);
1947 if (len < 1)
1948 return 0;
1949
1950 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1951}
1952
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001953static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1954{
1955 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1956 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001957 LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001958 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001959 if (!(what & BSC_FD_READ))
1960 return 0;
1961
1962 struct gtphub *hub = from_ggsns_ofd->data;
1963
1964 static uint8_t buf[4096];
1965 struct osmo_sockaddr from_addr;
1966 struct osmo_sockaddr to_addr;
1967 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001968 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001969 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001970
1971 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1972 if (len < 1)
1973 return 0;
1974
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001975 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1976 buf, len, gtphub_now(),
1977 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001978 if (len < 1)
1979 return 0;
1980
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001981 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001982}
1983
1984static int gtphub_unmap(struct gtphub *hub,
1985 struct gtp_packet_desc *p,
1986 struct gtphub_peer_port *from,
1987 struct gtphub_peer_port *to_proxy,
1988 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001989 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990{
1991 /* Always (try to) unmap sequence and TEI numbers, which need to be
1992 * replaced in the packet. Either way, give precedence to the proxy, if
1993 * configured. */
1994
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001995 if (unmapped_from_seq)
1996 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001997 if (final_unmapped)
1998 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001999 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002000
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002001 struct gtphub_peer_port *from_seq = NULL;
2002 struct gtphub_peer_port *from_tei = NULL;
2003 struct gtphub_peer_port *unmapped = NULL;
2004
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002005 from_seq = gtphub_unmap_seq(p, from);
2006
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002007 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002008 return -1;
2009
2010 struct gtphub_peer *from_peer = from->peer_addr->peer;
2011 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002012 LOG(LOGL_DEBUG,
2013 "Seq unmap and TEI unmap yield two different peers."
2014 " Using seq unmap."
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01002015 " (from %s %s: seq %d yields %s, tei %u yields %s)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002016 gtphub_plane_idx_names[p->plane_idx],
2017 gtphub_peer_str(from_peer),
2018 (int)p->seq,
2019 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002020 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002021 gtphub_port_str2(from_tei)
2022 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002023 }
2024 unmapped = (from_seq? from_seq : from_tei);
2025
2026 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002027 LOG(LOGL_NOTICE,
2028 "Unmap yields a different peer than the configured proxy."
2029 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002030 " unmapped: %s proxy: %s\n",
2031 gtphub_port_str(unmapped),
2032 gtphub_port_str2(to_proxy)
2033 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002034 }
2035 unmapped = (to_proxy? to_proxy : unmapped);
2036
2037 if (!unmapped) {
2038 /* Return no error, but returned pointers are all NULL. */
2039 return 0;
2040 }
2041
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002042 if (unmapped_from_seq)
2043 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002044 if (final_unmapped)
2045 *final_unmapped = unmapped;
2046 return 0;
2047}
2048
2049static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2050 uint16_t port,
2051 struct osmo_sockaddr *dst)
2052{
2053 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2054}
2055
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002056/* If p is an Echo request, replace p's data with the matching response and
2057 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2058 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002059static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2060 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002061{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002062 if (p->type != GTP_ECHO_REQ)
2063 return 0;
2064
2065 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002066 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002067 GTP_ECHO_RSP,
2068 0x00, 14 - 8, /* Length in network byte order */
2069 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2070 0, 0, /* Seq, to be replaced */
2071 0, 0, /* no extensions */
2072 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002073 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002074 };
2075 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2076 uint8_t *recovery = &echo_response_data[13];
2077
2078 *seq = hton16(p->seq);
2079 *recovery = hub->restart_counter;
2080
2081 *reply_buf = echo_response_data;
2082
2083 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002084}
2085
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002086struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2087 const struct osmo_sockaddr *addr);
2088
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002089/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2090 * address to forward to. Return a pointer to the osmo_fd, but copy the
2091 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2092 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2093 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002094int gtphub_handle_buf(struct gtphub *hub,
2095 unsigned int side_idx,
2096 unsigned int plane_idx,
2097 const struct osmo_sockaddr *from_addr,
2098 uint8_t *buf,
2099 size_t received,
2100 time_t now,
2101 uint8_t **reply_buf,
2102 struct osmo_fd **to_ofd,
2103 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002104{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002105 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2106 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002107
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002108 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2109 received);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002110
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002111 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002112 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002113
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002114 LOG(LOGL_DEBUG, "%s rx %s from %s %s%s\n",
2115 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
2116 gtphub_plane_idx_names[plane_idx],
2117 gtphub_side_idx_names[side_idx],
2118 osmo_sockaddr_to_str(from_addr),
2119 gtp_type_str(p.type));
2120
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002121 if (p.rc <= 0) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002122 LOG(LOGL_ERROR, "INVALID: dropping GTP packet%s from %s %s %s\n",
2123 gtp_type_str(p.type),
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002124 gtphub_side_idx_names[side_idx],
2125 gtphub_plane_idx_names[plane_idx],
2126 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002127 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002128 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002129
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002130 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2131
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002132 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002133 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002134 if (reply_len > 0) {
2135 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002136 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002137 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002138
2139 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2140 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2141 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002142 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2143 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2144 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002145 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002146 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002147 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002148 if (reply_len < 0)
2149 return -1;
2150
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002151 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002152
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002153 /* If a proxy is configured, check that it's indeed that proxy talking
2154 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2155 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002156 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2157 if (from_peer) {
2158 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002159 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002160 "Rejecting: %s proxy configured, but GTP packet"
2161 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002162 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002163 gtphub_side_idx_names[side_idx],
2164 gtphub_side_idx_names[side_idx],
2165 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002166 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002167 return -1;
2168 }
2169 }
2170
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002171 if (!from_peer) {
2172 /* Find or create a peer with a matching address. The sender's
2173 * port may in fact differ. */
2174 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002175 }
2176
2177 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002178 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2179 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002180 if (!from_peer) {
2181 if (side_idx == GTPH_SIDE_GGSN) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002182 LOG(LOGL_ERROR, "Dropping packet%s: unknown GGSN peer: %s\n",
2183 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002184 osmo_sockaddr_to_str(from_addr));
2185 return -1;
2186 } else {
2187 /* SGSN */
2188 /* A new peer. If this is on the Ctrl plane, an SGSN
2189 * may make first contact without being known yet, so
2190 * create the peer struct for the current sender. */
2191 if (plane_idx != GTPH_PLANE_CTRL) {
2192 LOG(LOGL_ERROR,
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002193 "Dropping packet%s: User plane peer was not"
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002194 "announced by PDP Context: %s\n",
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002195 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002196 osmo_sockaddr_to_str(from_addr));
2197 return -1;
2198 }
2199
2200 struct gsn_addr from_gsna;
2201 uint16_t from_port;
2202 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2203 return -1;
2204
2205 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2206 }
2207 }
2208
2209 if (!from_peer) {
2210 /* This could theoretically happen for invalid address data or
2211 * somesuch. */
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002212 LOG(LOGL_ERROR, "Dropping packet%s: invalid %s peer: %s\n",
2213 gtp_type_str(p.type),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002214 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002215 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002216 return -1;
2217 }
2218
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002219 rate_ctr_add(&from_peer->counters_io->ctr[GTPH_CTR_BYTES_IN],
2220 received);
2221 rate_ctr_inc(&from_peer->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2222
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002223 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2224 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002225
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002226 gtphub_check_restart_counter(hub, &p, from_peer);
2227 gtphub_map_restart_counter(hub, &p);
2228
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002229 struct gtphub_peer_port *to_peer_from_seq;
2230 struct gtphub_peer_port *to_peer;
2231 if (gtphub_unmap(hub, &p, from_peer,
2232 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002233 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002234 != 0) {
2235 return -1;
2236 }
2237
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002238 if (p.tun) {
2239 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[p.side_idx][p.plane_idx];
2240 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_IN],
2241 received);
2242 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2243 }
2244
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002245 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2246 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2247 return -1;
2248 }
2249
Neels Hofmeyrd010c492015-12-06 23:12:02 +01002250 if (!to_peer && p.tun && p.type == GTP_DELETE_PDP_RSP) {
2251 /* It's a delete confirmation for a tunnel that is partly
2252 * invalid, probably marked unsuable due to a restarted peer.
2253 * Remove the tunnel and be happy without forwarding. */
2254 expiring_item_del(&p.tun->expiry_entry);
2255 p.tun = NULL;
2256 return 0;
2257 }
2258
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002259 if (!to_peer) {
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002260 LOG(LOGL_ERROR, "No %s to send to. Dropping packet%s"
2261 " (type=%" PRIu8 ", header-TEI=%" PRIx32 ", seq=%" PRIx16 ").\n",
2262 gtphub_side_idx_names[other_side_idx(side_idx)],
2263 gtp_type_str(p.type),
2264 p.type, p.header_tei_rx, p.seq
2265 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002266 return -1;
2267 }
2268
2269 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002270 /* This may be a Create PDP Context response. If it is, there
2271 * are other addresses in the GTP message to set up apart from
2272 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002273 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002274 != 0)
2275 return -1;
2276 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002277
2278 /* Either to_peer was resolved from an existing tunnel,
2279 * or a PDP Ctx and thus a tunnel has just been created,
2280 * or the tunnel has been deleted due to this message. */
2281 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002282
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002283 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002284 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002285 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002286 if (!to_peer_from_seq)
2287 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002288
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002289 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002290
2291 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002292
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002293 if (received) {
2294 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2295 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2296 received);
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002297
2298 rate_ctr_inc(&to_peer->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2299 rate_ctr_add(&to_peer->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2300 received);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002301 }
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002302
2303 if (p.tun) {
2304 struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[other_side_idx(p.side_idx)][p.plane_idx];
2305 rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2306 rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2307 received);
2308 }
2309
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002310 LOG(LOGL_DEBUG, "%s Forward to %s:"
2311 " header-TEI %" PRIx32", seq %" PRIx16", %d bytes to %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002312 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2313 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyrbee75962015-12-06 23:07:02 +01002314 p.header_tei, p.seq,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002315 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002316 return received;
2317}
2318
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002319static void resolved_gssn_del_cb(struct expiring_item *expi)
2320{
2321 struct gtphub_resolved_ggsn *ggsn;
2322 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2323
2324 gtphub_port_ref_count_dec(ggsn->peer);
2325 llist_del(&ggsn->entry);
2326
2327 ggsn->expiry_entry.del_cb = 0;
2328 expiring_item_del(&ggsn->expiry_entry);
2329
2330 talloc_free(ggsn);
2331}
2332
2333void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2334 struct gsn_addr *resolved_addr,
2335 time_t now)
2336{
2337 struct gtphub_peer_port *pp;
2338 struct gtphub_resolved_ggsn *ggsn;
2339
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002340 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002341 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2342 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002343
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002344 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002345 resolved_addr, 2123);
2346 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002347 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2348 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002349 return;
2350 }
2351
2352 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2353 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002354 INIT_LLIST_HEAD(&ggsn->entry);
2355 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002356
2357 ggsn->peer = pp;
2358 gtphub_port_ref_count_inc(pp);
2359
2360 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002361 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002362
2363 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002364 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002365
2366 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2367}
2368
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002369static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2370{
2371 return pp->ref_count == 0;
2372}
2373
2374static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2375{
2376 struct gtphub_peer_port *pp, *npp;
2377 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2378 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002379 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002380 gtphub_port_str(pp));
2381 gtphub_peer_port_del(pp);
2382 }
2383 }
2384 return llist_empty(&pa->ports);
2385}
2386
2387static int gtphub_gc_peer(struct gtphub_peer *p)
2388{
2389 struct gtphub_peer_addr *pa, *npa;
2390 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2391 if (gtphub_gc_peer_addr(pa)) {
2392 gtphub_peer_addr_del(pa);
2393 }
2394 }
2395
2396 /* Note that there's a ref_count in each gtphub_peer_port instance
2397 * listed within p->addresses, referenced by TEI mappings from
2398 * hub->tei_map. As long as those don't expire, this peer will stay. */
2399
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002400 return llist_empty(&p->addresses)
2401 && nr_map_empty(&p->seq_map);
2402}
2403
2404static void gtphub_gc_bind(struct gtphub_bind *b)
2405{
2406 struct gtphub_peer *p, *n;
2407 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2408 if (gtphub_gc_peer(p)) {
2409 gtphub_peer_del(p);
2410 }
2411 }
2412}
2413
2414void gtphub_gc(struct gtphub *hub, time_t now)
2415{
2416 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002417 expired = expiry_tick(&hub->expire_quickly, now);
2418 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002419
2420 /* ... */
2421
2422 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002423 int s, p;
2424 for_each_side_and_plane(s, p) {
2425 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002426 }
2427 }
2428}
2429
2430static void gtphub_gc_cb(void *data)
2431{
2432 struct gtphub *hub = data;
2433 gtphub_gc(hub, gtphub_now());
2434 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2435}
2436
2437static void gtphub_gc_start(struct gtphub *hub)
2438{
2439 hub->gc_timer.cb = gtphub_gc_cb;
2440 hub->gc_timer.data = hub;
2441
2442 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2443}
2444
2445/* called by unit tests */
2446void gtphub_init(struct gtphub *hub)
2447{
2448 gtphub_zero(hub);
2449
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002450 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002451 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002452
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002453 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2454 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002455
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002456 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2457
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002458 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002459 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002460 for_each_side_and_plane(side_idx, plane_idx) {
2461 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002462 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002463
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002464 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2465 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2466 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2467 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002468}
2469
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002470/* For the test suite, this is kept separate from gtphub_stop(), which also
2471 * closes sockets. The test suite avoids using sockets and would cause
2472 * segfaults when trying to close uninitialized ofds. */
2473void gtphub_free(struct gtphub *hub)
2474{
2475 /* By expiring all mappings, a garbage collection should free
2476 * everything else. A gtphub_bind_free() will assert that everything is
2477 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002478 expiry_clear(&hub->expire_quickly);
2479 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002480
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002481 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002482 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002483 for_each_side_and_plane(side_idx, plane_idx) {
2484 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2485 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002486 }
2487}
2488
2489void gtphub_stop(struct gtphub *hub)
2490{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002491 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002492 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002493 for_each_side_and_plane(side_idx, plane_idx) {
2494 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002495 }
2496 gtphub_free(hub);
2497}
2498
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002499static int gtphub_make_proxy(struct gtphub *hub,
2500 struct gtphub_peer_port **pp,
2501 struct gtphub_bind *bind,
2502 const struct gtphub_cfg_addr *addr)
2503{
2504 if (!addr->addr_str)
2505 return 0;
2506
2507 struct gsn_addr gsna;
2508 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2509 return -1;
2510
2511 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2512
2513 /* This is *the* proxy. Make sure it is never expired. */
2514 gtphub_port_ref_count_inc(*pp);
2515 return 0;
2516}
2517
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002518int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2519 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002520{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002521 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002522
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002523 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002524 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002525
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002526 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002527 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002528 if (gtphub_ares_init(hub) != 0) {
2529 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2530 return -1;
2531 }
2532 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002533
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002534 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002535 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002536 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002537 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002538 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2539 &cfg->to_gsns[side_idx][plane_idx],
2540 (side_idx == GTPH_SIDE_SGSN)
2541 ? from_sgsns_read_cb
2542 : from_ggsns_read_cb,
2543 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002544 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002545 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2546 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002547 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002548 return rc;
2549 }
2550 }
2551
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002552 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002553 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002554 &hub->proxy[side_idx][plane_idx],
2555 &hub->to_gsns[side_idx][plane_idx],
2556 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002557 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002558 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002559 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002560 gtphub_side_idx_names[side_idx],
2561 cfg->proxy[side_idx][plane_idx].addr_str,
2562 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002563 return -1;
2564 }
2565 }
2566
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002567 for_each_side_and_plane(side_idx, plane_idx) {
2568 if (hub->proxy[side_idx][plane_idx])
2569 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2570 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002571 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002572 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002573 }
2574
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002575 if (hub->sgsn_use_sender)
Neels Hofmeyr9d8f5062015-12-03 14:52:33 +01002576 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 +01002577
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002578 gtphub_gc_start(hub);
2579 return 0;
2580}
2581
2582static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2583 const struct gsn_addr *addr)
2584{
2585 struct gtphub_peer_addr *a;
2586 llist_for_each_entry(a, &peer->addresses, entry) {
2587 if (gsn_addr_same(&a->addr, addr))
2588 return a;
2589 }
2590 return NULL;
2591}
2592
2593static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2594 uint16_t port)
2595{
2596 OSMO_ASSERT(port);
2597 struct gtphub_peer_port *pp;
2598 llist_for_each_entry(pp, &a->ports, entry) {
2599 if (pp->port == port)
2600 return pp;
2601 }
2602 return NULL;
2603}
2604
2605static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2606 const struct gsn_addr *addr)
2607{
2608 struct gtphub_peer *peer;
2609 llist_for_each_entry(peer, &bind->peers, entry) {
2610 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2611 if (a)
2612 return a;
2613 }
2614 return NULL;
2615}
2616
2617static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2618 const struct gsn_addr *addr,
2619 uint16_t port)
2620{
2621 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2622 if (!a)
2623 return NULL;
2624 return gtphub_addr_find_port(a, port);
2625}
2626
2627struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2628 const struct osmo_sockaddr *addr)
2629{
2630 struct gsn_addr gsna;
2631 uint16_t port;
2632 gsn_addr_from_sockaddr(&gsna, &port, addr);
2633 return gtphub_port_find(bind, &gsna, port);
2634}
2635
2636static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2637 struct gtphub_bind *bind)
2638{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002639 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2640 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002641 OSMO_ASSERT(peer);
2642
2643 INIT_LLIST_HEAD(&peer->addresses);
2644
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002645 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002646 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002647
2648 /* TODO use something random to pick the initial sequence nr.
2649 0x6d31 produces the ASCII character sequence 'm1', currently used in
2650 gtphub_nc_test.sh. */
2651 peer->seq_pool.last_nr = 0x6d31 - 1;
2652
2653 llist_add(&peer->entry, &bind->peers);
2654 return peer;
2655}
2656
2657static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2658 const struct gsn_addr *addr)
2659{
2660 struct gtphub_peer_addr *a;
2661 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2662 OSMO_ASSERT(a);
2663 a->peer = peer;
2664 gsn_addr_copy(&a->addr, addr);
2665 INIT_LLIST_HEAD(&a->ports);
2666 llist_add(&a->entry, &peer->addresses);
2667
2668 return a;
2669}
2670
2671static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2672 struct gtphub_bind *bind,
2673 const struct gsn_addr *addr)
2674{
2675 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2676 if (a)
2677 return a;
2678
2679 /* If we haven't found an address, that means we need to create an
2680 * entirely new peer for the new address. More addresses may be added
2681 * to this peer later, but not via this function. */
2682 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002683
2684 a = gtphub_peer_add_addr(peer, addr);
2685
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002686 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002687 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002688 gsn_addr_to_str(&a->addr));
2689
2690 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002691}
2692
2693static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2694 uint16_t port)
2695{
2696 struct gtphub_peer_port *pp;
2697
2698 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2699 OSMO_ASSERT(pp);
2700 pp->peer_addr = a;
2701 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002702 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002703
2704 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2705 talloc_free(pp);
2706 return NULL;
2707 }
2708
Neels Hofmeyre38fb662015-12-06 16:44:14 +01002709 pp->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
2710 &gtphub_ctrg_io_desc, 0);
2711
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002712 llist_add(&pp->entry, &a->ports);
2713
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002714 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002715 gsn_addr_to_str(&a->addr),
2716 (int)port);
2717
2718 return pp;
2719}
2720
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002721struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2722 struct gtphub_bind *bind,
2723 const struct gsn_addr *addr,
2724 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002725{
2726 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2727
2728 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2729 if (pp)
2730 return pp;
2731
2732 return gtphub_addr_add_port(a, port);
2733}
2734
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002735/* Find a GGSN peer with a matching address. If the address is known but the
2736 * port not, create a new port for that peer address. */
2737struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2738 const struct osmo_sockaddr *addr)
2739{
2740 struct gtphub_peer_addr *pa;
2741 struct gtphub_peer_port *pp;
2742
2743 struct gsn_addr gsna;
2744 uint16_t port;
2745 gsn_addr_from_sockaddr(&gsna, &port, addr);
2746
2747 pa = gtphub_addr_find(bind, &gsna);
2748 if (!pa)
2749 return NULL;
2750
2751 pp = gtphub_addr_find_port(pa, port);
2752
2753 if (!pp)
2754 pp = gtphub_addr_add_port(pa, port);
2755
2756 return pp;
2757}
2758
2759
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002760/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2761 * resolution should be possible but failed, and 1 if resolution was
2762 * successful. *pp will be set to NULL if <1 is returned. */
2763static int gtphub_resolve_ggsn(struct gtphub *hub,
2764 struct gtp_packet_desc *p,
2765 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002766{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002767 *pp = NULL;
2768
2769 /* TODO determine from message type whether IEs should be present? */
2770
2771 int rc;
2772 const char *imsi_str;
2773 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2774 if (rc < 1)
2775 return rc;
2776 OSMO_ASSERT(imsi_str);
2777
2778 const char *apn_str;
2779 rc = get_ie_apn_str(p->ie, &apn_str);
2780 if (rc < 1)
2781 return rc;
2782 OSMO_ASSERT(apn_str);
2783
2784 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2785 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002786}
2787
2788
2789/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002790/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002791/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2792 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002793static int _osmo_getaddrinfo(struct addrinfo **result,
2794 uint16_t family, uint16_t type, uint8_t proto,
2795 const char *host, uint16_t port)
2796{
2797 struct addrinfo hints;
2798 char portbuf[16];
2799
2800 sprintf(portbuf, "%u", port);
2801 memset(&hints, '\0', sizeof(struct addrinfo));
2802 hints.ai_family = family;
2803 if (type == SOCK_RAW) {
2804 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2805 * SOCK_RAW and IPPROTO_GRE is used.
2806 */
2807 hints.ai_socktype = SOCK_DGRAM;
2808 hints.ai_protocol = IPPROTO_UDP;
2809 } else {
2810 hints.ai_socktype = type;
2811 hints.ai_protocol = proto;
2812 }
2813
2814 return getaddrinfo(host, portbuf, &hints, result);
2815}
2816
2817/* TODO move to osmocom/core/socket.c ? */
2818int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2819 uint16_t family, uint16_t type, uint8_t proto,
2820 const char *host, uint16_t port)
2821{
2822 struct addrinfo *res;
2823 int rc;
2824 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2825
2826 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002827 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002828 return -EINVAL;
2829 }
2830
2831 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2832 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2833 addr->l = res->ai_addrlen;
2834 freeaddrinfo(res);
2835
2836 return 0;
2837}
2838
2839int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2840 char *port_str, size_t port_str_len,
2841 const struct osmo_sockaddr *addr,
2842 int flags)
2843{
2844 int rc;
2845
2846 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2847 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2848 return -1;
2849 }
2850
2851 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002852 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2853 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002854 return -1;
2855 }
2856
2857 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2858 addr_str, addr_str_len,
2859 port_str, port_str_len,
2860 flags);
2861
2862 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002863 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2864 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2865 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002866
2867 return rc;
2868}
2869
2870const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2871 char *buf, size_t buf_len)
2872{
2873 const int portbuf_len = 6;
2874 OSMO_ASSERT(buf_len > portbuf_len);
2875 char *portbuf = buf + buf_len - portbuf_len;
2876 buf_len -= portbuf_len;
2877 if (osmo_sockaddr_to_strs(buf, buf_len,
2878 portbuf, portbuf_len,
2879 addr,
2880 NI_NUMERICHOST | NI_NUMERICSERV))
2881 return NULL;
2882
2883 char *pos = buf + strnlen(buf, buf_len-1);
2884 size_t len = buf_len - (pos - buf);
2885
2886 snprintf(pos, len, " port %s", portbuf);
2887 buf[buf_len-1] = '\0';
2888
2889 return buf;
2890}
2891
2892const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2893{
2894 static char buf[256];
2895 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2896 if (! result)
2897 return "(invalid)";
2898 return result;
2899}
2900
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002901int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2902 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002903{
2904 if (a == b)
2905 return 0;
2906 if (!a)
2907 return -1;
2908 if (!b)
2909 return 1;
2910 if (a->l != b->l) {
2911 /* Lengths are not the same, but determine the order. Will
2912 * anyone ever sort a list by osmo_sockaddr though...? */
2913 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2914 if (cmp == 0) {
2915 if (a->l < b->l)
2916 return -1;
2917 else
2918 return 1;
2919 }
2920 return cmp;
2921 }
2922 return memcmp(&a->a, &b->a, a->l);
2923}
2924
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002925void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2926 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002927{
2928 OSMO_ASSERT(src->l <= sizeof(dst->a));
2929 memcpy(&dst->a, &src->a, src->l);
2930 dst->l = src->l;
2931}