blob: 22da9a303788ab0821f4a64814fc7f32fc9a86ed [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <string.h>
23#include <errno.h>
24#include <inttypes.h>
25#include <time.h>
26#include <limits.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#include <gtp.h>
32#include <gtpie.h>
33
34#include <openbsc/gtphub.h>
35#include <openbsc/debug.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010036#include <openbsc/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020037
38#include <osmocom/core/utils.h>
39#include <osmocom/core/logging.h>
40#include <osmocom/core/socket.h>
41
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010042
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020043static const int GTPH_GC_TICK_SECONDS = 1;
44
45void *osmo_gtphub_ctx;
46
Neels Hofmeyr063a8022015-11-16 14:35:13 +010047/* Convenience makro, note: only within this C file. */
48#define LOG(level, fmt, args...) \
49 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020050
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010051#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
52 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020053
54/* TODO move this to osmocom/core/select.h ? */
55typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
56
57/* TODO move this to osmocom/core/linuxlist.h ? */
58#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010059#define llist_first(head, type, entry) \
60 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020061
62/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
63
64enum gtp_rc {
65 GTP_RC_UNKNOWN = 0,
66 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010067 GTP_RC_PDU_C = 2, /* a real packet with IEs */
68 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069
70 GTP_RC_TOOSHORT = -1,
71 GTP_RC_UNSUPPORTED_VERSION = -2,
72 GTP_RC_INVALID_IE = -3,
73};
74
75struct gtp_packet_desc {
76 union gtp_packet *data;
77 int data_len;
78 int header_len;
79 int version;
80 uint8_t type;
81 uint16_t seq;
82 uint32_t header_tei;
83 int rc; /* enum gtp_rc */
84 unsigned int plane_idx;
85 union gtpie_member *ie[GTPIE_SIZE];
86};
87
88void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
89{
90 memcpy(gsna, src, sizeof(struct gsn_addr));
91}
92
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020093int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
94 const struct osmo_sockaddr *sa)
95{
96 char addr_str[256];
97 char port_str[6];
98
99 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
100 port_str, sizeof(port_str),
101 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
102 != 0) {
103 return -1;
104 }
105
106 if (port)
107 *port = atoi(port_str);
108
109 return gsn_addr_from_str(gsna, addr_str);
110}
111
112int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
113{
114 int af = AF_INET;
115 gsna->len = 4;
116 const char *pos = numeric_addr_str;
117 for (; *pos; pos++) {
118 if (*pos == ':') {
119 af = AF_INET6;
120 gsna->len = 16;
121 break;
122 }
123 }
124
125 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
126 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100127 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
128 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200129 return -1;
130 }
131 return 0;
132}
133
134const char *gsn_addr_to_str(const struct gsn_addr *gsna)
135{
136 static char buf[INET6_ADDRSTRLEN + 1];
137 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
138}
139
140const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
141 char *strbuf,
142 int strbuf_len)
143{
144 int af;
145 switch (gsna->len) {
146 case 4:
147 af = AF_INET;
148 break;
149 case 16:
150 af = AF_INET6;
151 break;
152 default:
153 return NULL;
154 }
155
156 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
157 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100158 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
159 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100160 strerror(errno),
161 (int)gsna->len,
162 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200163 }
164 return r;
165}
166
167int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
168{
169 if (a == b)
170 return 1;
171 if ((!a) || (!b))
172 return 0;
173 if (a->len != b->len)
174 return 0;
175 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
176}
177
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100178static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
179 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200180{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100181 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200182 return -1;
183
184 unsigned int len;
185 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
186 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
187 &len, gsna->buf, sizeof(gsna->buf))
188 != 0)
189 return -1;
190 gsna->len = len;
191 return 0;
192}
193
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100194static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
195 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200196{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100197 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200198 return -1;
199
200 int ie_idx;
201 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
202
203 if (ie_idx < 0)
204 return -1;
205
206 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
207 int ie_l = ntoh16(ie->l);
208 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100209 LOG(LOGL_ERROR, "Not implemented:"
210 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200211 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
212 return -1;
213 }
214
215 memcpy(ie->v, gsna->buf, (int)ie_l);
216 return 0;
217}
218
219/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
220 */
221void validate_gtp0_header(struct gtp_packet_desc *p)
222{
223 const struct gtp0_header *pheader = &(p->data->gtp0.h);
224 p->rc = GTP_RC_UNKNOWN;
225 p->header_len = 0;
226
227 OSMO_ASSERT(p->data_len >= 1);
228 OSMO_ASSERT(p->version == 0);
229
230 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100231 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200232 p->rc = GTP_RC_TOOSHORT;
233 return;
234 }
235
236 p->type = ntoh8(pheader->type);
237 p->seq = ntoh16(pheader->seq);
238 p->header_tei = 0; /* TODO */
239
240 if (p->data_len == GTP0_HEADER_SIZE) {
241 p->rc = GTP_RC_TINY;
242 p->header_len = GTP0_HEADER_SIZE;
243 return;
244 }
245
246 /* Check packet length field versus length of packet */
247 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100248 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
249 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100250 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
251 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200252 p->rc = GTP_RC_TOOSHORT;
253 return;
254 }
255
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100256 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200257 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100258 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259}
260
261/* Validate GTP version 1 data, and update p->rc with the result, as well as
262 * p->header_len in case of a valid header. */
263void validate_gtp1_header(struct gtp_packet_desc *p)
264{
265 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
266 p->rc = GTP_RC_UNKNOWN;
267 p->header_len = 0;
268
269 OSMO_ASSERT(p->data_len >= 1);
270 OSMO_ASSERT(p->version == 1);
271
272 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
273 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100274 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200275 p->rc = GTP_RC_TOOSHORT;
276 return;
277 }
278
279 p->type = ntoh8(pheader->type);
280 p->header_tei = ntoh32(pheader->tei);
281 p->seq = ntoh16(pheader->seq);
282
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100283 LOG(LOGL_DEBUG, "GTPv1\n"
284 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
285 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
286 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
287 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
288 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
289 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
290 p->type, p->type,
291 ntoh16(pheader->length), ntoh16(pheader->length),
292 p->header_tei, p->header_tei,
293 p->seq, p->seq,
294 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200295 pheader->next, pheader->next);
296
297 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
298 p->rc = GTP_RC_TINY;
299 p->header_len = GTP1_HEADER_SIZE_SHORT;
300 return;
301 }
302
303 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100304 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
305 if (p->data_len != announced_len) {
306 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
307 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100308 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
309 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200310 p->rc = GTP_RC_TOOSHORT;
311 return;
312 }
313
Neels Hofmeyre921e322015-11-11 00:45:50 +0100314 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200315 p->header_len = GTP1_HEADER_SIZE_LONG;
316}
317
318/* Examine whether p->data of size p->data_len has a valid GTP header. Set
319 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
320 * gtp_rc). p->data must point at a buffer with p->data_len set. */
321void validate_gtp_header(struct gtp_packet_desc *p)
322{
323 p->rc = GTP_RC_UNKNOWN;
324
325 /* Need at least 1 byte in order to check version */
326 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100327 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
328 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200329 p->rc = GTP_RC_TOOSHORT;
330 return;
331 }
332
333 p->version = p->data->flags >> 5;
334
335 switch (p->version) {
336 case 0:
337 validate_gtp0_header(p);
338 break;
339 case 1:
340 validate_gtp1_header(p);
341 break;
342 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100343 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200344 p->rc = GTP_RC_UNSUPPORTED_VERSION;
345 break;
346 }
347}
348
349
350/* Return the value of the i'th IMSI IEI by copying to *imsi.
351 * The first IEI is reached by passing i = 0.
352 * imsi must point at allocated space of (at least) 8 bytes.
353 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100354static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200355{
356 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
357}
358
359/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100360static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200361{
362 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
363}
364
365static char imsi_digit_to_char(uint8_t nibble)
366{
367 nibble &= 0x0f;
368 if (nibble > 9)
369 return (nibble == 0x0f) ? '\0' : '?';
370 return '0' + nibble;
371}
372
373/* Return a human readable IMSI string, in a static buffer.
374 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100375static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200376{
377 static char str[17];
378 int i;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100379 char c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200380
381 for (i = 0; i < 8; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100382 c = imsi_digit_to_char(imsi[i]);
383 if (c == '?')
384 return -1;
385 str[2*i] = c;
386
387 c = imsi_digit_to_char(imsi[i] >> 4);
388 if (c == '?')
389 return -1;
390 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200391 }
392 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100393 *imsi_str = str;
394 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200395}
396
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100397/* Return 0 if not present, 1 if present and decoded successfully, -1 if
398 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100399static int get_ie_imsi_str(union gtpie_member *ie[], int i,
400 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100401{
402 uint8_t imsi_buf[8];
403 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100404 return 0;
405 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100406}
407
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100408/* Return 0 if not present, 1 if present and decoded successfully, -1 if
409 * present but cannot be decoded. */
410static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100411{
412 static char apn_buf[GSM_APN_LENGTH];
413 unsigned int len;
414 if (gtpie_gettlv(ie, GTPIE_APN, 0,
415 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100416 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100417
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100418 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100419 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
420 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100421 return -1;
422 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100423
424 if (len > (sizeof(apn_buf) - 1))
425 len = sizeof(apn_buf) - 1;
426 apn_buf[len] = '\0';
427
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100428 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
429 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100430 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
431 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100432 return -1;
433 }
434 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100435}
436
437
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200438/* Validate header, and index information elements. Write decoded packet
439 * information to *res. res->data will point at the given data buffer. On
440 * error, p->rc is set <= 0 (see enum gtp_rc). */
441static void gtp_decode(const uint8_t *data, int data_len,
442 unsigned int from_plane_idx,
443 struct gtp_packet_desc *res)
444{
445 ZERO_STRUCT(res);
446 res->data = (union gtp_packet*)data;
447 res->data_len = data_len;
448 res->plane_idx = from_plane_idx;
449
450 validate_gtp_header(res);
451
452 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100453 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200454 return;
455 }
456
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100457 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200458
Neels Hofmeyre921e322015-11-11 00:45:50 +0100459 if (from_plane_idx == GTPH_PLANE_USER) {
460 res->rc = GTP_RC_PDU_U;
461 return;
462 }
463
464 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100465 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200466 return;
467 }
468
469 if (gtpie_decaps(res->ie, res->version,
470 (void*)(data + res->header_len),
471 res->data_len - res->header_len) != 0) {
472 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100473 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
474 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200475 return;
476 }
477
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100478#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100479 /* TODO if (<loglevel is debug>) { ...
480 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200481 int i;
482
483 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100484 const char *imsi;
485 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200486 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100487 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200488 }
489
490 for (i = 0; i < 10; i++) {
491 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100492 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200493 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100494 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200495 }
496
497 for (i = 0; i < 2; i++) {
498 struct gsn_addr addr;
499 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100500 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200501 }
502
503 for (i = 0; i < 10; i++) {
504 uint32_t tei;
505 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
506 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100507 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508 tei, tei);
509 }
510
511 for (i = 0; i < 10; i++) {
512 uint32_t tei;
513 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
514 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100515 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200516 tei, tei);
517 }
518#endif
519}
520
521
522/* expiry */
523
524void expiry_init(struct expiry *exq, int expiry_in_seconds)
525{
526 ZERO_STRUCT(exq);
527 exq->expiry_in_seconds = expiry_in_seconds;
528 INIT_LLIST_HEAD(&exq->items);
529}
530
531void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
532{
533 item->expiry = now + exq->expiry_in_seconds;
534
535 /* Add/move to the tail to always sort by expiry, ascending. */
536 llist_del(&item->entry);
537 llist_add_tail(&item->entry, &exq->items);
538}
539
540int expiry_tick(struct expiry *exq, time_t now)
541{
542 int expired = 0;
543 struct expiring_item *m, *n;
544 llist_for_each_entry_safe(m, n, &exq->items, entry) {
545 if (m->expiry <= now) {
546 expiring_item_del(m);
547 expired ++;
548 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200549 /* The items are added sorted by expiry. So when we hit
550 * an unexpired entry, only more unexpired ones will
551 * follow. */
552 break;
553 }
554 }
555 return expired;
556}
557
558void expiring_item_init(struct expiring_item *item)
559{
560 ZERO_STRUCT(item);
561 INIT_LLIST_HEAD(&item->entry);
562}
563
564void expiring_item_del(struct expiring_item *item)
565{
566 OSMO_ASSERT(item);
567 llist_del(&item->entry);
568 INIT_LLIST_HEAD(&item->entry);
569 if (item->del_cb) {
570 /* avoid loops */
571 del_cb_t del_cb = item->del_cb;
572 item->del_cb = 0;
573 (del_cb)(item);
574 }
575}
576
577
578/* nr_map, nr_pool */
579
580void nr_pool_init(struct nr_pool *pool)
581{
582 *pool = (struct nr_pool){};
583}
584
585nr_t nr_pool_next(struct nr_pool *pool)
586{
587 pool->last_nr ++;
588
589 OSMO_ASSERT(pool->last_nr > 0);
590 /* TODO: gracefully handle running out of TEIs. */
591 /* TODO: random TEIs. */
592
593 return pool->last_nr;
594}
595
596void nr_map_init(struct nr_map *map, struct nr_pool *pool,
597 struct expiry *exq)
598{
599 ZERO_STRUCT(map);
600 map->pool = pool;
601 map->add_items_to_expiry = exq;
602 INIT_LLIST_HEAD(&map->mappings);
603}
604
605void nr_mapping_init(struct nr_mapping *m)
606{
607 ZERO_STRUCT(m);
608 INIT_LLIST_HEAD(&m->entry);
609 expiring_item_init(&m->expiry_entry);
610}
611
612void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
613{
614 /* Generate a mapped number */
615 mapping->repl = nr_pool_next(map->pool);
616
617 /* Add to the tail to always yield a list sorted by expiry, in
618 * ascending order. */
619 llist_add_tail(&mapping->entry, &map->mappings);
620 if (map->add_items_to_expiry)
621 expiry_add(map->add_items_to_expiry,
622 &mapping->expiry_entry,
623 now);
624}
625
626void nr_map_clear(struct nr_map *map)
627{
628 struct nr_mapping *m;
629 struct nr_mapping *n;
630 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
631 nr_mapping_del(m);
632 }
633}
634
635int nr_map_empty(const struct nr_map *map)
636{
637 return llist_empty(&map->mappings);
638}
639
640struct nr_mapping *nr_map_get(const struct nr_map *map,
641 void *origin, nr_t nr_orig)
642{
643 struct nr_mapping *mapping;
644 llist_for_each_entry(mapping, &map->mappings, entry) {
645 if ((mapping->origin == origin)
646 && (mapping->orig == nr_orig))
647 return mapping;
648 }
649 /* Not found. */
650 return NULL;
651}
652
653struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
654{
655 struct nr_mapping *mapping;
656 llist_for_each_entry(mapping, &map->mappings, entry) {
657 if (mapping->repl == nr_repl) {
658 return mapping;
659 }
660 }
661 /* Not found. */
662 return NULL;
663}
664
665void nr_mapping_del(struct nr_mapping *mapping)
666{
667 OSMO_ASSERT(mapping);
668 llist_del(&mapping->entry);
669 INIT_LLIST_HEAD(&mapping->entry);
670 expiring_item_del(&mapping->expiry_entry);
671}
672
673
674/* gtphub */
675
676const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
677 "CTRL",
678 "USER",
679};
680
681const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
682 2123,
683 2152,
684};
685
686time_t gtphub_now(void)
687{
688 struct timespec now_tp;
689 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
690 return now_tp.tv_sec;
691}
692
693/* Remove a gtphub_peer from its list and free it. */
694static void gtphub_peer_del(struct gtphub_peer *peer)
695{
696 nr_map_clear(&peer->seq_map);
697 llist_del(&peer->entry);
698 talloc_free(peer);
699}
700
701static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
702{
703 OSMO_ASSERT(llist_empty(&pa->ports));
704 llist_del(&pa->entry);
705 talloc_free(pa);
706}
707
708static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
709{
710 OSMO_ASSERT(pp->ref_count == 0);
711 llist_del(&pp->entry);
712 talloc_free(pp);
713}
714
715/* From the information in the gtp_packet_desc, return the address of a GGSN.
716 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100717static int gtphub_resolve_ggsn(struct gtphub *hub,
718 struct gtp_packet_desc *p,
719 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200720
721/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100722struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
723 const char *imsi_str,
724 const char *apn_ni_str);
725int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200726
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200727static void gtphub_zero(struct gtphub *hub)
728{
729 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100730 INIT_LLIST_HEAD(&hub->ggsn_lookups);
731 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200732}
733
734static int gtphub_sock_init(struct osmo_fd *ofd,
735 const struct gtphub_cfg_addr *addr,
736 osmo_fd_cb_t cb,
737 void *data,
738 int ofd_id)
739{
740 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100741 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200742 return -1;
743 }
744 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100745 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200746 return -1;
747 }
748
749 ofd->when = BSC_FD_READ;
750 ofd->cb = cb;
751 ofd->data = data;
752 ofd->priv_nr = ofd_id;
753
754 int rc;
755 rc = osmo_sock_init_ofd(ofd,
756 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
757 addr->addr_str, addr->port,
758 OSMO_SOCK_F_BIND);
759 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100760 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
761 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200762 return -1;
763 }
764
765 return 0;
766}
767
768static void gtphub_bind_init(struct gtphub_bind *b)
769{
770 ZERO_STRUCT(b);
771
772 INIT_LLIST_HEAD(&b->peers);
773}
774
775static int gtphub_bind_start(struct gtphub_bind *b,
776 const struct gtphub_cfg_bind *cfg,
777 osmo_fd_cb_t cb, void *cb_data,
778 unsigned int ofd_id)
779{
780 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
781 return -1;
782 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
783 return -1;
784 return 0;
785}
786
Neels Hofmeyr40348972015-11-17 12:22:28 +0100787/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200788 * Return the number of bytes read, zero on error. */
789static int gtphub_read(const struct osmo_fd *from,
790 struct osmo_sockaddr *from_addr,
791 uint8_t *buf, size_t buf_len)
792{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100793 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200794
Neels Hofmeyr40348972015-11-17 12:22:28 +0100795 /* recvfrom requires the available length set in *from_addr_len. */
796 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200797 errno = 0;
798 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100799 (struct sockaddr*)&from_addr->a,
800 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200801 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
802 * is not truncated. Then maybe reduce buf's size. */
803
804 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100805 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
806 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200807 return 0;
808 }
809
Neels Hofmeyr40348972015-11-17 12:22:28 +0100810 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
811 (int)received, osmo_sockaddr_to_str(from_addr),
812 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200813
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200814 return received;
815}
816
817inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
818{
819 OSMO_ASSERT(pp->ref_count < UINT_MAX);
820 pp->ref_count++;
821}
822
823inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
824{
825 OSMO_ASSERT(pp->ref_count > 0);
826 pp->ref_count--;
827}
828
829inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
830{
831 OSMO_ASSERT(p->version == 1);
832 p->data->gtp1l.h.seq = hton16(seq);
833 p->seq = seq;
834}
835
836inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
837{
838 OSMO_ASSERT(p->version == 1);
839 p->data->gtp1l.h.tei = hton32(tei);
840 p->header_tei = tei;
841}
842
843static void gtphub_mapping_del_cb(struct expiring_item *expi);
844
845static struct nr_mapping *gtphub_mapping_new()
846{
847 struct nr_mapping *nrm;
848 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
849 OSMO_ASSERT(nrm);
850
851 nr_mapping_init(nrm);
852 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
853 return nrm;
854}
855
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100856static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
857 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200858{
859 if (llist_empty(&peer->addresses))
860 return "(addressless)";
861
862 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
863 struct gtphub_peer_addr,
864 entry);
865 return gsn_addr_to_strb(&a->addr, buf, buflen);
866}
867
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100868static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
869 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200870{
871 if (!port)
872 return "(null port)";
873
874 snprintf(buf, buflen, "%s port %d",
875 gsn_addr_to_str(&port->peer_addr->addr),
876 (int)port->port);
877 return buf;
878}
879
880const char *gtphub_peer_str(struct gtphub_peer *peer)
881{
882 static char buf[256];
883 return gtphub_peer_strb(peer, buf, sizeof(buf));
884}
885
886const char *gtphub_peer_str2(struct gtphub_peer *peer)
887{
888 static char buf[256];
889 return gtphub_peer_strb(peer, buf, sizeof(buf));
890}
891
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100892const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200893{
894 static char buf[256];
895 return gtphub_port_strb(port, buf, sizeof(buf));
896}
897
898static const char *gtphub_port_str2(struct gtphub_peer_port *port)
899{
900 static char buf[256];
901 return gtphub_port_strb(port, buf, sizeof(buf));
902}
903
904static void gtphub_mapping_del_cb(struct expiring_item *expi)
905{
906 expi->del_cb = 0; /* avoid recursion loops */
907
908 struct nr_mapping *nrm = container_of(expi,
909 struct nr_mapping,
910 expiry_entry);
911 llist_del(&nrm->entry);
912 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
913
914 /* Just for log */
915 struct gtphub_peer_port *from = nrm->origin;
916 OSMO_ASSERT(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100917 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %d->%d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200918 (int)nrm->expiry_entry.expiry,
919 gtphub_port_str(from),
920 (int)nrm->orig, (int)nrm->repl);
921
922 gtphub_port_ref_count_dec(from);
923
924 talloc_free(nrm);
925}
926
927static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
928 struct gtphub_peer_port *from,
929 nr_t orig_nr,
930 time_t now)
931{
932 struct nr_mapping *nrm;
933
934 nrm = nr_map_get(map, from, orig_nr);
935
936 if (!nrm) {
937 nrm = gtphub_mapping_new();
938 nrm->orig = orig_nr;
939 nrm->origin = from;
940 nr_map_add(map, nrm, now);
941 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100942 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200943 gtphub_port_str(from),
944 (int)(nrm->orig), (int)(nrm->repl));
945 } else {
946 /* restart expiry timeout */
947 expiry_add(map->add_items_to_expiry, &nrm->expiry_entry,
948 now);
949 }
950
951 OSMO_ASSERT(nrm);
952 return nrm;
953}
954
955static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
956 int plane_idx,
957 struct gtphub_peer_port *from,
958 uint32_t orig_tei,
959 time_t now)
960{
961 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
962 from, orig_tei, now);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100963 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %d) <-- TEI %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200964 gtphub_plane_idx_names[plane_idx],
965 gtphub_port_str(from),
966 (int)orig_tei, (int)nrm->repl);
967
968 return (uint32_t)nrm->repl;
969}
970
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100971static void gtphub_map_seq(struct gtp_packet_desc *p,
972 struct gtphub_peer_port *from_port,
973 struct gtphub_peer_port *to_port,
974 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200975{
976 /* Store a mapping in to_peer's map, so when we later receive a GTP
977 * packet back from to_peer, the seq nr can be unmapped back to its
978 * origin (from_peer here). */
979 struct nr_mapping *nrm;
980 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
981 from_port, p->seq, now);
982
983 /* Change the GTP packet to yield the new, mapped seq nr */
984 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200985}
986
987static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
988 struct gtphub_peer_port *responding_port)
989{
990 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100991 struct nr_mapping *nrm =
992 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
993 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200994 if (!nrm)
995 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100996 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
997 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200998 set_seq(p, nrm->orig);
999 return nrm->origin;
1000}
1001
1002static void gtphub_check_restart_counter(struct gtphub *hub,
1003 struct gtp_packet_desc *p,
1004 struct gtphub_peer_port *from)
1005{
1006 /* TODO */
1007 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1008 * that doesn't match the peer's previously sent restart counter, clear
1009 * that peer and cancel PDP contexts. */
1010}
1011
1012static void gtphub_map_restart_counter(struct gtphub *hub,
1013 struct gtp_packet_desc *p,
1014 struct gtphub_peer_port *from,
1015 struct gtphub_peer_port *to)
1016{
1017 /* TODO */
1018}
1019
1020/* gtphub_map_ie_teis() and gtphub_unmap_header_tei():
1021 *
1022 * TEI mapping must happen symmetrically. An SGSN contacts gtphub instead of N
1023 * GGSNs, and a GGSN replies to gtphub for N SGSNs. From either end, TEIs may
1024 * collide: two GGSNs picking the same TEIs, or two SGSNs picking the same
1025 * TEIs. Since the opposite side sees the sender address being gtphub's
1026 * address, TEIs among the SGSNs, and among the GGSNs, must not overlap. If a
1027 * peer sends a TEI already sent before from a peer of the same side, gtphub
1028 * replaces it with a TEI not yet seen from that side and remembers the
1029 * mapping.
1030 *
1031 * Consider two SGSNs A and B contacting two GGSNs C and D thru gtphub.
1032 *
1033 * A: Create PDP Ctx, I have TEI 1.
1034 * ---> gtphub: A has TEI 1, sending 1 for C.
1035 * ---> C: gtphub has TEI 1.
1036 * <--- C: Response to TEI 1: I have TEI 11.
1037 * <--- gtphub: ok, telling A: 11.
1038 * A: gtphub's first TEI is 11. (1)
1039 *
1040 * B: Create PDP Ctx, I have TEIs 1.
1041 * ---> gtphub: 1 already taken for C, sending 2 for B. (map)
1042 * ---> C: gtphub also has 2.
1043 * <--- C: Response to TEI 2: I have TEI 12.
1044 * <--- gtphub: ok, TEI 2 is actually B with TEI 1. (unmap)
1045 * B: gtphub's first TEI is 12, as far as I can tell.
1046 *
1047 * Now the second GGSN comes into play:
1048 *
1049 * A: Create PDP Ctx, I have TEI 2.
1050 * ---> gtphub: A also has TEI 2, but for D, sending 1. (2)
1051 * ---> D: gtphub has 1.
1052 * <--- D: Response to TEI 1: I have TEI 11.
1053 * <--- gtphub: from D, 1 is A. 11 already taken by C, sending 13. (3)
1054 * A: gtphub also has TEI 13. (4)
1055 *
1056 * And some messages routed through:
1057 *
1058 * A: message to TEI 11, see (1).
1059 * ---> gtphub: ok, telling C with TEI 11.
1060 * ---> C: I see, 11 means reply with 1.
1061 * <--- C: Response to TEI 1
1062 * <--- gtphub: 1 from C is actually for A with TEI 1.
1063 * A: ah, my TEI 1, thanks!
1064 *
1065 * A: message to TEI 13, see (4).
1066 * ---> gtphub: ok, but not 13, D wanted TEI 11 instead, see (3).
1067 * ---> D: I see, 11 means reply with 1.
1068 * <--- D: Response to TEI 1
1069 * <--- gtphub: 1 from D is actually for A with TEI 2, see (2).
1070 * A: ah, my TEI 2, thanks!
1071 *
1072 * What if a GGSN initiates a request:
1073 *
1074 * <--- D: Request to gtphub TEI 1
1075 * <--- gtphub: 1 from D is for A with 2, see (2).
1076 * A: my TEI 2 means reply with 13.
1077 * ---> gtphub: 13 was D with 11, see (3).
1078 * ---> D: 11 from gtphub: a reply to my request for TEI 1.
1079 *
1080 * Note that usually, it's the sequence numbers that route a response back to
1081 * the requesting peer. Nevertheless, the TEI mappings must be carried out to
1082 * replace the TEIs in the GTP packet that is relayed.
1083 *
1084 * Also note: the TEI in the GTP header is "reversed" from the TEI in the IEs:
1085 * the TEI in the header is used to send something *to* a peer, while the TEI
1086 * in e.g. a Create PDP Context Request's IE is for routing messages *back*
1087 * later. */
1088
1089static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1090 struct gtphub *hub,
1091 struct gtp_packet_desc *p,
1092 struct gtphub_peer_port *from_port)
1093{
1094 OSMO_ASSERT(p->version == 1);
1095 *to_port_p = NULL;
1096
1097 /* If the header's TEI is zero, no PDP context has been established
1098 * yet. If nonzero, a mapping should actually already exist for this
1099 * TEI, since it must have been announced in a PDP context creation. */
1100 uint32_t tei = p->header_tei;
1101 if (!tei)
1102 return 0;
1103
1104 /* to_peer has previously announced a TEI, which was stored and
1105 * mapped in from_peer's tei_map. */
1106 struct nr_mapping *nrm;
1107 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1108 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001109 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1110 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001111 return -1;
1112 }
1113
1114 struct gtphub_peer_port *to_port = nrm->origin;
1115 uint32_t unmapped_tei = nrm->orig;
1116 set_tei(p, unmapped_tei);
1117
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001118 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %d -> %d (to %s)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001119 gtphub_port_str(from_port), tei, unmapped_tei,
1120 gtphub_port_str2(to_port));
1121
1122 *to_port_p = to_port;
1123 return 0;
1124}
1125
1126/* Read GSN address IEs from p, and make sure these peer addresses exist in
1127 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1128 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1129 * the packet p. */
1130static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
1131 struct gtphub_bind from_bind[],
1132 struct gtphub_bind to_bind[],
1133 struct gtp_packet_desc *p,
1134 time_t now)
1135{
1136 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1137
1138 int rc;
1139 int plane_idx;
1140
1141 switch (p->type) {
1142 case GTP_CREATE_PDP_REQ:
1143 case GTP_CREATE_PDP_RSP:
1144 /* Go for it below */
1145 break;
1146 default:
1147 /* Nothing to do for this message type. */
1148 return 0;
1149 }
1150
1151 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1152
1153 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1154 plane_nrs_match_GSN_addr_IE_indices);
1155
1156 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1157 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1158
1159 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1160 struct gsn_addr addr_from_ie;
1161 uint32_t tei_from_ie;
1162 int ie_idx;
1163
1164 /* Fetch GSN Address and TEI from IEs */
1165 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1166 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001167 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1168 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001169 return -1;
1170 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001171 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001172 gtphub_plane_idx_names[plane_idx],
1173 gsn_addr_to_str(&addr_from_ie),
1174 addr_from_ie.len);
1175
1176 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1177 if (ie_idx < 0) {
1178 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001179 LOG(LOGL_ERROR,
1180 "Create PDP Context message invalid:"
1181 " missing IE %d\n",
1182 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001183 return -1;
1184 }
1185 tei_from_ie = 0;
1186 }
1187 else
1188 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1189
1190 /* Make sure an entry for this peer address with default port
1191 * exists */
1192 struct gtphub_peer_port *peer_from_ie =
1193 gtphub_port_have(hub, &from_bind[plane_idx],
1194 &addr_from_ie,
1195 gtphub_plane_idx_default_port[plane_idx]);
1196
1197 if (tei_from_ie) {
1198 /* Create TEI mapping and replace in GTP packet IE */
1199 uint32_t mapped_tei =
1200 gtphub_tei_mapping_have(hub, plane_idx,
1201 peer_from_ie,
1202 tei_from_ie,
1203 now);
1204 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1205 }
1206
1207 /* Replace the GSN address to reflect gtphub. */
1208 rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx);
1209 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001210 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1211 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001212 return -1;
1213 }
1214 }
1215
1216 return 0;
1217}
1218
1219static int gtphub_write(const struct osmo_fd *to,
1220 const struct osmo_sockaddr *to_addr,
1221 const uint8_t *buf, size_t buf_len)
1222{
1223 errno = 0;
1224 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1225 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001226 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001227
1228 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001229 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001230 return -EINVAL;
1231 }
1232
1233 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001234 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1235 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001236 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001237 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1238 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001239
1240 return 0;
1241}
1242
1243static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1244{
1245 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1246 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001247 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1248 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001249 if (!(what & BSC_FD_READ))
1250 return 0;
1251
1252 struct gtphub *hub = from_ggsns_ofd->data;
1253
1254 static uint8_t buf[4096];
1255 struct osmo_sockaddr from_addr;
1256 struct osmo_sockaddr to_addr;
1257 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001258 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001259 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001260
1261 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1262 if (len < 1)
1263 return 0;
1264
1265 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1266 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001267 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001268 if (len < 1)
1269 return 0;
1270
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001271 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001272}
1273
1274static int gtphub_unmap(struct gtphub *hub,
1275 struct gtp_packet_desc *p,
1276 struct gtphub_peer_port *from,
1277 struct gtphub_peer_port *to_proxy,
1278 struct gtphub_peer_port **final_unmapped,
1279 struct gtphub_peer_port **unmapped_from_seq,
1280 struct gtphub_peer_port **unmapped_from_tei)
1281{
1282 /* Always (try to) unmap sequence and TEI numbers, which need to be
1283 * replaced in the packet. Either way, give precedence to the proxy, if
1284 * configured. */
1285
1286 struct gtphub_peer_port *from_seq = NULL;
1287 struct gtphub_peer_port *from_tei = NULL;
1288 struct gtphub_peer_port *unmapped = NULL;
1289
1290 if (unmapped_from_seq)
1291 *unmapped_from_seq = from_seq;
1292 if (unmapped_from_tei)
1293 *unmapped_from_tei = from_tei;
1294 if (final_unmapped)
1295 *final_unmapped = unmapped;
1296
1297 from_seq = gtphub_unmap_seq(p, from);
1298
1299 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1300 return -1;
1301
1302 struct gtphub_peer *from_peer = from->peer_addr->peer;
1303 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001304 LOG(LOGL_DEBUG,
1305 "Seq unmap and TEI unmap yield two different peers."
1306 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001307 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1308 gtphub_plane_idx_names[p->plane_idx],
1309 gtphub_peer_str(from_peer),
1310 (int)p->seq,
1311 gtphub_port_str(from_seq),
1312 (int)p->header_tei,
1313 gtphub_port_str2(from_tei)
1314 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001315 }
1316 unmapped = (from_seq? from_seq : from_tei);
1317
1318 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001319 LOG(LOGL_NOTICE,
1320 "Unmap yields a different peer than the configured proxy."
1321 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001322 " unmapped: %s proxy: %s\n",
1323 gtphub_port_str(unmapped),
1324 gtphub_port_str2(to_proxy)
1325 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001326 }
1327 unmapped = (to_proxy? to_proxy : unmapped);
1328
1329 if (!unmapped) {
1330 /* Return no error, but returned pointers are all NULL. */
1331 return 0;
1332 }
1333
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001334 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001335 from_seq, from_tei, unmapped);
1336
1337 if (unmapped_from_seq)
1338 *unmapped_from_seq = from_seq;
1339 if (unmapped_from_tei)
1340 *unmapped_from_tei = from_tei;
1341 if (final_unmapped)
1342 *final_unmapped = unmapped;
1343 return 0;
1344}
1345
1346static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1347 uint16_t port,
1348 struct osmo_sockaddr *dst)
1349{
1350 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1351}
1352
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001353/* If p is an Echo request, replace p's data with the matching response and
1354 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1355 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001356static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1357 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001358{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001359 if (p->type != GTP_ECHO_REQ)
1360 return 0;
1361
1362 static uint8_t echo_response_data[14] = {
1363 0x32, /* flags */
1364 GTP_ECHO_RSP,
1365 0x00, 14 - 8, /* Length in network byte order */
1366 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1367 0, 0, /* Seq, to be replaced */
1368 0, 0, /* no extensions */
1369 0x0e, /* Recovery IE */
1370 0 /* Recovery counter, to be replaced */
1371 };
1372 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1373 uint8_t *recovery = &echo_response_data[13];
1374
1375 *seq = hton16(p->seq);
1376 *recovery = hub->restart_counter;
1377
1378 *reply_buf = echo_response_data;
1379
1380 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001381}
1382
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001383struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1384 const struct osmo_sockaddr *addr);
1385
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001386/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1387 * address to forward to. Return a pointer to the osmo_fd, but copy the
1388 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1389 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1390 * bytes to forward, 0 or less on failure. */
1391int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1392 unsigned int plane_idx,
1393 const struct osmo_sockaddr *from_addr,
1394 uint8_t *buf,
1395 size_t received,
1396 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001397 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001398 struct osmo_fd **to_ofd,
1399 struct osmo_sockaddr *to_addr)
1400{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001401 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001402 gtphub_plane_idx_names[plane_idx],
1403 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001404
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001405 static struct gtp_packet_desc p;
1406 gtp_decode(buf, received, plane_idx, &p);
1407
1408 if (p.rc <= 0)
1409 return -1;
1410
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001411 int reply_len;
1412 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1413 if (reply_len > 0) {
1414 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001415 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001416 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1417 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001418 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001419 if (reply_len < 0)
1420 return -1;
1421
1422 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001423
1424 /* If a GGSN proxy is configured, check that it's indeed that proxy
1425 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1426 * gtphub, so no-one else is allowed to talk to us from that side. */
1427 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1428 if (ggsn) {
1429 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001430 LOG(LOGL_ERROR,
1431 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001432 " received on GGSN bind is from another sender:"
1433 " proxy: %s sender: %s\n",
1434 gtphub_port_str(ggsn),
1435 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001436 return -1;
1437 }
1438 }
1439
1440 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001441 /* Find a GGSN peer with a matching address. The sender's port
1442 * may in fact differ. */
1443 ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx],
1444 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001445 }
1446
1447 /* If any PDP context has been created, we already have an entry for
1448 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1449 * us about. */
1450 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001451 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1452 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001453 return -1;
1454 }
1455
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001456 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001457
1458 struct gtphub_peer_port *sgsn_from_seq;
1459 struct gtphub_peer_port *sgsn;
1460 if (gtphub_unmap(hub, &p, ggsn,
1461 hub->sgsn_proxy[plane_idx],
1462 &sgsn, &sgsn_from_seq,
1463 NULL /* not interested, got it in &sgsn already */
1464 )
1465 != 0) {
1466 return -1;
1467 }
1468
1469 if (!sgsn) {
1470 /* A GGSN initiated request would go to a known TEI. So this is
1471 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001472 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001473 return -1;
1474 }
1475
1476 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001477 /* This may be a Create PDP Context response. If it is, there
1478 * are other addresses in the GTP message to set up apart from
1479 * the sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001480 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns,
1481 hub->to_sgsns, &p, now)
1482 != 0)
1483 return -1;
1484 }
1485
1486 gtphub_check_restart_counter(hub, &p, ggsn);
1487 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1488
1489 /* If the GGSN is replying to an SGSN request, the sequence nr has
1490 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1491 * create a new mapping. */
1492 if (!sgsn_from_seq)
1493 gtphub_map_seq(&p, ggsn, sgsn, now);
1494
1495 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001496
1497 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001498
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001499 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001500 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001501 return received;
1502}
1503
1504static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1505{
1506 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1507 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001508 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1509 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001510
1511 if (!(what & BSC_FD_READ))
1512 return 0;
1513
1514 struct gtphub *hub = from_sgsns_ofd->data;
1515
1516 static uint8_t buf[4096];
1517 struct osmo_sockaddr from_addr;
1518 struct osmo_sockaddr to_addr;
1519 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001520 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001521 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001522
1523 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1524 if (len < 1)
1525 return 0;
1526
1527 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1528 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001529 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001530 if (len < 1)
1531 return 0;
1532
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001533 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001534}
1535
1536/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1537int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1538 unsigned int plane_idx,
1539 const struct osmo_sockaddr *from_addr,
1540 uint8_t *buf,
1541 size_t received,
1542 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001543 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001544 struct osmo_fd **to_ofd,
1545 struct osmo_sockaddr *to_addr)
1546{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001547 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001548 gtphub_plane_idx_names[plane_idx],
1549 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001551 static struct gtp_packet_desc p;
1552 gtp_decode(buf, received, plane_idx, &p);
1553
1554 if (p.rc <= 0)
1555 return -1;
1556
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001557 int reply_len;
1558 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1559 if (reply_len > 0) {
1560 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001561 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001562 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1563 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001564 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001565 if (reply_len < 0)
1566 return -1;
1567
1568 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001569
1570 /* If an SGSN proxy is configured, check that it's indeed that proxy
1571 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1572 * gtphub, so no-one else is allowed to talk to us from that side. */
1573 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1574 if (sgsn) {
1575 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001576 LOG(LOGL_ERROR,
1577 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001578 " received on GGSN bind is from another sender:"
1579 " proxy: %s sender: %s\n",
1580 gtphub_port_str(sgsn),
1581 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001582 return -1;
1583 }
1584 }
1585
1586 if (!sgsn) {
1587 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001588 * entry for this SGSN. The port may differ. */
1589 sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx],
1590 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001591 }
1592
1593 if (!sgsn) {
1594 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1595 * first contact without being known yet, so create the peer
1596 * struct for the current sender. */
1597 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001598 LOG(LOGL_ERROR,
1599 "User plane peer was not announced by PDP Context,"
1600 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001601 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001602 return -1;
1603 }
1604
1605 struct gsn_addr from_gsna;
1606 uint16_t from_port;
1607 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1608 return -1;
1609
1610 sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx],
1611 &from_gsna, from_port);
1612 }
1613
1614 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001615 /* This could theoretically happen for invalid address data or
1616 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001617 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1618 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001619 return -1;
1620 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001621 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001622
1623 struct gtphub_peer_port *ggsn_from_seq;
1624 struct gtphub_peer_port *ggsn;
1625 if (gtphub_unmap(hub, &p, sgsn,
1626 hub->ggsn_proxy[plane_idx],
1627 &ggsn, &ggsn_from_seq,
1628 NULL /* not interested, got it in &ggsn already */
1629 )
1630 != 0) {
1631 return -1;
1632 }
1633
Neels Hofmeyra208c732015-11-11 19:26:09 +01001634 if (!ggsn) {
1635 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1636 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001637 }
1638
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001639 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001640 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001641 return -1;
1642 }
1643
1644 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001645 /* This may be a Create PDP Context requst. If it is, there are
1646 * other addresses in the GTP message to set up apart from the
1647 * sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001648 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns,
1649 hub->to_ggsns, &p, now)
1650 != 0)
1651 return -1;
1652 }
1653
1654 gtphub_check_restart_counter(hub, &p, sgsn);
1655 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1656
1657 /* If the SGSN is replying to a GGSN request, the sequence nr has
1658 * already been unmapped above (unmap_ggsn != NULL), and we need not
1659 * create a new outgoing sequence map. */
1660 if (!ggsn_from_seq)
1661 gtphub_map_seq(&p, sgsn, ggsn, now);
1662
1663 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1664
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001665 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001666
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001667 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001668 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001669 return received;
1670}
1671
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001672static void resolved_gssn_del_cb(struct expiring_item *expi)
1673{
1674 struct gtphub_resolved_ggsn *ggsn;
1675 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1676
1677 gtphub_port_ref_count_dec(ggsn->peer);
1678 llist_del(&ggsn->entry);
1679
1680 ggsn->expiry_entry.del_cb = 0;
1681 expiring_item_del(&ggsn->expiry_entry);
1682
1683 talloc_free(ggsn);
1684}
1685
1686void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1687 struct gsn_addr *resolved_addr,
1688 time_t now)
1689{
1690 struct gtphub_peer_port *pp;
1691 struct gtphub_resolved_ggsn *ggsn;
1692
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001693 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001694 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1695 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001696
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001697 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1698 resolved_addr, 2123);
1699 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001700 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1701 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001702 return;
1703 }
1704
1705 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1706 OSMO_ASSERT(ggsn);
1707
1708 ggsn->peer = pp;
1709 gtphub_port_ref_count_inc(pp);
1710
1711 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001712 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001713
1714 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1715 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1716
1717 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1718}
1719
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001720static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1721{
1722 return pp->ref_count == 0;
1723}
1724
1725static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1726{
1727 struct gtphub_peer_port *pp, *npp;
1728 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1729 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001730 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001731 gtphub_port_str(pp));
1732 gtphub_peer_port_del(pp);
1733 }
1734 }
1735 return llist_empty(&pa->ports);
1736}
1737
1738static int gtphub_gc_peer(struct gtphub_peer *p)
1739{
1740 struct gtphub_peer_addr *pa, *npa;
1741 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1742 if (gtphub_gc_peer_addr(pa)) {
1743 gtphub_peer_addr_del(pa);
1744 }
1745 }
1746
1747 /* Note that there's a ref_count in each gtphub_peer_port instance
1748 * listed within p->addresses, referenced by TEI mappings from
1749 * hub->tei_map. As long as those don't expire, this peer will stay. */
1750
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001751 return llist_empty(&p->addresses)
1752 && nr_map_empty(&p->seq_map);
1753}
1754
1755static void gtphub_gc_bind(struct gtphub_bind *b)
1756{
1757 struct gtphub_peer *p, *n;
1758 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1759 if (gtphub_gc_peer(p)) {
1760 gtphub_peer_del(p);
1761 }
1762 }
1763}
1764
1765void gtphub_gc(struct gtphub *hub, time_t now)
1766{
1767 int expired;
1768 expired = expiry_tick(&hub->expire_seq_maps, now);
1769 expired += expiry_tick(&hub->expire_tei_maps, now);
1770
1771 /* ... */
1772
1773 if (expired) {
1774 int i;
1775 for (i = 0; i < GTPH_PLANE_N; i++) {
1776 gtphub_gc_bind(&hub->to_sgsns[i]);
1777 gtphub_gc_bind(&hub->to_ggsns[i]);
1778 }
1779 }
1780}
1781
1782static void gtphub_gc_cb(void *data)
1783{
1784 struct gtphub *hub = data;
1785 gtphub_gc(hub, gtphub_now());
1786 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1787}
1788
1789static void gtphub_gc_start(struct gtphub *hub)
1790{
1791 hub->gc_timer.cb = gtphub_gc_cb;
1792 hub->gc_timer.data = hub;
1793
1794 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1795}
1796
1797/* called by unit tests */
1798void gtphub_init(struct gtphub *hub)
1799{
1800 gtphub_zero(hub);
1801
1802 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001803 expiry_init(&hub->expire_tei_maps,
1804 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001805
1806 int plane_idx;
1807 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1808 nr_pool_init(&hub->tei_pool[plane_idx]);
1809 nr_map_init(&hub->tei_map[plane_idx],
1810 &hub->tei_pool[plane_idx],
1811 &hub->expire_tei_maps);
1812
1813 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1814 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1815 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001816
1817 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1818 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1819 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1820 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001821}
1822
1823static int gtphub_make_proxy(struct gtphub *hub,
1824 struct gtphub_peer_port **pp,
1825 struct gtphub_bind *bind,
1826 const struct gtphub_cfg_addr *addr)
1827{
1828 if (!addr->addr_str)
1829 return 0;
1830
1831 struct gsn_addr gsna;
1832 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1833 return -1;
1834
1835 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1836
1837 /* This is *the* proxy. Make sure it is never expired. */
1838 gtphub_port_ref_count_inc(*pp);
1839 return 0;
1840}
1841
1842int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1843{
1844 int rc;
1845
1846 gtphub_init(hub);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001847 gtphub_ares_init(hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001848
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001849 /* TODO set hub->restart_counter from external file. */
1850
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001851 int plane_idx;
1852 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1853 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1854 &cfg->to_ggsns[plane_idx],
1855 from_ggsns_read_cb, hub, plane_idx);
1856 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001857 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1858 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001859 return rc;
1860 }
1861
1862 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1863 &cfg->to_sgsns[plane_idx],
1864 from_sgsns_read_cb, hub, plane_idx);
1865 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001866 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1867 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001868 return rc;
1869 }
1870 }
1871
1872
1873 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1874 if (gtphub_make_proxy(hub,
1875 &hub->sgsn_proxy[plane_idx],
1876 &hub->to_sgsns[plane_idx],
1877 &cfg->sgsn_proxy[plane_idx])
1878 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001879 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001880 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001881 cfg->sgsn_proxy[plane_idx].addr_str,
1882 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001883 return -1;
1884 }
1885 if (gtphub_make_proxy(hub,
1886 &hub->ggsn_proxy[plane_idx],
1887 &hub->to_ggsns[plane_idx],
1888 &cfg->ggsn_proxy[plane_idx])
1889 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001890 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001891 return -1;
1892 }
1893 }
1894
1895 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1896 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001897 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001898 gtphub_plane_idx_names[plane_idx],
1899 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1900 }
1901
1902 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001903 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001904 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001905 gtphub_plane_idx_names[plane_idx],
1906 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1907 }
1908
1909 gtphub_gc_start(hub);
1910 return 0;
1911}
1912
1913static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1914 const struct gsn_addr *addr)
1915{
1916 struct gtphub_peer_addr *a;
1917 llist_for_each_entry(a, &peer->addresses, entry) {
1918 if (gsn_addr_same(&a->addr, addr))
1919 return a;
1920 }
1921 return NULL;
1922}
1923
1924static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1925 uint16_t port)
1926{
1927 OSMO_ASSERT(port);
1928 struct gtphub_peer_port *pp;
1929 llist_for_each_entry(pp, &a->ports, entry) {
1930 if (pp->port == port)
1931 return pp;
1932 }
1933 return NULL;
1934}
1935
1936static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1937 const struct gsn_addr *addr)
1938{
1939 struct gtphub_peer *peer;
1940 llist_for_each_entry(peer, &bind->peers, entry) {
1941 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
1942 if (a)
1943 return a;
1944 }
1945 return NULL;
1946}
1947
1948static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
1949 const struct gsn_addr *addr,
1950 uint16_t port)
1951{
1952 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1953 if (!a)
1954 return NULL;
1955 return gtphub_addr_find_port(a, port);
1956}
1957
1958struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
1959 const struct osmo_sockaddr *addr)
1960{
1961 struct gsn_addr gsna;
1962 uint16_t port;
1963 gsn_addr_from_sockaddr(&gsna, &port, addr);
1964 return gtphub_port_find(bind, &gsna, port);
1965}
1966
1967static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
1968 struct gtphub_bind *bind)
1969{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001970 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
1971 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001972 OSMO_ASSERT(peer);
1973
1974 INIT_LLIST_HEAD(&peer->addresses);
1975
1976 nr_pool_init(&peer->seq_pool);
1977 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
1978
1979 /* TODO use something random to pick the initial sequence nr.
1980 0x6d31 produces the ASCII character sequence 'm1', currently used in
1981 gtphub_nc_test.sh. */
1982 peer->seq_pool.last_nr = 0x6d31 - 1;
1983
1984 llist_add(&peer->entry, &bind->peers);
1985 return peer;
1986}
1987
1988static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
1989 const struct gsn_addr *addr)
1990{
1991 struct gtphub_peer_addr *a;
1992 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
1993 OSMO_ASSERT(a);
1994 a->peer = peer;
1995 gsn_addr_copy(&a->addr, addr);
1996 INIT_LLIST_HEAD(&a->ports);
1997 llist_add(&a->entry, &peer->addresses);
1998
1999 return a;
2000}
2001
2002static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2003 struct gtphub_bind *bind,
2004 const struct gsn_addr *addr)
2005{
2006 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2007 if (a)
2008 return a;
2009
2010 /* If we haven't found an address, that means we need to create an
2011 * entirely new peer for the new address. More addresses may be added
2012 * to this peer later, but not via this function. */
2013 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002014
2015 a = gtphub_peer_add_addr(peer, addr);
2016
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002017 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002018 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002019 gsn_addr_to_str(&a->addr));
2020
2021 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002022}
2023
2024static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2025 uint16_t port)
2026{
2027 struct gtphub_peer_port *pp;
2028
2029 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2030 OSMO_ASSERT(pp);
2031 pp->peer_addr = a;
2032 pp->port = port;
2033
2034 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2035 talloc_free(pp);
2036 return NULL;
2037 }
2038
2039 llist_add(&pp->entry, &a->ports);
2040
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002041 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002042 gsn_addr_to_str(&a->addr),
2043 (int)port);
2044
2045 return pp;
2046}
2047
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002048struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2049 struct gtphub_bind *bind,
2050 const struct gsn_addr *addr,
2051 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002052{
2053 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2054
2055 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2056 if (pp)
2057 return pp;
2058
2059 return gtphub_addr_add_port(a, port);
2060}
2061
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002062/* Find a GGSN peer with a matching address. If the address is known but the
2063 * port not, create a new port for that peer address. */
2064struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2065 const struct osmo_sockaddr *addr)
2066{
2067 struct gtphub_peer_addr *pa;
2068 struct gtphub_peer_port *pp;
2069
2070 struct gsn_addr gsna;
2071 uint16_t port;
2072 gsn_addr_from_sockaddr(&gsna, &port, addr);
2073
2074 pa = gtphub_addr_find(bind, &gsna);
2075 if (!pa)
2076 return NULL;
2077
2078 pp = gtphub_addr_find_port(pa, port);
2079
2080 if (!pp)
2081 pp = gtphub_addr_add_port(pa, port);
2082
2083 return pp;
2084}
2085
2086
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002087/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2088 * resolution should be possible but failed, and 1 if resolution was
2089 * successful. *pp will be set to NULL if <1 is returned. */
2090static int gtphub_resolve_ggsn(struct gtphub *hub,
2091 struct gtp_packet_desc *p,
2092 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002093{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002094 *pp = NULL;
2095
2096 /* TODO determine from message type whether IEs should be present? */
2097
2098 int rc;
2099 const char *imsi_str;
2100 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2101 if (rc < 1)
2102 return rc;
2103 OSMO_ASSERT(imsi_str);
2104
2105 const char *apn_str;
2106 rc = get_ie_apn_str(p->ie, &apn_str);
2107 if (rc < 1)
2108 return rc;
2109 OSMO_ASSERT(apn_str);
2110
2111 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2112 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002113}
2114
2115
2116/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002117/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002118/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2119 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002120static int _osmo_getaddrinfo(struct addrinfo **result,
2121 uint16_t family, uint16_t type, uint8_t proto,
2122 const char *host, uint16_t port)
2123{
2124 struct addrinfo hints;
2125 char portbuf[16];
2126
2127 sprintf(portbuf, "%u", port);
2128 memset(&hints, '\0', sizeof(struct addrinfo));
2129 hints.ai_family = family;
2130 if (type == SOCK_RAW) {
2131 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2132 * SOCK_RAW and IPPROTO_GRE is used.
2133 */
2134 hints.ai_socktype = SOCK_DGRAM;
2135 hints.ai_protocol = IPPROTO_UDP;
2136 } else {
2137 hints.ai_socktype = type;
2138 hints.ai_protocol = proto;
2139 }
2140
2141 return getaddrinfo(host, portbuf, &hints, result);
2142}
2143
2144/* TODO move to osmocom/core/socket.c ? */
2145int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2146 uint16_t family, uint16_t type, uint8_t proto,
2147 const char *host, uint16_t port)
2148{
2149 struct addrinfo *res;
2150 int rc;
2151 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2152
2153 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002154 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002155 return -EINVAL;
2156 }
2157
2158 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2159 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2160 addr->l = res->ai_addrlen;
2161 freeaddrinfo(res);
2162
2163 return 0;
2164}
2165
2166int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2167 char *port_str, size_t port_str_len,
2168 const struct osmo_sockaddr *addr,
2169 int flags)
2170{
2171 int rc;
2172
2173 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2174 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2175 return -1;
2176 }
2177
2178 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002179 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2180 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002181 return -1;
2182 }
2183
2184 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2185 addr_str, addr_str_len,
2186 port_str, port_str_len,
2187 flags);
2188
2189 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002190 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2191 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2192 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002193
2194 return rc;
2195}
2196
2197const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2198 char *buf, size_t buf_len)
2199{
2200 const int portbuf_len = 6;
2201 OSMO_ASSERT(buf_len > portbuf_len);
2202 char *portbuf = buf + buf_len - portbuf_len;
2203 buf_len -= portbuf_len;
2204 if (osmo_sockaddr_to_strs(buf, buf_len,
2205 portbuf, portbuf_len,
2206 addr,
2207 NI_NUMERICHOST | NI_NUMERICSERV))
2208 return NULL;
2209
2210 char *pos = buf + strnlen(buf, buf_len-1);
2211 size_t len = buf_len - (pos - buf);
2212
2213 snprintf(pos, len, " port %s", portbuf);
2214 buf[buf_len-1] = '\0';
2215
2216 return buf;
2217}
2218
2219const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2220{
2221 static char buf[256];
2222 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2223 if (! result)
2224 return "(invalid)";
2225 return result;
2226}
2227
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002228int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2229 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002230{
2231 if (a == b)
2232 return 0;
2233 if (!a)
2234 return -1;
2235 if (!b)
2236 return 1;
2237 if (a->l != b->l) {
2238 /* Lengths are not the same, but determine the order. Will
2239 * anyone ever sort a list by osmo_sockaddr though...? */
2240 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2241 if (cmp == 0) {
2242 if (a->l < b->l)
2243 return -1;
2244 else
2245 return 1;
2246 }
2247 return cmp;
2248 }
2249 return memcmp(&a->a, &b->a, a->l);
2250}
2251
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002252void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2253 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002254{
2255 OSMO_ASSERT(src->l <= sizeof(dst->a));
2256 memcpy(&dst->a, &src->a, src->l);
2257 dst->l = src->l;
2258}