blob: 9a5e8822896be9913fcdcc5d31b4d1a3d0a98024 [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,
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001093 struct gtphub_bind from_bind_arr[],
1094 struct gtphub_bind to_bind_arr[],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001095 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 =
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001155 gtphub_port_have(hub, &from_bind_arr[plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001156 &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. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001170 rc = gsn_addr_put(&to_bind_arr[plane_idx].local_addr, p,
1171 plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001172 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001173 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1174 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001175 return -1;
1176 }
1177 }
1178
1179 return 0;
1180}
1181
1182static int gtphub_write(const struct osmo_fd *to,
1183 const struct osmo_sockaddr *to_addr,
1184 const uint8_t *buf, size_t buf_len)
1185{
1186 errno = 0;
1187 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1188 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001189 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001190
1191 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001192 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001193 return -EINVAL;
1194 }
1195
1196 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001197 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1198 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001199 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001200 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1201 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001202
1203 return 0;
1204}
1205
1206static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1207{
1208 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1209 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001210 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1211 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001212 if (!(what & BSC_FD_READ))
1213 return 0;
1214
1215 struct gtphub *hub = from_ggsns_ofd->data;
1216
1217 static uint8_t buf[4096];
1218 struct osmo_sockaddr from_addr;
1219 struct osmo_sockaddr to_addr;
1220 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001221 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001222 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001223
1224 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1225 if (len < 1)
1226 return 0;
1227
1228 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1229 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001230 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001231 if (len < 1)
1232 return 0;
1233
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001234 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001235}
1236
1237static int gtphub_unmap(struct gtphub *hub,
1238 struct gtp_packet_desc *p,
1239 struct gtphub_peer_port *from,
1240 struct gtphub_peer_port *to_proxy,
1241 struct gtphub_peer_port **final_unmapped,
1242 struct gtphub_peer_port **unmapped_from_seq,
1243 struct gtphub_peer_port **unmapped_from_tei)
1244{
1245 /* Always (try to) unmap sequence and TEI numbers, which need to be
1246 * replaced in the packet. Either way, give precedence to the proxy, if
1247 * configured. */
1248
1249 struct gtphub_peer_port *from_seq = NULL;
1250 struct gtphub_peer_port *from_tei = NULL;
1251 struct gtphub_peer_port *unmapped = NULL;
1252
1253 if (unmapped_from_seq)
1254 *unmapped_from_seq = from_seq;
1255 if (unmapped_from_tei)
1256 *unmapped_from_tei = from_tei;
1257 if (final_unmapped)
1258 *final_unmapped = unmapped;
1259
1260 from_seq = gtphub_unmap_seq(p, from);
1261
1262 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1263 return -1;
1264
1265 struct gtphub_peer *from_peer = from->peer_addr->peer;
1266 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001267 LOG(LOGL_DEBUG,
1268 "Seq unmap and TEI unmap yield two different peers."
1269 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001270 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1271 gtphub_plane_idx_names[p->plane_idx],
1272 gtphub_peer_str(from_peer),
1273 (int)p->seq,
1274 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001275 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001276 gtphub_port_str2(from_tei)
1277 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001278 }
1279 unmapped = (from_seq? from_seq : from_tei);
1280
1281 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001282 LOG(LOGL_NOTICE,
1283 "Unmap yields a different peer than the configured proxy."
1284 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001285 " unmapped: %s proxy: %s\n",
1286 gtphub_port_str(unmapped),
1287 gtphub_port_str2(to_proxy)
1288 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001289 }
1290 unmapped = (to_proxy? to_proxy : unmapped);
1291
1292 if (!unmapped) {
1293 /* Return no error, but returned pointers are all NULL. */
1294 return 0;
1295 }
1296
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001297 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001298 from_seq, from_tei, unmapped);
1299
1300 if (unmapped_from_seq)
1301 *unmapped_from_seq = from_seq;
1302 if (unmapped_from_tei)
1303 *unmapped_from_tei = from_tei;
1304 if (final_unmapped)
1305 *final_unmapped = unmapped;
1306 return 0;
1307}
1308
1309static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1310 uint16_t port,
1311 struct osmo_sockaddr *dst)
1312{
1313 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1314}
1315
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001316/* If p is an Echo request, replace p's data with the matching response and
1317 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1318 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001319static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1320 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001321{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001322 if (p->type != GTP_ECHO_REQ)
1323 return 0;
1324
1325 static uint8_t echo_response_data[14] = {
1326 0x32, /* flags */
1327 GTP_ECHO_RSP,
1328 0x00, 14 - 8, /* Length in network byte order */
1329 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1330 0, 0, /* Seq, to be replaced */
1331 0, 0, /* no extensions */
1332 0x0e, /* Recovery IE */
1333 0 /* Recovery counter, to be replaced */
1334 };
1335 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1336 uint8_t *recovery = &echo_response_data[13];
1337
1338 *seq = hton16(p->seq);
1339 *recovery = hub->restart_counter;
1340
1341 *reply_buf = echo_response_data;
1342
1343 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001344}
1345
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001346struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1347 const struct osmo_sockaddr *addr);
1348
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001349/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1350 * address to forward to. Return a pointer to the osmo_fd, but copy the
1351 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1352 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1353 * bytes to forward, 0 or less on failure. */
1354int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1355 unsigned int plane_idx,
1356 const struct osmo_sockaddr *from_addr,
1357 uint8_t *buf,
1358 size_t received,
1359 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001360 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001361 struct osmo_fd **to_ofd,
1362 struct osmo_sockaddr *to_addr)
1363{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001364 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
1365 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
1366 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1367 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1368
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001369 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001370 gtphub_plane_idx_names[plane_idx],
1371 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001372
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001373 static struct gtp_packet_desc p;
1374 gtp_decode(buf, received, plane_idx, &p);
1375
1376 if (p.rc <= 0)
1377 return -1;
1378
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001379 int reply_len;
1380 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1381 if (reply_len > 0) {
1382 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001383 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001384 *to_ofd = &from_bind->ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001385 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001386 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001387 if (reply_len < 0)
1388 return -1;
1389
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001390 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001391
1392 /* If a GGSN proxy is configured, check that it's indeed that proxy
1393 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1394 * gtphub, so no-one else is allowed to talk to us from that side. */
1395 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1396 if (ggsn) {
1397 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001398 LOG(LOGL_ERROR,
1399 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001400 " received on GGSN bind is from another sender:"
1401 " proxy: %s sender: %s\n",
1402 gtphub_port_str(ggsn),
1403 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001404 return -1;
1405 }
1406 }
1407
1408 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001409 /* Find a GGSN peer with a matching address. The sender's port
1410 * may in fact differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001411 ggsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001412 }
1413
1414 /* If any PDP context has been created, we already have an entry for
1415 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1416 * us about. */
1417 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001418 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1419 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001420 return -1;
1421 }
1422
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001423 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001424
1425 struct gtphub_peer_port *sgsn_from_seq;
1426 struct gtphub_peer_port *sgsn;
1427 if (gtphub_unmap(hub, &p, ggsn,
1428 hub->sgsn_proxy[plane_idx],
1429 &sgsn, &sgsn_from_seq,
1430 NULL /* not interested, got it in &sgsn already */
1431 )
1432 != 0) {
1433 return -1;
1434 }
1435
1436 if (!sgsn) {
1437 /* A GGSN initiated request would go to a known TEI. So this is
1438 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001439 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001440 return -1;
1441 }
1442
1443 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001444 /* This may be a Create PDP Context response. If it is, there
1445 * are other addresses in the GTP message to set up apart from
1446 * the sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001447 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
1448 &p, now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001449 != 0)
1450 return -1;
1451 }
1452
1453 gtphub_check_restart_counter(hub, &p, ggsn);
1454 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1455
1456 /* If the GGSN is replying to an SGSN request, the sequence nr has
1457 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1458 * create a new mapping. */
1459 if (!sgsn_from_seq)
1460 gtphub_map_seq(&p, ggsn, sgsn, now);
1461
1462 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001463
1464 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001465
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001466 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001467 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001468 return received;
1469}
1470
1471static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1472{
1473 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1474 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001475 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1476 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001477
1478 if (!(what & BSC_FD_READ))
1479 return 0;
1480
1481 struct gtphub *hub = from_sgsns_ofd->data;
1482
1483 static uint8_t buf[4096];
1484 struct osmo_sockaddr from_addr;
1485 struct osmo_sockaddr to_addr;
1486 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001487 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001488 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001489
1490 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1491 if (len < 1)
1492 return 0;
1493
1494 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1495 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001496 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001497 if (len < 1)
1498 return 0;
1499
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001500 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001501}
1502
1503/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1504int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1505 unsigned int plane_idx,
1506 const struct osmo_sockaddr *from_addr,
1507 uint8_t *buf,
1508 size_t received,
1509 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001510 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001511 struct osmo_fd **to_ofd,
1512 struct osmo_sockaddr *to_addr)
1513{
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001514 struct gtphub_bind *from_bind_arr = hub->to_sgsns;
1515 struct gtphub_bind *to_bind_arr = hub->to_ggsns;
1516 struct gtphub_bind *from_bind = &from_bind_arr[plane_idx];
1517 struct gtphub_bind *to_bind = &to_bind_arr[plane_idx];
1518
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001519 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001520 gtphub_plane_idx_names[plane_idx],
1521 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001522
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001523 static struct gtp_packet_desc p;
1524 gtp_decode(buf, received, plane_idx, &p);
1525
1526 if (p.rc <= 0)
1527 return -1;
1528
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001529 int reply_len;
1530 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1531 if (reply_len > 0) {
1532 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001533 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001534 *to_ofd = &from_bind->ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001535 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001536 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001537 if (reply_len < 0)
1538 return -1;
1539
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001540 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001541
1542 /* If an SGSN proxy is configured, check that it's indeed that proxy
1543 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1544 * gtphub, so no-one else is allowed to talk to us from that side. */
1545 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1546 if (sgsn) {
1547 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001548 LOG(LOGL_ERROR,
1549 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001550 " received on GGSN bind is from another sender:"
1551 " proxy: %s sender: %s\n",
1552 gtphub_port_str(sgsn),
1553 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001554 return -1;
1555 }
1556 }
1557
1558 if (!sgsn) {
1559 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001560 * entry for this SGSN. The port may differ. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001561 sgsn = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001562 }
1563
1564 if (!sgsn) {
1565 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1566 * first contact without being known yet, so create the peer
1567 * struct for the current sender. */
1568 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001569 LOG(LOGL_ERROR,
1570 "User plane peer was not announced by PDP Context,"
1571 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001572 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
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001581 sgsn = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001582 }
1583
1584 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001585 /* This could theoretically happen for invalid address data or
1586 * 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) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001615 /* This may be a Create PDP Context requst. If it is, there are
1616 * other addresses in the GTP message to set up apart from the
1617 * sender. */
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01001618 if (gtphub_handle_pdp_ctx_ies(hub, from_bind_arr, to_bind_arr,
1619 &p, now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001620 != 0)
1621 return -1;
1622 }
1623
1624 gtphub_check_restart_counter(hub, &p, sgsn);
1625 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1626
1627 /* If the SGSN is replying to a GGSN request, the sequence nr has
1628 * already been unmapped above (unmap_ggsn != NULL), and we need not
1629 * create a new outgoing sequence map. */
1630 if (!ggsn_from_seq)
1631 gtphub_map_seq(&p, sgsn, ggsn, now);
1632
1633 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1634
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001635 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001636
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001637 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001638 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001639 return received;
1640}
1641
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001642static void resolved_gssn_del_cb(struct expiring_item *expi)
1643{
1644 struct gtphub_resolved_ggsn *ggsn;
1645 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1646
1647 gtphub_port_ref_count_dec(ggsn->peer);
1648 llist_del(&ggsn->entry);
1649
1650 ggsn->expiry_entry.del_cb = 0;
1651 expiring_item_del(&ggsn->expiry_entry);
1652
1653 talloc_free(ggsn);
1654}
1655
1656void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1657 struct gsn_addr *resolved_addr,
1658 time_t now)
1659{
1660 struct gtphub_peer_port *pp;
1661 struct gtphub_resolved_ggsn *ggsn;
1662
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001663 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001664 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1665 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001666
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001667 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1668 resolved_addr, 2123);
1669 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001670 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1671 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001672 return;
1673 }
1674
1675 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1676 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001677 INIT_LLIST_HEAD(&ggsn->entry);
1678 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001679
1680 ggsn->peer = pp;
1681 gtphub_port_ref_count_inc(pp);
1682
1683 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001684 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001685
1686 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1687 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1688
1689 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1690}
1691
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001692static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1693{
1694 return pp->ref_count == 0;
1695}
1696
1697static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1698{
1699 struct gtphub_peer_port *pp, *npp;
1700 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1701 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001702 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001703 gtphub_port_str(pp));
1704 gtphub_peer_port_del(pp);
1705 }
1706 }
1707 return llist_empty(&pa->ports);
1708}
1709
1710static int gtphub_gc_peer(struct gtphub_peer *p)
1711{
1712 struct gtphub_peer_addr *pa, *npa;
1713 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1714 if (gtphub_gc_peer_addr(pa)) {
1715 gtphub_peer_addr_del(pa);
1716 }
1717 }
1718
1719 /* Note that there's a ref_count in each gtphub_peer_port instance
1720 * listed within p->addresses, referenced by TEI mappings from
1721 * hub->tei_map. As long as those don't expire, this peer will stay. */
1722
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001723 return llist_empty(&p->addresses)
1724 && nr_map_empty(&p->seq_map);
1725}
1726
1727static void gtphub_gc_bind(struct gtphub_bind *b)
1728{
1729 struct gtphub_peer *p, *n;
1730 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1731 if (gtphub_gc_peer(p)) {
1732 gtphub_peer_del(p);
1733 }
1734 }
1735}
1736
1737void gtphub_gc(struct gtphub *hub, time_t now)
1738{
1739 int expired;
1740 expired = expiry_tick(&hub->expire_seq_maps, now);
1741 expired += expiry_tick(&hub->expire_tei_maps, now);
1742
1743 /* ... */
1744
1745 if (expired) {
1746 int i;
1747 for (i = 0; i < GTPH_PLANE_N; i++) {
1748 gtphub_gc_bind(&hub->to_sgsns[i]);
1749 gtphub_gc_bind(&hub->to_ggsns[i]);
1750 }
1751 }
1752}
1753
1754static void gtphub_gc_cb(void *data)
1755{
1756 struct gtphub *hub = data;
1757 gtphub_gc(hub, gtphub_now());
1758 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1759}
1760
1761static void gtphub_gc_start(struct gtphub *hub)
1762{
1763 hub->gc_timer.cb = gtphub_gc_cb;
1764 hub->gc_timer.data = hub;
1765
1766 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1767}
1768
1769/* called by unit tests */
1770void gtphub_init(struct gtphub *hub)
1771{
1772 gtphub_zero(hub);
1773
1774 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001775 expiry_init(&hub->expire_tei_maps,
1776 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001777
1778 int plane_idx;
1779 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001780 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001781 nr_map_init(&hub->tei_map[plane_idx],
1782 &hub->tei_pool[plane_idx],
1783 &hub->expire_tei_maps);
1784
1785 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1786 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1787 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001788
1789 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1790 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1791 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1792 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001793}
1794
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01001795/* For the test suite, this is kept separate from gtphub_stop(), which also
1796 * closes sockets. The test suite avoids using sockets and would cause
1797 * segfaults when trying to close uninitialized ofds. */
1798void gtphub_free(struct gtphub *hub)
1799{
1800 /* By expiring all mappings, a garbage collection should free
1801 * everything else. A gtphub_bind_free() will assert that everything is
1802 * indeed empty. */
1803 expiry_clear(&hub->expire_seq_maps);
1804 expiry_clear(&hub->expire_tei_maps);
1805
1806 int plane_idx;
1807 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1808 gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
1809 gtphub_bind_free(&hub->to_ggsns[plane_idx]);
1810
1811 gtphub_gc_bind(&hub->to_sgsns[plane_idx]);
1812 gtphub_bind_free(&hub->to_sgsns[plane_idx]);
1813 }
1814}
1815
1816void gtphub_stop(struct gtphub *hub)
1817{
1818 int plane_idx;
1819 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1820 gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
1821 gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
1822 }
1823 gtphub_free(hub);
1824}
1825
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001826static int gtphub_make_proxy(struct gtphub *hub,
1827 struct gtphub_peer_port **pp,
1828 struct gtphub_bind *bind,
1829 const struct gtphub_cfg_addr *addr)
1830{
1831 if (!addr->addr_str)
1832 return 0;
1833
1834 struct gsn_addr gsna;
1835 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1836 return -1;
1837
1838 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1839
1840 /* This is *the* proxy. Make sure it is never expired. */
1841 gtphub_port_ref_count_inc(*pp);
1842 return 0;
1843}
1844
1845int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1846{
1847 int rc;
1848
1849 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001850
1851 /* If a Ctrl plane proxy is configured, ares will never be used. */
1852 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1853 if (gtphub_ares_init(hub) != 0) {
1854 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1855 return -1;
1856 }
1857 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001858
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001859 /* TODO set hub->restart_counter from external file. */
1860
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001861 int plane_idx;
1862 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1863 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1864 &cfg->to_ggsns[plane_idx],
1865 from_ggsns_read_cb, hub, plane_idx);
1866 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001867 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1868 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001869 return rc;
1870 }
1871
1872 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1873 &cfg->to_sgsns[plane_idx],
1874 from_sgsns_read_cb, hub, plane_idx);
1875 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001876 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1877 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001878 return rc;
1879 }
1880 }
1881
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001882 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1883 if (gtphub_make_proxy(hub,
1884 &hub->sgsn_proxy[plane_idx],
1885 &hub->to_sgsns[plane_idx],
1886 &cfg->sgsn_proxy[plane_idx])
1887 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001888 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001889 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001890 cfg->sgsn_proxy[plane_idx].addr_str,
1891 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001892 return -1;
1893 }
1894 if (gtphub_make_proxy(hub,
1895 &hub->ggsn_proxy[plane_idx],
1896 &hub->to_ggsns[plane_idx],
1897 &cfg->ggsn_proxy[plane_idx])
1898 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001899 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001900 return -1;
1901 }
1902 }
1903
1904 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1905 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001906 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001907 gtphub_plane_idx_names[plane_idx],
1908 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1909 }
1910
1911 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001912 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001913 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001914 gtphub_plane_idx_names[plane_idx],
1915 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1916 }
1917
1918 gtphub_gc_start(hub);
1919 return 0;
1920}
1921
1922static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1923 const struct gsn_addr *addr)
1924{
1925 struct gtphub_peer_addr *a;
1926 llist_for_each_entry(a, &peer->addresses, entry) {
1927 if (gsn_addr_same(&a->addr, addr))
1928 return a;
1929 }
1930 return NULL;
1931}
1932
1933static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1934 uint16_t port)
1935{
1936 OSMO_ASSERT(port);
1937 struct gtphub_peer_port *pp;
1938 llist_for_each_entry(pp, &a->ports, entry) {
1939 if (pp->port == port)
1940 return pp;
1941 }
1942 return NULL;
1943}
1944
1945static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1946 const struct gsn_addr *addr)
1947{
1948 struct gtphub_peer *peer;
1949 llist_for_each_entry(peer, &bind->peers, entry) {
1950 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
1951 if (a)
1952 return a;
1953 }
1954 return NULL;
1955}
1956
1957static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
1958 const struct gsn_addr *addr,
1959 uint16_t port)
1960{
1961 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1962 if (!a)
1963 return NULL;
1964 return gtphub_addr_find_port(a, port);
1965}
1966
1967struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
1968 const struct osmo_sockaddr *addr)
1969{
1970 struct gsn_addr gsna;
1971 uint16_t port;
1972 gsn_addr_from_sockaddr(&gsna, &port, addr);
1973 return gtphub_port_find(bind, &gsna, port);
1974}
1975
1976static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
1977 struct gtphub_bind *bind)
1978{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001979 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
1980 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001981 OSMO_ASSERT(peer);
1982
1983 INIT_LLIST_HEAD(&peer->addresses);
1984
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001985 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
1987
1988 /* TODO use something random to pick the initial sequence nr.
1989 0x6d31 produces the ASCII character sequence 'm1', currently used in
1990 gtphub_nc_test.sh. */
1991 peer->seq_pool.last_nr = 0x6d31 - 1;
1992
1993 llist_add(&peer->entry, &bind->peers);
1994 return peer;
1995}
1996
1997static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
1998 const struct gsn_addr *addr)
1999{
2000 struct gtphub_peer_addr *a;
2001 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2002 OSMO_ASSERT(a);
2003 a->peer = peer;
2004 gsn_addr_copy(&a->addr, addr);
2005 INIT_LLIST_HEAD(&a->ports);
2006 llist_add(&a->entry, &peer->addresses);
2007
2008 return a;
2009}
2010
2011static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2012 struct gtphub_bind *bind,
2013 const struct gsn_addr *addr)
2014{
2015 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2016 if (a)
2017 return a;
2018
2019 /* If we haven't found an address, that means we need to create an
2020 * entirely new peer for the new address. More addresses may be added
2021 * to this peer later, but not via this function. */
2022 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002023
2024 a = gtphub_peer_add_addr(peer, addr);
2025
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002026 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002027 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002028 gsn_addr_to_str(&a->addr));
2029
2030 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002031}
2032
2033static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2034 uint16_t port)
2035{
2036 struct gtphub_peer_port *pp;
2037
2038 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2039 OSMO_ASSERT(pp);
2040 pp->peer_addr = a;
2041 pp->port = port;
2042
2043 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2044 talloc_free(pp);
2045 return NULL;
2046 }
2047
2048 llist_add(&pp->entry, &a->ports);
2049
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002050 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002051 gsn_addr_to_str(&a->addr),
2052 (int)port);
2053
2054 return pp;
2055}
2056
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002057struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2058 struct gtphub_bind *bind,
2059 const struct gsn_addr *addr,
2060 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002061{
2062 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2063
2064 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2065 if (pp)
2066 return pp;
2067
2068 return gtphub_addr_add_port(a, port);
2069}
2070
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002071/* Find a GGSN peer with a matching address. If the address is known but the
2072 * port not, create a new port for that peer address. */
2073struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2074 const struct osmo_sockaddr *addr)
2075{
2076 struct gtphub_peer_addr *pa;
2077 struct gtphub_peer_port *pp;
2078
2079 struct gsn_addr gsna;
2080 uint16_t port;
2081 gsn_addr_from_sockaddr(&gsna, &port, addr);
2082
2083 pa = gtphub_addr_find(bind, &gsna);
2084 if (!pa)
2085 return NULL;
2086
2087 pp = gtphub_addr_find_port(pa, port);
2088
2089 if (!pp)
2090 pp = gtphub_addr_add_port(pa, port);
2091
2092 return pp;
2093}
2094
2095
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002096/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2097 * resolution should be possible but failed, and 1 if resolution was
2098 * successful. *pp will be set to NULL if <1 is returned. */
2099static int gtphub_resolve_ggsn(struct gtphub *hub,
2100 struct gtp_packet_desc *p,
2101 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002102{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002103 *pp = NULL;
2104
2105 /* TODO determine from message type whether IEs should be present? */
2106
2107 int rc;
2108 const char *imsi_str;
2109 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2110 if (rc < 1)
2111 return rc;
2112 OSMO_ASSERT(imsi_str);
2113
2114 const char *apn_str;
2115 rc = get_ie_apn_str(p->ie, &apn_str);
2116 if (rc < 1)
2117 return rc;
2118 OSMO_ASSERT(apn_str);
2119
2120 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2121 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002122}
2123
2124
2125/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002126/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002127/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2128 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002129static int _osmo_getaddrinfo(struct addrinfo **result,
2130 uint16_t family, uint16_t type, uint8_t proto,
2131 const char *host, uint16_t port)
2132{
2133 struct addrinfo hints;
2134 char portbuf[16];
2135
2136 sprintf(portbuf, "%u", port);
2137 memset(&hints, '\0', sizeof(struct addrinfo));
2138 hints.ai_family = family;
2139 if (type == SOCK_RAW) {
2140 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2141 * SOCK_RAW and IPPROTO_GRE is used.
2142 */
2143 hints.ai_socktype = SOCK_DGRAM;
2144 hints.ai_protocol = IPPROTO_UDP;
2145 } else {
2146 hints.ai_socktype = type;
2147 hints.ai_protocol = proto;
2148 }
2149
2150 return getaddrinfo(host, portbuf, &hints, result);
2151}
2152
2153/* TODO move to osmocom/core/socket.c ? */
2154int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2155 uint16_t family, uint16_t type, uint8_t proto,
2156 const char *host, uint16_t port)
2157{
2158 struct addrinfo *res;
2159 int rc;
2160 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2161
2162 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002163 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002164 return -EINVAL;
2165 }
2166
2167 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2168 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2169 addr->l = res->ai_addrlen;
2170 freeaddrinfo(res);
2171
2172 return 0;
2173}
2174
2175int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2176 char *port_str, size_t port_str_len,
2177 const struct osmo_sockaddr *addr,
2178 int flags)
2179{
2180 int rc;
2181
2182 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2183 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2184 return -1;
2185 }
2186
2187 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002188 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2189 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002190 return -1;
2191 }
2192
2193 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2194 addr_str, addr_str_len,
2195 port_str, port_str_len,
2196 flags);
2197
2198 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002199 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2200 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2201 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002202
2203 return rc;
2204}
2205
2206const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2207 char *buf, size_t buf_len)
2208{
2209 const int portbuf_len = 6;
2210 OSMO_ASSERT(buf_len > portbuf_len);
2211 char *portbuf = buf + buf_len - portbuf_len;
2212 buf_len -= portbuf_len;
2213 if (osmo_sockaddr_to_strs(buf, buf_len,
2214 portbuf, portbuf_len,
2215 addr,
2216 NI_NUMERICHOST | NI_NUMERICSERV))
2217 return NULL;
2218
2219 char *pos = buf + strnlen(buf, buf_len-1);
2220 size_t len = buf_len - (pos - buf);
2221
2222 snprintf(pos, len, " port %s", portbuf);
2223 buf[buf_len-1] = '\0';
2224
2225 return buf;
2226}
2227
2228const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2229{
2230 static char buf[256];
2231 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2232 if (! result)
2233 return "(invalid)";
2234 return result;
2235}
2236
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002237int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2238 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002239{
2240 if (a == b)
2241 return 0;
2242 if (!a)
2243 return -1;
2244 if (!b)
2245 return 1;
2246 if (a->l != b->l) {
2247 /* Lengths are not the same, but determine the order. Will
2248 * anyone ever sort a list by osmo_sockaddr though...? */
2249 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2250 if (cmp == 0) {
2251 if (a->l < b->l)
2252 return -1;
2253 else
2254 return 1;
2255 }
2256 return cmp;
2257 }
2258 return memcmp(&a->a, &b->a, a->l);
2259}
2260
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002261void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2262 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002263{
2264 OSMO_ASSERT(src->l <= sizeof(dst->a));
2265 memcpy(&dst->a, &src->a, src->l);
2266 dst->l = src->l;
2267}