blob: 4507e6a149bc47e85e9cd3230ff2356e301fd11e [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
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100580void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200581{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100582 *pool = (struct nr_pool){
583 .nr_min = nr_min,
584 .nr_max = nr_max,
585 .last_nr = nr_max
586 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200587}
588
589nr_t nr_pool_next(struct nr_pool *pool)
590{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100591 if (pool->last_nr >= pool->nr_max)
592 pool->last_nr = pool->nr_min;
593 else
594 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200595
596 return pool->last_nr;
597}
598
599void nr_map_init(struct nr_map *map, struct nr_pool *pool,
600 struct expiry *exq)
601{
602 ZERO_STRUCT(map);
603 map->pool = pool;
604 map->add_items_to_expiry = exq;
605 INIT_LLIST_HEAD(&map->mappings);
606}
607
608void nr_mapping_init(struct nr_mapping *m)
609{
610 ZERO_STRUCT(m);
611 INIT_LLIST_HEAD(&m->entry);
612 expiring_item_init(&m->expiry_entry);
613}
614
615void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
616{
617 /* Generate a mapped number */
618 mapping->repl = nr_pool_next(map->pool);
619
620 /* Add to the tail to always yield a list sorted by expiry, in
621 * ascending order. */
622 llist_add_tail(&mapping->entry, &map->mappings);
623 if (map->add_items_to_expiry)
624 expiry_add(map->add_items_to_expiry,
625 &mapping->expiry_entry,
626 now);
627}
628
629void nr_map_clear(struct nr_map *map)
630{
631 struct nr_mapping *m;
632 struct nr_mapping *n;
633 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
634 nr_mapping_del(m);
635 }
636}
637
638int nr_map_empty(const struct nr_map *map)
639{
640 return llist_empty(&map->mappings);
641}
642
643struct nr_mapping *nr_map_get(const struct nr_map *map,
644 void *origin, nr_t nr_orig)
645{
646 struct nr_mapping *mapping;
647 llist_for_each_entry(mapping, &map->mappings, entry) {
648 if ((mapping->origin == origin)
649 && (mapping->orig == nr_orig))
650 return mapping;
651 }
652 /* Not found. */
653 return NULL;
654}
655
656struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
657{
658 struct nr_mapping *mapping;
659 llist_for_each_entry(mapping, &map->mappings, entry) {
660 if (mapping->repl == nr_repl) {
661 return mapping;
662 }
663 }
664 /* Not found. */
665 return NULL;
666}
667
668void nr_mapping_del(struct nr_mapping *mapping)
669{
670 OSMO_ASSERT(mapping);
671 llist_del(&mapping->entry);
672 INIT_LLIST_HEAD(&mapping->entry);
673 expiring_item_del(&mapping->expiry_entry);
674}
675
676
677/* gtphub */
678
679const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
680 "CTRL",
681 "USER",
682};
683
684const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
685 2123,
686 2152,
687};
688
689time_t gtphub_now(void)
690{
691 struct timespec now_tp;
692 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
693 return now_tp.tv_sec;
694}
695
696/* Remove a gtphub_peer from its list and free it. */
697static void gtphub_peer_del(struct gtphub_peer *peer)
698{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100699 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200700 nr_map_clear(&peer->seq_map);
701 llist_del(&peer->entry);
702 talloc_free(peer);
703}
704
705static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
706{
707 OSMO_ASSERT(llist_empty(&pa->ports));
708 llist_del(&pa->entry);
709 talloc_free(pa);
710}
711
712static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
713{
714 OSMO_ASSERT(pp->ref_count == 0);
715 llist_del(&pp->entry);
716 talloc_free(pp);
717}
718
719/* From the information in the gtp_packet_desc, return the address of a GGSN.
720 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100721static int gtphub_resolve_ggsn(struct gtphub *hub,
722 struct gtp_packet_desc *p,
723 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200724
725/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100726struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
727 const char *imsi_str,
728 const char *apn_ni_str);
729int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200730
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200731static void gtphub_zero(struct gtphub *hub)
732{
733 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100734 INIT_LLIST_HEAD(&hub->ggsn_lookups);
735 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200736}
737
738static int gtphub_sock_init(struct osmo_fd *ofd,
739 const struct gtphub_cfg_addr *addr,
740 osmo_fd_cb_t cb,
741 void *data,
742 int ofd_id)
743{
744 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100745 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200746 return -1;
747 }
748 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100749 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200750 return -1;
751 }
752
753 ofd->when = BSC_FD_READ;
754 ofd->cb = cb;
755 ofd->data = data;
756 ofd->priv_nr = ofd_id;
757
758 int rc;
759 rc = osmo_sock_init_ofd(ofd,
760 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
761 addr->addr_str, addr->port,
762 OSMO_SOCK_F_BIND);
763 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100764 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
765 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200766 return -1;
767 }
768
769 return 0;
770}
771
772static void gtphub_bind_init(struct gtphub_bind *b)
773{
774 ZERO_STRUCT(b);
775
776 INIT_LLIST_HEAD(&b->peers);
777}
778
779static int gtphub_bind_start(struct gtphub_bind *b,
780 const struct gtphub_cfg_bind *cfg,
781 osmo_fd_cb_t cb, void *cb_data,
782 unsigned int ofd_id)
783{
784 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0)
785 return -1;
786 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0)
787 return -1;
788 return 0;
789}
790
Neels Hofmeyr40348972015-11-17 12:22:28 +0100791/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200792 * Return the number of bytes read, zero on error. */
793static int gtphub_read(const struct osmo_fd *from,
794 struct osmo_sockaddr *from_addr,
795 uint8_t *buf, size_t buf_len)
796{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100797 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200798
Neels Hofmeyr40348972015-11-17 12:22:28 +0100799 /* recvfrom requires the available length set in *from_addr_len. */
800 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200801 errno = 0;
802 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100803 (struct sockaddr*)&from_addr->a,
804 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200805 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
806 * is not truncated. Then maybe reduce buf's size. */
807
808 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100809 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
810 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200811 return 0;
812 }
813
Neels Hofmeyr40348972015-11-17 12:22:28 +0100814 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
815 (int)received, osmo_sockaddr_to_str(from_addr),
816 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200817
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200818 return received;
819}
820
821inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
822{
823 OSMO_ASSERT(pp->ref_count < UINT_MAX);
824 pp->ref_count++;
825}
826
827inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
828{
829 OSMO_ASSERT(pp->ref_count > 0);
830 pp->ref_count--;
831}
832
833inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
834{
835 OSMO_ASSERT(p->version == 1);
836 p->data->gtp1l.h.seq = hton16(seq);
837 p->seq = seq;
838}
839
840inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
841{
842 OSMO_ASSERT(p->version == 1);
843 p->data->gtp1l.h.tei = hton32(tei);
844 p->header_tei = tei;
845}
846
847static void gtphub_mapping_del_cb(struct expiring_item *expi);
848
849static struct nr_mapping *gtphub_mapping_new()
850{
851 struct nr_mapping *nrm;
852 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
853 OSMO_ASSERT(nrm);
854
855 nr_mapping_init(nrm);
856 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
857 return nrm;
858}
859
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100860static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
861 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200862{
863 if (llist_empty(&peer->addresses))
864 return "(addressless)";
865
866 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
867 struct gtphub_peer_addr,
868 entry);
869 return gsn_addr_to_strb(&a->addr, buf, buflen);
870}
871
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100872static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
873 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200874{
875 if (!port)
876 return "(null port)";
877
878 snprintf(buf, buflen, "%s port %d",
879 gsn_addr_to_str(&port->peer_addr->addr),
880 (int)port->port);
881 return buf;
882}
883
884const char *gtphub_peer_str(struct gtphub_peer *peer)
885{
886 static char buf[256];
887 return gtphub_peer_strb(peer, buf, sizeof(buf));
888}
889
890const char *gtphub_peer_str2(struct gtphub_peer *peer)
891{
892 static char buf[256];
893 return gtphub_peer_strb(peer, buf, sizeof(buf));
894}
895
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100896const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200897{
898 static char buf[256];
899 return gtphub_port_strb(port, buf, sizeof(buf));
900}
901
902static const char *gtphub_port_str2(struct gtphub_peer_port *port)
903{
904 static char buf[256];
905 return gtphub_port_strb(port, buf, sizeof(buf));
906}
907
908static void gtphub_mapping_del_cb(struct expiring_item *expi)
909{
910 expi->del_cb = 0; /* avoid recursion loops */
911
912 struct nr_mapping *nrm = container_of(expi,
913 struct nr_mapping,
914 expiry_entry);
915 llist_del(&nrm->entry);
916 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
917
918 /* Just for log */
919 struct gtphub_peer_port *from = nrm->origin;
920 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100921 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200922 (int)nrm->expiry_entry.expiry,
923 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100924 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200925
926 gtphub_port_ref_count_dec(from);
927
928 talloc_free(nrm);
929}
930
931static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
932 struct gtphub_peer_port *from,
933 nr_t orig_nr,
934 time_t now)
935{
936 struct nr_mapping *nrm;
937
938 nrm = nr_map_get(map, from, orig_nr);
939
940 if (!nrm) {
941 nrm = gtphub_mapping_new();
942 nrm->orig = orig_nr;
943 nrm->origin = from;
944 nr_map_add(map, nrm, now);
945 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100946 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200947 gtphub_port_str(from),
948 (int)(nrm->orig), (int)(nrm->repl));
949 } else {
950 /* restart expiry timeout */
951 expiry_add(map->add_items_to_expiry, &nrm->expiry_entry,
952 now);
953 }
954
955 OSMO_ASSERT(nrm);
956 return nrm;
957}
958
959static uint32_t gtphub_tei_mapping_have(struct gtphub *hub,
960 int plane_idx,
961 struct gtphub_peer_port *from,
962 uint32_t orig_tei,
963 time_t now)
964{
965 struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx],
966 from, orig_tei, now);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100967 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200968 gtphub_plane_idx_names[plane_idx],
969 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100970 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200971
972 return (uint32_t)nrm->repl;
973}
974
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100975static void gtphub_map_seq(struct gtp_packet_desc *p,
976 struct gtphub_peer_port *from_port,
977 struct gtphub_peer_port *to_port,
978 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200979{
980 /* Store a mapping in to_peer's map, so when we later receive a GTP
981 * packet back from to_peer, the seq nr can be unmapped back to its
982 * origin (from_peer here). */
983 struct nr_mapping *nrm;
984 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
985 from_port, p->seq, now);
986
987 /* Change the GTP packet to yield the new, mapped seq nr */
988 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200989}
990
991static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
992 struct gtphub_peer_port *responding_port)
993{
994 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100995 struct nr_mapping *nrm =
996 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
997 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200998 if (!nrm)
999 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001000 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1001 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001002 set_seq(p, nrm->orig);
1003 return nrm->origin;
1004}
1005
1006static void gtphub_check_restart_counter(struct gtphub *hub,
1007 struct gtp_packet_desc *p,
1008 struct gtphub_peer_port *from)
1009{
1010 /* TODO */
1011 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1012 * that doesn't match the peer's previously sent restart counter, clear
1013 * that peer and cancel PDP contexts. */
1014}
1015
1016static void gtphub_map_restart_counter(struct gtphub *hub,
1017 struct gtp_packet_desc *p,
1018 struct gtphub_peer_port *from,
1019 struct gtphub_peer_port *to)
1020{
1021 /* TODO */
1022}
1023
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001024static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1025 struct gtphub *hub,
1026 struct gtp_packet_desc *p,
1027 struct gtphub_peer_port *from_port)
1028{
1029 OSMO_ASSERT(p->version == 1);
1030 *to_port_p = NULL;
1031
1032 /* If the header's TEI is zero, no PDP context has been established
1033 * yet. If nonzero, a mapping should actually already exist for this
1034 * TEI, since it must have been announced in a PDP context creation. */
1035 uint32_t tei = p->header_tei;
1036 if (!tei)
1037 return 0;
1038
1039 /* to_peer has previously announced a TEI, which was stored and
1040 * mapped in from_peer's tei_map. */
1041 struct nr_mapping *nrm;
1042 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1043 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001044 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1045 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001046 return -1;
1047 }
1048
1049 struct gtphub_peer_port *to_port = nrm->origin;
1050 uint32_t unmapped_tei = nrm->orig;
1051 set_tei(p, unmapped_tei);
1052
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001053 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1054 gtphub_port_str(from_port),
1055 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001056 gtphub_port_str2(to_port));
1057
1058 *to_port_p = to_port;
1059 return 0;
1060}
1061
1062/* Read GSN address IEs from p, and make sure these peer addresses exist in
1063 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1064 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1065 * the packet p. */
1066static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
1067 struct gtphub_bind from_bind[],
1068 struct gtphub_bind to_bind[],
1069 struct gtp_packet_desc *p,
1070 time_t now)
1071{
1072 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1073
1074 int rc;
1075 int plane_idx;
1076
1077 switch (p->type) {
1078 case GTP_CREATE_PDP_REQ:
1079 case GTP_CREATE_PDP_RSP:
1080 /* Go for it below */
1081 break;
1082 default:
1083 /* Nothing to do for this message type. */
1084 return 0;
1085 }
1086
1087 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1088
1089 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1090 plane_nrs_match_GSN_addr_IE_indices);
1091
1092 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1093 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1094
1095 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1096 struct gsn_addr addr_from_ie;
1097 uint32_t tei_from_ie;
1098 int ie_idx;
1099
1100 /* Fetch GSN Address and TEI from IEs */
1101 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1102 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001103 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1104 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001105 return -1;
1106 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001107 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001108 gtphub_plane_idx_names[plane_idx],
1109 gsn_addr_to_str(&addr_from_ie),
1110 addr_from_ie.len);
1111
1112 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1113 if (ie_idx < 0) {
1114 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001115 LOG(LOGL_ERROR,
1116 "Create PDP Context message invalid:"
1117 " missing IE %d\n",
1118 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001119 return -1;
1120 }
1121 tei_from_ie = 0;
1122 }
1123 else
1124 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1125
1126 /* Make sure an entry for this peer address with default port
1127 * exists */
1128 struct gtphub_peer_port *peer_from_ie =
1129 gtphub_port_have(hub, &from_bind[plane_idx],
1130 &addr_from_ie,
1131 gtphub_plane_idx_default_port[plane_idx]);
1132
1133 if (tei_from_ie) {
1134 /* Create TEI mapping and replace in GTP packet IE */
1135 uint32_t mapped_tei =
1136 gtphub_tei_mapping_have(hub, plane_idx,
1137 peer_from_ie,
1138 tei_from_ie,
1139 now);
1140 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1141 }
1142
1143 /* Replace the GSN address to reflect gtphub. */
1144 rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx);
1145 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001146 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1147 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001148 return -1;
1149 }
1150 }
1151
1152 return 0;
1153}
1154
1155static int gtphub_write(const struct osmo_fd *to,
1156 const struct osmo_sockaddr *to_addr,
1157 const uint8_t *buf, size_t buf_len)
1158{
1159 errno = 0;
1160 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1161 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001162 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001163
1164 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001165 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001166 return -EINVAL;
1167 }
1168
1169 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001170 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1171 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001172 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001173 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1174 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001175
1176 return 0;
1177}
1178
1179static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1180{
1181 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1182 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001183 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1184 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001185 if (!(what & BSC_FD_READ))
1186 return 0;
1187
1188 struct gtphub *hub = from_ggsns_ofd->data;
1189
1190 static uint8_t buf[4096];
1191 struct osmo_sockaddr from_addr;
1192 struct osmo_sockaddr to_addr;
1193 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001194 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001195 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001196
1197 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1198 if (len < 1)
1199 return 0;
1200
1201 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1202 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001203 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001204 if (len < 1)
1205 return 0;
1206
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001207 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001208}
1209
1210static int gtphub_unmap(struct gtphub *hub,
1211 struct gtp_packet_desc *p,
1212 struct gtphub_peer_port *from,
1213 struct gtphub_peer_port *to_proxy,
1214 struct gtphub_peer_port **final_unmapped,
1215 struct gtphub_peer_port **unmapped_from_seq,
1216 struct gtphub_peer_port **unmapped_from_tei)
1217{
1218 /* Always (try to) unmap sequence and TEI numbers, which need to be
1219 * replaced in the packet. Either way, give precedence to the proxy, if
1220 * configured. */
1221
1222 struct gtphub_peer_port *from_seq = NULL;
1223 struct gtphub_peer_port *from_tei = NULL;
1224 struct gtphub_peer_port *unmapped = NULL;
1225
1226 if (unmapped_from_seq)
1227 *unmapped_from_seq = from_seq;
1228 if (unmapped_from_tei)
1229 *unmapped_from_tei = from_tei;
1230 if (final_unmapped)
1231 *final_unmapped = unmapped;
1232
1233 from_seq = gtphub_unmap_seq(p, from);
1234
1235 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1236 return -1;
1237
1238 struct gtphub_peer *from_peer = from->peer_addr->peer;
1239 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001240 LOG(LOGL_DEBUG,
1241 "Seq unmap and TEI unmap yield two different peers."
1242 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001243 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1244 gtphub_plane_idx_names[p->plane_idx],
1245 gtphub_peer_str(from_peer),
1246 (int)p->seq,
1247 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001248 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001249 gtphub_port_str2(from_tei)
1250 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001251 }
1252 unmapped = (from_seq? from_seq : from_tei);
1253
1254 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001255 LOG(LOGL_NOTICE,
1256 "Unmap yields a different peer than the configured proxy."
1257 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001258 " unmapped: %s proxy: %s\n",
1259 gtphub_port_str(unmapped),
1260 gtphub_port_str2(to_proxy)
1261 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001262 }
1263 unmapped = (to_proxy? to_proxy : unmapped);
1264
1265 if (!unmapped) {
1266 /* Return no error, but returned pointers are all NULL. */
1267 return 0;
1268 }
1269
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001270 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001271 from_seq, from_tei, unmapped);
1272
1273 if (unmapped_from_seq)
1274 *unmapped_from_seq = from_seq;
1275 if (unmapped_from_tei)
1276 *unmapped_from_tei = from_tei;
1277 if (final_unmapped)
1278 *final_unmapped = unmapped;
1279 return 0;
1280}
1281
1282static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1283 uint16_t port,
1284 struct osmo_sockaddr *dst)
1285{
1286 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1287}
1288
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001289/* If p is an Echo request, replace p's data with the matching response and
1290 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1291 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001292static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1293 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001294{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001295 if (p->type != GTP_ECHO_REQ)
1296 return 0;
1297
1298 static uint8_t echo_response_data[14] = {
1299 0x32, /* flags */
1300 GTP_ECHO_RSP,
1301 0x00, 14 - 8, /* Length in network byte order */
1302 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1303 0, 0, /* Seq, to be replaced */
1304 0, 0, /* no extensions */
1305 0x0e, /* Recovery IE */
1306 0 /* Recovery counter, to be replaced */
1307 };
1308 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1309 uint8_t *recovery = &echo_response_data[13];
1310
1311 *seq = hton16(p->seq);
1312 *recovery = hub->restart_counter;
1313
1314 *reply_buf = echo_response_data;
1315
1316 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001317}
1318
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001319struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1320 const struct osmo_sockaddr *addr);
1321
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001322/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1323 * address to forward to. Return a pointer to the osmo_fd, but copy the
1324 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1325 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1326 * bytes to forward, 0 or less on failure. */
1327int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1328 unsigned int plane_idx,
1329 const struct osmo_sockaddr *from_addr,
1330 uint8_t *buf,
1331 size_t received,
1332 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001333 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001334 struct osmo_fd **to_ofd,
1335 struct osmo_sockaddr *to_addr)
1336{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001337 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001338 gtphub_plane_idx_names[plane_idx],
1339 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001340
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001341 static struct gtp_packet_desc p;
1342 gtp_decode(buf, received, plane_idx, &p);
1343
1344 if (p.rc <= 0)
1345 return -1;
1346
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001347 int reply_len;
1348 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1349 if (reply_len > 0) {
1350 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001351 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001352 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1353 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001354 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001355 if (reply_len < 0)
1356 return -1;
1357
1358 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001359
1360 /* If a GGSN proxy is configured, check that it's indeed that proxy
1361 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1362 * gtphub, so no-one else is allowed to talk to us from that side. */
1363 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1364 if (ggsn) {
1365 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001366 LOG(LOGL_ERROR,
1367 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001368 " received on GGSN bind is from another sender:"
1369 " proxy: %s sender: %s\n",
1370 gtphub_port_str(ggsn),
1371 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001372 return -1;
1373 }
1374 }
1375
1376 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001377 /* Find a GGSN peer with a matching address. The sender's port
1378 * may in fact differ. */
1379 ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx],
1380 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001381 }
1382
1383 /* If any PDP context has been created, we already have an entry for
1384 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1385 * us about. */
1386 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001387 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1388 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001389 return -1;
1390 }
1391
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001392 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393
1394 struct gtphub_peer_port *sgsn_from_seq;
1395 struct gtphub_peer_port *sgsn;
1396 if (gtphub_unmap(hub, &p, ggsn,
1397 hub->sgsn_proxy[plane_idx],
1398 &sgsn, &sgsn_from_seq,
1399 NULL /* not interested, got it in &sgsn already */
1400 )
1401 != 0) {
1402 return -1;
1403 }
1404
1405 if (!sgsn) {
1406 /* A GGSN initiated request would go to a known TEI. So this is
1407 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001408 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001409 return -1;
1410 }
1411
1412 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001413 /* This may be a Create PDP Context response. If it is, there
1414 * are other addresses in the GTP message to set up apart from
1415 * the sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001416 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns,
1417 hub->to_sgsns, &p, now)
1418 != 0)
1419 return -1;
1420 }
1421
1422 gtphub_check_restart_counter(hub, &p, ggsn);
1423 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1424
1425 /* If the GGSN is replying to an SGSN request, the sequence nr has
1426 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1427 * create a new mapping. */
1428 if (!sgsn_from_seq)
1429 gtphub_map_seq(&p, ggsn, sgsn, now);
1430
1431 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001432
1433 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001434
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001435 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001436 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001437 return received;
1438}
1439
1440static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1441{
1442 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1443 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001444 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1445 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001446
1447 if (!(what & BSC_FD_READ))
1448 return 0;
1449
1450 struct gtphub *hub = from_sgsns_ofd->data;
1451
1452 static uint8_t buf[4096];
1453 struct osmo_sockaddr from_addr;
1454 struct osmo_sockaddr to_addr;
1455 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001456 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001457 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001458
1459 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1460 if (len < 1)
1461 return 0;
1462
1463 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1464 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001465 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001466 if (len < 1)
1467 return 0;
1468
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001469 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001470}
1471
1472/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1473int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1474 unsigned int plane_idx,
1475 const struct osmo_sockaddr *from_addr,
1476 uint8_t *buf,
1477 size_t received,
1478 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001479 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001480 struct osmo_fd **to_ofd,
1481 struct osmo_sockaddr *to_addr)
1482{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001483 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001484 gtphub_plane_idx_names[plane_idx],
1485 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001486
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001487 static struct gtp_packet_desc p;
1488 gtp_decode(buf, received, plane_idx, &p);
1489
1490 if (p.rc <= 0)
1491 return -1;
1492
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001493 int reply_len;
1494 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1495 if (reply_len > 0) {
1496 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001497 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyr6187e012015-11-20 00:57:05 +01001498 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001499 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001500 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001501 if (reply_len < 0)
1502 return -1;
1503
1504 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001505
1506 /* If an SGSN proxy is configured, check that it's indeed that proxy
1507 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1508 * gtphub, so no-one else is allowed to talk to us from that side. */
1509 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1510 if (sgsn) {
1511 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001512 LOG(LOGL_ERROR,
1513 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001514 " received on GGSN bind is from another sender:"
1515 " proxy: %s sender: %s\n",
1516 gtphub_port_str(sgsn),
1517 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001518 return -1;
1519 }
1520 }
1521
1522 if (!sgsn) {
1523 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001524 * entry for this SGSN. The port may differ. */
1525 sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx],
1526 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001527 }
1528
1529 if (!sgsn) {
1530 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1531 * first contact without being known yet, so create the peer
1532 * struct for the current sender. */
1533 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001534 LOG(LOGL_ERROR,
1535 "User plane peer was not announced by PDP Context,"
1536 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001537 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001538 return -1;
1539 }
1540
1541 struct gsn_addr from_gsna;
1542 uint16_t from_port;
1543 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1544 return -1;
1545
1546 sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx],
1547 &from_gsna, from_port);
1548 }
1549
1550 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001551 /* This could theoretically happen for invalid address data or
1552 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001553 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1554 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001555 return -1;
1556 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001557 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001558
1559 struct gtphub_peer_port *ggsn_from_seq;
1560 struct gtphub_peer_port *ggsn;
1561 if (gtphub_unmap(hub, &p, sgsn,
1562 hub->ggsn_proxy[plane_idx],
1563 &ggsn, &ggsn_from_seq,
1564 NULL /* not interested, got it in &ggsn already */
1565 )
1566 != 0) {
1567 return -1;
1568 }
1569
Neels Hofmeyra208c732015-11-11 19:26:09 +01001570 if (!ggsn) {
1571 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1572 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001573 }
1574
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001575 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001576 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001577 return -1;
1578 }
1579
1580 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001581 /* This may be a Create PDP Context requst. If it is, there are
1582 * other addresses in the GTP message to set up apart from the
1583 * sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001584 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns,
1585 hub->to_ggsns, &p, now)
1586 != 0)
1587 return -1;
1588 }
1589
1590 gtphub_check_restart_counter(hub, &p, sgsn);
1591 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1592
1593 /* If the SGSN is replying to a GGSN request, the sequence nr has
1594 * already been unmapped above (unmap_ggsn != NULL), and we need not
1595 * create a new outgoing sequence map. */
1596 if (!ggsn_from_seq)
1597 gtphub_map_seq(&p, sgsn, ggsn, now);
1598
1599 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1600
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001601 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001602
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001603 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001604 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001605 return received;
1606}
1607
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001608static void resolved_gssn_del_cb(struct expiring_item *expi)
1609{
1610 struct gtphub_resolved_ggsn *ggsn;
1611 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1612
1613 gtphub_port_ref_count_dec(ggsn->peer);
1614 llist_del(&ggsn->entry);
1615
1616 ggsn->expiry_entry.del_cb = 0;
1617 expiring_item_del(&ggsn->expiry_entry);
1618
1619 talloc_free(ggsn);
1620}
1621
1622void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1623 struct gsn_addr *resolved_addr,
1624 time_t now)
1625{
1626 struct gtphub_peer_port *pp;
1627 struct gtphub_resolved_ggsn *ggsn;
1628
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001629 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001630 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1631 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001632
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001633 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1634 resolved_addr, 2123);
1635 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001636 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1637 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001638 return;
1639 }
1640
1641 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1642 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001643 INIT_LLIST_HEAD(&ggsn->entry);
1644 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001645
1646 ggsn->peer = pp;
1647 gtphub_port_ref_count_inc(pp);
1648
1649 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001650 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001651
1652 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1653 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1654
1655 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1656}
1657
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001658static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1659{
1660 return pp->ref_count == 0;
1661}
1662
1663static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1664{
1665 struct gtphub_peer_port *pp, *npp;
1666 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1667 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001668 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001669 gtphub_port_str(pp));
1670 gtphub_peer_port_del(pp);
1671 }
1672 }
1673 return llist_empty(&pa->ports);
1674}
1675
1676static int gtphub_gc_peer(struct gtphub_peer *p)
1677{
1678 struct gtphub_peer_addr *pa, *npa;
1679 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1680 if (gtphub_gc_peer_addr(pa)) {
1681 gtphub_peer_addr_del(pa);
1682 }
1683 }
1684
1685 /* Note that there's a ref_count in each gtphub_peer_port instance
1686 * listed within p->addresses, referenced by TEI mappings from
1687 * hub->tei_map. As long as those don't expire, this peer will stay. */
1688
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001689 return llist_empty(&p->addresses)
1690 && nr_map_empty(&p->seq_map);
1691}
1692
1693static void gtphub_gc_bind(struct gtphub_bind *b)
1694{
1695 struct gtphub_peer *p, *n;
1696 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1697 if (gtphub_gc_peer(p)) {
1698 gtphub_peer_del(p);
1699 }
1700 }
1701}
1702
1703void gtphub_gc(struct gtphub *hub, time_t now)
1704{
1705 int expired;
1706 expired = expiry_tick(&hub->expire_seq_maps, now);
1707 expired += expiry_tick(&hub->expire_tei_maps, now);
1708
1709 /* ... */
1710
1711 if (expired) {
1712 int i;
1713 for (i = 0; i < GTPH_PLANE_N; i++) {
1714 gtphub_gc_bind(&hub->to_sgsns[i]);
1715 gtphub_gc_bind(&hub->to_ggsns[i]);
1716 }
1717 }
1718}
1719
1720static void gtphub_gc_cb(void *data)
1721{
1722 struct gtphub *hub = data;
1723 gtphub_gc(hub, gtphub_now());
1724 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1725}
1726
1727static void gtphub_gc_start(struct gtphub *hub)
1728{
1729 hub->gc_timer.cb = gtphub_gc_cb;
1730 hub->gc_timer.data = hub;
1731
1732 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1733}
1734
1735/* called by unit tests */
1736void gtphub_init(struct gtphub *hub)
1737{
1738 gtphub_zero(hub);
1739
1740 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001741 expiry_init(&hub->expire_tei_maps,
1742 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001743
1744 int plane_idx;
1745 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001746 nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001747 nr_map_init(&hub->tei_map[plane_idx],
1748 &hub->tei_pool[plane_idx],
1749 &hub->expire_tei_maps);
1750
1751 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1752 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1753 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001754
1755 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1756 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1757 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1758 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001759}
1760
1761static int gtphub_make_proxy(struct gtphub *hub,
1762 struct gtphub_peer_port **pp,
1763 struct gtphub_bind *bind,
1764 const struct gtphub_cfg_addr *addr)
1765{
1766 if (!addr->addr_str)
1767 return 0;
1768
1769 struct gsn_addr gsna;
1770 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1771 return -1;
1772
1773 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1774
1775 /* This is *the* proxy. Make sure it is never expired. */
1776 gtphub_port_ref_count_inc(*pp);
1777 return 0;
1778}
1779
1780int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1781{
1782 int rc;
1783
1784 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01001785
1786 /* If a Ctrl plane proxy is configured, ares will never be used. */
1787 if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
1788 if (gtphub_ares_init(hub) != 0) {
1789 LOG(LOGL_FATAL, "Failed to initialize ares\n");
1790 return -1;
1791 }
1792 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001793
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001794 /* TODO set hub->restart_counter from external file. */
1795
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001796 int plane_idx;
1797 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1798 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1799 &cfg->to_ggsns[plane_idx],
1800 from_ggsns_read_cb, hub, plane_idx);
1801 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001802 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1803 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001804 return rc;
1805 }
1806
1807 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1808 &cfg->to_sgsns[plane_idx],
1809 from_sgsns_read_cb, hub, plane_idx);
1810 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001811 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1812 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001813 return rc;
1814 }
1815 }
1816
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001817 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1818 if (gtphub_make_proxy(hub,
1819 &hub->sgsn_proxy[plane_idx],
1820 &hub->to_sgsns[plane_idx],
1821 &cfg->sgsn_proxy[plane_idx])
1822 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001823 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001824 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001825 cfg->sgsn_proxy[plane_idx].addr_str,
1826 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001827 return -1;
1828 }
1829 if (gtphub_make_proxy(hub,
1830 &hub->ggsn_proxy[plane_idx],
1831 &hub->to_ggsns[plane_idx],
1832 &cfg->ggsn_proxy[plane_idx])
1833 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001834 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001835 return -1;
1836 }
1837 }
1838
1839 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1840 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001841 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001842 gtphub_plane_idx_names[plane_idx],
1843 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1844 }
1845
1846 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001847 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001848 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001849 gtphub_plane_idx_names[plane_idx],
1850 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1851 }
1852
1853 gtphub_gc_start(hub);
1854 return 0;
1855}
1856
1857static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1858 const struct gsn_addr *addr)
1859{
1860 struct gtphub_peer_addr *a;
1861 llist_for_each_entry(a, &peer->addresses, entry) {
1862 if (gsn_addr_same(&a->addr, addr))
1863 return a;
1864 }
1865 return NULL;
1866}
1867
1868static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1869 uint16_t port)
1870{
1871 OSMO_ASSERT(port);
1872 struct gtphub_peer_port *pp;
1873 llist_for_each_entry(pp, &a->ports, entry) {
1874 if (pp->port == port)
1875 return pp;
1876 }
1877 return NULL;
1878}
1879
1880static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1881 const struct gsn_addr *addr)
1882{
1883 struct gtphub_peer *peer;
1884 llist_for_each_entry(peer, &bind->peers, entry) {
1885 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
1886 if (a)
1887 return a;
1888 }
1889 return NULL;
1890}
1891
1892static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
1893 const struct gsn_addr *addr,
1894 uint16_t port)
1895{
1896 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1897 if (!a)
1898 return NULL;
1899 return gtphub_addr_find_port(a, port);
1900}
1901
1902struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
1903 const struct osmo_sockaddr *addr)
1904{
1905 struct gsn_addr gsna;
1906 uint16_t port;
1907 gsn_addr_from_sockaddr(&gsna, &port, addr);
1908 return gtphub_port_find(bind, &gsna, port);
1909}
1910
1911static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
1912 struct gtphub_bind *bind)
1913{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001914 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
1915 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001916 OSMO_ASSERT(peer);
1917
1918 INIT_LLIST_HEAD(&peer->addresses);
1919
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01001920 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001921 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
1922
1923 /* TODO use something random to pick the initial sequence nr.
1924 0x6d31 produces the ASCII character sequence 'm1', currently used in
1925 gtphub_nc_test.sh. */
1926 peer->seq_pool.last_nr = 0x6d31 - 1;
1927
1928 llist_add(&peer->entry, &bind->peers);
1929 return peer;
1930}
1931
1932static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
1933 const struct gsn_addr *addr)
1934{
1935 struct gtphub_peer_addr *a;
1936 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
1937 OSMO_ASSERT(a);
1938 a->peer = peer;
1939 gsn_addr_copy(&a->addr, addr);
1940 INIT_LLIST_HEAD(&a->ports);
1941 llist_add(&a->entry, &peer->addresses);
1942
1943 return a;
1944}
1945
1946static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
1947 struct gtphub_bind *bind,
1948 const struct gsn_addr *addr)
1949{
1950 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1951 if (a)
1952 return a;
1953
1954 /* If we haven't found an address, that means we need to create an
1955 * entirely new peer for the new address. More addresses may be added
1956 * to this peer later, but not via this function. */
1957 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01001958
1959 a = gtphub_peer_add_addr(peer, addr);
1960
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001961 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001962 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01001963 gsn_addr_to_str(&a->addr));
1964
1965 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001966}
1967
1968static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
1969 uint16_t port)
1970{
1971 struct gtphub_peer_port *pp;
1972
1973 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
1974 OSMO_ASSERT(pp);
1975 pp->peer_addr = a;
1976 pp->port = port;
1977
1978 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
1979 talloc_free(pp);
1980 return NULL;
1981 }
1982
1983 llist_add(&pp->entry, &a->ports);
1984
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001985 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986 gsn_addr_to_str(&a->addr),
1987 (int)port);
1988
1989 return pp;
1990}
1991
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001992struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
1993 struct gtphub_bind *bind,
1994 const struct gsn_addr *addr,
1995 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001996{
1997 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
1998
1999 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2000 if (pp)
2001 return pp;
2002
2003 return gtphub_addr_add_port(a, port);
2004}
2005
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002006/* Find a GGSN peer with a matching address. If the address is known but the
2007 * port not, create a new port for that peer address. */
2008struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2009 const struct osmo_sockaddr *addr)
2010{
2011 struct gtphub_peer_addr *pa;
2012 struct gtphub_peer_port *pp;
2013
2014 struct gsn_addr gsna;
2015 uint16_t port;
2016 gsn_addr_from_sockaddr(&gsna, &port, addr);
2017
2018 pa = gtphub_addr_find(bind, &gsna);
2019 if (!pa)
2020 return NULL;
2021
2022 pp = gtphub_addr_find_port(pa, port);
2023
2024 if (!pp)
2025 pp = gtphub_addr_add_port(pa, port);
2026
2027 return pp;
2028}
2029
2030
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002031/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2032 * resolution should be possible but failed, and 1 if resolution was
2033 * successful. *pp will be set to NULL if <1 is returned. */
2034static int gtphub_resolve_ggsn(struct gtphub *hub,
2035 struct gtp_packet_desc *p,
2036 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002037{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002038 *pp = NULL;
2039
2040 /* TODO determine from message type whether IEs should be present? */
2041
2042 int rc;
2043 const char *imsi_str;
2044 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2045 if (rc < 1)
2046 return rc;
2047 OSMO_ASSERT(imsi_str);
2048
2049 const char *apn_str;
2050 rc = get_ie_apn_str(p->ie, &apn_str);
2051 if (rc < 1)
2052 return rc;
2053 OSMO_ASSERT(apn_str);
2054
2055 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2056 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002057}
2058
2059
2060/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002061/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002062/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2063 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002064static int _osmo_getaddrinfo(struct addrinfo **result,
2065 uint16_t family, uint16_t type, uint8_t proto,
2066 const char *host, uint16_t port)
2067{
2068 struct addrinfo hints;
2069 char portbuf[16];
2070
2071 sprintf(portbuf, "%u", port);
2072 memset(&hints, '\0', sizeof(struct addrinfo));
2073 hints.ai_family = family;
2074 if (type == SOCK_RAW) {
2075 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2076 * SOCK_RAW and IPPROTO_GRE is used.
2077 */
2078 hints.ai_socktype = SOCK_DGRAM;
2079 hints.ai_protocol = IPPROTO_UDP;
2080 } else {
2081 hints.ai_socktype = type;
2082 hints.ai_protocol = proto;
2083 }
2084
2085 return getaddrinfo(host, portbuf, &hints, result);
2086}
2087
2088/* TODO move to osmocom/core/socket.c ? */
2089int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2090 uint16_t family, uint16_t type, uint8_t proto,
2091 const char *host, uint16_t port)
2092{
2093 struct addrinfo *res;
2094 int rc;
2095 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2096
2097 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002098 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002099 return -EINVAL;
2100 }
2101
2102 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2103 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2104 addr->l = res->ai_addrlen;
2105 freeaddrinfo(res);
2106
2107 return 0;
2108}
2109
2110int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2111 char *port_str, size_t port_str_len,
2112 const struct osmo_sockaddr *addr,
2113 int flags)
2114{
2115 int rc;
2116
2117 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2118 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2119 return -1;
2120 }
2121
2122 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002123 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2124 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002125 return -1;
2126 }
2127
2128 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2129 addr_str, addr_str_len,
2130 port_str, port_str_len,
2131 flags);
2132
2133 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002134 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2135 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2136 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002137
2138 return rc;
2139}
2140
2141const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2142 char *buf, size_t buf_len)
2143{
2144 const int portbuf_len = 6;
2145 OSMO_ASSERT(buf_len > portbuf_len);
2146 char *portbuf = buf + buf_len - portbuf_len;
2147 buf_len -= portbuf_len;
2148 if (osmo_sockaddr_to_strs(buf, buf_len,
2149 portbuf, portbuf_len,
2150 addr,
2151 NI_NUMERICHOST | NI_NUMERICSERV))
2152 return NULL;
2153
2154 char *pos = buf + strnlen(buf, buf_len-1);
2155 size_t len = buf_len - (pos - buf);
2156
2157 snprintf(pos, len, " port %s", portbuf);
2158 buf[buf_len-1] = '\0';
2159
2160 return buf;
2161}
2162
2163const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2164{
2165 static char buf[256];
2166 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2167 if (! result)
2168 return "(invalid)";
2169 return result;
2170}
2171
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002172int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2173 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002174{
2175 if (a == b)
2176 return 0;
2177 if (!a)
2178 return -1;
2179 if (!b)
2180 return 1;
2181 if (a->l != b->l) {
2182 /* Lengths are not the same, but determine the order. Will
2183 * anyone ever sort a list by osmo_sockaddr though...? */
2184 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2185 if (cmp == 0) {
2186 if (a->l < b->l)
2187 return -1;
2188 else
2189 return 1;
2190 }
2191 return cmp;
2192 }
2193 return memcmp(&a->a, &b->a, a->l);
2194}
2195
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002196void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2197 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002198{
2199 OSMO_ASSERT(src->l <= sizeof(dst->a));
2200 memcpy(&dst->a, &src->a, src->l);
2201 dst->l = src->l;
2202}