blob: 97ccf61ab9e62471299dfb92dae80fb76c2457b0 [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);
663 if (map->add_items_to_expiry)
664 expiry_add(map->add_items_to_expiry,
665 &mapping->expiry_entry,
666 now);
667}
668
669void nr_map_clear(struct nr_map *map)
670{
671 struct nr_mapping *m;
672 struct nr_mapping *n;
673 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
674 nr_mapping_del(m);
675 }
676}
677
678int nr_map_empty(const struct nr_map *map)
679{
680 return llist_empty(&map->mappings);
681}
682
683struct nr_mapping *nr_map_get(const struct nr_map *map,
684 void *origin, nr_t nr_orig)
685{
686 struct nr_mapping *mapping;
687 llist_for_each_entry(mapping, &map->mappings, entry) {
688 if ((mapping->origin == origin)
689 && (mapping->orig == nr_orig))
690 return mapping;
691 }
692 /* Not found. */
693 return NULL;
694}
695
696struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
697{
698 struct nr_mapping *mapping;
699 llist_for_each_entry(mapping, &map->mappings, entry) {
700 if (mapping->repl == nr_repl) {
701 return mapping;
702 }
703 }
704 /* Not found. */
705 return NULL;
706}
707
708void nr_mapping_del(struct nr_mapping *mapping)
709{
710 OSMO_ASSERT(mapping);
711 llist_del(&mapping->entry);
712 INIT_LLIST_HEAD(&mapping->entry);
713 expiring_item_del(&mapping->expiry_entry);
714}
715
716
717/* gtphub */
718
719const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
720 "CTRL",
721 "USER",
722};
723
724const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
725 2123,
726 2152,
727};
728
729time_t gtphub_now(void)
730{
731 struct timespec now_tp;
732 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
733 return now_tp.tv_sec;
734}
735
736/* Remove a gtphub_peer from its list and free it. */
737static void gtphub_peer_del(struct gtphub_peer *peer)
738{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100739 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200740 nr_map_clear(&peer->seq_map);
741 llist_del(&peer->entry);
742 talloc_free(peer);
743}
744
745static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
746{
747 OSMO_ASSERT(llist_empty(&pa->ports));
748 llist_del(&pa->entry);
749 talloc_free(pa);
750}
751
752static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
753{
754 OSMO_ASSERT(pp->ref_count == 0);
755 llist_del(&pp->entry);
756 talloc_free(pp);
757}
758
759/* From the information in the gtp_packet_desc, return the address of a GGSN.
760 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100761static int gtphub_resolve_ggsn(struct gtphub *hub,
762 struct gtp_packet_desc *p,
763 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200764
765/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100766struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
767 const char *imsi_str,
768 const char *apn_ni_str);
769int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200770
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200771static void gtphub_zero(struct gtphub *hub)
772{
773 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100774 INIT_LLIST_HEAD(&hub->ggsn_lookups);
775 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200776}
777
778static int gtphub_sock_init(struct osmo_fd *ofd,
779 const struct gtphub_cfg_addr *addr,
780 osmo_fd_cb_t cb,
781 void *data,
782 int ofd_id)
783{
784 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100785 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200786 return -1;
787 }
788 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100789 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200790 return -1;
791 }
792
793 ofd->when = BSC_FD_READ;
794 ofd->cb = cb;
795 ofd->data = data;
796 ofd->priv_nr = ofd_id;
797
798 int rc;
799 rc = osmo_sock_init_ofd(ofd,
800 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
801 addr->addr_str, addr->port,
802 OSMO_SOCK_F_BIND);
803 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100804 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
805 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806 return -1;
807 }
808
809 return 0;
810}
811
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100812static void gtphub_sock_close(struct osmo_fd *ofd)
813{
814 close(ofd->fd);
815 osmo_fd_unregister(ofd);
816 ofd->cb = NULL;
817}
818
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200819static void gtphub_bind_init(struct gtphub_bind *b)
820{
821 ZERO_STRUCT(b);
822
823 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100824
825 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
826 &gtphub_ctrg_io_desc, 0);
827 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200828}
829
830static int gtphub_bind_start(struct gtphub_bind *b,
831 const struct gtphub_cfg_bind *cfg,
832 osmo_fd_cb_t cb, void *cb_data,
833 unsigned int ofd_id)
834{
835 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
836 return -1;
837 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
838 return -1;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100839 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200840 return 0;
841}
842
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100843static void gtphub_bind_free(struct gtphub_bind *b)
844{
845 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100846 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100847}
848
849static void gtphub_bind_stop(struct gtphub_bind *b) {
850 gtphub_sock_close(&b->ofd);
851 gtphub_bind_free(b);
852}
853
Neels Hofmeyr40348972015-11-17 12:22:28 +0100854/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200855 * Return the number of bytes read, zero on error. */
856static int gtphub_read(const struct osmo_fd *from,
857 struct osmo_sockaddr *from_addr,
858 uint8_t *buf, size_t buf_len)
859{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100860 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200861
Neels Hofmeyr40348972015-11-17 12:22:28 +0100862 /* recvfrom requires the available length set in *from_addr_len. */
863 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200864 errno = 0;
865 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100866 (struct sockaddr*)&from_addr->a,
867 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200868 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
869 * is not truncated. Then maybe reduce buf's size. */
870
871 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100872 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
873 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200874 return 0;
875 }
876
Neels Hofmeyr40348972015-11-17 12:22:28 +0100877 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
878 (int)received, osmo_sockaddr_to_str(from_addr),
879 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200881 return received;
882}
883
884inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
885{
886 OSMO_ASSERT(pp->ref_count < UINT_MAX);
887 pp->ref_count++;
888}
889
890inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
891{
892 OSMO_ASSERT(pp->ref_count > 0);
893 pp->ref_count--;
894}
895
896inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
897{
898 OSMO_ASSERT(p->version == 1);
899 p->data->gtp1l.h.seq = hton16(seq);
900 p->seq = seq;
901}
902
903inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
904{
905 OSMO_ASSERT(p->version == 1);
906 p->data->gtp1l.h.tei = hton32(tei);
907 p->header_tei = tei;
908}
909
910static void gtphub_mapping_del_cb(struct expiring_item *expi);
911
912static struct nr_mapping *gtphub_mapping_new()
913{
914 struct nr_mapping *nrm;
915 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
916 OSMO_ASSERT(nrm);
917
918 nr_mapping_init(nrm);
919 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
920 return nrm;
921}
922
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100923static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
924 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200925{
926 if (llist_empty(&peer->addresses))
927 return "(addressless)";
928
929 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
930 struct gtphub_peer_addr,
931 entry);
932 return gsn_addr_to_strb(&a->addr, buf, buflen);
933}
934
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100935static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
936 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200937{
938 if (!port)
939 return "(null port)";
940
941 snprintf(buf, buflen, "%s port %d",
942 gsn_addr_to_str(&port->peer_addr->addr),
943 (int)port->port);
944 return buf;
945}
946
947const char *gtphub_peer_str(struct gtphub_peer *peer)
948{
949 static char buf[256];
950 return gtphub_peer_strb(peer, buf, sizeof(buf));
951}
952
953const char *gtphub_peer_str2(struct gtphub_peer *peer)
954{
955 static char buf[256];
956 return gtphub_peer_strb(peer, buf, sizeof(buf));
957}
958
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100959const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200960{
961 static char buf[256];
962 return gtphub_port_strb(port, buf, sizeof(buf));
963}
964
965static const char *gtphub_port_str2(struct gtphub_peer_port *port)
966{
967 static char buf[256];
968 return gtphub_port_strb(port, buf, sizeof(buf));
969}
970
971static void gtphub_mapping_del_cb(struct expiring_item *expi)
972{
973 expi->del_cb = 0; /* avoid recursion loops */
974
975 struct nr_mapping *nrm = container_of(expi,
976 struct nr_mapping,
977 expiry_entry);
978 llist_del(&nrm->entry);
979 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
980
981 /* Just for log */
982 struct gtphub_peer_port *from = nrm->origin;
983 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100984 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200985 (int)nrm->expiry_entry.expiry,
986 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100987 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200988
989 gtphub_port_ref_count_dec(from);
990
991 talloc_free(nrm);
992}
993
994static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
995 struct gtphub_peer_port *from,
996 nr_t orig_nr,
997 time_t now)
998{
999 struct nr_mapping *nrm;
1000
1001 nrm = nr_map_get(map, from, orig_nr);
1002
1003 if (!nrm) {
1004 nrm = gtphub_mapping_new();
1005 nrm->orig = orig_nr;
1006 nrm->origin = from;
1007 nr_map_add(map, nrm, now);
1008 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001009 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001010 gtphub_port_str(from),
1011 (int)(nrm->orig), (int)(nrm->repl));
1012 } else {
1013 /* restart expiry timeout */
1014 expiry_add(map->add_items_to_expiry, &nrm->expiry_entry,
1015 now);
1016 }
1017
1018 OSMO_ASSERT(nrm);
1019 return nrm;
1020}
1021
1022static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
1023 int plane_idx,
1024 struct gtphub_peer_port *from,
1025 uint32_t orig_tei,
1026 time_t now)
1027{
1028 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
1029 from, orig_tei, now);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001030 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001031 gtphub_plane_idx_names[plane_idx],
1032 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001033 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001034
1035 return (uint32_t)nrm->repl;
1036}
1037
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001038static void gtphub_map_seq(struct gtp_packet_desc *p,
1039 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001040 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001041{
1042 /* Store a mapping in to_peer's map, so when we later receive a GTP
1043 * packet back from to_peer, the seq nr can be unmapped back to its
1044 * origin (from_peer here). */
1045 struct nr_mapping *nrm;
1046 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001047 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001048
1049 /* Change the GTP packet to yield the new, mapped seq nr */
1050 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001051}
1052
1053static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1054 struct gtphub_peer_port *responding_port)
1055{
1056 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001057 struct nr_mapping *nrm =
1058 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1059 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001060 if (!nrm)
1061 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001062 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1063 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001064 set_seq(p, nrm->orig);
1065 return nrm->origin;
1066}
1067
1068static void gtphub_check_restart_counter(struct gtphub *hub,
1069 struct gtp_packet_desc *p,
1070 struct gtphub_peer_port *from)
1071{
1072 /* TODO */
1073 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1074 * that doesn't match the peer's previously sent restart counter, clear
1075 * that peer and cancel PDP contexts. */
1076}
1077
1078static void gtphub_map_restart_counter(struct gtphub *hub,
1079 struct gtp_packet_desc *p,
1080 struct gtphub_peer_port *from,
1081 struct gtphub_peer_port *to)
1082{
1083 /* TODO */
1084}
1085
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001086static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1087 struct gtphub *hub,
1088 struct gtp_packet_desc *p,
1089 struct gtphub_peer_port *from_port)
1090{
1091 OSMO_ASSERT(p->version == 1);
1092 *to_port_p = NULL;
1093
1094 /* If the header's TEI is zero, no PDP context has been established
1095 * yet. If nonzero, a mapping should actually already exist for this
1096 * TEI, since it must have been announced in a PDP context creation. */
1097 uint32_t tei = p->header_tei;
1098 if (!tei)
1099 return 0;
1100
1101 /* to_peer has previously announced a TEI, which was stored and
1102 * mapped in from_peer's tei_map. */
1103 struct nr_mapping *nrm;
1104 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1105 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001106 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1107 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001108 return -1;
1109 }
1110
1111 struct gtphub_peer_port *to_port = nrm->origin;
1112 uint32_t unmapped_tei = nrm->orig;
1113 set_tei(p, unmapped_tei);
1114
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001115 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1116 gtphub_port_str(from_port),
1117 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001118 gtphub_port_str2(to_port));
1119
1120 *to_port_p = to_port;
1121 return 0;
1122}
1123
1124/* Read GSN address IEs from p, and make sure these peer addresses exist in
1125 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1126 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1127 * the packet p. */
1128static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001129 struct gtphub_bind from_bind_arr[],
1130 struct gtphub_bind to_bind_arr[],
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001131 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001132{
1133 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1134
1135 int rc;
1136 int plane_idx;
1137
1138 switch (p->type) {
1139 case GTP_CREATE_PDP_REQ:
1140 case GTP_CREATE_PDP_RSP:
1141 /* Go for it below */
1142 break;
1143 default:
1144 /* Nothing to do for this message type. */
1145 return 0;
1146 }
1147
1148 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1149
1150 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1151 plane_nrs_match_GSN_addr_IE_indices);
1152
1153 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1154 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1155
1156 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1157 struct gsn_addr addr_from_ie;
1158 uint32_t tei_from_ie;
1159 int ie_idx;
1160
1161 /* Fetch GSN Address and TEI from IEs */
1162 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1163 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001164 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1165 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001166 return -1;
1167 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001168 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001169 gtphub_plane_idx_names[plane_idx],
1170 gsn_addr_to_str(&addr_from_ie),
1171 addr_from_ie.len);
1172
1173 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1174 if (ie_idx < 0) {
1175 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001176 LOG(LOGL_ERROR,
1177 "Create PDP Context message invalid:"
1178 " missing IE %d\n",
1179 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001180 return -1;
1181 }
1182 tei_from_ie = 0;
1183 }
1184 else
1185 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1186
1187 /* Make sure an entry for this peer address with default port
1188 * exists */
1189 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001190 gtphub_port_have(hub, &from_bind_arr[plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001191 &addr_from_ie,
1192 gtphub_plane_idx_default_port[plane_idx]);
1193
1194 if (tei_from_ie) {
1195 /* Create TEI mapping and replace in GTP packet IE */
1196 uint32_t mapped_tei =
1197 gtphub_tei_mapping_have(hub, plane_idx,
1198 peer_from_ie,
1199 tei_from_ie,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001200 p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001201 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1202 }
1203
1204 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001205 rc = gsn_addr_put(&to_bind_arr[plane_idx].local_addr, p,
1206 plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001207 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001208 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1209 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001210 return -1;
1211 }
1212 }
1213
1214 return 0;
1215}
1216
1217static int gtphub_write(const struct osmo_fd *to,
1218 const struct osmo_sockaddr *to_addr,
1219 const uint8_t *buf, size_t buf_len)
1220{
1221 errno = 0;
1222 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1223 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001224 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001225
1226 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001227 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001228 return -EINVAL;
1229 }
1230
1231 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001232 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1233 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001234 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001235 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1236 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001237
1238 return 0;
1239}
1240
1241static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1242{
1243 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1244 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001245 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1246 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001247 if (!(what & BSC_FD_READ))
1248 return 0;
1249
1250 struct gtphub *hub = from_ggsns_ofd->data;
1251
1252 static uint8_t buf[4096];
1253 struct osmo_sockaddr from_addr;
1254 struct osmo_sockaddr to_addr;
1255 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001256 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001257 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001258
1259 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1260 if (len < 1)
1261 return 0;
1262
1263 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1264 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001265 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001266 if (len < 1)
1267 return 0;
1268
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001269 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001270}
1271
1272static int gtphub_unmap(struct gtphub *hub,
1273 struct gtp_packet_desc *p,
1274 struct gtphub_peer_port *from,
1275 struct gtphub_peer_port *to_proxy,
1276 struct gtphub_peer_port **final_unmapped,
1277 struct gtphub_peer_port **unmapped_from_seq,
1278 struct gtphub_peer_port **unmapped_from_tei)
1279{
1280 /* Always (try to) unmap sequence and TEI numbers, which need to be
1281 * replaced in the packet. Either way, give precedence to the proxy, if
1282 * configured. */
1283
1284 struct gtphub_peer_port *from_seq = NULL;
1285 struct gtphub_peer_port *from_tei = NULL;
1286 struct gtphub_peer_port *unmapped = NULL;
1287
1288 if (unmapped_from_seq)
1289 *unmapped_from_seq = from_seq;
1290 if (unmapped_from_tei)
1291 *unmapped_from_tei = from_tei;
1292 if (final_unmapped)
1293 *final_unmapped = unmapped;
1294
1295 from_seq = gtphub_unmap_seq(p, from);
1296
1297 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1298 return -1;
1299
1300 struct gtphub_peer *from_peer = from->peer_addr->peer;
1301 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001302 LOG(LOGL_DEBUG,
1303 "Seq unmap and TEI unmap yield two different peers."
1304 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001305 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1306 gtphub_plane_idx_names[p->plane_idx],
1307 gtphub_peer_str(from_peer),
1308 (int)p->seq,
1309 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001310 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001311 gtphub_port_str2(from_tei)
1312 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001313 }
1314 unmapped = (from_seq? from_seq : from_tei);
1315
1316 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001317 LOG(LOGL_NOTICE,
1318 "Unmap yields a different peer than the configured proxy."
1319 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001320 " unmapped: %s proxy: %s\n",
1321 gtphub_port_str(unmapped),
1322 gtphub_port_str2(to_proxy)
1323 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001324 }
1325 unmapped = (to_proxy? to_proxy : unmapped);
1326
1327 if (!unmapped) {
1328 /* Return no error, but returned pointers are all NULL. */
1329 return 0;
1330 }
1331
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001332 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001333 from_seq, from_tei, unmapped);
1334
1335 if (unmapped_from_seq)
1336 *unmapped_from_seq = from_seq;
1337 if (unmapped_from_tei)
1338 *unmapped_from_tei = from_tei;
1339 if (final_unmapped)
1340 *final_unmapped = unmapped;
1341 return 0;
1342}
1343
1344static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1345 uint16_t port,
1346 struct osmo_sockaddr *dst)
1347{
1348 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1349}
1350
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001351/* If p is an Echo request, replace p's data with the matching response and
1352 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1353 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001354static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1355 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001356{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001357 if (p->type != GTP_ECHO_REQ)
1358 return 0;
1359
1360 static uint8_t echo_response_data[14] = {
1361 0x32, /* flags */
1362 GTP_ECHO_RSP,
1363 0x00, 14 - 8, /* Length in network byte order */
1364 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1365 0, 0, /* Seq, to be replaced */
1366 0, 0, /* no extensions */
1367 0x0e, /* Recovery IE */
1368 0 /* Recovery counter, to be replaced */
1369 };
1370 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1371 uint8_t *recovery = &echo_response_data[13];
1372
1373 *seq = hton16(p->seq);
1374 *recovery = hub->restart_counter;
1375
1376 *reply_buf = echo_response_data;
1377
1378 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001379}
1380
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001381struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1382 const struct osmo_sockaddr *addr);
1383
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001384/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1385 * address to forward to. Return a pointer to the osmo_fd, but copy the
1386 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1387 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1388 * bytes to forward, 0 or less on failure. */
1389int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1390 unsigned int plane_idx,
1391 const struct osmo_sockaddr *from_addr,
1392 uint8_t *buf,
1393 size_t received,
1394 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001395 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001396 struct osmo_fd **to_ofd,
1397 struct osmo_sockaddr *to_addr)
1398{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001399 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1400 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
1401 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1402 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1403
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001404 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1405 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001406 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001407 gtphub_plane_idx_names[plane_idx],
1408 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001409
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001410 static struct gtp_packet_desc p;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001411 gtp_decode(buf, received, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001412
1413 if (p.rc <= 0)
1414 return -1;
1415
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001416 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1417
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001418 int reply_len;
1419 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1420 if (reply_len > 0) {
1421 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001422 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001423 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001424
1425 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1426 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1427 reply_len);
1428 LOG(LOGL_DEBUG, "--> Echo response to GGSN: %d bytes to %s\n",
1429 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001430 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001431 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001432 if (reply_len < 0)
1433 return -1;
1434
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001435 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001436
1437 /* If a GGSN proxy is configured, check that it's indeed that proxy
1438 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1439 * gtphub, so no-one else is allowed to talk to us from that side. */
1440 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1441 if (ggsn) {
1442 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001443 LOG(LOGL_ERROR,
1444 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001445 " received on GGSN bind is from another sender:"
1446 " proxy: %s sender: %s\n",
1447 gtphub_port_str(ggsn),
1448 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001449 return -1;
1450 }
1451 }
1452
1453 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001454 /* Find a GGSN peer with a matching address. The sender's port
1455 * may in fact differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001456 ggsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001457 }
1458
1459 /* If any PDP context has been created, we already have an entry for
1460 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1461 * us about. */
1462 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001463 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1464 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001465 return -1;
1466 }
1467
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001468 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001469
1470 struct gtphub_peer_port *sgsn_from_seq;
1471 struct gtphub_peer_port *sgsn;
1472 if (gtphub_unmap(hub, &p, ggsn,
1473 hub->sgsn_proxy[plane_idx],
1474 &sgsn, &sgsn_from_seq,
1475 NULL /* not interested, got it in &sgsn already */
1476 )
1477 != 0) {
1478 return -1;
1479 }
1480
1481 if (!sgsn) {
1482 /* A GGSN initiated request would go to a known TEI. So this is
1483 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001484 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001485 return -1;
1486 }
1487
1488 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001489 /* This may be a Create PDP Context response. If it is, there
1490 * are other addresses in the GTP message to set up apart from
1491 * the sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001492 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001493 &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001494 != 0)
1495 return -1;
1496 }
1497
1498 gtphub_check_restart_counter(hub, &p, ggsn);
1499 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1500
1501 /* If the GGSN is replying to an SGSN request, the sequence nr has
1502 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1503 * create a new mapping. */
1504 if (!sgsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001505 gtphub_map_seq(&p, ggsn, sgsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001506
1507 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001508
1509 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001510
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001511 if (received) {
1512 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1513 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1514 received);
1515 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001516 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001517 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001518 return received;
1519}
1520
1521static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1522{
1523 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1524 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001525 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1526 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001527
1528 if (!(what & BSC_FD_READ))
1529 return 0;
1530
1531 struct gtphub *hub = from_sgsns_ofd->data;
1532
1533 static uint8_t buf[4096];
1534 struct osmo_sockaddr from_addr;
1535 struct osmo_sockaddr to_addr;
1536 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001537 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001538 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001539
1540 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1541 if (len < 1)
1542 return 0;
1543
1544 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1545 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001546 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001547 if (len < 1)
1548 return 0;
1549
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001550 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001551}
1552
1553/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1554int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1555 unsigned int plane_idx,
1556 const struct osmo_sockaddr *from_addr,
1557 uint8_t *buf,
1558 size_t received,
1559 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001560 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001561 struct osmo_fd **to_ofd,
1562 struct osmo_sockaddr *to_addr)
1563{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001564 struct gtphub_bind *from_bind_arr = hub->to_sgsns;
1565 struct gtphub_bind *to_bind_arr = hub->to_ggsns;
1566 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1567 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1568
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001569 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1570 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001571 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001572 gtphub_plane_idx_names[plane_idx],
1573 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001574
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001575 static struct gtp_packet_desc p;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001576 gtp_decode(buf, received, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001577
1578 if (p.rc <= 0)
1579 return -1;
1580
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001581 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1582
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001583 int reply_len;
1584 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1585 if (reply_len > 0) {
1586 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001587 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001588 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001589
1590 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1591 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1592 reply_len);
1593 LOG(LOGL_DEBUG, "<-- Echo response to SGSN: %d bytes to %s\n",
1594 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001595 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001596 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001597 if (reply_len < 0)
1598 return -1;
1599
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001600 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001601
1602 /* If an SGSN proxy is configured, check that it's indeed that proxy
1603 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1604 * gtphub, so no-one else is allowed to talk to us from that side. */
1605 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1606 if (sgsn) {
1607 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001608 LOG(LOGL_ERROR,
1609 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001610 " received on GGSN bind is from another sender:"
1611 " proxy: %s sender: %s\n",
1612 gtphub_port_str(sgsn),
1613 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001614 return -1;
1615 }
1616 }
1617
1618 if (!sgsn) {
1619 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001620 * entry for this SGSN. The port may differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001621 sgsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001622 }
1623
1624 if (!sgsn) {
1625 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1626 * first contact without being known yet, so create the peer
1627 * struct for the current sender. */
1628 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001629 LOG(LOGL_ERROR,
1630 "User plane peer was not announced by PDP Context,"
1631 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001632 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001633 return -1;
1634 }
1635
1636 struct gsn_addr from_gsna;
1637 uint16_t from_port;
1638 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1639 return -1;
1640
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001641 sgsn = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001642 }
1643
1644 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001645 /* This could theoretically happen for invalid address data or
1646 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001647 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1648 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001649 return -1;
1650 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001651 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001652
1653 struct gtphub_peer_port *ggsn_from_seq;
1654 struct gtphub_peer_port *ggsn;
1655 if (gtphub_unmap(hub, &p, sgsn,
1656 hub->ggsn_proxy[plane_idx],
1657 &ggsn, &ggsn_from_seq,
1658 NULL /* not interested, got it in &ggsn already */
1659 )
1660 != 0) {
1661 return -1;
1662 }
1663
Neels Hofmeyra208c732015-11-11 19:26:09 +01001664 if (!ggsn) {
1665 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1666 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001667 }
1668
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001669 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001670 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001671 return -1;
1672 }
1673
1674 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001675 /* This may be a Create PDP Context requst. If it is, there are
1676 * other addresses in the GTP message to set up apart from the
1677 * sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001678 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001679 &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001680 != 0)
1681 return -1;
1682 }
1683
1684 gtphub_check_restart_counter(hub, &p, sgsn);
1685 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1686
1687 /* If the SGSN is replying to a GGSN request, the sequence nr has
1688 * already been unmapped above (unmap_ggsn != NULL), and we need not
1689 * create a new outgoing sequence map. */
1690 if (!ggsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001691 gtphub_map_seq(&p, sgsn, ggsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001692
1693 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1694
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001695 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001696
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001697 if (received) {
1698 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1699 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1700 received);
1701 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001702 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001703 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001704 return received;
1705}
1706
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001707static void resolved_gssn_del_cb(struct expiring_item *expi)
1708{
1709 struct gtphub_resolved_ggsn *ggsn;
1710 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1711
1712 gtphub_port_ref_count_dec(ggsn->peer);
1713 llist_del(&ggsn->entry);
1714
1715 ggsn->expiry_entry.del_cb = 0;
1716 expiring_item_del(&ggsn->expiry_entry);
1717
1718 talloc_free(ggsn);
1719}
1720
1721void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1722 struct gsn_addr *resolved_addr,
1723 time_t now)
1724{
1725 struct gtphub_peer_port *pp;
1726 struct gtphub_resolved_ggsn *ggsn;
1727
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001728 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001729 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1730 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001731
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001732 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1733 resolved_addr, 2123);
1734 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001735 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1736 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001737 return;
1738 }
1739
1740 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1741 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001742 INIT_LLIST_HEAD(&ggsn->entry);
1743 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001744
1745 ggsn->peer = pp;
1746 gtphub_port_ref_count_inc(pp);
1747
1748 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001749 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001750
1751 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1752 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1753
1754 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1755}
1756
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001757static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1758{
1759 return pp->ref_count == 0;
1760}
1761
1762static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1763{
1764 struct gtphub_peer_port *pp, *npp;
1765 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1766 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001767 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001768 gtphub_port_str(pp));
1769 gtphub_peer_port_del(pp);
1770 }
1771 }
1772 return llist_empty(&pa->ports);
1773}
1774
1775static int gtphub_gc_peer(struct gtphub_peer *p)
1776{
1777 struct gtphub_peer_addr *pa, *npa;
1778 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1779 if (gtphub_gc_peer_addr(pa)) {
1780 gtphub_peer_addr_del(pa);
1781 }
1782 }
1783
1784 /* Note that there's a ref_count in each gtphub_peer_port instance
1785 * listed within p->addresses, referenced by TEI mappings from
1786 * hub->tei_map. As long as those don't expire, this peer will stay. */
1787
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001788 return llist_empty(&p->addresses)
1789 && nr_map_empty(&p->seq_map);
1790}
1791
1792static void gtphub_gc_bind(struct gtphub_bind *b)
1793{
1794 struct gtphub_peer *p, *n;
1795 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1796 if (gtphub_gc_peer(p)) {
1797 gtphub_peer_del(p);
1798 }
1799 }
1800}
1801
1802void gtphub_gc(struct gtphub *hub, time_t now)
1803{
1804 int expired;
1805 expired = expiry_tick(&hub->expire_seq_maps, now);
1806 expired += expiry_tick(&hub->expire_tei_maps, now);
1807
1808 /* ... */
1809
1810 if (expired) {
1811 int i;
1812 for (i = 0; i < GTPH_PLANE_N; i++) {
1813 gtphub_gc_bind(&hub->to_sgsns[i]);
1814 gtphub_gc_bind(&hub->to_ggsns[i]);
1815 }
1816 }
1817}
1818
1819static void gtphub_gc_cb(void *data)
1820{
1821 struct gtphub *hub = data;
1822 gtphub_gc(hub, gtphub_now());
1823 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1824}
1825
1826static void gtphub_gc_start(struct gtphub *hub)
1827{
1828 hub->gc_timer.cb = gtphub_gc_cb;
1829 hub->gc_timer.data = hub;
1830
1831 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1832}
1833
1834/* called by unit tests */
1835void gtphub_init(struct gtphub *hub)
1836{
1837 gtphub_zero(hub);
1838
1839 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001840 expiry_init(&hub->expire_tei_maps,
1841 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001842
1843 int plane_idx;
1844 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001845 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001846 nr_map_init(&hub->tei_map[plane_idx],
1847 &hub->tei_pool[plane_idx],
1848 &hub->expire_tei_maps);
1849
1850 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1851 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1852 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001853
1854 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1855 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1856 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1857 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001858}
1859
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001860/* For the test suite, this is kept separate from gtphub_stop(), which also
1861 * closes sockets. The test suite avoids using sockets and would cause
1862 * segfaults when trying to close uninitialized ofds. */
1863void gtphub_free(struct gtphub *hub)
1864{
1865 /* By expiring all mappings, a garbage collection should free
1866 * everything else. A gtphub_bind_free() will assert that everything is
1867 * indeed empty. */
1868 expiry_clear(&hub->expire_seq_maps);
1869 expiry_clear(&hub->expire_tei_maps);
1870
1871 int plane_idx;
1872 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1873 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
1874 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
1875
1876 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
1877 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
1878 }
1879}
1880
1881void gtphub_stop(struct gtphub *hub)
1882{
1883 int plane_idx;
1884 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1885 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
1886 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
1887 }
1888 gtphub_free(hub);
1889}
1890
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001891static int gtphub_make_proxy(struct gtphub *hub,
1892 struct gtphub_peer_port **pp,
1893 struct gtphub_bind *bind,
1894 const struct gtphub_cfg_addr *addr)
1895{
1896 if (!addr->addr_str)
1897 return 0;
1898
1899 struct gsn_addr gsna;
1900 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1901 return -1;
1902
1903 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1904
1905 /* This is *the* proxy. Make sure it is never expired. */
1906 gtphub_port_ref_count_inc(*pp);
1907 return 0;
1908}
1909
1910int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1911{
1912 int rc;
1913
1914 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001915
1916 /* If a Ctrl plane proxy is configured, ares will never be used. */
1917 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1918 if (gtphub_ares_init(hub) != 0) {
1919 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1920 return -1;
1921 }
1922 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001923
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001924 /* TODO set hub->restart_counter from external file. */
1925
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001926 int plane_idx;
1927 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1928 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1929 &cfg->to_ggsns[plane_idx],
1930 from_ggsns_read_cb, hub, plane_idx);
1931 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001932 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1933 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001934 return rc;
1935 }
1936
1937 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1938 &cfg->to_sgsns[plane_idx],
1939 from_sgsns_read_cb, hub, plane_idx);
1940 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001941 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1942 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001943 return rc;
1944 }
1945 }
1946
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001947 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1948 if (gtphub_make_proxy(hub,
1949 &hub->sgsn_proxy[plane_idx],
1950 &hub->to_sgsns[plane_idx],
1951 &cfg->sgsn_proxy[plane_idx])
1952 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001953 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001954 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001955 cfg->sgsn_proxy[plane_idx].addr_str,
1956 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001957 return -1;
1958 }
1959 if (gtphub_make_proxy(hub,
1960 &hub->ggsn_proxy[plane_idx],
1961 &hub->to_ggsns[plane_idx],
1962 &cfg->ggsn_proxy[plane_idx])
1963 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001964 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001965 return -1;
1966 }
1967 }
1968
1969 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1970 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001971 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001972 gtphub_plane_idx_names[plane_idx],
1973 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1974 }
1975
1976 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001977 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001978 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001979 gtphub_plane_idx_names[plane_idx],
1980 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1981 }
1982
1983 gtphub_gc_start(hub);
1984 return 0;
1985}
1986
1987static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1988 const struct gsn_addr *addr)
1989{
1990 struct gtphub_peer_addr *a;
1991 llist_for_each_entry(a, &peer->addresses, entry) {
1992 if (gsn_addr_same(&a->addr, addr))
1993 return a;
1994 }
1995 return NULL;
1996}
1997
1998static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1999 uint16_t port)
2000{
2001 OSMO_ASSERT(port);
2002 struct gtphub_peer_port *pp;
2003 llist_for_each_entry(pp, &a->ports, entry) {
2004 if (pp->port == port)
2005 return pp;
2006 }
2007 return NULL;
2008}
2009
2010static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2011 const struct gsn_addr *addr)
2012{
2013 struct gtphub_peer *peer;
2014 llist_for_each_entry(peer, &bind->peers, entry) {
2015 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2016 if (a)
2017 return a;
2018 }
2019 return NULL;
2020}
2021
2022static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2023 const struct gsn_addr *addr,
2024 uint16_t port)
2025{
2026 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2027 if (!a)
2028 return NULL;
2029 return gtphub_addr_find_port(a, port);
2030}
2031
2032struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2033 const struct osmo_sockaddr *addr)
2034{
2035 struct gsn_addr gsna;
2036 uint16_t port;
2037 gsn_addr_from_sockaddr(&gsna, &port, addr);
2038 return gtphub_port_find(bind, &gsna, port);
2039}
2040
2041static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2042 struct gtphub_bind *bind)
2043{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002044 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2045 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002046 OSMO_ASSERT(peer);
2047
2048 INIT_LLIST_HEAD(&peer->addresses);
2049
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002050 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002051 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
2052
2053 /* TODO use something random to pick the initial sequence nr.
2054 0x6d31 produces the ASCII character sequence 'm1', currently used in
2055 gtphub_nc_test.sh. */
2056 peer->seq_pool.last_nr = 0x6d31 - 1;
2057
2058 llist_add(&peer->entry, &bind->peers);
2059 return peer;
2060}
2061
2062static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2063 const struct gsn_addr *addr)
2064{
2065 struct gtphub_peer_addr *a;
2066 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2067 OSMO_ASSERT(a);
2068 a->peer = peer;
2069 gsn_addr_copy(&a->addr, addr);
2070 INIT_LLIST_HEAD(&a->ports);
2071 llist_add(&a->entry, &peer->addresses);
2072
2073 return a;
2074}
2075
2076static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2077 struct gtphub_bind *bind,
2078 const struct gsn_addr *addr)
2079{
2080 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2081 if (a)
2082 return a;
2083
2084 /* If we haven't found an address, that means we need to create an
2085 * entirely new peer for the new address. More addresses may be added
2086 * to this peer later, but not via this function. */
2087 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002088
2089 a = gtphub_peer_add_addr(peer, addr);
2090
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002091 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002092 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002093 gsn_addr_to_str(&a->addr));
2094
2095 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002096}
2097
2098static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2099 uint16_t port)
2100{
2101 struct gtphub_peer_port *pp;
2102
2103 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2104 OSMO_ASSERT(pp);
2105 pp->peer_addr = a;
2106 pp->port = port;
2107
2108 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2109 talloc_free(pp);
2110 return NULL;
2111 }
2112
2113 llist_add(&pp->entry, &a->ports);
2114
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002115 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002116 gsn_addr_to_str(&a->addr),
2117 (int)port);
2118
2119 return pp;
2120}
2121
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002122struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2123 struct gtphub_bind *bind,
2124 const struct gsn_addr *addr,
2125 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002126{
2127 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2128
2129 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2130 if (pp)
2131 return pp;
2132
2133 return gtphub_addr_add_port(a, port);
2134}
2135
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002136/* Find a GGSN peer with a matching address. If the address is known but the
2137 * port not, create a new port for that peer address. */
2138struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2139 const struct osmo_sockaddr *addr)
2140{
2141 struct gtphub_peer_addr *pa;
2142 struct gtphub_peer_port *pp;
2143
2144 struct gsn_addr gsna;
2145 uint16_t port;
2146 gsn_addr_from_sockaddr(&gsna, &port, addr);
2147
2148 pa = gtphub_addr_find(bind, &gsna);
2149 if (!pa)
2150 return NULL;
2151
2152 pp = gtphub_addr_find_port(pa, port);
2153
2154 if (!pp)
2155 pp = gtphub_addr_add_port(pa, port);
2156
2157 return pp;
2158}
2159
2160
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002161/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2162 * resolution should be possible but failed, and 1 if resolution was
2163 * successful. *pp will be set to NULL if <1 is returned. */
2164static int gtphub_resolve_ggsn(struct gtphub *hub,
2165 struct gtp_packet_desc *p,
2166 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002167{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002168 *pp = NULL;
2169
2170 /* TODO determine from message type whether IEs should be present? */
2171
2172 int rc;
2173 const char *imsi_str;
2174 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2175 if (rc < 1)
2176 return rc;
2177 OSMO_ASSERT(imsi_str);
2178
2179 const char *apn_str;
2180 rc = get_ie_apn_str(p->ie, &apn_str);
2181 if (rc < 1)
2182 return rc;
2183 OSMO_ASSERT(apn_str);
2184
2185 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2186 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002187}
2188
2189
2190/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002191/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002192/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2193 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002194static int _osmo_getaddrinfo(struct addrinfo **result,
2195 uint16_t family, uint16_t type, uint8_t proto,
2196 const char *host, uint16_t port)
2197{
2198 struct addrinfo hints;
2199 char portbuf[16];
2200
2201 sprintf(portbuf, "%u", port);
2202 memset(&hints, '\0', sizeof(struct addrinfo));
2203 hints.ai_family = family;
2204 if (type == SOCK_RAW) {
2205 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2206 * SOCK_RAW and IPPROTO_GRE is used.
2207 */
2208 hints.ai_socktype = SOCK_DGRAM;
2209 hints.ai_protocol = IPPROTO_UDP;
2210 } else {
2211 hints.ai_socktype = type;
2212 hints.ai_protocol = proto;
2213 }
2214
2215 return getaddrinfo(host, portbuf, &hints, result);
2216}
2217
2218/* TODO move to osmocom/core/socket.c ? */
2219int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2220 uint16_t family, uint16_t type, uint8_t proto,
2221 const char *host, uint16_t port)
2222{
2223 struct addrinfo *res;
2224 int rc;
2225 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2226
2227 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002228 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002229 return -EINVAL;
2230 }
2231
2232 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2233 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2234 addr->l = res->ai_addrlen;
2235 freeaddrinfo(res);
2236
2237 return 0;
2238}
2239
2240int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2241 char *port_str, size_t port_str_len,
2242 const struct osmo_sockaddr *addr,
2243 int flags)
2244{
2245 int rc;
2246
2247 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2248 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2249 return -1;
2250 }
2251
2252 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002253 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2254 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002255 return -1;
2256 }
2257
2258 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2259 addr_str, addr_str_len,
2260 port_str, port_str_len,
2261 flags);
2262
2263 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002264 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2265 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2266 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002267
2268 return rc;
2269}
2270
2271const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2272 char *buf, size_t buf_len)
2273{
2274 const int portbuf_len = 6;
2275 OSMO_ASSERT(buf_len > portbuf_len);
2276 char *portbuf = buf + buf_len - portbuf_len;
2277 buf_len -= portbuf_len;
2278 if (osmo_sockaddr_to_strs(buf, buf_len,
2279 portbuf, portbuf_len,
2280 addr,
2281 NI_NUMERICHOST | NI_NUMERICSERV))
2282 return NULL;
2283
2284 char *pos = buf + strnlen(buf, buf_len-1);
2285 size_t len = buf_len - (pos - buf);
2286
2287 snprintf(pos, len, " port %s", portbuf);
2288 buf[buf_len-1] = '\0';
2289
2290 return buf;
2291}
2292
2293const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2294{
2295 static char buf[256];
2296 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2297 if (! result)
2298 return "(invalid)";
2299 return result;
2300}
2301
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002302int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2303 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002304{
2305 if (a == b)
2306 return 0;
2307 if (!a)
2308 return -1;
2309 if (!b)
2310 return 1;
2311 if (a->l != b->l) {
2312 /* Lengths are not the same, but determine the order. Will
2313 * anyone ever sort a list by osmo_sockaddr though...? */
2314 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2315 if (cmp == 0) {
2316 if (a->l < b->l)
2317 return -1;
2318 else
2319 return 1;
2320 }
2321 return cmp;
2322 }
2323 return memcmp(&a->a, &b->a, a->l);
2324}
2325
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002326void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2327 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002328{
2329 OSMO_ASSERT(src->l <= sizeof(dst->a));
2330 memcpy(&dst->a, &src->a, src->l);
2331 dst->l = src->l;
2332}