blob: 4200822aad531fc2817f83cab1bb1758f2bef9c8 [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <string.h>
23#include <errno.h>
24#include <inttypes.h>
25#include <time.h>
26#include <limits.h>
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +010027#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#include <gtp.h>
33#include <gtpie.h>
34
35#include <openbsc/gtphub.h>
36#include <openbsc/debug.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010037#include <openbsc/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020038
39#include <osmocom/core/utils.h>
40#include <osmocom/core/logging.h>
41#include <osmocom/core/socket.h>
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010042#include <osmocom/core/rate_ctr.h>
43#include <osmocom/core/stats.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020044
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010045
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020046static const int GTPH_GC_TICK_SECONDS = 1;
47
48void *osmo_gtphub_ctx;
49
Neels Hofmeyr063a8022015-11-16 14:35:13 +010050/* Convenience makro, note: only within this C file. */
51#define LOG(level, fmt, args...) \
52 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020053
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010054#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
55 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020056
57/* TODO move this to osmocom/core/select.h ? */
58typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
59
60/* TODO move this to osmocom/core/linuxlist.h ? */
61#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010062#define llist_first(head, type, entry) \
63 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020064
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +010065#define __llist_last(head) (((head)->next == (head)) ? NULL : (head)->prev)
66#define llist_last(head, type, entry) \
67 llist_entry(__llist_last(head), type, entry)
68
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
70
71enum gtp_rc {
72 GTP_RC_UNKNOWN = 0,
73 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010074 GTP_RC_PDU_C = 2, /* a real packet with IEs */
75 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020076
77 GTP_RC_TOOSHORT = -1,
78 GTP_RC_UNSUPPORTED_VERSION = -2,
79 GTP_RC_INVALID_IE = -3,
80};
81
82struct gtp_packet_desc {
83 union gtp_packet *data;
84 int data_len;
85 int header_len;
86 int version;
87 uint8_t type;
88 uint16_t seq;
89 uint32_t header_tei;
90 int rc; /* enum gtp_rc */
91 unsigned int plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010092 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020093 union gtpie_member *ie[GTPIE_SIZE];
94};
95
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010096
97/* counters */
98
99enum gtphub_counters_io {
100 GTPH_CTR_PKTS_IN = 0,
101 GTPH_CTR_PKTS_OUT,
102 GTPH_CTR_BYTES_IN,
103 GTPH_CTR_BYTES_OUT
104};
105
106static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
107 { "packets.in", "Packets ( In)" },
108 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100109 { "bytes.in", "Bytes ( In)" },
110 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100111};
112
113static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
114 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100115 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100116 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
117 .ctr_desc = gtphub_counters_io_desc,
118 .class_id = OSMO_STATS_CLASS_GLOBAL,
119};
120
121
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200122void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
123{
124 memcpy(gsna, src, sizeof(struct gsn_addr));
125}
126
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200127int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
128 const struct osmo_sockaddr *sa)
129{
130 char addr_str[256];
131 char port_str[6];
132
133 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
134 port_str, sizeof(port_str),
135 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
136 != 0) {
137 return -1;
138 }
139
140 if (port)
141 *port = atoi(port_str);
142
143 return gsn_addr_from_str(gsna, addr_str);
144}
145
146int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
147{
148 int af = AF_INET;
149 gsna->len = 4;
150 const char *pos = numeric_addr_str;
151 for (; *pos; pos++) {
152 if (*pos == ':') {
153 af = AF_INET6;
154 gsna->len = 16;
155 break;
156 }
157 }
158
159 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
160 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100161 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
162 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200163 return -1;
164 }
165 return 0;
166}
167
168const char *gsn_addr_to_str(const struct gsn_addr *gsna)
169{
170 static char buf[INET6_ADDRSTRLEN + 1];
171 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
172}
173
174const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
175 char *strbuf,
176 int strbuf_len)
177{
178 int af;
179 switch (gsna->len) {
180 case 4:
181 af = AF_INET;
182 break;
183 case 16:
184 af = AF_INET6;
185 break;
186 default:
187 return NULL;
188 }
189
190 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
191 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100192 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
193 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100194 strerror(errno),
195 (int)gsna->len,
196 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200197 }
198 return r;
199}
200
201int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
202{
203 if (a == b)
204 return 1;
205 if ((!a) || (!b))
206 return 0;
207 if (a->len != b->len)
208 return 0;
209 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
210}
211
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100212static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
213 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200214{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100215 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200216 return -1;
217
218 unsigned int len;
219 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
220 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
221 &len, gsna->buf, sizeof(gsna->buf))
222 != 0)
223 return -1;
224 gsna->len = len;
225 return 0;
226}
227
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100228static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
229 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200230{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100231 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200232 return -1;
233
234 int ie_idx;
235 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
236
237 if (ie_idx < 0)
238 return -1;
239
240 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
241 int ie_l = ntoh16(ie->l);
242 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100243 LOG(LOGL_ERROR, "Not implemented:"
244 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200245 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
246 return -1;
247 }
248
249 memcpy(ie->v, gsna->buf, (int)ie_l);
250 return 0;
251}
252
253/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
254 */
255void validate_gtp0_header(struct gtp_packet_desc *p)
256{
257 const struct gtp0_header *pheader = &(p->data->gtp0.h);
258 p->rc = GTP_RC_UNKNOWN;
259 p->header_len = 0;
260
261 OSMO_ASSERT(p->data_len >= 1);
262 OSMO_ASSERT(p->version == 0);
263
264 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100265 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200266 p->rc = GTP_RC_TOOSHORT;
267 return;
268 }
269
270 p->type = ntoh8(pheader->type);
271 p->seq = ntoh16(pheader->seq);
272 p->header_tei = 0; /* TODO */
273
274 if (p->data_len == GTP0_HEADER_SIZE) {
275 p->rc = GTP_RC_TINY;
276 p->header_len = GTP0_HEADER_SIZE;
277 return;
278 }
279
280 /* Check packet length field versus length of packet */
281 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100282 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
283 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100284 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
285 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200286 p->rc = GTP_RC_TOOSHORT;
287 return;
288 }
289
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100290 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200291 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100292 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200293}
294
295/* Validate GTP version 1 data, and update p->rc with the result, as well as
296 * p->header_len in case of a valid header. */
297void validate_gtp1_header(struct gtp_packet_desc *p)
298{
299 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
300 p->rc = GTP_RC_UNKNOWN;
301 p->header_len = 0;
302
303 OSMO_ASSERT(p->data_len >= 1);
304 OSMO_ASSERT(p->version == 1);
305
306 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
307 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100308 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200309 p->rc = GTP_RC_TOOSHORT;
310 return;
311 }
312
313 p->type = ntoh8(pheader->type);
314 p->header_tei = ntoh32(pheader->tei);
315 p->seq = ntoh16(pheader->seq);
316
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100317 LOG(LOGL_DEBUG, "GTPv1\n"
318 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
319 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
320 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
321 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
322 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
323 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
324 p->type, p->type,
325 ntoh16(pheader->length), ntoh16(pheader->length),
326 p->header_tei, p->header_tei,
327 p->seq, p->seq,
328 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200329 pheader->next, pheader->next);
330
331 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
332 p->rc = GTP_RC_TINY;
333 p->header_len = GTP1_HEADER_SIZE_SHORT;
334 return;
335 }
336
337 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100338 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
339 if (p->data_len != announced_len) {
340 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
341 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100342 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
343 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200344 p->rc = GTP_RC_TOOSHORT;
345 return;
346 }
347
Neels Hofmeyre921e322015-11-11 00:45:50 +0100348 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200349 p->header_len = GTP1_HEADER_SIZE_LONG;
350}
351
352/* Examine whether p->data of size p->data_len has a valid GTP header. Set
353 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
354 * gtp_rc). p->data must point at a buffer with p->data_len set. */
355void validate_gtp_header(struct gtp_packet_desc *p)
356{
357 p->rc = GTP_RC_UNKNOWN;
358
359 /* Need at least 1 byte in order to check version */
360 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100361 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
362 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200363 p->rc = GTP_RC_TOOSHORT;
364 return;
365 }
366
367 p->version = p->data->flags >> 5;
368
369 switch (p->version) {
370 case 0:
371 validate_gtp0_header(p);
372 break;
373 case 1:
374 validate_gtp1_header(p);
375 break;
376 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100377 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200378 p->rc = GTP_RC_UNSUPPORTED_VERSION;
379 break;
380 }
381}
382
383
384/* Return the value of the i'th IMSI IEI by copying to *imsi.
385 * The first IEI is reached by passing i = 0.
386 * imsi must point at allocated space of (at least) 8 bytes.
387 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100388static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200389{
390 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
391}
392
393/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100394static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200395{
396 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
397}
398
399static char imsi_digit_to_char(uint8_t nibble)
400{
401 nibble &= 0x0f;
402 if (nibble > 9)
403 return (nibble == 0x0f) ? '\0' : '?';
404 return '0' + nibble;
405}
406
407/* Return a human readable IMSI string, in a static buffer.
408 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100409static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200410{
411 static char str[17];
412 int i;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100413 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200414
415 for (i = 0; i < 8; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100416 c = imsi_digit_to_char(imsi[i]);
417 if (c == '?')
418 return -1;
419 str[2*i] = c;
420
421 c = imsi_digit_to_char(imsi[i] >> 4);
422 if (c == '?')
423 return -1;
424 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200425 }
426 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100427 *imsi_str = str;
428 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200429}
430
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100431/* Return 0 if not present, 1 if present and decoded successfully, -1 if
432 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100433static int get_ie_imsi_str(union gtpie_member *ie[], int i,
434 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100435{
436 uint8_t imsi_buf[8];
437 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100438 return 0;
439 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100440}
441
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100442/* Return 0 if not present, 1 if present and decoded successfully, -1 if
443 * present but cannot be decoded. */
444static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100445{
446 static char apn_buf[GSM_APN_LENGTH];
447 unsigned int len;
448 if (gtpie_gettlv(ie, GTPIE_APN, 0,
449 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100450 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100451
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100452 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100453 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
454 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100455 return -1;
456 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100457
458 if (len > (sizeof(apn_buf) - 1))
459 len = sizeof(apn_buf) - 1;
460 apn_buf[len] = '\0';
461
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100462 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
463 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100464 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
465 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 return -1;
467 }
468 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100469}
470
471
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200472/* Validate header, and index information elements. Write decoded packet
473 * information to *res. res->data will point at the given data buffer. On
474 * error, p->rc is set <= 0 (see enum gtp_rc). */
475static void gtp_decode(const uint8_t *data, int data_len,
476 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100477 struct gtp_packet_desc *res,
478 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200479{
480 ZERO_STRUCT(res);
481 res->data = (union gtp_packet*)data;
482 res->data_len = data_len;
483 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100484 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200485
486 validate_gtp_header(res);
487
488 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100489 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200490 return;
491 }
492
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100493 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200494
Neels Hofmeyre921e322015-11-11 00:45:50 +0100495 if (from_plane_idx == GTPH_PLANE_USER) {
496 res->rc = GTP_RC_PDU_U;
497 return;
498 }
499
500 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100501 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200502 return;
503 }
504
505 if (gtpie_decaps(res->ie, res->version,
506 (void*)(data + res->header_len),
507 res->data_len - res->header_len) != 0) {
508 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100509 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
510 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200511 return;
512 }
513
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100514#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100515 /* TODO if (<loglevel is debug>) { ...
516 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200517 int i;
518
519 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100520 const char *imsi;
521 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200522 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100523 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200524 }
525
526 for (i = 0; i < 10; i++) {
527 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100528 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200529 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100530 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200531 }
532
533 for (i = 0; i < 2; i++) {
534 struct gsn_addr addr;
535 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100536 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200537 }
538
539 for (i = 0; i < 10; i++) {
540 uint32_t tei;
541 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
542 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100543 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200544 tei, tei);
545 }
546
547 for (i = 0; i < 10; i++) {
548 uint32_t tei;
549 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
550 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100551 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200552 tei, tei);
553 }
554#endif
555}
556
557
558/* expiry */
559
560void expiry_init(struct expiry *exq, int expiry_in_seconds)
561{
562 ZERO_STRUCT(exq);
563 exq->expiry_in_seconds = expiry_in_seconds;
564 INIT_LLIST_HEAD(&exq->items);
565}
566
567void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
568{
569 item->expiry = now + exq->expiry_in_seconds;
570
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100571 OSMO_ASSERT(llist_empty(&exq->items)
572 || (item->expiry
573 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
574
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200575 /* Add/move to the tail to always sort by expiry, ascending. */
576 llist_del(&item->entry);
577 llist_add_tail(&item->entry, &exq->items);
578}
579
580int expiry_tick(struct expiry *exq, time_t now)
581{
582 int expired = 0;
583 struct expiring_item *m, *n;
584 llist_for_each_entry_safe(m, n, &exq->items, entry) {
585 if (m->expiry <= now) {
586 expiring_item_del(m);
587 expired ++;
588 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200589 /* The items are added sorted by expiry. So when we hit
590 * an unexpired entry, only more unexpired ones will
591 * follow. */
592 break;
593 }
594 }
595 return expired;
596}
597
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100598void expiry_clear(struct expiry *exq)
599{
600 struct expiring_item *m, *n;
601 llist_for_each_entry_safe(m, n, &exq->items, entry) {
602 expiring_item_del(m);
603 }
604}
605
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200606void expiring_item_init(struct expiring_item *item)
607{
608 ZERO_STRUCT(item);
609 INIT_LLIST_HEAD(&item->entry);
610}
611
612void expiring_item_del(struct expiring_item *item)
613{
614 OSMO_ASSERT(item);
615 llist_del(&item->entry);
616 INIT_LLIST_HEAD(&item->entry);
617 if (item->del_cb) {
618 /* avoid loops */
619 del_cb_t del_cb = item->del_cb;
620 item->del_cb = 0;
621 (del_cb)(item);
622 }
623}
624
625
626/* nr_map, nr_pool */
627
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100628void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200629{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100630 *pool = (struct nr_pool){
631 .nr_min = nr_min,
632 .nr_max = nr_max,
633 .last_nr = nr_max
634 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200635}
636
637nr_t nr_pool_next(struct nr_pool *pool)
638{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100639 if (pool->last_nr >= pool->nr_max)
640 pool->last_nr = pool->nr_min;
641 else
642 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200643
644 return pool->last_nr;
645}
646
647void nr_map_init(struct nr_map *map, struct nr_pool *pool,
648 struct expiry *exq)
649{
650 ZERO_STRUCT(map);
651 map->pool = pool;
652 map->add_items_to_expiry = exq;
653 INIT_LLIST_HEAD(&map->mappings);
654}
655
656void nr_mapping_init(struct nr_mapping *m)
657{
658 ZERO_STRUCT(m);
659 INIT_LLIST_HEAD(&m->entry);
660 expiring_item_init(&m->expiry_entry);
661}
662
663void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
664{
665 /* Generate a mapped number */
666 mapping->repl = nr_pool_next(map->pool);
667
668 /* Add to the tail to always yield a list sorted by expiry, in
669 * ascending order. */
670 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100671 nr_map_refresh(map, mapping, now);
672}
673
674void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
675{
676 if (!map->add_items_to_expiry)
677 return;
678 expiry_add(map->add_items_to_expiry,
679 &mapping->expiry_entry,
680 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200681}
682
683void nr_map_clear(struct nr_map *map)
684{
685 struct nr_mapping *m;
686 struct nr_mapping *n;
687 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
688 nr_mapping_del(m);
689 }
690}
691
692int nr_map_empty(const struct nr_map *map)
693{
694 return llist_empty(&map->mappings);
695}
696
697struct nr_mapping *nr_map_get(const struct nr_map *map,
698 void *origin, nr_t nr_orig)
699{
700 struct nr_mapping *mapping;
701 llist_for_each_entry(mapping, &map->mappings, entry) {
702 if ((mapping->origin == origin)
703 && (mapping->orig == nr_orig))
704 return mapping;
705 }
706 /* Not found. */
707 return NULL;
708}
709
710struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
711{
712 struct nr_mapping *mapping;
713 llist_for_each_entry(mapping, &map->mappings, entry) {
714 if (mapping->repl == nr_repl) {
715 return mapping;
716 }
717 }
718 /* Not found. */
719 return NULL;
720}
721
722void nr_mapping_del(struct nr_mapping *mapping)
723{
724 OSMO_ASSERT(mapping);
725 llist_del(&mapping->entry);
726 INIT_LLIST_HEAD(&mapping->entry);
727 expiring_item_del(&mapping->expiry_entry);
728}
729
730
731/* gtphub */
732
733const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
734 "CTRL",
735 "USER",
736};
737
738const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
739 2123,
740 2152,
741};
742
743time_t gtphub_now(void)
744{
745 struct timespec now_tp;
746 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
747 return now_tp.tv_sec;
748}
749
750/* Remove a gtphub_peer from its list and free it. */
751static void gtphub_peer_del(struct gtphub_peer *peer)
752{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100753 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200754 nr_map_clear(&peer->seq_map);
755 llist_del(&peer->entry);
756 talloc_free(peer);
757}
758
759static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
760{
761 OSMO_ASSERT(llist_empty(&pa->ports));
762 llist_del(&pa->entry);
763 talloc_free(pa);
764}
765
766static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
767{
768 OSMO_ASSERT(pp->ref_count == 0);
769 llist_del(&pp->entry);
770 talloc_free(pp);
771}
772
773/* From the information in the gtp_packet_desc, return the address of a GGSN.
774 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100775static int gtphub_resolve_ggsn(struct gtphub *hub,
776 struct gtp_packet_desc *p,
777 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200778
779/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100780struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
781 const char *imsi_str,
782 const char *apn_ni_str);
783int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200784
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200785static void gtphub_zero(struct gtphub *hub)
786{
787 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100788 INIT_LLIST_HEAD(&hub->ggsn_lookups);
789 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200790}
791
792static int gtphub_sock_init(struct osmo_fd *ofd,
793 const struct gtphub_cfg_addr *addr,
794 osmo_fd_cb_t cb,
795 void *data,
796 int ofd_id)
797{
798 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100799 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200800 return -1;
801 }
802 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100803 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200804 return -1;
805 }
806
807 ofd->when = BSC_FD_READ;
808 ofd->cb = cb;
809 ofd->data = data;
810 ofd->priv_nr = ofd_id;
811
812 int rc;
813 rc = osmo_sock_init_ofd(ofd,
814 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
815 addr->addr_str, addr->port,
816 OSMO_SOCK_F_BIND);
817 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100818 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
819 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200820 return -1;
821 }
822
823 return 0;
824}
825
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100826static void gtphub_sock_close(struct osmo_fd *ofd)
827{
828 close(ofd->fd);
829 osmo_fd_unregister(ofd);
830 ofd->cb = NULL;
831}
832
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200833static void gtphub_bind_init(struct gtphub_bind *b)
834{
835 ZERO_STRUCT(b);
836
837 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100838
839 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
840 &gtphub_ctrg_io_desc, 0);
841 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200842}
843
844static int gtphub_bind_start(struct gtphub_bind *b,
845 const struct gtphub_cfg_bind *cfg,
846 osmo_fd_cb_t cb, void *cb_data,
847 unsigned int ofd_id)
848{
849 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
850 return -1;
851 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
852 return -1;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100853 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200854 return 0;
855}
856
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100857static void gtphub_bind_free(struct gtphub_bind *b)
858{
859 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100860 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100861}
862
863static void gtphub_bind_stop(struct gtphub_bind *b) {
864 gtphub_sock_close(&b->ofd);
865 gtphub_bind_free(b);
866}
867
Neels Hofmeyr40348972015-11-17 12:22:28 +0100868/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200869 * Return the number of bytes read, zero on error. */
870static int gtphub_read(const struct osmo_fd *from,
871 struct osmo_sockaddr *from_addr,
872 uint8_t *buf, size_t buf_len)
873{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100874 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200875
Neels Hofmeyr40348972015-11-17 12:22:28 +0100876 /* recvfrom requires the available length set in *from_addr_len. */
877 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200878 errno = 0;
879 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100880 (struct sockaddr*)&from_addr->a,
881 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200882 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
883 * is not truncated. Then maybe reduce buf's size. */
884
885 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100886 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
887 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200888 return 0;
889 }
890
Neels Hofmeyr40348972015-11-17 12:22:28 +0100891 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
892 (int)received, osmo_sockaddr_to_str(from_addr),
893 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200894
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200895 return received;
896}
897
898inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
899{
900 OSMO_ASSERT(pp->ref_count < UINT_MAX);
901 pp->ref_count++;
902}
903
904inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
905{
906 OSMO_ASSERT(pp->ref_count > 0);
907 pp->ref_count--;
908}
909
910inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
911{
912 OSMO_ASSERT(p->version == 1);
913 p->data->gtp1l.h.seq = hton16(seq);
914 p->seq = seq;
915}
916
917inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
918{
919 OSMO_ASSERT(p->version == 1);
920 p->data->gtp1l.h.tei = hton32(tei);
921 p->header_tei = tei;
922}
923
924static void gtphub_mapping_del_cb(struct expiring_item *expi);
925
926static struct nr_mapping *gtphub_mapping_new()
927{
928 struct nr_mapping *nrm;
929 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
930 OSMO_ASSERT(nrm);
931
932 nr_mapping_init(nrm);
933 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
934 return nrm;
935}
936
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100937static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
938 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200939{
940 if (llist_empty(&peer->addresses))
941 return "(addressless)";
942
943 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
944 struct gtphub_peer_addr,
945 entry);
946 return gsn_addr_to_strb(&a->addr, buf, buflen);
947}
948
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100949static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
950 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200951{
952 if (!port)
953 return "(null port)";
954
955 snprintf(buf, buflen, "%s port %d",
956 gsn_addr_to_str(&port->peer_addr->addr),
957 (int)port->port);
958 return buf;
959}
960
961const char *gtphub_peer_str(struct gtphub_peer *peer)
962{
963 static char buf[256];
964 return gtphub_peer_strb(peer, buf, sizeof(buf));
965}
966
967const char *gtphub_peer_str2(struct gtphub_peer *peer)
968{
969 static char buf[256];
970 return gtphub_peer_strb(peer, buf, sizeof(buf));
971}
972
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100973const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200974{
975 static char buf[256];
976 return gtphub_port_strb(port, buf, sizeof(buf));
977}
978
979static const char *gtphub_port_str2(struct gtphub_peer_port *port)
980{
981 static char buf[256];
982 return gtphub_port_strb(port, buf, sizeof(buf));
983}
984
985static void gtphub_mapping_del_cb(struct expiring_item *expi)
986{
987 expi->del_cb = 0; /* avoid recursion loops */
988
989 struct nr_mapping *nrm = container_of(expi,
990 struct nr_mapping,
991 expiry_entry);
992 llist_del(&nrm->entry);
993 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
994
995 /* Just for log */
996 struct gtphub_peer_port *from = nrm->origin;
997 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100998 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200999 (int)nrm->expiry_entry.expiry,
1000 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001001 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001002
1003 gtphub_port_ref_count_dec(from);
1004
1005 talloc_free(nrm);
1006}
1007
1008static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1009 struct gtphub_peer_port *from,
1010 nr_t orig_nr,
1011 time_t now)
1012{
1013 struct nr_mapping *nrm;
1014
1015 nrm = nr_map_get(map, from, orig_nr);
1016
1017 if (!nrm) {
1018 nrm = gtphub_mapping_new();
1019 nrm->orig = orig_nr;
1020 nrm->origin = from;
1021 nr_map_add(map, nrm, now);
1022 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001023 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001024 gtphub_port_str(from),
1025 (int)(nrm->orig), (int)(nrm->repl));
1026 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001027 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001028 }
1029
1030 OSMO_ASSERT(nrm);
1031 return nrm;
1032}
1033
1034static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
1035 int plane_idx,
1036 struct gtphub_peer_port *from,
1037 uint32_t orig_tei,
1038 time_t now)
1039{
1040 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
1041 from, orig_tei, now);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001042 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001043 gtphub_plane_idx_names[plane_idx],
1044 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001045 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001046
1047 return (uint32_t)nrm->repl;
1048}
1049
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001050static void gtphub_map_seq(struct gtp_packet_desc *p,
1051 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001052 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001053{
1054 /* Store a mapping in to_peer's map, so when we later receive a GTP
1055 * packet back from to_peer, the seq nr can be unmapped back to its
1056 * origin (from_peer here). */
1057 struct nr_mapping *nrm;
1058 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001059 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001060
1061 /* Change the GTP packet to yield the new, mapped seq nr */
1062 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001063}
1064
1065static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1066 struct gtphub_peer_port *responding_port)
1067{
1068 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001069 struct nr_mapping *nrm =
1070 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1071 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001072 if (!nrm)
1073 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001074 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1075 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001076 set_seq(p, nrm->orig);
1077 return nrm->origin;
1078}
1079
1080static void gtphub_check_restart_counter(struct gtphub *hub,
1081 struct gtp_packet_desc *p,
1082 struct gtphub_peer_port *from)
1083{
1084 /* TODO */
1085 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1086 * that doesn't match the peer's previously sent restart counter, clear
1087 * that peer and cancel PDP contexts. */
1088}
1089
1090static void gtphub_map_restart_counter(struct gtphub *hub,
1091 struct gtp_packet_desc *p,
1092 struct gtphub_peer_port *from,
1093 struct gtphub_peer_port *to)
1094{
1095 /* TODO */
1096}
1097
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001098static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1099 struct gtphub *hub,
1100 struct gtp_packet_desc *p,
1101 struct gtphub_peer_port *from_port)
1102{
1103 OSMO_ASSERT(p->version == 1);
1104 *to_port_p = NULL;
1105
1106 /* If the header's TEI is zero, no PDP context has been established
1107 * yet. If nonzero, a mapping should actually already exist for this
1108 * TEI, since it must have been announced in a PDP context creation. */
1109 uint32_t tei = p->header_tei;
1110 if (!tei)
1111 return 0;
1112
1113 /* to_peer has previously announced a TEI, which was stored and
1114 * mapped in from_peer's tei_map. */
1115 struct nr_mapping *nrm;
1116 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1117 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001118 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1119 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001120 return -1;
1121 }
1122
1123 struct gtphub_peer_port *to_port = nrm->origin;
1124 uint32_t unmapped_tei = nrm->orig;
1125 set_tei(p, unmapped_tei);
1126
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001127 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1128 gtphub_port_str(from_port),
1129 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001130 gtphub_port_str2(to_port));
1131
1132 *to_port_p = to_port;
1133 return 0;
1134}
1135
1136/* Read GSN address IEs from p, and make sure these peer addresses exist in
1137 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1138 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1139 * the packet p. */
1140static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001141 struct gtphub_bind from_bind_arr[],
1142 struct gtphub_bind to_bind_arr[],
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001143 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001144{
1145 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1146
1147 int rc;
1148 int plane_idx;
1149
1150 switch (p->type) {
1151 case GTP_CREATE_PDP_REQ:
1152 case GTP_CREATE_PDP_RSP:
1153 /* Go for it below */
1154 break;
1155 default:
1156 /* Nothing to do for this message type. */
1157 return 0;
1158 }
1159
1160 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1161
1162 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1163 plane_nrs_match_GSN_addr_IE_indices);
1164
1165 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1166 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1167
1168 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1169 struct gsn_addr addr_from_ie;
1170 uint32_t tei_from_ie;
1171 int ie_idx;
1172
1173 /* Fetch GSN Address and TEI from IEs */
1174 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1175 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001176 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1177 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001178 return -1;
1179 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001180 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001181 gtphub_plane_idx_names[plane_idx],
1182 gsn_addr_to_str(&addr_from_ie),
1183 addr_from_ie.len);
1184
1185 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1186 if (ie_idx < 0) {
1187 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001188 LOG(LOGL_ERROR,
1189 "Create PDP Context message invalid:"
1190 " missing IE %d\n",
1191 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001192 return -1;
1193 }
1194 tei_from_ie = 0;
1195 }
1196 else
1197 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1198
1199 /* Make sure an entry for this peer address with default port
1200 * exists */
1201 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001202 gtphub_port_have(hub, &from_bind_arr[plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001203 &addr_from_ie,
1204 gtphub_plane_idx_default_port[plane_idx]);
1205
1206 if (tei_from_ie) {
1207 /* Create TEI mapping and replace in GTP packet IE */
1208 uint32_t mapped_tei =
1209 gtphub_tei_mapping_have(hub, plane_idx,
1210 peer_from_ie,
1211 tei_from_ie,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001212 p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001213 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1214 }
1215
1216 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001217 rc = gsn_addr_put(&to_bind_arr[plane_idx].local_addr, p,
1218 plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001219 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001220 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1221 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001222 return -1;
1223 }
1224 }
1225
1226 return 0;
1227}
1228
1229static int gtphub_write(const struct osmo_fd *to,
1230 const struct osmo_sockaddr *to_addr,
1231 const uint8_t *buf, size_t buf_len)
1232{
1233 errno = 0;
1234 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1235 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001236 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001237
1238 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001239 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001240 return -EINVAL;
1241 }
1242
1243 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001244 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1245 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001246 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001247 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1248 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001249
1250 return 0;
1251}
1252
1253static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1254{
1255 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1256 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001257 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1258 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001259 if (!(what & BSC_FD_READ))
1260 return 0;
1261
1262 struct gtphub *hub = from_ggsns_ofd->data;
1263
1264 static uint8_t buf[4096];
1265 struct osmo_sockaddr from_addr;
1266 struct osmo_sockaddr to_addr;
1267 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001268 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001269 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001270
1271 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1272 if (len < 1)
1273 return 0;
1274
1275 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1276 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001277 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001278 if (len < 1)
1279 return 0;
1280
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001281 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001282}
1283
1284static int gtphub_unmap(struct gtphub *hub,
1285 struct gtp_packet_desc *p,
1286 struct gtphub_peer_port *from,
1287 struct gtphub_peer_port *to_proxy,
1288 struct gtphub_peer_port **final_unmapped,
1289 struct gtphub_peer_port **unmapped_from_seq,
1290 struct gtphub_peer_port **unmapped_from_tei)
1291{
1292 /* Always (try to) unmap sequence and TEI numbers, which need to be
1293 * replaced in the packet. Either way, give precedence to the proxy, if
1294 * configured. */
1295
1296 struct gtphub_peer_port *from_seq = NULL;
1297 struct gtphub_peer_port *from_tei = NULL;
1298 struct gtphub_peer_port *unmapped = NULL;
1299
1300 if (unmapped_from_seq)
1301 *unmapped_from_seq = from_seq;
1302 if (unmapped_from_tei)
1303 *unmapped_from_tei = from_tei;
1304 if (final_unmapped)
1305 *final_unmapped = unmapped;
1306
1307 from_seq = gtphub_unmap_seq(p, from);
1308
1309 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1310 return -1;
1311
1312 struct gtphub_peer *from_peer = from->peer_addr->peer;
1313 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001314 LOG(LOGL_DEBUG,
1315 "Seq unmap and TEI unmap yield two different peers."
1316 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001317 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1318 gtphub_plane_idx_names[p->plane_idx],
1319 gtphub_peer_str(from_peer),
1320 (int)p->seq,
1321 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001322 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001323 gtphub_port_str2(from_tei)
1324 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001325 }
1326 unmapped = (from_seq? from_seq : from_tei);
1327
1328 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001329 LOG(LOGL_NOTICE,
1330 "Unmap yields a different peer than the configured proxy."
1331 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001332 " unmapped: %s proxy: %s\n",
1333 gtphub_port_str(unmapped),
1334 gtphub_port_str2(to_proxy)
1335 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001336 }
1337 unmapped = (to_proxy? to_proxy : unmapped);
1338
1339 if (!unmapped) {
1340 /* Return no error, but returned pointers are all NULL. */
1341 return 0;
1342 }
1343
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001344 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001345 from_seq, from_tei, unmapped);
1346
1347 if (unmapped_from_seq)
1348 *unmapped_from_seq = from_seq;
1349 if (unmapped_from_tei)
1350 *unmapped_from_tei = from_tei;
1351 if (final_unmapped)
1352 *final_unmapped = unmapped;
1353 return 0;
1354}
1355
1356static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1357 uint16_t port,
1358 struct osmo_sockaddr *dst)
1359{
1360 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1361}
1362
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001363/* If p is an Echo request, replace p's data with the matching response and
1364 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1365 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001366static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1367 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001368{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001369 if (p->type != GTP_ECHO_REQ)
1370 return 0;
1371
1372 static uint8_t echo_response_data[14] = {
1373 0x32, /* flags */
1374 GTP_ECHO_RSP,
1375 0x00, 14 - 8, /* Length in network byte order */
1376 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1377 0, 0, /* Seq, to be replaced */
1378 0, 0, /* no extensions */
1379 0x0e, /* Recovery IE */
1380 0 /* Recovery counter, to be replaced */
1381 };
1382 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1383 uint8_t *recovery = &echo_response_data[13];
1384
1385 *seq = hton16(p->seq);
1386 *recovery = hub->restart_counter;
1387
1388 *reply_buf = echo_response_data;
1389
1390 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001391}
1392
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001393struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1394 const struct osmo_sockaddr *addr);
1395
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001396/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1397 * address to forward to. Return a pointer to the osmo_fd, but copy the
1398 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1399 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1400 * bytes to forward, 0 or less on failure. */
1401int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1402 unsigned int plane_idx,
1403 const struct osmo_sockaddr *from_addr,
1404 uint8_t *buf,
1405 size_t received,
1406 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001407 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408 struct osmo_fd **to_ofd,
1409 struct osmo_sockaddr *to_addr)
1410{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001411 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1412 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
1413 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1414 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1415
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001416 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1417 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001418 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001419 gtphub_plane_idx_names[plane_idx],
1420 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001421
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001422 static struct gtp_packet_desc p;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001423 gtp_decode(buf, received, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001424
1425 if (p.rc <= 0)
1426 return -1;
1427
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001428 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1429
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001430 int reply_len;
1431 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1432 if (reply_len > 0) {
1433 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001434 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001435 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001436
1437 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1438 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1439 reply_len);
1440 LOG(LOGL_DEBUG, "--> Echo response to GGSN: %d bytes to %s\n",
1441 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001442 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001443 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001444 if (reply_len < 0)
1445 return -1;
1446
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001447 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001448
1449 /* If a GGSN proxy is configured, check that it's indeed that proxy
1450 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1451 * gtphub, so no-one else is allowed to talk to us from that side. */
1452 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1453 if (ggsn) {
1454 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001455 LOG(LOGL_ERROR,
1456 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001457 " received on GGSN bind is from another sender:"
1458 " proxy: %s sender: %s\n",
1459 gtphub_port_str(ggsn),
1460 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001461 return -1;
1462 }
1463 }
1464
1465 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001466 /* Find a GGSN peer with a matching address. The sender's port
1467 * may in fact differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001468 ggsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001469 }
1470
1471 /* If any PDP context has been created, we already have an entry for
1472 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1473 * us about. */
1474 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001475 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1476 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001477 return -1;
1478 }
1479
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001480 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001481
1482 struct gtphub_peer_port *sgsn_from_seq;
1483 struct gtphub_peer_port *sgsn;
1484 if (gtphub_unmap(hub, &p, ggsn,
1485 hub->sgsn_proxy[plane_idx],
1486 &sgsn, &sgsn_from_seq,
1487 NULL /* not interested, got it in &sgsn already */
1488 )
1489 != 0) {
1490 return -1;
1491 }
1492
1493 if (!sgsn) {
1494 /* A GGSN initiated request would go to a known TEI. So this is
1495 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001496 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001497 return -1;
1498 }
1499
1500 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001501 /* This may be a Create PDP Context response. If it is, there
1502 * are other addresses in the GTP message to set up apart from
1503 * the sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001504 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001505 &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001506 != 0)
1507 return -1;
1508 }
1509
1510 gtphub_check_restart_counter(hub, &p, ggsn);
1511 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1512
1513 /* If the GGSN is replying to an SGSN request, the sequence nr has
1514 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1515 * create a new mapping. */
1516 if (!sgsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001517 gtphub_map_seq(&p, ggsn, sgsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001518
1519 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001520
1521 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001522
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001523 if (received) {
1524 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1525 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1526 received);
1527 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001528 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001529 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001530 return received;
1531}
1532
1533static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1534{
1535 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1536 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001537 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1538 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001539
1540 if (!(what & BSC_FD_READ))
1541 return 0;
1542
1543 struct gtphub *hub = from_sgsns_ofd->data;
1544
1545 static uint8_t buf[4096];
1546 struct osmo_sockaddr from_addr;
1547 struct osmo_sockaddr to_addr;
1548 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001549 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001550 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001551
1552 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1553 if (len < 1)
1554 return 0;
1555
1556 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1557 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001558 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001559 if (len < 1)
1560 return 0;
1561
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001562 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001563}
1564
1565/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1566int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1567 unsigned int plane_idx,
1568 const struct osmo_sockaddr *from_addr,
1569 uint8_t *buf,
1570 size_t received,
1571 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001572 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001573 struct osmo_fd **to_ofd,
1574 struct osmo_sockaddr *to_addr)
1575{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001576 struct gtphub_bind *from_bind_arr = hub->to_sgsns;
1577 struct gtphub_bind *to_bind_arr = hub->to_ggsns;
1578 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1579 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1580
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001581 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
1582 received);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001583 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001584 gtphub_plane_idx_names[plane_idx],
1585 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001586
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001587 static struct gtp_packet_desc p;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001588 gtp_decode(buf, received, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001589
1590 if (p.rc <= 0)
1591 return -1;
1592
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001593 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
1594
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001595 int reply_len;
1596 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1597 if (reply_len > 0) {
1598 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001599 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001600 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01001601
1602 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1603 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1604 reply_len);
1605 LOG(LOGL_DEBUG, "<-- Echo response to SGSN: %d bytes to %s\n",
1606 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001607 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001608 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001609 if (reply_len < 0)
1610 return -1;
1611
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001612 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001613
1614 /* If an SGSN proxy is configured, check that it's indeed that proxy
1615 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1616 * gtphub, so no-one else is allowed to talk to us from that side. */
1617 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1618 if (sgsn) {
1619 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001620 LOG(LOGL_ERROR,
1621 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001622 " received on GGSN bind is from another sender:"
1623 " proxy: %s sender: %s\n",
1624 gtphub_port_str(sgsn),
1625 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001626 return -1;
1627 }
1628 }
1629
1630 if (!sgsn) {
1631 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001632 * entry for this SGSN. The port may differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001633 sgsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001634 }
1635
1636 if (!sgsn) {
1637 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1638 * first contact without being known yet, so create the peer
1639 * struct for the current sender. */
1640 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001641 LOG(LOGL_ERROR,
1642 "User plane peer was not announced by PDP Context,"
1643 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001644 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001645 return -1;
1646 }
1647
1648 struct gsn_addr from_gsna;
1649 uint16_t from_port;
1650 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1651 return -1;
1652
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001653 sgsn = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001654 }
1655
1656 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001657 /* This could theoretically happen for invalid address data or
1658 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001659 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1660 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001661 return -1;
1662 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001663 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001664
1665 struct gtphub_peer_port *ggsn_from_seq;
1666 struct gtphub_peer_port *ggsn;
1667 if (gtphub_unmap(hub, &p, sgsn,
1668 hub->ggsn_proxy[plane_idx],
1669 &ggsn, &ggsn_from_seq,
1670 NULL /* not interested, got it in &ggsn already */
1671 )
1672 != 0) {
1673 return -1;
1674 }
1675
Neels Hofmeyra208c732015-11-11 19:26:09 +01001676 if (!ggsn) {
1677 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1678 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001679 }
1680
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001681 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001682 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001683 return -1;
1684 }
1685
1686 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001687 /* This may be a Create PDP Context requst. If it is, there are
1688 * other addresses in the GTP message to set up apart from the
1689 * sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001690 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001691 &p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001692 != 0)
1693 return -1;
1694 }
1695
1696 gtphub_check_restart_counter(hub, &p, sgsn);
1697 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1698
1699 /* If the SGSN is replying to a GGSN request, the sequence nr has
1700 * already been unmapped above (unmap_ggsn != NULL), and we need not
1701 * create a new outgoing sequence map. */
1702 if (!ggsn_from_seq)
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001703 gtphub_map_seq(&p, sgsn, ggsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001704
1705 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1706
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001707 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001708
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01001709 if (received) {
1710 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
1711 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
1712 received);
1713 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001714 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001715 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001716 return received;
1717}
1718
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001719static void resolved_gssn_del_cb(struct expiring_item *expi)
1720{
1721 struct gtphub_resolved_ggsn *ggsn;
1722 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1723
1724 gtphub_port_ref_count_dec(ggsn->peer);
1725 llist_del(&ggsn->entry);
1726
1727 ggsn->expiry_entry.del_cb = 0;
1728 expiring_item_del(&ggsn->expiry_entry);
1729
1730 talloc_free(ggsn);
1731}
1732
1733void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1734 struct gsn_addr *resolved_addr,
1735 time_t now)
1736{
1737 struct gtphub_peer_port *pp;
1738 struct gtphub_resolved_ggsn *ggsn;
1739
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001740 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001741 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1742 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001743
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001744 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1745 resolved_addr, 2123);
1746 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001747 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1748 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001749 return;
1750 }
1751
1752 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1753 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001754 INIT_LLIST_HEAD(&ggsn->entry);
1755 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001756
1757 ggsn->peer = pp;
1758 gtphub_port_ref_count_inc(pp);
1759
1760 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001761 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001762
1763 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01001764 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001765
1766 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1767}
1768
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001769static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1770{
1771 return pp->ref_count == 0;
1772}
1773
1774static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1775{
1776 struct gtphub_peer_port *pp, *npp;
1777 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1778 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001779 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001780 gtphub_port_str(pp));
1781 gtphub_peer_port_del(pp);
1782 }
1783 }
1784 return llist_empty(&pa->ports);
1785}
1786
1787static int gtphub_gc_peer(struct gtphub_peer *p)
1788{
1789 struct gtphub_peer_addr *pa, *npa;
1790 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1791 if (gtphub_gc_peer_addr(pa)) {
1792 gtphub_peer_addr_del(pa);
1793 }
1794 }
1795
1796 /* Note that there's a ref_count in each gtphub_peer_port instance
1797 * listed within p->addresses, referenced by TEI mappings from
1798 * hub->tei_map. As long as those don't expire, this peer will stay. */
1799
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001800 return llist_empty(&p->addresses)
1801 && nr_map_empty(&p->seq_map);
1802}
1803
1804static void gtphub_gc_bind(struct gtphub_bind *b)
1805{
1806 struct gtphub_peer *p, *n;
1807 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1808 if (gtphub_gc_peer(p)) {
1809 gtphub_peer_del(p);
1810 }
1811 }
1812}
1813
1814void gtphub_gc(struct gtphub *hub, time_t now)
1815{
1816 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01001817 expired = expiry_tick(&hub->expire_quickly, now);
1818 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001819
1820 /* ... */
1821
1822 if (expired) {
1823 int i;
1824 for (i = 0; i < GTPH_PLANE_N; i++) {
1825 gtphub_gc_bind(&hub->to_sgsns[i]);
1826 gtphub_gc_bind(&hub->to_ggsns[i]);
1827 }
1828 }
1829}
1830
1831static void gtphub_gc_cb(void *data)
1832{
1833 struct gtphub *hub = data;
1834 gtphub_gc(hub, gtphub_now());
1835 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1836}
1837
1838static void gtphub_gc_start(struct gtphub *hub)
1839{
1840 hub->gc_timer.cb = gtphub_gc_cb;
1841 hub->gc_timer.data = hub;
1842
1843 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1844}
1845
1846/* called by unit tests */
1847void gtphub_init(struct gtphub *hub)
1848{
1849 gtphub_zero(hub);
1850
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01001851 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
1852 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001853
1854 int plane_idx;
1855 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001856 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001857 nr_map_init(&hub->tei_map[plane_idx],
1858 &hub->tei_pool[plane_idx],
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01001859 &hub->expire_slowly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001860
1861 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1862 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1863 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001864
1865 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1866 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1867 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1868 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001869}
1870
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001871/* For the test suite, this is kept separate from gtphub_stop(), which also
1872 * closes sockets. The test suite avoids using sockets and would cause
1873 * segfaults when trying to close uninitialized ofds. */
1874void gtphub_free(struct gtphub *hub)
1875{
1876 /* By expiring all mappings, a garbage collection should free
1877 * everything else. A gtphub_bind_free() will assert that everything is
1878 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01001879 expiry_clear(&hub->expire_quickly);
1880 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001881
1882 int plane_idx;
1883 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1884 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
1885 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
1886
1887 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
1888 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
1889 }
1890}
1891
1892void gtphub_stop(struct gtphub *hub)
1893{
1894 int plane_idx;
1895 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1896 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
1897 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
1898 }
1899 gtphub_free(hub);
1900}
1901
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001902static int gtphub_make_proxy(struct gtphub *hub,
1903 struct gtphub_peer_port **pp,
1904 struct gtphub_bind *bind,
1905 const struct gtphub_cfg_addr *addr)
1906{
1907 if (!addr->addr_str)
1908 return 0;
1909
1910 struct gsn_addr gsna;
1911 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1912 return -1;
1913
1914 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1915
1916 /* This is *the* proxy. Make sure it is never expired. */
1917 gtphub_port_ref_count_inc(*pp);
1918 return 0;
1919}
1920
1921int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1922{
1923 int rc;
1924
1925 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001926
1927 /* If a Ctrl plane proxy is configured, ares will never be used. */
1928 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1929 if (gtphub_ares_init(hub) != 0) {
1930 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1931 return -1;
1932 }
1933 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001934
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001935 /* TODO set hub->restart_counter from external file. */
1936
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001937 int plane_idx;
1938 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1939 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1940 &cfg->to_ggsns[plane_idx],
1941 from_ggsns_read_cb, hub, plane_idx);
1942 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001943 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1944 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001945 return rc;
1946 }
1947
1948 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1949 &cfg->to_sgsns[plane_idx],
1950 from_sgsns_read_cb, hub, plane_idx);
1951 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001952 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1953 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001954 return rc;
1955 }
1956 }
1957
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001958 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1959 if (gtphub_make_proxy(hub,
1960 &hub->sgsn_proxy[plane_idx],
1961 &hub->to_sgsns[plane_idx],
1962 &cfg->sgsn_proxy[plane_idx])
1963 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001964 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001965 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001966 cfg->sgsn_proxy[plane_idx].addr_str,
1967 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001968 return -1;
1969 }
1970 if (gtphub_make_proxy(hub,
1971 &hub->ggsn_proxy[plane_idx],
1972 &hub->to_ggsns[plane_idx],
1973 &cfg->ggsn_proxy[plane_idx])
1974 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001975 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001976 return -1;
1977 }
1978 }
1979
1980 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1981 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001982 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001983 gtphub_plane_idx_names[plane_idx],
1984 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1985 }
1986
1987 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001988 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001989 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990 gtphub_plane_idx_names[plane_idx],
1991 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1992 }
1993
1994 gtphub_gc_start(hub);
1995 return 0;
1996}
1997
1998static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1999 const struct gsn_addr *addr)
2000{
2001 struct gtphub_peer_addr *a;
2002 llist_for_each_entry(a, &peer->addresses, entry) {
2003 if (gsn_addr_same(&a->addr, addr))
2004 return a;
2005 }
2006 return NULL;
2007}
2008
2009static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2010 uint16_t port)
2011{
2012 OSMO_ASSERT(port);
2013 struct gtphub_peer_port *pp;
2014 llist_for_each_entry(pp, &a->ports, entry) {
2015 if (pp->port == port)
2016 return pp;
2017 }
2018 return NULL;
2019}
2020
2021static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2022 const struct gsn_addr *addr)
2023{
2024 struct gtphub_peer *peer;
2025 llist_for_each_entry(peer, &bind->peers, entry) {
2026 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2027 if (a)
2028 return a;
2029 }
2030 return NULL;
2031}
2032
2033static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2034 const struct gsn_addr *addr,
2035 uint16_t port)
2036{
2037 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2038 if (!a)
2039 return NULL;
2040 return gtphub_addr_find_port(a, port);
2041}
2042
2043struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2044 const struct osmo_sockaddr *addr)
2045{
2046 struct gsn_addr gsna;
2047 uint16_t port;
2048 gsn_addr_from_sockaddr(&gsna, &port, addr);
2049 return gtphub_port_find(bind, &gsna, port);
2050}
2051
2052static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2053 struct gtphub_bind *bind)
2054{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002055 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2056 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002057 OSMO_ASSERT(peer);
2058
2059 INIT_LLIST_HEAD(&peer->addresses);
2060
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002061 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002062 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002063
2064 /* TODO use something random to pick the initial sequence nr.
2065 0x6d31 produces the ASCII character sequence 'm1', currently used in
2066 gtphub_nc_test.sh. */
2067 peer->seq_pool.last_nr = 0x6d31 - 1;
2068
2069 llist_add(&peer->entry, &bind->peers);
2070 return peer;
2071}
2072
2073static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2074 const struct gsn_addr *addr)
2075{
2076 struct gtphub_peer_addr *a;
2077 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2078 OSMO_ASSERT(a);
2079 a->peer = peer;
2080 gsn_addr_copy(&a->addr, addr);
2081 INIT_LLIST_HEAD(&a->ports);
2082 llist_add(&a->entry, &peer->addresses);
2083
2084 return a;
2085}
2086
2087static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2088 struct gtphub_bind *bind,
2089 const struct gsn_addr *addr)
2090{
2091 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2092 if (a)
2093 return a;
2094
2095 /* If we haven't found an address, that means we need to create an
2096 * entirely new peer for the new address. More addresses may be added
2097 * to this peer later, but not via this function. */
2098 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002099
2100 a = gtphub_peer_add_addr(peer, addr);
2101
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002102 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002103 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002104 gsn_addr_to_str(&a->addr));
2105
2106 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002107}
2108
2109static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2110 uint16_t port)
2111{
2112 struct gtphub_peer_port *pp;
2113
2114 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2115 OSMO_ASSERT(pp);
2116 pp->peer_addr = a;
2117 pp->port = port;
2118
2119 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2120 talloc_free(pp);
2121 return NULL;
2122 }
2123
2124 llist_add(&pp->entry, &a->ports);
2125
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002126 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002127 gsn_addr_to_str(&a->addr),
2128 (int)port);
2129
2130 return pp;
2131}
2132
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002133struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2134 struct gtphub_bind *bind,
2135 const struct gsn_addr *addr,
2136 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002137{
2138 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2139
2140 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2141 if (pp)
2142 return pp;
2143
2144 return gtphub_addr_add_port(a, port);
2145}
2146
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002147/* Find a GGSN peer with a matching address. If the address is known but the
2148 * port not, create a new port for that peer address. */
2149struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2150 const struct osmo_sockaddr *addr)
2151{
2152 struct gtphub_peer_addr *pa;
2153 struct gtphub_peer_port *pp;
2154
2155 struct gsn_addr gsna;
2156 uint16_t port;
2157 gsn_addr_from_sockaddr(&gsna, &port, addr);
2158
2159 pa = gtphub_addr_find(bind, &gsna);
2160 if (!pa)
2161 return NULL;
2162
2163 pp = gtphub_addr_find_port(pa, port);
2164
2165 if (!pp)
2166 pp = gtphub_addr_add_port(pa, port);
2167
2168 return pp;
2169}
2170
2171
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002172/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2173 * resolution should be possible but failed, and 1 if resolution was
2174 * successful. *pp will be set to NULL if <1 is returned. */
2175static int gtphub_resolve_ggsn(struct gtphub *hub,
2176 struct gtp_packet_desc *p,
2177 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002178{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002179 *pp = NULL;
2180
2181 /* TODO determine from message type whether IEs should be present? */
2182
2183 int rc;
2184 const char *imsi_str;
2185 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2186 if (rc < 1)
2187 return rc;
2188 OSMO_ASSERT(imsi_str);
2189
2190 const char *apn_str;
2191 rc = get_ie_apn_str(p->ie, &apn_str);
2192 if (rc < 1)
2193 return rc;
2194 OSMO_ASSERT(apn_str);
2195
2196 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2197 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002198}
2199
2200
2201/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002202/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002203/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2204 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002205static int _osmo_getaddrinfo(struct addrinfo **result,
2206 uint16_t family, uint16_t type, uint8_t proto,
2207 const char *host, uint16_t port)
2208{
2209 struct addrinfo hints;
2210 char portbuf[16];
2211
2212 sprintf(portbuf, "%u", port);
2213 memset(&hints, '\0', sizeof(struct addrinfo));
2214 hints.ai_family = family;
2215 if (type == SOCK_RAW) {
2216 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2217 * SOCK_RAW and IPPROTO_GRE is used.
2218 */
2219 hints.ai_socktype = SOCK_DGRAM;
2220 hints.ai_protocol = IPPROTO_UDP;
2221 } else {
2222 hints.ai_socktype = type;
2223 hints.ai_protocol = proto;
2224 }
2225
2226 return getaddrinfo(host, portbuf, &hints, result);
2227}
2228
2229/* TODO move to osmocom/core/socket.c ? */
2230int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2231 uint16_t family, uint16_t type, uint8_t proto,
2232 const char *host, uint16_t port)
2233{
2234 struct addrinfo *res;
2235 int rc;
2236 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2237
2238 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002239 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002240 return -EINVAL;
2241 }
2242
2243 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2244 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2245 addr->l = res->ai_addrlen;
2246 freeaddrinfo(res);
2247
2248 return 0;
2249}
2250
2251int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2252 char *port_str, size_t port_str_len,
2253 const struct osmo_sockaddr *addr,
2254 int flags)
2255{
2256 int rc;
2257
2258 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2259 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2260 return -1;
2261 }
2262
2263 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002264 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2265 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002266 return -1;
2267 }
2268
2269 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2270 addr_str, addr_str_len,
2271 port_str, port_str_len,
2272 flags);
2273
2274 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002275 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2276 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2277 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002278
2279 return rc;
2280}
2281
2282const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2283 char *buf, size_t buf_len)
2284{
2285 const int portbuf_len = 6;
2286 OSMO_ASSERT(buf_len > portbuf_len);
2287 char *portbuf = buf + buf_len - portbuf_len;
2288 buf_len -= portbuf_len;
2289 if (osmo_sockaddr_to_strs(buf, buf_len,
2290 portbuf, portbuf_len,
2291 addr,
2292 NI_NUMERICHOST | NI_NUMERICSERV))
2293 return NULL;
2294
2295 char *pos = buf + strnlen(buf, buf_len-1);
2296 size_t len = buf_len - (pos - buf);
2297
2298 snprintf(pos, len, " port %s", portbuf);
2299 buf[buf_len-1] = '\0';
2300
2301 return buf;
2302}
2303
2304const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2305{
2306 static char buf[256];
2307 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2308 if (! result)
2309 return "(invalid)";
2310 return result;
2311}
2312
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002313int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2314 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002315{
2316 if (a == b)
2317 return 0;
2318 if (!a)
2319 return -1;
2320 if (!b)
2321 return 1;
2322 if (a->l != b->l) {
2323 /* Lengths are not the same, but determine the order. Will
2324 * anyone ever sort a list by osmo_sockaddr though...? */
2325 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2326 if (cmp == 0) {
2327 if (a->l < b->l)
2328 return -1;
2329 else
2330 return 1;
2331 }
2332 return cmp;
2333 }
2334 return memcmp(&a->a, &b->a, a->l);
2335}
2336
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002337void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2338 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002339{
2340 OSMO_ASSERT(src->l <= sizeof(dst->a));
2341 memcpy(&dst->a, &src->a, src->l);
2342 dst->l = src->l;
2343}