blob: 0550c7ffeb7e084648e826916a98d19b4a359da2 [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
65/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
66
67enum gtp_rc {
68 GTP_RC_UNKNOWN = 0,
69 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010070 GTP_RC_PDU_C = 2, /* a real packet with IEs */
71 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020072
73 GTP_RC_TOOSHORT = -1,
74 GTP_RC_UNSUPPORTED_VERSION = -2,
75 GTP_RC_INVALID_IE = -3,
76};
77
78struct gtp_packet_desc {
79 union gtp_packet *data;
80 int data_len;
81 int header_len;
82 int version;
83 uint8_t type;
84 uint16_t seq;
85 uint32_t header_tei;
86 int rc; /* enum gtp_rc */
87 unsigned int plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010088 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020089 union gtpie_member *ie[GTPIE_SIZE];
90};
91
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010092
93/* counters */
94
95enum gtphub_counters_io {
96 GTPH_CTR_PKTS_IN = 0,
97 GTPH_CTR_PKTS_OUT,
98 GTPH_CTR_BYTES_IN,
99 GTPH_CTR_BYTES_OUT
100};
101
102static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
103 { "packets.in", "Packets ( In)" },
104 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100105 { "bytes.in", "Bytes ( In)" },
106 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100107};
108
109static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
110 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100111 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100112 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
113 .ctr_desc = gtphub_counters_io_desc,
114 .class_id = OSMO_STATS_CLASS_GLOBAL,
115};
116
117
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200118void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
119{
120 memcpy(gsna, src, sizeof(struct gsn_addr));
121}
122
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200123int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
124 const struct osmo_sockaddr *sa)
125{
126 char addr_str[256];
127 char port_str[6];
128
129 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
130 port_str, sizeof(port_str),
131 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
132 != 0) {
133 return -1;
134 }
135
136 if (port)
137 *port = atoi(port_str);
138
139 return gsn_addr_from_str(gsna, addr_str);
140}
141
142int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
143{
144 int af = AF_INET;
145 gsna->len = 4;
146 const char *pos = numeric_addr_str;
147 for (; *pos; pos++) {
148 if (*pos == ':') {
149 af = AF_INET6;
150 gsna->len = 16;
151 break;
152 }
153 }
154
155 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
156 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100157 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
158 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200159 return -1;
160 }
161 return 0;
162}
163
164const char *gsn_addr_to_str(const struct gsn_addr *gsna)
165{
166 static char buf[INET6_ADDRSTRLEN + 1];
167 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
168}
169
170const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
171 char *strbuf,
172 int strbuf_len)
173{
174 int af;
175 switch (gsna->len) {
176 case 4:
177 af = AF_INET;
178 break;
179 case 16:
180 af = AF_INET6;
181 break;
182 default:
183 return NULL;
184 }
185
186 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
187 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100188 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
189 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100190 strerror(errno),
191 (int)gsna->len,
192 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200193 }
194 return r;
195}
196
197int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
198{
199 if (a == b)
200 return 1;
201 if ((!a) || (!b))
202 return 0;
203 if (a->len != b->len)
204 return 0;
205 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
206}
207
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100208static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
209 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200210{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100211 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200212 return -1;
213
214 unsigned int len;
215 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
216 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
217 &len, gsna->buf, sizeof(gsna->buf))
218 != 0)
219 return -1;
220 gsna->len = len;
221 return 0;
222}
223
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100224static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
225 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200226{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100227 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200228 return -1;
229
230 int ie_idx;
231 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
232
233 if (ie_idx < 0)
234 return -1;
235
236 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
237 int ie_l = ntoh16(ie->l);
238 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100239 LOG(LOGL_ERROR, "Not implemented:"
240 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200241 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
242 return -1;
243 }
244
245 memcpy(ie->v, gsna->buf, (int)ie_l);
246 return 0;
247}
248
249/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
250 */
251void validate_gtp0_header(struct gtp_packet_desc *p)
252{
253 const struct gtp0_header *pheader = &(p->data->gtp0.h);
254 p->rc = GTP_RC_UNKNOWN;
255 p->header_len = 0;
256
257 OSMO_ASSERT(p->data_len >= 1);
258 OSMO_ASSERT(p->version == 0);
259
260 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100261 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200262 p->rc = GTP_RC_TOOSHORT;
263 return;
264 }
265
266 p->type = ntoh8(pheader->type);
267 p->seq = ntoh16(pheader->seq);
268 p->header_tei = 0; /* TODO */
269
270 if (p->data_len == GTP0_HEADER_SIZE) {
271 p->rc = GTP_RC_TINY;
272 p->header_len = GTP0_HEADER_SIZE;
273 return;
274 }
275
276 /* Check packet length field versus length of packet */
277 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100278 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
279 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100280 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
281 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200282 p->rc = GTP_RC_TOOSHORT;
283 return;
284 }
285
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100286 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200287 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100288 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200289}
290
291/* Validate GTP version 1 data, and update p->rc with the result, as well as
292 * p->header_len in case of a valid header. */
293void validate_gtp1_header(struct gtp_packet_desc *p)
294{
295 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
296 p->rc = GTP_RC_UNKNOWN;
297 p->header_len = 0;
298
299 OSMO_ASSERT(p->data_len >= 1);
300 OSMO_ASSERT(p->version == 1);
301
302 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
303 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100304 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200305 p->rc = GTP_RC_TOOSHORT;
306 return;
307 }
308
309 p->type = ntoh8(pheader->type);
310 p->header_tei = ntoh32(pheader->tei);
311 p->seq = ntoh16(pheader->seq);
312
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100313 LOG(LOGL_DEBUG, "GTPv1\n"
314 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
315 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
316 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
317 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
318 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
319 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
320 p->type, p->type,
321 ntoh16(pheader->length), ntoh16(pheader->length),
322 p->header_tei, p->header_tei,
323 p->seq, p->seq,
324 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200325 pheader->next, pheader->next);
326
327 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
328 p->rc = GTP_RC_TINY;
329 p->header_len = GTP1_HEADER_SIZE_SHORT;
330 return;
331 }
332
333 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100334 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
335 if (p->data_len != announced_len) {
336 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
337 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100338 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
339 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200340 p->rc = GTP_RC_TOOSHORT;
341 return;
342 }
343
Neels Hofmeyre921e322015-11-11 00:45:50 +0100344 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345 p->header_len = GTP1_HEADER_SIZE_LONG;
346}
347
348/* Examine whether p->data of size p->data_len has a valid GTP header. Set
349 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
350 * gtp_rc). p->data must point at a buffer with p->data_len set. */
351void validate_gtp_header(struct gtp_packet_desc *p)
352{
353 p->rc = GTP_RC_UNKNOWN;
354
355 /* Need at least 1 byte in order to check version */
356 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100357 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
358 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200359 p->rc = GTP_RC_TOOSHORT;
360 return;
361 }
362
363 p->version = p->data->flags >> 5;
364
365 switch (p->version) {
366 case 0:
367 validate_gtp0_header(p);
368 break;
369 case 1:
370 validate_gtp1_header(p);
371 break;
372 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100373 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200374 p->rc = GTP_RC_UNSUPPORTED_VERSION;
375 break;
376 }
377}
378
379
380/* Return the value of the i'th IMSI IEI by copying to *imsi.
381 * The first IEI is reached by passing i = 0.
382 * imsi must point at allocated space of (at least) 8 bytes.
383 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100384static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200385{
386 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
387}
388
389/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100390static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200391{
392 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
393}
394
395static char imsi_digit_to_char(uint8_t nibble)
396{
397 nibble &= 0x0f;
398 if (nibble > 9)
399 return (nibble == 0x0f) ? '\0' : '?';
400 return '0' + nibble;
401}
402
403/* Return a human readable IMSI string, in a static buffer.
404 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100405static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200406{
407 static char str[17];
408 int i;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100409 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200410
411 for (i = 0; i < 8; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100412 c = imsi_digit_to_char(imsi[i]);
413 if (c == '?')
414 return -1;
415 str[2*i] = c;
416
417 c = imsi_digit_to_char(imsi[i] >> 4);
418 if (c == '?')
419 return -1;
420 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200421 }
422 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100423 *imsi_str = str;
424 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200425}
426
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100427/* Return 0 if not present, 1 if present and decoded successfully, -1 if
428 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100429static int get_ie_imsi_str(union gtpie_member *ie[], int i,
430 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100431{
432 uint8_t imsi_buf[8];
433 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100434 return 0;
435 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100436}
437
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100438/* Return 0 if not present, 1 if present and decoded successfully, -1 if
439 * present but cannot be decoded. */
440static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100441{
442 static char apn_buf[GSM_APN_LENGTH];
443 unsigned int len;
444 if (gtpie_gettlv(ie, GTPIE_APN, 0,
445 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100446 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100447
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100448 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100449 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
450 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100451 return -1;
452 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100453
454 if (len > (sizeof(apn_buf) - 1))
455 len = sizeof(apn_buf) - 1;
456 apn_buf[len] = '\0';
457
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100458 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
459 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100460 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
461 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100462 return -1;
463 }
464 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100465}
466
467
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200468/* Validate header, and index information elements. Write decoded packet
469 * information to *res. res->data will point at the given data buffer. On
470 * error, p->rc is set <= 0 (see enum gtp_rc). */
471static void gtp_decode(const uint8_t *data, int data_len,
472 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100473 struct gtp_packet_desc *res,
474 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200475{
476 ZERO_STRUCT(res);
477 res->data = (union gtp_packet*)data;
478 res->data_len = data_len;
479 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100480 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200481
482 validate_gtp_header(res);
483
484 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100485 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200486 return;
487 }
488
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100489 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200490
Neels Hofmeyre921e322015-11-11 00:45:50 +0100491 if (from_plane_idx == GTPH_PLANE_USER) {
492 res->rc = GTP_RC_PDU_U;
493 return;
494 }
495
496 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100497 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200498 return;
499 }
500
501 if (gtpie_decaps(res->ie, res->version,
502 (void*)(data + res->header_len),
503 res->data_len - res->header_len) != 0) {
504 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100505 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
506 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200507 return;
508 }
509
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100510#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100511 /* TODO if (<loglevel is debug>) { ...
512 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200513 int i;
514
515 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100516 const char *imsi;
517 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200518 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100519 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200520 }
521
522 for (i = 0; i < 10; i++) {
523 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100524 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200525 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100526 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200527 }
528
529 for (i = 0; i < 2; i++) {
530 struct gsn_addr addr;
531 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100532 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533 }
534
535 for (i = 0; i < 10; i++) {
536 uint32_t tei;
537 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
538 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100539 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540 tei, tei);
541 }
542
543 for (i = 0; i < 10; i++) {
544 uint32_t tei;
545 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
546 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100547 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200548 tei, tei);
549 }
550#endif
551}
552
553
554/* expiry */
555
556void expiry_init(struct expiry *exq, int expiry_in_seconds)
557{
558 ZERO_STRUCT(exq);
559 exq->expiry_in_seconds = expiry_in_seconds;
560 INIT_LLIST_HEAD(&exq->items);
561}
562
563void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
564{
565 item->expiry = now + exq->expiry_in_seconds;
566
567 /* Add/move to the tail to always sort by expiry, ascending. */
568 llist_del(&item->entry);
569 llist_add_tail(&item->entry, &exq->items);
570}
571
572int expiry_tick(struct expiry *exq, time_t now)
573{
574 int expired = 0;
575 struct expiring_item *m, *n;
576 llist_for_each_entry_safe(m, n, &exq->items, entry) {
577 if (m->expiry <= now) {
578 expiring_item_del(m);
579 expired ++;
580 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200581 /* The items are added sorted by expiry. So when we hit
582 * an unexpired entry, only more unexpired ones will
583 * follow. */
584 break;
585 }
586 }
587 return expired;
588}
589
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100590void expiry_clear(struct expiry *exq)
591{
592 struct expiring_item *m, *n;
593 llist_for_each_entry_safe(m, n, &exq->items, entry) {
594 expiring_item_del(m);
595 }
596}
597
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200598void expiring_item_init(struct expiring_item *item)
599{
600 ZERO_STRUCT(item);
601 INIT_LLIST_HEAD(&item->entry);
602}
603
604void expiring_item_del(struct expiring_item *item)
605{
606 OSMO_ASSERT(item);
607 llist_del(&item->entry);
608 INIT_LLIST_HEAD(&item->entry);
609 if (item->del_cb) {
610 /* avoid loops */
611 del_cb_t del_cb = item->del_cb;
612 item->del_cb = 0;
613 (del_cb)(item);
614 }
615}
616
617
618/* nr_map, nr_pool */
619
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100620void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200621{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100622 *pool = (struct nr_pool){
623 .nr_min = nr_min,
624 .nr_max = nr_max,
625 .last_nr = nr_max
626 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200627}
628
629nr_t nr_pool_next(struct nr_pool *pool)
630{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100631 if (pool->last_nr >= pool->nr_max)
632 pool->last_nr = pool->nr_min;
633 else
634 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200635
636 return pool->last_nr;
637}
638
639void nr_map_init(struct nr_map *map, struct nr_pool *pool,
640 struct expiry *exq)
641{
642 ZERO_STRUCT(map);
643 map->pool = pool;
644 map->add_items_to_expiry = exq;
645 INIT_LLIST_HEAD(&map->mappings);
646}
647
648void nr_mapping_init(struct nr_mapping *m)
649{
650 ZERO_STRUCT(m);
651 INIT_LLIST_HEAD(&m->entry);
652 expiring_item_init(&m->expiry_entry);
653}
654
655void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
656{
657 /* Generate a mapped number */
658 mapping->repl = nr_pool_next(map->pool);
659
660 /* Add to the tail to always yield a list sorted by expiry, in
661 * ascending order. */
662 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100663 nr_map_refresh(map, mapping, now);
664}
665
666void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
667{
668 if (!map->add_items_to_expiry)
669 return;
670 expiry_add(map->add_items_to_expiry,
671 &mapping->expiry_entry,
672 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200673}
674
675void nr_map_clear(struct nr_map *map)
676{
677 struct nr_mapping *m;
678 struct nr_mapping *n;
679 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
680 nr_mapping_del(m);
681 }
682}
683
684int nr_map_empty(const struct nr_map *map)
685{
686 return llist_empty(&map->mappings);
687}
688
689struct nr_mapping *nr_map_get(const struct nr_map *map,
690 void *origin, nr_t nr_orig)
691{
692 struct nr_mapping *mapping;
693 llist_for_each_entry(mapping, &map->mappings, entry) {
694 if ((mapping->origin == origin)
695 && (mapping->orig == nr_orig))
696 return mapping;
697 }
698 /* Not found. */
699 return NULL;
700}
701
702struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
703{
704 struct nr_mapping *mapping;
705 llist_for_each_entry(mapping, &map->mappings, entry) {
706 if (mapping->repl == nr_repl) {
707 return mapping;
708 }
709 }
710 /* Not found. */
711 return NULL;
712}
713
714void nr_mapping_del(struct nr_mapping *mapping)
715{
716 OSMO_ASSERT(mapping);
717 llist_del(&mapping->entry);
718 INIT_LLIST_HEAD(&mapping->entry);
719 expiring_item_del(&mapping->expiry_entry);
720}
721
722
723/* gtphub */
724
725const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
726 "CTRL",
727 "USER",
728};
729
730const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
731 2123,
732 2152,
733};
734
735time_t gtphub_now(void)
736{
737 struct timespec now_tp;
738 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
739 return now_tp.tv_sec;
740}
741
742/* Remove a gtphub_peer from its list and free it. */
743static void gtphub_peer_del(struct gtphub_peer *peer)
744{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100745 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200746 nr_map_clear(&peer->seq_map);
747 llist_del(&peer->entry);
748 talloc_free(peer);
749}
750
751static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
752{
753 OSMO_ASSERT(llist_empty(&pa->ports));
754 llist_del(&pa->entry);
755 talloc_free(pa);
756}
757
758static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
759{
760 OSMO_ASSERT(pp->ref_count == 0);
761 llist_del(&pp->entry);
762 talloc_free(pp);
763}
764
765/* From the information in the gtp_packet_desc, return the address of a GGSN.
766 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100767static int gtphub_resolve_ggsn(struct gtphub *hub,
768 struct gtp_packet_desc *p,
769 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200770
771/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100772struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
773 const char *imsi_str,
774 const char *apn_ni_str);
775int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200776
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200777static void gtphub_zero(struct gtphub *hub)
778{
779 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100780 INIT_LLIST_HEAD(&hub->ggsn_lookups);
781 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200782}
783
784static int gtphub_sock_init(struct osmo_fd *ofd,
785 const struct gtphub_cfg_addr *addr,
786 osmo_fd_cb_t cb,
787 void *data,
788 int ofd_id)
789{
790 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100791 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200792 return -1;
793 }
794 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100795 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200796 return -1;
797 }
798
799 ofd->when = BSC_FD_READ;
800 ofd->cb = cb;
801 ofd->data = data;
802 ofd->priv_nr = ofd_id;
803
804 int rc;
805 rc = osmo_sock_init_ofd(ofd,
806 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
807 addr->addr_str, addr->port,
808 OSMO_SOCK_F_BIND);
809 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100810 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
811 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200812 return -1;
813 }
814
815 return 0;
816}
817
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100818static void gtphub_sock_close(struct osmo_fd *ofd)
819{
820 close(ofd->fd);
821 osmo_fd_unregister(ofd);
822 ofd->cb = NULL;
823}
824
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200825static void gtphub_bind_init(struct gtphub_bind *b)
826{
827 ZERO_STRUCT(b);
828
829 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100830
831 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
832 &gtphub_ctrg_io_desc, 0);
833 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200834}
835
836static int gtphub_bind_start(struct gtphub_bind *b,
837 const struct gtphub_cfg_bind *cfg,
838 osmo_fd_cb_t cb, void *cb_data,
839 unsigned int ofd_id)
840{
841 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
842 return -1;
843 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
844 return -1;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100845 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200846 return 0;
847}
848
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100849static void gtphub_bind_free(struct gtphub_bind *b)
850{
851 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100852 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100853}
854
855static void gtphub_bind_stop(struct gtphub_bind *b) {
856 gtphub_sock_close(&b->ofd);
857 gtphub_bind_free(b);
858}
859
Neels Hofmeyr40348972015-11-17 12:22:28 +0100860/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200861 * Return the number of bytes read, zero on error. */
862static int gtphub_read(const struct osmo_fd *from,
863 struct osmo_sockaddr *from_addr,
864 uint8_t *buf, size_t buf_len)
865{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100866 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200867
Neels Hofmeyr40348972015-11-17 12:22:28 +0100868 /* recvfrom requires the available length set in *from_addr_len. */
869 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200870 errno = 0;
871 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100872 (struct sockaddr*)&from_addr->a,
873 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200874 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
875 * is not truncated. Then maybe reduce buf's size. */
876
877 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100878 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
879 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880 return 0;
881 }
882
Neels Hofmeyr40348972015-11-17 12:22:28 +0100883 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
884 (int)received, osmo_sockaddr_to_str(from_addr),
885 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200886
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200887 return received;
888}
889
890inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
891{
892 OSMO_ASSERT(pp->ref_count < UINT_MAX);
893 pp->ref_count++;
894}
895
896inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
897{
898 OSMO_ASSERT(pp->ref_count > 0);
899 pp->ref_count--;
900}
901
902inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
903{
904 OSMO_ASSERT(p->version == 1);
905 p->data->gtp1l.h.seq = hton16(seq);
906 p->seq = seq;
907}
908
909inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
910{
911 OSMO_ASSERT(p->version == 1);
912 p->data->gtp1l.h.tei = hton32(tei);
913 p->header_tei = tei;
914}
915
916static void gtphub_mapping_del_cb(struct expiring_item *expi);
917
918static struct nr_mapping *gtphub_mapping_new()
919{
920 struct nr_mapping *nrm;
921 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
922 OSMO_ASSERT(nrm);
923
924 nr_mapping_init(nrm);
925 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
926 return nrm;
927}
928
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100929static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
930 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200931{
932 if (llist_empty(&peer->addresses))
933 return "(addressless)";
934
935 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
936 struct gtphub_peer_addr,
937 entry);
938 return gsn_addr_to_strb(&a->addr, buf, buflen);
939}
940
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100941static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
942 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200943{
944 if (!port)
945 return "(null port)";
946
947 snprintf(buf, buflen, "%s port %d",
948 gsn_addr_to_str(&port->peer_addr->addr),
949 (int)port->port);
950 return buf;
951}
952
953const char *gtphub_peer_str(struct gtphub_peer *peer)
954{
955 static char buf[256];
956 return gtphub_peer_strb(peer, buf, sizeof(buf));
957}
958
959const char *gtphub_peer_str2(struct gtphub_peer *peer)
960{
961 static char buf[256];
962 return gtphub_peer_strb(peer, buf, sizeof(buf));
963}
964
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100965const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200966{
967 static char buf[256];
968 return gtphub_port_strb(port, buf, sizeof(buf));
969}
970
971static const char *gtphub_port_str2(struct gtphub_peer_port *port)
972{
973 static char buf[256];
974 return gtphub_port_strb(port, buf, sizeof(buf));
975}
976
977static void gtphub_mapping_del_cb(struct expiring_item *expi)
978{
979 expi->del_cb = 0; /* avoid recursion loops */
980
981 struct nr_mapping *nrm = container_of(expi,
982 struct nr_mapping,
983 expiry_entry);
984 llist_del(&nrm->entry);
985 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
986
987 /* Just for log */
988 struct gtphub_peer_port *from = nrm->origin;
989 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100990 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200991 (int)nrm->expiry_entry.expiry,
992 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100993 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200994
995 gtphub_port_ref_count_dec(from);
996
997 talloc_free(nrm);
998}
999
1000static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1001 struct gtphub_peer_port *from,
1002 nr_t orig_nr,
1003 time_t now)
1004{
1005 struct nr_mapping *nrm;
1006
1007 nrm = nr_map_get(map, from, orig_nr);
1008
1009 if (!nrm) {
1010 nrm = gtphub_mapping_new();
1011 nrm->orig = orig_nr;
1012 nrm->origin = from;
1013 nr_map_add(map, nrm, now);
1014 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001015 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001016 gtphub_port_str(from),
1017 (int)(nrm->orig), (int)(nrm->repl));
1018 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001019 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001020 }
1021
1022 OSMO_ASSERT(nrm);
1023 return nrm;
1024}
1025
1026static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
1027 int plane_idx,
1028 struct gtphub_peer_port *from,
1029 uint32_t orig_tei,
1030 time_t now)
1031{
1032 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
1033 from, orig_tei, now);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001034 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001035 gtphub_plane_idx_names[plane_idx],
1036 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001037 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001038
1039 return (uint32_t)nrm->repl;
1040}
1041
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001042static void gtphub_map_seq(struct gtp_packet_desc *p,
1043 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001044 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001045{
1046 /* Store a mapping in to_peer's map, so when we later receive a GTP
1047 * packet back from to_peer, the seq nr can be unmapped back to its
1048 * origin (from_peer here). */
1049 struct nr_mapping *nrm;
1050 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001051 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001052
1053 /* Change the GTP packet to yield the new, mapped seq nr */
1054 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001055}
1056
1057static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1058 struct gtphub_peer_port *responding_port)
1059{
1060 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001061 struct nr_mapping *nrm =
1062 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1063 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001064 if (!nrm)
1065 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001066 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1067 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001068 set_seq(p, nrm->orig);
1069 return nrm->origin;
1070}
1071
1072static void gtphub_check_restart_counter(struct gtphub *hub,
1073 struct gtp_packet_desc *p,
1074 struct gtphub_peer_port *from)
1075{
1076 /* TODO */
1077 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1078 * that doesn't match the peer's previously sent restart counter, clear
1079 * that peer and cancel PDP contexts. */
1080}
1081
1082static void gtphub_map_restart_counter(struct gtphub *hub,
1083 struct gtp_packet_desc *p,
1084 struct gtphub_peer_port *from,
1085 struct gtphub_peer_port *to)
1086{
1087 /* TODO */
1088}
1089
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001090static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1091 struct gtphub *hub,
1092 struct gtp_packet_desc *p,
1093 struct gtphub_peer_port *from_port)
1094{
1095 OSMO_ASSERT(p->version == 1);
1096 *to_port_p = NULL;
1097
1098 /* If the header's TEI is zero, no PDP context has been established
1099 * yet. If nonzero, a mapping should actually already exist for this
1100 * TEI, since it must have been announced in a PDP context creation. */
1101 uint32_t tei = p->header_tei;
1102 if (!tei)
1103 return 0;
1104
1105 /* to_peer has previously announced a TEI, which was stored and
1106 * mapped in from_peer's tei_map. */
1107 struct nr_mapping *nrm;
1108 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1109 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001110 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1111 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001112 return -1;
1113 }
1114
1115 struct gtphub_peer_port *to_port = nrm->origin;
1116 uint32_t unmapped_tei = nrm->orig;
1117 set_tei(p, unmapped_tei);
1118
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001119 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1120 gtphub_port_str(from_port),
1121 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001122 gtphub_port_str2(to_port));
1123
1124 *to_port_p = to_port;
1125 return 0;
1126}
1127
1128/* Read GSN address IEs from p, and make sure these peer addresses exist in
1129 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1130 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1131 * the packet p. */
1132static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001133 struct gtphub_bind from_bind_arr[],
1134 struct gtphub_bind to_bind_arr[],
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001135 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001136{
1137 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1138
1139 int rc;
1140 int plane_idx;
1141
1142 switch (p->type) {
1143 case GTP_CREATE_PDP_REQ:
1144 case GTP_CREATE_PDP_RSP:
1145 /* Go for it below */
1146 break;
1147 default:
1148 /* Nothing to do for this message type. */
1149 return 0;
1150 }
1151
1152 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1153
1154 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1155 plane_nrs_match_GSN_addr_IE_indices);
1156
1157 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1158 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1159
1160 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1161 struct gsn_addr addr_from_ie;
1162 uint32_t tei_from_ie;
1163 int ie_idx;
1164
1165 /* Fetch GSN Address and TEI from IEs */
1166 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1167 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001168 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1169 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001170 return -1;
1171 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001172 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001173 gtphub_plane_idx_names[plane_idx],
1174 gsn_addr_to_str(&addr_from_ie),
1175 addr_from_ie.len);
1176
1177 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1178 if (ie_idx < 0) {
1179 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001180 LOG(LOGL_ERROR,
1181 "Create PDP Context message invalid:"
1182 " missing IE %d\n",
1183 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001184 return -1;
1185 }
1186 tei_from_ie = 0;
1187 }
1188 else
1189 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1190
1191 /* Make sure an entry for this peer address with default port
1192 * exists */
1193 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001194 gtphub_port_have(hub, &from_bind_arr[plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001195 &addr_from_ie,
1196 gtphub_plane_idx_default_port[plane_idx]);
1197
1198 if (tei_from_ie) {
1199 /* Create TEI mapping and replace in GTP packet IE */
1200 uint32_t mapped_tei =
1201 gtphub_tei_mapping_have(hub, plane_idx,
1202 peer_from_ie,
1203 tei_from_ie,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001204 p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001205 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1206 }
1207
1208 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001209 rc = gsn_addr_put(&to_bind_arr[plane_idx].local_addr, p,
1210 plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001211 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001212 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1213 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001214 return -1;
1215 }
1216 }
1217
1218 return 0;
1219}
1220
1221static int gtphub_write(const struct osmo_fd *to,
1222 const struct osmo_sockaddr *to_addr,
1223 const uint8_t *buf, size_t buf_len)
1224{
1225 errno = 0;
1226 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1227 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001228 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001229
1230 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001231 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001232 return -EINVAL;
1233 }
1234
1235 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001236 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1237 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001238 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001239 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1240 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001241
1242 return 0;
1243}
1244
1245static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1246{
1247 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1248 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001249 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1250 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001251 if (!(what & BSC_FD_READ))
1252 return 0;
1253
1254 struct gtphub *hub = from_ggsns_ofd->data;
1255
1256 static uint8_t buf[4096];
1257 struct osmo_sockaddr from_addr;
1258 struct osmo_sockaddr to_addr;
1259 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001260 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001261 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001262
1263 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1264 if (len < 1)
1265 return 0;
1266
1267 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1268 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001269 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001270 if (len < 1)
1271 return 0;
1272
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001273 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001274}
1275
1276static int gtphub_unmap(struct gtphub *hub,
1277 struct gtp_packet_desc *p,
1278 struct gtphub_peer_port *from,
1279 struct gtphub_peer_port *to_proxy,
1280 struct gtphub_peer_port **final_unmapped,
1281 struct gtphub_peer_port **unmapped_from_seq,
1282 struct gtphub_peer_port **unmapped_from_tei)
1283{
1284 /* Always (try to) unmap sequence and TEI numbers, which need to be
1285 * replaced in the packet. Either way, give precedence to the proxy, if
1286 * configured. */
1287
1288 struct gtphub_peer_port *from_seq = NULL;
1289 struct gtphub_peer_port *from_tei = NULL;
1290 struct gtphub_peer_port *unmapped = NULL;
1291
1292 if (unmapped_from_seq)
1293 *unmapped_from_seq = from_seq;
1294 if (unmapped_from_tei)
1295 *unmapped_from_tei = from_tei;
1296 if (final_unmapped)
1297 *final_unmapped = unmapped;
1298
1299 from_seq = gtphub_unmap_seq(p, from);
1300
1301 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1302 return -1;
1303
1304 struct gtphub_peer *from_peer = from->peer_addr->peer;
1305 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001306 LOG(LOGL_DEBUG,
1307 "Seq unmap and TEI unmap yield two different peers."
1308 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001309 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1310 gtphub_plane_idx_names[p->plane_idx],
1311 gtphub_peer_str(from_peer),
1312 (int)p->seq,
1313 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001314 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001315 gtphub_port_str2(from_tei)
1316 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001317 }
1318 unmapped = (from_seq? from_seq : from_tei);
1319
1320 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001321 LOG(LOGL_NOTICE,
1322 "Unmap yields a different peer than the configured proxy."
1323 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001324 " unmapped: %s proxy: %s\n",
1325 gtphub_port_str(unmapped),
1326 gtphub_port_str2(to_proxy)
1327 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001328 }
1329 unmapped = (to_proxy? to_proxy : unmapped);
1330
1331 if (!unmapped) {
1332 /* Return no error, but returned pointers are all NULL. */
1333 return 0;
1334 }
1335
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001336 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001337 from_seq, from_tei, unmapped);
1338
1339 if (unmapped_from_seq)
1340 *unmapped_from_seq = from_seq;
1341 if (unmapped_from_tei)
1342 *unmapped_from_tei = from_tei;
1343 if (final_unmapped)
1344 *final_unmapped = unmapped;
1345 return 0;
1346}
1347
1348static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1349 uint16_t port,
1350 struct osmo_sockaddr *dst)
1351{
1352 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1353}
1354
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001355/* If p is an Echo request, replace p's data with the matching response and
1356 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1357 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001358static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1359 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001360{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001361 if (p->type != GTP_ECHO_REQ)
1362 return 0;
1363
1364 static uint8_t echo_response_data[14] = {
1365 0x32, /* flags */
1366 GTP_ECHO_RSP,
1367 0x00, 14 - 8, /* Length in network byte order */
1368 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1369 0, 0, /* Seq, to be replaced */
1370 0, 0, /* no extensions */
1371 0x0e, /* Recovery IE */
1372 0 /* Recovery counter, to be replaced */
1373 };
1374 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1375 uint8_t *recovery = &echo_response_data[13];
1376
1377 *seq = hton16(p->seq);
1378 *recovery = hub->restart_counter;
1379
1380 *reply_buf = echo_response_data;
1381
1382 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001383}
1384
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001385struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1386 const struct osmo_sockaddr *addr);
1387
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001388/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1389 * address to forward to. Return a pointer to the osmo_fd, but copy the
1390 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1391 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1392 * bytes to forward, 0 or less on failure. */
1393int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1394 unsigned int plane_idx,
1395 const struct osmo_sockaddr *from_addr,
1396 uint8_t *buf,
1397 size_t received,
1398 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001399 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001400 struct osmo_fd **to_ofd,
1401 struct osmo_sockaddr *to_addr)
1402{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001403 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1404 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
1405 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1406 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1407
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001408 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1409 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001410 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001411 gtphub_plane_idx_names[plane_idx],
1412 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001413
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414 static struct gtp_packet_desc p;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001415 gtp_decode(buf, received, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001416
1417 if (p.rc <= 0)
1418 return -1;
1419
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001420 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1421
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001422 int reply_len;
1423 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1424 if (reply_len > 0) {
1425 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001426 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001427 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001428
1429 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1430 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1431 reply_len);
1432 LOG(LOGL_DEBUG, "--> Echo response to GGSN: %d bytes to %s\n",
1433 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001434 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001435 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001436 if (reply_len < 0)
1437 return -1;
1438
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001439 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001440
1441 /* If a GGSN proxy is configured, check that it's indeed that proxy
1442 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1443 * gtphub, so no-one else is allowed to talk to us from that side. */
1444 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1445 if (ggsn) {
1446 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001447 LOG(LOGL_ERROR,
1448 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001449 " received on GGSN bind is from another sender:"
1450 " proxy: %s sender: %s\n",
1451 gtphub_port_str(ggsn),
1452 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001453 return -1;
1454 }
1455 }
1456
1457 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001458 /* Find a GGSN peer with a matching address. The sender's port
1459 * may in fact differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001460 ggsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001461 }
1462
1463 /* If any PDP context has been created, we already have an entry for
1464 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1465 * us about. */
1466 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001467 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1468 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001469 return -1;
1470 }
1471
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001472 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001473
1474 struct gtphub_peer_port *sgsn_from_seq;
1475 struct gtphub_peer_port *sgsn;
1476 if (gtphub_unmap(hub, &p, ggsn,
1477 hub->sgsn_proxy[plane_idx],
1478 &sgsn, &sgsn_from_seq,
1479 NULL /* not interested, got it in &sgsn already */
1480 )
1481 != 0) {
1482 return -1;
1483 }
1484
1485 if (!sgsn) {
1486 /* A GGSN initiated request would go to a known TEI. So this is
1487 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001488 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001489 return -1;
1490 }
1491
1492 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001493 /* This may be a Create PDP Context response. If it is, there
1494 * are other addresses in the GTP message to set up apart from
1495 * the sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001496 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001497 &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001498 != 0)
1499 return -1;
1500 }
1501
1502 gtphub_check_restart_counter(hub, &p, ggsn);
1503 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1504
1505 /* If the GGSN is replying to an SGSN request, the sequence nr has
1506 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1507 * create a new mapping. */
1508 if (!sgsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001509 gtphub_map_seq(&p, ggsn, sgsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001510
1511 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001512
1513 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001514
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001515 if (received) {
1516 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1517 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1518 received);
1519 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001520 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001521 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001522 return received;
1523}
1524
1525static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1526{
1527 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1528 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001529 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1530 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001531
1532 if (!(what & BSC_FD_READ))
1533 return 0;
1534
1535 struct gtphub *hub = from_sgsns_ofd->data;
1536
1537 static uint8_t buf[4096];
1538 struct osmo_sockaddr from_addr;
1539 struct osmo_sockaddr to_addr;
1540 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001541 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001542 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543
1544 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1545 if (len < 1)
1546 return 0;
1547
1548 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1549 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001550 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001551 if (len < 1)
1552 return 0;
1553
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001554 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001555}
1556
1557/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1558int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1559 unsigned int plane_idx,
1560 const struct osmo_sockaddr *from_addr,
1561 uint8_t *buf,
1562 size_t received,
1563 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001564 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001565 struct osmo_fd **to_ofd,
1566 struct osmo_sockaddr *to_addr)
1567{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001568 struct gtphub_bind *from_bind_arr = hub->to_sgsns;
1569 struct gtphub_bind *to_bind_arr = hub->to_ggsns;
1570 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1571 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1572
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001573 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1574 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001575 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001576 gtphub_plane_idx_names[plane_idx],
1577 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001578
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001579 static struct gtp_packet_desc p;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001580 gtp_decode(buf, received, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001581
1582 if (p.rc <= 0)
1583 return -1;
1584
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001585 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1586
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001587 int reply_len;
1588 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1589 if (reply_len > 0) {
1590 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001591 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001592 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001593
1594 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1595 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1596 reply_len);
1597 LOG(LOGL_DEBUG, "<-- Echo response to SGSN: %d bytes to %s\n",
1598 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001599 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001600 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001601 if (reply_len < 0)
1602 return -1;
1603
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001604 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001605
1606 /* If an SGSN proxy is configured, check that it's indeed that proxy
1607 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1608 * gtphub, so no-one else is allowed to talk to us from that side. */
1609 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1610 if (sgsn) {
1611 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001612 LOG(LOGL_ERROR,
1613 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001614 " received on GGSN bind is from another sender:"
1615 " proxy: %s sender: %s\n",
1616 gtphub_port_str(sgsn),
1617 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001618 return -1;
1619 }
1620 }
1621
1622 if (!sgsn) {
1623 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001624 * entry for this SGSN. The port may differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001625 sgsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001626 }
1627
1628 if (!sgsn) {
1629 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1630 * first contact without being known yet, so create the peer
1631 * struct for the current sender. */
1632 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001633 LOG(LOGL_ERROR,
1634 "User plane peer was not announced by PDP Context,"
1635 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001636 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001637 return -1;
1638 }
1639
1640 struct gsn_addr from_gsna;
1641 uint16_t from_port;
1642 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1643 return -1;
1644
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001645 sgsn = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001646 }
1647
1648 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001649 /* This could theoretically happen for invalid address data or
1650 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001651 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1652 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001653 return -1;
1654 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001655 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001656
1657 struct gtphub_peer_port *ggsn_from_seq;
1658 struct gtphub_peer_port *ggsn;
1659 if (gtphub_unmap(hub, &p, sgsn,
1660 hub->ggsn_proxy[plane_idx],
1661 &ggsn, &ggsn_from_seq,
1662 NULL /* not interested, got it in &ggsn already */
1663 )
1664 != 0) {
1665 return -1;
1666 }
1667
Neels Hofmeyra208c732015-11-11 19:26:09 +01001668 if (!ggsn) {
1669 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1670 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001671 }
1672
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001673 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001674 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001675 return -1;
1676 }
1677
1678 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001679 /* This may be a Create PDP Context requst. If it is, there are
1680 * other addresses in the GTP message to set up apart from the
1681 * sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001682 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001683 &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001684 != 0)
1685 return -1;
1686 }
1687
1688 gtphub_check_restart_counter(hub, &p, sgsn);
1689 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1690
1691 /* If the SGSN is replying to a GGSN request, the sequence nr has
1692 * already been unmapped above (unmap_ggsn != NULL), and we need not
1693 * create a new outgoing sequence map. */
1694 if (!ggsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001695 gtphub_map_seq(&p, sgsn, ggsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001696
1697 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1698
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001699 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001700
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001701 if (received) {
1702 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1703 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1704 received);
1705 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001706 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001707 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001708 return received;
1709}
1710
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001711static void resolved_gssn_del_cb(struct expiring_item *expi)
1712{
1713 struct gtphub_resolved_ggsn *ggsn;
1714 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1715
1716 gtphub_port_ref_count_dec(ggsn->peer);
1717 llist_del(&ggsn->entry);
1718
1719 ggsn->expiry_entry.del_cb = 0;
1720 expiring_item_del(&ggsn->expiry_entry);
1721
1722 talloc_free(ggsn);
1723}
1724
1725void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1726 struct gsn_addr *resolved_addr,
1727 time_t now)
1728{
1729 struct gtphub_peer_port *pp;
1730 struct gtphub_resolved_ggsn *ggsn;
1731
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001732 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001733 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1734 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001735
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001736 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1737 resolved_addr, 2123);
1738 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001739 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1740 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001741 return;
1742 }
1743
1744 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1745 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001746 INIT_LLIST_HEAD(&ggsn->entry);
1747 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001748
1749 ggsn->peer = pp;
1750 gtphub_port_ref_count_inc(pp);
1751
1752 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001753 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001754
1755 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1756 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1757
1758 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1759}
1760
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001761static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1762{
1763 return pp->ref_count == 0;
1764}
1765
1766static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1767{
1768 struct gtphub_peer_port *pp, *npp;
1769 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1770 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001771 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001772 gtphub_port_str(pp));
1773 gtphub_peer_port_del(pp);
1774 }
1775 }
1776 return llist_empty(&pa->ports);
1777}
1778
1779static int gtphub_gc_peer(struct gtphub_peer *p)
1780{
1781 struct gtphub_peer_addr *pa, *npa;
1782 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1783 if (gtphub_gc_peer_addr(pa)) {
1784 gtphub_peer_addr_del(pa);
1785 }
1786 }
1787
1788 /* Note that there's a ref_count in each gtphub_peer_port instance
1789 * listed within p->addresses, referenced by TEI mappings from
1790 * hub->tei_map. As long as those don't expire, this peer will stay. */
1791
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001792 return llist_empty(&p->addresses)
1793 && nr_map_empty(&p->seq_map);
1794}
1795
1796static void gtphub_gc_bind(struct gtphub_bind *b)
1797{
1798 struct gtphub_peer *p, *n;
1799 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1800 if (gtphub_gc_peer(p)) {
1801 gtphub_peer_del(p);
1802 }
1803 }
1804}
1805
1806void gtphub_gc(struct gtphub *hub, time_t now)
1807{
1808 int expired;
1809 expired = expiry_tick(&hub->expire_seq_maps, now);
1810 expired += expiry_tick(&hub->expire_tei_maps, now);
1811
1812 /* ... */
1813
1814 if (expired) {
1815 int i;
1816 for (i = 0; i < GTPH_PLANE_N; i++) {
1817 gtphub_gc_bind(&hub->to_sgsns[i]);
1818 gtphub_gc_bind(&hub->to_ggsns[i]);
1819 }
1820 }
1821}
1822
1823static void gtphub_gc_cb(void *data)
1824{
1825 struct gtphub *hub = data;
1826 gtphub_gc(hub, gtphub_now());
1827 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1828}
1829
1830static void gtphub_gc_start(struct gtphub *hub)
1831{
1832 hub->gc_timer.cb = gtphub_gc_cb;
1833 hub->gc_timer.data = hub;
1834
1835 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1836}
1837
1838/* called by unit tests */
1839void gtphub_init(struct gtphub *hub)
1840{
1841 gtphub_zero(hub);
1842
1843 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001844 expiry_init(&hub->expire_tei_maps,
1845 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001846
1847 int plane_idx;
1848 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001849 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001850 nr_map_init(&hub->tei_map[plane_idx],
1851 &hub->tei_pool[plane_idx],
1852 &hub->expire_tei_maps);
1853
1854 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1855 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1856 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001857
1858 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1859 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1860 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1861 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001862}
1863
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001864/* For the test suite, this is kept separate from gtphub_stop(), which also
1865 * closes sockets. The test suite avoids using sockets and would cause
1866 * segfaults when trying to close uninitialized ofds. */
1867void gtphub_free(struct gtphub *hub)
1868{
1869 /* By expiring all mappings, a garbage collection should free
1870 * everything else. A gtphub_bind_free() will assert that everything is
1871 * indeed empty. */
1872 expiry_clear(&hub->expire_seq_maps);
1873 expiry_clear(&hub->expire_tei_maps);
1874
1875 int plane_idx;
1876 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1877 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
1878 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
1879
1880 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
1881 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
1882 }
1883}
1884
1885void gtphub_stop(struct gtphub *hub)
1886{
1887 int plane_idx;
1888 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1889 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
1890 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
1891 }
1892 gtphub_free(hub);
1893}
1894
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001895static int gtphub_make_proxy(struct gtphub *hub,
1896 struct gtphub_peer_port **pp,
1897 struct gtphub_bind *bind,
1898 const struct gtphub_cfg_addr *addr)
1899{
1900 if (!addr->addr_str)
1901 return 0;
1902
1903 struct gsn_addr gsna;
1904 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1905 return -1;
1906
1907 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1908
1909 /* This is *the* proxy. Make sure it is never expired. */
1910 gtphub_port_ref_count_inc(*pp);
1911 return 0;
1912}
1913
1914int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1915{
1916 int rc;
1917
1918 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001919
1920 /* If a Ctrl plane proxy is configured, ares will never be used. */
1921 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1922 if (gtphub_ares_init(hub) != 0) {
1923 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1924 return -1;
1925 }
1926 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001927
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001928 /* TODO set hub->restart_counter from external file. */
1929
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001930 int plane_idx;
1931 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1932 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1933 &cfg->to_ggsns[plane_idx],
1934 from_ggsns_read_cb, hub, plane_idx);
1935 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001936 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1937 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001938 return rc;
1939 }
1940
1941 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1942 &cfg->to_sgsns[plane_idx],
1943 from_sgsns_read_cb, hub, plane_idx);
1944 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001945 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1946 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001947 return rc;
1948 }
1949 }
1950
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001951 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1952 if (gtphub_make_proxy(hub,
1953 &hub->sgsn_proxy[plane_idx],
1954 &hub->to_sgsns[plane_idx],
1955 &cfg->sgsn_proxy[plane_idx])
1956 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001957 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001958 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001959 cfg->sgsn_proxy[plane_idx].addr_str,
1960 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001961 return -1;
1962 }
1963 if (gtphub_make_proxy(hub,
1964 &hub->ggsn_proxy[plane_idx],
1965 &hub->to_ggsns[plane_idx],
1966 &cfg->ggsn_proxy[plane_idx])
1967 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001968 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001969 return -1;
1970 }
1971 }
1972
1973 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1974 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001975 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001976 gtphub_plane_idx_names[plane_idx],
1977 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1978 }
1979
1980 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001981 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001982 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001983 gtphub_plane_idx_names[plane_idx],
1984 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1985 }
1986
1987 gtphub_gc_start(hub);
1988 return 0;
1989}
1990
1991static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1992 const struct gsn_addr *addr)
1993{
1994 struct gtphub_peer_addr *a;
1995 llist_for_each_entry(a, &peer->addresses, entry) {
1996 if (gsn_addr_same(&a->addr, addr))
1997 return a;
1998 }
1999 return NULL;
2000}
2001
2002static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2003 uint16_t port)
2004{
2005 OSMO_ASSERT(port);
2006 struct gtphub_peer_port *pp;
2007 llist_for_each_entry(pp, &a->ports, entry) {
2008 if (pp->port == port)
2009 return pp;
2010 }
2011 return NULL;
2012}
2013
2014static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2015 const struct gsn_addr *addr)
2016{
2017 struct gtphub_peer *peer;
2018 llist_for_each_entry(peer, &bind->peers, entry) {
2019 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2020 if (a)
2021 return a;
2022 }
2023 return NULL;
2024}
2025
2026static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2027 const struct gsn_addr *addr,
2028 uint16_t port)
2029{
2030 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2031 if (!a)
2032 return NULL;
2033 return gtphub_addr_find_port(a, port);
2034}
2035
2036struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2037 const struct osmo_sockaddr *addr)
2038{
2039 struct gsn_addr gsna;
2040 uint16_t port;
2041 gsn_addr_from_sockaddr(&gsna, &port, addr);
2042 return gtphub_port_find(bind, &gsna, port);
2043}
2044
2045static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2046 struct gtphub_bind *bind)
2047{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002048 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2049 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002050 OSMO_ASSERT(peer);
2051
2052 INIT_LLIST_HEAD(&peer->addresses);
2053
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002054 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002055 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
2056
2057 /* TODO use something random to pick the initial sequence nr.
2058 0x6d31 produces the ASCII character sequence 'm1', currently used in
2059 gtphub_nc_test.sh. */
2060 peer->seq_pool.last_nr = 0x6d31 - 1;
2061
2062 llist_add(&peer->entry, &bind->peers);
2063 return peer;
2064}
2065
2066static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2067 const struct gsn_addr *addr)
2068{
2069 struct gtphub_peer_addr *a;
2070 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2071 OSMO_ASSERT(a);
2072 a->peer = peer;
2073 gsn_addr_copy(&a->addr, addr);
2074 INIT_LLIST_HEAD(&a->ports);
2075 llist_add(&a->entry, &peer->addresses);
2076
2077 return a;
2078}
2079
2080static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2081 struct gtphub_bind *bind,
2082 const struct gsn_addr *addr)
2083{
2084 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2085 if (a)
2086 return a;
2087
2088 /* If we haven't found an address, that means we need to create an
2089 * entirely new peer for the new address. More addresses may be added
2090 * to this peer later, but not via this function. */
2091 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002092
2093 a = gtphub_peer_add_addr(peer, addr);
2094
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002095 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002096 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002097 gsn_addr_to_str(&a->addr));
2098
2099 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002100}
2101
2102static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2103 uint16_t port)
2104{
2105 struct gtphub_peer_port *pp;
2106
2107 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2108 OSMO_ASSERT(pp);
2109 pp->peer_addr = a;
2110 pp->port = port;
2111
2112 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2113 talloc_free(pp);
2114 return NULL;
2115 }
2116
2117 llist_add(&pp->entry, &a->ports);
2118
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002119 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002120 gsn_addr_to_str(&a->addr),
2121 (int)port);
2122
2123 return pp;
2124}
2125
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002126struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2127 struct gtphub_bind *bind,
2128 const struct gsn_addr *addr,
2129 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002130{
2131 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2132
2133 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2134 if (pp)
2135 return pp;
2136
2137 return gtphub_addr_add_port(a, port);
2138}
2139
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002140/* Find a GGSN peer with a matching address. If the address is known but the
2141 * port not, create a new port for that peer address. */
2142struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2143 const struct osmo_sockaddr *addr)
2144{
2145 struct gtphub_peer_addr *pa;
2146 struct gtphub_peer_port *pp;
2147
2148 struct gsn_addr gsna;
2149 uint16_t port;
2150 gsn_addr_from_sockaddr(&gsna, &port, addr);
2151
2152 pa = gtphub_addr_find(bind, &gsna);
2153 if (!pa)
2154 return NULL;
2155
2156 pp = gtphub_addr_find_port(pa, port);
2157
2158 if (!pp)
2159 pp = gtphub_addr_add_port(pa, port);
2160
2161 return pp;
2162}
2163
2164
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002165/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2166 * resolution should be possible but failed, and 1 if resolution was
2167 * successful. *pp will be set to NULL if <1 is returned. */
2168static int gtphub_resolve_ggsn(struct gtphub *hub,
2169 struct gtp_packet_desc *p,
2170 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002171{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002172 *pp = NULL;
2173
2174 /* TODO determine from message type whether IEs should be present? */
2175
2176 int rc;
2177 const char *imsi_str;
2178 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2179 if (rc < 1)
2180 return rc;
2181 OSMO_ASSERT(imsi_str);
2182
2183 const char *apn_str;
2184 rc = get_ie_apn_str(p->ie, &apn_str);
2185 if (rc < 1)
2186 return rc;
2187 OSMO_ASSERT(apn_str);
2188
2189 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2190 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002191}
2192
2193
2194/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002195/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002196/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2197 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002198static int _osmo_getaddrinfo(struct addrinfo **result,
2199 uint16_t family, uint16_t type, uint8_t proto,
2200 const char *host, uint16_t port)
2201{
2202 struct addrinfo hints;
2203 char portbuf[16];
2204
2205 sprintf(portbuf, "%u", port);
2206 memset(&hints, '\0', sizeof(struct addrinfo));
2207 hints.ai_family = family;
2208 if (type == SOCK_RAW) {
2209 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2210 * SOCK_RAW and IPPROTO_GRE is used.
2211 */
2212 hints.ai_socktype = SOCK_DGRAM;
2213 hints.ai_protocol = IPPROTO_UDP;
2214 } else {
2215 hints.ai_socktype = type;
2216 hints.ai_protocol = proto;
2217 }
2218
2219 return getaddrinfo(host, portbuf, &hints, result);
2220}
2221
2222/* TODO move to osmocom/core/socket.c ? */
2223int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2224 uint16_t family, uint16_t type, uint8_t proto,
2225 const char *host, uint16_t port)
2226{
2227 struct addrinfo *res;
2228 int rc;
2229 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2230
2231 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002232 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002233 return -EINVAL;
2234 }
2235
2236 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2237 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2238 addr->l = res->ai_addrlen;
2239 freeaddrinfo(res);
2240
2241 return 0;
2242}
2243
2244int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2245 char *port_str, size_t port_str_len,
2246 const struct osmo_sockaddr *addr,
2247 int flags)
2248{
2249 int rc;
2250
2251 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2252 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2253 return -1;
2254 }
2255
2256 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002257 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2258 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002259 return -1;
2260 }
2261
2262 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2263 addr_str, addr_str_len,
2264 port_str, port_str_len,
2265 flags);
2266
2267 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002268 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2269 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2270 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002271
2272 return rc;
2273}
2274
2275const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2276 char *buf, size_t buf_len)
2277{
2278 const int portbuf_len = 6;
2279 OSMO_ASSERT(buf_len > portbuf_len);
2280 char *portbuf = buf + buf_len - portbuf_len;
2281 buf_len -= portbuf_len;
2282 if (osmo_sockaddr_to_strs(buf, buf_len,
2283 portbuf, portbuf_len,
2284 addr,
2285 NI_NUMERICHOST | NI_NUMERICSERV))
2286 return NULL;
2287
2288 char *pos = buf + strnlen(buf, buf_len-1);
2289 size_t len = buf_len - (pos - buf);
2290
2291 snprintf(pos, len, " port %s", portbuf);
2292 buf[buf_len-1] = '\0';
2293
2294 return buf;
2295}
2296
2297const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2298{
2299 static char buf[256];
2300 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2301 if (! result)
2302 return "(invalid)";
2303 return result;
2304}
2305
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002306int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2307 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002308{
2309 if (a == b)
2310 return 0;
2311 if (!a)
2312 return -1;
2313 if (!b)
2314 return 1;
2315 if (a->l != b->l) {
2316 /* Lengths are not the same, but determine the order. Will
2317 * anyone ever sort a list by osmo_sockaddr though...? */
2318 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2319 if (cmp == 0) {
2320 if (a->l < b->l)
2321 return -1;
2322 else
2323 return 1;
2324 }
2325 return cmp;
2326 }
2327 return memcmp(&a->a, &b->a, a->l);
2328}
2329
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002330void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2331 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002332{
2333 OSMO_ASSERT(src->l <= sizeof(dst->a));
2334 memcpy(&dst->a, &src->a, src->l);
2335 dst->l = src->l;
2336}