blob: 6037d3bbea71cf85c59842b018e7200d3bbf518d [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;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100417 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200418
419 for (i = 0; i < 8; i++) {
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
749time_t gtphub_now(void)
750{
751 struct timespec now_tp;
752 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
753 return now_tp.tv_sec;
754}
755
756/* Remove a gtphub_peer from its list and free it. */
757static void gtphub_peer_del(struct gtphub_peer *peer)
758{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100759 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200760 nr_map_clear(&peer->seq_map);
761 llist_del(&peer->entry);
762 talloc_free(peer);
763}
764
765static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
766{
767 OSMO_ASSERT(llist_empty(&pa->ports));
768 llist_del(&pa->entry);
769 talloc_free(pa);
770}
771
772static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
773{
774 OSMO_ASSERT(pp->ref_count == 0);
775 llist_del(&pp->entry);
776 talloc_free(pp);
777}
778
779/* From the information in the gtp_packet_desc, return the address of a GGSN.
780 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100781static int gtphub_resolve_ggsn(struct gtphub *hub,
782 struct gtp_packet_desc *p,
783 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200784
785/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100786struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
787 const char *imsi_str,
788 const char *apn_ni_str);
789int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200790
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200791static void gtphub_zero(struct gtphub *hub)
792{
793 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100794 INIT_LLIST_HEAD(&hub->ggsn_lookups);
795 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200796}
797
798static int gtphub_sock_init(struct osmo_fd *ofd,
799 const struct gtphub_cfg_addr *addr,
800 osmo_fd_cb_t cb,
801 void *data,
802 int ofd_id)
803{
804 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100805 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806 return -1;
807 }
808 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100809 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200810 return -1;
811 }
812
813 ofd->when = BSC_FD_READ;
814 ofd->cb = cb;
815 ofd->data = data;
816 ofd->priv_nr = ofd_id;
817
818 int rc;
819 rc = osmo_sock_init_ofd(ofd,
820 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
821 addr->addr_str, addr->port,
822 OSMO_SOCK_F_BIND);
823 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100824 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
825 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200826 return -1;
827 }
828
829 return 0;
830}
831
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100832static void gtphub_sock_close(struct osmo_fd *ofd)
833{
834 close(ofd->fd);
835 osmo_fd_unregister(ofd);
836 ofd->cb = NULL;
837}
838
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200839static void gtphub_bind_init(struct gtphub_bind *b)
840{
841 ZERO_STRUCT(b);
842
843 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100844
845 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
846 &gtphub_ctrg_io_desc, 0);
847 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200848}
849
850static int gtphub_bind_start(struct gtphub_bind *b,
851 const struct gtphub_cfg_bind *cfg,
852 osmo_fd_cb_t cb, void *cb_data,
853 unsigned int ofd_id)
854{
855 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
856 return -1;
857 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
858 return -1;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100859 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200860 return 0;
861}
862
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100863static void gtphub_bind_free(struct gtphub_bind *b)
864{
865 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100866 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100867}
868
869static void gtphub_bind_stop(struct gtphub_bind *b) {
870 gtphub_sock_close(&b->ofd);
871 gtphub_bind_free(b);
872}
873
Neels Hofmeyr40348972015-11-17 12:22:28 +0100874/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200875 * Return the number of bytes read, zero on error. */
876static int gtphub_read(const struct osmo_fd *from,
877 struct osmo_sockaddr *from_addr,
878 uint8_t *buf, size_t buf_len)
879{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100880 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200881
Neels Hofmeyr40348972015-11-17 12:22:28 +0100882 /* recvfrom requires the available length set in *from_addr_len. */
883 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200884 errno = 0;
885 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100886 (struct sockaddr*)&from_addr->a,
887 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200888 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
889 * is not truncated. Then maybe reduce buf's size. */
890
891 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100892 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
893 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200894 return 0;
895 }
896
Neels Hofmeyr40348972015-11-17 12:22:28 +0100897 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
898 (int)received, osmo_sockaddr_to_str(from_addr),
899 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200900
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200901 return received;
902}
903
904inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
905{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100906 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200907 OSMO_ASSERT(pp->ref_count < UINT_MAX);
908 pp->ref_count++;
909}
910
911inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
912{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100913 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200914 OSMO_ASSERT(pp->ref_count > 0);
915 pp->ref_count--;
916}
917
918inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
919{
920 OSMO_ASSERT(p->version == 1);
921 p->data->gtp1l.h.seq = hton16(seq);
922 p->seq = seq;
923}
924
925inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
926{
927 OSMO_ASSERT(p->version == 1);
928 p->data->gtp1l.h.tei = hton32(tei);
929 p->header_tei = tei;
930}
931
932static void gtphub_mapping_del_cb(struct expiring_item *expi);
933
934static struct nr_mapping *gtphub_mapping_new()
935{
936 struct nr_mapping *nrm;
937 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
938 OSMO_ASSERT(nrm);
939
940 nr_mapping_init(nrm);
941 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
942 return nrm;
943}
944
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100945
946#define APPEND(args...) \
947 l = snprintf(pos, left, args); \
948 pos += l; \
949 left -= l
950
951static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
952 int side_idx)
953{
954 static char buf[256];
955 char *pos = buf;
956 int left = sizeof(buf);
957 int l;
958
959 struct gtphub_tunnel_endpoint *c, *u;
960 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
961 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
962
963 /* print both only if they differ. */
964 if (!c->peer) {
965 APPEND("(uninitialized)");
966 } else {
967 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
968 }
969
970 if (!u->peer) {
971 if (c->peer) {
972 APPEND(" / (uninitialized)");
973 }
974 } else if ((!c->peer)
975 || (!gsn_addr_same(&u->peer->peer_addr->addr,
976 &c->peer->peer_addr->addr))) {
977 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
978 }
979
980 APPEND(" (TEI C %x=%x / U %x=%x)",
981 c->tei_orig, c->tei_repl,
982 u->tei_orig, u->tei_repl);
983 return buf;
984}
985
986const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
987{
988 static char buf[512];
989 char *pos = buf;
990 int left = sizeof(buf);
991 int l;
992
993 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
994 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
995
996 return buf;
997}
998
999#undef APPEND
1000
1001void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1002 struct gtphub_peer_port *pp)
1003{
1004 if (te->peer)
1005 gtphub_port_ref_count_dec(te->peer);
1006 te->peer = pp;
1007 if (te->peer)
1008 gtphub_port_ref_count_inc(te->peer);
1009}
1010
1011int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1012{
1013 if (!tun)
1014 return 0;
1015 int side_idx;
1016 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001017 for_each_side_and_plane(side_idx, plane_idx) {
1018 struct gtphub_tunnel_endpoint *te =
1019 &tun->endpoint[side_idx][plane_idx];
1020 if (!(te->peer && te->tei_orig && te->tei_repl))
1021 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001022 }
1023 return 1;
1024}
1025
1026static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1027{
1028 struct gtphub_tunnel *tun = container_of(expi,
1029 struct gtphub_tunnel,
1030 expiry_entry);
1031 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1032
1033 llist_del(&tun->entry);
1034 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1035
1036 expi->del_cb = 0; /* avoid recursion loops */
1037 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1038
1039 int side_idx;
1040 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001041 for_each_side_and_plane(side_idx, plane_idx) {
1042 /* clear ref count */
1043 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1044 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001045 }
1046
1047 talloc_free(tun);
1048}
1049
1050static struct gtphub_tunnel *gtphub_tunnel_new()
1051{
1052 struct gtphub_tunnel *tun;
1053 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1054 OSMO_ASSERT(tun);
1055
1056 INIT_LLIST_HEAD(&tun->entry);
1057 expiring_item_init(&tun->expiry_entry);
1058
1059 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1060 return tun;
1061}
1062
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001063static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1064 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001065{
1066 if (llist_empty(&peer->addresses))
1067 return "(addressless)";
1068
1069 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1070 struct gtphub_peer_addr,
1071 entry);
1072 return gsn_addr_to_strb(&a->addr, buf, buflen);
1073}
1074
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001075static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1076 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001077{
1078 if (!port)
1079 return "(null port)";
1080
1081 snprintf(buf, buflen, "%s port %d",
1082 gsn_addr_to_str(&port->peer_addr->addr),
1083 (int)port->port);
1084 return buf;
1085}
1086
1087const char *gtphub_peer_str(struct gtphub_peer *peer)
1088{
1089 static char buf[256];
1090 return gtphub_peer_strb(peer, buf, sizeof(buf));
1091}
1092
1093const char *gtphub_peer_str2(struct gtphub_peer *peer)
1094{
1095 static char buf[256];
1096 return gtphub_peer_strb(peer, buf, sizeof(buf));
1097}
1098
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001099const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001100{
1101 static char buf[256];
1102 return gtphub_port_strb(port, buf, sizeof(buf));
1103}
1104
1105static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1106{
1107 static char buf[256];
1108 return gtphub_port_strb(port, buf, sizeof(buf));
1109}
1110
1111static void gtphub_mapping_del_cb(struct expiring_item *expi)
1112{
1113 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001114 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001115
1116 struct nr_mapping *nrm = container_of(expi,
1117 struct nr_mapping,
1118 expiry_entry);
1119 llist_del(&nrm->entry);
1120 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1121
1122 /* Just for log */
1123 struct gtphub_peer_port *from = nrm->origin;
1124 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001125 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001126 (int)nrm->expiry_entry.expiry,
1127 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001128 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001129
1130 gtphub_port_ref_count_dec(from);
1131
1132 talloc_free(nrm);
1133}
1134
1135static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1136 struct gtphub_peer_port *from,
1137 nr_t orig_nr,
1138 time_t now)
1139{
1140 struct nr_mapping *nrm;
1141
1142 nrm = nr_map_get(map, from, orig_nr);
1143
1144 if (!nrm) {
1145 nrm = gtphub_mapping_new();
1146 nrm->orig = orig_nr;
1147 nrm->origin = from;
1148 nr_map_add(map, nrm, now);
1149 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001150 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001151 gtphub_port_str(from),
1152 (int)(nrm->orig), (int)(nrm->repl));
1153 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001154 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001155 }
1156
1157 OSMO_ASSERT(nrm);
1158 return nrm;
1159}
1160
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001161static void gtphub_map_seq(struct gtp_packet_desc *p,
1162 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001163 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001164{
1165 /* Store a mapping in to_peer's map, so when we later receive a GTP
1166 * packet back from to_peer, the seq nr can be unmapped back to its
1167 * origin (from_peer here). */
1168 struct nr_mapping *nrm;
1169 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001170 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001171
1172 /* Change the GTP packet to yield the new, mapped seq nr */
1173 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001174}
1175
1176static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1177 struct gtphub_peer_port *responding_port)
1178{
1179 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001180 struct nr_mapping *nrm =
1181 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1182 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001183 if (!nrm)
1184 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001185 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1186 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001187 set_seq(p, nrm->orig);
1188 return nrm->origin;
1189}
1190
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001191static struct gtphub_tunnel *gtphub_tun_find(struct gtphub *hub,
1192 int side_idx,
1193 int plane_idx,
1194 struct gtphub_peer_port *from,
1195 uint32_t tei_orig,
1196 uint32_t tei_repl)
1197{
1198 OSMO_ASSERT(from);
1199
1200 struct gtphub_tunnel *tun;
1201 /* TODO: optimize: don't iterate *all* tunnels. */
1202 llist_for_each_entry(tun, &hub->tunnels, entry) {
1203 struct gtphub_tunnel_endpoint *te =
1204 &tun->endpoint[side_idx][plane_idx];
1205 if (((!tei_orig) || (te->tei_orig == tei_orig))
1206 && ((!tei_repl) || (te->tei_repl == tei_repl))
1207 && gsn_addr_same(&te->peer->peer_addr->addr, &from->peer_addr->addr))
1208 return tun;
1209 }
1210 return NULL;
1211}
1212
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001213static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1214 struct gtphub_tunnel_endpoint *iterated_te,
1215 uint32_t *tei_min,
1216 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001217{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001218 if (new_te->tei_repl != iterated_te->tei_repl)
1219 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001220
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001221 /* new_te->tei_repl is already taken. Try to find one out of the known
1222 * range. */
1223
1224 if ((*tei_max) < 0xffffffff) {
1225 (*tei_max)++;
1226 new_te->tei_repl = *tei_max;
1227 return 1;
1228 } else if ((*tei_min) > 1) {
1229 (*tei_min)--;
1230 new_te->tei_repl = *tei_min;
1231 return 1;
1232 }
1233
1234 /* None seems to be available. */
1235 return 0;
1236}
1237
1238static int gtphub_check_reused_teis(struct gtphub *hub,
1239 struct gtphub_tunnel *new_tun)
1240{
1241 uint32_t tei_min = 0xffffffff;
1242 uint32_t tei_max = 0;
1243 int side_idx;
1244 int plane_idx;
1245 int side_idx2;
1246 int plane_idx2;
1247 struct gtphub_tunnel_endpoint *te;
1248 struct gtphub_tunnel_endpoint *te2;
1249
1250 struct gtphub_tunnel *tun, *ntun;
1251
1252 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1253 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001254 continue;
1255
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001256 /* Check whether the GSN sent a TEI that it is reusing from a
1257 * previous tunnel. */
1258 for_each_side_and_plane(side_idx, plane_idx) {
1259 te = &tun->endpoint[side_idx][plane_idx];
1260 te2 = &new_tun->endpoint[side_idx][plane_idx];
1261 if ((te->tei_orig == te2->tei_orig)
1262 && gsn_addr_same(&te->peer->peer_addr->addr,
1263 &te2->peer->peer_addr->addr)) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001264
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001265 /* The peer is reusing a TEI that I believe to
1266 * be part of another tunnel. The other tunnel
1267 * must be stale, then. */
1268 LOG(LOGL_NOTICE,
1269 "Expiring tunnel due to reused TEI:"
1270 " peer %s sent %s TEI %x,"
1271 " previously used by tunnel %s...\n",
1272 gtphub_port_str(te->peer),
1273 gtphub_plane_idx_names[plane_idx],
1274 te->tei_orig,
1275 gtphub_tunnel_str(tun));
1276 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1277 gtphub_tunnel_str(new_tun));
1278 expiring_item_del(&tun->expiry_entry);
1279 /* continue to find more matches. There shouldn't be
1280 * any, but let's make sure. */
1281 }
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001282 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001283
1284 /* Check whether the mapped TEIs assigned to the endpoints are
1285 * used anywhere else. */
1286 for_each_side_and_plane(side_idx, plane_idx) {
1287 te = &tun->endpoint[side_idx][plane_idx];
1288 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1289 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1290
1291 for_each_side_and_plane(side_idx2, plane_idx2) {
1292 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1293 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1294 LOG(LOGL_ERROR,
1295 "No mapped TEI is readily available."
1296 " Searching for holes between occupied"
1297 " TEIs not implemented.");
1298 return 0;
1299 }
1300 }
1301 }
1302
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001303 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001304
1305 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001306}
1307
1308static void gtphub_tunnel_refresh(struct gtphub *hub,
1309 struct gtphub_tunnel *tun,
1310 time_t now)
1311{
1312 expiry_add(&hub->expire_slowly,
1313 &tun->expiry_entry,
1314 now);
1315}
1316
1317static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1318 struct gtp_packet_desc *p,
1319 struct gtphub_peer_port *from)
1320{
1321 OSMO_ASSERT(from);
1322 int other_side = other_side_idx(p->side_idx);
1323
1324 struct gtphub_tunnel *tun;
1325 llist_for_each_entry(tun, &hub->tunnels, entry) {
1326 struct gtphub_tunnel_endpoint *te_from =
1327 &tun->endpoint[p->side_idx][p->plane_idx];
1328 struct gtphub_tunnel_endpoint *te_to =
1329 &tun->endpoint[other_side][p->plane_idx];
1330 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001331 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001332 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1333 &from->peer_addr->addr)) {
1334 gtphub_tunnel_refresh(hub, tun, p->timestamp);
1335 return te_to;
1336 }
1337 }
1338 return NULL;
1339}
1340
1341
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001342static void gtphub_check_restart_counter(struct gtphub *hub,
1343 struct gtp_packet_desc *p,
1344 struct gtphub_peer_port *from)
1345{
1346 /* TODO */
1347 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1348 * that doesn't match the peer's previously sent restart counter, clear
1349 * that peer and cancel PDP contexts. */
1350}
1351
1352static void gtphub_map_restart_counter(struct gtphub *hub,
1353 struct gtp_packet_desc *p,
1354 struct gtphub_peer_port *from,
1355 struct gtphub_peer_port *to)
1356{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001357 /* Always send gtphub's own restart counter */
1358 if (p->rc != GTP_RC_PDU_C)
1359 return;
1360
1361 int ie_idx;
1362 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1363 if (ie_idx < 0)
1364 return;
1365
1366 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001367}
1368
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001369static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1370 struct gtphub *hub,
1371 struct gtp_packet_desc *p,
1372 struct gtphub_peer_port *from_port)
1373{
1374 OSMO_ASSERT(p->version == 1);
1375 *to_port_p = NULL;
1376
1377 /* If the header's TEI is zero, no PDP context has been established
1378 * yet. If nonzero, a mapping should actually already exist for this
1379 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001380 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001381 return 0;
1382
1383 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001384 * mapped in a tunnel struct. */
1385 struct gtphub_tunnel_endpoint *to;
1386 to = gtphub_unmap_tei(hub, p, from_port);
1387 if (!to) {
1388 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1389 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001390 return -1;
1391 }
1392
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001393 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001394 set_tei(p, unmapped_tei);
1395
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001396 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %" PRIx32 " -> %" PRIx32 " (to %s)\n",
1397 gtphub_port_str(from_port), p->header_tei_rx, unmapped_tei,
1398 gtphub_port_str2(to->peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001399
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001400 *to_port_p = to->peer;
1401
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001402 return 0;
1403}
1404
1405/* Read GSN address IEs from p, and make sure these peer addresses exist in
1406 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1407 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1408 * the packet p. */
1409static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001410 struct gtphub_bind from_bind_arr[],
1411 struct gtphub_bind to_bind_arr[],
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001412 struct gtphub_peer_port *sgsn_ctrl,
1413 struct gtphub_peer_port *ggsn_ctrl,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001414 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001415{
1416 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1417
1418 int rc;
1419 int plane_idx;
1420
1421 switch (p->type) {
1422 case GTP_CREATE_PDP_REQ:
1423 case GTP_CREATE_PDP_RSP:
1424 /* Go for it below */
1425 break;
1426 default:
1427 /* Nothing to do for this message type. */
1428 return 0;
1429 }
1430
1431 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1432
1433 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1434 plane_nrs_match_GSN_addr_IE_indices);
1435
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001436 struct gtphub_tunnel *tun = NULL;
1437
1438 if (p->type == GTP_CREATE_PDP_REQ) {
1439 /* A new tunnel. */
1440 tun = gtphub_tunnel_new();
1441 llist_add(&tun->entry, &hub->tunnels);
1442 gtphub_tunnel_refresh(hub, tun, p->timestamp);
1443 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
1444 ggsn_ctrl);
1445 } else if (p->type == GTP_CREATE_PDP_RSP) {
1446 /* Find the tunnel created during request */
1447 OSMO_ASSERT(sgsn_ctrl);
1448 tun = gtphub_tun_find(hub, other_side_idx(p->side_idx),
1449 p->plane_idx, sgsn_ctrl, 0, p->header_tei_rx);
1450
1451 if (!tun) {
1452 LOG(LOGL_ERROR, "Create PDP Context Response: cannot"
1453 " find matching request from SGSN %s, mapped TEI %x.\n",
1454 gtphub_port_str(sgsn_ctrl), p->header_tei_rx);
1455 return -1;
1456 }
1457 }
1458
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001459 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1460 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001461 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001462
1463 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1464 struct gsn_addr addr_from_ie;
1465 uint32_t tei_from_ie;
1466 int ie_idx;
1467
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001468 /* Fetch GSN Address and TEI from IEs. As ensured by above
1469 * static asserts, plane_idx corresponds to the GSN Address IE
1470 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001471 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1472 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001473 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1474 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001475 return -1;
1476 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001477 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001478 gtphub_plane_idx_names[plane_idx],
1479 gsn_addr_to_str(&addr_from_ie),
1480 addr_from_ie.len);
1481
1482 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1483 if (ie_idx < 0) {
1484 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001485 LOG(LOGL_ERROR,
1486 "Create PDP Context message invalid:"
1487 " missing IE %d\n",
1488 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001489 return -1;
1490 }
1491 tei_from_ie = 0;
1492 }
1493 else
1494 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1495
1496 /* Make sure an entry for this peer address with default port
1497 * exists */
1498 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001499 gtphub_port_have(hub, &from_bind_arr[plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001500 &addr_from_ie,
1501 gtphub_plane_idx_default_port[plane_idx]);
1502
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001503 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1504 peer_from_ie);
1505
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001506 if (tei_from_ie) {
1507 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001508 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001509
1510 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1511 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001512 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001513
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001514 if (!gtphub_check_reused_teis(hub, tun))
1515 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001516 }
1517
1518 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001519 rc = gsn_addr_put(&to_bind_arr[plane_idx].local_addr, p,
1520 plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001521 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001522 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1523 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001524 return -1;
1525 }
1526 }
1527
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001528 if (p->type == GTP_CREATE_PDP_REQ) {
1529 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1530 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001531 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001532 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1533 gtphub_tunnel_str(tun));
1534 }
1535
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001536 return 0;
1537}
1538
1539static int gtphub_write(const struct osmo_fd *to,
1540 const struct osmo_sockaddr *to_addr,
1541 const uint8_t *buf, size_t buf_len)
1542{
1543 errno = 0;
1544 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1545 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001546 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001547
1548 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001549 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550 return -EINVAL;
1551 }
1552
1553 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001554 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1555 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001556 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001557 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1558 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001559
1560 return 0;
1561}
1562
1563static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1564{
1565 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1566 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001567 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1568 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001569 if (!(what & BSC_FD_READ))
1570 return 0;
1571
1572 struct gtphub *hub = from_ggsns_ofd->data;
1573
1574 static uint8_t buf[4096];
1575 struct osmo_sockaddr from_addr;
1576 struct osmo_sockaddr to_addr;
1577 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001578 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001579 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001580
1581 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1582 if (len < 1)
1583 return 0;
1584
1585 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1586 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001587 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001588 if (len < 1)
1589 return 0;
1590
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001591 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001592}
1593
1594static int gtphub_unmap(struct gtphub *hub,
1595 struct gtp_packet_desc *p,
1596 struct gtphub_peer_port *from,
1597 struct gtphub_peer_port *to_proxy,
1598 struct gtphub_peer_port **final_unmapped,
1599 struct gtphub_peer_port **unmapped_from_seq,
1600 struct gtphub_peer_port **unmapped_from_tei)
1601{
1602 /* Always (try to) unmap sequence and TEI numbers, which need to be
1603 * replaced in the packet. Either way, give precedence to the proxy, if
1604 * configured. */
1605
1606 struct gtphub_peer_port *from_seq = NULL;
1607 struct gtphub_peer_port *from_tei = NULL;
1608 struct gtphub_peer_port *unmapped = NULL;
1609
1610 if (unmapped_from_seq)
1611 *unmapped_from_seq = from_seq;
1612 if (unmapped_from_tei)
1613 *unmapped_from_tei = from_tei;
1614 if (final_unmapped)
1615 *final_unmapped = unmapped;
1616
1617 from_seq = gtphub_unmap_seq(p, from);
1618
1619 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1620 return -1;
1621
1622 struct gtphub_peer *from_peer = from->peer_addr->peer;
1623 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001624 LOG(LOGL_DEBUG,
1625 "Seq unmap and TEI unmap yield two different peers."
1626 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001627 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1628 gtphub_plane_idx_names[p->plane_idx],
1629 gtphub_peer_str(from_peer),
1630 (int)p->seq,
1631 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001632 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001633 gtphub_port_str2(from_tei)
1634 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001635 }
1636 unmapped = (from_seq? from_seq : from_tei);
1637
1638 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001639 LOG(LOGL_NOTICE,
1640 "Unmap yields a different peer than the configured proxy."
1641 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001642 " unmapped: %s proxy: %s\n",
1643 gtphub_port_str(unmapped),
1644 gtphub_port_str2(to_proxy)
1645 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001646 }
1647 unmapped = (to_proxy? to_proxy : unmapped);
1648
1649 if (!unmapped) {
1650 /* Return no error, but returned pointers are all NULL. */
1651 return 0;
1652 }
1653
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001654 if (unmapped_from_seq)
1655 *unmapped_from_seq = from_seq;
1656 if (unmapped_from_tei)
1657 *unmapped_from_tei = from_tei;
1658 if (final_unmapped)
1659 *final_unmapped = unmapped;
1660 return 0;
1661}
1662
1663static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1664 uint16_t port,
1665 struct osmo_sockaddr *dst)
1666{
1667 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1668}
1669
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001670/* If p is an Echo request, replace p's data with the matching response and
1671 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1672 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001673static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1674 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001675{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001676 if (p->type != GTP_ECHO_REQ)
1677 return 0;
1678
1679 static uint8_t echo_response_data[14] = {
1680 0x32, /* flags */
1681 GTP_ECHO_RSP,
1682 0x00, 14 - 8, /* Length in network byte order */
1683 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1684 0, 0, /* Seq, to be replaced */
1685 0, 0, /* no extensions */
1686 0x0e, /* Recovery IE */
1687 0 /* Recovery counter, to be replaced */
1688 };
1689 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1690 uint8_t *recovery = &echo_response_data[13];
1691
1692 *seq = hton16(p->seq);
1693 *recovery = hub->restart_counter;
1694
1695 *reply_buf = echo_response_data;
1696
1697 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001698}
1699
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001700struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1701 const struct osmo_sockaddr *addr);
1702
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001703/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1704 * address to forward to. Return a pointer to the osmo_fd, but copy the
1705 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1706 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1707 * bytes to forward, 0 or less on failure. */
1708int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1709 unsigned int plane_idx,
1710 const struct osmo_sockaddr *from_addr,
1711 uint8_t *buf,
1712 size_t received,
1713 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001714 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001715 struct osmo_fd **to_ofd,
1716 struct osmo_sockaddr *to_addr)
1717{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001718 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1719 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
1720 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1721 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1722
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001723 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1724 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001725 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001726 gtphub_plane_idx_names[plane_idx],
1727 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001728
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001729 static struct gtp_packet_desc p;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001730 gtp_decode(buf, received, GTPH_SIDE_GGSN, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001731
1732 if (p.rc <= 0)
1733 return -1;
1734
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001735 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1736
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001737 int reply_len;
1738 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1739 if (reply_len > 0) {
1740 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001741 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001742 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001743
1744 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1745 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1746 reply_len);
1747 LOG(LOGL_DEBUG, "--> Echo response to GGSN: %d bytes to %s\n",
1748 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001749 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001750 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001751 if (reply_len < 0)
1752 return -1;
1753
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001754 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001755
1756 /* If a GGSN proxy is configured, check that it's indeed that proxy
1757 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1758 * gtphub, so no-one else is allowed to talk to us from that side. */
1759 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1760 if (ggsn) {
1761 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001762 LOG(LOGL_ERROR,
1763 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001764 " received on GGSN bind is from another sender:"
1765 " proxy: %s sender: %s\n",
1766 gtphub_port_str(ggsn),
1767 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001768 return -1;
1769 }
1770 }
1771
1772 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001773 /* Find a GGSN peer with a matching address. The sender's port
1774 * may in fact differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001775 ggsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001776 }
1777
1778 /* If any PDP context has been created, we already have an entry for
1779 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1780 * us about. */
1781 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001782 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1783 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001784 return -1;
1785 }
1786
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001787 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001788
1789 struct gtphub_peer_port *sgsn_from_seq;
1790 struct gtphub_peer_port *sgsn;
1791 if (gtphub_unmap(hub, &p, ggsn,
1792 hub->sgsn_proxy[plane_idx],
1793 &sgsn, &sgsn_from_seq,
1794 NULL /* not interested, got it in &sgsn already */
1795 )
1796 != 0) {
1797 return -1;
1798 }
1799
1800 if (!sgsn) {
1801 /* A GGSN initiated request would go to a known TEI. So this is
1802 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001803 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001804 return -1;
1805 }
1806
1807 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001808 /* This may be a Create PDP Context response. If it is, there
1809 * are other addresses in the GTP message to set up apart from
1810 * the sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001811 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001812 sgsn, ggsn, &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001813 != 0)
1814 return -1;
1815 }
1816
1817 gtphub_check_restart_counter(hub, &p, ggsn);
1818 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1819
1820 /* If the GGSN is replying to an SGSN request, the sequence nr has
1821 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1822 * create a new mapping. */
1823 if (!sgsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001824 gtphub_map_seq(&p, ggsn, sgsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001825
1826 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001827
1828 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001829
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001830 if (received) {
1831 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1832 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1833 received);
1834 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001835 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001836 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001837 return received;
1838}
1839
1840static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1841{
1842 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1843 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001844 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1845 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001846
1847 if (!(what & BSC_FD_READ))
1848 return 0;
1849
1850 struct gtphub *hub = from_sgsns_ofd->data;
1851
1852 static uint8_t buf[4096];
1853 struct osmo_sockaddr from_addr;
1854 struct osmo_sockaddr to_addr;
1855 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001856 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001857 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001858
1859 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1860 if (len < 1)
1861 return 0;
1862
1863 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1864 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001865 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001866 if (len < 1)
1867 return 0;
1868
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001869 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001870}
1871
1872/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1873int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1874 unsigned int plane_idx,
1875 const struct osmo_sockaddr *from_addr,
1876 uint8_t *buf,
1877 size_t received,
1878 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001879 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001880 struct osmo_fd **to_ofd,
1881 struct osmo_sockaddr *to_addr)
1882{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001883 struct gtphub_bind *from_bind_arr = hub->to_sgsns;
1884 struct gtphub_bind *to_bind_arr = hub->to_ggsns;
1885 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1886 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1887
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001888 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1889 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001890 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001891 gtphub_plane_idx_names[plane_idx],
1892 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001893
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001894 static struct gtp_packet_desc p;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001895 gtp_decode(buf, received, GTPH_SIDE_SGSN, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001896
1897 if (p.rc <= 0)
1898 return -1;
1899
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001900 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1901
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001902 int reply_len;
1903 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1904 if (reply_len > 0) {
1905 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001906 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001907 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001908
1909 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1910 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1911 reply_len);
1912 LOG(LOGL_DEBUG, "<-- Echo response to SGSN: %d bytes to %s\n",
1913 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001914 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001915 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001916 if (reply_len < 0)
1917 return -1;
1918
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001919 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001920
1921 /* If an SGSN proxy is configured, check that it's indeed that proxy
1922 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1923 * gtphub, so no-one else is allowed to talk to us from that side. */
1924 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1925 if (sgsn) {
1926 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001927 LOG(LOGL_ERROR,
1928 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001929 " received on GGSN bind is from another sender:"
1930 " proxy: %s sender: %s\n",
1931 gtphub_port_str(sgsn),
1932 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001933 return -1;
1934 }
1935 }
1936
1937 if (!sgsn) {
1938 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001939 * entry for this SGSN. The port may differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001940 sgsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001941 }
1942
1943 if (!sgsn) {
1944 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1945 * first contact without being known yet, so create the peer
1946 * struct for the current sender. */
1947 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001948 LOG(LOGL_ERROR,
1949 "User plane peer was not announced by PDP Context,"
1950 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001951 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001952 return -1;
1953 }
1954
1955 struct gsn_addr from_gsna;
1956 uint16_t from_port;
1957 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1958 return -1;
1959
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001960 sgsn = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001961 }
1962
1963 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001964 /* This could theoretically happen for invalid address data or
1965 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001966 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1967 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001968 return -1;
1969 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001970 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001971
1972 struct gtphub_peer_port *ggsn_from_seq;
1973 struct gtphub_peer_port *ggsn;
1974 if (gtphub_unmap(hub, &p, sgsn,
1975 hub->ggsn_proxy[plane_idx],
1976 &ggsn, &ggsn_from_seq,
1977 NULL /* not interested, got it in &ggsn already */
1978 )
1979 != 0) {
1980 return -1;
1981 }
1982
Neels Hofmeyra208c732015-11-11 19:26:09 +01001983 if (!ggsn) {
1984 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1985 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986 }
1987
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001988 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001989 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990 return -1;
1991 }
1992
1993 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001994 /* This may be a Create PDP Context request. If it is, there are
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001995 * other addresses in the GTP message to set up apart from the
1996 * sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001997 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001998 sgsn, ggsn, &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001999 != 0)
2000 return -1;
2001 }
2002
2003 gtphub_check_restart_counter(hub, &p, sgsn);
2004 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
2005
2006 /* If the SGSN is replying to a GGSN request, the sequence nr has
2007 * already been unmapped above (unmap_ggsn != NULL), and we need not
2008 * create a new outgoing sequence map. */
2009 if (!ggsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01002010 gtphub_map_seq(&p, sgsn, ggsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002011
2012 osmo_sockaddr_copy(to_addr, &ggsn->sa);
2013
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002014 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002015
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002016 if (received) {
2017 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2018 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2019 received);
2020 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002021 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002022 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002023 return received;
2024}
2025
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002026static void resolved_gssn_del_cb(struct expiring_item *expi)
2027{
2028 struct gtphub_resolved_ggsn *ggsn;
2029 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2030
2031 gtphub_port_ref_count_dec(ggsn->peer);
2032 llist_del(&ggsn->entry);
2033
2034 ggsn->expiry_entry.del_cb = 0;
2035 expiring_item_del(&ggsn->expiry_entry);
2036
2037 talloc_free(ggsn);
2038}
2039
2040void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2041 struct gsn_addr *resolved_addr,
2042 time_t now)
2043{
2044 struct gtphub_peer_port *pp;
2045 struct gtphub_resolved_ggsn *ggsn;
2046
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002047 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002048 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2049 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002050
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002051 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
2052 resolved_addr, 2123);
2053 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002054 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2055 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002056 return;
2057 }
2058
2059 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2060 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002061 INIT_LLIST_HEAD(&ggsn->entry);
2062 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002063
2064 ggsn->peer = pp;
2065 gtphub_port_ref_count_inc(pp);
2066
2067 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002068 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002069
2070 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002071 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002072
2073 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2074}
2075
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002076static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2077{
2078 return pp->ref_count == 0;
2079}
2080
2081static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2082{
2083 struct gtphub_peer_port *pp, *npp;
2084 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2085 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002086 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002087 gtphub_port_str(pp));
2088 gtphub_peer_port_del(pp);
2089 }
2090 }
2091 return llist_empty(&pa->ports);
2092}
2093
2094static int gtphub_gc_peer(struct gtphub_peer *p)
2095{
2096 struct gtphub_peer_addr *pa, *npa;
2097 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2098 if (gtphub_gc_peer_addr(pa)) {
2099 gtphub_peer_addr_del(pa);
2100 }
2101 }
2102
2103 /* Note that there's a ref_count in each gtphub_peer_port instance
2104 * listed within p->addresses, referenced by TEI mappings from
2105 * hub->tei_map. As long as those don't expire, this peer will stay. */
2106
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002107 return llist_empty(&p->addresses)
2108 && nr_map_empty(&p->seq_map);
2109}
2110
2111static void gtphub_gc_bind(struct gtphub_bind *b)
2112{
2113 struct gtphub_peer *p, *n;
2114 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2115 if (gtphub_gc_peer(p)) {
2116 gtphub_peer_del(p);
2117 }
2118 }
2119}
2120
2121void gtphub_gc(struct gtphub *hub, time_t now)
2122{
2123 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002124 expired = expiry_tick(&hub->expire_quickly, now);
2125 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002126
2127 /* ... */
2128
2129 if (expired) {
2130 int i;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002131 for_each_plane(i) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002132 gtphub_gc_bind(&hub->to_sgsns[i]);
2133 gtphub_gc_bind(&hub->to_ggsns[i]);
2134 }
2135 }
2136}
2137
2138static void gtphub_gc_cb(void *data)
2139{
2140 struct gtphub *hub = data;
2141 gtphub_gc(hub, gtphub_now());
2142 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2143}
2144
2145static void gtphub_gc_start(struct gtphub *hub)
2146{
2147 hub->gc_timer.cb = gtphub_gc_cb;
2148 hub->gc_timer.data = hub;
2149
2150 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2151}
2152
2153/* called by unit tests */
2154void gtphub_init(struct gtphub *hub)
2155{
2156 gtphub_zero(hub);
2157
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002158 INIT_LLIST_HEAD(&hub->tunnels);
2159
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002160 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2161 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002162
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002163 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2164
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002165 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002166 for_each_plane(plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002167 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
2168 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
2169 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002170
2171 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2172 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2173 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
2174 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002175}
2176
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002177/* For the test suite, this is kept separate from gtphub_stop(), which also
2178 * closes sockets. The test suite avoids using sockets and would cause
2179 * segfaults when trying to close uninitialized ofds. */
2180void gtphub_free(struct gtphub *hub)
2181{
2182 /* By expiring all mappings, a garbage collection should free
2183 * everything else. A gtphub_bind_free() will assert that everything is
2184 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002185 expiry_clear(&hub->expire_quickly);
2186 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002187
2188 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002189 for_each_plane(plane_idx) {
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002190 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
2191 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
2192
2193 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
2194 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
2195 }
2196}
2197
2198void gtphub_stop(struct gtphub *hub)
2199{
2200 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002201 for_each_plane(plane_idx) {
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002202 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
2203 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
2204 }
2205 gtphub_free(hub);
2206}
2207
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002208static int gtphub_make_proxy(struct gtphub *hub,
2209 struct gtphub_peer_port **pp,
2210 struct gtphub_bind *bind,
2211 const struct gtphub_cfg_addr *addr)
2212{
2213 if (!addr->addr_str)
2214 return 0;
2215
2216 struct gsn_addr gsna;
2217 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2218 return -1;
2219
2220 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2221
2222 /* This is *the* proxy. Make sure it is never expired. */
2223 gtphub_port_ref_count_inc(*pp);
2224 return 0;
2225}
2226
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002227int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2228 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002229{
2230 int rc;
2231
2232 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002233
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002234 hub->restart_counter = restart_counter;
2235
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002236 /* If a Ctrl plane proxy is configured, ares will never be used. */
2237 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
2238 if (gtphub_ares_init(hub) != 0) {
2239 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2240 return -1;
2241 }
2242 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002243
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002244 /* TODO set hub->restart_counter from external file. */
2245
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002246 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002247 for_each_plane(plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002248 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
2249 &cfg->to_ggsns[plane_idx],
2250 from_ggsns_read_cb, hub, plane_idx);
2251 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002252 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
2253 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002254 return rc;
2255 }
2256
2257 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
2258 &cfg->to_sgsns[plane_idx],
2259 from_sgsns_read_cb, hub, plane_idx);
2260 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002261 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
2262 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002263 return rc;
2264 }
2265 }
2266
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002267 for_each_plane(plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002268 if (gtphub_make_proxy(hub,
2269 &hub->sgsn_proxy[plane_idx],
2270 &hub->to_sgsns[plane_idx],
2271 &cfg->sgsn_proxy[plane_idx])
2272 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01002273 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002274 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002275 cfg->sgsn_proxy[plane_idx].addr_str,
2276 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002277 return -1;
2278 }
2279 if (gtphub_make_proxy(hub,
2280 &hub->ggsn_proxy[plane_idx],
2281 &hub->to_ggsns[plane_idx],
2282 &cfg->ggsn_proxy[plane_idx])
2283 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002284 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002285 return -1;
2286 }
2287 }
2288
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002289 for_each_plane(plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002290 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002291 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002292 gtphub_plane_idx_names[plane_idx],
2293 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
2294 }
2295
Neels Hofmeyrf9773202015-11-27 00:05:56 +01002296 for_each_plane(plane_idx) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002297 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002298 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002299 gtphub_plane_idx_names[plane_idx],
2300 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
2301 }
2302
2303 gtphub_gc_start(hub);
2304 return 0;
2305}
2306
2307static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2308 const struct gsn_addr *addr)
2309{
2310 struct gtphub_peer_addr *a;
2311 llist_for_each_entry(a, &peer->addresses, entry) {
2312 if (gsn_addr_same(&a->addr, addr))
2313 return a;
2314 }
2315 return NULL;
2316}
2317
2318static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2319 uint16_t port)
2320{
2321 OSMO_ASSERT(port);
2322 struct gtphub_peer_port *pp;
2323 llist_for_each_entry(pp, &a->ports, entry) {
2324 if (pp->port == port)
2325 return pp;
2326 }
2327 return NULL;
2328}
2329
2330static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2331 const struct gsn_addr *addr)
2332{
2333 struct gtphub_peer *peer;
2334 llist_for_each_entry(peer, &bind->peers, entry) {
2335 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2336 if (a)
2337 return a;
2338 }
2339 return NULL;
2340}
2341
2342static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2343 const struct gsn_addr *addr,
2344 uint16_t port)
2345{
2346 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2347 if (!a)
2348 return NULL;
2349 return gtphub_addr_find_port(a, port);
2350}
2351
2352struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2353 const struct osmo_sockaddr *addr)
2354{
2355 struct gsn_addr gsna;
2356 uint16_t port;
2357 gsn_addr_from_sockaddr(&gsna, &port, addr);
2358 return gtphub_port_find(bind, &gsna, port);
2359}
2360
2361static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2362 struct gtphub_bind *bind)
2363{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002364 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2365 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002366 OSMO_ASSERT(peer);
2367
2368 INIT_LLIST_HEAD(&peer->addresses);
2369
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002370 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002371 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002372
2373 /* TODO use something random to pick the initial sequence nr.
2374 0x6d31 produces the ASCII character sequence 'm1', currently used in
2375 gtphub_nc_test.sh. */
2376 peer->seq_pool.last_nr = 0x6d31 - 1;
2377
2378 llist_add(&peer->entry, &bind->peers);
2379 return peer;
2380}
2381
2382static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2383 const struct gsn_addr *addr)
2384{
2385 struct gtphub_peer_addr *a;
2386 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2387 OSMO_ASSERT(a);
2388 a->peer = peer;
2389 gsn_addr_copy(&a->addr, addr);
2390 INIT_LLIST_HEAD(&a->ports);
2391 llist_add(&a->entry, &peer->addresses);
2392
2393 return a;
2394}
2395
2396static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2397 struct gtphub_bind *bind,
2398 const struct gsn_addr *addr)
2399{
2400 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2401 if (a)
2402 return a;
2403
2404 /* If we haven't found an address, that means we need to create an
2405 * entirely new peer for the new address. More addresses may be added
2406 * to this peer later, but not via this function. */
2407 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002408
2409 a = gtphub_peer_add_addr(peer, addr);
2410
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002411 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002412 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002413 gsn_addr_to_str(&a->addr));
2414
2415 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002416}
2417
2418static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2419 uint16_t port)
2420{
2421 struct gtphub_peer_port *pp;
2422
2423 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2424 OSMO_ASSERT(pp);
2425 pp->peer_addr = a;
2426 pp->port = port;
2427
2428 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2429 talloc_free(pp);
2430 return NULL;
2431 }
2432
2433 llist_add(&pp->entry, &a->ports);
2434
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002435 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002436 gsn_addr_to_str(&a->addr),
2437 (int)port);
2438
2439 return pp;
2440}
2441
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002442struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2443 struct gtphub_bind *bind,
2444 const struct gsn_addr *addr,
2445 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002446{
2447 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2448
2449 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2450 if (pp)
2451 return pp;
2452
2453 return gtphub_addr_add_port(a, port);
2454}
2455
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002456/* Find a GGSN peer with a matching address. If the address is known but the
2457 * port not, create a new port for that peer address. */
2458struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2459 const struct osmo_sockaddr *addr)
2460{
2461 struct gtphub_peer_addr *pa;
2462 struct gtphub_peer_port *pp;
2463
2464 struct gsn_addr gsna;
2465 uint16_t port;
2466 gsn_addr_from_sockaddr(&gsna, &port, addr);
2467
2468 pa = gtphub_addr_find(bind, &gsna);
2469 if (!pa)
2470 return NULL;
2471
2472 pp = gtphub_addr_find_port(pa, port);
2473
2474 if (!pp)
2475 pp = gtphub_addr_add_port(pa, port);
2476
2477 return pp;
2478}
2479
2480
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002481/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2482 * resolution should be possible but failed, and 1 if resolution was
2483 * successful. *pp will be set to NULL if <1 is returned. */
2484static int gtphub_resolve_ggsn(struct gtphub *hub,
2485 struct gtp_packet_desc *p,
2486 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002487{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002488 *pp = NULL;
2489
2490 /* TODO determine from message type whether IEs should be present? */
2491
2492 int rc;
2493 const char *imsi_str;
2494 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2495 if (rc < 1)
2496 return rc;
2497 OSMO_ASSERT(imsi_str);
2498
2499 const char *apn_str;
2500 rc = get_ie_apn_str(p->ie, &apn_str);
2501 if (rc < 1)
2502 return rc;
2503 OSMO_ASSERT(apn_str);
2504
2505 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2506 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002507}
2508
2509
2510/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002511/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002512/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2513 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002514static int _osmo_getaddrinfo(struct addrinfo **result,
2515 uint16_t family, uint16_t type, uint8_t proto,
2516 const char *host, uint16_t port)
2517{
2518 struct addrinfo hints;
2519 char portbuf[16];
2520
2521 sprintf(portbuf, "%u", port);
2522 memset(&hints, '\0', sizeof(struct addrinfo));
2523 hints.ai_family = family;
2524 if (type == SOCK_RAW) {
2525 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2526 * SOCK_RAW and IPPROTO_GRE is used.
2527 */
2528 hints.ai_socktype = SOCK_DGRAM;
2529 hints.ai_protocol = IPPROTO_UDP;
2530 } else {
2531 hints.ai_socktype = type;
2532 hints.ai_protocol = proto;
2533 }
2534
2535 return getaddrinfo(host, portbuf, &hints, result);
2536}
2537
2538/* TODO move to osmocom/core/socket.c ? */
2539int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2540 uint16_t family, uint16_t type, uint8_t proto,
2541 const char *host, uint16_t port)
2542{
2543 struct addrinfo *res;
2544 int rc;
2545 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2546
2547 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002548 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002549 return -EINVAL;
2550 }
2551
2552 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2553 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2554 addr->l = res->ai_addrlen;
2555 freeaddrinfo(res);
2556
2557 return 0;
2558}
2559
2560int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2561 char *port_str, size_t port_str_len,
2562 const struct osmo_sockaddr *addr,
2563 int flags)
2564{
2565 int rc;
2566
2567 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2568 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2569 return -1;
2570 }
2571
2572 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002573 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2574 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002575 return -1;
2576 }
2577
2578 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2579 addr_str, addr_str_len,
2580 port_str, port_str_len,
2581 flags);
2582
2583 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002584 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2585 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2586 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002587
2588 return rc;
2589}
2590
2591const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2592 char *buf, size_t buf_len)
2593{
2594 const int portbuf_len = 6;
2595 OSMO_ASSERT(buf_len > portbuf_len);
2596 char *portbuf = buf + buf_len - portbuf_len;
2597 buf_len -= portbuf_len;
2598 if (osmo_sockaddr_to_strs(buf, buf_len,
2599 portbuf, portbuf_len,
2600 addr,
2601 NI_NUMERICHOST | NI_NUMERICSERV))
2602 return NULL;
2603
2604 char *pos = buf + strnlen(buf, buf_len-1);
2605 size_t len = buf_len - (pos - buf);
2606
2607 snprintf(pos, len, " port %s", portbuf);
2608 buf[buf_len-1] = '\0';
2609
2610 return buf;
2611}
2612
2613const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2614{
2615 static char buf[256];
2616 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2617 if (! result)
2618 return "(invalid)";
2619 return result;
2620}
2621
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002622int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2623 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002624{
2625 if (a == b)
2626 return 0;
2627 if (!a)
2628 return -1;
2629 if (!b)
2630 return 1;
2631 if (a->l != b->l) {
2632 /* Lengths are not the same, but determine the order. Will
2633 * anyone ever sort a list by osmo_sockaddr though...? */
2634 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2635 if (cmp == 0) {
2636 if (a->l < b->l)
2637 return -1;
2638 else
2639 return 1;
2640 }
2641 return cmp;
2642 }
2643 return memcmp(&a->a, &b->a, a->l);
2644}
2645
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002646void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2647 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002648{
2649 OSMO_ASSERT(src->l <= sizeof(dst->a));
2650 memcpy(&dst->a, &src->a, src->l);
2651 dst->l = src->l;
2652}