blob: 01e34563ffb3da99f215fe4f28df469178fab5b0 [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>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#include <gtp.h>
32#include <gtpie.h>
33
34#include <openbsc/gtphub.h>
35#include <openbsc/debug.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010036#include <openbsc/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020037
38#include <osmocom/core/utils.h>
39#include <osmocom/core/logging.h>
40#include <osmocom/core/socket.h>
41
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010042
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020043static const int GTPH_GC_TICK_SECONDS = 1;
44
45void *osmo_gtphub_ctx;
46
Neels Hofmeyr063a8022015-11-16 14:35:13 +010047/* Convenience makro, note: only within this C file. */
48#define LOG(level, fmt, args...) \
49 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020050
51#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', sizeof(*(struct_pointer)))
52
53/* TODO move this to osmocom/core/select.h ? */
54typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
55
56/* TODO move this to osmocom/core/linuxlist.h ? */
57#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
58#define llist_first(head, type, entry) llist_entry(__llist_first(head), type, entry)
59
60/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
61
62enum gtp_rc {
63 GTP_RC_UNKNOWN = 0,
64 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010065 GTP_RC_PDU_C = 2, /* a real packet with IEs */
66 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020067
68 GTP_RC_TOOSHORT = -1,
69 GTP_RC_UNSUPPORTED_VERSION = -2,
70 GTP_RC_INVALID_IE = -3,
71};
72
73struct gtp_packet_desc {
74 union gtp_packet *data;
75 int data_len;
76 int header_len;
77 int version;
78 uint8_t type;
79 uint16_t seq;
80 uint32_t header_tei;
81 int rc; /* enum gtp_rc */
82 unsigned int plane_idx;
83 union gtpie_member *ie[GTPIE_SIZE];
84};
85
86void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
87{
88 memcpy(gsna, src, sizeof(struct gsn_addr));
89}
90
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020091int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
92 const struct osmo_sockaddr *sa)
93{
94 char addr_str[256];
95 char port_str[6];
96
97 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
98 port_str, sizeof(port_str),
99 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
100 != 0) {
101 return -1;
102 }
103
104 if (port)
105 *port = atoi(port_str);
106
107 return gsn_addr_from_str(gsna, addr_str);
108}
109
110int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
111{
112 int af = AF_INET;
113 gsna->len = 4;
114 const char *pos = numeric_addr_str;
115 for (; *pos; pos++) {
116 if (*pos == ':') {
117 af = AF_INET6;
118 gsna->len = 16;
119 break;
120 }
121 }
122
123 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
124 if (rc != 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100125 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n", numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200126 return -1;
127 }
128 return 0;
129}
130
131const char *gsn_addr_to_str(const struct gsn_addr *gsna)
132{
133 static char buf[INET6_ADDRSTRLEN + 1];
134 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
135}
136
137const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
138 char *strbuf,
139 int strbuf_len)
140{
141 int af;
142 switch (gsna->len) {
143 case 4:
144 af = AF_INET;
145 break;
146 case 16:
147 af = AF_INET6;
148 break;
149 default:
150 return NULL;
151 }
152
153 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
154 if (!r) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100155 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string: %s: len=%d, buf=%s\n",
156 strerror(errno),
157 (int)gsna->len,
158 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200159 }
160 return r;
161}
162
163int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
164{
165 if (a == b)
166 return 1;
167 if ((!a) || (!b))
168 return 0;
169 if (a->len != b->len)
170 return 0;
171 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
172}
173
174static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p, int idx)
175{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100176 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200177 return -1;
178
179 unsigned int len;
180 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
181 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
182 &len, gsna->buf, sizeof(gsna->buf))
183 != 0)
184 return -1;
185 gsna->len = len;
186 return 0;
187}
188
189static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p, int idx)
190{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100191 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200192 return -1;
193
194 int ie_idx;
195 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
196
197 if (ie_idx < 0)
198 return -1;
199
200 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
201 int ie_l = ntoh16(ie->l);
202 if (ie_l != gsna->len) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100203 LOG(LOGL_ERROR, "Not implemented: replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200204 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
205 return -1;
206 }
207
208 memcpy(ie->v, gsna->buf, (int)ie_l);
209 return 0;
210}
211
212/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
213 */
214void validate_gtp0_header(struct gtp_packet_desc *p)
215{
216 const struct gtp0_header *pheader = &(p->data->gtp0.h);
217 p->rc = GTP_RC_UNKNOWN;
218 p->header_len = 0;
219
220 OSMO_ASSERT(p->data_len >= 1);
221 OSMO_ASSERT(p->version == 0);
222
223 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100224 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200225 p->rc = GTP_RC_TOOSHORT;
226 return;
227 }
228
229 p->type = ntoh8(pheader->type);
230 p->seq = ntoh16(pheader->seq);
231 p->header_tei = 0; /* TODO */
232
233 if (p->data_len == GTP0_HEADER_SIZE) {
234 p->rc = GTP_RC_TINY;
235 p->header_len = GTP0_HEADER_SIZE;
236 return;
237 }
238
239 /* Check packet length field versus length of packet */
240 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100241 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not match"
242 " actual length (%d)\n",
243 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
244 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200245 p->rc = GTP_RC_TOOSHORT;
246 return;
247 }
248
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100249 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200250 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100251 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200252}
253
254/* Validate GTP version 1 data, and update p->rc with the result, as well as
255 * p->header_len in case of a valid header. */
256void validate_gtp1_header(struct gtp_packet_desc *p)
257{
258 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
259 p->rc = GTP_RC_UNKNOWN;
260 p->header_len = 0;
261
262 OSMO_ASSERT(p->data_len >= 1);
263 OSMO_ASSERT(p->version == 1);
264
265 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
266 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100267 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200268 p->rc = GTP_RC_TOOSHORT;
269 return;
270 }
271
272 p->type = ntoh8(pheader->type);
273 p->header_tei = ntoh32(pheader->tei);
274 p->seq = ntoh16(pheader->seq);
275
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100276 LOG(LOGL_DEBUG, "GTPv1\n"
277 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
278 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
279 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
280 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
281 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
282 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
283 p->type, p->type,
284 ntoh16(pheader->length), ntoh16(pheader->length),
285 p->header_tei, p->header_tei,
286 p->seq, p->seq,
287 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200288 pheader->next, pheader->next);
289
290 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
291 p->rc = GTP_RC_TINY;
292 p->header_len = GTP1_HEADER_SIZE_SHORT;
293 return;
294 }
295
296 /* Check packet length field versus length of packet */
297 if (p->data_len != (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100298 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not match"
299 " actual length (%d)\n",
300 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
301 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200302 p->rc = GTP_RC_TOOSHORT;
303 return;
304 }
305
Neels Hofmeyre921e322015-11-11 00:45:50 +0100306 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200307 p->header_len = GTP1_HEADER_SIZE_LONG;
308}
309
310/* Examine whether p->data of size p->data_len has a valid GTP header. Set
311 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
312 * gtp_rc). p->data must point at a buffer with p->data_len set. */
313void validate_gtp_header(struct gtp_packet_desc *p)
314{
315 p->rc = GTP_RC_UNKNOWN;
316
317 /* Need at least 1 byte in order to check version */
318 if (p->data_len < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100319 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200320 p->rc = GTP_RC_TOOSHORT;
321 return;
322 }
323
324 p->version = p->data->flags >> 5;
325
326 switch (p->version) {
327 case 0:
328 validate_gtp0_header(p);
329 break;
330 case 1:
331 validate_gtp1_header(p);
332 break;
333 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100334 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200335 p->rc = GTP_RC_UNSUPPORTED_VERSION;
336 break;
337 }
338}
339
340
341/* Return the value of the i'th IMSI IEI by copying to *imsi.
342 * The first IEI is reached by passing i = 0.
343 * imsi must point at allocated space of (at least) 8 bytes.
344 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100345static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200346{
347 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
348}
349
350/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100351static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200352{
353 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
354}
355
356static char imsi_digit_to_char(uint8_t nibble)
357{
358 nibble &= 0x0f;
359 if (nibble > 9)
360 return (nibble == 0x0f) ? '\0' : '?';
361 return '0' + nibble;
362}
363
364/* Return a human readable IMSI string, in a static buffer.
365 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100366static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200367{
368 static char str[17];
369 int i;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100370 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200371
372 for (i = 0; i < 8; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100373 c = imsi_digit_to_char(imsi[i]);
374 if (c == '?')
375 return -1;
376 str[2*i] = c;
377
378 c = imsi_digit_to_char(imsi[i] >> 4);
379 if (c == '?')
380 return -1;
381 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200382 }
383 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100384 *imsi_str = str;
385 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200386}
387
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100388/* Return 0 if not present, 1 if present and decoded successfully, -1 if
389 * present but cannot be decoded. */
390static int get_ie_imsi_str(union gtpie_member *ie[], int i, const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100391{
392 uint8_t imsi_buf[8];
393 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100394 return 0;
395 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100396}
397
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100398/* Return 0 if not present, 1 if present and decoded successfully, -1 if
399 * present but cannot be decoded. */
400static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100401{
402 static char apn_buf[GSM_APN_LENGTH];
403 unsigned int len;
404 if (gtpie_gettlv(ie, GTPIE_APN, 0,
405 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100406 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100407
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100408 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100409 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
410 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100411 return -1;
412 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100413
414 if (len > (sizeof(apn_buf) - 1))
415 len = sizeof(apn_buf) - 1;
416 apn_buf[len] = '\0';
417
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100418 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
419 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100420 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
421 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100422 return -1;
423 }
424 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100425}
426
427
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200428/* Validate header, and index information elements. Write decoded packet
429 * information to *res. res->data will point at the given data buffer. On
430 * error, p->rc is set <= 0 (see enum gtp_rc). */
431static void gtp_decode(const uint8_t *data, int data_len,
432 unsigned int from_plane_idx,
433 struct gtp_packet_desc *res)
434{
435 ZERO_STRUCT(res);
436 res->data = (union gtp_packet*)data;
437 res->data_len = data_len;
438 res->plane_idx = from_plane_idx;
439
440 validate_gtp_header(res);
441
442 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100443 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200444 return;
445 }
446
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100447 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200448
Neels Hofmeyre921e322015-11-11 00:45:50 +0100449 if (from_plane_idx == GTPH_PLANE_USER) {
450 res->rc = GTP_RC_PDU_U;
451 return;
452 }
453
454 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100455 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200456 return;
457 }
458
459 if (gtpie_decaps(res->ie, res->version,
460 (void*)(data + res->header_len),
461 res->data_len - res->header_len) != 0) {
462 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100463 LOG(LOGL_ERROR, "INVALID: cannot decode IEs. Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200464 return;
465 }
466
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100467#if 1
468 /* TODO if (<loglevel is debug>) { ... (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200469 int i;
470
471 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100472 const char *imsi;
473 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200474 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100475 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200476 }
477
478 for (i = 0; i < 10; i++) {
479 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100480 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200481 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100482 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200483 }
484
485 for (i = 0; i < 2; i++) {
486 struct gsn_addr addr;
487 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100488 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200489 }
490
491 for (i = 0; i < 10; i++) {
492 uint32_t tei;
493 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
494 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100495 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496 tei, tei);
497 }
498
499 for (i = 0; i < 10; i++) {
500 uint32_t tei;
501 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
502 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100503 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200504 tei, tei);
505 }
506#endif
507}
508
509
510/* expiry */
511
512void expiry_init(struct expiry *exq, int expiry_in_seconds)
513{
514 ZERO_STRUCT(exq);
515 exq->expiry_in_seconds = expiry_in_seconds;
516 INIT_LLIST_HEAD(&exq->items);
517}
518
519void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
520{
521 item->expiry = now + exq->expiry_in_seconds;
522
523 /* Add/move to the tail to always sort by expiry, ascending. */
524 llist_del(&item->entry);
525 llist_add_tail(&item->entry, &exq->items);
526}
527
528int expiry_tick(struct expiry *exq, time_t now)
529{
530 int expired = 0;
531 struct expiring_item *m, *n;
532 llist_for_each_entry_safe(m, n, &exq->items, entry) {
533 if (m->expiry <= now) {
534 expiring_item_del(m);
535 expired ++;
536 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200537 /* The items are added sorted by expiry. So when we hit
538 * an unexpired entry, only more unexpired ones will
539 * follow. */
540 break;
541 }
542 }
543 return expired;
544}
545
546void expiring_item_init(struct expiring_item *item)
547{
548 ZERO_STRUCT(item);
549 INIT_LLIST_HEAD(&item->entry);
550}
551
552void expiring_item_del(struct expiring_item *item)
553{
554 OSMO_ASSERT(item);
555 llist_del(&item->entry);
556 INIT_LLIST_HEAD(&item->entry);
557 if (item->del_cb) {
558 /* avoid loops */
559 del_cb_t del_cb = item->del_cb;
560 item->del_cb = 0;
561 (del_cb)(item);
562 }
563}
564
565
566/* nr_map, nr_pool */
567
568void nr_pool_init(struct nr_pool *pool)
569{
570 *pool = (struct nr_pool){};
571}
572
573nr_t nr_pool_next(struct nr_pool *pool)
574{
575 pool->last_nr ++;
576
577 OSMO_ASSERT(pool->last_nr > 0);
578 /* TODO: gracefully handle running out of TEIs. */
579 /* TODO: random TEIs. */
580
581 return pool->last_nr;
582}
583
584void nr_map_init(struct nr_map *map, struct nr_pool *pool,
585 struct expiry *exq)
586{
587 ZERO_STRUCT(map);
588 map->pool = pool;
589 map->add_items_to_expiry = exq;
590 INIT_LLIST_HEAD(&map->mappings);
591}
592
593void nr_mapping_init(struct nr_mapping *m)
594{
595 ZERO_STRUCT(m);
596 INIT_LLIST_HEAD(&m->entry);
597 expiring_item_init(&m->expiry_entry);
598}
599
600void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
601{
602 /* Generate a mapped number */
603 mapping->repl = nr_pool_next(map->pool);
604
605 /* Add to the tail to always yield a list sorted by expiry, in
606 * ascending order. */
607 llist_add_tail(&mapping->entry, &map->mappings);
608 if (map->add_items_to_expiry)
609 expiry_add(map->add_items_to_expiry,
610 &mapping->expiry_entry,
611 now);
612}
613
614void nr_map_clear(struct nr_map *map)
615{
616 struct nr_mapping *m;
617 struct nr_mapping *n;
618 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
619 nr_mapping_del(m);
620 }
621}
622
623int nr_map_empty(const struct nr_map *map)
624{
625 return llist_empty(&map->mappings);
626}
627
628struct nr_mapping *nr_map_get(const struct nr_map *map,
629 void *origin, nr_t nr_orig)
630{
631 struct nr_mapping *mapping;
632 llist_for_each_entry(mapping, &map->mappings, entry) {
633 if ((mapping->origin == origin)
634 && (mapping->orig == nr_orig))
635 return mapping;
636 }
637 /* Not found. */
638 return NULL;
639}
640
641struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
642{
643 struct nr_mapping *mapping;
644 llist_for_each_entry(mapping, &map->mappings, entry) {
645 if (mapping->repl == nr_repl) {
646 return mapping;
647 }
648 }
649 /* Not found. */
650 return NULL;
651}
652
653void nr_mapping_del(struct nr_mapping *mapping)
654{
655 OSMO_ASSERT(mapping);
656 llist_del(&mapping->entry);
657 INIT_LLIST_HEAD(&mapping->entry);
658 expiring_item_del(&mapping->expiry_entry);
659}
660
661
662/* gtphub */
663
664const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
665 "CTRL",
666 "USER",
667};
668
669const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
670 2123,
671 2152,
672};
673
674time_t gtphub_now(void)
675{
676 struct timespec now_tp;
677 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
678 return now_tp.tv_sec;
679}
680
681/* Remove a gtphub_peer from its list and free it. */
682static void gtphub_peer_del(struct gtphub_peer *peer)
683{
684 nr_map_clear(&peer->seq_map);
685 llist_del(&peer->entry);
686 talloc_free(peer);
687}
688
689static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
690{
691 OSMO_ASSERT(llist_empty(&pa->ports));
692 llist_del(&pa->entry);
693 talloc_free(pa);
694}
695
696static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
697{
698 OSMO_ASSERT(pp->ref_count == 0);
699 llist_del(&pp->entry);
700 talloc_free(pp);
701}
702
703/* From the information in the gtp_packet_desc, return the address of a GGSN.
704 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100705static int gtphub_resolve_ggsn(struct gtphub *hub,
706 struct gtp_packet_desc *p,
707 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200708
709/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100710struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
711 const char *imsi_str,
712 const char *apn_ni_str);
713int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200714
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200715static void gtphub_zero(struct gtphub *hub)
716{
717 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100718 INIT_LLIST_HEAD(&hub->ggsn_lookups);
719 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200720}
721
722static int gtphub_sock_init(struct osmo_fd *ofd,
723 const struct gtphub_cfg_addr *addr,
724 osmo_fd_cb_t cb,
725 void *data,
726 int ofd_id)
727{
728 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100729 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200730 return -1;
731 }
732 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100733 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200734 return -1;
735 }
736
737 ofd->when = BSC_FD_READ;
738 ofd->cb = cb;
739 ofd->data = data;
740 ofd->priv_nr = ofd_id;
741
742 int rc;
743 rc = osmo_sock_init_ofd(ofd,
744 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
745 addr->addr_str, addr->port,
746 OSMO_SOCK_F_BIND);
747 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100748 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
749 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200750 return -1;
751 }
752
753 return 0;
754}
755
756static void gtphub_bind_init(struct gtphub_bind *b)
757{
758 ZERO_STRUCT(b);
759
760 INIT_LLIST_HEAD(&b->peers);
761}
762
763static int gtphub_bind_start(struct gtphub_bind *b,
764 const struct gtphub_cfg_bind *cfg,
765 osmo_fd_cb_t cb, void *cb_data,
766 unsigned int ofd_id)
767{
768 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
769 return -1;
770 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
771 return -1;
772 return 0;
773}
774
775/* Recv datagram from from->fd, optionally write sender's address to *from_addr.
776 * Return the number of bytes read, zero on error. */
777static int gtphub_read(const struct osmo_fd *from,
778 struct osmo_sockaddr *from_addr,
779 uint8_t *buf, size_t buf_len)
780{
781 /* recvfrom requires the available length to be set in *from_addr_len. */
782 if (from_addr)
783 from_addr->l = sizeof(from_addr->a);
784
785 errno = 0;
786 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
787 (struct sockaddr*)&from_addr->a, &from_addr->l);
788 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
789 * is not truncated. Then maybe reduce buf's size. */
790
791 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100792 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
793 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200794 return 0;
795 }
796
797 if (from_addr) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100798 LOG(LOGL_DEBUG, "from %s\n", osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200799 }
800
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100801 LOG(LOGL_DEBUG, "Received %d\n", (int)received); //%s\n", (int)received, osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200802 return received;
803}
804
805inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
806{
807 OSMO_ASSERT(pp->ref_count < UINT_MAX);
808 pp->ref_count++;
809}
810
811inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
812{
813 OSMO_ASSERT(pp->ref_count > 0);
814 pp->ref_count--;
815}
816
817inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
818{
819 OSMO_ASSERT(p->version == 1);
820 p->data->gtp1l.h.seq = hton16(seq);
821 p->seq = seq;
822}
823
824inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
825{
826 OSMO_ASSERT(p->version == 1);
827 p->data->gtp1l.h.tei = hton32(tei);
828 p->header_tei = tei;
829}
830
831static void gtphub_mapping_del_cb(struct expiring_item *expi);
832
833static struct nr_mapping *gtphub_mapping_new()
834{
835 struct nr_mapping *nrm;
836 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
837 OSMO_ASSERT(nrm);
838
839 nr_mapping_init(nrm);
840 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
841 return nrm;
842}
843
844static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf, int buflen)
845{
846 if (llist_empty(&peer->addresses))
847 return "(addressless)";
848
849 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
850 struct gtphub_peer_addr,
851 entry);
852 return gsn_addr_to_strb(&a->addr, buf, buflen);
853}
854
855static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf, int buflen)
856{
857 if (!port)
858 return "(null port)";
859
860 snprintf(buf, buflen, "%s port %d",
861 gsn_addr_to_str(&port->peer_addr->addr),
862 (int)port->port);
863 return buf;
864}
865
866const char *gtphub_peer_str(struct gtphub_peer *peer)
867{
868 static char buf[256];
869 return gtphub_peer_strb(peer, buf, sizeof(buf));
870}
871
872const char *gtphub_peer_str2(struct gtphub_peer *peer)
873{
874 static char buf[256];
875 return gtphub_peer_strb(peer, buf, sizeof(buf));
876}
877
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100878const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200879{
880 static char buf[256];
881 return gtphub_port_strb(port, buf, sizeof(buf));
882}
883
884static const char *gtphub_port_str2(struct gtphub_peer_port *port)
885{
886 static char buf[256];
887 return gtphub_port_strb(port, buf, sizeof(buf));
888}
889
890static void gtphub_mapping_del_cb(struct expiring_item *expi)
891{
892 expi->del_cb = 0; /* avoid recursion loops */
893
894 struct nr_mapping *nrm = container_of(expi,
895 struct nr_mapping,
896 expiry_entry);
897 llist_del(&nrm->entry);
898 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
899
900 /* Just for log */
901 struct gtphub_peer_port *from = nrm->origin;
902 OSMO_ASSERT(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100903 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %d->%d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200904 (int)nrm->expiry_entry.expiry,
905 gtphub_port_str(from),
906 (int)nrm->orig, (int)nrm->repl);
907
908 gtphub_port_ref_count_dec(from);
909
910 talloc_free(nrm);
911}
912
913static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
914 struct gtphub_peer_port *from,
915 nr_t orig_nr,
916 time_t now)
917{
918 struct nr_mapping *nrm;
919
920 nrm = nr_map_get(map, from, orig_nr);
921
922 if (!nrm) {
923 nrm = gtphub_mapping_new();
924 nrm->orig = orig_nr;
925 nrm->origin = from;
926 nr_map_add(map, nrm, now);
927 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100928 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200929 gtphub_port_str(from),
930 (int)(nrm->orig), (int)(nrm->repl));
931 } else {
932 /* restart expiry timeout */
933 expiry_add(map->add_items_to_expiry, &nrm->expiry_entry,
934 now);
935 }
936
937 OSMO_ASSERT(nrm);
938 return nrm;
939}
940
941static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
942 int plane_idx,
943 struct gtphub_peer_port *from,
944 uint32_t orig_tei,
945 time_t now)
946{
947 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
948 from, orig_tei, now);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100949 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %d) <-- TEI %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200950 gtphub_plane_idx_names[plane_idx],
951 gtphub_port_str(from),
952 (int)orig_tei, (int)nrm->repl);
953
954 return (uint32_t)nrm->repl;
955}
956
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100957static void gtphub_map_seq(struct gtp_packet_desc *p,
958 struct gtphub_peer_port *from_port,
959 struct gtphub_peer_port *to_port,
960 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200961{
962 /* Store a mapping in to_peer's map, so when we later receive a GTP
963 * packet back from to_peer, the seq nr can be unmapped back to its
964 * origin (from_peer here). */
965 struct nr_mapping *nrm;
966 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
967 from_port, p->seq, now);
968
969 /* Change the GTP packet to yield the new, mapped seq nr */
970 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200971}
972
973static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
974 struct gtphub_peer_port *responding_port)
975{
976 OSMO_ASSERT(p->version == 1);
977 struct nr_mapping *nrm = nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
978 p->seq);
979 if (!nrm)
980 return NULL;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100981 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n", nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200982 set_seq(p, nrm->orig);
983 return nrm->origin;
984}
985
986static void gtphub_check_restart_counter(struct gtphub *hub,
987 struct gtp_packet_desc *p,
988 struct gtphub_peer_port *from)
989{
990 /* TODO */
991 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
992 * that doesn't match the peer's previously sent restart counter, clear
993 * that peer and cancel PDP contexts. */
994}
995
996static void gtphub_map_restart_counter(struct gtphub *hub,
997 struct gtp_packet_desc *p,
998 struct gtphub_peer_port *from,
999 struct gtphub_peer_port *to)
1000{
1001 /* TODO */
1002}
1003
1004/* gtphub_map_ie_teis() and gtphub_unmap_header_tei():
1005 *
1006 * TEI mapping must happen symmetrically. An SGSN contacts gtphub instead of N
1007 * GGSNs, and a GGSN replies to gtphub for N SGSNs. From either end, TEIs may
1008 * collide: two GGSNs picking the same TEIs, or two SGSNs picking the same
1009 * TEIs. Since the opposite side sees the sender address being gtphub's
1010 * address, TEIs among the SGSNs, and among the GGSNs, must not overlap. If a
1011 * peer sends a TEI already sent before from a peer of the same side, gtphub
1012 * replaces it with a TEI not yet seen from that side and remembers the
1013 * mapping.
1014 *
1015 * Consider two SGSNs A and B contacting two GGSNs C and D thru gtphub.
1016 *
1017 * A: Create PDP Ctx, I have TEI 1.
1018 * ---> gtphub: A has TEI 1, sending 1 for C.
1019 * ---> C: gtphub has TEI 1.
1020 * <--- C: Response to TEI 1: I have TEI 11.
1021 * <--- gtphub: ok, telling A: 11.
1022 * A: gtphub's first TEI is 11. (1)
1023 *
1024 * B: Create PDP Ctx, I have TEIs 1.
1025 * ---> gtphub: 1 already taken for C, sending 2 for B. (map)
1026 * ---> C: gtphub also has 2.
1027 * <--- C: Response to TEI 2: I have TEI 12.
1028 * <--- gtphub: ok, TEI 2 is actually B with TEI 1. (unmap)
1029 * B: gtphub's first TEI is 12, as far as I can tell.
1030 *
1031 * Now the second GGSN comes into play:
1032 *
1033 * A: Create PDP Ctx, I have TEI 2.
1034 * ---> gtphub: A also has TEI 2, but for D, sending 1. (2)
1035 * ---> D: gtphub has 1.
1036 * <--- D: Response to TEI 1: I have TEI 11.
1037 * <--- gtphub: from D, 1 is A. 11 already taken by C, sending 13. (3)
1038 * A: gtphub also has TEI 13. (4)
1039 *
1040 * And some messages routed through:
1041 *
1042 * A: message to TEI 11, see (1).
1043 * ---> gtphub: ok, telling C with TEI 11.
1044 * ---> C: I see, 11 means reply with 1.
1045 * <--- C: Response to TEI 1
1046 * <--- gtphub: 1 from C is actually for A with TEI 1.
1047 * A: ah, my TEI 1, thanks!
1048 *
1049 * A: message to TEI 13, see (4).
1050 * ---> gtphub: ok, but not 13, D wanted TEI 11 instead, see (3).
1051 * ---> D: I see, 11 means reply with 1.
1052 * <--- D: Response to TEI 1
1053 * <--- gtphub: 1 from D is actually for A with TEI 2, see (2).
1054 * A: ah, my TEI 2, thanks!
1055 *
1056 * What if a GGSN initiates a request:
1057 *
1058 * <--- D: Request to gtphub TEI 1
1059 * <--- gtphub: 1 from D is for A with 2, see (2).
1060 * A: my TEI 2 means reply with 13.
1061 * ---> gtphub: 13 was D with 11, see (3).
1062 * ---> D: 11 from gtphub: a reply to my request for TEI 1.
1063 *
1064 * Note that usually, it's the sequence numbers that route a response back to
1065 * the requesting peer. Nevertheless, the TEI mappings must be carried out to
1066 * replace the TEIs in the GTP packet that is relayed.
1067 *
1068 * Also note: the TEI in the GTP header is "reversed" from the TEI in the IEs:
1069 * the TEI in the header is used to send something *to* a peer, while the TEI
1070 * in e.g. a Create PDP Context Request's IE is for routing messages *back*
1071 * later. */
1072
1073static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1074 struct gtphub *hub,
1075 struct gtp_packet_desc *p,
1076 struct gtphub_peer_port *from_port)
1077{
1078 OSMO_ASSERT(p->version == 1);
1079 *to_port_p = NULL;
1080
1081 /* If the header's TEI is zero, no PDP context has been established
1082 * yet. If nonzero, a mapping should actually already exist for this
1083 * TEI, since it must have been announced in a PDP context creation. */
1084 uint32_t tei = p->header_tei;
1085 if (!tei)
1086 return 0;
1087
1088 /* to_peer has previously announced a TEI, which was stored and
1089 * mapped in from_peer's tei_map. */
1090 struct nr_mapping *nrm;
1091 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1092 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001093 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1094 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001095 return -1;
1096 }
1097
1098 struct gtphub_peer_port *to_port = nrm->origin;
1099 uint32_t unmapped_tei = nrm->orig;
1100 set_tei(p, unmapped_tei);
1101
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001102 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %d -> %d (to %s)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001103 gtphub_port_str(from_port), tei, unmapped_tei,
1104 gtphub_port_str2(to_port));
1105
1106 *to_port_p = to_port;
1107 return 0;
1108}
1109
1110/* Read GSN address IEs from p, and make sure these peer addresses exist in
1111 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1112 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1113 * the packet p. */
1114static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
1115 struct gtphub_bind from_bind[],
1116 struct gtphub_bind to_bind[],
1117 struct gtp_packet_desc *p,
1118 time_t now)
1119{
1120 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1121
1122 int rc;
1123 int plane_idx;
1124
1125 switch (p->type) {
1126 case GTP_CREATE_PDP_REQ:
1127 case GTP_CREATE_PDP_RSP:
1128 /* Go for it below */
1129 break;
1130 default:
1131 /* Nothing to do for this message type. */
1132 return 0;
1133 }
1134
1135 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1136
1137 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1138 plane_nrs_match_GSN_addr_IE_indices);
1139
1140 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1141 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1142
1143 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1144 struct gsn_addr addr_from_ie;
1145 uint32_t tei_from_ie;
1146 int ie_idx;
1147
1148 /* Fetch GSN Address and TEI from IEs */
1149 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1150 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001151 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1152 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001153 return -1;
1154 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001155 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001156 gtphub_plane_idx_names[plane_idx],
1157 gsn_addr_to_str(&addr_from_ie),
1158 addr_from_ie.len);
1159
1160 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1161 if (ie_idx < 0) {
1162 if (ie_mandatory) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001163 LOG(LOGL_ERROR, "Create PDP Context message invalid:"
1164 " missing IE %d\n", (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001165 return -1;
1166 }
1167 tei_from_ie = 0;
1168 }
1169 else
1170 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1171
1172 /* Make sure an entry for this peer address with default port
1173 * exists */
1174 struct gtphub_peer_port *peer_from_ie =
1175 gtphub_port_have(hub, &from_bind[plane_idx],
1176 &addr_from_ie,
1177 gtphub_plane_idx_default_port[plane_idx]);
1178
1179 if (tei_from_ie) {
1180 /* Create TEI mapping and replace in GTP packet IE */
1181 uint32_t mapped_tei =
1182 gtphub_tei_mapping_have(hub, plane_idx,
1183 peer_from_ie,
1184 tei_from_ie,
1185 now);
1186 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1187 }
1188
1189 /* Replace the GSN address to reflect gtphub. */
1190 rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx);
1191 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001192 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1193 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001194 return -1;
1195 }
1196 }
1197
1198 return 0;
1199}
1200
1201static int gtphub_write(const struct osmo_fd *to,
1202 const struct osmo_sockaddr *to_addr,
1203 const uint8_t *buf, size_t buf_len)
1204{
1205 errno = 0;
1206 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1207 (struct sockaddr*)&to_addr->a, to_addr->l);
1208
1209 if (to_addr) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001210 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001211 }
1212
1213 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001214 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001215 return -EINVAL;
1216 }
1217
1218 if (sent != buf_len)
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001219 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n", (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001220 else
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001221 LOG(LOGL_DEBUG, "Sent %d\n%s\n", (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001222
1223 return 0;
1224}
1225
1226static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1227{
1228 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1229 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001230 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n", gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001231 if (!(what & BSC_FD_READ))
1232 return 0;
1233
1234 struct gtphub *hub = from_ggsns_ofd->data;
1235
1236 static uint8_t buf[4096];
1237 struct osmo_sockaddr from_addr;
1238 struct osmo_sockaddr to_addr;
1239 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001240 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001241 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001242
1243 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1244 if (len < 1)
1245 return 0;
1246
1247 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1248 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001249 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001250 if (len < 1)
1251 return 0;
1252
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001253 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001254}
1255
1256static int gtphub_unmap(struct gtphub *hub,
1257 struct gtp_packet_desc *p,
1258 struct gtphub_peer_port *from,
1259 struct gtphub_peer_port *to_proxy,
1260 struct gtphub_peer_port **final_unmapped,
1261 struct gtphub_peer_port **unmapped_from_seq,
1262 struct gtphub_peer_port **unmapped_from_tei)
1263{
1264 /* Always (try to) unmap sequence and TEI numbers, which need to be
1265 * replaced in the packet. Either way, give precedence to the proxy, if
1266 * configured. */
1267
1268 struct gtphub_peer_port *from_seq = NULL;
1269 struct gtphub_peer_port *from_tei = NULL;
1270 struct gtphub_peer_port *unmapped = NULL;
1271
1272 if (unmapped_from_seq)
1273 *unmapped_from_seq = from_seq;
1274 if (unmapped_from_tei)
1275 *unmapped_from_tei = from_tei;
1276 if (final_unmapped)
1277 *final_unmapped = unmapped;
1278
1279 from_seq = gtphub_unmap_seq(p, from);
1280
1281 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1282 return -1;
1283
1284 struct gtphub_peer *from_peer = from->peer_addr->peer;
1285 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001286 LOG(LOGL_DEBUG, "Seq unmap and TEI unmap yield two different peers. Using seq unmap."
1287 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1288 gtphub_plane_idx_names[p->plane_idx],
1289 gtphub_peer_str(from_peer),
1290 (int)p->seq,
1291 gtphub_port_str(from_seq),
1292 (int)p->header_tei,
1293 gtphub_port_str2(from_tei)
1294 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001295 }
1296 unmapped = (from_seq? from_seq : from_tei);
1297
1298 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001299 LOG(LOGL_NOTICE, "Unmap yields a different peer than the configured proxy. Using proxy."
1300 " unmapped: %s proxy: %s\n",
1301 gtphub_port_str(unmapped),
1302 gtphub_port_str2(to_proxy)
1303 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001304 }
1305 unmapped = (to_proxy? to_proxy : unmapped);
1306
1307 if (!unmapped) {
1308 /* Return no error, but returned pointers are all NULL. */
1309 return 0;
1310 }
1311
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001312 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001313 from_seq, from_tei, unmapped);
1314
1315 if (unmapped_from_seq)
1316 *unmapped_from_seq = from_seq;
1317 if (unmapped_from_tei)
1318 *unmapped_from_tei = from_tei;
1319 if (final_unmapped)
1320 *final_unmapped = unmapped;
1321 return 0;
1322}
1323
1324static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1325 uint16_t port,
1326 struct osmo_sockaddr *dst)
1327{
1328 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1329}
1330
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001331/* If p is an Echo request, replace p's data with the matching response and
1332 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1333 * detected. */
1334static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p, uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001335{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001336 if (p->type != GTP_ECHO_REQ)
1337 return 0;
1338
1339 static uint8_t echo_response_data[14] = {
1340 0x32, /* flags */
1341 GTP_ECHO_RSP,
1342 0x00, 14 - 8, /* Length in network byte order */
1343 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1344 0, 0, /* Seq, to be replaced */
1345 0, 0, /* no extensions */
1346 0x0e, /* Recovery IE */
1347 0 /* Recovery counter, to be replaced */
1348 };
1349 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1350 uint8_t *recovery = &echo_response_data[13];
1351
1352 *seq = hton16(p->seq);
1353 *recovery = hub->restart_counter;
1354
1355 *reply_buf = echo_response_data;
1356
1357 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001358}
1359
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001360struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1361 const struct osmo_sockaddr *addr);
1362
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001363/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1364 * address to forward to. Return a pointer to the osmo_fd, but copy the
1365 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1366 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1367 * bytes to forward, 0 or less on failure. */
1368int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1369 unsigned int plane_idx,
1370 const struct osmo_sockaddr *from_addr,
1371 uint8_t *buf,
1372 size_t received,
1373 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001374 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001375 struct osmo_fd **to_ofd,
1376 struct osmo_sockaddr *to_addr)
1377{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001378 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001379 gtphub_plane_idx_names[plane_idx],
1380 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001381
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001382 static struct gtp_packet_desc p;
1383 gtp_decode(buf, received, plane_idx, &p);
1384
1385 if (p.rc <= 0)
1386 return -1;
1387
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001388 int reply_len;
1389 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1390 if (reply_len > 0) {
1391 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001392 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001393 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1394 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001395 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001396 if (reply_len < 0)
1397 return -1;
1398
1399 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001400
1401 /* If a GGSN proxy is configured, check that it's indeed that proxy
1402 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1403 * gtphub, so no-one else is allowed to talk to us from that side. */
1404 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1405 if (ggsn) {
1406 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001407 LOG(LOGL_ERROR, "Rejecting: GGSN proxy configured, but GTP packet"
1408 " received on GGSN bind is from another sender:"
1409 " proxy: %s sender: %s\n",
1410 gtphub_port_str(ggsn),
1411 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001412 return -1;
1413 }
1414 }
1415
1416 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001417 /* Find a GGSN peer with a matching address. The sender's port
1418 * may in fact differ. */
1419 ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx],
1420 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001421 }
1422
1423 /* If any PDP context has been created, we already have an entry for
1424 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1425 * us about. */
1426 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001427 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1428 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001429 return -1;
1430 }
1431
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001432 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001433
1434 struct gtphub_peer_port *sgsn_from_seq;
1435 struct gtphub_peer_port *sgsn;
1436 if (gtphub_unmap(hub, &p, ggsn,
1437 hub->sgsn_proxy[plane_idx],
1438 &sgsn, &sgsn_from_seq,
1439 NULL /* not interested, got it in &sgsn already */
1440 )
1441 != 0) {
1442 return -1;
1443 }
1444
1445 if (!sgsn) {
1446 /* A GGSN initiated request would go to a known TEI. So this is
1447 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001448 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001449 return -1;
1450 }
1451
1452 if (plane_idx == GTPH_PLANE_CTRL) {
1453 /* This may be a Create PDP Context response. If it is, there are other
1454 * addresses in the GTP message to set up apart from the sender. */
1455 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns,
1456 hub->to_sgsns, &p, now)
1457 != 0)
1458 return -1;
1459 }
1460
1461 gtphub_check_restart_counter(hub, &p, ggsn);
1462 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1463
1464 /* If the GGSN is replying to an SGSN request, the sequence nr has
1465 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1466 * create a new mapping. */
1467 if (!sgsn_from_seq)
1468 gtphub_map_seq(&p, ggsn, sgsn, now);
1469
1470 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001471
1472 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001473
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001474 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001475 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001476 return received;
1477}
1478
1479static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1480{
1481 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1482 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001483 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n", gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001484
1485 if (!(what & BSC_FD_READ))
1486 return 0;
1487
1488 struct gtphub *hub = from_sgsns_ofd->data;
1489
1490 static uint8_t buf[4096];
1491 struct osmo_sockaddr from_addr;
1492 struct osmo_sockaddr to_addr;
1493 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001494 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001495 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001496
1497 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1498 if (len < 1)
1499 return 0;
1500
1501 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1502 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001503 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001504 if (len < 1)
1505 return 0;
1506
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001507 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001508}
1509
1510/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1511int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1512 unsigned int plane_idx,
1513 const struct osmo_sockaddr *from_addr,
1514 uint8_t *buf,
1515 size_t received,
1516 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001517 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001518 struct osmo_fd **to_ofd,
1519 struct osmo_sockaddr *to_addr)
1520{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001521 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001522 gtphub_plane_idx_names[plane_idx],
1523 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001524
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001525 static struct gtp_packet_desc p;
1526 gtp_decode(buf, received, plane_idx, &p);
1527
1528 if (p.rc <= 0)
1529 return -1;
1530
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001531 int reply_len;
1532 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1533 if (reply_len > 0) {
1534 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001535 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001536 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1537 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001538 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001539 if (reply_len < 0)
1540 return -1;
1541
1542 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543
1544 /* If an SGSN proxy is configured, check that it's indeed that proxy
1545 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1546 * gtphub, so no-one else is allowed to talk to us from that side. */
1547 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1548 if (sgsn) {
1549 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001550 LOG(LOGL_ERROR, "Rejecting: GGSN proxy configured, but GTP packet"
1551 " received on GGSN bind is from another sender:"
1552 " proxy: %s sender: %s\n",
1553 gtphub_port_str(sgsn),
1554 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001555 return -1;
1556 }
1557 }
1558
1559 if (!sgsn) {
1560 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001561 * entry for this SGSN. The port may differ. */
1562 sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx],
1563 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001564 }
1565
1566 if (!sgsn) {
1567 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1568 * first contact without being known yet, so create the peer
1569 * struct for the current sender. */
1570 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001571 LOG(LOGL_ERROR, "User plane peer was not announced by PDP Context, discarding: %s\n",
1572 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001573 return -1;
1574 }
1575
1576 struct gsn_addr from_gsna;
1577 uint16_t from_port;
1578 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1579 return -1;
1580
1581 sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx],
1582 &from_gsna, from_port);
1583 }
1584
1585 if (!sgsn) {
1586 /* This could theoretically happen for invalid address data or somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001587 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1588 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001589 return -1;
1590 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001591 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001592
1593 struct gtphub_peer_port *ggsn_from_seq;
1594 struct gtphub_peer_port *ggsn;
1595 if (gtphub_unmap(hub, &p, sgsn,
1596 hub->ggsn_proxy[plane_idx],
1597 &ggsn, &ggsn_from_seq,
1598 NULL /* not interested, got it in &ggsn already */
1599 )
1600 != 0) {
1601 return -1;
1602 }
1603
Neels Hofmeyra208c732015-11-11 19:26:09 +01001604 if (!ggsn) {
1605 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1606 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001607 }
1608
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001609 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001610 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001611 return -1;
1612 }
1613
1614 if (plane_idx == GTPH_PLANE_CTRL) {
1615 /* This may be a Create PDP Context requst. If it is, there are other
1616 * addresses in the GTP message to set up apart from the sender. */
1617 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns,
1618 hub->to_ggsns, &p, now)
1619 != 0)
1620 return -1;
1621 }
1622
1623 gtphub_check_restart_counter(hub, &p, sgsn);
1624 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1625
1626 /* If the SGSN is replying to a GGSN request, the sequence nr has
1627 * already been unmapped above (unmap_ggsn != NULL), and we need not
1628 * create a new outgoing sequence map. */
1629 if (!ggsn_from_seq)
1630 gtphub_map_seq(&p, sgsn, ggsn, now);
1631
1632 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1633
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001634 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001635
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001636 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001637 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001638 return received;
1639}
1640
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001641static void resolved_gssn_del_cb(struct expiring_item *expi)
1642{
1643 struct gtphub_resolved_ggsn *ggsn;
1644 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1645
1646 gtphub_port_ref_count_dec(ggsn->peer);
1647 llist_del(&ggsn->entry);
1648
1649 ggsn->expiry_entry.del_cb = 0;
1650 expiring_item_del(&ggsn->expiry_entry);
1651
1652 talloc_free(ggsn);
1653}
1654
1655void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1656 struct gsn_addr *resolved_addr,
1657 time_t now)
1658{
1659 struct gtphub_peer_port *pp;
1660 struct gtphub_resolved_ggsn *ggsn;
1661
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001662 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001663 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr, sizeof(*resolved_addr)));
1664
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001665 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1666 resolved_addr, 2123);
1667 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001668 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1669 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001670 return;
1671 }
1672
1673 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1674 OSMO_ASSERT(ggsn);
1675
1676 ggsn->peer = pp;
1677 gtphub_port_ref_count_inc(pp);
1678
1679 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
1680
1681 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1682 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1683
1684 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1685}
1686
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001687static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1688{
1689 return pp->ref_count == 0;
1690}
1691
1692static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1693{
1694 struct gtphub_peer_port *pp, *npp;
1695 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1696 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001697 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001698 gtphub_port_str(pp));
1699 gtphub_peer_port_del(pp);
1700 }
1701 }
1702 return llist_empty(&pa->ports);
1703}
1704
1705static int gtphub_gc_peer(struct gtphub_peer *p)
1706{
1707 struct gtphub_peer_addr *pa, *npa;
1708 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1709 if (gtphub_gc_peer_addr(pa)) {
1710 gtphub_peer_addr_del(pa);
1711 }
1712 }
1713
1714 /* Note that there's a ref_count in each gtphub_peer_port instance
1715 * listed within p->addresses, referenced by TEI mappings from
1716 * hub->tei_map. As long as those don't expire, this peer will stay. */
1717
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001718 return llist_empty(&p->addresses)
1719 && nr_map_empty(&p->seq_map);
1720}
1721
1722static void gtphub_gc_bind(struct gtphub_bind *b)
1723{
1724 struct gtphub_peer *p, *n;
1725 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1726 if (gtphub_gc_peer(p)) {
1727 gtphub_peer_del(p);
1728 }
1729 }
1730}
1731
1732void gtphub_gc(struct gtphub *hub, time_t now)
1733{
1734 int expired;
1735 expired = expiry_tick(&hub->expire_seq_maps, now);
1736 expired += expiry_tick(&hub->expire_tei_maps, now);
1737
1738 /* ... */
1739
1740 if (expired) {
1741 int i;
1742 for (i = 0; i < GTPH_PLANE_N; i++) {
1743 gtphub_gc_bind(&hub->to_sgsns[i]);
1744 gtphub_gc_bind(&hub->to_ggsns[i]);
1745 }
1746 }
1747}
1748
1749static void gtphub_gc_cb(void *data)
1750{
1751 struct gtphub *hub = data;
1752 gtphub_gc(hub, gtphub_now());
1753 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1754}
1755
1756static void gtphub_gc_start(struct gtphub *hub)
1757{
1758 hub->gc_timer.cb = gtphub_gc_cb;
1759 hub->gc_timer.data = hub;
1760
1761 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1762}
1763
1764/* called by unit tests */
1765void gtphub_init(struct gtphub *hub)
1766{
1767 gtphub_zero(hub);
1768
1769 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
1770 expiry_init(&hub->expire_tei_maps, GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
1771
1772 int plane_idx;
1773 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1774 nr_pool_init(&hub->tei_pool[plane_idx]);
1775 nr_map_init(&hub->tei_map[plane_idx],
1776 &hub->tei_pool[plane_idx],
1777 &hub->expire_tei_maps);
1778
1779 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1780 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1781 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001782
1783 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1784 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1785 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1786 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001787}
1788
1789static int gtphub_make_proxy(struct gtphub *hub,
1790 struct gtphub_peer_port **pp,
1791 struct gtphub_bind *bind,
1792 const struct gtphub_cfg_addr *addr)
1793{
1794 if (!addr->addr_str)
1795 return 0;
1796
1797 struct gsn_addr gsna;
1798 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1799 return -1;
1800
1801 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1802
1803 /* This is *the* proxy. Make sure it is never expired. */
1804 gtphub_port_ref_count_inc(*pp);
1805 return 0;
1806}
1807
1808int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1809{
1810 int rc;
1811
1812 gtphub_init(hub);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001813 gtphub_ares_init(hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001814
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001815 /* TODO set hub->restart_counter from external file. */
1816
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001817 int plane_idx;
1818 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1819 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1820 &cfg->to_ggsns[plane_idx],
1821 from_ggsns_read_cb, hub, plane_idx);
1822 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001823 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1824 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001825 return rc;
1826 }
1827
1828 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1829 &cfg->to_sgsns[plane_idx],
1830 from_sgsns_read_cb, hub, plane_idx);
1831 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001832 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1833 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001834 return rc;
1835 }
1836 }
1837
1838
1839 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1840 if (gtphub_make_proxy(hub,
1841 &hub->sgsn_proxy[plane_idx],
1842 &hub->to_sgsns[plane_idx],
1843 &cfg->sgsn_proxy[plane_idx])
1844 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001845 LOG(LOGL_FATAL, "Cannot configure SGSN proxy %s port %d.\n",
1846 cfg->sgsn_proxy[plane_idx].addr_str,
1847 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001848 return -1;
1849 }
1850 if (gtphub_make_proxy(hub,
1851 &hub->ggsn_proxy[plane_idx],
1852 &hub->to_ggsns[plane_idx],
1853 &cfg->ggsn_proxy[plane_idx])
1854 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001855 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001856 return -1;
1857 }
1858 }
1859
1860 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1861 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001862 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001863 gtphub_plane_idx_names[plane_idx],
1864 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1865 }
1866
1867 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1868 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001869 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001870 gtphub_plane_idx_names[plane_idx],
1871 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1872 }
1873
1874 gtphub_gc_start(hub);
1875 return 0;
1876}
1877
1878static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1879 const struct gsn_addr *addr)
1880{
1881 struct gtphub_peer_addr *a;
1882 llist_for_each_entry(a, &peer->addresses, entry) {
1883 if (gsn_addr_same(&a->addr, addr))
1884 return a;
1885 }
1886 return NULL;
1887}
1888
1889static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1890 uint16_t port)
1891{
1892 OSMO_ASSERT(port);
1893 struct gtphub_peer_port *pp;
1894 llist_for_each_entry(pp, &a->ports, entry) {
1895 if (pp->port == port)
1896 return pp;
1897 }
1898 return NULL;
1899}
1900
1901static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1902 const struct gsn_addr *addr)
1903{
1904 struct gtphub_peer *peer;
1905 llist_for_each_entry(peer, &bind->peers, entry) {
1906 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
1907 if (a)
1908 return a;
1909 }
1910 return NULL;
1911}
1912
1913static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
1914 const struct gsn_addr *addr,
1915 uint16_t port)
1916{
1917 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1918 if (!a)
1919 return NULL;
1920 return gtphub_addr_find_port(a, port);
1921}
1922
1923struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
1924 const struct osmo_sockaddr *addr)
1925{
1926 struct gsn_addr gsna;
1927 uint16_t port;
1928 gsn_addr_from_sockaddr(&gsna, &port, addr);
1929 return gtphub_port_find(bind, &gsna, port);
1930}
1931
1932static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
1933 struct gtphub_bind *bind)
1934{
1935 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer);
1936 OSMO_ASSERT(peer);
1937
1938 INIT_LLIST_HEAD(&peer->addresses);
1939
1940 nr_pool_init(&peer->seq_pool);
1941 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
1942
1943 /* TODO use something random to pick the initial sequence nr.
1944 0x6d31 produces the ASCII character sequence 'm1', currently used in
1945 gtphub_nc_test.sh. */
1946 peer->seq_pool.last_nr = 0x6d31 - 1;
1947
1948 llist_add(&peer->entry, &bind->peers);
1949 return peer;
1950}
1951
1952static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
1953 const struct gsn_addr *addr)
1954{
1955 struct gtphub_peer_addr *a;
1956 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
1957 OSMO_ASSERT(a);
1958 a->peer = peer;
1959 gsn_addr_copy(&a->addr, addr);
1960 INIT_LLIST_HEAD(&a->ports);
1961 llist_add(&a->entry, &peer->addresses);
1962
1963 return a;
1964}
1965
1966static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
1967 struct gtphub_bind *bind,
1968 const struct gsn_addr *addr)
1969{
1970 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1971 if (a)
1972 return a;
1973
1974 /* If we haven't found an address, that means we need to create an
1975 * entirely new peer for the new address. More addresses may be added
1976 * to this peer later, but not via this function. */
1977 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01001978
1979 a = gtphub_peer_add_addr(peer, addr);
1980
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001981 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001982 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01001983 gsn_addr_to_str(&a->addr));
1984
1985 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986}
1987
1988static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
1989 uint16_t port)
1990{
1991 struct gtphub_peer_port *pp;
1992
1993 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
1994 OSMO_ASSERT(pp);
1995 pp->peer_addr = a;
1996 pp->port = port;
1997
1998 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
1999 talloc_free(pp);
2000 return NULL;
2001 }
2002
2003 llist_add(&pp->entry, &a->ports);
2004
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002005 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002006 gsn_addr_to_str(&a->addr),
2007 (int)port);
2008
2009 return pp;
2010}
2011
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002012struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2013 struct gtphub_bind *bind,
2014 const struct gsn_addr *addr,
2015 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002016{
2017 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2018
2019 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2020 if (pp)
2021 return pp;
2022
2023 return gtphub_addr_add_port(a, port);
2024}
2025
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002026/* Find a GGSN peer with a matching address. If the address is known but the
2027 * port not, create a new port for that peer address. */
2028struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2029 const struct osmo_sockaddr *addr)
2030{
2031 struct gtphub_peer_addr *pa;
2032 struct gtphub_peer_port *pp;
2033
2034 struct gsn_addr gsna;
2035 uint16_t port;
2036 gsn_addr_from_sockaddr(&gsna, &port, addr);
2037
2038 pa = gtphub_addr_find(bind, &gsna);
2039 if (!pa)
2040 return NULL;
2041
2042 pp = gtphub_addr_find_port(pa, port);
2043
2044 if (!pp)
2045 pp = gtphub_addr_add_port(pa, port);
2046
2047 return pp;
2048}
2049
2050
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002051/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2052 * resolution should be possible but failed, and 1 if resolution was
2053 * successful. *pp will be set to NULL if <1 is returned. */
2054static int gtphub_resolve_ggsn(struct gtphub *hub,
2055 struct gtp_packet_desc *p,
2056 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002057{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002058 *pp = NULL;
2059
2060 /* TODO determine from message type whether IEs should be present? */
2061
2062 int rc;
2063 const char *imsi_str;
2064 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2065 if (rc < 1)
2066 return rc;
2067 OSMO_ASSERT(imsi_str);
2068
2069 const char *apn_str;
2070 rc = get_ie_apn_str(p->ie, &apn_str);
2071 if (rc < 1)
2072 return rc;
2073 OSMO_ASSERT(apn_str);
2074
2075 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2076 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002077}
2078
2079
2080/* TODO move to osmocom/core/socket.c ? */
2081/* The caller is required to call freeaddrinfo(*result), iff zero is returned. */
2082/* use this in osmo_sock_init() to remove dup. */
2083static int _osmo_getaddrinfo(struct addrinfo **result,
2084 uint16_t family, uint16_t type, uint8_t proto,
2085 const char *host, uint16_t port)
2086{
2087 struct addrinfo hints;
2088 char portbuf[16];
2089
2090 sprintf(portbuf, "%u", port);
2091 memset(&hints, '\0', sizeof(struct addrinfo));
2092 hints.ai_family = family;
2093 if (type == SOCK_RAW) {
2094 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2095 * SOCK_RAW and IPPROTO_GRE is used.
2096 */
2097 hints.ai_socktype = SOCK_DGRAM;
2098 hints.ai_protocol = IPPROTO_UDP;
2099 } else {
2100 hints.ai_socktype = type;
2101 hints.ai_protocol = proto;
2102 }
2103
2104 return getaddrinfo(host, portbuf, &hints, result);
2105}
2106
2107/* TODO move to osmocom/core/socket.c ? */
2108int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2109 uint16_t family, uint16_t type, uint8_t proto,
2110 const char *host, uint16_t port)
2111{
2112 struct addrinfo *res;
2113 int rc;
2114 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2115
2116 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002117 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002118 return -EINVAL;
2119 }
2120
2121 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2122 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2123 addr->l = res->ai_addrlen;
2124 freeaddrinfo(res);
2125
2126 return 0;
2127}
2128
2129int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2130 char *port_str, size_t port_str_len,
2131 const struct osmo_sockaddr *addr,
2132 int flags)
2133{
2134 int rc;
2135
2136 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2137 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2138 return -1;
2139 }
2140
2141 if (addr->l > sizeof(addr->a)) {
2142 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n", addr->l);
2143 return -1;
2144 }
2145
2146 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2147 addr_str, addr_str_len,
2148 port_str, port_str_len,
2149 flags);
2150
2151 if (rc)
2152 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n", gai_strerror(rc),
2153 osmo_hexdump((uint8_t*)&addr->a, addr->l));
2154
2155 return rc;
2156}
2157
2158const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2159 char *buf, size_t buf_len)
2160{
2161 const int portbuf_len = 6;
2162 OSMO_ASSERT(buf_len > portbuf_len);
2163 char *portbuf = buf + buf_len - portbuf_len;
2164 buf_len -= portbuf_len;
2165 if (osmo_sockaddr_to_strs(buf, buf_len,
2166 portbuf, portbuf_len,
2167 addr,
2168 NI_NUMERICHOST | NI_NUMERICSERV))
2169 return NULL;
2170
2171 char *pos = buf + strnlen(buf, buf_len-1);
2172 size_t len = buf_len - (pos - buf);
2173
2174 snprintf(pos, len, " port %s", portbuf);
2175 buf[buf_len-1] = '\0';
2176
2177 return buf;
2178}
2179
2180const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2181{
2182 static char buf[256];
2183 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2184 if (! result)
2185 return "(invalid)";
2186 return result;
2187}
2188
2189int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, const struct osmo_sockaddr *b)
2190{
2191 if (a == b)
2192 return 0;
2193 if (!a)
2194 return -1;
2195 if (!b)
2196 return 1;
2197 if (a->l != b->l) {
2198 /* Lengths are not the same, but determine the order. Will
2199 * anyone ever sort a list by osmo_sockaddr though...? */
2200 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2201 if (cmp == 0) {
2202 if (a->l < b->l)
2203 return -1;
2204 else
2205 return 1;
2206 }
2207 return cmp;
2208 }
2209 return memcmp(&a->a, &b->a, a->l);
2210}
2211
2212void osmo_sockaddr_copy(struct osmo_sockaddr *dst, const struct osmo_sockaddr *src)
2213{
2214 OSMO_ASSERT(src->l <= sizeof(dst->a));
2215 memcpy(&dst->a, &src->a, src->l);
2216 dst->l = src->l;
2217}