blob: dcfa823fa67b0f3a2d1ef9a234d401ff1e46319a [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 Hofmeyr29d926b2015-11-24 13:27:13 +010094 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020095 union gtpie_member *ie[GTPIE_SIZE];
96};
97
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010098struct pending_delete {
99 struct llist_head entry;
100 struct expiring_item expiry_entry;
101
102 struct gtphub_tunnel *tun;
103 uint8_t teardown_ind;
104 uint8_t nsapi;
105};
106
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100107
108/* counters */
109
110enum gtphub_counters_io {
111 GTPH_CTR_PKTS_IN = 0,
112 GTPH_CTR_PKTS_OUT,
113 GTPH_CTR_BYTES_IN,
114 GTPH_CTR_BYTES_OUT
115};
116
117static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
118 { "packets.in", "Packets ( In)" },
119 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100120 { "bytes.in", "Bytes ( In)" },
121 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100122};
123
124static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
125 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100126 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100127 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
128 .ctr_desc = gtphub_counters_io_desc,
129 .class_id = OSMO_STATS_CLASS_GLOBAL,
130};
131
132
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200133void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
134{
135 memcpy(gsna, src, sizeof(struct gsn_addr));
136}
137
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200138int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
139 const struct osmo_sockaddr *sa)
140{
141 char addr_str[256];
142 char port_str[6];
143
144 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
145 port_str, sizeof(port_str),
146 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
147 != 0) {
148 return -1;
149 }
150
151 if (port)
152 *port = atoi(port_str);
153
154 return gsn_addr_from_str(gsna, addr_str);
155}
156
157int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
158{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100159 if ((!gsna) || (!numeric_addr_str))
160 return -1;
161
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200162 int af = AF_INET;
163 gsna->len = 4;
164 const char *pos = numeric_addr_str;
165 for (; *pos; pos++) {
166 if (*pos == ':') {
167 af = AF_INET6;
168 gsna->len = 16;
169 break;
170 }
171 }
172
173 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
174 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100175 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
176 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200177 return -1;
178 }
179 return 0;
180}
181
182const char *gsn_addr_to_str(const struct gsn_addr *gsna)
183{
184 static char buf[INET6_ADDRSTRLEN + 1];
185 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
186}
187
188const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
189 char *strbuf,
190 int strbuf_len)
191{
192 int af;
193 switch (gsna->len) {
194 case 4:
195 af = AF_INET;
196 break;
197 case 16:
198 af = AF_INET6;
199 break;
200 default:
201 return NULL;
202 }
203
204 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
205 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100206 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
207 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100208 strerror(errno),
209 (int)gsna->len,
210 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200211 }
212 return r;
213}
214
215int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
216{
217 if (a == b)
218 return 1;
219 if ((!a) || (!b))
220 return 0;
221 if (a->len != b->len)
222 return 0;
223 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
224}
225
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100226static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
227 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200228{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100229 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200230 return -1;
231
232 unsigned int len;
233 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
234 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
235 &len, gsna->buf, sizeof(gsna->buf))
236 != 0)
237 return -1;
238 gsna->len = len;
239 return 0;
240}
241
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100242static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
243 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200244{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100245 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200246 return -1;
247
248 int ie_idx;
249 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
250
251 if (ie_idx < 0)
252 return -1;
253
254 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
255 int ie_l = ntoh16(ie->l);
256 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100257 LOG(LOGL_ERROR, "Not implemented:"
258 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
260 return -1;
261 }
262
263 memcpy(ie->v, gsna->buf, (int)ie_l);
264 return 0;
265}
266
267/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
268 */
269void validate_gtp0_header(struct gtp_packet_desc *p)
270{
271 const struct gtp0_header *pheader = &(p->data->gtp0.h);
272 p->rc = GTP_RC_UNKNOWN;
273 p->header_len = 0;
274
275 OSMO_ASSERT(p->data_len >= 1);
276 OSMO_ASSERT(p->version == 0);
277
278 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100279 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200280 p->rc = GTP_RC_TOOSHORT;
281 return;
282 }
283
284 p->type = ntoh8(pheader->type);
285 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100286 p->header_tei_rx = 0; /* TODO */
287 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200288
289 if (p->data_len == GTP0_HEADER_SIZE) {
290 p->rc = GTP_RC_TINY;
291 p->header_len = GTP0_HEADER_SIZE;
292 return;
293 }
294
295 /* Check packet length field versus length of packet */
296 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100297 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
298 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100299 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
300 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200301 p->rc = GTP_RC_TOOSHORT;
302 return;
303 }
304
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100305 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200306 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100307 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200308}
309
310/* Validate GTP version 1 data, and update p->rc with the result, as well as
311 * p->header_len in case of a valid header. */
312void validate_gtp1_header(struct gtp_packet_desc *p)
313{
314 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
315 p->rc = GTP_RC_UNKNOWN;
316 p->header_len = 0;
317
318 OSMO_ASSERT(p->data_len >= 1);
319 OSMO_ASSERT(p->version == 1);
320
321 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
322 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100323 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200324 p->rc = GTP_RC_TOOSHORT;
325 return;
326 }
327
328 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100329 p->header_tei_rx = ntoh32(pheader->tei);
330 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200331 p->seq = ntoh16(pheader->seq);
332
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100333 LOG(LOGL_DEBUG, "GTPv1\n"
334 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
335 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
336 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
337 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
338 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
339 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
340 p->type, p->type,
341 ntoh16(pheader->length), ntoh16(pheader->length),
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100342 p->header_tei_rx, p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100343 p->seq, p->seq,
344 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345 pheader->next, pheader->next);
346
347 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
348 p->rc = GTP_RC_TINY;
349 p->header_len = GTP1_HEADER_SIZE_SHORT;
350 return;
351 }
352
353 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100354 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
355 if (p->data_len != announced_len) {
356 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
357 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100358 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
359 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200360 p->rc = GTP_RC_TOOSHORT;
361 return;
362 }
363
Neels Hofmeyre921e322015-11-11 00:45:50 +0100364 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200365 p->header_len = GTP1_HEADER_SIZE_LONG;
366}
367
368/* Examine whether p->data of size p->data_len has a valid GTP header. Set
369 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
370 * gtp_rc). p->data must point at a buffer with p->data_len set. */
371void validate_gtp_header(struct gtp_packet_desc *p)
372{
373 p->rc = GTP_RC_UNKNOWN;
374
375 /* Need at least 1 byte in order to check version */
376 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100377 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
378 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200379 p->rc = GTP_RC_TOOSHORT;
380 return;
381 }
382
383 p->version = p->data->flags >> 5;
384
385 switch (p->version) {
386 case 0:
387 validate_gtp0_header(p);
388 break;
389 case 1:
390 validate_gtp1_header(p);
391 break;
392 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100393 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200394 p->rc = GTP_RC_UNSUPPORTED_VERSION;
395 break;
396 }
397}
398
399
400/* Return the value of the i'th IMSI IEI by copying to *imsi.
401 * The first IEI is reached by passing i = 0.
402 * imsi must point at allocated space of (at least) 8 bytes.
403 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100404static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200405{
406 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
407}
408
409/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100410static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200411{
412 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
413}
414
415static char imsi_digit_to_char(uint8_t nibble)
416{
417 nibble &= 0x0f;
418 if (nibble > 9)
419 return (nibble == 0x0f) ? '\0' : '?';
420 return '0' + nibble;
421}
422
423/* Return a human readable IMSI string, in a static buffer.
424 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100425static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200426{
427 static char str[17];
428 int i;
429
430 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100431 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100432 c = imsi_digit_to_char(imsi[i]);
433 if (c == '?')
434 return -1;
435 str[2*i] = c;
436
437 c = imsi_digit_to_char(imsi[i] >> 4);
438 if (c == '?')
439 return -1;
440 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200441 }
442 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100443 *imsi_str = str;
444 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200445}
446
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100447/* Return 0 if not present, 1 if present and decoded successfully, -1 if
448 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100449static int get_ie_imsi_str(union gtpie_member *ie[], int i,
450 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100451{
452 uint8_t imsi_buf[8];
453 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100454 return 0;
455 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100456}
457
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100458/* Return 0 if not present, 1 if present and decoded successfully, -1 if
459 * present but cannot be decoded. */
460static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100461{
462 static char apn_buf[GSM_APN_LENGTH];
463 unsigned int len;
464 if (gtpie_gettlv(ie, GTPIE_APN, 0,
465 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100467
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100468 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100469 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
470 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100471 return -1;
472 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100473
474 if (len > (sizeof(apn_buf) - 1))
475 len = sizeof(apn_buf) - 1;
476 apn_buf[len] = '\0';
477
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100478 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
479 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100480 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
481 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100482 return -1;
483 }
484 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100485}
486
487
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200488/* Validate header, and index information elements. Write decoded packet
489 * information to *res. res->data will point at the given data buffer. On
490 * error, p->rc is set <= 0 (see enum gtp_rc). */
491static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100492 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200493 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100494 struct gtp_packet_desc *res,
495 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496{
497 ZERO_STRUCT(res);
498 res->data = (union gtp_packet*)data;
499 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100500 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200501 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100502 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200503
504 validate_gtp_header(res);
505
506 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100507 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508 return;
509 }
510
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100511 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200512
Neels Hofmeyre921e322015-11-11 00:45:50 +0100513 if (from_plane_idx == GTPH_PLANE_USER) {
514 res->rc = GTP_RC_PDU_U;
515 return;
516 }
517
518 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100519 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200520 return;
521 }
522
523 if (gtpie_decaps(res->ie, res->version,
524 (void*)(data + res->header_len),
525 res->data_len - res->header_len) != 0) {
526 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100527 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
528 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200529 return;
530 }
531
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100532#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100533 /* TODO if (<loglevel is debug>) { ...
534 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200535 int i;
536
537 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100538 const char *imsi;
539 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100541 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200542 }
543
544 for (i = 0; i < 10; i++) {
545 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100546 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200547 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100548 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200549 }
550
551 for (i = 0; i < 2; i++) {
552 struct gsn_addr addr;
553 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100554 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200555 }
556
557 for (i = 0; i < 10; i++) {
558 uint32_t tei;
559 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
560 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100561 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200562 tei, tei);
563 }
564
565 for (i = 0; i < 10; i++) {
566 uint32_t tei;
567 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
568 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100569 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200570 tei, tei);
571 }
572#endif
573}
574
575
576/* expiry */
577
578void expiry_init(struct expiry *exq, int expiry_in_seconds)
579{
580 ZERO_STRUCT(exq);
581 exq->expiry_in_seconds = expiry_in_seconds;
582 INIT_LLIST_HEAD(&exq->items);
583}
584
585void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
586{
587 item->expiry = now + exq->expiry_in_seconds;
588
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100589 OSMO_ASSERT(llist_empty(&exq->items)
590 || (item->expiry
591 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
592
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200593 /* Add/move to the tail to always sort by expiry, ascending. */
594 llist_del(&item->entry);
595 llist_add_tail(&item->entry, &exq->items);
596}
597
598int expiry_tick(struct expiry *exq, time_t now)
599{
600 int expired = 0;
601 struct expiring_item *m, *n;
602 llist_for_each_entry_safe(m, n, &exq->items, entry) {
603 if (m->expiry <= now) {
604 expiring_item_del(m);
605 expired ++;
606 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200607 /* The items are added sorted by expiry. So when we hit
608 * an unexpired entry, only more unexpired ones will
609 * follow. */
610 break;
611 }
612 }
613 return expired;
614}
615
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100616void expiry_clear(struct expiry *exq)
617{
618 struct expiring_item *m, *n;
619 llist_for_each_entry_safe(m, n, &exq->items, entry) {
620 expiring_item_del(m);
621 }
622}
623
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200624void expiring_item_init(struct expiring_item *item)
625{
626 ZERO_STRUCT(item);
627 INIT_LLIST_HEAD(&item->entry);
628}
629
630void expiring_item_del(struct expiring_item *item)
631{
632 OSMO_ASSERT(item);
633 llist_del(&item->entry);
634 INIT_LLIST_HEAD(&item->entry);
635 if (item->del_cb) {
636 /* avoid loops */
637 del_cb_t del_cb = item->del_cb;
638 item->del_cb = 0;
639 (del_cb)(item);
640 }
641}
642
643
644/* nr_map, nr_pool */
645
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100646void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200647{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100648 *pool = (struct nr_pool){
649 .nr_min = nr_min,
650 .nr_max = nr_max,
651 .last_nr = nr_max
652 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200653}
654
655nr_t nr_pool_next(struct nr_pool *pool)
656{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100657 if (pool->last_nr >= pool->nr_max)
658 pool->last_nr = pool->nr_min;
659 else
660 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200661
662 return pool->last_nr;
663}
664
665void nr_map_init(struct nr_map *map, struct nr_pool *pool,
666 struct expiry *exq)
667{
668 ZERO_STRUCT(map);
669 map->pool = pool;
670 map->add_items_to_expiry = exq;
671 INIT_LLIST_HEAD(&map->mappings);
672}
673
674void nr_mapping_init(struct nr_mapping *m)
675{
676 ZERO_STRUCT(m);
677 INIT_LLIST_HEAD(&m->entry);
678 expiring_item_init(&m->expiry_entry);
679}
680
681void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
682{
683 /* Generate a mapped number */
684 mapping->repl = nr_pool_next(map->pool);
685
686 /* Add to the tail to always yield a list sorted by expiry, in
687 * ascending order. */
688 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100689 nr_map_refresh(map, mapping, now);
690}
691
692void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
693{
694 if (!map->add_items_to_expiry)
695 return;
696 expiry_add(map->add_items_to_expiry,
697 &mapping->expiry_entry,
698 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200699}
700
701void nr_map_clear(struct nr_map *map)
702{
703 struct nr_mapping *m;
704 struct nr_mapping *n;
705 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
706 nr_mapping_del(m);
707 }
708}
709
710int nr_map_empty(const struct nr_map *map)
711{
712 return llist_empty(&map->mappings);
713}
714
715struct nr_mapping *nr_map_get(const struct nr_map *map,
716 void *origin, nr_t nr_orig)
717{
718 struct nr_mapping *mapping;
719 llist_for_each_entry(mapping, &map->mappings, entry) {
720 if ((mapping->origin == origin)
721 && (mapping->orig == nr_orig))
722 return mapping;
723 }
724 /* Not found. */
725 return NULL;
726}
727
728struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
729{
730 struct nr_mapping *mapping;
731 llist_for_each_entry(mapping, &map->mappings, entry) {
732 if (mapping->repl == nr_repl) {
733 return mapping;
734 }
735 }
736 /* Not found. */
737 return NULL;
738}
739
740void nr_mapping_del(struct nr_mapping *mapping)
741{
742 OSMO_ASSERT(mapping);
743 llist_del(&mapping->entry);
744 INIT_LLIST_HEAD(&mapping->entry);
745 expiring_item_del(&mapping->expiry_entry);
746}
747
748
749/* gtphub */
750
751const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
752 "CTRL",
753 "USER",
754};
755
756const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
757 2123,
758 2152,
759};
760
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100761const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
762 "SGSN",
763 "GGSN",
764};
765
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200766time_t gtphub_now(void)
767{
768 struct timespec now_tp;
769 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
770 return now_tp.tv_sec;
771}
772
773/* Remove a gtphub_peer from its list and free it. */
774static void gtphub_peer_del(struct gtphub_peer *peer)
775{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100776 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200777 nr_map_clear(&peer->seq_map);
778 llist_del(&peer->entry);
779 talloc_free(peer);
780}
781
782static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
783{
784 OSMO_ASSERT(llist_empty(&pa->ports));
785 llist_del(&pa->entry);
786 talloc_free(pa);
787}
788
789static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
790{
791 OSMO_ASSERT(pp->ref_count == 0);
792 llist_del(&pp->entry);
793 talloc_free(pp);
794}
795
796/* From the information in the gtp_packet_desc, return the address of a GGSN.
797 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100798static int gtphub_resolve_ggsn(struct gtphub *hub,
799 struct gtp_packet_desc *p,
800 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200801
802/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100803struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
804 const char *imsi_str,
805 const char *apn_ni_str);
806int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200807
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200808static void gtphub_zero(struct gtphub *hub)
809{
810 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100811 INIT_LLIST_HEAD(&hub->ggsn_lookups);
812 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200813}
814
815static int gtphub_sock_init(struct osmo_fd *ofd,
816 const struct gtphub_cfg_addr *addr,
817 osmo_fd_cb_t cb,
818 void *data,
819 int ofd_id)
820{
821 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100822 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200823 return -1;
824 }
825 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100826 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200827 return -1;
828 }
829
830 ofd->when = BSC_FD_READ;
831 ofd->cb = cb;
832 ofd->data = data;
833 ofd->priv_nr = ofd_id;
834
835 int rc;
836 rc = osmo_sock_init_ofd(ofd,
837 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
838 addr->addr_str, addr->port,
839 OSMO_SOCK_F_BIND);
840 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100841 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
842 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200843 return -1;
844 }
845
846 return 0;
847}
848
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100849static void gtphub_sock_close(struct osmo_fd *ofd)
850{
851 close(ofd->fd);
852 osmo_fd_unregister(ofd);
853 ofd->cb = NULL;
854}
855
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200856static void gtphub_bind_init(struct gtphub_bind *b)
857{
858 ZERO_STRUCT(b);
859
860 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100861
862 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
863 &gtphub_ctrg_io_desc, 0);
864 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200865}
866
867static int gtphub_bind_start(struct gtphub_bind *b,
868 const struct gtphub_cfg_bind *cfg,
869 osmo_fd_cb_t cb, void *cb_data,
870 unsigned int ofd_id)
871{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100872 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
873 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
874 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
875 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200876 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100877 }
878 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
879 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
880 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200881 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100882 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100883 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200884 return 0;
885}
886
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100887static void gtphub_bind_free(struct gtphub_bind *b)
888{
889 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100890 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100891}
892
893static void gtphub_bind_stop(struct gtphub_bind *b) {
894 gtphub_sock_close(&b->ofd);
895 gtphub_bind_free(b);
896}
897
Neels Hofmeyr40348972015-11-17 12:22:28 +0100898/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200899 * Return the number of bytes read, zero on error. */
900static int gtphub_read(const struct osmo_fd *from,
901 struct osmo_sockaddr *from_addr,
902 uint8_t *buf, size_t buf_len)
903{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100904 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200905
Neels Hofmeyr40348972015-11-17 12:22:28 +0100906 /* recvfrom requires the available length set in *from_addr_len. */
907 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200908 errno = 0;
909 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100910 (struct sockaddr*)&from_addr->a,
911 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200912 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
913 * is not truncated. Then maybe reduce buf's size. */
914
915 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100916 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
917 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200918 return 0;
919 }
920
Neels Hofmeyr40348972015-11-17 12:22:28 +0100921 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
922 (int)received, osmo_sockaddr_to_str(from_addr),
923 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200924
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200925 return received;
926}
927
928inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
929{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100930 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200931 OSMO_ASSERT(pp->ref_count < UINT_MAX);
932 pp->ref_count++;
933}
934
935inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
936{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100937 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200938 OSMO_ASSERT(pp->ref_count > 0);
939 pp->ref_count--;
940}
941
942inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
943{
944 OSMO_ASSERT(p->version == 1);
945 p->data->gtp1l.h.seq = hton16(seq);
946 p->seq = seq;
947}
948
949inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
950{
951 OSMO_ASSERT(p->version == 1);
952 p->data->gtp1l.h.tei = hton32(tei);
953 p->header_tei = tei;
954}
955
956static void gtphub_mapping_del_cb(struct expiring_item *expi);
957
958static struct nr_mapping *gtphub_mapping_new()
959{
960 struct nr_mapping *nrm;
961 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
962 OSMO_ASSERT(nrm);
963
964 nr_mapping_init(nrm);
965 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
966 return nrm;
967}
968
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100969
970#define APPEND(args...) \
971 l = snprintf(pos, left, args); \
972 pos += l; \
973 left -= l
974
975static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
976 int side_idx)
977{
978 static char buf[256];
979 char *pos = buf;
980 int left = sizeof(buf);
981 int l;
982
983 struct gtphub_tunnel_endpoint *c, *u;
984 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
985 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
986
987 /* print both only if they differ. */
988 if (!c->peer) {
989 APPEND("(uninitialized)");
990 } else {
991 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
992 }
993
994 if (!u->peer) {
995 if (c->peer) {
996 APPEND(" / (uninitialized)");
997 }
998 } else if ((!c->peer)
999 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1000 &c->peer->peer_addr->addr))) {
1001 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
1002 }
1003
1004 APPEND(" (TEI C %x=%x / U %x=%x)",
1005 c->tei_orig, c->tei_repl,
1006 u->tei_orig, u->tei_repl);
1007 return buf;
1008}
1009
1010const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1011{
1012 static char buf[512];
1013 char *pos = buf;
1014 int left = sizeof(buf);
1015 int l;
1016
1017 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1018 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1019
1020 return buf;
1021}
1022
1023#undef APPEND
1024
1025void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1026 struct gtphub_peer_port *pp)
1027{
1028 if (te->peer)
1029 gtphub_port_ref_count_dec(te->peer);
1030 te->peer = pp;
1031 if (te->peer)
1032 gtphub_port_ref_count_inc(te->peer);
1033}
1034
1035int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1036{
1037 if (!tun)
1038 return 0;
1039 int side_idx;
1040 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001041 for_each_side_and_plane(side_idx, plane_idx) {
1042 struct gtphub_tunnel_endpoint *te =
1043 &tun->endpoint[side_idx][plane_idx];
1044 if (!(te->peer && te->tei_orig && te->tei_repl))
1045 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001046 }
1047 return 1;
1048}
1049
1050static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1051{
1052 struct gtphub_tunnel *tun = container_of(expi,
1053 struct gtphub_tunnel,
1054 expiry_entry);
1055 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1056
1057 llist_del(&tun->entry);
1058 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1059
1060 expi->del_cb = 0; /* avoid recursion loops */
1061 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1062
1063 int side_idx;
1064 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001065 for_each_side_and_plane(side_idx, plane_idx) {
1066 /* clear ref count */
1067 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1068 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001069 }
1070
1071 talloc_free(tun);
1072}
1073
1074static struct gtphub_tunnel *gtphub_tunnel_new()
1075{
1076 struct gtphub_tunnel *tun;
1077 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1078 OSMO_ASSERT(tun);
1079
1080 INIT_LLIST_HEAD(&tun->entry);
1081 expiring_item_init(&tun->expiry_entry);
1082
1083 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1084 return tun;
1085}
1086
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001087static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1088 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001089{
1090 if (llist_empty(&peer->addresses))
1091 return "(addressless)";
1092
1093 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1094 struct gtphub_peer_addr,
1095 entry);
1096 return gsn_addr_to_strb(&a->addr, buf, buflen);
1097}
1098
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001099static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1100 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001101{
1102 if (!port)
1103 return "(null port)";
1104
1105 snprintf(buf, buflen, "%s port %d",
1106 gsn_addr_to_str(&port->peer_addr->addr),
1107 (int)port->port);
1108 return buf;
1109}
1110
1111const char *gtphub_peer_str(struct gtphub_peer *peer)
1112{
1113 static char buf[256];
1114 return gtphub_peer_strb(peer, buf, sizeof(buf));
1115}
1116
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001117const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001118{
1119 static char buf[256];
1120 return gtphub_port_strb(port, buf, sizeof(buf));
1121}
1122
1123static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1124{
1125 static char buf[256];
1126 return gtphub_port_strb(port, buf, sizeof(buf));
1127}
1128
1129static void gtphub_mapping_del_cb(struct expiring_item *expi)
1130{
1131 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001132 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001133
1134 struct nr_mapping *nrm = container_of(expi,
1135 struct nr_mapping,
1136 expiry_entry);
1137 llist_del(&nrm->entry);
1138 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1139
1140 /* Just for log */
1141 struct gtphub_peer_port *from = nrm->origin;
1142 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001143 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001144 (int)nrm->expiry_entry.expiry,
1145 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001146 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001147
1148 gtphub_port_ref_count_dec(from);
1149
1150 talloc_free(nrm);
1151}
1152
1153static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1154 struct gtphub_peer_port *from,
1155 nr_t orig_nr,
1156 time_t now)
1157{
1158 struct nr_mapping *nrm;
1159
1160 nrm = nr_map_get(map, from, orig_nr);
1161
1162 if (!nrm) {
1163 nrm = gtphub_mapping_new();
1164 nrm->orig = orig_nr;
1165 nrm->origin = from;
1166 nr_map_add(map, nrm, now);
1167 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001168 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001169 gtphub_port_str(from),
1170 (int)(nrm->orig), (int)(nrm->repl));
1171 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001172 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001173 }
1174
1175 OSMO_ASSERT(nrm);
1176 return nrm;
1177}
1178
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001179static void gtphub_map_seq(struct gtp_packet_desc *p,
1180 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001181 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001182{
1183 /* Store a mapping in to_peer's map, so when we later receive a GTP
1184 * packet back from to_peer, the seq nr can be unmapped back to its
1185 * origin (from_peer here). */
1186 struct nr_mapping *nrm;
1187 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001188 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001189
1190 /* Change the GTP packet to yield the new, mapped seq nr */
1191 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001192}
1193
1194static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1195 struct gtphub_peer_port *responding_port)
1196{
1197 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001198 struct nr_mapping *nrm =
1199 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1200 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001201 if (!nrm)
1202 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001203 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1204 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001205 set_seq(p, nrm->orig);
1206 return nrm->origin;
1207}
1208
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001209static struct gtphub_tunnel *gtphub_tun_find(struct gtphub *hub,
1210 int side_idx,
1211 int plane_idx,
1212 struct gtphub_peer_port *from,
1213 uint32_t tei_orig,
1214 uint32_t tei_repl)
1215{
1216 OSMO_ASSERT(from);
1217
1218 struct gtphub_tunnel *tun;
1219 /* TODO: optimize: don't iterate *all* tunnels. */
1220 llist_for_each_entry(tun, &hub->tunnels, entry) {
1221 struct gtphub_tunnel_endpoint *te =
1222 &tun->endpoint[side_idx][plane_idx];
1223 if (((!tei_orig) || (te->tei_orig == tei_orig))
1224 && ((!tei_repl) || (te->tei_repl == tei_repl))
1225 && gsn_addr_same(&te->peer->peer_addr->addr, &from->peer_addr->addr))
1226 return tun;
1227 }
1228 return NULL;
1229}
1230
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001231static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1232 struct gtphub_tunnel_endpoint *iterated_te,
1233 uint32_t *tei_min,
1234 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001235{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001236 if (new_te->tei_repl != iterated_te->tei_repl)
1237 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001238
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001239 /* new_te->tei_repl is already taken. Try to find one out of the known
1240 * range. */
1241
1242 if ((*tei_max) < 0xffffffff) {
1243 (*tei_max)++;
1244 new_te->tei_repl = *tei_max;
1245 return 1;
1246 } else if ((*tei_min) > 1) {
1247 (*tei_min)--;
1248 new_te->tei_repl = *tei_min;
1249 return 1;
1250 }
1251
1252 /* None seems to be available. */
1253 return 0;
1254}
1255
1256static int gtphub_check_reused_teis(struct gtphub *hub,
1257 struct gtphub_tunnel *new_tun)
1258{
1259 uint32_t tei_min = 0xffffffff;
1260 uint32_t tei_max = 0;
1261 int side_idx;
1262 int plane_idx;
1263 int side_idx2;
1264 int plane_idx2;
1265 struct gtphub_tunnel_endpoint *te;
1266 struct gtphub_tunnel_endpoint *te2;
1267
1268 struct gtphub_tunnel *tun, *ntun;
1269
1270 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1271 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001272 continue;
1273
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001274 /* Check whether the GSN sent a TEI that it is reusing from a
1275 * previous tunnel. */
1276 for_each_side_and_plane(side_idx, plane_idx) {
1277 te = &tun->endpoint[side_idx][plane_idx];
1278 te2 = &new_tun->endpoint[side_idx][plane_idx];
1279 if ((te->tei_orig == te2->tei_orig)
1280 && gsn_addr_same(&te->peer->peer_addr->addr,
1281 &te2->peer->peer_addr->addr)) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001282
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001283 /* The peer is reusing a TEI that I believe to
1284 * be part of another tunnel. The other tunnel
1285 * must be stale, then. */
1286 LOG(LOGL_NOTICE,
1287 "Expiring tunnel due to reused TEI:"
1288 " peer %s sent %s TEI %x,"
1289 " previously used by tunnel %s...\n",
1290 gtphub_port_str(te->peer),
1291 gtphub_plane_idx_names[plane_idx],
1292 te->tei_orig,
1293 gtphub_tunnel_str(tun));
1294 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1295 gtphub_tunnel_str(new_tun));
1296 expiring_item_del(&tun->expiry_entry);
1297 /* continue to find more matches. There shouldn't be
1298 * any, but let's make sure. */
1299 }
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001300 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001301
1302 /* Check whether the mapped TEIs assigned to the endpoints are
1303 * used anywhere else. */
1304 for_each_side_and_plane(side_idx, plane_idx) {
1305 te = &tun->endpoint[side_idx][plane_idx];
1306 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1307 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1308
1309 for_each_side_and_plane(side_idx2, plane_idx2) {
1310 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1311 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1312 LOG(LOGL_ERROR,
1313 "No mapped TEI is readily available."
1314 " Searching for holes between occupied"
1315 " TEIs not implemented.");
1316 return 0;
1317 }
1318 }
1319 }
1320
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001321 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001322
1323 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001324}
1325
1326static void gtphub_tunnel_refresh(struct gtphub *hub,
1327 struct gtphub_tunnel *tun,
1328 time_t now)
1329{
1330 expiry_add(&hub->expire_slowly,
1331 &tun->expiry_entry,
1332 now);
1333}
1334
1335static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1336 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001337 struct gtphub_peer_port *from,
1338 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001339{
1340 OSMO_ASSERT(from);
1341 int other_side = other_side_idx(p->side_idx);
1342
1343 struct gtphub_tunnel *tun;
1344 llist_for_each_entry(tun, &hub->tunnels, entry) {
1345 struct gtphub_tunnel_endpoint *te_from =
1346 &tun->endpoint[p->side_idx][p->plane_idx];
1347 struct gtphub_tunnel_endpoint *te_to =
1348 &tun->endpoint[other_side][p->plane_idx];
1349 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001350 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001351 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1352 &from->peer_addr->addr)) {
1353 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001354 if (unmapped_from_tun)
1355 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001356 return te_to;
1357 }
1358 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001359
1360 if (unmapped_from_tun)
1361 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001362 return NULL;
1363}
1364
1365
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001366static void gtphub_check_restart_counter(struct gtphub *hub,
1367 struct gtp_packet_desc *p,
1368 struct gtphub_peer_port *from)
1369{
1370 /* TODO */
1371 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1372 * that doesn't match the peer's previously sent restart counter, clear
1373 * that peer and cancel PDP contexts. */
1374}
1375
1376static void gtphub_map_restart_counter(struct gtphub *hub,
1377 struct gtp_packet_desc *p,
1378 struct gtphub_peer_port *from,
1379 struct gtphub_peer_port *to)
1380{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001381 /* Always send gtphub's own restart counter */
1382 if (p->rc != GTP_RC_PDU_C)
1383 return;
1384
1385 int ie_idx;
1386 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1387 if (ie_idx < 0)
1388 return;
1389
1390 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001391}
1392
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001394 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001395 struct gtphub *hub,
1396 struct gtp_packet_desc *p,
1397 struct gtphub_peer_port *from_port)
1398{
1399 OSMO_ASSERT(p->version == 1);
1400 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001401 if (unmapped_from_tun)
1402 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001403
1404 /* If the header's TEI is zero, no PDP context has been established
1405 * yet. If nonzero, a mapping should actually already exist for this
1406 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001407 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408 return 0;
1409
1410 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001411 * mapped in a tunnel struct. */
1412 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001413 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001414 if (!to) {
1415 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1416 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001417 return -1;
1418 }
1419
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001420 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001421 set_tei(p, unmapped_tei);
1422
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001423 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %" PRIx32 " -> %" PRIx32 " (to %s)\n",
1424 gtphub_port_str(from_port), p->header_tei_rx, unmapped_tei,
1425 gtphub_port_str2(to->peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001426
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001427 *to_port_p = to->peer;
1428
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001429 return 0;
1430}
1431
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001432static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1433 struct gtp_packet_desc *p,
1434 struct gtphub_peer_port *from_ctrl,
1435 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001436{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001437 int plane_idx;
1438
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001439 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001440 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1441 plane_nrs_match_GSN_addr_IE_indices);
1442
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001443 struct gtphub_tunnel *tun = NULL;
1444
1445 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001446 if (p->side_idx != GTPH_SIDE_SGSN) {
1447 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1448 " Request from the GGSN side");
1449 return -1;
1450 }
1451
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001452 /* A new tunnel. */
1453 tun = gtphub_tunnel_new();
1454 llist_add(&tun->entry, &hub->tunnels);
1455 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001456 /* The endpoint peers on this side (SGSN) will be set from IEs
1457 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001458 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001459 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001460 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001461 if (p->side_idx != GTPH_SIDE_GGSN) {
1462 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1463 " Response from the SGSN side");
1464 return -1;
1465 }
1466
1467 /* Find the tunnel created during request. This is coming from
1468 * the GGSN side, so to_ctrl corresponds to the SGSN. */
1469 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001470 tun = gtphub_tun_find(hub, other_side_idx(p->side_idx),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001471 p->plane_idx, to_ctrl, 0, p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001472
1473 if (!tun) {
1474 LOG(LOGL_ERROR, "Create PDP Context Response: cannot"
1475 " find matching request from SGSN %s, mapped TEI %x.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001476 gtphub_port_str(to_ctrl), p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001477 return -1;
1478 }
1479 }
1480
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001481 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1482 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001483 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001484
1485 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001486 int rc;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001487 struct gsn_addr addr_from_ie;
1488 uint32_t tei_from_ie;
1489 int ie_idx;
1490
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001491 /* Fetch GSN Address and TEI from IEs. As ensured by above
1492 * static asserts, plane_idx corresponds to the GSN Address IE
1493 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001494 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1495 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001496 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1497 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001498 return -1;
1499 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001500 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001501 gtphub_plane_idx_names[plane_idx],
1502 gsn_addr_to_str(&addr_from_ie),
1503 addr_from_ie.len);
1504
1505 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1506 if (ie_idx < 0) {
1507 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001508 LOG(LOGL_ERROR,
1509 "Create PDP Context message invalid:"
1510 " missing IE %d\n",
1511 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001512 return -1;
1513 }
1514 tei_from_ie = 0;
1515 }
1516 else
1517 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1518
1519 /* Make sure an entry for this peer address with default port
1520 * exists */
1521 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001522 gtphub_port_have(hub, &hub->to_gsns[side_idx][plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001523 &addr_from_ie,
1524 gtphub_plane_idx_default_port[plane_idx]);
1525
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001526 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1527 peer_from_ie);
1528
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001529 if (tei_from_ie) {
1530 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001531 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001532
1533 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1534 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001535 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001536
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001537 if (!gtphub_check_reused_teis(hub, tun)) {
1538 /* It's highly unlikely that all TEIs are
1539 * taken. But the code looking for an unused
1540 * TEI is, at the time of writing this comment,
1541 * not able to find gaps in the TEI space. To
1542 * explicitly alert the user of this problem,
1543 * rather abort than carry on. */
1544 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1545 abort();
1546 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001547 }
1548
1549 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001550 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1551 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001552 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001553 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1554 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001555 return -1;
1556 }
1557 }
1558
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001559 if (p->type == GTP_CREATE_PDP_REQ) {
1560 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1561 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001562 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001563 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1564 gtphub_tunnel_str(tun));
1565 }
1566
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001567 return 0;
1568}
1569
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001570static void pending_delete_del_cb(struct expiring_item *expi)
1571{
1572 struct pending_delete *pd;
1573 pd = container_of(expi, struct pending_delete, expiry_entry);
1574
1575 llist_del(&pd->entry);
1576 INIT_LLIST_HEAD(&pd->entry);
1577
1578 pd->expiry_entry.del_cb = 0;
1579 expiring_item_del(&pd->expiry_entry);
1580
1581 talloc_free(pd);
1582}
1583
1584static struct pending_delete *pending_delete_new(void)
1585{
1586 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1587 INIT_LLIST_HEAD(&pd->entry);
1588 expiring_item_init(&pd->expiry_entry);
1589 pd->expiry_entry.del_cb = pending_delete_del_cb;
1590 return pd;
1591}
1592
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001593static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1594 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001595 struct gtphub_tunnel *known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001596 struct gtphub_peer_port *from_ctrl,
1597 struct gtphub_peer_port *to_ctrl)
1598{
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001599 if (p->type == GTP_DELETE_PDP_REQ) {
1600 if (!known_tun) {
1601 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1602 return -1;
1603 }
1604
1605 /* Store the Delete Request until a successful Response is seen. */
1606 uint8_t teardown_ind;
1607 uint8_t nsapi;
1608
1609 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1610 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1611 return -1;
1612 }
1613
1614 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1615 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1616 return -1;
1617 }
1618
1619 struct pending_delete *pd = NULL;
1620
1621 struct pending_delete *pdi = NULL;
1622 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1623 if ((pdi->tun == known_tun)
1624 && (pdi->teardown_ind == teardown_ind)
1625 && (pdi->nsapi == nsapi)) {
1626 pd = pdi;
1627 break;
1628 }
1629 }
1630
1631 if (!pd) {
1632 pd = pending_delete_new();
1633 pd->tun = known_tun;
1634 pd->teardown_ind = teardown_ind;
1635 pd->nsapi = nsapi;
1636
1637 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1638 gtphub_tunnel_str(known_tun));
1639 llist_add(&pd->entry, &hub->pending_deletes);
1640 }
1641
1642 /* Add or refresh timeout. */
1643 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1644
1645 /* If a pending_delete should expire before the response to
1646 * indicate success comes in, the responding peer will have the
1647 * tunnel deactivated, while the requesting peer gets no reply
1648 * and keeps the tunnel. The hope is that the requesting peer
1649 * will try again and get a useful response. */
1650 } else if (p->type == GTP_DELETE_PDP_RSP) {
1651 /* Find the Delete Request for this Response. */
1652 struct pending_delete *pd = NULL;
1653
1654 struct pending_delete *pdi;
1655 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1656 if (known_tun == pdi->tun) {
1657 pd = pdi;
1658 break;
1659 }
1660 }
1661
1662 if (!pd) {
1663 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1664 " Cannot find matching request.");
1665 /* If we delete the tunnel now, anyone can send a
1666 * Delete response to kill tunnels at will. */
1667 return -1;
1668 }
1669
1670 /* TODO handle teardown_ind and nsapi */
1671
1672 expiring_item_del(&pd->expiry_entry);
1673
1674 uint8_t cause;
1675 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1676 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1677 " Missing Cause IE.");
1678 /* If we delete the tunnel now, at least one of the
1679 * peers may still think it is active. */
1680 return -1;
1681 }
1682
1683 if (cause != GTPCAUSE_ACC_REQ) {
1684 LOG(LOGL_NOTICE,
1685 "Delete PDP Context Response indicates failure;"
1686 "for %s\n",
1687 gtphub_tunnel_str(known_tun));
1688 return -1;
1689 }
1690
1691 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1692 gtphub_tunnel_str(known_tun));
1693 expiring_item_del(&known_tun->expiry_entry);
1694 }
1695
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001696 return 0;
1697}
1698
1699static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1700 struct gtp_packet_desc *p,
1701 struct gtphub_peer_port *from_ctrl,
1702 struct gtphub_peer_port *to_ctrl)
1703{
1704 /* TODO */
1705 return 0;
1706}
1707
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001708/* Read GSN address IEs from p, and make sure these peer addresses exist in
1709 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1710 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1711 * the packet p. */
1712static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1713 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001714 struct gtphub_tunnel *known_tun,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001715 struct gtphub_peer_port *from_ctrl,
1716 struct gtphub_peer_port *to_ctrl)
1717{
1718 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1719
1720 switch (p->type) {
1721 case GTP_CREATE_PDP_REQ:
1722 case GTP_CREATE_PDP_RSP:
1723 return gtphub_handle_create_pdp_ctx(hub, p,
1724 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001725
1726 case GTP_DELETE_PDP_REQ:
1727 case GTP_DELETE_PDP_RSP:
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001728 return gtphub_handle_delete_pdp_ctx(hub, p, known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001729 from_ctrl, to_ctrl);
1730
1731 case GTP_UPDATE_PDP_REQ:
1732 case GTP_UPDATE_PDP_RSP:
1733 return gtphub_handle_update_pdp_ctx(hub, p,
1734 from_ctrl, to_ctrl);
1735
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001736 default:
1737 /* Nothing to do for this message type. */
1738 return 0;
1739 }
1740
1741}
1742
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001743static int gtphub_write(const struct osmo_fd *to,
1744 const struct osmo_sockaddr *to_addr,
1745 const uint8_t *buf, size_t buf_len)
1746{
1747 errno = 0;
1748 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1749 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001750 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001751
1752 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001753 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001754 return -EINVAL;
1755 }
1756
1757 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001758 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1759 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001760 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001761 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1762 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001763
1764 return 0;
1765}
1766
1767static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1768{
1769 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1770 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001771 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1772 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001773 if (!(what & BSC_FD_READ))
1774 return 0;
1775
1776 struct gtphub *hub = from_ggsns_ofd->data;
1777
1778 static uint8_t buf[4096];
1779 struct osmo_sockaddr from_addr;
1780 struct osmo_sockaddr to_addr;
1781 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001782 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001783 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001784
1785 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1786 if (len < 1)
1787 return 0;
1788
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001789 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1790 buf, len, gtphub_now(),
1791 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001792 if (len < 1)
1793 return 0;
1794
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001795 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001796}
1797
1798static int gtphub_unmap(struct gtphub *hub,
1799 struct gtp_packet_desc *p,
1800 struct gtphub_peer_port *from,
1801 struct gtphub_peer_port *to_proxy,
1802 struct gtphub_peer_port **final_unmapped,
1803 struct gtphub_peer_port **unmapped_from_seq,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001804 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001805{
1806 /* Always (try to) unmap sequence and TEI numbers, which need to be
1807 * replaced in the packet. Either way, give precedence to the proxy, if
1808 * configured. */
1809
1810 struct gtphub_peer_port *from_seq = NULL;
1811 struct gtphub_peer_port *from_tei = NULL;
1812 struct gtphub_peer_port *unmapped = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001813 struct gtphub_tunnel *tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001814
1815 if (unmapped_from_seq)
1816 *unmapped_from_seq = from_seq;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001817 if (unmapped_from_tun)
1818 *unmapped_from_tun = tun;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001819 if (final_unmapped)
1820 *final_unmapped = unmapped;
1821
1822 from_seq = gtphub_unmap_seq(p, from);
1823
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001824 if (gtphub_unmap_header_tei(&from_tei, &tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001825 return -1;
1826
1827 struct gtphub_peer *from_peer = from->peer_addr->peer;
1828 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001829 LOG(LOGL_DEBUG,
1830 "Seq unmap and TEI unmap yield two different peers."
1831 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001832 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1833 gtphub_plane_idx_names[p->plane_idx],
1834 gtphub_peer_str(from_peer),
1835 (int)p->seq,
1836 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001837 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001838 gtphub_port_str2(from_tei)
1839 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001840 }
1841 unmapped = (from_seq? from_seq : from_tei);
1842
1843 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001844 LOG(LOGL_NOTICE,
1845 "Unmap yields a different peer than the configured proxy."
1846 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001847 " unmapped: %s proxy: %s\n",
1848 gtphub_port_str(unmapped),
1849 gtphub_port_str2(to_proxy)
1850 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001851 }
1852 unmapped = (to_proxy? to_proxy : unmapped);
1853
1854 if (!unmapped) {
1855 /* Return no error, but returned pointers are all NULL. */
1856 return 0;
1857 }
1858
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001859 if (unmapped_from_seq)
1860 *unmapped_from_seq = from_seq;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001861 if (unmapped_from_tun)
1862 *unmapped_from_tun = tun;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001863 if (final_unmapped)
1864 *final_unmapped = unmapped;
1865 return 0;
1866}
1867
1868static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1869 uint16_t port,
1870 struct osmo_sockaddr *dst)
1871{
1872 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1873}
1874
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001875/* If p is an Echo request, replace p's data with the matching response and
1876 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1877 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001878static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1879 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001880{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001881 if (p->type != GTP_ECHO_REQ)
1882 return 0;
1883
1884 static uint8_t echo_response_data[14] = {
1885 0x32, /* flags */
1886 GTP_ECHO_RSP,
1887 0x00, 14 - 8, /* Length in network byte order */
1888 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1889 0, 0, /* Seq, to be replaced */
1890 0, 0, /* no extensions */
1891 0x0e, /* Recovery IE */
1892 0 /* Recovery counter, to be replaced */
1893 };
1894 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1895 uint8_t *recovery = &echo_response_data[13];
1896
1897 *seq = hton16(p->seq);
1898 *recovery = hub->restart_counter;
1899
1900 *reply_buf = echo_response_data;
1901
1902 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001903}
1904
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001905struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1906 const struct osmo_sockaddr *addr);
1907
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001908/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1909 * address to forward to. Return a pointer to the osmo_fd, but copy the
1910 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1911 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1912 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001913int gtphub_handle_buf(struct gtphub *hub,
1914 unsigned int side_idx,
1915 unsigned int plane_idx,
1916 const struct osmo_sockaddr *from_addr,
1917 uint8_t *buf,
1918 size_t received,
1919 time_t now,
1920 uint8_t **reply_buf,
1921 struct osmo_fd **to_ofd,
1922 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001923{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001924 /*
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001925 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1926 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001927 */
1928 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
1929 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001930
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001931 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1932 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001933 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
1934 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001935 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001936 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01001937 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001938
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001939 static struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001940 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001941
1942 if (p.rc <= 0)
1943 return -1;
1944
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001945 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1946
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001947 int reply_len;
1948 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1949 if (reply_len > 0) {
1950 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001951 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001952 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001953
1954 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1955 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1956 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001957 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
1958 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
1959 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001960 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001961 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001962 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001963 if (reply_len < 0)
1964 return -1;
1965
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001966 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001967
1968 /* If a GGSN proxy is configured, check that it's indeed that proxy
1969 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1970 * gtphub, so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001971 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
1972 if (from_peer) {
1973 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001974 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001975 "Rejecting: %s proxy configured, but GTP packet"
1976 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001977 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001978 gtphub_side_idx_names[side_idx],
1979 gtphub_side_idx_names[side_idx],
1980 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001981 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001982 return -1;
1983 }
1984 }
1985
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001986 if (!from_peer) {
1987 /* Find or create a peer with a matching address. The sender's
1988 * port may in fact differ. */
1989 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990 }
1991
1992 /* If any PDP context has been created, we already have an entry for
1993 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1994 * us about. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001995 if (!from_peer) {
1996 if (side_idx == GTPH_SIDE_GGSN) {
1997 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1998 osmo_sockaddr_to_str(from_addr));
1999 return -1;
2000 } else {
2001 /* SGSN */
2002 /* A new peer. If this is on the Ctrl plane, an SGSN
2003 * may make first contact without being known yet, so
2004 * create the peer struct for the current sender. */
2005 if (plane_idx != GTPH_PLANE_CTRL) {
2006 LOG(LOGL_ERROR,
2007 "Dropping packet: User plane peer was not"
2008 "announced by PDP Context: %s\n",
2009 osmo_sockaddr_to_str(from_addr));
2010 return -1;
2011 }
2012
2013 struct gsn_addr from_gsna;
2014 uint16_t from_port;
2015 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2016 return -1;
2017
2018 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2019 }
2020 }
2021
2022 if (!from_peer) {
2023 /* This could theoretically happen for invalid address data or
2024 * somesuch. */
2025 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2026 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002027 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002028 return -1;
2029 }
2030
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002031 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2032 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002033
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002034 struct gtphub_peer_port *to_peer_from_seq;
2035 struct gtphub_peer_port *to_peer;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002036 struct gtphub_tunnel *tun;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002037 if (gtphub_unmap(hub, &p, from_peer,
2038 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002039 &to_peer, &to_peer_from_seq, &tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002040 != 0) {
2041 return -1;
2042 }
2043
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002044 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2045 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2046 return -1;
2047 }
2048
2049 if (!to_peer) {
2050 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2051 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002052 return -1;
2053 }
2054
2055 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002056 /* This may be a Create PDP Context response. If it is, there
2057 * are other addresses in the GTP message to set up apart from
2058 * the sender. */
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002059 if (gtphub_handle_pdp_ctx(hub, &p, tun, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002060 != 0)
2061 return -1;
2062 }
2063
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002064 gtphub_check_restart_counter(hub, &p, from_peer);
2065 gtphub_map_restart_counter(hub, &p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002066
2067 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002068 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002069 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002070 if (!to_peer_from_seq)
2071 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002072
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002073 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002074
2075 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002076
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002077 if (received) {
2078 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2079 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2080 received);
2081 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002082 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2083 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2084 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002085 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002086 return received;
2087}
2088
2089static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
2090{
2091 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
2092 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002093 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
2094 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002095
2096 if (!(what & BSC_FD_READ))
2097 return 0;
2098
2099 struct gtphub *hub = from_sgsns_ofd->data;
2100
2101 static uint8_t buf[4096];
2102 struct osmo_sockaddr from_addr;
2103 struct osmo_sockaddr to_addr;
2104 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01002105 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002106 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002107
2108 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
2109 if (len < 1)
2110 return 0;
2111
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002112 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
2113 buf, len, gtphub_now(),
2114 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002115 if (len < 1)
2116 return 0;
2117
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002118 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002119}
2120
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002121static void resolved_gssn_del_cb(struct expiring_item *expi)
2122{
2123 struct gtphub_resolved_ggsn *ggsn;
2124 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2125
2126 gtphub_port_ref_count_dec(ggsn->peer);
2127 llist_del(&ggsn->entry);
2128
2129 ggsn->expiry_entry.del_cb = 0;
2130 expiring_item_del(&ggsn->expiry_entry);
2131
2132 talloc_free(ggsn);
2133}
2134
2135void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2136 struct gsn_addr *resolved_addr,
2137 time_t now)
2138{
2139 struct gtphub_peer_port *pp;
2140 struct gtphub_resolved_ggsn *ggsn;
2141
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002142 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002143 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2144 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002145
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002146 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002147 resolved_addr, 2123);
2148 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002149 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2150 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002151 return;
2152 }
2153
2154 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2155 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002156 INIT_LLIST_HEAD(&ggsn->entry);
2157 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002158
2159 ggsn->peer = pp;
2160 gtphub_port_ref_count_inc(pp);
2161
2162 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002163 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002164
2165 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002166 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002167
2168 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2169}
2170
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002171static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2172{
2173 return pp->ref_count == 0;
2174}
2175
2176static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2177{
2178 struct gtphub_peer_port *pp, *npp;
2179 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2180 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002181 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002182 gtphub_port_str(pp));
2183 gtphub_peer_port_del(pp);
2184 }
2185 }
2186 return llist_empty(&pa->ports);
2187}
2188
2189static int gtphub_gc_peer(struct gtphub_peer *p)
2190{
2191 struct gtphub_peer_addr *pa, *npa;
2192 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2193 if (gtphub_gc_peer_addr(pa)) {
2194 gtphub_peer_addr_del(pa);
2195 }
2196 }
2197
2198 /* Note that there's a ref_count in each gtphub_peer_port instance
2199 * listed within p->addresses, referenced by TEI mappings from
2200 * hub->tei_map. As long as those don't expire, this peer will stay. */
2201
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002202 return llist_empty(&p->addresses)
2203 && nr_map_empty(&p->seq_map);
2204}
2205
2206static void gtphub_gc_bind(struct gtphub_bind *b)
2207{
2208 struct gtphub_peer *p, *n;
2209 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2210 if (gtphub_gc_peer(p)) {
2211 gtphub_peer_del(p);
2212 }
2213 }
2214}
2215
2216void gtphub_gc(struct gtphub *hub, time_t now)
2217{
2218 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002219 expired = expiry_tick(&hub->expire_quickly, now);
2220 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002221
2222 /* ... */
2223
2224 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002225 int s, p;
2226 for_each_side_and_plane(s, p) {
2227 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002228 }
2229 }
2230}
2231
2232static void gtphub_gc_cb(void *data)
2233{
2234 struct gtphub *hub = data;
2235 gtphub_gc(hub, gtphub_now());
2236 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2237}
2238
2239static void gtphub_gc_start(struct gtphub *hub)
2240{
2241 hub->gc_timer.cb = gtphub_gc_cb;
2242 hub->gc_timer.data = hub;
2243
2244 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2245}
2246
2247/* called by unit tests */
2248void gtphub_init(struct gtphub *hub)
2249{
2250 gtphub_zero(hub);
2251
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002252 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002253 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002254
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002255 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2256 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002257
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002258 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2259
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002260 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002261 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002262 for_each_side_and_plane(side_idx, plane_idx) {
2263 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002264 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002265
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002266 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2267 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2268 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2269 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002270}
2271
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002272/* For the test suite, this is kept separate from gtphub_stop(), which also
2273 * closes sockets. The test suite avoids using sockets and would cause
2274 * segfaults when trying to close uninitialized ofds. */
2275void gtphub_free(struct gtphub *hub)
2276{
2277 /* By expiring all mappings, a garbage collection should free
2278 * everything else. A gtphub_bind_free() will assert that everything is
2279 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002280 expiry_clear(&hub->expire_quickly);
2281 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002282
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002283 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002284 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002285 for_each_side_and_plane(side_idx, plane_idx) {
2286 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2287 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002288 }
2289}
2290
2291void gtphub_stop(struct gtphub *hub)
2292{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002293 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002294 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002295 for_each_side_and_plane(side_idx, plane_idx) {
2296 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002297 }
2298 gtphub_free(hub);
2299}
2300
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002301static int gtphub_make_proxy(struct gtphub *hub,
2302 struct gtphub_peer_port **pp,
2303 struct gtphub_bind *bind,
2304 const struct gtphub_cfg_addr *addr)
2305{
2306 if (!addr->addr_str)
2307 return 0;
2308
2309 struct gsn_addr gsna;
2310 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2311 return -1;
2312
2313 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2314
2315 /* This is *the* proxy. Make sure it is never expired. */
2316 gtphub_port_ref_count_inc(*pp);
2317 return 0;
2318}
2319
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002320int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2321 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002322{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002323 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002324
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002325 hub->restart_counter = restart_counter;
2326
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002327 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002328 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002329 if (gtphub_ares_init(hub) != 0) {
2330 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2331 return -1;
2332 }
2333 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002334
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002335 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002336 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002337 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002338 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002339 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2340 &cfg->to_gsns[side_idx][plane_idx],
2341 (side_idx == GTPH_SIDE_SGSN)
2342 ? from_sgsns_read_cb
2343 : from_ggsns_read_cb,
2344 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002345 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002346 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2347 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002348 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002349 return rc;
2350 }
2351 }
2352
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002353 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002354 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002355 &hub->proxy[side_idx][plane_idx],
2356 &hub->to_gsns[side_idx][plane_idx],
2357 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002358 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002359 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002360 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002361 gtphub_side_idx_names[side_idx],
2362 cfg->proxy[side_idx][plane_idx].addr_str,
2363 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002364 return -1;
2365 }
2366 }
2367
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002368 for_each_side_and_plane(side_idx, plane_idx) {
2369 if (hub->proxy[side_idx][plane_idx])
2370 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2371 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002372 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002373 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002374 }
2375
2376 gtphub_gc_start(hub);
2377 return 0;
2378}
2379
2380static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2381 const struct gsn_addr *addr)
2382{
2383 struct gtphub_peer_addr *a;
2384 llist_for_each_entry(a, &peer->addresses, entry) {
2385 if (gsn_addr_same(&a->addr, addr))
2386 return a;
2387 }
2388 return NULL;
2389}
2390
2391static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2392 uint16_t port)
2393{
2394 OSMO_ASSERT(port);
2395 struct gtphub_peer_port *pp;
2396 llist_for_each_entry(pp, &a->ports, entry) {
2397 if (pp->port == port)
2398 return pp;
2399 }
2400 return NULL;
2401}
2402
2403static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2404 const struct gsn_addr *addr)
2405{
2406 struct gtphub_peer *peer;
2407 llist_for_each_entry(peer, &bind->peers, entry) {
2408 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2409 if (a)
2410 return a;
2411 }
2412 return NULL;
2413}
2414
2415static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2416 const struct gsn_addr *addr,
2417 uint16_t port)
2418{
2419 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2420 if (!a)
2421 return NULL;
2422 return gtphub_addr_find_port(a, port);
2423}
2424
2425struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2426 const struct osmo_sockaddr *addr)
2427{
2428 struct gsn_addr gsna;
2429 uint16_t port;
2430 gsn_addr_from_sockaddr(&gsna, &port, addr);
2431 return gtphub_port_find(bind, &gsna, port);
2432}
2433
2434static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2435 struct gtphub_bind *bind)
2436{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002437 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2438 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002439 OSMO_ASSERT(peer);
2440
2441 INIT_LLIST_HEAD(&peer->addresses);
2442
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002443 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002444 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002445
2446 /* TODO use something random to pick the initial sequence nr.
2447 0x6d31 produces the ASCII character sequence 'm1', currently used in
2448 gtphub_nc_test.sh. */
2449 peer->seq_pool.last_nr = 0x6d31 - 1;
2450
2451 llist_add(&peer->entry, &bind->peers);
2452 return peer;
2453}
2454
2455static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2456 const struct gsn_addr *addr)
2457{
2458 struct gtphub_peer_addr *a;
2459 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2460 OSMO_ASSERT(a);
2461 a->peer = peer;
2462 gsn_addr_copy(&a->addr, addr);
2463 INIT_LLIST_HEAD(&a->ports);
2464 llist_add(&a->entry, &peer->addresses);
2465
2466 return a;
2467}
2468
2469static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2470 struct gtphub_bind *bind,
2471 const struct gsn_addr *addr)
2472{
2473 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2474 if (a)
2475 return a;
2476
2477 /* If we haven't found an address, that means we need to create an
2478 * entirely new peer for the new address. More addresses may be added
2479 * to this peer later, but not via this function. */
2480 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002481
2482 a = gtphub_peer_add_addr(peer, addr);
2483
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002484 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002485 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002486 gsn_addr_to_str(&a->addr));
2487
2488 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002489}
2490
2491static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2492 uint16_t port)
2493{
2494 struct gtphub_peer_port *pp;
2495
2496 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2497 OSMO_ASSERT(pp);
2498 pp->peer_addr = a;
2499 pp->port = port;
2500
2501 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2502 talloc_free(pp);
2503 return NULL;
2504 }
2505
2506 llist_add(&pp->entry, &a->ports);
2507
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002508 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002509 gsn_addr_to_str(&a->addr),
2510 (int)port);
2511
2512 return pp;
2513}
2514
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002515struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2516 struct gtphub_bind *bind,
2517 const struct gsn_addr *addr,
2518 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002519{
2520 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2521
2522 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2523 if (pp)
2524 return pp;
2525
2526 return gtphub_addr_add_port(a, port);
2527}
2528
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002529/* Find a GGSN peer with a matching address. If the address is known but the
2530 * port not, create a new port for that peer address. */
2531struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2532 const struct osmo_sockaddr *addr)
2533{
2534 struct gtphub_peer_addr *pa;
2535 struct gtphub_peer_port *pp;
2536
2537 struct gsn_addr gsna;
2538 uint16_t port;
2539 gsn_addr_from_sockaddr(&gsna, &port, addr);
2540
2541 pa = gtphub_addr_find(bind, &gsna);
2542 if (!pa)
2543 return NULL;
2544
2545 pp = gtphub_addr_find_port(pa, port);
2546
2547 if (!pp)
2548 pp = gtphub_addr_add_port(pa, port);
2549
2550 return pp;
2551}
2552
2553
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002554/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2555 * resolution should be possible but failed, and 1 if resolution was
2556 * successful. *pp will be set to NULL if <1 is returned. */
2557static int gtphub_resolve_ggsn(struct gtphub *hub,
2558 struct gtp_packet_desc *p,
2559 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002560{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002561 *pp = NULL;
2562
2563 /* TODO determine from message type whether IEs should be present? */
2564
2565 int rc;
2566 const char *imsi_str;
2567 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2568 if (rc < 1)
2569 return rc;
2570 OSMO_ASSERT(imsi_str);
2571
2572 const char *apn_str;
2573 rc = get_ie_apn_str(p->ie, &apn_str);
2574 if (rc < 1)
2575 return rc;
2576 OSMO_ASSERT(apn_str);
2577
2578 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2579 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002580}
2581
2582
2583/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002584/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002585/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2586 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002587static int _osmo_getaddrinfo(struct addrinfo **result,
2588 uint16_t family, uint16_t type, uint8_t proto,
2589 const char *host, uint16_t port)
2590{
2591 struct addrinfo hints;
2592 char portbuf[16];
2593
2594 sprintf(portbuf, "%u", port);
2595 memset(&hints, '\0', sizeof(struct addrinfo));
2596 hints.ai_family = family;
2597 if (type == SOCK_RAW) {
2598 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2599 * SOCK_RAW and IPPROTO_GRE is used.
2600 */
2601 hints.ai_socktype = SOCK_DGRAM;
2602 hints.ai_protocol = IPPROTO_UDP;
2603 } else {
2604 hints.ai_socktype = type;
2605 hints.ai_protocol = proto;
2606 }
2607
2608 return getaddrinfo(host, portbuf, &hints, result);
2609}
2610
2611/* TODO move to osmocom/core/socket.c ? */
2612int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2613 uint16_t family, uint16_t type, uint8_t proto,
2614 const char *host, uint16_t port)
2615{
2616 struct addrinfo *res;
2617 int rc;
2618 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2619
2620 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002621 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002622 return -EINVAL;
2623 }
2624
2625 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2626 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2627 addr->l = res->ai_addrlen;
2628 freeaddrinfo(res);
2629
2630 return 0;
2631}
2632
2633int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2634 char *port_str, size_t port_str_len,
2635 const struct osmo_sockaddr *addr,
2636 int flags)
2637{
2638 int rc;
2639
2640 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2641 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2642 return -1;
2643 }
2644
2645 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002646 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2647 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002648 return -1;
2649 }
2650
2651 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2652 addr_str, addr_str_len,
2653 port_str, port_str_len,
2654 flags);
2655
2656 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002657 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2658 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2659 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002660
2661 return rc;
2662}
2663
2664const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2665 char *buf, size_t buf_len)
2666{
2667 const int portbuf_len = 6;
2668 OSMO_ASSERT(buf_len > portbuf_len);
2669 char *portbuf = buf + buf_len - portbuf_len;
2670 buf_len -= portbuf_len;
2671 if (osmo_sockaddr_to_strs(buf, buf_len,
2672 portbuf, portbuf_len,
2673 addr,
2674 NI_NUMERICHOST | NI_NUMERICSERV))
2675 return NULL;
2676
2677 char *pos = buf + strnlen(buf, buf_len-1);
2678 size_t len = buf_len - (pos - buf);
2679
2680 snprintf(pos, len, " port %s", portbuf);
2681 buf[buf_len-1] = '\0';
2682
2683 return buf;
2684}
2685
2686const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2687{
2688 static char buf[256];
2689 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2690 if (! result)
2691 return "(invalid)";
2692 return result;
2693}
2694
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002695int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2696 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002697{
2698 if (a == b)
2699 return 0;
2700 if (!a)
2701 return -1;
2702 if (!b)
2703 return 1;
2704 if (a->l != b->l) {
2705 /* Lengths are not the same, but determine the order. Will
2706 * anyone ever sort a list by osmo_sockaddr though...? */
2707 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2708 if (cmp == 0) {
2709 if (a->l < b->l)
2710 return -1;
2711 else
2712 return 1;
2713 }
2714 return cmp;
2715 }
2716 return memcmp(&a->a, &b->a, a->l);
2717}
2718
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002719void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2720 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002721{
2722 OSMO_ASSERT(src->l <= sizeof(dst->a));
2723 memcpy(&dst->a, &src->a, src->l);
2724 dst->l = src->l;
2725}