blob: 45599cc3436c4c3714af21c704f696db50e1689e [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;
88 union gtpie_member *ie[GTPIE_SIZE];
89};
90
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010091
92/* counters */
93
94enum gtphub_counters_io {
95 GTPH_CTR_PKTS_IN = 0,
96 GTPH_CTR_PKTS_OUT,
97 GTPH_CTR_BYTES_IN,
98 GTPH_CTR_BYTES_OUT
99};
100
101static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
102 { "packets.in", "Packets ( In)" },
103 { "packets.out", "Packets (Out)" },
104 { "bytes.in", "Packets ( In)" },
105 { "bytes.out", "Packets (Out)" },
106};
107
108static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
109 .group_name_prefix = "gtphub.bind",
110 .group_description = "Local address I/O statistics",
111 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
112 .ctr_desc = gtphub_counters_io_desc,
113 .class_id = OSMO_STATS_CLASS_GLOBAL,
114};
115
116
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200117void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
118{
119 memcpy(gsna, src, sizeof(struct gsn_addr));
120}
121
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200122int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
123 const struct osmo_sockaddr *sa)
124{
125 char addr_str[256];
126 char port_str[6];
127
128 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
129 port_str, sizeof(port_str),
130 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
131 != 0) {
132 return -1;
133 }
134
135 if (port)
136 *port = atoi(port_str);
137
138 return gsn_addr_from_str(gsna, addr_str);
139}
140
141int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
142{
143 int af = AF_INET;
144 gsna->len = 4;
145 const char *pos = numeric_addr_str;
146 for (; *pos; pos++) {
147 if (*pos == ':') {
148 af = AF_INET6;
149 gsna->len = 16;
150 break;
151 }
152 }
153
154 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
155 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100156 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
157 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200158 return -1;
159 }
160 return 0;
161}
162
163const char *gsn_addr_to_str(const struct gsn_addr *gsna)
164{
165 static char buf[INET6_ADDRSTRLEN + 1];
166 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
167}
168
169const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
170 char *strbuf,
171 int strbuf_len)
172{
173 int af;
174 switch (gsna->len) {
175 case 4:
176 af = AF_INET;
177 break;
178 case 16:
179 af = AF_INET6;
180 break;
181 default:
182 return NULL;
183 }
184
185 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
186 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100187 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
188 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100189 strerror(errno),
190 (int)gsna->len,
191 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200192 }
193 return r;
194}
195
196int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
197{
198 if (a == b)
199 return 1;
200 if ((!a) || (!b))
201 return 0;
202 if (a->len != b->len)
203 return 0;
204 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
205}
206
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100207static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
208 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200209{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100210 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200211 return -1;
212
213 unsigned int len;
214 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
215 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
216 &len, gsna->buf, sizeof(gsna->buf))
217 != 0)
218 return -1;
219 gsna->len = len;
220 return 0;
221}
222
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100223static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
224 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200225{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100226 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200227 return -1;
228
229 int ie_idx;
230 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
231
232 if (ie_idx < 0)
233 return -1;
234
235 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
236 int ie_l = ntoh16(ie->l);
237 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100238 LOG(LOGL_ERROR, "Not implemented:"
239 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200240 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
241 return -1;
242 }
243
244 memcpy(ie->v, gsna->buf, (int)ie_l);
245 return 0;
246}
247
248/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
249 */
250void validate_gtp0_header(struct gtp_packet_desc *p)
251{
252 const struct gtp0_header *pheader = &(p->data->gtp0.h);
253 p->rc = GTP_RC_UNKNOWN;
254 p->header_len = 0;
255
256 OSMO_ASSERT(p->data_len >= 1);
257 OSMO_ASSERT(p->version == 0);
258
259 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100260 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200261 p->rc = GTP_RC_TOOSHORT;
262 return;
263 }
264
265 p->type = ntoh8(pheader->type);
266 p->seq = ntoh16(pheader->seq);
267 p->header_tei = 0; /* TODO */
268
269 if (p->data_len == GTP0_HEADER_SIZE) {
270 p->rc = GTP_RC_TINY;
271 p->header_len = GTP0_HEADER_SIZE;
272 return;
273 }
274
275 /* Check packet length field versus length of packet */
276 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100277 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
278 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100279 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
280 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200281 p->rc = GTP_RC_TOOSHORT;
282 return;
283 }
284
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100285 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200286 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100287 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200288}
289
290/* Validate GTP version 1 data, and update p->rc with the result, as well as
291 * p->header_len in case of a valid header. */
292void validate_gtp1_header(struct gtp_packet_desc *p)
293{
294 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
295 p->rc = GTP_RC_UNKNOWN;
296 p->header_len = 0;
297
298 OSMO_ASSERT(p->data_len >= 1);
299 OSMO_ASSERT(p->version == 1);
300
301 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
302 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100303 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200304 p->rc = GTP_RC_TOOSHORT;
305 return;
306 }
307
308 p->type = ntoh8(pheader->type);
309 p->header_tei = ntoh32(pheader->tei);
310 p->seq = ntoh16(pheader->seq);
311
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100312 LOG(LOGL_DEBUG, "GTPv1\n"
313 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
314 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
315 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
316 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
317 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
318 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
319 p->type, p->type,
320 ntoh16(pheader->length), ntoh16(pheader->length),
321 p->header_tei, p->header_tei,
322 p->seq, p->seq,
323 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200324 pheader->next, pheader->next);
325
326 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
327 p->rc = GTP_RC_TINY;
328 p->header_len = GTP1_HEADER_SIZE_SHORT;
329 return;
330 }
331
332 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100333 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
334 if (p->data_len != announced_len) {
335 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
336 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100337 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
338 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200339 p->rc = GTP_RC_TOOSHORT;
340 return;
341 }
342
Neels Hofmeyre921e322015-11-11 00:45:50 +0100343 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200344 p->header_len = GTP1_HEADER_SIZE_LONG;
345}
346
347/* Examine whether p->data of size p->data_len has a valid GTP header. Set
348 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
349 * gtp_rc). p->data must point at a buffer with p->data_len set. */
350void validate_gtp_header(struct gtp_packet_desc *p)
351{
352 p->rc = GTP_RC_UNKNOWN;
353
354 /* Need at least 1 byte in order to check version */
355 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100356 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
357 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200358 p->rc = GTP_RC_TOOSHORT;
359 return;
360 }
361
362 p->version = p->data->flags >> 5;
363
364 switch (p->version) {
365 case 0:
366 validate_gtp0_header(p);
367 break;
368 case 1:
369 validate_gtp1_header(p);
370 break;
371 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100372 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200373 p->rc = GTP_RC_UNSUPPORTED_VERSION;
374 break;
375 }
376}
377
378
379/* Return the value of the i'th IMSI IEI by copying to *imsi.
380 * The first IEI is reached by passing i = 0.
381 * imsi must point at allocated space of (at least) 8 bytes.
382 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100383static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200384{
385 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
386}
387
388/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100389static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200390{
391 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
392}
393
394static char imsi_digit_to_char(uint8_t nibble)
395{
396 nibble &= 0x0f;
397 if (nibble > 9)
398 return (nibble == 0x0f) ? '\0' : '?';
399 return '0' + nibble;
400}
401
402/* Return a human readable IMSI string, in a static buffer.
403 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100404static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200405{
406 static char str[17];
407 int i;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100408 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200409
410 for (i = 0; i < 8; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100411 c = imsi_digit_to_char(imsi[i]);
412 if (c == '?')
413 return -1;
414 str[2*i] = c;
415
416 c = imsi_digit_to_char(imsi[i] >> 4);
417 if (c == '?')
418 return -1;
419 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200420 }
421 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100422 *imsi_str = str;
423 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200424}
425
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100426/* Return 0 if not present, 1 if present and decoded successfully, -1 if
427 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100428static int get_ie_imsi_str(union gtpie_member *ie[], int i,
429 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100430{
431 uint8_t imsi_buf[8];
432 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100433 return 0;
434 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100435}
436
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100437/* Return 0 if not present, 1 if present and decoded successfully, -1 if
438 * present but cannot be decoded. */
439static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100440{
441 static char apn_buf[GSM_APN_LENGTH];
442 unsigned int len;
443 if (gtpie_gettlv(ie, GTPIE_APN, 0,
444 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100445 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100446
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100447 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100448 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
449 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100450 return -1;
451 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100452
453 if (len > (sizeof(apn_buf) - 1))
454 len = sizeof(apn_buf) - 1;
455 apn_buf[len] = '\0';
456
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100457 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
458 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100459 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
460 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100461 return -1;
462 }
463 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100464}
465
466
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200467/* Validate header, and index information elements. Write decoded packet
468 * information to *res. res->data will point at the given data buffer. On
469 * error, p->rc is set <= 0 (see enum gtp_rc). */
470static void gtp_decode(const uint8_t *data, int data_len,
471 unsigned int from_plane_idx,
472 struct gtp_packet_desc *res)
473{
474 ZERO_STRUCT(res);
475 res->data = (union gtp_packet*)data;
476 res->data_len = data_len;
477 res->plane_idx = from_plane_idx;
478
479 validate_gtp_header(res);
480
481 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100482 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200483 return;
484 }
485
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100486 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200487
Neels Hofmeyre921e322015-11-11 00:45:50 +0100488 if (from_plane_idx == GTPH_PLANE_USER) {
489 res->rc = GTP_RC_PDU_U;
490 return;
491 }
492
493 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100494 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200495 return;
496 }
497
498 if (gtpie_decaps(res->ie, res->version,
499 (void*)(data + res->header_len),
500 res->data_len - res->header_len) != 0) {
501 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100502 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
503 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200504 return;
505 }
506
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100507#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100508 /* TODO if (<loglevel is debug>) { ...
509 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200510 int i;
511
512 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100513 const char *imsi;
514 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200515 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100516 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200517 }
518
519 for (i = 0; i < 10; i++) {
520 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100521 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200522 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100523 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200524 }
525
526 for (i = 0; i < 2; i++) {
527 struct gsn_addr addr;
528 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100529 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200530 }
531
532 for (i = 0; i < 10; i++) {
533 uint32_t tei;
534 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
535 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100536 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200537 tei, tei);
538 }
539
540 for (i = 0; i < 10; i++) {
541 uint32_t tei;
542 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
543 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100544 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200545 tei, tei);
546 }
547#endif
548}
549
550
551/* expiry */
552
553void expiry_init(struct expiry *exq, int expiry_in_seconds)
554{
555 ZERO_STRUCT(exq);
556 exq->expiry_in_seconds = expiry_in_seconds;
557 INIT_LLIST_HEAD(&exq->items);
558}
559
560void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
561{
562 item->expiry = now + exq->expiry_in_seconds;
563
564 /* Add/move to the tail to always sort by expiry, ascending. */
565 llist_del(&item->entry);
566 llist_add_tail(&item->entry, &exq->items);
567}
568
569int expiry_tick(struct expiry *exq, time_t now)
570{
571 int expired = 0;
572 struct expiring_item *m, *n;
573 llist_for_each_entry_safe(m, n, &exq->items, entry) {
574 if (m->expiry <= now) {
575 expiring_item_del(m);
576 expired ++;
577 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200578 /* The items are added sorted by expiry. So when we hit
579 * an unexpired entry, only more unexpired ones will
580 * follow. */
581 break;
582 }
583 }
584 return expired;
585}
586
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100587void expiry_clear(struct expiry *exq)
588{
589 struct expiring_item *m, *n;
590 llist_for_each_entry_safe(m, n, &exq->items, entry) {
591 expiring_item_del(m);
592 }
593}
594
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200595void expiring_item_init(struct expiring_item *item)
596{
597 ZERO_STRUCT(item);
598 INIT_LLIST_HEAD(&item->entry);
599}
600
601void expiring_item_del(struct expiring_item *item)
602{
603 OSMO_ASSERT(item);
604 llist_del(&item->entry);
605 INIT_LLIST_HEAD(&item->entry);
606 if (item->del_cb) {
607 /* avoid loops */
608 del_cb_t del_cb = item->del_cb;
609 item->del_cb = 0;
610 (del_cb)(item);
611 }
612}
613
614
615/* nr_map, nr_pool */
616
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100617void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200618{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100619 *pool = (struct nr_pool){
620 .nr_min = nr_min,
621 .nr_max = nr_max,
622 .last_nr = nr_max
623 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200624}
625
626nr_t nr_pool_next(struct nr_pool *pool)
627{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100628 if (pool->last_nr >= pool->nr_max)
629 pool->last_nr = pool->nr_min;
630 else
631 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200632
633 return pool->last_nr;
634}
635
636void nr_map_init(struct nr_map *map, struct nr_pool *pool,
637 struct expiry *exq)
638{
639 ZERO_STRUCT(map);
640 map->pool = pool;
641 map->add_items_to_expiry = exq;
642 INIT_LLIST_HEAD(&map->mappings);
643}
644
645void nr_mapping_init(struct nr_mapping *m)
646{
647 ZERO_STRUCT(m);
648 INIT_LLIST_HEAD(&m->entry);
649 expiring_item_init(&m->expiry_entry);
650}
651
652void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
653{
654 /* Generate a mapped number */
655 mapping->repl = nr_pool_next(map->pool);
656
657 /* Add to the tail to always yield a list sorted by expiry, in
658 * ascending order. */
659 llist_add_tail(&mapping->entry, &map->mappings);
660 if (map->add_items_to_expiry)
661 expiry_add(map->add_items_to_expiry,
662 &mapping->expiry_entry,
663 now);
664}
665
666void nr_map_clear(struct nr_map *map)
667{
668 struct nr_mapping *m;
669 struct nr_mapping *n;
670 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
671 nr_mapping_del(m);
672 }
673}
674
675int nr_map_empty(const struct nr_map *map)
676{
677 return llist_empty(&map->mappings);
678}
679
680struct nr_mapping *nr_map_get(const struct nr_map *map,
681 void *origin, nr_t nr_orig)
682{
683 struct nr_mapping *mapping;
684 llist_for_each_entry(mapping, &map->mappings, entry) {
685 if ((mapping->origin == origin)
686 && (mapping->orig == nr_orig))
687 return mapping;
688 }
689 /* Not found. */
690 return NULL;
691}
692
693struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
694{
695 struct nr_mapping *mapping;
696 llist_for_each_entry(mapping, &map->mappings, entry) {
697 if (mapping->repl == nr_repl) {
698 return mapping;
699 }
700 }
701 /* Not found. */
702 return NULL;
703}
704
705void nr_mapping_del(struct nr_mapping *mapping)
706{
707 OSMO_ASSERT(mapping);
708 llist_del(&mapping->entry);
709 INIT_LLIST_HEAD(&mapping->entry);
710 expiring_item_del(&mapping->expiry_entry);
711}
712
713
714/* gtphub */
715
716const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
717 "CTRL",
718 "USER",
719};
720
721const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
722 2123,
723 2152,
724};
725
726time_t gtphub_now(void)
727{
728 struct timespec now_tp;
729 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
730 return now_tp.tv_sec;
731}
732
733/* Remove a gtphub_peer from its list and free it. */
734static void gtphub_peer_del(struct gtphub_peer *peer)
735{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100736 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200737 nr_map_clear(&peer->seq_map);
738 llist_del(&peer->entry);
739 talloc_free(peer);
740}
741
742static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
743{
744 OSMO_ASSERT(llist_empty(&pa->ports));
745 llist_del(&pa->entry);
746 talloc_free(pa);
747}
748
749static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
750{
751 OSMO_ASSERT(pp->ref_count == 0);
752 llist_del(&pp->entry);
753 talloc_free(pp);
754}
755
756/* From the information in the gtp_packet_desc, return the address of a GGSN.
757 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100758static int gtphub_resolve_ggsn(struct gtphub *hub,
759 struct gtp_packet_desc *p,
760 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200761
762/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100763struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
764 const char *imsi_str,
765 const char *apn_ni_str);
766int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200767
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200768static void gtphub_zero(struct gtphub *hub)
769{
770 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100771 INIT_LLIST_HEAD(&hub->ggsn_lookups);
772 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200773}
774
775static int gtphub_sock_init(struct osmo_fd *ofd,
776 const struct gtphub_cfg_addr *addr,
777 osmo_fd_cb_t cb,
778 void *data,
779 int ofd_id)
780{
781 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100782 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200783 return -1;
784 }
785 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100786 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200787 return -1;
788 }
789
790 ofd->when = BSC_FD_READ;
791 ofd->cb = cb;
792 ofd->data = data;
793 ofd->priv_nr = ofd_id;
794
795 int rc;
796 rc = osmo_sock_init_ofd(ofd,
797 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
798 addr->addr_str, addr->port,
799 OSMO_SOCK_F_BIND);
800 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100801 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
802 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200803 return -1;
804 }
805
806 return 0;
807}
808
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100809static void gtphub_sock_close(struct osmo_fd *ofd)
810{
811 close(ofd->fd);
812 osmo_fd_unregister(ofd);
813 ofd->cb = NULL;
814}
815
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200816static void gtphub_bind_init(struct gtphub_bind *b)
817{
818 ZERO_STRUCT(b);
819
820 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100821
822 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
823 &gtphub_ctrg_io_desc, 0);
824 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200825}
826
827static int gtphub_bind_start(struct gtphub_bind *b,
828 const struct gtphub_cfg_bind *cfg,
829 osmo_fd_cb_t cb, void *cb_data,
830 unsigned int ofd_id)
831{
832 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
833 return -1;
834 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
835 return -1;
836 return 0;
837}
838
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100839static void gtphub_bind_free(struct gtphub_bind *b)
840{
841 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100842 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100843}
844
845static void gtphub_bind_stop(struct gtphub_bind *b) {
846 gtphub_sock_close(&b->ofd);
847 gtphub_bind_free(b);
848}
849
Neels Hofmeyr40348972015-11-17 12:22:28 +0100850/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200851 * Return the number of bytes read, zero on error. */
852static int gtphub_read(const struct osmo_fd *from,
853 struct osmo_sockaddr *from_addr,
854 uint8_t *buf, size_t buf_len)
855{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100856 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200857
Neels Hofmeyr40348972015-11-17 12:22:28 +0100858 /* recvfrom requires the available length set in *from_addr_len. */
859 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200860 errno = 0;
861 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100862 (struct sockaddr*)&from_addr->a,
863 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200864 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
865 * is not truncated. Then maybe reduce buf's size. */
866
867 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100868 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
869 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200870 return 0;
871 }
872
Neels Hofmeyr40348972015-11-17 12:22:28 +0100873 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
874 (int)received, osmo_sockaddr_to_str(from_addr),
875 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200876
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200877 return received;
878}
879
880inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
881{
882 OSMO_ASSERT(pp->ref_count < UINT_MAX);
883 pp->ref_count++;
884}
885
886inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
887{
888 OSMO_ASSERT(pp->ref_count > 0);
889 pp->ref_count--;
890}
891
892inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
893{
894 OSMO_ASSERT(p->version == 1);
895 p->data->gtp1l.h.seq = hton16(seq);
896 p->seq = seq;
897}
898
899inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
900{
901 OSMO_ASSERT(p->version == 1);
902 p->data->gtp1l.h.tei = hton32(tei);
903 p->header_tei = tei;
904}
905
906static void gtphub_mapping_del_cb(struct expiring_item *expi);
907
908static struct nr_mapping *gtphub_mapping_new()
909{
910 struct nr_mapping *nrm;
911 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
912 OSMO_ASSERT(nrm);
913
914 nr_mapping_init(nrm);
915 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
916 return nrm;
917}
918
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100919static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
920 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200921{
922 if (llist_empty(&peer->addresses))
923 return "(addressless)";
924
925 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
926 struct gtphub_peer_addr,
927 entry);
928 return gsn_addr_to_strb(&a->addr, buf, buflen);
929}
930
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100931static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
932 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200933{
934 if (!port)
935 return "(null port)";
936
937 snprintf(buf, buflen, "%s port %d",
938 gsn_addr_to_str(&port->peer_addr->addr),
939 (int)port->port);
940 return buf;
941}
942
943const char *gtphub_peer_str(struct gtphub_peer *peer)
944{
945 static char buf[256];
946 return gtphub_peer_strb(peer, buf, sizeof(buf));
947}
948
949const char *gtphub_peer_str2(struct gtphub_peer *peer)
950{
951 static char buf[256];
952 return gtphub_peer_strb(peer, buf, sizeof(buf));
953}
954
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100955const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200956{
957 static char buf[256];
958 return gtphub_port_strb(port, buf, sizeof(buf));
959}
960
961static const char *gtphub_port_str2(struct gtphub_peer_port *port)
962{
963 static char buf[256];
964 return gtphub_port_strb(port, buf, sizeof(buf));
965}
966
967static void gtphub_mapping_del_cb(struct expiring_item *expi)
968{
969 expi->del_cb = 0; /* avoid recursion loops */
970
971 struct nr_mapping *nrm = container_of(expi,
972 struct nr_mapping,
973 expiry_entry);
974 llist_del(&nrm->entry);
975 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
976
977 /* Just for log */
978 struct gtphub_peer_port *from = nrm->origin;
979 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100980 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200981 (int)nrm->expiry_entry.expiry,
982 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100983 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200984
985 gtphub_port_ref_count_dec(from);
986
987 talloc_free(nrm);
988}
989
990static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
991 struct gtphub_peer_port *from,
992 nr_t orig_nr,
993 time_t now)
994{
995 struct nr_mapping *nrm;
996
997 nrm = nr_map_get(map, from, orig_nr);
998
999 if (!nrm) {
1000 nrm = gtphub_mapping_new();
1001 nrm->orig = orig_nr;
1002 nrm->origin = from;
1003 nr_map_add(map, nrm, now);
1004 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001005 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001006 gtphub_port_str(from),
1007 (int)(nrm->orig), (int)(nrm->repl));
1008 } else {
1009 /* restart expiry timeout */
1010 expiry_add(map->add_items_to_expiry, &nrm->expiry_entry,
1011 now);
1012 }
1013
1014 OSMO_ASSERT(nrm);
1015 return nrm;
1016}
1017
1018static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
1019 int plane_idx,
1020 struct gtphub_peer_port *from,
1021 uint32_t orig_tei,
1022 time_t now)
1023{
1024 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
1025 from, orig_tei, now);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001026 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001027 gtphub_plane_idx_names[plane_idx],
1028 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001029 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001030
1031 return (uint32_t)nrm->repl;
1032}
1033
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001034static void gtphub_map_seq(struct gtp_packet_desc *p,
1035 struct gtphub_peer_port *from_port,
1036 struct gtphub_peer_port *to_port,
1037 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001038{
1039 /* Store a mapping in to_peer's map, so when we later receive a GTP
1040 * packet back from to_peer, the seq nr can be unmapped back to its
1041 * origin (from_peer here). */
1042 struct nr_mapping *nrm;
1043 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
1044 from_port, p->seq, now);
1045
1046 /* Change the GTP packet to yield the new, mapped seq nr */
1047 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001048}
1049
1050static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1051 struct gtphub_peer_port *responding_port)
1052{
1053 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001054 struct nr_mapping *nrm =
1055 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1056 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001057 if (!nrm)
1058 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001059 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1060 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001061 set_seq(p, nrm->orig);
1062 return nrm->origin;
1063}
1064
1065static void gtphub_check_restart_counter(struct gtphub *hub,
1066 struct gtp_packet_desc *p,
1067 struct gtphub_peer_port *from)
1068{
1069 /* TODO */
1070 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1071 * that doesn't match the peer's previously sent restart counter, clear
1072 * that peer and cancel PDP contexts. */
1073}
1074
1075static void gtphub_map_restart_counter(struct gtphub *hub,
1076 struct gtp_packet_desc *p,
1077 struct gtphub_peer_port *from,
1078 struct gtphub_peer_port *to)
1079{
1080 /* TODO */
1081}
1082
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001083static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1084 struct gtphub *hub,
1085 struct gtp_packet_desc *p,
1086 struct gtphub_peer_port *from_port)
1087{
1088 OSMO_ASSERT(p->version == 1);
1089 *to_port_p = NULL;
1090
1091 /* If the header's TEI is zero, no PDP context has been established
1092 * yet. If nonzero, a mapping should actually already exist for this
1093 * TEI, since it must have been announced in a PDP context creation. */
1094 uint32_t tei = p->header_tei;
1095 if (!tei)
1096 return 0;
1097
1098 /* to_peer has previously announced a TEI, which was stored and
1099 * mapped in from_peer's tei_map. */
1100 struct nr_mapping *nrm;
1101 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1102 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001103 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1104 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001105 return -1;
1106 }
1107
1108 struct gtphub_peer_port *to_port = nrm->origin;
1109 uint32_t unmapped_tei = nrm->orig;
1110 set_tei(p, unmapped_tei);
1111
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001112 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1113 gtphub_port_str(from_port),
1114 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001115 gtphub_port_str2(to_port));
1116
1117 *to_port_p = to_port;
1118 return 0;
1119}
1120
1121/* Read GSN address IEs from p, and make sure these peer addresses exist in
1122 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1123 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1124 * the packet p. */
1125static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001126 struct gtphub_bind from_bind_arr[],
1127 struct gtphub_bind to_bind_arr[],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001128 struct gtp_packet_desc *p,
1129 time_t now)
1130{
1131 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1132
1133 int rc;
1134 int plane_idx;
1135
1136 switch (p->type) {
1137 case GTP_CREATE_PDP_REQ:
1138 case GTP_CREATE_PDP_RSP:
1139 /* Go for it below */
1140 break;
1141 default:
1142 /* Nothing to do for this message type. */
1143 return 0;
1144 }
1145
1146 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1147
1148 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1149 plane_nrs_match_GSN_addr_IE_indices);
1150
1151 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1152 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1153
1154 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1155 struct gsn_addr addr_from_ie;
1156 uint32_t tei_from_ie;
1157 int ie_idx;
1158
1159 /* Fetch GSN Address and TEI from IEs */
1160 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1161 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001162 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1163 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001164 return -1;
1165 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001166 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001167 gtphub_plane_idx_names[plane_idx],
1168 gsn_addr_to_str(&addr_from_ie),
1169 addr_from_ie.len);
1170
1171 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1172 if (ie_idx < 0) {
1173 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001174 LOG(LOGL_ERROR,
1175 "Create PDP Context message invalid:"
1176 " missing IE %d\n",
1177 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001178 return -1;
1179 }
1180 tei_from_ie = 0;
1181 }
1182 else
1183 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1184
1185 /* Make sure an entry for this peer address with default port
1186 * exists */
1187 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001188 gtphub_port_have(hub, &from_bind_arr[plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001189 &addr_from_ie,
1190 gtphub_plane_idx_default_port[plane_idx]);
1191
1192 if (tei_from_ie) {
1193 /* Create TEI mapping and replace in GTP packet IE */
1194 uint32_t mapped_tei =
1195 gtphub_tei_mapping_have(hub, plane_idx,
1196 peer_from_ie,
1197 tei_from_ie,
1198 now);
1199 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1200 }
1201
1202 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001203 rc = gsn_addr_put(&to_bind_arr[plane_idx].local_addr, p,
1204 plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001205 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001206 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1207 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001208 return -1;
1209 }
1210 }
1211
1212 return 0;
1213}
1214
1215static int gtphub_write(const struct osmo_fd *to,
1216 const struct osmo_sockaddr *to_addr,
1217 const uint8_t *buf, size_t buf_len)
1218{
1219 errno = 0;
1220 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1221 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001222 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001223
1224 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001225 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001226 return -EINVAL;
1227 }
1228
1229 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001230 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1231 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001232 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001233 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1234 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001235
1236 return 0;
1237}
1238
1239static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1240{
1241 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1242 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001243 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1244 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001245 if (!(what & BSC_FD_READ))
1246 return 0;
1247
1248 struct gtphub *hub = from_ggsns_ofd->data;
1249
1250 static uint8_t buf[4096];
1251 struct osmo_sockaddr from_addr;
1252 struct osmo_sockaddr to_addr;
1253 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001254 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001255 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001256
1257 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1258 if (len < 1)
1259 return 0;
1260
1261 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1262 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001263 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001264 if (len < 1)
1265 return 0;
1266
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001267 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001268}
1269
1270static int gtphub_unmap(struct gtphub *hub,
1271 struct gtp_packet_desc *p,
1272 struct gtphub_peer_port *from,
1273 struct gtphub_peer_port *to_proxy,
1274 struct gtphub_peer_port **final_unmapped,
1275 struct gtphub_peer_port **unmapped_from_seq,
1276 struct gtphub_peer_port **unmapped_from_tei)
1277{
1278 /* Always (try to) unmap sequence and TEI numbers, which need to be
1279 * replaced in the packet. Either way, give precedence to the proxy, if
1280 * configured. */
1281
1282 struct gtphub_peer_port *from_seq = NULL;
1283 struct gtphub_peer_port *from_tei = NULL;
1284 struct gtphub_peer_port *unmapped = NULL;
1285
1286 if (unmapped_from_seq)
1287 *unmapped_from_seq = from_seq;
1288 if (unmapped_from_tei)
1289 *unmapped_from_tei = from_tei;
1290 if (final_unmapped)
1291 *final_unmapped = unmapped;
1292
1293 from_seq = gtphub_unmap_seq(p, from);
1294
1295 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1296 return -1;
1297
1298 struct gtphub_peer *from_peer = from->peer_addr->peer;
1299 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001300 LOG(LOGL_DEBUG,
1301 "Seq unmap and TEI unmap yield two different peers."
1302 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001303 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1304 gtphub_plane_idx_names[p->plane_idx],
1305 gtphub_peer_str(from_peer),
1306 (int)p->seq,
1307 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001308 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001309 gtphub_port_str2(from_tei)
1310 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001311 }
1312 unmapped = (from_seq? from_seq : from_tei);
1313
1314 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001315 LOG(LOGL_NOTICE,
1316 "Unmap yields a different peer than the configured proxy."
1317 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001318 " unmapped: %s proxy: %s\n",
1319 gtphub_port_str(unmapped),
1320 gtphub_port_str2(to_proxy)
1321 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001322 }
1323 unmapped = (to_proxy? to_proxy : unmapped);
1324
1325 if (!unmapped) {
1326 /* Return no error, but returned pointers are all NULL. */
1327 return 0;
1328 }
1329
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001330 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001331 from_seq, from_tei, unmapped);
1332
1333 if (unmapped_from_seq)
1334 *unmapped_from_seq = from_seq;
1335 if (unmapped_from_tei)
1336 *unmapped_from_tei = from_tei;
1337 if (final_unmapped)
1338 *final_unmapped = unmapped;
1339 return 0;
1340}
1341
1342static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1343 uint16_t port,
1344 struct osmo_sockaddr *dst)
1345{
1346 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1347}
1348
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001349/* If p is an Echo request, replace p's data with the matching response and
1350 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1351 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001352static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1353 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001354{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001355 if (p->type != GTP_ECHO_REQ)
1356 return 0;
1357
1358 static uint8_t echo_response_data[14] = {
1359 0x32, /* flags */
1360 GTP_ECHO_RSP,
1361 0x00, 14 - 8, /* Length in network byte order */
1362 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1363 0, 0, /* Seq, to be replaced */
1364 0, 0, /* no extensions */
1365 0x0e, /* Recovery IE */
1366 0 /* Recovery counter, to be replaced */
1367 };
1368 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1369 uint8_t *recovery = &echo_response_data[13];
1370
1371 *seq = hton16(p->seq);
1372 *recovery = hub->restart_counter;
1373
1374 *reply_buf = echo_response_data;
1375
1376 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001377}
1378
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001379struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1380 const struct osmo_sockaddr *addr);
1381
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001382/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1383 * address to forward to. Return a pointer to the osmo_fd, but copy the
1384 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1385 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1386 * bytes to forward, 0 or less on failure. */
1387int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1388 unsigned int plane_idx,
1389 const struct osmo_sockaddr *from_addr,
1390 uint8_t *buf,
1391 size_t received,
1392 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001393 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001394 struct osmo_fd **to_ofd,
1395 struct osmo_sockaddr *to_addr)
1396{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001397 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1398 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
1399 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1400 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1401
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001402 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1403 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001404 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001405 gtphub_plane_idx_names[plane_idx],
1406 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001407
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408 static struct gtp_packet_desc p;
1409 gtp_decode(buf, received, plane_idx, &p);
1410
1411 if (p.rc <= 0)
1412 return -1;
1413
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001414 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1415
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001416 int reply_len;
1417 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1418 if (reply_len > 0) {
1419 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001420 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001421 *to_ofd = &from_bind->ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001422 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001423 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001424 if (reply_len < 0)
1425 return -1;
1426
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001427 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001428
1429 /* If a GGSN proxy is configured, check that it's indeed that proxy
1430 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1431 * gtphub, so no-one else is allowed to talk to us from that side. */
1432 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1433 if (ggsn) {
1434 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001435 LOG(LOGL_ERROR,
1436 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001437 " received on GGSN bind is from another sender:"
1438 " proxy: %s sender: %s\n",
1439 gtphub_port_str(ggsn),
1440 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001441 return -1;
1442 }
1443 }
1444
1445 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001446 /* Find a GGSN peer with a matching address. The sender's port
1447 * may in fact differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001448 ggsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001449 }
1450
1451 /* If any PDP context has been created, we already have an entry for
1452 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1453 * us about. */
1454 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001455 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1456 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001457 return -1;
1458 }
1459
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001460 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001461
1462 struct gtphub_peer_port *sgsn_from_seq;
1463 struct gtphub_peer_port *sgsn;
1464 if (gtphub_unmap(hub, &p, ggsn,
1465 hub->sgsn_proxy[plane_idx],
1466 &sgsn, &sgsn_from_seq,
1467 NULL /* not interested, got it in &sgsn already */
1468 )
1469 != 0) {
1470 return -1;
1471 }
1472
1473 if (!sgsn) {
1474 /* A GGSN initiated request would go to a known TEI. So this is
1475 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001476 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001477 return -1;
1478 }
1479
1480 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001481 /* This may be a Create PDP Context response. If it is, there
1482 * are other addresses in the GTP message to set up apart from
1483 * the sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001484 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
1485 &p, now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001486 != 0)
1487 return -1;
1488 }
1489
1490 gtphub_check_restart_counter(hub, &p, ggsn);
1491 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1492
1493 /* If the GGSN is replying to an SGSN request, the sequence nr has
1494 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1495 * create a new mapping. */
1496 if (!sgsn_from_seq)
1497 gtphub_map_seq(&p, ggsn, sgsn, now);
1498
1499 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001500
1501 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001502
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001503 if (received) {
1504 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1505 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1506 received);
1507 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001508 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001509 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001510 return received;
1511}
1512
1513static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1514{
1515 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1516 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001517 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1518 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001519
1520 if (!(what & BSC_FD_READ))
1521 return 0;
1522
1523 struct gtphub *hub = from_sgsns_ofd->data;
1524
1525 static uint8_t buf[4096];
1526 struct osmo_sockaddr from_addr;
1527 struct osmo_sockaddr to_addr;
1528 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001529 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001530 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001531
1532 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1533 if (len < 1)
1534 return 0;
1535
1536 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1537 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001538 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001539 if (len < 1)
1540 return 0;
1541
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001542 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543}
1544
1545/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1546int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1547 unsigned int plane_idx,
1548 const struct osmo_sockaddr *from_addr,
1549 uint8_t *buf,
1550 size_t received,
1551 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001552 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001553 struct osmo_fd **to_ofd,
1554 struct osmo_sockaddr *to_addr)
1555{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001556 struct gtphub_bind *from_bind_arr = hub->to_sgsns;
1557 struct gtphub_bind *to_bind_arr = hub->to_ggsns;
1558 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1559 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1560
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001561 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1562 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001563 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001564 gtphub_plane_idx_names[plane_idx],
1565 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001566
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001567 static struct gtp_packet_desc p;
1568 gtp_decode(buf, received, plane_idx, &p);
1569
1570 if (p.rc <= 0)
1571 return -1;
1572
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001573 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1574
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001575 int reply_len;
1576 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1577 if (reply_len > 0) {
1578 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001579 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001580 *to_ofd = &from_bind->ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001581 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001582 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001583 if (reply_len < 0)
1584 return -1;
1585
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001586 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001587
1588 /* If an SGSN proxy is configured, check that it's indeed that proxy
1589 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1590 * gtphub, so no-one else is allowed to talk to us from that side. */
1591 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1592 if (sgsn) {
1593 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001594 LOG(LOGL_ERROR,
1595 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001596 " received on GGSN bind is from another sender:"
1597 " proxy: %s sender: %s\n",
1598 gtphub_port_str(sgsn),
1599 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001600 return -1;
1601 }
1602 }
1603
1604 if (!sgsn) {
1605 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001606 * entry for this SGSN. The port may differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001607 sgsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001608 }
1609
1610 if (!sgsn) {
1611 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1612 * first contact without being known yet, so create the peer
1613 * struct for the current sender. */
1614 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001615 LOG(LOGL_ERROR,
1616 "User plane peer was not announced by PDP Context,"
1617 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001618 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001619 return -1;
1620 }
1621
1622 struct gsn_addr from_gsna;
1623 uint16_t from_port;
1624 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1625 return -1;
1626
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001627 sgsn = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001628 }
1629
1630 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001631 /* This could theoretically happen for invalid address data or
1632 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001633 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1634 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001635 return -1;
1636 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001637 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001638
1639 struct gtphub_peer_port *ggsn_from_seq;
1640 struct gtphub_peer_port *ggsn;
1641 if (gtphub_unmap(hub, &p, sgsn,
1642 hub->ggsn_proxy[plane_idx],
1643 &ggsn, &ggsn_from_seq,
1644 NULL /* not interested, got it in &ggsn already */
1645 )
1646 != 0) {
1647 return -1;
1648 }
1649
Neels Hofmeyra208c732015-11-11 19:26:09 +01001650 if (!ggsn) {
1651 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1652 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001653 }
1654
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001655 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001656 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001657 return -1;
1658 }
1659
1660 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001661 /* This may be a Create PDP Context requst. If it is, there are
1662 * other addresses in the GTP message to set up apart from the
1663 * sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001664 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
1665 &p, now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001666 != 0)
1667 return -1;
1668 }
1669
1670 gtphub_check_restart_counter(hub, &p, sgsn);
1671 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1672
1673 /* If the SGSN is replying to a GGSN request, the sequence nr has
1674 * already been unmapped above (unmap_ggsn != NULL), and we need not
1675 * create a new outgoing sequence map. */
1676 if (!ggsn_from_seq)
1677 gtphub_map_seq(&p, sgsn, ggsn, now);
1678
1679 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1680
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001681 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001682
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001683 if (received) {
1684 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1685 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1686 received);
1687 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001688 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001689 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001690 return received;
1691}
1692
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001693static void resolved_gssn_del_cb(struct expiring_item *expi)
1694{
1695 struct gtphub_resolved_ggsn *ggsn;
1696 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1697
1698 gtphub_port_ref_count_dec(ggsn->peer);
1699 llist_del(&ggsn->entry);
1700
1701 ggsn->expiry_entry.del_cb = 0;
1702 expiring_item_del(&ggsn->expiry_entry);
1703
1704 talloc_free(ggsn);
1705}
1706
1707void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1708 struct gsn_addr *resolved_addr,
1709 time_t now)
1710{
1711 struct gtphub_peer_port *pp;
1712 struct gtphub_resolved_ggsn *ggsn;
1713
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001714 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001715 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1716 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001717
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001718 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1719 resolved_addr, 2123);
1720 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001721 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1722 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001723 return;
1724 }
1725
1726 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1727 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001728 INIT_LLIST_HEAD(&ggsn->entry);
1729 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001730
1731 ggsn->peer = pp;
1732 gtphub_port_ref_count_inc(pp);
1733
1734 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001735 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001736
1737 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1738 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1739
1740 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1741}
1742
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001743static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1744{
1745 return pp->ref_count == 0;
1746}
1747
1748static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1749{
1750 struct gtphub_peer_port *pp, *npp;
1751 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1752 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001753 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001754 gtphub_port_str(pp));
1755 gtphub_peer_port_del(pp);
1756 }
1757 }
1758 return llist_empty(&pa->ports);
1759}
1760
1761static int gtphub_gc_peer(struct gtphub_peer *p)
1762{
1763 struct gtphub_peer_addr *pa, *npa;
1764 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1765 if (gtphub_gc_peer_addr(pa)) {
1766 gtphub_peer_addr_del(pa);
1767 }
1768 }
1769
1770 /* Note that there's a ref_count in each gtphub_peer_port instance
1771 * listed within p->addresses, referenced by TEI mappings from
1772 * hub->tei_map. As long as those don't expire, this peer will stay. */
1773
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001774 return llist_empty(&p->addresses)
1775 && nr_map_empty(&p->seq_map);
1776}
1777
1778static void gtphub_gc_bind(struct gtphub_bind *b)
1779{
1780 struct gtphub_peer *p, *n;
1781 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1782 if (gtphub_gc_peer(p)) {
1783 gtphub_peer_del(p);
1784 }
1785 }
1786}
1787
1788void gtphub_gc(struct gtphub *hub, time_t now)
1789{
1790 int expired;
1791 expired = expiry_tick(&hub->expire_seq_maps, now);
1792 expired += expiry_tick(&hub->expire_tei_maps, now);
1793
1794 /* ... */
1795
1796 if (expired) {
1797 int i;
1798 for (i = 0; i < GTPH_PLANE_N; i++) {
1799 gtphub_gc_bind(&hub->to_sgsns[i]);
1800 gtphub_gc_bind(&hub->to_ggsns[i]);
1801 }
1802 }
1803}
1804
1805static void gtphub_gc_cb(void *data)
1806{
1807 struct gtphub *hub = data;
1808 gtphub_gc(hub, gtphub_now());
1809 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1810}
1811
1812static void gtphub_gc_start(struct gtphub *hub)
1813{
1814 hub->gc_timer.cb = gtphub_gc_cb;
1815 hub->gc_timer.data = hub;
1816
1817 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1818}
1819
1820/* called by unit tests */
1821void gtphub_init(struct gtphub *hub)
1822{
1823 gtphub_zero(hub);
1824
1825 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001826 expiry_init(&hub->expire_tei_maps,
1827 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001828
1829 int plane_idx;
1830 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001831 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001832 nr_map_init(&hub->tei_map[plane_idx],
1833 &hub->tei_pool[plane_idx],
1834 &hub->expire_tei_maps);
1835
1836 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1837 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1838 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001839
1840 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1841 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1842 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1843 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001844}
1845
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001846/* For the test suite, this is kept separate from gtphub_stop(), which also
1847 * closes sockets. The test suite avoids using sockets and would cause
1848 * segfaults when trying to close uninitialized ofds. */
1849void gtphub_free(struct gtphub *hub)
1850{
1851 /* By expiring all mappings, a garbage collection should free
1852 * everything else. A gtphub_bind_free() will assert that everything is
1853 * indeed empty. */
1854 expiry_clear(&hub->expire_seq_maps);
1855 expiry_clear(&hub->expire_tei_maps);
1856
1857 int plane_idx;
1858 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1859 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
1860 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
1861
1862 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
1863 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
1864 }
1865}
1866
1867void gtphub_stop(struct gtphub *hub)
1868{
1869 int plane_idx;
1870 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1871 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
1872 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
1873 }
1874 gtphub_free(hub);
1875}
1876
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001877static int gtphub_make_proxy(struct gtphub *hub,
1878 struct gtphub_peer_port **pp,
1879 struct gtphub_bind *bind,
1880 const struct gtphub_cfg_addr *addr)
1881{
1882 if (!addr->addr_str)
1883 return 0;
1884
1885 struct gsn_addr gsna;
1886 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1887 return -1;
1888
1889 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1890
1891 /* This is *the* proxy. Make sure it is never expired. */
1892 gtphub_port_ref_count_inc(*pp);
1893 return 0;
1894}
1895
1896int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1897{
1898 int rc;
1899
1900 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001901
1902 /* If a Ctrl plane proxy is configured, ares will never be used. */
1903 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1904 if (gtphub_ares_init(hub) != 0) {
1905 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1906 return -1;
1907 }
1908 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001909
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001910 /* TODO set hub->restart_counter from external file. */
1911
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001912 int plane_idx;
1913 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1914 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1915 &cfg->to_ggsns[plane_idx],
1916 from_ggsns_read_cb, hub, plane_idx);
1917 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001918 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1919 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001920 return rc;
1921 }
1922
1923 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1924 &cfg->to_sgsns[plane_idx],
1925 from_sgsns_read_cb, hub, plane_idx);
1926 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001927 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1928 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001929 return rc;
1930 }
1931 }
1932
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001933 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1934 if (gtphub_make_proxy(hub,
1935 &hub->sgsn_proxy[plane_idx],
1936 &hub->to_sgsns[plane_idx],
1937 &cfg->sgsn_proxy[plane_idx])
1938 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001939 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001940 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001941 cfg->sgsn_proxy[plane_idx].addr_str,
1942 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001943 return -1;
1944 }
1945 if (gtphub_make_proxy(hub,
1946 &hub->ggsn_proxy[plane_idx],
1947 &hub->to_ggsns[plane_idx],
1948 &cfg->ggsn_proxy[plane_idx])
1949 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001950 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001951 return -1;
1952 }
1953 }
1954
1955 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1956 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001957 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001958 gtphub_plane_idx_names[plane_idx],
1959 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1960 }
1961
1962 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001963 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001964 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001965 gtphub_plane_idx_names[plane_idx],
1966 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1967 }
1968
1969 gtphub_gc_start(hub);
1970 return 0;
1971}
1972
1973static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1974 const struct gsn_addr *addr)
1975{
1976 struct gtphub_peer_addr *a;
1977 llist_for_each_entry(a, &peer->addresses, entry) {
1978 if (gsn_addr_same(&a->addr, addr))
1979 return a;
1980 }
1981 return NULL;
1982}
1983
1984static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1985 uint16_t port)
1986{
1987 OSMO_ASSERT(port);
1988 struct gtphub_peer_port *pp;
1989 llist_for_each_entry(pp, &a->ports, entry) {
1990 if (pp->port == port)
1991 return pp;
1992 }
1993 return NULL;
1994}
1995
1996static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1997 const struct gsn_addr *addr)
1998{
1999 struct gtphub_peer *peer;
2000 llist_for_each_entry(peer, &bind->peers, entry) {
2001 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2002 if (a)
2003 return a;
2004 }
2005 return NULL;
2006}
2007
2008static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2009 const struct gsn_addr *addr,
2010 uint16_t port)
2011{
2012 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2013 if (!a)
2014 return NULL;
2015 return gtphub_addr_find_port(a, port);
2016}
2017
2018struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2019 const struct osmo_sockaddr *addr)
2020{
2021 struct gsn_addr gsna;
2022 uint16_t port;
2023 gsn_addr_from_sockaddr(&gsna, &port, addr);
2024 return gtphub_port_find(bind, &gsna, port);
2025}
2026
2027static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2028 struct gtphub_bind *bind)
2029{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002030 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2031 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002032 OSMO_ASSERT(peer);
2033
2034 INIT_LLIST_HEAD(&peer->addresses);
2035
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002036 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002037 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
2038
2039 /* TODO use something random to pick the initial sequence nr.
2040 0x6d31 produces the ASCII character sequence 'm1', currently used in
2041 gtphub_nc_test.sh. */
2042 peer->seq_pool.last_nr = 0x6d31 - 1;
2043
2044 llist_add(&peer->entry, &bind->peers);
2045 return peer;
2046}
2047
2048static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2049 const struct gsn_addr *addr)
2050{
2051 struct gtphub_peer_addr *a;
2052 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2053 OSMO_ASSERT(a);
2054 a->peer = peer;
2055 gsn_addr_copy(&a->addr, addr);
2056 INIT_LLIST_HEAD(&a->ports);
2057 llist_add(&a->entry, &peer->addresses);
2058
2059 return a;
2060}
2061
2062static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2063 struct gtphub_bind *bind,
2064 const struct gsn_addr *addr)
2065{
2066 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2067 if (a)
2068 return a;
2069
2070 /* If we haven't found an address, that means we need to create an
2071 * entirely new peer for the new address. More addresses may be added
2072 * to this peer later, but not via this function. */
2073 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002074
2075 a = gtphub_peer_add_addr(peer, addr);
2076
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002077 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002078 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002079 gsn_addr_to_str(&a->addr));
2080
2081 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002082}
2083
2084static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2085 uint16_t port)
2086{
2087 struct gtphub_peer_port *pp;
2088
2089 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2090 OSMO_ASSERT(pp);
2091 pp->peer_addr = a;
2092 pp->port = port;
2093
2094 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2095 talloc_free(pp);
2096 return NULL;
2097 }
2098
2099 llist_add(&pp->entry, &a->ports);
2100
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002101 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002102 gsn_addr_to_str(&a->addr),
2103 (int)port);
2104
2105 return pp;
2106}
2107
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002108struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2109 struct gtphub_bind *bind,
2110 const struct gsn_addr *addr,
2111 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002112{
2113 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2114
2115 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2116 if (pp)
2117 return pp;
2118
2119 return gtphub_addr_add_port(a, port);
2120}
2121
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002122/* Find a GGSN peer with a matching address. If the address is known but the
2123 * port not, create a new port for that peer address. */
2124struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2125 const struct osmo_sockaddr *addr)
2126{
2127 struct gtphub_peer_addr *pa;
2128 struct gtphub_peer_port *pp;
2129
2130 struct gsn_addr gsna;
2131 uint16_t port;
2132 gsn_addr_from_sockaddr(&gsna, &port, addr);
2133
2134 pa = gtphub_addr_find(bind, &gsna);
2135 if (!pa)
2136 return NULL;
2137
2138 pp = gtphub_addr_find_port(pa, port);
2139
2140 if (!pp)
2141 pp = gtphub_addr_add_port(pa, port);
2142
2143 return pp;
2144}
2145
2146
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002147/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2148 * resolution should be possible but failed, and 1 if resolution was
2149 * successful. *pp will be set to NULL if <1 is returned. */
2150static int gtphub_resolve_ggsn(struct gtphub *hub,
2151 struct gtp_packet_desc *p,
2152 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002153{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002154 *pp = NULL;
2155
2156 /* TODO determine from message type whether IEs should be present? */
2157
2158 int rc;
2159 const char *imsi_str;
2160 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2161 if (rc < 1)
2162 return rc;
2163 OSMO_ASSERT(imsi_str);
2164
2165 const char *apn_str;
2166 rc = get_ie_apn_str(p->ie, &apn_str);
2167 if (rc < 1)
2168 return rc;
2169 OSMO_ASSERT(apn_str);
2170
2171 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2172 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173}
2174
2175
2176/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002177/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002178/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2179 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002180static int _osmo_getaddrinfo(struct addrinfo **result,
2181 uint16_t family, uint16_t type, uint8_t proto,
2182 const char *host, uint16_t port)
2183{
2184 struct addrinfo hints;
2185 char portbuf[16];
2186
2187 sprintf(portbuf, "%u", port);
2188 memset(&hints, '\0', sizeof(struct addrinfo));
2189 hints.ai_family = family;
2190 if (type == SOCK_RAW) {
2191 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2192 * SOCK_RAW and IPPROTO_GRE is used.
2193 */
2194 hints.ai_socktype = SOCK_DGRAM;
2195 hints.ai_protocol = IPPROTO_UDP;
2196 } else {
2197 hints.ai_socktype = type;
2198 hints.ai_protocol = proto;
2199 }
2200
2201 return getaddrinfo(host, portbuf, &hints, result);
2202}
2203
2204/* TODO move to osmocom/core/socket.c ? */
2205int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2206 uint16_t family, uint16_t type, uint8_t proto,
2207 const char *host, uint16_t port)
2208{
2209 struct addrinfo *res;
2210 int rc;
2211 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2212
2213 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002214 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002215 return -EINVAL;
2216 }
2217
2218 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2219 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2220 addr->l = res->ai_addrlen;
2221 freeaddrinfo(res);
2222
2223 return 0;
2224}
2225
2226int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2227 char *port_str, size_t port_str_len,
2228 const struct osmo_sockaddr *addr,
2229 int flags)
2230{
2231 int rc;
2232
2233 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2234 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2235 return -1;
2236 }
2237
2238 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002239 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2240 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002241 return -1;
2242 }
2243
2244 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2245 addr_str, addr_str_len,
2246 port_str, port_str_len,
2247 flags);
2248
2249 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002250 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2251 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2252 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002253
2254 return rc;
2255}
2256
2257const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2258 char *buf, size_t buf_len)
2259{
2260 const int portbuf_len = 6;
2261 OSMO_ASSERT(buf_len > portbuf_len);
2262 char *portbuf = buf + buf_len - portbuf_len;
2263 buf_len -= portbuf_len;
2264 if (osmo_sockaddr_to_strs(buf, buf_len,
2265 portbuf, portbuf_len,
2266 addr,
2267 NI_NUMERICHOST | NI_NUMERICSERV))
2268 return NULL;
2269
2270 char *pos = buf + strnlen(buf, buf_len-1);
2271 size_t len = buf_len - (pos - buf);
2272
2273 snprintf(pos, len, " port %s", portbuf);
2274 buf[buf_len-1] = '\0';
2275
2276 return buf;
2277}
2278
2279const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2280{
2281 static char buf[256];
2282 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2283 if (! result)
2284 return "(invalid)";
2285 return result;
2286}
2287
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002288int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2289 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002290{
2291 if (a == b)
2292 return 0;
2293 if (!a)
2294 return -1;
2295 if (!b)
2296 return 1;
2297 if (a->l != b->l) {
2298 /* Lengths are not the same, but determine the order. Will
2299 * anyone ever sort a list by osmo_sockaddr though...? */
2300 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2301 if (cmp == 0) {
2302 if (a->l < b->l)
2303 return -1;
2304 else
2305 return 1;
2306 }
2307 return cmp;
2308 }
2309 return memcmp(&a->a, &b->a, a->l);
2310}
2311
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002312void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2313 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002314{
2315 OSMO_ASSERT(src->l <= sizeof(dst->a));
2316 memcpy(&dst->a, &src->a, src->l);
2317 dst->l = src->l;
2318}