blob: d7422de0fd2f23f1d7b487fe09b6819ac6fbbd80 [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 Hofmeyr1ba50c62015-11-20 01:28:40 +010098
99/* counters */
100
101enum gtphub_counters_io {
102 GTPH_CTR_PKTS_IN = 0,
103 GTPH_CTR_PKTS_OUT,
104 GTPH_CTR_BYTES_IN,
105 GTPH_CTR_BYTES_OUT
106};
107
108static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
109 { "packets.in", "Packets ( In)" },
110 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100111 { "bytes.in", "Bytes ( In)" },
112 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100113};
114
115static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
116 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100117 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100118 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
119 .ctr_desc = gtphub_counters_io_desc,
120 .class_id = OSMO_STATS_CLASS_GLOBAL,
121};
122
123
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200124void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
125{
126 memcpy(gsna, src, sizeof(struct gsn_addr));
127}
128
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200129int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
130 const struct osmo_sockaddr *sa)
131{
132 char addr_str[256];
133 char port_str[6];
134
135 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
136 port_str, sizeof(port_str),
137 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
138 != 0) {
139 return -1;
140 }
141
142 if (port)
143 *port = atoi(port_str);
144
145 return gsn_addr_from_str(gsna, addr_str);
146}
147
148int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
149{
150 int af = AF_INET;
151 gsna->len = 4;
152 const char *pos = numeric_addr_str;
153 for (; *pos; pos++) {
154 if (*pos == ':') {
155 af = AF_INET6;
156 gsna->len = 16;
157 break;
158 }
159 }
160
161 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
162 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100163 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
164 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200165 return -1;
166 }
167 return 0;
168}
169
170const char *gsn_addr_to_str(const struct gsn_addr *gsna)
171{
172 static char buf[INET6_ADDRSTRLEN + 1];
173 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
174}
175
176const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
177 char *strbuf,
178 int strbuf_len)
179{
180 int af;
181 switch (gsna->len) {
182 case 4:
183 af = AF_INET;
184 break;
185 case 16:
186 af = AF_INET6;
187 break;
188 default:
189 return NULL;
190 }
191
192 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
193 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100194 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
195 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100196 strerror(errno),
197 (int)gsna->len,
198 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200199 }
200 return r;
201}
202
203int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
204{
205 if (a == b)
206 return 1;
207 if ((!a) || (!b))
208 return 0;
209 if (a->len != b->len)
210 return 0;
211 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
212}
213
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100214static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
215 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200216{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100217 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200218 return -1;
219
220 unsigned int len;
221 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
222 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
223 &len, gsna->buf, sizeof(gsna->buf))
224 != 0)
225 return -1;
226 gsna->len = len;
227 return 0;
228}
229
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100230static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
231 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200232{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100233 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200234 return -1;
235
236 int ie_idx;
237 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
238
239 if (ie_idx < 0)
240 return -1;
241
242 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
243 int ie_l = ntoh16(ie->l);
244 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100245 LOG(LOGL_ERROR, "Not implemented:"
246 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200247 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
248 return -1;
249 }
250
251 memcpy(ie->v, gsna->buf, (int)ie_l);
252 return 0;
253}
254
255/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
256 */
257void validate_gtp0_header(struct gtp_packet_desc *p)
258{
259 const struct gtp0_header *pheader = &(p->data->gtp0.h);
260 p->rc = GTP_RC_UNKNOWN;
261 p->header_len = 0;
262
263 OSMO_ASSERT(p->data_len >= 1);
264 OSMO_ASSERT(p->version == 0);
265
266 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100267 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200268 p->rc = GTP_RC_TOOSHORT;
269 return;
270 }
271
272 p->type = ntoh8(pheader->type);
273 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100274 p->header_tei_rx = 0; /* TODO */
275 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200276
277 if (p->data_len == GTP0_HEADER_SIZE) {
278 p->rc = GTP_RC_TINY;
279 p->header_len = GTP0_HEADER_SIZE;
280 return;
281 }
282
283 /* Check packet length field versus length of packet */
284 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100285 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
286 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100287 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
288 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200289 p->rc = GTP_RC_TOOSHORT;
290 return;
291 }
292
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100293 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200294 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100295 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200296}
297
298/* Validate GTP version 1 data, and update p->rc with the result, as well as
299 * p->header_len in case of a valid header. */
300void validate_gtp1_header(struct gtp_packet_desc *p)
301{
302 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
303 p->rc = GTP_RC_UNKNOWN;
304 p->header_len = 0;
305
306 OSMO_ASSERT(p->data_len >= 1);
307 OSMO_ASSERT(p->version == 1);
308
309 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
310 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100311 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200312 p->rc = GTP_RC_TOOSHORT;
313 return;
314 }
315
316 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100317 p->header_tei_rx = ntoh32(pheader->tei);
318 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200319 p->seq = ntoh16(pheader->seq);
320
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100321 LOG(LOGL_DEBUG, "GTPv1\n"
322 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
323 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
324 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
325 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
326 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
327 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
328 p->type, p->type,
329 ntoh16(pheader->length), ntoh16(pheader->length),
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100330 p->header_tei_rx, p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100331 p->seq, p->seq,
332 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200333 pheader->next, pheader->next);
334
335 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
336 p->rc = GTP_RC_TINY;
337 p->header_len = GTP1_HEADER_SIZE_SHORT;
338 return;
339 }
340
341 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100342 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
343 if (p->data_len != announced_len) {
344 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
345 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100346 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
347 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200348 p->rc = GTP_RC_TOOSHORT;
349 return;
350 }
351
Neels Hofmeyre921e322015-11-11 00:45:50 +0100352 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200353 p->header_len = GTP1_HEADER_SIZE_LONG;
354}
355
356/* Examine whether p->data of size p->data_len has a valid GTP header. Set
357 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
358 * gtp_rc). p->data must point at a buffer with p->data_len set. */
359void validate_gtp_header(struct gtp_packet_desc *p)
360{
361 p->rc = GTP_RC_UNKNOWN;
362
363 /* Need at least 1 byte in order to check version */
364 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100365 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
366 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200367 p->rc = GTP_RC_TOOSHORT;
368 return;
369 }
370
371 p->version = p->data->flags >> 5;
372
373 switch (p->version) {
374 case 0:
375 validate_gtp0_header(p);
376 break;
377 case 1:
378 validate_gtp1_header(p);
379 break;
380 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100381 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200382 p->rc = GTP_RC_UNSUPPORTED_VERSION;
383 break;
384 }
385}
386
387
388/* Return the value of the i'th IMSI IEI by copying to *imsi.
389 * The first IEI is reached by passing i = 0.
390 * imsi must point at allocated space of (at least) 8 bytes.
391 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100392static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200393{
394 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
395}
396
397/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100398static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200399{
400 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
401}
402
403static char imsi_digit_to_char(uint8_t nibble)
404{
405 nibble &= 0x0f;
406 if (nibble > 9)
407 return (nibble == 0x0f) ? '\0' : '?';
408 return '0' + nibble;
409}
410
411/* Return a human readable IMSI string, in a static buffer.
412 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100413static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200414{
415 static char str[17];
416 int i;
417
418 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100419 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100420 c = imsi_digit_to_char(imsi[i]);
421 if (c == '?')
422 return -1;
423 str[2*i] = c;
424
425 c = imsi_digit_to_char(imsi[i] >> 4);
426 if (c == '?')
427 return -1;
428 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200429 }
430 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100431 *imsi_str = str;
432 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200433}
434
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100435/* Return 0 if not present, 1 if present and decoded successfully, -1 if
436 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100437static int get_ie_imsi_str(union gtpie_member *ie[], int i,
438 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100439{
440 uint8_t imsi_buf[8];
441 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100442 return 0;
443 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100444}
445
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100446/* Return 0 if not present, 1 if present and decoded successfully, -1 if
447 * present but cannot be decoded. */
448static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100449{
450 static char apn_buf[GSM_APN_LENGTH];
451 unsigned int len;
452 if (gtpie_gettlv(ie, GTPIE_APN, 0,
453 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100454 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100455
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100456 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100457 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
458 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100459 return -1;
460 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100461
462 if (len > (sizeof(apn_buf) - 1))
463 len = sizeof(apn_buf) - 1;
464 apn_buf[len] = '\0';
465
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
467 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100468 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
469 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100470 return -1;
471 }
472 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100473}
474
475
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200476/* Validate header, and index information elements. Write decoded packet
477 * information to *res. res->data will point at the given data buffer. On
478 * error, p->rc is set <= 0 (see enum gtp_rc). */
479static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100480 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200481 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100482 struct gtp_packet_desc *res,
483 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200484{
485 ZERO_STRUCT(res);
486 res->data = (union gtp_packet*)data;
487 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100488 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200489 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100490 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200491
492 validate_gtp_header(res);
493
494 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100495 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496 return;
497 }
498
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100499 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200500
Neels Hofmeyre921e322015-11-11 00:45:50 +0100501 if (from_plane_idx == GTPH_PLANE_USER) {
502 res->rc = GTP_RC_PDU_U;
503 return;
504 }
505
506 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100507 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508 return;
509 }
510
511 if (gtpie_decaps(res->ie, res->version,
512 (void*)(data + res->header_len),
513 res->data_len - res->header_len) != 0) {
514 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100515 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
516 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200517 return;
518 }
519
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100520#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100521 /* TODO if (<loglevel is debug>) { ...
522 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200523 int i;
524
525 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100526 const char *imsi;
527 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200528 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100529 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200530 }
531
532 for (i = 0; i < 10; i++) {
533 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100534 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200535 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100536 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200537 }
538
539 for (i = 0; i < 2; i++) {
540 struct gsn_addr addr;
541 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100542 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200543 }
544
545 for (i = 0; i < 10; i++) {
546 uint32_t tei;
547 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
548 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100549 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200550 tei, tei);
551 }
552
553 for (i = 0; i < 10; i++) {
554 uint32_t tei;
555 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
556 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100557 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200558 tei, tei);
559 }
560#endif
561}
562
563
564/* expiry */
565
566void expiry_init(struct expiry *exq, int expiry_in_seconds)
567{
568 ZERO_STRUCT(exq);
569 exq->expiry_in_seconds = expiry_in_seconds;
570 INIT_LLIST_HEAD(&exq->items);
571}
572
573void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
574{
575 item->expiry = now + exq->expiry_in_seconds;
576
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100577 OSMO_ASSERT(llist_empty(&exq->items)
578 || (item->expiry
579 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
580
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200581 /* Add/move to the tail to always sort by expiry, ascending. */
582 llist_del(&item->entry);
583 llist_add_tail(&item->entry, &exq->items);
584}
585
586int expiry_tick(struct expiry *exq, time_t now)
587{
588 int expired = 0;
589 struct expiring_item *m, *n;
590 llist_for_each_entry_safe(m, n, &exq->items, entry) {
591 if (m->expiry <= now) {
592 expiring_item_del(m);
593 expired ++;
594 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200595 /* The items are added sorted by expiry. So when we hit
596 * an unexpired entry, only more unexpired ones will
597 * follow. */
598 break;
599 }
600 }
601 return expired;
602}
603
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100604void expiry_clear(struct expiry *exq)
605{
606 struct expiring_item *m, *n;
607 llist_for_each_entry_safe(m, n, &exq->items, entry) {
608 expiring_item_del(m);
609 }
610}
611
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200612void expiring_item_init(struct expiring_item *item)
613{
614 ZERO_STRUCT(item);
615 INIT_LLIST_HEAD(&item->entry);
616}
617
618void expiring_item_del(struct expiring_item *item)
619{
620 OSMO_ASSERT(item);
621 llist_del(&item->entry);
622 INIT_LLIST_HEAD(&item->entry);
623 if (item->del_cb) {
624 /* avoid loops */
625 del_cb_t del_cb = item->del_cb;
626 item->del_cb = 0;
627 (del_cb)(item);
628 }
629}
630
631
632/* nr_map, nr_pool */
633
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100634void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200635{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100636 *pool = (struct nr_pool){
637 .nr_min = nr_min,
638 .nr_max = nr_max,
639 .last_nr = nr_max
640 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200641}
642
643nr_t nr_pool_next(struct nr_pool *pool)
644{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100645 if (pool->last_nr >= pool->nr_max)
646 pool->last_nr = pool->nr_min;
647 else
648 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200649
650 return pool->last_nr;
651}
652
653void nr_map_init(struct nr_map *map, struct nr_pool *pool,
654 struct expiry *exq)
655{
656 ZERO_STRUCT(map);
657 map->pool = pool;
658 map->add_items_to_expiry = exq;
659 INIT_LLIST_HEAD(&map->mappings);
660}
661
662void nr_mapping_init(struct nr_mapping *m)
663{
664 ZERO_STRUCT(m);
665 INIT_LLIST_HEAD(&m->entry);
666 expiring_item_init(&m->expiry_entry);
667}
668
669void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
670{
671 /* Generate a mapped number */
672 mapping->repl = nr_pool_next(map->pool);
673
674 /* Add to the tail to always yield a list sorted by expiry, in
675 * ascending order. */
676 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100677 nr_map_refresh(map, mapping, now);
678}
679
680void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
681{
682 if (!map->add_items_to_expiry)
683 return;
684 expiry_add(map->add_items_to_expiry,
685 &mapping->expiry_entry,
686 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200687}
688
689void nr_map_clear(struct nr_map *map)
690{
691 struct nr_mapping *m;
692 struct nr_mapping *n;
693 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
694 nr_mapping_del(m);
695 }
696}
697
698int nr_map_empty(const struct nr_map *map)
699{
700 return llist_empty(&map->mappings);
701}
702
703struct nr_mapping *nr_map_get(const struct nr_map *map,
704 void *origin, nr_t nr_orig)
705{
706 struct nr_mapping *mapping;
707 llist_for_each_entry(mapping, &map->mappings, entry) {
708 if ((mapping->origin == origin)
709 && (mapping->orig == nr_orig))
710 return mapping;
711 }
712 /* Not found. */
713 return NULL;
714}
715
716struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
717{
718 struct nr_mapping *mapping;
719 llist_for_each_entry(mapping, &map->mappings, entry) {
720 if (mapping->repl == nr_repl) {
721 return mapping;
722 }
723 }
724 /* Not found. */
725 return NULL;
726}
727
728void nr_mapping_del(struct nr_mapping *mapping)
729{
730 OSMO_ASSERT(mapping);
731 llist_del(&mapping->entry);
732 INIT_LLIST_HEAD(&mapping->entry);
733 expiring_item_del(&mapping->expiry_entry);
734}
735
736
737/* gtphub */
738
739const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
740 "CTRL",
741 "USER",
742};
743
744const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
745 2123,
746 2152,
747};
748
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100749const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
750 "SGSN",
751 "GGSN",
752};
753
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200754time_t gtphub_now(void)
755{
756 struct timespec now_tp;
757 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
758 return now_tp.tv_sec;
759}
760
761/* Remove a gtphub_peer from its list and free it. */
762static void gtphub_peer_del(struct gtphub_peer *peer)
763{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100764 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200765 nr_map_clear(&peer->seq_map);
766 llist_del(&peer->entry);
767 talloc_free(peer);
768}
769
770static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
771{
772 OSMO_ASSERT(llist_empty(&pa->ports));
773 llist_del(&pa->entry);
774 talloc_free(pa);
775}
776
777static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
778{
779 OSMO_ASSERT(pp->ref_count == 0);
780 llist_del(&pp->entry);
781 talloc_free(pp);
782}
783
784/* From the information in the gtp_packet_desc, return the address of a GGSN.
785 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100786static int gtphub_resolve_ggsn(struct gtphub *hub,
787 struct gtp_packet_desc *p,
788 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200789
790/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100791struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
792 const char *imsi_str,
793 const char *apn_ni_str);
794int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200795
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200796static void gtphub_zero(struct gtphub *hub)
797{
798 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100799 INIT_LLIST_HEAD(&hub->ggsn_lookups);
800 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200801}
802
803static int gtphub_sock_init(struct osmo_fd *ofd,
804 const struct gtphub_cfg_addr *addr,
805 osmo_fd_cb_t cb,
806 void *data,
807 int ofd_id)
808{
809 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100810 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200811 return -1;
812 }
813 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100814 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200815 return -1;
816 }
817
818 ofd->when = BSC_FD_READ;
819 ofd->cb = cb;
820 ofd->data = data;
821 ofd->priv_nr = ofd_id;
822
823 int rc;
824 rc = osmo_sock_init_ofd(ofd,
825 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
826 addr->addr_str, addr->port,
827 OSMO_SOCK_F_BIND);
828 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100829 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
830 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200831 return -1;
832 }
833
834 return 0;
835}
836
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100837static void gtphub_sock_close(struct osmo_fd *ofd)
838{
839 close(ofd->fd);
840 osmo_fd_unregister(ofd);
841 ofd->cb = NULL;
842}
843
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200844static void gtphub_bind_init(struct gtphub_bind *b)
845{
846 ZERO_STRUCT(b);
847
848 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100849
850 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
851 &gtphub_ctrg_io_desc, 0);
852 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200853}
854
855static int gtphub_bind_start(struct gtphub_bind *b,
856 const struct gtphub_cfg_bind *cfg,
857 osmo_fd_cb_t cb, void *cb_data,
858 unsigned int ofd_id)
859{
860 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
861 return -1;
862 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
863 return -1;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100864 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200865 return 0;
866}
867
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100868static void gtphub_bind_free(struct gtphub_bind *b)
869{
870 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100871 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100872}
873
874static void gtphub_bind_stop(struct gtphub_bind *b) {
875 gtphub_sock_close(&b->ofd);
876 gtphub_bind_free(b);
877}
878
Neels Hofmeyr40348972015-11-17 12:22:28 +0100879/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880 * Return the number of bytes read, zero on error. */
881static int gtphub_read(const struct osmo_fd *from,
882 struct osmo_sockaddr *from_addr,
883 uint8_t *buf, size_t buf_len)
884{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100885 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200886
Neels Hofmeyr40348972015-11-17 12:22:28 +0100887 /* recvfrom requires the available length set in *from_addr_len. */
888 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200889 errno = 0;
890 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100891 (struct sockaddr*)&from_addr->a,
892 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200893 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
894 * is not truncated. Then maybe reduce buf's size. */
895
896 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100897 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
898 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200899 return 0;
900 }
901
Neels Hofmeyr40348972015-11-17 12:22:28 +0100902 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
903 (int)received, osmo_sockaddr_to_str(from_addr),
904 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200905
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200906 return received;
907}
908
909inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
910{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100911 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200912 OSMO_ASSERT(pp->ref_count < UINT_MAX);
913 pp->ref_count++;
914}
915
916inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
917{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100918 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200919 OSMO_ASSERT(pp->ref_count > 0);
920 pp->ref_count--;
921}
922
923inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
924{
925 OSMO_ASSERT(p->version == 1);
926 p->data->gtp1l.h.seq = hton16(seq);
927 p->seq = seq;
928}
929
930inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
931{
932 OSMO_ASSERT(p->version == 1);
933 p->data->gtp1l.h.tei = hton32(tei);
934 p->header_tei = tei;
935}
936
937static void gtphub_mapping_del_cb(struct expiring_item *expi);
938
939static struct nr_mapping *gtphub_mapping_new()
940{
941 struct nr_mapping *nrm;
942 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
943 OSMO_ASSERT(nrm);
944
945 nr_mapping_init(nrm);
946 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
947 return nrm;
948}
949
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100950
951#define APPEND(args...) \
952 l = snprintf(pos, left, args); \
953 pos += l; \
954 left -= l
955
956static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
957 int side_idx)
958{
959 static char buf[256];
960 char *pos = buf;
961 int left = sizeof(buf);
962 int l;
963
964 struct gtphub_tunnel_endpoint *c, *u;
965 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
966 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
967
968 /* print both only if they differ. */
969 if (!c->peer) {
970 APPEND("(uninitialized)");
971 } else {
972 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
973 }
974
975 if (!u->peer) {
976 if (c->peer) {
977 APPEND(" / (uninitialized)");
978 }
979 } else if ((!c->peer)
980 || (!gsn_addr_same(&u->peer->peer_addr->addr,
981 &c->peer->peer_addr->addr))) {
982 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
983 }
984
985 APPEND(" (TEI C %x=%x / U %x=%x)",
986 c->tei_orig, c->tei_repl,
987 u->tei_orig, u->tei_repl);
988 return buf;
989}
990
991const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
992{
993 static char buf[512];
994 char *pos = buf;
995 int left = sizeof(buf);
996 int l;
997
998 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
999 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1000
1001 return buf;
1002}
1003
1004#undef APPEND
1005
1006void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1007 struct gtphub_peer_port *pp)
1008{
1009 if (te->peer)
1010 gtphub_port_ref_count_dec(te->peer);
1011 te->peer = pp;
1012 if (te->peer)
1013 gtphub_port_ref_count_inc(te->peer);
1014}
1015
1016int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1017{
1018 if (!tun)
1019 return 0;
1020 int side_idx;
1021 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001022 for_each_side_and_plane(side_idx, plane_idx) {
1023 struct gtphub_tunnel_endpoint *te =
1024 &tun->endpoint[side_idx][plane_idx];
1025 if (!(te->peer && te->tei_orig && te->tei_repl))
1026 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001027 }
1028 return 1;
1029}
1030
1031static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1032{
1033 struct gtphub_tunnel *tun = container_of(expi,
1034 struct gtphub_tunnel,
1035 expiry_entry);
1036 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1037
1038 llist_del(&tun->entry);
1039 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1040
1041 expi->del_cb = 0; /* avoid recursion loops */
1042 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1043
1044 int side_idx;
1045 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001046 for_each_side_and_plane(side_idx, plane_idx) {
1047 /* clear ref count */
1048 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1049 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001050 }
1051
1052 talloc_free(tun);
1053}
1054
1055static struct gtphub_tunnel *gtphub_tunnel_new()
1056{
1057 struct gtphub_tunnel *tun;
1058 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1059 OSMO_ASSERT(tun);
1060
1061 INIT_LLIST_HEAD(&tun->entry);
1062 expiring_item_init(&tun->expiry_entry);
1063
1064 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1065 return tun;
1066}
1067
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001068static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1069 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001070{
1071 if (llist_empty(&peer->addresses))
1072 return "(addressless)";
1073
1074 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1075 struct gtphub_peer_addr,
1076 entry);
1077 return gsn_addr_to_strb(&a->addr, buf, buflen);
1078}
1079
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001080static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1081 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001082{
1083 if (!port)
1084 return "(null port)";
1085
1086 snprintf(buf, buflen, "%s port %d",
1087 gsn_addr_to_str(&port->peer_addr->addr),
1088 (int)port->port);
1089 return buf;
1090}
1091
1092const char *gtphub_peer_str(struct gtphub_peer *peer)
1093{
1094 static char buf[256];
1095 return gtphub_peer_strb(peer, buf, sizeof(buf));
1096}
1097
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001098const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001099{
1100 static char buf[256];
1101 return gtphub_port_strb(port, buf, sizeof(buf));
1102}
1103
1104static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1105{
1106 static char buf[256];
1107 return gtphub_port_strb(port, buf, sizeof(buf));
1108}
1109
1110static void gtphub_mapping_del_cb(struct expiring_item *expi)
1111{
1112 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001113 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001114
1115 struct nr_mapping *nrm = container_of(expi,
1116 struct nr_mapping,
1117 expiry_entry);
1118 llist_del(&nrm->entry);
1119 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1120
1121 /* Just for log */
1122 struct gtphub_peer_port *from = nrm->origin;
1123 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001124 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001125 (int)nrm->expiry_entry.expiry,
1126 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001127 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001128
1129 gtphub_port_ref_count_dec(from);
1130
1131 talloc_free(nrm);
1132}
1133
1134static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1135 struct gtphub_peer_port *from,
1136 nr_t orig_nr,
1137 time_t now)
1138{
1139 struct nr_mapping *nrm;
1140
1141 nrm = nr_map_get(map, from, orig_nr);
1142
1143 if (!nrm) {
1144 nrm = gtphub_mapping_new();
1145 nrm->orig = orig_nr;
1146 nrm->origin = from;
1147 nr_map_add(map, nrm, now);
1148 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001149 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001150 gtphub_port_str(from),
1151 (int)(nrm->orig), (int)(nrm->repl));
1152 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001153 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001154 }
1155
1156 OSMO_ASSERT(nrm);
1157 return nrm;
1158}
1159
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001160static void gtphub_map_seq(struct gtp_packet_desc *p,
1161 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001162 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001163{
1164 /* Store a mapping in to_peer's map, so when we later receive a GTP
1165 * packet back from to_peer, the seq nr can be unmapped back to its
1166 * origin (from_peer here). */
1167 struct nr_mapping *nrm;
1168 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001169 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001170
1171 /* Change the GTP packet to yield the new, mapped seq nr */
1172 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001173}
1174
1175static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1176 struct gtphub_peer_port *responding_port)
1177{
1178 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001179 struct nr_mapping *nrm =
1180 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1181 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001182 if (!nrm)
1183 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001184 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1185 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001186 set_seq(p, nrm->orig);
1187 return nrm->origin;
1188}
1189
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001190static struct gtphub_tunnel *gtphub_tun_find(struct gtphub *hub,
1191 int side_idx,
1192 int plane_idx,
1193 struct gtphub_peer_port *from,
1194 uint32_t tei_orig,
1195 uint32_t tei_repl)
1196{
1197 OSMO_ASSERT(from);
1198
1199 struct gtphub_tunnel *tun;
1200 /* TODO: optimize: don't iterate *all* tunnels. */
1201 llist_for_each_entry(tun, &hub->tunnels, entry) {
1202 struct gtphub_tunnel_endpoint *te =
1203 &tun->endpoint[side_idx][plane_idx];
1204 if (((!tei_orig) || (te->tei_orig == tei_orig))
1205 && ((!tei_repl) || (te->tei_repl == tei_repl))
1206 && gsn_addr_same(&te->peer->peer_addr->addr, &from->peer_addr->addr))
1207 return tun;
1208 }
1209 return NULL;
1210}
1211
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001212static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1213 struct gtphub_tunnel_endpoint *iterated_te,
1214 uint32_t *tei_min,
1215 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001216{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001217 if (new_te->tei_repl != iterated_te->tei_repl)
1218 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001219
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001220 /* new_te->tei_repl is already taken. Try to find one out of the known
1221 * range. */
1222
1223 if ((*tei_max) < 0xffffffff) {
1224 (*tei_max)++;
1225 new_te->tei_repl = *tei_max;
1226 return 1;
1227 } else if ((*tei_min) > 1) {
1228 (*tei_min)--;
1229 new_te->tei_repl = *tei_min;
1230 return 1;
1231 }
1232
1233 /* None seems to be available. */
1234 return 0;
1235}
1236
1237static int gtphub_check_reused_teis(struct gtphub *hub,
1238 struct gtphub_tunnel *new_tun)
1239{
1240 uint32_t tei_min = 0xffffffff;
1241 uint32_t tei_max = 0;
1242 int side_idx;
1243 int plane_idx;
1244 int side_idx2;
1245 int plane_idx2;
1246 struct gtphub_tunnel_endpoint *te;
1247 struct gtphub_tunnel_endpoint *te2;
1248
1249 struct gtphub_tunnel *tun, *ntun;
1250
1251 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1252 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001253 continue;
1254
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001255 /* Check whether the GSN sent a TEI that it is reusing from a
1256 * previous tunnel. */
1257 for_each_side_and_plane(side_idx, plane_idx) {
1258 te = &tun->endpoint[side_idx][plane_idx];
1259 te2 = &new_tun->endpoint[side_idx][plane_idx];
1260 if ((te->tei_orig == te2->tei_orig)
1261 && gsn_addr_same(&te->peer->peer_addr->addr,
1262 &te2->peer->peer_addr->addr)) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001263
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001264 /* The peer is reusing a TEI that I believe to
1265 * be part of another tunnel. The other tunnel
1266 * must be stale, then. */
1267 LOG(LOGL_NOTICE,
1268 "Expiring tunnel due to reused TEI:"
1269 " peer %s sent %s TEI %x,"
1270 " previously used by tunnel %s...\n",
1271 gtphub_port_str(te->peer),
1272 gtphub_plane_idx_names[plane_idx],
1273 te->tei_orig,
1274 gtphub_tunnel_str(tun));
1275 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1276 gtphub_tunnel_str(new_tun));
1277 expiring_item_del(&tun->expiry_entry);
1278 /* continue to find more matches. There shouldn't be
1279 * any, but let's make sure. */
1280 }
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001281 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001282
1283 /* Check whether the mapped TEIs assigned to the endpoints are
1284 * used anywhere else. */
1285 for_each_side_and_plane(side_idx, plane_idx) {
1286 te = &tun->endpoint[side_idx][plane_idx];
1287 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1288 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1289
1290 for_each_side_and_plane(side_idx2, plane_idx2) {
1291 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1292 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1293 LOG(LOGL_ERROR,
1294 "No mapped TEI is readily available."
1295 " Searching for holes between occupied"
1296 " TEIs not implemented.");
1297 return 0;
1298 }
1299 }
1300 }
1301
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001302 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001303
1304 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001305}
1306
1307static void gtphub_tunnel_refresh(struct gtphub *hub,
1308 struct gtphub_tunnel *tun,
1309 time_t now)
1310{
1311 expiry_add(&hub->expire_slowly,
1312 &tun->expiry_entry,
1313 now);
1314}
1315
1316static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1317 struct gtp_packet_desc *p,
1318 struct gtphub_peer_port *from)
1319{
1320 OSMO_ASSERT(from);
1321 int other_side = other_side_idx(p->side_idx);
1322
1323 struct gtphub_tunnel *tun;
1324 llist_for_each_entry(tun, &hub->tunnels, entry) {
1325 struct gtphub_tunnel_endpoint *te_from =
1326 &tun->endpoint[p->side_idx][p->plane_idx];
1327 struct gtphub_tunnel_endpoint *te_to =
1328 &tun->endpoint[other_side][p->plane_idx];
1329 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001330 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001331 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1332 &from->peer_addr->addr)) {
1333 gtphub_tunnel_refresh(hub, tun, p->timestamp);
1334 return te_to;
1335 }
1336 }
1337 return NULL;
1338}
1339
1340
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001341static void gtphub_check_restart_counter(struct gtphub *hub,
1342 struct gtp_packet_desc *p,
1343 struct gtphub_peer_port *from)
1344{
1345 /* TODO */
1346 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1347 * that doesn't match the peer's previously sent restart counter, clear
1348 * that peer and cancel PDP contexts. */
1349}
1350
1351static void gtphub_map_restart_counter(struct gtphub *hub,
1352 struct gtp_packet_desc *p,
1353 struct gtphub_peer_port *from,
1354 struct gtphub_peer_port *to)
1355{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001356 /* Always send gtphub's own restart counter */
1357 if (p->rc != GTP_RC_PDU_C)
1358 return;
1359
1360 int ie_idx;
1361 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1362 if (ie_idx < 0)
1363 return;
1364
1365 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001366}
1367
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001368static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1369 struct gtphub *hub,
1370 struct gtp_packet_desc *p,
1371 struct gtphub_peer_port *from_port)
1372{
1373 OSMO_ASSERT(p->version == 1);
1374 *to_port_p = NULL;
1375
1376 /* If the header's TEI is zero, no PDP context has been established
1377 * yet. If nonzero, a mapping should actually already exist for this
1378 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001379 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001380 return 0;
1381
1382 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001383 * mapped in a tunnel struct. */
1384 struct gtphub_tunnel_endpoint *to;
1385 to = gtphub_unmap_tei(hub, p, from_port);
1386 if (!to) {
1387 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1388 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001389 return -1;
1390 }
1391
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001392 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393 set_tei(p, unmapped_tei);
1394
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001395 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %" PRIx32 " -> %" PRIx32 " (to %s)\n",
1396 gtphub_port_str(from_port), p->header_tei_rx, unmapped_tei,
1397 gtphub_port_str2(to->peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001398
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001399 *to_port_p = to->peer;
1400
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001401 return 0;
1402}
1403
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001404static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1405 struct gtp_packet_desc *p,
1406 struct gtphub_peer_port *from_ctrl,
1407 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001409 int plane_idx;
1410
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001411 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001412 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1413 plane_nrs_match_GSN_addr_IE_indices);
1414
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001415 struct gtphub_tunnel *tun = NULL;
1416
1417 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001418 if (p->side_idx != GTPH_SIDE_SGSN) {
1419 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1420 " Request from the GGSN side");
1421 return -1;
1422 }
1423
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001424 /* A new tunnel. */
1425 tun = gtphub_tunnel_new();
1426 llist_add(&tun->entry, &hub->tunnels);
1427 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001428 /* The endpoint peers on this side (SGSN) will be set from IEs
1429 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001430 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001431 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001432 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001433 if (p->side_idx != GTPH_SIDE_GGSN) {
1434 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1435 " Response from the SGSN side");
1436 return -1;
1437 }
1438
1439 /* Find the tunnel created during request. This is coming from
1440 * the GGSN side, so to_ctrl corresponds to the SGSN. */
1441 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001442 tun = gtphub_tun_find(hub, other_side_idx(p->side_idx),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001443 p->plane_idx, to_ctrl, 0, p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001444
1445 if (!tun) {
1446 LOG(LOGL_ERROR, "Create PDP Context Response: cannot"
1447 " find matching request from SGSN %s, mapped TEI %x.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001448 gtphub_port_str(to_ctrl), p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001449 return -1;
1450 }
1451 }
1452
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001453 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1454 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001455 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001456
1457 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001458 int rc;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001459 struct gsn_addr addr_from_ie;
1460 uint32_t tei_from_ie;
1461 int ie_idx;
1462
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001463 /* Fetch GSN Address and TEI from IEs. As ensured by above
1464 * static asserts, plane_idx corresponds to the GSN Address IE
1465 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001466 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1467 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001468 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1469 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001470 return -1;
1471 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001472 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001473 gtphub_plane_idx_names[plane_idx],
1474 gsn_addr_to_str(&addr_from_ie),
1475 addr_from_ie.len);
1476
1477 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1478 if (ie_idx < 0) {
1479 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001480 LOG(LOGL_ERROR,
1481 "Create PDP Context message invalid:"
1482 " missing IE %d\n",
1483 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001484 return -1;
1485 }
1486 tei_from_ie = 0;
1487 }
1488 else
1489 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1490
1491 /* Make sure an entry for this peer address with default port
1492 * exists */
1493 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001494 gtphub_port_have(hub, &hub->to_gsns[side_idx][plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001495 &addr_from_ie,
1496 gtphub_plane_idx_default_port[plane_idx]);
1497
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001498 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1499 peer_from_ie);
1500
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001501 if (tei_from_ie) {
1502 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001503 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001504
1505 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1506 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001507 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001508
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001509 if (!gtphub_check_reused_teis(hub, tun)) {
1510 /* It's highly unlikely that all TEIs are
1511 * taken. But the code looking for an unused
1512 * TEI is, at the time of writing this comment,
1513 * not able to find gaps in the TEI space. To
1514 * explicitly alert the user of this problem,
1515 * rather abort than carry on. */
1516 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1517 abort();
1518 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001519 }
1520
1521 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001522 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1523 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001524 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001525 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1526 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001527 return -1;
1528 }
1529 }
1530
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001531 if (p->type == GTP_CREATE_PDP_REQ) {
1532 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1533 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001534 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001535 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1536 gtphub_tunnel_str(tun));
1537 }
1538
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001539 return 0;
1540}
1541
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001542static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1543 struct gtp_packet_desc *p,
1544 struct gtphub_peer_port *from_ctrl,
1545 struct gtphub_peer_port *to_ctrl)
1546{
1547 /* TODO */
1548 return 0;
1549}
1550
1551static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1552 struct gtp_packet_desc *p,
1553 struct gtphub_peer_port *from_ctrl,
1554 struct gtphub_peer_port *to_ctrl)
1555{
1556 /* TODO */
1557 return 0;
1558}
1559
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001560/* Read GSN address IEs from p, and make sure these peer addresses exist in
1561 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1562 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1563 * the packet p. */
1564static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1565 struct gtp_packet_desc *p,
1566 struct gtphub_peer_port *from_ctrl,
1567 struct gtphub_peer_port *to_ctrl)
1568{
1569 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1570
1571 switch (p->type) {
1572 case GTP_CREATE_PDP_REQ:
1573 case GTP_CREATE_PDP_RSP:
1574 return gtphub_handle_create_pdp_ctx(hub, p,
1575 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001576
1577 case GTP_DELETE_PDP_REQ:
1578 case GTP_DELETE_PDP_RSP:
1579 return gtphub_handle_delete_pdp_ctx(hub, p,
1580 from_ctrl, to_ctrl);
1581
1582 case GTP_UPDATE_PDP_REQ:
1583 case GTP_UPDATE_PDP_RSP:
1584 return gtphub_handle_update_pdp_ctx(hub, p,
1585 from_ctrl, to_ctrl);
1586
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001587 default:
1588 /* Nothing to do for this message type. */
1589 return 0;
1590 }
1591
1592}
1593
1594
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001595static int gtphub_write(const struct osmo_fd *to,
1596 const struct osmo_sockaddr *to_addr,
1597 const uint8_t *buf, size_t buf_len)
1598{
1599 errno = 0;
1600 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1601 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001602 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001603
1604 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001605 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001606 return -EINVAL;
1607 }
1608
1609 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001610 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1611 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001612 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001613 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1614 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001615
1616 return 0;
1617}
1618
1619static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1620{
1621 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1622 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001623 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1624 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001625 if (!(what & BSC_FD_READ))
1626 return 0;
1627
1628 struct gtphub *hub = from_ggsns_ofd->data;
1629
1630 static uint8_t buf[4096];
1631 struct osmo_sockaddr from_addr;
1632 struct osmo_sockaddr to_addr;
1633 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001634 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001635 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001636
1637 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1638 if (len < 1)
1639 return 0;
1640
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001641 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1642 buf, len, gtphub_now(),
1643 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001644 if (len < 1)
1645 return 0;
1646
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001647 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001648}
1649
1650static int gtphub_unmap(struct gtphub *hub,
1651 struct gtp_packet_desc *p,
1652 struct gtphub_peer_port *from,
1653 struct gtphub_peer_port *to_proxy,
1654 struct gtphub_peer_port **final_unmapped,
1655 struct gtphub_peer_port **unmapped_from_seq,
1656 struct gtphub_peer_port **unmapped_from_tei)
1657{
1658 /* Always (try to) unmap sequence and TEI numbers, which need to be
1659 * replaced in the packet. Either way, give precedence to the proxy, if
1660 * configured. */
1661
1662 struct gtphub_peer_port *from_seq = NULL;
1663 struct gtphub_peer_port *from_tei = NULL;
1664 struct gtphub_peer_port *unmapped = NULL;
1665
1666 if (unmapped_from_seq)
1667 *unmapped_from_seq = from_seq;
1668 if (unmapped_from_tei)
1669 *unmapped_from_tei = from_tei;
1670 if (final_unmapped)
1671 *final_unmapped = unmapped;
1672
1673 from_seq = gtphub_unmap_seq(p, from);
1674
1675 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1676 return -1;
1677
1678 struct gtphub_peer *from_peer = from->peer_addr->peer;
1679 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001680 LOG(LOGL_DEBUG,
1681 "Seq unmap and TEI unmap yield two different peers."
1682 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001683 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1684 gtphub_plane_idx_names[p->plane_idx],
1685 gtphub_peer_str(from_peer),
1686 (int)p->seq,
1687 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001688 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001689 gtphub_port_str2(from_tei)
1690 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001691 }
1692 unmapped = (from_seq? from_seq : from_tei);
1693
1694 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001695 LOG(LOGL_NOTICE,
1696 "Unmap yields a different peer than the configured proxy."
1697 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001698 " unmapped: %s proxy: %s\n",
1699 gtphub_port_str(unmapped),
1700 gtphub_port_str2(to_proxy)
1701 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001702 }
1703 unmapped = (to_proxy? to_proxy : unmapped);
1704
1705 if (!unmapped) {
1706 /* Return no error, but returned pointers are all NULL. */
1707 return 0;
1708 }
1709
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001710 if (unmapped_from_seq)
1711 *unmapped_from_seq = from_seq;
1712 if (unmapped_from_tei)
1713 *unmapped_from_tei = from_tei;
1714 if (final_unmapped)
1715 *final_unmapped = unmapped;
1716 return 0;
1717}
1718
1719static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1720 uint16_t port,
1721 struct osmo_sockaddr *dst)
1722{
1723 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1724}
1725
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001726/* If p is an Echo request, replace p's data with the matching response and
1727 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1728 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001729static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1730 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001731{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001732 if (p->type != GTP_ECHO_REQ)
1733 return 0;
1734
1735 static uint8_t echo_response_data[14] = {
1736 0x32, /* flags */
1737 GTP_ECHO_RSP,
1738 0x00, 14 - 8, /* Length in network byte order */
1739 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1740 0, 0, /* Seq, to be replaced */
1741 0, 0, /* no extensions */
1742 0x0e, /* Recovery IE */
1743 0 /* Recovery counter, to be replaced */
1744 };
1745 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1746 uint8_t *recovery = &echo_response_data[13];
1747
1748 *seq = hton16(p->seq);
1749 *recovery = hub->restart_counter;
1750
1751 *reply_buf = echo_response_data;
1752
1753 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001754}
1755
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001756struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1757 const struct osmo_sockaddr *addr);
1758
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001759/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1760 * address to forward to. Return a pointer to the osmo_fd, but copy the
1761 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1762 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1763 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001764int gtphub_handle_buf(struct gtphub *hub,
1765 unsigned int side_idx,
1766 unsigned int plane_idx,
1767 const struct osmo_sockaddr *from_addr,
1768 uint8_t *buf,
1769 size_t received,
1770 time_t now,
1771 uint8_t **reply_buf,
1772 struct osmo_fd **to_ofd,
1773 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001774{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001775 /*
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001776 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1777 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001778 */
1779 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
1780 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001781
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001782 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1783 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001784 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
1785 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001786 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001787 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01001788 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001789
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001790 static struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001791 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001792
1793 if (p.rc <= 0)
1794 return -1;
1795
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001796 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1797
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001798 int reply_len;
1799 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1800 if (reply_len > 0) {
1801 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001802 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001803 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001804
1805 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1806 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1807 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001808 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
1809 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
1810 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001811 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001812 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001813 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001814 if (reply_len < 0)
1815 return -1;
1816
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001817 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001818
1819 /* If a GGSN proxy is configured, check that it's indeed that proxy
1820 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1821 * gtphub, so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001822 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
1823 if (from_peer) {
1824 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001825 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001826 "Rejecting: %s proxy configured, but GTP packet"
1827 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001828 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001829 gtphub_side_idx_names[side_idx],
1830 gtphub_side_idx_names[side_idx],
1831 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001832 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001833 return -1;
1834 }
1835 }
1836
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001837 if (!from_peer) {
1838 /* Find or create a peer with a matching address. The sender's
1839 * port may in fact differ. */
1840 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001841 }
1842
1843 /* If any PDP context has been created, we already have an entry for
1844 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1845 * us about. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001846 if (!from_peer) {
1847 if (side_idx == GTPH_SIDE_GGSN) {
1848 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1849 osmo_sockaddr_to_str(from_addr));
1850 return -1;
1851 } else {
1852 /* SGSN */
1853 /* A new peer. If this is on the Ctrl plane, an SGSN
1854 * may make first contact without being known yet, so
1855 * create the peer struct for the current sender. */
1856 if (plane_idx != GTPH_PLANE_CTRL) {
1857 LOG(LOGL_ERROR,
1858 "Dropping packet: User plane peer was not"
1859 "announced by PDP Context: %s\n",
1860 osmo_sockaddr_to_str(from_addr));
1861 return -1;
1862 }
1863
1864 struct gsn_addr from_gsna;
1865 uint16_t from_port;
1866 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1867 return -1;
1868
1869 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
1870 }
1871 }
1872
1873 if (!from_peer) {
1874 /* This could theoretically happen for invalid address data or
1875 * somesuch. */
1876 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
1877 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001878 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001879 return -1;
1880 }
1881
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001882 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
1883 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001884
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001885 struct gtphub_peer_port *to_peer_from_seq;
1886 struct gtphub_peer_port *to_peer;
1887 if (gtphub_unmap(hub, &p, from_peer,
1888 hub->proxy[other_side_idx(side_idx)][plane_idx],
1889 &to_peer, &to_peer_from_seq,
1890 NULL /* not interested, got it in &to_peer already */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001891 )
1892 != 0) {
1893 return -1;
1894 }
1895
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001896 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
1897 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
1898 return -1;
1899 }
1900
1901 if (!to_peer) {
1902 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
1903 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001904 return -1;
1905 }
1906
1907 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001908 /* This may be a Create PDP Context response. If it is, there
1909 * are other addresses in the GTP message to set up apart from
1910 * the sender. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001911 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001912 != 0)
1913 return -1;
1914 }
1915
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001916 gtphub_check_restart_counter(hub, &p, from_peer);
1917 gtphub_map_restart_counter(hub, &p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001918
1919 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001920 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001921 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001922 if (!to_peer_from_seq)
1923 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001924
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001925 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001926
1927 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001928
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001929 if (received) {
1930 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1931 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1932 received);
1933 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001934 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
1935 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
1936 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01001937 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001938 return received;
1939}
1940
1941static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1942{
1943 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1944 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001945 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1946 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001947
1948 if (!(what & BSC_FD_READ))
1949 return 0;
1950
1951 struct gtphub *hub = from_sgsns_ofd->data;
1952
1953 static uint8_t buf[4096];
1954 struct osmo_sockaddr from_addr;
1955 struct osmo_sockaddr to_addr;
1956 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001957 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001958 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001959
1960 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1961 if (len < 1)
1962 return 0;
1963
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001964 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1965 buf, len, gtphub_now(),
1966 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001967 if (len < 1)
1968 return 0;
1969
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001970 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001971}
1972
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001973static void resolved_gssn_del_cb(struct expiring_item *expi)
1974{
1975 struct gtphub_resolved_ggsn *ggsn;
1976 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1977
1978 gtphub_port_ref_count_dec(ggsn->peer);
1979 llist_del(&ggsn->entry);
1980
1981 ggsn->expiry_entry.del_cb = 0;
1982 expiring_item_del(&ggsn->expiry_entry);
1983
1984 talloc_free(ggsn);
1985}
1986
1987void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1988 struct gsn_addr *resolved_addr,
1989 time_t now)
1990{
1991 struct gtphub_peer_port *pp;
1992 struct gtphub_resolved_ggsn *ggsn;
1993
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001994 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001995 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1996 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001997
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001998 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001999 resolved_addr, 2123);
2000 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002001 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2002 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002003 return;
2004 }
2005
2006 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2007 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002008 INIT_LLIST_HEAD(&ggsn->entry);
2009 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002010
2011 ggsn->peer = pp;
2012 gtphub_port_ref_count_inc(pp);
2013
2014 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002015 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002016
2017 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002018 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002019
2020 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2021}
2022
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002023static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2024{
2025 return pp->ref_count == 0;
2026}
2027
2028static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2029{
2030 struct gtphub_peer_port *pp, *npp;
2031 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2032 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002033 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002034 gtphub_port_str(pp));
2035 gtphub_peer_port_del(pp);
2036 }
2037 }
2038 return llist_empty(&pa->ports);
2039}
2040
2041static int gtphub_gc_peer(struct gtphub_peer *p)
2042{
2043 struct gtphub_peer_addr *pa, *npa;
2044 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2045 if (gtphub_gc_peer_addr(pa)) {
2046 gtphub_peer_addr_del(pa);
2047 }
2048 }
2049
2050 /* Note that there's a ref_count in each gtphub_peer_port instance
2051 * listed within p->addresses, referenced by TEI mappings from
2052 * hub->tei_map. As long as those don't expire, this peer will stay. */
2053
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002054 return llist_empty(&p->addresses)
2055 && nr_map_empty(&p->seq_map);
2056}
2057
2058static void gtphub_gc_bind(struct gtphub_bind *b)
2059{
2060 struct gtphub_peer *p, *n;
2061 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2062 if (gtphub_gc_peer(p)) {
2063 gtphub_peer_del(p);
2064 }
2065 }
2066}
2067
2068void gtphub_gc(struct gtphub *hub, time_t now)
2069{
2070 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002071 expired = expiry_tick(&hub->expire_quickly, now);
2072 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002073
2074 /* ... */
2075
2076 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002077 int s, p;
2078 for_each_side_and_plane(s, p) {
2079 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002080 }
2081 }
2082}
2083
2084static void gtphub_gc_cb(void *data)
2085{
2086 struct gtphub *hub = data;
2087 gtphub_gc(hub, gtphub_now());
2088 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2089}
2090
2091static void gtphub_gc_start(struct gtphub *hub)
2092{
2093 hub->gc_timer.cb = gtphub_gc_cb;
2094 hub->gc_timer.data = hub;
2095
2096 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2097}
2098
2099/* called by unit tests */
2100void gtphub_init(struct gtphub *hub)
2101{
2102 gtphub_zero(hub);
2103
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002104 INIT_LLIST_HEAD(&hub->tunnels);
2105
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002106 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2107 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002108
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002109 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2110
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002111 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002112 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002113 for_each_side_and_plane(side_idx, plane_idx) {
2114 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002115 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002116
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002117 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2118 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2119 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2120 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002121}
2122
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002123/* For the test suite, this is kept separate from gtphub_stop(), which also
2124 * closes sockets. The test suite avoids using sockets and would cause
2125 * segfaults when trying to close uninitialized ofds. */
2126void gtphub_free(struct gtphub *hub)
2127{
2128 /* By expiring all mappings, a garbage collection should free
2129 * everything else. A gtphub_bind_free() will assert that everything is
2130 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002131 expiry_clear(&hub->expire_quickly);
2132 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002133
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002134 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002135 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002136 for_each_side_and_plane(side_idx, plane_idx) {
2137 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2138 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002139 }
2140}
2141
2142void gtphub_stop(struct gtphub *hub)
2143{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002144 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002145 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002146 for_each_side_and_plane(side_idx, plane_idx) {
2147 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002148 }
2149 gtphub_free(hub);
2150}
2151
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002152static int gtphub_make_proxy(struct gtphub *hub,
2153 struct gtphub_peer_port **pp,
2154 struct gtphub_bind *bind,
2155 const struct gtphub_cfg_addr *addr)
2156{
2157 if (!addr->addr_str)
2158 return 0;
2159
2160 struct gsn_addr gsna;
2161 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2162 return -1;
2163
2164 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2165
2166 /* This is *the* proxy. Make sure it is never expired. */
2167 gtphub_port_ref_count_inc(*pp);
2168 return 0;
2169}
2170
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002171int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2172 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002174 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002175
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002176 hub->restart_counter = restart_counter;
2177
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002178 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002179 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002180 if (gtphub_ares_init(hub) != 0) {
2181 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2182 return -1;
2183 }
2184 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002185
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002186 /* TODO set hub->restart_counter from external file. */
2187
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002188 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002189 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002190 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002191 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002192 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2193 &cfg->to_gsns[side_idx][plane_idx],
2194 (side_idx == GTPH_SIDE_SGSN)
2195 ? from_sgsns_read_cb
2196 : from_ggsns_read_cb,
2197 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002198 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002199 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2200 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002201 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002202 return rc;
2203 }
2204 }
2205
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002206 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002207 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002208 &hub->proxy[side_idx][plane_idx],
2209 &hub->to_gsns[side_idx][plane_idx],
2210 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002211 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002212 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002213 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002214 gtphub_side_idx_names[side_idx],
2215 cfg->proxy[side_idx][plane_idx].addr_str,
2216 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002217 return -1;
2218 }
2219 }
2220
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002221 for_each_side_and_plane(side_idx, plane_idx) {
2222 if (hub->proxy[side_idx][plane_idx])
2223 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2224 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002225 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002226 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002227 }
2228
2229 gtphub_gc_start(hub);
2230 return 0;
2231}
2232
2233static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2234 const struct gsn_addr *addr)
2235{
2236 struct gtphub_peer_addr *a;
2237 llist_for_each_entry(a, &peer->addresses, entry) {
2238 if (gsn_addr_same(&a->addr, addr))
2239 return a;
2240 }
2241 return NULL;
2242}
2243
2244static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2245 uint16_t port)
2246{
2247 OSMO_ASSERT(port);
2248 struct gtphub_peer_port *pp;
2249 llist_for_each_entry(pp, &a->ports, entry) {
2250 if (pp->port == port)
2251 return pp;
2252 }
2253 return NULL;
2254}
2255
2256static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2257 const struct gsn_addr *addr)
2258{
2259 struct gtphub_peer *peer;
2260 llist_for_each_entry(peer, &bind->peers, entry) {
2261 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2262 if (a)
2263 return a;
2264 }
2265 return NULL;
2266}
2267
2268static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2269 const struct gsn_addr *addr,
2270 uint16_t port)
2271{
2272 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2273 if (!a)
2274 return NULL;
2275 return gtphub_addr_find_port(a, port);
2276}
2277
2278struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2279 const struct osmo_sockaddr *addr)
2280{
2281 struct gsn_addr gsna;
2282 uint16_t port;
2283 gsn_addr_from_sockaddr(&gsna, &port, addr);
2284 return gtphub_port_find(bind, &gsna, port);
2285}
2286
2287static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2288 struct gtphub_bind *bind)
2289{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002290 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2291 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002292 OSMO_ASSERT(peer);
2293
2294 INIT_LLIST_HEAD(&peer->addresses);
2295
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002296 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002297 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002298
2299 /* TODO use something random to pick the initial sequence nr.
2300 0x6d31 produces the ASCII character sequence 'm1', currently used in
2301 gtphub_nc_test.sh. */
2302 peer->seq_pool.last_nr = 0x6d31 - 1;
2303
2304 llist_add(&peer->entry, &bind->peers);
2305 return peer;
2306}
2307
2308static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2309 const struct gsn_addr *addr)
2310{
2311 struct gtphub_peer_addr *a;
2312 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2313 OSMO_ASSERT(a);
2314 a->peer = peer;
2315 gsn_addr_copy(&a->addr, addr);
2316 INIT_LLIST_HEAD(&a->ports);
2317 llist_add(&a->entry, &peer->addresses);
2318
2319 return a;
2320}
2321
2322static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2323 struct gtphub_bind *bind,
2324 const struct gsn_addr *addr)
2325{
2326 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2327 if (a)
2328 return a;
2329
2330 /* If we haven't found an address, that means we need to create an
2331 * entirely new peer for the new address. More addresses may be added
2332 * to this peer later, but not via this function. */
2333 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002334
2335 a = gtphub_peer_add_addr(peer, addr);
2336
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002337 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002338 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002339 gsn_addr_to_str(&a->addr));
2340
2341 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002342}
2343
2344static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2345 uint16_t port)
2346{
2347 struct gtphub_peer_port *pp;
2348
2349 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2350 OSMO_ASSERT(pp);
2351 pp->peer_addr = a;
2352 pp->port = port;
2353
2354 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2355 talloc_free(pp);
2356 return NULL;
2357 }
2358
2359 llist_add(&pp->entry, &a->ports);
2360
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002361 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002362 gsn_addr_to_str(&a->addr),
2363 (int)port);
2364
2365 return pp;
2366}
2367
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002368struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2369 struct gtphub_bind *bind,
2370 const struct gsn_addr *addr,
2371 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002372{
2373 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2374
2375 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2376 if (pp)
2377 return pp;
2378
2379 return gtphub_addr_add_port(a, port);
2380}
2381
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002382/* Find a GGSN peer with a matching address. If the address is known but the
2383 * port not, create a new port for that peer address. */
2384struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2385 const struct osmo_sockaddr *addr)
2386{
2387 struct gtphub_peer_addr *pa;
2388 struct gtphub_peer_port *pp;
2389
2390 struct gsn_addr gsna;
2391 uint16_t port;
2392 gsn_addr_from_sockaddr(&gsna, &port, addr);
2393
2394 pa = gtphub_addr_find(bind, &gsna);
2395 if (!pa)
2396 return NULL;
2397
2398 pp = gtphub_addr_find_port(pa, port);
2399
2400 if (!pp)
2401 pp = gtphub_addr_add_port(pa, port);
2402
2403 return pp;
2404}
2405
2406
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002407/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2408 * resolution should be possible but failed, and 1 if resolution was
2409 * successful. *pp will be set to NULL if <1 is returned. */
2410static int gtphub_resolve_ggsn(struct gtphub *hub,
2411 struct gtp_packet_desc *p,
2412 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002413{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002414 *pp = NULL;
2415
2416 /* TODO determine from message type whether IEs should be present? */
2417
2418 int rc;
2419 const char *imsi_str;
2420 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2421 if (rc < 1)
2422 return rc;
2423 OSMO_ASSERT(imsi_str);
2424
2425 const char *apn_str;
2426 rc = get_ie_apn_str(p->ie, &apn_str);
2427 if (rc < 1)
2428 return rc;
2429 OSMO_ASSERT(apn_str);
2430
2431 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2432 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002433}
2434
2435
2436/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002437/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002438/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2439 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002440static int _osmo_getaddrinfo(struct addrinfo **result,
2441 uint16_t family, uint16_t type, uint8_t proto,
2442 const char *host, uint16_t port)
2443{
2444 struct addrinfo hints;
2445 char portbuf[16];
2446
2447 sprintf(portbuf, "%u", port);
2448 memset(&hints, '\0', sizeof(struct addrinfo));
2449 hints.ai_family = family;
2450 if (type == SOCK_RAW) {
2451 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2452 * SOCK_RAW and IPPROTO_GRE is used.
2453 */
2454 hints.ai_socktype = SOCK_DGRAM;
2455 hints.ai_protocol = IPPROTO_UDP;
2456 } else {
2457 hints.ai_socktype = type;
2458 hints.ai_protocol = proto;
2459 }
2460
2461 return getaddrinfo(host, portbuf, &hints, result);
2462}
2463
2464/* TODO move to osmocom/core/socket.c ? */
2465int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2466 uint16_t family, uint16_t type, uint8_t proto,
2467 const char *host, uint16_t port)
2468{
2469 struct addrinfo *res;
2470 int rc;
2471 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2472
2473 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002474 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002475 return -EINVAL;
2476 }
2477
2478 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2479 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2480 addr->l = res->ai_addrlen;
2481 freeaddrinfo(res);
2482
2483 return 0;
2484}
2485
2486int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2487 char *port_str, size_t port_str_len,
2488 const struct osmo_sockaddr *addr,
2489 int flags)
2490{
2491 int rc;
2492
2493 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2494 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2495 return -1;
2496 }
2497
2498 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002499 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2500 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002501 return -1;
2502 }
2503
2504 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2505 addr_str, addr_str_len,
2506 port_str, port_str_len,
2507 flags);
2508
2509 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002510 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2511 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2512 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002513
2514 return rc;
2515}
2516
2517const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2518 char *buf, size_t buf_len)
2519{
2520 const int portbuf_len = 6;
2521 OSMO_ASSERT(buf_len > portbuf_len);
2522 char *portbuf = buf + buf_len - portbuf_len;
2523 buf_len -= portbuf_len;
2524 if (osmo_sockaddr_to_strs(buf, buf_len,
2525 portbuf, portbuf_len,
2526 addr,
2527 NI_NUMERICHOST | NI_NUMERICSERV))
2528 return NULL;
2529
2530 char *pos = buf + strnlen(buf, buf_len-1);
2531 size_t len = buf_len - (pos - buf);
2532
2533 snprintf(pos, len, " port %s", portbuf);
2534 buf[buf_len-1] = '\0';
2535
2536 return buf;
2537}
2538
2539const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2540{
2541 static char buf[256];
2542 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2543 if (! result)
2544 return "(invalid)";
2545 return result;
2546}
2547
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002548int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2549 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002550{
2551 if (a == b)
2552 return 0;
2553 if (!a)
2554 return -1;
2555 if (!b)
2556 return 1;
2557 if (a->l != b->l) {
2558 /* Lengths are not the same, but determine the order. Will
2559 * anyone ever sort a list by osmo_sockaddr though...? */
2560 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2561 if (cmp == 0) {
2562 if (a->l < b->l)
2563 return -1;
2564 else
2565 return 1;
2566 }
2567 return cmp;
2568 }
2569 return memcmp(&a->a, &b->a, a->l);
2570}
2571
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002572void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2573 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002574{
2575 OSMO_ASSERT(src->l <= sizeof(dst->a));
2576 memcpy(&dst->a, &src->a, src->l);
2577 dst->l = src->l;
2578}