blob: 53152493f496094c21674bcfe18b12ab2ce2154a [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>
42
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010043
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020044static const int GTPH_GC_TICK_SECONDS = 1;
45
46void *osmo_gtphub_ctx;
47
Neels Hofmeyr063a8022015-11-16 14:35:13 +010048/* Convenience makro, note: only within this C file. */
49#define LOG(level, fmt, args...) \
50 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020051
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010052#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
53 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020054
55/* TODO move this to osmocom/core/select.h ? */
56typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
57
58/* TODO move this to osmocom/core/linuxlist.h ? */
59#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010060#define llist_first(head, type, entry) \
61 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020062
63/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
64
65enum gtp_rc {
66 GTP_RC_UNKNOWN = 0,
67 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010068 GTP_RC_PDU_C = 2, /* a real packet with IEs */
69 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020070
71 GTP_RC_TOOSHORT = -1,
72 GTP_RC_UNSUPPORTED_VERSION = -2,
73 GTP_RC_INVALID_IE = -3,
74};
75
76struct gtp_packet_desc {
77 union gtp_packet *data;
78 int data_len;
79 int header_len;
80 int version;
81 uint8_t type;
82 uint16_t seq;
83 uint32_t header_tei;
84 int rc; /* enum gtp_rc */
85 unsigned int plane_idx;
86 union gtpie_member *ie[GTPIE_SIZE];
87};
88
89void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
90{
91 memcpy(gsna, src, sizeof(struct gsn_addr));
92}
93
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020094int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
95 const struct osmo_sockaddr *sa)
96{
97 char addr_str[256];
98 char port_str[6];
99
100 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
101 port_str, sizeof(port_str),
102 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
103 != 0) {
104 return -1;
105 }
106
107 if (port)
108 *port = atoi(port_str);
109
110 return gsn_addr_from_str(gsna, addr_str);
111}
112
113int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
114{
115 int af = AF_INET;
116 gsna->len = 4;
117 const char *pos = numeric_addr_str;
118 for (; *pos; pos++) {
119 if (*pos == ':') {
120 af = AF_INET6;
121 gsna->len = 16;
122 break;
123 }
124 }
125
126 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
127 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100128 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
129 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200130 return -1;
131 }
132 return 0;
133}
134
135const char *gsn_addr_to_str(const struct gsn_addr *gsna)
136{
137 static char buf[INET6_ADDRSTRLEN + 1];
138 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
139}
140
141const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
142 char *strbuf,
143 int strbuf_len)
144{
145 int af;
146 switch (gsna->len) {
147 case 4:
148 af = AF_INET;
149 break;
150 case 16:
151 af = AF_INET6;
152 break;
153 default:
154 return NULL;
155 }
156
157 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
158 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100159 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
160 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100161 strerror(errno),
162 (int)gsna->len,
163 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200164 }
165 return r;
166}
167
168int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
169{
170 if (a == b)
171 return 1;
172 if ((!a) || (!b))
173 return 0;
174 if (a->len != b->len)
175 return 0;
176 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
177}
178
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100179static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
180 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200181{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100182 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200183 return -1;
184
185 unsigned int len;
186 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
187 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
188 &len, gsna->buf, sizeof(gsna->buf))
189 != 0)
190 return -1;
191 gsna->len = len;
192 return 0;
193}
194
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100195static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
196 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200197{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100198 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200199 return -1;
200
201 int ie_idx;
202 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
203
204 if (ie_idx < 0)
205 return -1;
206
207 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
208 int ie_l = ntoh16(ie->l);
209 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100210 LOG(LOGL_ERROR, "Not implemented:"
211 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200212 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
213 return -1;
214 }
215
216 memcpy(ie->v, gsna->buf, (int)ie_l);
217 return 0;
218}
219
220/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
221 */
222void validate_gtp0_header(struct gtp_packet_desc *p)
223{
224 const struct gtp0_header *pheader = &(p->data->gtp0.h);
225 p->rc = GTP_RC_UNKNOWN;
226 p->header_len = 0;
227
228 OSMO_ASSERT(p->data_len >= 1);
229 OSMO_ASSERT(p->version == 0);
230
231 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100232 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200233 p->rc = GTP_RC_TOOSHORT;
234 return;
235 }
236
237 p->type = ntoh8(pheader->type);
238 p->seq = ntoh16(pheader->seq);
239 p->header_tei = 0; /* TODO */
240
241 if (p->data_len == GTP0_HEADER_SIZE) {
242 p->rc = GTP_RC_TINY;
243 p->header_len = GTP0_HEADER_SIZE;
244 return;
245 }
246
247 /* Check packet length field versus length of packet */
248 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100249 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
250 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100251 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
252 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200253 p->rc = GTP_RC_TOOSHORT;
254 return;
255 }
256
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100257 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200258 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100259 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200260}
261
262/* Validate GTP version 1 data, and update p->rc with the result, as well as
263 * p->header_len in case of a valid header. */
264void validate_gtp1_header(struct gtp_packet_desc *p)
265{
266 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
267 p->rc = GTP_RC_UNKNOWN;
268 p->header_len = 0;
269
270 OSMO_ASSERT(p->data_len >= 1);
271 OSMO_ASSERT(p->version == 1);
272
273 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
274 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100275 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200276 p->rc = GTP_RC_TOOSHORT;
277 return;
278 }
279
280 p->type = ntoh8(pheader->type);
281 p->header_tei = ntoh32(pheader->tei);
282 p->seq = ntoh16(pheader->seq);
283
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100284 LOG(LOGL_DEBUG, "GTPv1\n"
285 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
286 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
287 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
288 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
289 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
290 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
291 p->type, p->type,
292 ntoh16(pheader->length), ntoh16(pheader->length),
293 p->header_tei, p->header_tei,
294 p->seq, p->seq,
295 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200296 pheader->next, pheader->next);
297
298 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
299 p->rc = GTP_RC_TINY;
300 p->header_len = GTP1_HEADER_SIZE_SHORT;
301 return;
302 }
303
304 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100305 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
306 if (p->data_len != announced_len) {
307 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
308 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100309 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
310 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200311 p->rc = GTP_RC_TOOSHORT;
312 return;
313 }
314
Neels Hofmeyre921e322015-11-11 00:45:50 +0100315 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200316 p->header_len = GTP1_HEADER_SIZE_LONG;
317}
318
319/* Examine whether p->data of size p->data_len has a valid GTP header. Set
320 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
321 * gtp_rc). p->data must point at a buffer with p->data_len set. */
322void validate_gtp_header(struct gtp_packet_desc *p)
323{
324 p->rc = GTP_RC_UNKNOWN;
325
326 /* Need at least 1 byte in order to check version */
327 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100328 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
329 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200330 p->rc = GTP_RC_TOOSHORT;
331 return;
332 }
333
334 p->version = p->data->flags >> 5;
335
336 switch (p->version) {
337 case 0:
338 validate_gtp0_header(p);
339 break;
340 case 1:
341 validate_gtp1_header(p);
342 break;
343 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100344 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345 p->rc = GTP_RC_UNSUPPORTED_VERSION;
346 break;
347 }
348}
349
350
351/* Return the value of the i'th IMSI IEI by copying to *imsi.
352 * The first IEI is reached by passing i = 0.
353 * imsi must point at allocated space of (at least) 8 bytes.
354 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100355static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200356{
357 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
358}
359
360/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100361static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200362{
363 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
364}
365
366static char imsi_digit_to_char(uint8_t nibble)
367{
368 nibble &= 0x0f;
369 if (nibble > 9)
370 return (nibble == 0x0f) ? '\0' : '?';
371 return '0' + nibble;
372}
373
374/* Return a human readable IMSI string, in a static buffer.
375 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100376static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200377{
378 static char str[17];
379 int i;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100380 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200381
382 for (i = 0; i < 8; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100383 c = imsi_digit_to_char(imsi[i]);
384 if (c == '?')
385 return -1;
386 str[2*i] = c;
387
388 c = imsi_digit_to_char(imsi[i] >> 4);
389 if (c == '?')
390 return -1;
391 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200392 }
393 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100394 *imsi_str = str;
395 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200396}
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. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100400static int get_ie_imsi_str(union gtpie_member *ie[], int i,
401 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100402{
403 uint8_t imsi_buf[8];
404 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100405 return 0;
406 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100407}
408
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100409/* Return 0 if not present, 1 if present and decoded successfully, -1 if
410 * present but cannot be decoded. */
411static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100412{
413 static char apn_buf[GSM_APN_LENGTH];
414 unsigned int len;
415 if (gtpie_gettlv(ie, GTPIE_APN, 0,
416 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100417 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100418
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100419 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100420 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
421 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100422 return -1;
423 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100424
425 if (len > (sizeof(apn_buf) - 1))
426 len = sizeof(apn_buf) - 1;
427 apn_buf[len] = '\0';
428
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100429 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
430 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100431 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
432 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100433 return -1;
434 }
435 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100436}
437
438
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200439/* Validate header, and index information elements. Write decoded packet
440 * information to *res. res->data will point at the given data buffer. On
441 * error, p->rc is set <= 0 (see enum gtp_rc). */
442static void gtp_decode(const uint8_t *data, int data_len,
443 unsigned int from_plane_idx,
444 struct gtp_packet_desc *res)
445{
446 ZERO_STRUCT(res);
447 res->data = (union gtp_packet*)data;
448 res->data_len = data_len;
449 res->plane_idx = from_plane_idx;
450
451 validate_gtp_header(res);
452
453 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100454 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200455 return;
456 }
457
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100458 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200459
Neels Hofmeyre921e322015-11-11 00:45:50 +0100460 if (from_plane_idx == GTPH_PLANE_USER) {
461 res->rc = GTP_RC_PDU_U;
462 return;
463 }
464
465 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100466 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200467 return;
468 }
469
470 if (gtpie_decaps(res->ie, res->version,
471 (void*)(data + res->header_len),
472 res->data_len - res->header_len) != 0) {
473 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100474 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
475 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200476 return;
477 }
478
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100479#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100480 /* TODO if (<loglevel is debug>) { ...
481 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200482 int i;
483
484 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100485 const char *imsi;
486 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200487 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100488 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200489 }
490
491 for (i = 0; i < 10; i++) {
492 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100493 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200494 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100495 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496 }
497
498 for (i = 0; i < 2; i++) {
499 struct gsn_addr addr;
500 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100501 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200502 }
503
504 for (i = 0; i < 10; i++) {
505 uint32_t tei;
506 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
507 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100508 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200509 tei, tei);
510 }
511
512 for (i = 0; i < 10; i++) {
513 uint32_t tei;
514 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
515 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100516 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200517 tei, tei);
518 }
519#endif
520}
521
522
523/* expiry */
524
525void expiry_init(struct expiry *exq, int expiry_in_seconds)
526{
527 ZERO_STRUCT(exq);
528 exq->expiry_in_seconds = expiry_in_seconds;
529 INIT_LLIST_HEAD(&exq->items);
530}
531
532void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
533{
534 item->expiry = now + exq->expiry_in_seconds;
535
536 /* Add/move to the tail to always sort by expiry, ascending. */
537 llist_del(&item->entry);
538 llist_add_tail(&item->entry, &exq->items);
539}
540
541int expiry_tick(struct expiry *exq, time_t now)
542{
543 int expired = 0;
544 struct expiring_item *m, *n;
545 llist_for_each_entry_safe(m, n, &exq->items, entry) {
546 if (m->expiry <= now) {
547 expiring_item_del(m);
548 expired ++;
549 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200550 /* The items are added sorted by expiry. So when we hit
551 * an unexpired entry, only more unexpired ones will
552 * follow. */
553 break;
554 }
555 }
556 return expired;
557}
558
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100559void expiry_clear(struct expiry *exq)
560{
561 struct expiring_item *m, *n;
562 llist_for_each_entry_safe(m, n, &exq->items, entry) {
563 expiring_item_del(m);
564 }
565}
566
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200567void expiring_item_init(struct expiring_item *item)
568{
569 ZERO_STRUCT(item);
570 INIT_LLIST_HEAD(&item->entry);
571}
572
573void expiring_item_del(struct expiring_item *item)
574{
575 OSMO_ASSERT(item);
576 llist_del(&item->entry);
577 INIT_LLIST_HEAD(&item->entry);
578 if (item->del_cb) {
579 /* avoid loops */
580 del_cb_t del_cb = item->del_cb;
581 item->del_cb = 0;
582 (del_cb)(item);
583 }
584}
585
586
587/* nr_map, nr_pool */
588
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100589void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200590{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100591 *pool = (struct nr_pool){
592 .nr_min = nr_min,
593 .nr_max = nr_max,
594 .last_nr = nr_max
595 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200596}
597
598nr_t nr_pool_next(struct nr_pool *pool)
599{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100600 if (pool->last_nr >= pool->nr_max)
601 pool->last_nr = pool->nr_min;
602 else
603 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200604
605 return pool->last_nr;
606}
607
608void nr_map_init(struct nr_map *map, struct nr_pool *pool,
609 struct expiry *exq)
610{
611 ZERO_STRUCT(map);
612 map->pool = pool;
613 map->add_items_to_expiry = exq;
614 INIT_LLIST_HEAD(&map->mappings);
615}
616
617void nr_mapping_init(struct nr_mapping *m)
618{
619 ZERO_STRUCT(m);
620 INIT_LLIST_HEAD(&m->entry);
621 expiring_item_init(&m->expiry_entry);
622}
623
624void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
625{
626 /* Generate a mapped number */
627 mapping->repl = nr_pool_next(map->pool);
628
629 /* Add to the tail to always yield a list sorted by expiry, in
630 * ascending order. */
631 llist_add_tail(&mapping->entry, &map->mappings);
632 if (map->add_items_to_expiry)
633 expiry_add(map->add_items_to_expiry,
634 &mapping->expiry_entry,
635 now);
636}
637
638void nr_map_clear(struct nr_map *map)
639{
640 struct nr_mapping *m;
641 struct nr_mapping *n;
642 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
643 nr_mapping_del(m);
644 }
645}
646
647int nr_map_empty(const struct nr_map *map)
648{
649 return llist_empty(&map->mappings);
650}
651
652struct nr_mapping *nr_map_get(const struct nr_map *map,
653 void *origin, nr_t nr_orig)
654{
655 struct nr_mapping *mapping;
656 llist_for_each_entry(mapping, &map->mappings, entry) {
657 if ((mapping->origin == origin)
658 && (mapping->orig == nr_orig))
659 return mapping;
660 }
661 /* Not found. */
662 return NULL;
663}
664
665struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
666{
667 struct nr_mapping *mapping;
668 llist_for_each_entry(mapping, &map->mappings, entry) {
669 if (mapping->repl == nr_repl) {
670 return mapping;
671 }
672 }
673 /* Not found. */
674 return NULL;
675}
676
677void nr_mapping_del(struct nr_mapping *mapping)
678{
679 OSMO_ASSERT(mapping);
680 llist_del(&mapping->entry);
681 INIT_LLIST_HEAD(&mapping->entry);
682 expiring_item_del(&mapping->expiry_entry);
683}
684
685
686/* gtphub */
687
688const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
689 "CTRL",
690 "USER",
691};
692
693const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
694 2123,
695 2152,
696};
697
698time_t gtphub_now(void)
699{
700 struct timespec now_tp;
701 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
702 return now_tp.tv_sec;
703}
704
705/* Remove a gtphub_peer from its list and free it. */
706static void gtphub_peer_del(struct gtphub_peer *peer)
707{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100708 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200709 nr_map_clear(&peer->seq_map);
710 llist_del(&peer->entry);
711 talloc_free(peer);
712}
713
714static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
715{
716 OSMO_ASSERT(llist_empty(&pa->ports));
717 llist_del(&pa->entry);
718 talloc_free(pa);
719}
720
721static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
722{
723 OSMO_ASSERT(pp->ref_count == 0);
724 llist_del(&pp->entry);
725 talloc_free(pp);
726}
727
728/* From the information in the gtp_packet_desc, return the address of a GGSN.
729 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100730static int gtphub_resolve_ggsn(struct gtphub *hub,
731 struct gtp_packet_desc *p,
732 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200733
734/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100735struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
736 const char *imsi_str,
737 const char *apn_ni_str);
738int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200739
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200740static void gtphub_zero(struct gtphub *hub)
741{
742 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100743 INIT_LLIST_HEAD(&hub->ggsn_lookups);
744 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200745}
746
747static int gtphub_sock_init(struct osmo_fd *ofd,
748 const struct gtphub_cfg_addr *addr,
749 osmo_fd_cb_t cb,
750 void *data,
751 int ofd_id)
752{
753 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100754 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200755 return -1;
756 }
757 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100758 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200759 return -1;
760 }
761
762 ofd->when = BSC_FD_READ;
763 ofd->cb = cb;
764 ofd->data = data;
765 ofd->priv_nr = ofd_id;
766
767 int rc;
768 rc = osmo_sock_init_ofd(ofd,
769 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
770 addr->addr_str, addr->port,
771 OSMO_SOCK_F_BIND);
772 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100773 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
774 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200775 return -1;
776 }
777
778 return 0;
779}
780
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100781static void gtphub_sock_close(struct osmo_fd *ofd)
782{
783 close(ofd->fd);
784 osmo_fd_unregister(ofd);
785 ofd->cb = NULL;
786}
787
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200788static void gtphub_bind_init(struct gtphub_bind *b)
789{
790 ZERO_STRUCT(b);
791
792 INIT_LLIST_HEAD(&b->peers);
793}
794
795static int gtphub_bind_start(struct gtphub_bind *b,
796 const struct gtphub_cfg_bind *cfg,
797 osmo_fd_cb_t cb, void *cb_data,
798 unsigned int ofd_id)
799{
800 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
801 return -1;
802 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
803 return -1;
804 return 0;
805}
806
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100807static void gtphub_bind_free(struct gtphub_bind *b)
808{
809 OSMO_ASSERT(llist_empty(&b->peers));
810}
811
812static void gtphub_bind_stop(struct gtphub_bind *b) {
813 gtphub_sock_close(&b->ofd);
814 gtphub_bind_free(b);
815}
816
Neels Hofmeyr40348972015-11-17 12:22:28 +0100817/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200818 * Return the number of bytes read, zero on error. */
819static int gtphub_read(const struct osmo_fd *from,
820 struct osmo_sockaddr *from_addr,
821 uint8_t *buf, size_t buf_len)
822{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100823 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200824
Neels Hofmeyr40348972015-11-17 12:22:28 +0100825 /* recvfrom requires the available length set in *from_addr_len. */
826 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200827 errno = 0;
828 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100829 (struct sockaddr*)&from_addr->a,
830 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200831 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
832 * is not truncated. Then maybe reduce buf's size. */
833
834 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100835 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
836 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200837 return 0;
838 }
839
Neels Hofmeyr40348972015-11-17 12:22:28 +0100840 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
841 (int)received, osmo_sockaddr_to_str(from_addr),
842 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200843
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200844 return received;
845}
846
847inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
848{
849 OSMO_ASSERT(pp->ref_count < UINT_MAX);
850 pp->ref_count++;
851}
852
853inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
854{
855 OSMO_ASSERT(pp->ref_count > 0);
856 pp->ref_count--;
857}
858
859inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
860{
861 OSMO_ASSERT(p->version == 1);
862 p->data->gtp1l.h.seq = hton16(seq);
863 p->seq = seq;
864}
865
866inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
867{
868 OSMO_ASSERT(p->version == 1);
869 p->data->gtp1l.h.tei = hton32(tei);
870 p->header_tei = tei;
871}
872
873static void gtphub_mapping_del_cb(struct expiring_item *expi);
874
875static struct nr_mapping *gtphub_mapping_new()
876{
877 struct nr_mapping *nrm;
878 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
879 OSMO_ASSERT(nrm);
880
881 nr_mapping_init(nrm);
882 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
883 return nrm;
884}
885
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100886static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
887 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200888{
889 if (llist_empty(&peer->addresses))
890 return "(addressless)";
891
892 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
893 struct gtphub_peer_addr,
894 entry);
895 return gsn_addr_to_strb(&a->addr, buf, buflen);
896}
897
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100898static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
899 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200900{
901 if (!port)
902 return "(null port)";
903
904 snprintf(buf, buflen, "%s port %d",
905 gsn_addr_to_str(&port->peer_addr->addr),
906 (int)port->port);
907 return buf;
908}
909
910const char *gtphub_peer_str(struct gtphub_peer *peer)
911{
912 static char buf[256];
913 return gtphub_peer_strb(peer, buf, sizeof(buf));
914}
915
916const char *gtphub_peer_str2(struct gtphub_peer *peer)
917{
918 static char buf[256];
919 return gtphub_peer_strb(peer, buf, sizeof(buf));
920}
921
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100922const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923{
924 static char buf[256];
925 return gtphub_port_strb(port, buf, sizeof(buf));
926}
927
928static const char *gtphub_port_str2(struct gtphub_peer_port *port)
929{
930 static char buf[256];
931 return gtphub_port_strb(port, buf, sizeof(buf));
932}
933
934static void gtphub_mapping_del_cb(struct expiring_item *expi)
935{
936 expi->del_cb = 0; /* avoid recursion loops */
937
938 struct nr_mapping *nrm = container_of(expi,
939 struct nr_mapping,
940 expiry_entry);
941 llist_del(&nrm->entry);
942 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
943
944 /* Just for log */
945 struct gtphub_peer_port *from = nrm->origin;
946 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100947 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200948 (int)nrm->expiry_entry.expiry,
949 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100950 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200951
952 gtphub_port_ref_count_dec(from);
953
954 talloc_free(nrm);
955}
956
957static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
958 struct gtphub_peer_port *from,
959 nr_t orig_nr,
960 time_t now)
961{
962 struct nr_mapping *nrm;
963
964 nrm = nr_map_get(map, from, orig_nr);
965
966 if (!nrm) {
967 nrm = gtphub_mapping_new();
968 nrm->orig = orig_nr;
969 nrm->origin = from;
970 nr_map_add(map, nrm, now);
971 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100972 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200973 gtphub_port_str(from),
974 (int)(nrm->orig), (int)(nrm->repl));
975 } else {
976 /* restart expiry timeout */
977 expiry_add(map->add_items_to_expiry, &nrm->expiry_entry,
978 now);
979 }
980
981 OSMO_ASSERT(nrm);
982 return nrm;
983}
984
985static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
986 int plane_idx,
987 struct gtphub_peer_port *from,
988 uint32_t orig_tei,
989 time_t now)
990{
991 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
992 from, orig_tei, now);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100993 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200994 gtphub_plane_idx_names[plane_idx],
995 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100996 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200997
998 return (uint32_t)nrm->repl;
999}
1000
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001001static void gtphub_map_seq(struct gtp_packet_desc *p,
1002 struct gtphub_peer_port *from_port,
1003 struct gtphub_peer_port *to_port,
1004 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001005{
1006 /* Store a mapping in to_peer's map, so when we later receive a GTP
1007 * packet back from to_peer, the seq nr can be unmapped back to its
1008 * origin (from_peer here). */
1009 struct nr_mapping *nrm;
1010 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
1011 from_port, p->seq, now);
1012
1013 /* Change the GTP packet to yield the new, mapped seq nr */
1014 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001015}
1016
1017static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1018 struct gtphub_peer_port *responding_port)
1019{
1020 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001021 struct nr_mapping *nrm =
1022 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1023 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001024 if (!nrm)
1025 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001026 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1027 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001028 set_seq(p, nrm->orig);
1029 return nrm->origin;
1030}
1031
1032static void gtphub_check_restart_counter(struct gtphub *hub,
1033 struct gtp_packet_desc *p,
1034 struct gtphub_peer_port *from)
1035{
1036 /* TODO */
1037 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1038 * that doesn't match the peer's previously sent restart counter, clear
1039 * that peer and cancel PDP contexts. */
1040}
1041
1042static void gtphub_map_restart_counter(struct gtphub *hub,
1043 struct gtp_packet_desc *p,
1044 struct gtphub_peer_port *from,
1045 struct gtphub_peer_port *to)
1046{
1047 /* TODO */
1048}
1049
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001050static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1051 struct gtphub *hub,
1052 struct gtp_packet_desc *p,
1053 struct gtphub_peer_port *from_port)
1054{
1055 OSMO_ASSERT(p->version == 1);
1056 *to_port_p = NULL;
1057
1058 /* If the header's TEI is zero, no PDP context has been established
1059 * yet. If nonzero, a mapping should actually already exist for this
1060 * TEI, since it must have been announced in a PDP context creation. */
1061 uint32_t tei = p->header_tei;
1062 if (!tei)
1063 return 0;
1064
1065 /* to_peer has previously announced a TEI, which was stored and
1066 * mapped in from_peer's tei_map. */
1067 struct nr_mapping *nrm;
1068 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1069 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001070 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1071 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001072 return -1;
1073 }
1074
1075 struct gtphub_peer_port *to_port = nrm->origin;
1076 uint32_t unmapped_tei = nrm->orig;
1077 set_tei(p, unmapped_tei);
1078
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001079 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1080 gtphub_port_str(from_port),
1081 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001082 gtphub_port_str2(to_port));
1083
1084 *to_port_p = to_port;
1085 return 0;
1086}
1087
1088/* Read GSN address IEs from p, and make sure these peer addresses exist in
1089 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1090 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1091 * the packet p. */
1092static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
1093 struct gtphub_bind from_bind[],
1094 struct gtphub_bind to_bind[],
1095 struct gtp_packet_desc *p,
1096 time_t now)
1097{
1098 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1099
1100 int rc;
1101 int plane_idx;
1102
1103 switch (p->type) {
1104 case GTP_CREATE_PDP_REQ:
1105 case GTP_CREATE_PDP_RSP:
1106 /* Go for it below */
1107 break;
1108 default:
1109 /* Nothing to do for this message type. */
1110 return 0;
1111 }
1112
1113 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1114
1115 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1116 plane_nrs_match_GSN_addr_IE_indices);
1117
1118 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1119 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1120
1121 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1122 struct gsn_addr addr_from_ie;
1123 uint32_t tei_from_ie;
1124 int ie_idx;
1125
1126 /* Fetch GSN Address and TEI from IEs */
1127 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1128 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001129 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1130 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001131 return -1;
1132 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001133 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001134 gtphub_plane_idx_names[plane_idx],
1135 gsn_addr_to_str(&addr_from_ie),
1136 addr_from_ie.len);
1137
1138 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1139 if (ie_idx < 0) {
1140 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001141 LOG(LOGL_ERROR,
1142 "Create PDP Context message invalid:"
1143 " missing IE %d\n",
1144 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001145 return -1;
1146 }
1147 tei_from_ie = 0;
1148 }
1149 else
1150 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1151
1152 /* Make sure an entry for this peer address with default port
1153 * exists */
1154 struct gtphub_peer_port *peer_from_ie =
1155 gtphub_port_have(hub, &from_bind[plane_idx],
1156 &addr_from_ie,
1157 gtphub_plane_idx_default_port[plane_idx]);
1158
1159 if (tei_from_ie) {
1160 /* Create TEI mapping and replace in GTP packet IE */
1161 uint32_t mapped_tei =
1162 gtphub_tei_mapping_have(hub, plane_idx,
1163 peer_from_ie,
1164 tei_from_ie,
1165 now);
1166 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1167 }
1168
1169 /* Replace the GSN address to reflect gtphub. */
1170 rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx);
1171 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001172 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1173 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001174 return -1;
1175 }
1176 }
1177
1178 return 0;
1179}
1180
1181static int gtphub_write(const struct osmo_fd *to,
1182 const struct osmo_sockaddr *to_addr,
1183 const uint8_t *buf, size_t buf_len)
1184{
1185 errno = 0;
1186 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1187 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001188 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001189
1190 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001191 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001192 return -EINVAL;
1193 }
1194
1195 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001196 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1197 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001198 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001199 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1200 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001201
1202 return 0;
1203}
1204
1205static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1206{
1207 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1208 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001209 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1210 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001211 if (!(what & BSC_FD_READ))
1212 return 0;
1213
1214 struct gtphub *hub = from_ggsns_ofd->data;
1215
1216 static uint8_t buf[4096];
1217 struct osmo_sockaddr from_addr;
1218 struct osmo_sockaddr to_addr;
1219 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001220 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001221 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001222
1223 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1224 if (len < 1)
1225 return 0;
1226
1227 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1228 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001229 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001230 if (len < 1)
1231 return 0;
1232
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001233 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001234}
1235
1236static int gtphub_unmap(struct gtphub *hub,
1237 struct gtp_packet_desc *p,
1238 struct gtphub_peer_port *from,
1239 struct gtphub_peer_port *to_proxy,
1240 struct gtphub_peer_port **final_unmapped,
1241 struct gtphub_peer_port **unmapped_from_seq,
1242 struct gtphub_peer_port **unmapped_from_tei)
1243{
1244 /* Always (try to) unmap sequence and TEI numbers, which need to be
1245 * replaced in the packet. Either way, give precedence to the proxy, if
1246 * configured. */
1247
1248 struct gtphub_peer_port *from_seq = NULL;
1249 struct gtphub_peer_port *from_tei = NULL;
1250 struct gtphub_peer_port *unmapped = NULL;
1251
1252 if (unmapped_from_seq)
1253 *unmapped_from_seq = from_seq;
1254 if (unmapped_from_tei)
1255 *unmapped_from_tei = from_tei;
1256 if (final_unmapped)
1257 *final_unmapped = unmapped;
1258
1259 from_seq = gtphub_unmap_seq(p, from);
1260
1261 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1262 return -1;
1263
1264 struct gtphub_peer *from_peer = from->peer_addr->peer;
1265 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001266 LOG(LOGL_DEBUG,
1267 "Seq unmap and TEI unmap yield two different peers."
1268 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001269 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1270 gtphub_plane_idx_names[p->plane_idx],
1271 gtphub_peer_str(from_peer),
1272 (int)p->seq,
1273 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001274 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001275 gtphub_port_str2(from_tei)
1276 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001277 }
1278 unmapped = (from_seq? from_seq : from_tei);
1279
1280 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001281 LOG(LOGL_NOTICE,
1282 "Unmap yields a different peer than the configured proxy."
1283 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001284 " unmapped: %s proxy: %s\n",
1285 gtphub_port_str(unmapped),
1286 gtphub_port_str2(to_proxy)
1287 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001288 }
1289 unmapped = (to_proxy? to_proxy : unmapped);
1290
1291 if (!unmapped) {
1292 /* Return no error, but returned pointers are all NULL. */
1293 return 0;
1294 }
1295
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001296 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001297 from_seq, from_tei, unmapped);
1298
1299 if (unmapped_from_seq)
1300 *unmapped_from_seq = from_seq;
1301 if (unmapped_from_tei)
1302 *unmapped_from_tei = from_tei;
1303 if (final_unmapped)
1304 *final_unmapped = unmapped;
1305 return 0;
1306}
1307
1308static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1309 uint16_t port,
1310 struct osmo_sockaddr *dst)
1311{
1312 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1313}
1314
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001315/* If p is an Echo request, replace p's data with the matching response and
1316 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1317 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001318static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1319 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001320{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001321 if (p->type != GTP_ECHO_REQ)
1322 return 0;
1323
1324 static uint8_t echo_response_data[14] = {
1325 0x32, /* flags */
1326 GTP_ECHO_RSP,
1327 0x00, 14 - 8, /* Length in network byte order */
1328 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1329 0, 0, /* Seq, to be replaced */
1330 0, 0, /* no extensions */
1331 0x0e, /* Recovery IE */
1332 0 /* Recovery counter, to be replaced */
1333 };
1334 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1335 uint8_t *recovery = &echo_response_data[13];
1336
1337 *seq = hton16(p->seq);
1338 *recovery = hub->restart_counter;
1339
1340 *reply_buf = echo_response_data;
1341
1342 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001343}
1344
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001345struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1346 const struct osmo_sockaddr *addr);
1347
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001348/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1349 * address to forward to. Return a pointer to the osmo_fd, but copy the
1350 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1351 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1352 * bytes to forward, 0 or less on failure. */
1353int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1354 unsigned int plane_idx,
1355 const struct osmo_sockaddr *from_addr,
1356 uint8_t *buf,
1357 size_t received,
1358 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001359 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001360 struct osmo_fd **to_ofd,
1361 struct osmo_sockaddr *to_addr)
1362{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001363 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001364 gtphub_plane_idx_names[plane_idx],
1365 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001366
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001367 static struct gtp_packet_desc p;
1368 gtp_decode(buf, received, plane_idx, &p);
1369
1370 if (p.rc <= 0)
1371 return -1;
1372
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001373 int reply_len;
1374 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1375 if (reply_len > 0) {
1376 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001377 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001378 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1379 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001380 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001381 if (reply_len < 0)
1382 return -1;
1383
1384 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001385
1386 /* If a GGSN proxy is configured, check that it's indeed that proxy
1387 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1388 * gtphub, so no-one else is allowed to talk to us from that side. */
1389 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1390 if (ggsn) {
1391 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001392 LOG(LOGL_ERROR,
1393 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001394 " received on GGSN bind is from another sender:"
1395 " proxy: %s sender: %s\n",
1396 gtphub_port_str(ggsn),
1397 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001398 return -1;
1399 }
1400 }
1401
1402 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001403 /* Find a GGSN peer with a matching address. The sender's port
1404 * may in fact differ. */
1405 ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx],
1406 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001407 }
1408
1409 /* If any PDP context has been created, we already have an entry for
1410 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1411 * us about. */
1412 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001413 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1414 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001415 return -1;
1416 }
1417
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001418 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001419
1420 struct gtphub_peer_port *sgsn_from_seq;
1421 struct gtphub_peer_port *sgsn;
1422 if (gtphub_unmap(hub, &p, ggsn,
1423 hub->sgsn_proxy[plane_idx],
1424 &sgsn, &sgsn_from_seq,
1425 NULL /* not interested, got it in &sgsn already */
1426 )
1427 != 0) {
1428 return -1;
1429 }
1430
1431 if (!sgsn) {
1432 /* A GGSN initiated request would go to a known TEI. So this is
1433 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001434 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001435 return -1;
1436 }
1437
1438 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001439 /* This may be a Create PDP Context response. If it is, there
1440 * are other addresses in the GTP message to set up apart from
1441 * the sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001442 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns,
1443 hub->to_sgsns, &p, now)
1444 != 0)
1445 return -1;
1446 }
1447
1448 gtphub_check_restart_counter(hub, &p, ggsn);
1449 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1450
1451 /* If the GGSN is replying to an SGSN request, the sequence nr has
1452 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1453 * create a new mapping. */
1454 if (!sgsn_from_seq)
1455 gtphub_map_seq(&p, ggsn, sgsn, now);
1456
1457 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001458
1459 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001460
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001461 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001462 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001463 return received;
1464}
1465
1466static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1467{
1468 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1469 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001470 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1471 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001472
1473 if (!(what & BSC_FD_READ))
1474 return 0;
1475
1476 struct gtphub *hub = from_sgsns_ofd->data;
1477
1478 static uint8_t buf[4096];
1479 struct osmo_sockaddr from_addr;
1480 struct osmo_sockaddr to_addr;
1481 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001482 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001483 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001484
1485 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1486 if (len < 1)
1487 return 0;
1488
1489 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1490 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001491 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001492 if (len < 1)
1493 return 0;
1494
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001495 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001496}
1497
1498/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1499int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1500 unsigned int plane_idx,
1501 const struct osmo_sockaddr *from_addr,
1502 uint8_t *buf,
1503 size_t received,
1504 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001505 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001506 struct osmo_fd **to_ofd,
1507 struct osmo_sockaddr *to_addr)
1508{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001509 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001510 gtphub_plane_idx_names[plane_idx],
1511 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001512
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001513 static struct gtp_packet_desc p;
1514 gtp_decode(buf, received, plane_idx, &p);
1515
1516 if (p.rc <= 0)
1517 return -1;
1518
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001519 int reply_len;
1520 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1521 if (reply_len > 0) {
1522 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001523 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyr6187e012015-11-20 00:57:05 +01001524 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001525 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001526 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001527 if (reply_len < 0)
1528 return -1;
1529
1530 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001531
1532 /* If an SGSN proxy is configured, check that it's indeed that proxy
1533 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1534 * gtphub, so no-one else is allowed to talk to us from that side. */
1535 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1536 if (sgsn) {
1537 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001538 LOG(LOGL_ERROR,
1539 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001540 " received on GGSN bind is from another sender:"
1541 " proxy: %s sender: %s\n",
1542 gtphub_port_str(sgsn),
1543 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001544 return -1;
1545 }
1546 }
1547
1548 if (!sgsn) {
1549 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001550 * entry for this SGSN. The port may differ. */
1551 sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx],
1552 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001553 }
1554
1555 if (!sgsn) {
1556 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1557 * first contact without being known yet, so create the peer
1558 * struct for the current sender. */
1559 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001560 LOG(LOGL_ERROR,
1561 "User plane peer was not announced by PDP Context,"
1562 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001563 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001564 return -1;
1565 }
1566
1567 struct gsn_addr from_gsna;
1568 uint16_t from_port;
1569 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1570 return -1;
1571
1572 sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx],
1573 &from_gsna, from_port);
1574 }
1575
1576 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001577 /* This could theoretically happen for invalid address data or
1578 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001579 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1580 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001581 return -1;
1582 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001583 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001584
1585 struct gtphub_peer_port *ggsn_from_seq;
1586 struct gtphub_peer_port *ggsn;
1587 if (gtphub_unmap(hub, &p, sgsn,
1588 hub->ggsn_proxy[plane_idx],
1589 &ggsn, &ggsn_from_seq,
1590 NULL /* not interested, got it in &ggsn already */
1591 )
1592 != 0) {
1593 return -1;
1594 }
1595
Neels Hofmeyra208c732015-11-11 19:26:09 +01001596 if (!ggsn) {
1597 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1598 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001599 }
1600
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001601 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001602 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001603 return -1;
1604 }
1605
1606 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001607 /* This may be a Create PDP Context requst. If it is, there are
1608 * other addresses in the GTP message to set up apart from the
1609 * sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001610 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns,
1611 hub->to_ggsns, &p, now)
1612 != 0)
1613 return -1;
1614 }
1615
1616 gtphub_check_restart_counter(hub, &p, sgsn);
1617 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1618
1619 /* If the SGSN is replying to a GGSN request, the sequence nr has
1620 * already been unmapped above (unmap_ggsn != NULL), and we need not
1621 * create a new outgoing sequence map. */
1622 if (!ggsn_from_seq)
1623 gtphub_map_seq(&p, sgsn, ggsn, now);
1624
1625 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1626
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001627 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001628
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001629 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001630 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001631 return received;
1632}
1633
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001634static void resolved_gssn_del_cb(struct expiring_item *expi)
1635{
1636 struct gtphub_resolved_ggsn *ggsn;
1637 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1638
1639 gtphub_port_ref_count_dec(ggsn->peer);
1640 llist_del(&ggsn->entry);
1641
1642 ggsn->expiry_entry.del_cb = 0;
1643 expiring_item_del(&ggsn->expiry_entry);
1644
1645 talloc_free(ggsn);
1646}
1647
1648void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1649 struct gsn_addr *resolved_addr,
1650 time_t now)
1651{
1652 struct gtphub_peer_port *pp;
1653 struct gtphub_resolved_ggsn *ggsn;
1654
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001655 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001656 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1657 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001658
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001659 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1660 resolved_addr, 2123);
1661 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001662 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1663 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001664 return;
1665 }
1666
1667 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1668 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001669 INIT_LLIST_HEAD(&ggsn->entry);
1670 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001671
1672 ggsn->peer = pp;
1673 gtphub_port_ref_count_inc(pp);
1674
1675 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001676 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001677
1678 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1679 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1680
1681 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1682}
1683
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001684static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1685{
1686 return pp->ref_count == 0;
1687}
1688
1689static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1690{
1691 struct gtphub_peer_port *pp, *npp;
1692 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1693 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001694 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001695 gtphub_port_str(pp));
1696 gtphub_peer_port_del(pp);
1697 }
1698 }
1699 return llist_empty(&pa->ports);
1700}
1701
1702static int gtphub_gc_peer(struct gtphub_peer *p)
1703{
1704 struct gtphub_peer_addr *pa, *npa;
1705 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1706 if (gtphub_gc_peer_addr(pa)) {
1707 gtphub_peer_addr_del(pa);
1708 }
1709 }
1710
1711 /* Note that there's a ref_count in each gtphub_peer_port instance
1712 * listed within p->addresses, referenced by TEI mappings from
1713 * hub->tei_map. As long as those don't expire, this peer will stay. */
1714
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001715 return llist_empty(&p->addresses)
1716 && nr_map_empty(&p->seq_map);
1717}
1718
1719static void gtphub_gc_bind(struct gtphub_bind *b)
1720{
1721 struct gtphub_peer *p, *n;
1722 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1723 if (gtphub_gc_peer(p)) {
1724 gtphub_peer_del(p);
1725 }
1726 }
1727}
1728
1729void gtphub_gc(struct gtphub *hub, time_t now)
1730{
1731 int expired;
1732 expired = expiry_tick(&hub->expire_seq_maps, now);
1733 expired += expiry_tick(&hub->expire_tei_maps, now);
1734
1735 /* ... */
1736
1737 if (expired) {
1738 int i;
1739 for (i = 0; i < GTPH_PLANE_N; i++) {
1740 gtphub_gc_bind(&hub->to_sgsns[i]);
1741 gtphub_gc_bind(&hub->to_ggsns[i]);
1742 }
1743 }
1744}
1745
1746static void gtphub_gc_cb(void *data)
1747{
1748 struct gtphub *hub = data;
1749 gtphub_gc(hub, gtphub_now());
1750 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1751}
1752
1753static void gtphub_gc_start(struct gtphub *hub)
1754{
1755 hub->gc_timer.cb = gtphub_gc_cb;
1756 hub->gc_timer.data = hub;
1757
1758 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1759}
1760
1761/* called by unit tests */
1762void gtphub_init(struct gtphub *hub)
1763{
1764 gtphub_zero(hub);
1765
1766 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001767 expiry_init(&hub->expire_tei_maps,
1768 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001769
1770 int plane_idx;
1771 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001772 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001773 nr_map_init(&hub->tei_map[plane_idx],
1774 &hub->tei_pool[plane_idx],
1775 &hub->expire_tei_maps);
1776
1777 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1778 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1779 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001780
1781 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1782 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1783 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1784 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001785}
1786
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001787/* For the test suite, this is kept separate from gtphub_stop(), which also
1788 * closes sockets. The test suite avoids using sockets and would cause
1789 * segfaults when trying to close uninitialized ofds. */
1790void gtphub_free(struct gtphub *hub)
1791{
1792 /* By expiring all mappings, a garbage collection should free
1793 * everything else. A gtphub_bind_free() will assert that everything is
1794 * indeed empty. */
1795 expiry_clear(&hub->expire_seq_maps);
1796 expiry_clear(&hub->expire_tei_maps);
1797
1798 int plane_idx;
1799 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1800 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
1801 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
1802
1803 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
1804 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
1805 }
1806}
1807
1808void gtphub_stop(struct gtphub *hub)
1809{
1810 int plane_idx;
1811 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1812 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
1813 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
1814 }
1815 gtphub_free(hub);
1816}
1817
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001818static int gtphub_make_proxy(struct gtphub *hub,
1819 struct gtphub_peer_port **pp,
1820 struct gtphub_bind *bind,
1821 const struct gtphub_cfg_addr *addr)
1822{
1823 if (!addr->addr_str)
1824 return 0;
1825
1826 struct gsn_addr gsna;
1827 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1828 return -1;
1829
1830 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1831
1832 /* This is *the* proxy. Make sure it is never expired. */
1833 gtphub_port_ref_count_inc(*pp);
1834 return 0;
1835}
1836
1837int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1838{
1839 int rc;
1840
1841 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001842
1843 /* If a Ctrl plane proxy is configured, ares will never be used. */
1844 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1845 if (gtphub_ares_init(hub) != 0) {
1846 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1847 return -1;
1848 }
1849 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001850
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001851 /* TODO set hub->restart_counter from external file. */
1852
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001853 int plane_idx;
1854 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1855 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1856 &cfg->to_ggsns[plane_idx],
1857 from_ggsns_read_cb, hub, plane_idx);
1858 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001859 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1860 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001861 return rc;
1862 }
1863
1864 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1865 &cfg->to_sgsns[plane_idx],
1866 from_sgsns_read_cb, hub, plane_idx);
1867 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001868 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1869 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001870 return rc;
1871 }
1872 }
1873
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001874 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1875 if (gtphub_make_proxy(hub,
1876 &hub->sgsn_proxy[plane_idx],
1877 &hub->to_sgsns[plane_idx],
1878 &cfg->sgsn_proxy[plane_idx])
1879 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001880 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001881 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001882 cfg->sgsn_proxy[plane_idx].addr_str,
1883 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001884 return -1;
1885 }
1886 if (gtphub_make_proxy(hub,
1887 &hub->ggsn_proxy[plane_idx],
1888 &hub->to_ggsns[plane_idx],
1889 &cfg->ggsn_proxy[plane_idx])
1890 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001891 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001892 return -1;
1893 }
1894 }
1895
1896 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1897 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001898 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001899 gtphub_plane_idx_names[plane_idx],
1900 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1901 }
1902
1903 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001904 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001905 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001906 gtphub_plane_idx_names[plane_idx],
1907 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1908 }
1909
1910 gtphub_gc_start(hub);
1911 return 0;
1912}
1913
1914static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1915 const struct gsn_addr *addr)
1916{
1917 struct gtphub_peer_addr *a;
1918 llist_for_each_entry(a, &peer->addresses, entry) {
1919 if (gsn_addr_same(&a->addr, addr))
1920 return a;
1921 }
1922 return NULL;
1923}
1924
1925static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1926 uint16_t port)
1927{
1928 OSMO_ASSERT(port);
1929 struct gtphub_peer_port *pp;
1930 llist_for_each_entry(pp, &a->ports, entry) {
1931 if (pp->port == port)
1932 return pp;
1933 }
1934 return NULL;
1935}
1936
1937static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1938 const struct gsn_addr *addr)
1939{
1940 struct gtphub_peer *peer;
1941 llist_for_each_entry(peer, &bind->peers, entry) {
1942 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
1943 if (a)
1944 return a;
1945 }
1946 return NULL;
1947}
1948
1949static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
1950 const struct gsn_addr *addr,
1951 uint16_t port)
1952{
1953 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1954 if (!a)
1955 return NULL;
1956 return gtphub_addr_find_port(a, port);
1957}
1958
1959struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
1960 const struct osmo_sockaddr *addr)
1961{
1962 struct gsn_addr gsna;
1963 uint16_t port;
1964 gsn_addr_from_sockaddr(&gsna, &port, addr);
1965 return gtphub_port_find(bind, &gsna, port);
1966}
1967
1968static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
1969 struct gtphub_bind *bind)
1970{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001971 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
1972 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001973 OSMO_ASSERT(peer);
1974
1975 INIT_LLIST_HEAD(&peer->addresses);
1976
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001977 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001978 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
1979
1980 /* TODO use something random to pick the initial sequence nr.
1981 0x6d31 produces the ASCII character sequence 'm1', currently used in
1982 gtphub_nc_test.sh. */
1983 peer->seq_pool.last_nr = 0x6d31 - 1;
1984
1985 llist_add(&peer->entry, &bind->peers);
1986 return peer;
1987}
1988
1989static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
1990 const struct gsn_addr *addr)
1991{
1992 struct gtphub_peer_addr *a;
1993 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
1994 OSMO_ASSERT(a);
1995 a->peer = peer;
1996 gsn_addr_copy(&a->addr, addr);
1997 INIT_LLIST_HEAD(&a->ports);
1998 llist_add(&a->entry, &peer->addresses);
1999
2000 return a;
2001}
2002
2003static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2004 struct gtphub_bind *bind,
2005 const struct gsn_addr *addr)
2006{
2007 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2008 if (a)
2009 return a;
2010
2011 /* If we haven't found an address, that means we need to create an
2012 * entirely new peer for the new address. More addresses may be added
2013 * to this peer later, but not via this function. */
2014 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002015
2016 a = gtphub_peer_add_addr(peer, addr);
2017
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002018 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002019 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002020 gsn_addr_to_str(&a->addr));
2021
2022 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002023}
2024
2025static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2026 uint16_t port)
2027{
2028 struct gtphub_peer_port *pp;
2029
2030 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2031 OSMO_ASSERT(pp);
2032 pp->peer_addr = a;
2033 pp->port = port;
2034
2035 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2036 talloc_free(pp);
2037 return NULL;
2038 }
2039
2040 llist_add(&pp->entry, &a->ports);
2041
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002042 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002043 gsn_addr_to_str(&a->addr),
2044 (int)port);
2045
2046 return pp;
2047}
2048
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002049struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2050 struct gtphub_bind *bind,
2051 const struct gsn_addr *addr,
2052 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002053{
2054 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2055
2056 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2057 if (pp)
2058 return pp;
2059
2060 return gtphub_addr_add_port(a, port);
2061}
2062
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002063/* Find a GGSN peer with a matching address. If the address is known but the
2064 * port not, create a new port for that peer address. */
2065struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2066 const struct osmo_sockaddr *addr)
2067{
2068 struct gtphub_peer_addr *pa;
2069 struct gtphub_peer_port *pp;
2070
2071 struct gsn_addr gsna;
2072 uint16_t port;
2073 gsn_addr_from_sockaddr(&gsna, &port, addr);
2074
2075 pa = gtphub_addr_find(bind, &gsna);
2076 if (!pa)
2077 return NULL;
2078
2079 pp = gtphub_addr_find_port(pa, port);
2080
2081 if (!pp)
2082 pp = gtphub_addr_add_port(pa, port);
2083
2084 return pp;
2085}
2086
2087
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002088/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2089 * resolution should be possible but failed, and 1 if resolution was
2090 * successful. *pp will be set to NULL if <1 is returned. */
2091static int gtphub_resolve_ggsn(struct gtphub *hub,
2092 struct gtp_packet_desc *p,
2093 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002094{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002095 *pp = NULL;
2096
2097 /* TODO determine from message type whether IEs should be present? */
2098
2099 int rc;
2100 const char *imsi_str;
2101 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2102 if (rc < 1)
2103 return rc;
2104 OSMO_ASSERT(imsi_str);
2105
2106 const char *apn_str;
2107 rc = get_ie_apn_str(p->ie, &apn_str);
2108 if (rc < 1)
2109 return rc;
2110 OSMO_ASSERT(apn_str);
2111
2112 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2113 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002114}
2115
2116
2117/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002118/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002119/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2120 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002121static int _osmo_getaddrinfo(struct addrinfo **result,
2122 uint16_t family, uint16_t type, uint8_t proto,
2123 const char *host, uint16_t port)
2124{
2125 struct addrinfo hints;
2126 char portbuf[16];
2127
2128 sprintf(portbuf, "%u", port);
2129 memset(&hints, '\0', sizeof(struct addrinfo));
2130 hints.ai_family = family;
2131 if (type == SOCK_RAW) {
2132 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2133 * SOCK_RAW and IPPROTO_GRE is used.
2134 */
2135 hints.ai_socktype = SOCK_DGRAM;
2136 hints.ai_protocol = IPPROTO_UDP;
2137 } else {
2138 hints.ai_socktype = type;
2139 hints.ai_protocol = proto;
2140 }
2141
2142 return getaddrinfo(host, portbuf, &hints, result);
2143}
2144
2145/* TODO move to osmocom/core/socket.c ? */
2146int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2147 uint16_t family, uint16_t type, uint8_t proto,
2148 const char *host, uint16_t port)
2149{
2150 struct addrinfo *res;
2151 int rc;
2152 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2153
2154 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002155 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002156 return -EINVAL;
2157 }
2158
2159 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2160 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2161 addr->l = res->ai_addrlen;
2162 freeaddrinfo(res);
2163
2164 return 0;
2165}
2166
2167int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2168 char *port_str, size_t port_str_len,
2169 const struct osmo_sockaddr *addr,
2170 int flags)
2171{
2172 int rc;
2173
2174 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2175 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2176 return -1;
2177 }
2178
2179 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002180 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2181 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002182 return -1;
2183 }
2184
2185 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2186 addr_str, addr_str_len,
2187 port_str, port_str_len,
2188 flags);
2189
2190 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002191 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2192 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2193 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002194
2195 return rc;
2196}
2197
2198const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2199 char *buf, size_t buf_len)
2200{
2201 const int portbuf_len = 6;
2202 OSMO_ASSERT(buf_len > portbuf_len);
2203 char *portbuf = buf + buf_len - portbuf_len;
2204 buf_len -= portbuf_len;
2205 if (osmo_sockaddr_to_strs(buf, buf_len,
2206 portbuf, portbuf_len,
2207 addr,
2208 NI_NUMERICHOST | NI_NUMERICSERV))
2209 return NULL;
2210
2211 char *pos = buf + strnlen(buf, buf_len-1);
2212 size_t len = buf_len - (pos - buf);
2213
2214 snprintf(pos, len, " port %s", portbuf);
2215 buf[buf_len-1] = '\0';
2216
2217 return buf;
2218}
2219
2220const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2221{
2222 static char buf[256];
2223 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2224 if (! result)
2225 return "(invalid)";
2226 return result;
2227}
2228
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002229int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2230 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002231{
2232 if (a == b)
2233 return 0;
2234 if (!a)
2235 return -1;
2236 if (!b)
2237 return 1;
2238 if (a->l != b->l) {
2239 /* Lengths are not the same, but determine the order. Will
2240 * anyone ever sort a list by osmo_sockaddr though...? */
2241 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2242 if (cmp == 0) {
2243 if (a->l < b->l)
2244 return -1;
2245 else
2246 return 1;
2247 }
2248 return cmp;
2249 }
2250 return memcmp(&a->a, &b->a, a->l);
2251}
2252
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002253void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2254 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002255{
2256 OSMO_ASSERT(src->l <= sizeof(dst->a));
2257 memcpy(&dst->a, &src->a, src->l);
2258 dst->l = src->l;
2259}